<commit_before>"""Top-level survey simulation manager.
"""
from __future__ import print_function, division, absolute_import
import numpy as np
import os.path
from shutil import copyfile
import datetime
from astropy.time import Time
from astropy.table import Table, vstack
import astropy.io.fits as pyfits
import astropy.units as u
import astropy.time
from surveysim.weather import weatherModule
from desisurvey.ephemerides import Ephemerides
from desisurvey.afternoonplan import surveyPlan
from surveysim.nightops import obsCount, nightOps
import desiutil.log
import desisurvey.utils

class Simulator(object):
    """Initialize a survey simulation.

    Parameters
    ----------
    start_date : datetime.date
        Survey starts on the evening of this date.
    stop_date : datetime.date
        Survey stops on the morning of this date.
    seed : int or None
        Random number seed used to generate weather conditions.
    tilesubset : array or None
        Array of integer tileIDs to use while ignoring others
        in the DESI footprint.
    use_jpl : bool
        Which avoidobject to use: astropy+jplephem if True, else pyephem.
    tile_file : string or None
        Name of FITS file that specifies previously observed tiles.
        The survey will start from scratch when None.
    """
    def __init__(self, start_date, stop_date, seed=None, tilesubset=None,
                 use_jpl=False, tile_file=None):
        self.log = desiutil.log.get_logger()
        self.use_jpl = use_jpl

        # Validate date range.
        self.num_days = (stop_date - start_date).days + 1
        if self.num_days <= 0:
            raise ValueError('Expected start_date < stop_date.')
        self.start_date = start_date
        self.stop_date = stop_date

        # Tabulate sun and moon ephemerides for each night of the survey.
        self.ephem = Ephemerides(start_date, stop_date, use_cache=True)

        # Build the survey plan.
        self.sp = surveyPlan(self.ephem.start.mjd, self.ephem.stop.mjd,
                             self.ephem, tilesubset=tilesubset)

        # Initialize the survey weather conditions generator.
        self.w = weatherModule(self.ephem.start.datetime, seed)

        # Resume a simulation using previously observed tiles, if requested.
        if tile_file is not None:
            tilesObserved = Table.read(tile_file)
            start_val = len(tilesObserved)+1
            self.log.info('Survey will resume after observing {0} tiles.'
                      .format(len(tilesObserved)))
        else:
            self.log.info('Survey will start from scratch.')
            tilesObserved = Table(
                names=('TILEID', 'STATUS'), dtype=('i8', 'i4'))
            tilesObserved.meta['MJDBEGIN'] = self.ephem.start.mjd
            start_val = 0
        self.tilesObserved = tilesObserved
        self.ocnt = obsCount(start_val)

        self.day_index = 0
        self.survey_done = False
        self.tiles_todo = self.sp.numtiles
        self.log.info(
            'Simulator initialized for {0} to {1} with {2} tiles remaining.'
            .format(start_date, stop_date, self.tiles_todo))

    def next_day(self):
        """Simulate the next day of survey operations.

        A day runs from local noon to local noon. A survey ends, with this
        method returning False, when either we reach the last scheduled day or
        else we run out of tiles to observe.

        Returns
        -------
        bool
            True if there are more days to simulate.
        """
        if self.day_index >= self.num_days or self.survey_done:
            return False

        date = self.start_date + datetime.timedelta(days=self.day_index)
        self.log.info('Simulating {0}'.format(date))

        if desisurvey.utils.is_monsoon(date):
            self.log.info('No observing during monsoon.')
        else:

            # Prepare a date string YYYYMMDD to use in filenames.
            date_string = '{y:04d}{m:02d}{d:02d}'.format(
                y=date.year, m=date.month, d=date.day)

            # Each day of observing starts at local noon.
            local_noon = desisurvey.utils.local_noon_on_date(date)

            # Lookup today's ephemerides.
            today = self.ephem.get(local_noon)
            sunset = today['MJDsunset']
            assert sunset > local_noon.mjd and sunset - local_noon.mjd < 1

            # Check if we are in the full-moon engineering period.
            if today['MoonFrac'] < 0.85:

                # Simulate a normal observing night.
                ntodate = len(self.tilesObserved)
                self.w.resetDome(local_noon.datetime)
                obsplan = self.sp.afternoonPlan(
                    today, date_string, self.tilesObserved)
                self.tilesObserved = nightOps(
                    today, date_string, obsplan, self.w, self.ocnt,
                    self.tilesObserved, use_jpl=self.use_jpl)
                ntiles_tonight = len(self.tilesObserved)-ntodate
                self.tiles_todo -= ntiles_tonight
                self.log.info('Observed {0} tiles tonight, {1} remaining.'
                              .format(ntiles_tonight, self.tiles_todo))
                if (self.sp.numtiles - len(self.tilesObserved)) == 0:
                    self.survey_done = True
            else:
                self.log.info(
                    'No observing around full moon ({0:.1f}% illuminated).'
                    .format(100 * today['MoonFrac']))

        self.day_index += 1
        if self.day_index == self.num_days:
            self.survey_done = True

        return not self.survey_done
<commit_msg>Fix stop_date error and use get_night()<commit_after>"""Top-level survey simulation manager.
"""
from __future__ import print_function, division, absolute_import
import numpy as np
import os.path
from shutil import copyfile
import datetime
from astropy.time import Time
from astropy.table import Table, vstack
import astropy.io.fits as pyfits
import astropy.units as u
import astropy.time
from surveysim.weather import weatherModule
from desisurvey.ephemerides import Ephemerides
from desisurvey.afternoonplan import surveyPlan
from surveysim.nightops import obsCount, nightOps
import desiutil.log
import desisurvey.utils

class Simulator(object):
    """Initialize a survey simulation.

    Parameters
    ----------
    start_date : datetime.date
        Survey starts on the evening of this date.
    stop_date : datetime.date
        Survey stops on the morning of this date.
    seed : int or None
        Random number seed used to generate weather conditions.
    tilesubset : array or None
        Array of integer tileIDs to use while ignoring others
        in the DESI footprint.
    use_jpl : bool
        Which avoidobject to use: astropy+jplephem if True, else pyephem.
    tile_file : string or None
        Name of FITS file that specifies previously observed tiles.
        The survey will start from scratch when None.
    """
    def __init__(self, start_date, stop_date, seed=None, tilesubset=None,
                 use_jpl=False, tile_file=None):
        self.log = desiutil.log.get_logger()
        self.use_jpl = use_jpl

        # Validate date range.
        self.num_days = (stop_date - start_date).days
        if self.num_days <= 0:
            raise ValueError('Expected start_date < stop_date.')
        self.start_date = start_date
        self.stop_date = stop_date

        # Tabulate sun and moon ephemerides for each night of the survey.
        self.ephem = Ephemerides(start_date, stop_date, use_cache=True)

        # Build the survey plan.
        self.sp = surveyPlan(self.ephem.start.mjd, self.ephem.stop.mjd,
                             self.ephem, tilesubset=tilesubset)

        # Initialize the survey weather conditions generator.
        self.w = weatherModule(self.ephem.start.datetime, seed)

        # Resume a simulation using previously observed tiles, if requested.
        if tile_file is not None:
            tilesObserved = Table.read(tile_file)
            start_val = len(tilesObserved)+1
            self.log.info('Survey will resume after observing {0} tiles.'
                      .format(len(tilesObserved)))
        else:
            self.log.info('Survey will start from scratch.')
            tilesObserved = Table(
                names=('TILEID', 'STATUS'), dtype=('i8', 'i4'))
            tilesObserved.meta['MJDBEGIN'] = self.ephem.start.mjd
            start_val = 0
        self.tilesObserved = tilesObserved
        self.ocnt = obsCount(start_val)

        self.day_index = 0
        self.survey_done = False
        self.tiles_todo = self.sp.numtiles
        self.log.info(
            'Simulator initialized for {0} to {1} with {2} tiles remaining.'
            .format(start_date, stop_date, self.tiles_todo))

    def next_day(self):
        """Simulate the next day of survey operations.

        A day runs from local noon to local noon. A survey ends, with this
        method returning False, when either we reach the last scheduled day or
        else we run out of tiles to observe.

        Returns
        -------
        bool
            True if there are more days to simulate.
        """
        if self.day_index >= self.num_days or self.survey_done:
            return False

        date = self.start_date + datetime.timedelta(days=self.day_index)
        self.log.info('Simulating {0}'.format(date))

        if desisurvey.utils.is_monsoon(date):
            self.log.info('No observing during monsoon.')
        else:

            # Prepare a date string YYYYMMDD to use in filenames.
            date_string = '{y:04d}{m:02d}{d:02d}'.format(
                y=date.year, m=date.month, d=date.day)

            # Each day of observing starts at local noon.
            local_noon = desisurvey.utils.local_noon_on_date(date)

            # Lookup today's ephemerides.
            today = self.ephem.get_night(date)
            sunset = today['MJDsunset']
            assert sunset > local_noon.mjd and sunset - local_noon.mjd < 1

            # Check if we are in the full-moon engineering period.
            if today['MoonFrac'] < 0.85:

                # Simulate a normal observing night.
                ntodate = len(self.tilesObserved)
                self.w.resetDome(local_noon.datetime)
                obsplan = self.sp.afternoonPlan(
                    today, date_string, self.tilesObserved)
                self.tilesObserved = nightOps(
                    today, date_string, obsplan, self.w, self.ocnt,
                    self.tilesObserved, use_jpl=self.use_jpl)
                ntiles_tonight = len(self.tilesObserved)-ntodate
                self.tiles_todo -= ntiles_tonight
                self.log.info('Observed {0} tiles tonight, {1} remaining.'
                              .format(ntiles_tonight, self.tiles_todo))
                if (self.sp.numtiles - len(self.tilesObserved)) == 0:
                    self.survey_done = True
            else:
                self.log.info(
                    'No observing around full moon ({0:.1f}% illuminated).'
                    .format(100 * today['MoonFrac']))

        self.day_index += 1
        if self.day_index == self.num_days:
            self.survey_done = True

        return not self.survey_done
<|endoftext|>


<commit_before>""" Processing of docstrings, which means parsing for types. """

import re

import evaluate
import parsing

DOCSTRING_PARAM_PATTERNS = [
    r'\s*:type\s+%s:\s*([^\n]+)', # Sphinx
    r'\s*@type\s+%s:\s*([^\n]+)', # Epidoc
]

DOCSTRING_RETURN_PATTERNS = [
        re.compile(r'\s*:rtype:\s*([^\n]+)', re.M), # Sphinx
        re.compile(r'\s*@rtype:\s*([^\n]+)', re.M), # Epidoc
]

#@cache.memoize_default()  # TODO add
def follow_param(param):
    func = param.parent_function
    #print func, param, param.parent_function
    param_str = search_param_in_docstr(func.docstr, str(param.get_name()))

    if param_str is not None:
        p = parsing.PyFuzzyParser(param_str, None, (1, 0), no_docstr=True)
        p.user_stmt.parent = func
        return evaluate.follow_statement(p.user_stmt)
    return []

def search_param_in_docstr(docstr, param_str):
    # look at #40 to see definitions of those params
    patterns = [ re.compile(p % re.escape(param_str)) for p in DOCSTRING_PARAM_PATTERNS ]
    for pattern in patterns:
        match = pattern.search(docstr)
        if match:
            return match.group(1)

    return None

def find_return_types(func):
    if isinstance(func, evaluate.InstanceElement):
        func = func.var

    if isinstance(func, evaluate.Function):
        func = func.base_func

    type_str = search_return_in_docstr(func.docstr)
    if not type_str:
        return []

    p = parsing.PyFuzzyParser(type_str, None, (1, 0), no_docstr=True)
    p.user_stmt.parent = func
    return list(evaluate.follow_statement(p.user_stmt))

def search_return_in_docstr(code):
    for p in DOCSTRING_RETURN_PATTERNS:
        match = p.search(code)
        if match:
            return match.group(1)
<commit_msg>Implement import for dotted types in dcostring<commit_after>""" Processing of docstrings, which means parsing for types. """

import re

import evaluate
import parsing

DOCSTRING_PARAM_PATTERNS = [
    r'\s*:type\s+%s:\s*([^\n]+)', # Sphinx
    r'\s*@type\s+%s:\s*([^\n]+)', # Epidoc
]

DOCSTRING_RETURN_PATTERNS = [
        re.compile(r'\s*:rtype:\s*([^\n]+)', re.M), # Sphinx
        re.compile(r'\s*@rtype:\s*([^\n]+)', re.M), # Epidoc
]

#@cache.memoize_default()  # TODO add
def follow_param(param):
    func = param.parent_function
    #print func, param, param.parent_function
    param_str = search_param_in_docstr(func.docstr, str(param.get_name()))
    user_position = (1, 0)

    if param_str is not None:

        # Try to import module part in dotted name.
        # (e.g., 'threading' in 'threading.Thread').
        if '.' in param_str:
            param_str = 'import {0}\n{1}'.format(
                param_str.rsplit('.', 1)[0],
                param_str)
            user_position = (2, 0)

        p = parsing.PyFuzzyParser(param_str, None, user_position,
                                  no_docstr=True)
        p.user_stmt.parent = func
        return evaluate.follow_statement(p.user_stmt)
    return []

def search_param_in_docstr(docstr, param_str):
    # look at #40 to see definitions of those params
    patterns = [ re.compile(p % re.escape(param_str)) for p in DOCSTRING_PARAM_PATTERNS ]
    for pattern in patterns:
        match = pattern.search(docstr)
        if match:
            return match.group(1)

    return None

def find_return_types(func):
    if isinstance(func, evaluate.InstanceElement):
        func = func.var

    if isinstance(func, evaluate.Function):
        func = func.base_func

    type_str = search_return_in_docstr(func.docstr)
    if not type_str:
        return []

    p = parsing.PyFuzzyParser(type_str, None, (1, 0), no_docstr=True)
    p.user_stmt.parent = func
    return list(evaluate.follow_statement(p.user_stmt))

def search_return_in_docstr(code):
    for p in DOCSTRING_RETURN_PATTERNS:
        match = p.search(code)
        if match:
            return match.group(1)
<|endoftext|>


<commit_before>import asyncio
import re
import datetime

import discord

import utils
import errors

class MentionSearchModule:
   def __init__(self, client):
      self.re_option_ch = re.compile("ch=[\w\W]+") # e.g. "ch=<#124672134>"
      self.re_option_m = re.compile("m=\d+") # e.g. "m=100"
      self.re_option_r = re.compile("r=\d+") # e.g. "m=1000"

      self._client = client
      return

   # Call this every time a message is received.
   async def on_message(msg):
      pass

   # Call this to process a command.
   async def process_cmd(self, substr, msg):
      send_as_pm = False # TYPE: Boolean
      verbose = False # TYPE: Boolean
      ch = None # TYPE: String, or None. This is a channel name, channel mention, or channel ID.
      mentions_to_get = None # TYPE: Int, or None. This is the number of mentions this function will try to fetch.
      search_range = None # TYPE: Int, or None. This is the number of messages the function will search through.

      flags = utils.parse_flags(substr)
      for flag in flags:
         if (send_as_pm == False) and ((flag == "p") or (flag == "privmsg")):
            send_as_pm = True
         elif (verbose == False) and ((flag == "v") or (flag == "verbose")):
            verbose = True
         elif (ch is None) and self.re_option_ch.fullmatch(flag):
            ch = flag[3:]
         elif (mentions_to_get == None) and self.re_option_m.fullmatch(flag):
            mentions_to_get = int(flag[2:])
         elif (search_range == None) and self.re_option_r.fullmatch(flag):
            search_range = int(flag[2:])
         else: # Invalid flag!
            raise InvalidCommandArgumentsError

      # Get channel object from ch (and handle the default value)
      if ch is None:
         channel = msg.channel
      else:
         server_to_search = msg.server
         if server_to_search == None:
            return await self._client.send_msg(msg, "Sorry, the --ch option is unusable in private channels.")
         channel = self._client.search_for_channel(ch, enablenamesearch=True, serverrestriction=server_to_search)
         if channel is None:
            return await self._client.send_msg(msg, "Channel not found. Search failed.")
      # Handle other default values or invalid inputs.
      if mentions_to_get == None:
         mentions_to_get = 3
      elif mentions_to_get == 0:
         raise InvalidCommandArgumentsError
      if search_range == None:
         search_range = 2000
      elif search_range == 0:
         raise InvalidCommandArgumentsError

      # Search
      search_results = []
      searched = 0 # Used for feedback on how many messages were searched.
      mentions_left = mentions_to_get
      search_before = None # Used for generating more logs after the limit is reached.
      while True:
         print("RETRIEVING " + str(search_range - searched) + " MESSAGES FROM " + channel.name + "...")
         await self._client.send_typing(msg.channel) # Feedback that the bot is still running.
         more_left_to_search = False
         async for retrieved_msg in self._client.logs_from(channel, limit=(search_range - searched), before=search_before):
            if searched >= search_range:
               break
            searched += 1
            print(str(searched))
            prev_retrieved_msg = retrieved_msg # Used for generating more logs after the limit is reached.
            more_left_to_search = True # Used for generating more logs after the limit is reached.
            # print("MSG: " + str(retrieved_msg))
            # TODO: _client._logs_from() is a private method. Consider removing dependence from it. or lol jk maybe not.
            if msg.author in retrieved_msg.mentions:
               search_results.append(retrieved_msg)
               mentions_left -= 1
               if mentions_left == 0:
                  break
         if (searched >= search_range) or not (more_left_to_search and (mentions_left > 0)):
            break
         search_before = prev_retrieved_msg

      # Report search results
      if len(search_results) == 0:
         buf = "No results found."
         buf += "\nLooked through " + str(searched) + " messages in <#" + channel.id + ">."
         buf += "\n(mentions_to_get=" + str(mentions_to_get) + ", range=" + str(search_range) + ")"
      else:
         mentions_found = mentions_to_get - mentions_left
         buf = "Here are your " + str(mentions_found) + " latest mentions in <#" + channel.id + ">."
         buf += "\n(mentions_to_get=" + str(mentions_to_get) + ", range="
         buf += str(search_range) + ", searched=" + str(searched) + "):"
         buf += "\n\n"
         buf += _msg_list_to_string(search_results, verbose=verbose)

      return await self._client.send_msg(msg, buf)

# TODO: There already is a copy of this function in mentionbot.py...
def _msg_list_to_string(mentions, verbose=False): # TYPE: String
   now = datetime.datetime.utcnow()
   buf = "" # FORMAT: String
   for i in mentions:
      timediff = now - i.timestamp
      if verbose:
         buf += "Message ID: " + i.id + "\n"
         # buf += "Timestamp: " + i.timestamp.strftime("%c UTC") + "\n" # Unnecessary
      buf += "By " + i.author.name + " in " + "<#{}>".format(i.channel.id) + ", " + utils.seconds_to_string(timediff.seconds) + " ago\n"
      buf += i.content + "\n\n"
   if buf != "":
      buf = buf[:-2]
   return buf

<commit_msg>(BROKEN) Pulling message logs now takes advantage of the API's extended search range over single calls.<commit_after>import asyncio
import re
import datetime

import discord

import utils
import errors

class MentionSearchModule:
   def __init__(self, client):
      self.re_option_ch = re.compile("ch=[\w\W]+") # e.g. "ch=<#124672134>"
      self.re_option_m = re.compile("m=\d+") # e.g. "m=100"
      self.re_option_r = re.compile("r=\d+") # e.g. "m=1000"

      self._client = client
      return

   # Call this every time a message is received.
   async def on_message(msg):
      pass

   # Call this to process a command.
   async def process_cmd(self, substr, msg):
      send_as_pm = False # TYPE: Boolean
      verbose = False # TYPE: Boolean
      ch = None # TYPE: String, or None. This is a channel name, channel mention, or channel ID.
      mentions_to_get = None # TYPE: Int, or None. This is the number of mentions this function will try to fetch.
      search_range = None # TYPE: Int, or None. This is the number of messages the function will search through.

      flags = utils.parse_flags(substr)
      for flag in flags:
         if (send_as_pm == False) and ((flag == "p") or (flag == "privmsg")):
            send_as_pm = True
         elif (verbose == False) and ((flag == "v") or (flag == "verbose")):
            verbose = True
         elif (ch is None) and self.re_option_ch.fullmatch(flag):
            ch = flag[3:]
         elif (mentions_to_get == None) and self.re_option_m.fullmatch(flag):
            mentions_to_get = int(flag[2:])
         elif (search_range == None) and self.re_option_r.fullmatch(flag):
            search_range = int(flag[2:])
         else: # Invalid flag!
            raise InvalidCommandArgumentsError

      # Get channel object from ch (and handle the default value)
      if ch is None:
         channel = msg.channel
      else:
         server_to_search = msg.server
         if server_to_search == None:
            return await self._client.send_msg(msg, "Sorry, the --ch option is unusable in private channels.")
         channel = self._client.search_for_channel(ch, enablenamesearch=True, serverrestriction=server_to_search)
         if channel is None:
            return await self._client.send_msg(msg, "Channel not found. Search failed.")
      # Handle other default values or invalid inputs.
      if mentions_to_get == None:
         mentions_to_get = 3
      elif mentions_to_get == 0:
         raise InvalidCommandArgumentsError
      if search_range == None:
         search_range = 2000
      elif search_range == 0:
         raise InvalidCommandArgumentsError

      # Search
      search_results = []
      searched = 0
      mentions_left = mentions_to_get
      print("MENTIONS SEARCH IS RETRIEVING {} MESSAGES.".format(str(search_range)))
      async for retrieved_msg in self._client.logs_from(channel, limit=search_range):
         searched += 1
         if msg.author in retrieved_msg.mentions:
            search_results.append(retrieved_msg)
            mentions_left -= 1
            if mentions_left == 0:
               break
      print("MENTIONS SEARCH LOOKED THROUGH {} MESSAGES.".format(str(searched)))

      # Report search results
      if len(search_results) == 0:
         buf = "No results found."
         buf += "\nLooked through " + str(searched) + " messages in <#" + channel.id + ">."
         buf += "\n(mentions_to_get=" + str(mentions_to_get) + ", range=" + str(search_range) + ")"
      else:
         mentions_found = mentions_to_get - mentions_left
         buf = "Here are your " + str(mentions_found) + " latest mentions in <#" + channel.id + ">."
         buf += "\n(mentions_to_get=" + str(mentions_to_get) + ", range="
         buf += str(search_range) + ", searched=" + str(searched) + "):"
         buf += "\n\n"
         buf += _msg_list_to_string(search_results, verbose=verbose)

      return await self._client.send_msg(msg, buf)

# TODO: There already is a copy of this function in mentionbot.py...
def _msg_list_to_string(mentions, verbose=False): # TYPE: String
   now = datetime.datetime.utcnow()
   buf = "" # FORMAT: String
   for i in mentions:
      timediff = now - i.timestamp
      if verbose:
         buf += "Message ID: " + i.id + "\n"
         # buf += "Timestamp: " + i.timestamp.strftime("%c UTC") + "\n" # Unnecessary
      buf += "By " + i.author.name + " in " + "<#{}>".format(i.channel.id) + ", " + utils.seconds_to_string(timediff.seconds) + " ago\n"
      buf += i.content + "\n\n"
   if buf != "":
      buf = buf[:-2]
   return buf

<|endoftext|>


<commit_before>import collections
from unittest import mock

import pytest
import requests

import omgorm as orm

@pytest.fixture
def resource(SessionSubclass):

    class Post(orm.Resource, session_cls=SessionSubclass):
        title = orm.Field()
        body = orm.Field()
        user = orm.Field()

    return Post

class TestField:

    @mock.patch.object(orm.Field, 'get_value')
    def test_field_accessor(self, value_getter, SessionSubclass):

        class Email(orm.Resource, session_cls=SessionSubclass):
            subject = orm.Field()

        assert isinstance(Email.subject, orm.Field)

        email = Email()
        assert email.subject is value_getter.return_value

class TestResource:

    def test_fields_linked_to_resource(self, SessionSubclass):

        class Post(orm.Resource, session_cls=SessionSubclass):
            title = orm.Field()
            body = orm.Field()
            user = orm.Field()

        assert Post.title.name == 'title'
        assert Post.title.resource is Post

        expect_fields = collections.OrderedDict([
            ('title', Post.title),
            ('body', Post.body),
            ('user', Post.user),
        ])
        assert Post.fields == expect_fields

    def test_from_api_obj(self, resource):
        Post = resource
        api_obj = object()

        instance = Post.wrap_api_obj(api_obj)
        assert isinstance(instance, Post)
        assert instance.api_obj is api_obj

    def test_resource_requires_session_cls(self):

        with pytest.raises(TypeError):
            class Poll(orm.Resource):
                pass

    def test_subclassing_keeps_fields(self, resource):
        Post = resource

        class BlogPost(Post):
            url = orm.Field()

        assert BlogPost.fields == collections.OrderedDict([
            ('title', BlogPost.title),
            ('body', BlogPost.body),
            ('user', BlogPost.user),
            ('url', BlogPost.url),
        ])

        assert BlogPost.title.resource is BlogPost

class TestSession:

    def test_resource_linked_to_session(self):

        class MySiteSession(orm.Session):
            pass

        class Post(orm.Resource, session_cls=MySiteSession):
            title = orm.Field()
            body = orm.Field()
            user = orm.Field()

        class Comment(orm.Resource, session_cls=MySiteSession):
            name = orm.Field()
            email = orm.Field()
            body = orm.Field()

        assert MySiteSession.resources == {'Post': Post, 'Comment': Comment}

    def test_init(self, SessionSubclass):

        class Post(orm.Resource, session_cls=SessionSubclass):
            title = orm.Field()
            body = orm.Field()
            user = orm.Field()

        my_session = SessionSubclass()

        assert my_session.Post is not Post
        assert issubclass(my_session.Post, Post)
        assert my_session.Post.session is my_session
        assert isinstance(my_session.requests, requests.Session)
<commit_msg>add tests for session and resource repr<commit_after>import collections
from unittest import mock

import pytest
import requests

import omgorm as orm

@pytest.fixture
def resource(SessionSubclass):

    class Post(orm.Resource, session_cls=SessionSubclass):
        title = orm.Field()
        body = orm.Field()
        user = orm.Field()

    return Post

class TestField:

    @mock.patch.object(orm.Field, 'get_value')
    def test_field_accessor(self, value_getter, SessionSubclass):

        class Email(orm.Resource, session_cls=SessionSubclass):
            subject = orm.Field()

        assert isinstance(Email.subject, orm.Field)

        email = Email()
        assert email.subject is value_getter.return_value

class TestResource:

    def test_fields_linked_to_resource(self, SessionSubclass):

        class Post(orm.Resource, session_cls=SessionSubclass):
            title = orm.Field()
            body = orm.Field()
            user = orm.Field()

        assert Post.title.name == 'title'
        assert Post.title.resource is Post

        expect_fields = collections.OrderedDict([
            ('title', Post.title),
            ('body', Post.body),
            ('user', Post.user),
        ])
        assert Post.fields == expect_fields

    def test_from_api_obj(self, resource):
        Post = resource
        api_obj = object()

        instance = Post.wrap_api_obj(api_obj)
        assert isinstance(instance, Post)
        assert instance.api_obj is api_obj

    def test_resource_requires_session_cls(self):

        with pytest.raises(TypeError):
            class Poll(orm.Resource):
                pass

    def test_subclassing_keeps_fields(self, resource):
        Post = resource

        class BlogPost(Post):
            url = orm.Field()

        assert BlogPost.fields == collections.OrderedDict([
            ('title', BlogPost.title),
            ('body', BlogPost.body),
            ('user', BlogPost.user),
            ('url', BlogPost.url),
        ])

        assert BlogPost.title.resource is BlogPost

    def test_repr(self):

        class MySession(orm.Session):

            def __str__(self):
                return 'bla'

        MySession.__module__ = 'mysite'

        class User(orm.Resource, session_cls=MySession):
            pass

            def __str__(self):
                return 'foo'

        User.__module__ = 'mysite'

        my_session = MySession()

        # class repr
        assert repr(User) == '<resource mysite.User>'

        # bound class repr
        assert repr(my_session.User) == (
            '<resource mysite.User bound to <mysite.MySession: bla>>')

        # instance repr
        user = User()
        assert repr(user) == '<mysite.User: foo>'

class TestSession:

    def test_resource_linked_to_session(self):

        class MySiteSession(orm.Session):
            pass

        class Post(orm.Resource, session_cls=MySiteSession):
            title = orm.Field()
            body = orm.Field()
            user = orm.Field()

        class Comment(orm.Resource, session_cls=MySiteSession):
            name = orm.Field()
            email = orm.Field()
            body = orm.Field()

        assert MySiteSession.resources == {'Post': Post, 'Comment': Comment}

    def test_init(self, SessionSubclass):

        class Post(orm.Resource, session_cls=SessionSubclass):
            title = orm.Field()
            body = orm.Field()
            user = orm.Field()

        my_session = SessionSubclass()

        assert my_session.Post is not Post
        assert issubclass(my_session.Post, Post)
        assert my_session.Post.session is my_session
        assert isinstance(my_session.requests, requests.Session)

    def test_repr(self):

        class MySession(orm.Session):

            def __str__(self):
                return 'foo'

        MySession.__module__ = 'mysite'

        session = MySession()
        assert repr(session) == '<mysite.MySession: foo>'
<|endoftext|>


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

import logging
import sqlite3 as sqlite
from subprocess import Popen, PIPE

# global
DATABASE = 'sbnredirect.db'

# logging
logger = logging.getLogger('sbnredirect.database')

# SELECT page_id, page_title, page_touched, page_latest, page_len FROM pagelinks, page
# WHERE pl_title="Controllo_di_autorità" AND page_id=pl_from AND page_namespace=0
# LIMIT 10;
# query = """ SELECT page_id, page_title, page_touched, page_latest, page_len
#         FROM pagelinks, page
#         WHERE pl_title="Controllo_di_autorità"
#             AND page_id=pl_from
#             AND page_namespace=0
#         """

def create():
    con = sqlite.connect(DATABASE)

    try:
        with con:
            cur = con.cursor()
            cur.execute("""CREATE TABLE
                           sbn(code TEXT PRIMARY KEY,
                               page_id BIGINT,
                               data_id BIGINT
                              )
                        """)
            cur.execute("""CREATE TABLE
                           viaf(code TEXT PRIMARY KEY,
                                page_id BIGINT,
                                data_id BIGINT
                               )
                        """)
            cur.execute("""CREATE TABLE
                           lccn(code TEXT PRIMARY KEY,
                                page_id BIGINT,
                                data_id BIGINT
                               )
                        """)
            cur.execute("""CREATE TABLE
                           Pages(id BIGINT PRIMARY KEY,
                                 title TEXT,
                                 touched BIGINT,
                                 latest BIGINT,
                                 linked TEXT)
                        """)
            cur.execute("""CREATE TABLE
                           Data(id BIGINT PRIMARY KEY,
                                title TEXT,
                                touched BIGINT,
                                latest BIGINT,
                                linked TEXT)
                        """)
    except sqlite.OperationalError as error:
        logger.error(error)

def drop():
    proc = Popen(["rm", DATABASE], stderr=PIPE)
    status = proc.wait()

    if status == 0:
        logger.info("Removed database {}".format(DATABASE))
    else:
        output = proc.stderr.read()
        logger.error("rm exited with status: {}".format(status))
        logger.error(output)

def write_codes(code_name, code_value, page=None, data=None, linked=None):

    logger.debug('value: %s' %(code_value and (page or data)) )

    if code_value and (page or data):
        con = sqlite.connect(DATABASE)

        if page is None:
            page = {}

        if data is None:
            data = {}

        query = None
        if page.get('pageid', None):
          query = """INSERT OR REPLACE INTO
                         {code_name}(code,
                                     page_id,
                                     data_id
                                    )
                          VALUES("{code_value}",
                                 {page_id},
                                 (SELECT data_id
                                  FROM {code_name}
                                  WHERE code = "{code_value}")
                               )
                  """.format(
                      code_name=code_name,
                      code_value=code_value,
                      page_id=page['pageid']
                     )

        if data.get('pageid', None):
          query = """INSERT OR REPLACE INTO
               {code_name}(code,
                           page_id,
                           data_id
                          )
                VALUES("{code_value}",
                       (SELECT page_id
                        FROM {code_name}
                        WHERE code = "{code_value}"),
                       {data_id}
                     )
                """.format(
                    code_name=code_name,
                    code_value=code_value,
                    data_id=data['pageid']
                   )

        if query:
          # logger.debug(query)
          try:
              with con:
                  cur = con.cursor()
                  cur.execute(query)
          except Exception as e:
              logger.error("Insertion of codes into table {code_name}"\
                           " failed".format(code_name=code_name))
              logger.error(query)
              logger.error(e)

          con = sqlite.connect(DATABASE)

          sources = {'Pages': page, 'Data': data}

          for s in sources:
              datum = sources[s]
              logger.debug('{s}: {datum}'.format(s=s, datum=datum))
              if datum:

                  query = """INSERT OR REPLACE INTO
                                 {table}(id,
                                         title,
                                         touched,
                                         latest,
                                         linked
                                        )
                                 VALUES({id},
                                        "{title}",
                                        {touched},
                                        {latest},
                                        {linked}
                                       )
                              """.format(
                                  table=s,
                                  id=datum['pageid'] or 'NULL',
                                  title=datum['title'].encode('utf-8') or 'NULL',
                                  touched=datum['touched'] or 'NULL',
                                  latest=datum['lastrevid'] or 'NULL',
                                  linked=linked or 'NULL'
                                 )
                  try:
                      with con:
                          cur = con.cursor()

                          cur.execute(query)
                  except Exception as e:
                      logger.error("Insertion into {table} failed".format(
                          table=s))
                      logger.error(query)
                      logger.error(e)

FIELDS = ("id", "title", "touched", "latest")

def query(table, page_id):
    con = sqlite.connect(DATABASE)

    query = """SELECT id, title, touched, latest
               FROM {table}
               WHERE id = "{page_id}"
            """.format(table=table, page_id=page_id)

    # logger.debug(query)
    data = None
    try:
        with con:
            cur = con.cursor()
            cur.execute(query)
            data = cur.fetchone()
    except Exception as e:
        logger.error("Retrieval of code {page_id} from table {table} failed"\
                     .format(page_id=page_id))
        logger.error(query)
        logger.error(e)

    if data:
        data = dict(zip(FIELDS, data))

    return data

if __name__ == '__main__':
    # logging
    LOGFORMAT_STDOUT = {logging.DEBUG: '%(module)s:%(funcName)s:%(lineno)s - %(levelname)-8s: %(message)s',
                        logging.INFO: '%(levelname)-8s: %(message)s',
                        logging.WARNING: '%(levelname)-8s: %(message)s',
                        logging.ERROR: '%(levelname)-8s: %(message)s',
                        logging.CRITICAL: '%(levelname)-8s: %(message)s'
                        }

    # --- root logger
    rootlogger = logging.getLogger('sbnredirect')
    rootlogger.setLevel(logging.DEBUG)

    lvl_config_logger = logging.DEBUG

    console = logging.StreamHandler()
    console.setLevel(lvl_config_logger)

    formatter = logging.Formatter(LOGFORMAT_STDOUT[lvl_config_logger])
    console.setFormatter(formatter)

    rootlogger.addHandler(console)

    # Alessandro Manzoni
    print query_pages('2719455')
    print
    print query_pages('14854')
    print<commit_msg>Fix encoding problem in database.py<commit_after>#! /usr/bin/env python
# -*- coding: utf-8 -*-

import logging
import sqlite3 as sqlite
from subprocess import Popen, PIPE

# global
DATABASE = 'sbnredirect.db'

# logging
logger = logging.getLogger('sbnredirect.database')

# SELECT page_id, page_title, page_touched, page_latest, page_len FROM pagelinks, page
# WHERE pl_title="Controllo_di_autorità" AND page_id=pl_from AND page_namespace=0
# LIMIT 10;
# query = """ SELECT page_id, page_title, page_touched, page_latest, page_len
#         FROM pagelinks, page
#         WHERE pl_title="Controllo_di_autorità"
#             AND page_id=pl_from
#             AND page_namespace=0
#         """

def create():
    con = sqlite.connect(DATABASE)

    try:
        with con:
            cur = con.cursor()
            cur.execute("""CREATE TABLE
                           sbn(code TEXT PRIMARY KEY,
                               page_id BIGINT,
                               data_id BIGINT
                              )
                        """)
            cur.execute("""CREATE TABLE
                           viaf(code TEXT PRIMARY KEY,
                                page_id BIGINT,
                                data_id BIGINT
                               )
                        """)
            cur.execute("""CREATE TABLE
                           lccn(code TEXT PRIMARY KEY,
                                page_id BIGINT,
                                data_id BIGINT
                               )
                        """)
            cur.execute("""CREATE TABLE
                           Pages(id BIGINT PRIMARY KEY,
                                 title TEXT,
                                 touched BIGINT,
                                 latest BIGINT,
                                 linked TEXT)
                        """)
            cur.execute("""CREATE TABLE
                           Data(id BIGINT PRIMARY KEY,
                                title TEXT,
                                touched BIGINT,
                                latest BIGINT,
                                linked TEXT)
                        """)
    except sqlite.OperationalError as error:
        logger.error(error)

def drop():
    proc = Popen(["rm", DATABASE], stderr=PIPE)
    status = proc.wait()

    if status == 0:
        logger.info("Removed database {}".format(DATABASE))
    else:
        output = proc.stderr.read()
        logger.error("rm exited with status: {}".format(status))
        logger.error(output)

def write_codes(code_name, code_value, page=None, data=None, linked=None):

    # logger.debug('value: %s' %(code_value and (page or data)) )

    if code_value and (page or data):
        con = sqlite.connect(DATABASE)

        if page is None:
            page = {}

        if data is None:
            data = {}

        query = None
        if page.get('pageid', None):
          query = """INSERT OR REPLACE INTO
                         {code_name}(code,
                                     page_id,
                                     data_id
                                    )
                          VALUES("{code_value}",
                                 {page_id},
                                 (SELECT data_id
                                  FROM {code_name}
                                  WHERE code = "{code_value}")
                               )
                  """.format(
                      code_name=code_name,
                      code_value=code_value,
                      page_id=page['pageid']
                     )

        if data.get('pageid', None):
          query = """INSERT OR REPLACE INTO
               {code_name}(code,
                           page_id,
                           data_id
                          )
                VALUES("{code_value}",
                       (SELECT page_id
                        FROM {code_name}
                        WHERE code = "{code_value}"),
                       {data_id}
                     )
                """.format(
                    code_name=code_name,
                    code_value=code_value,
                    data_id=data['pageid']
                   )

        if query:
          # logger.debug(query)
          try:
              with con:
                  cur = con.cursor()
                  cur.execute(query)
          except Exception as e:
              logger.error("Insertion of codes into table {code_name}"\
                           " failed".format(code_name=code_name))
              logger.error(query)
              logger.error(e)

          con = sqlite.connect(DATABASE)

          sources = {'Pages': page, 'Data': data}

          for s in sources:
              datum = sources[s]
              # logger.debug('{s}: {datum}'.format(s=s, datum=datum))
              if datum:

                  query = """INSERT OR REPLACE INTO
                                 {table}(id,
                                         title,
                                         touched,
                                         latest,
                                         linked
                                        )
                                 VALUES({id},
                                        "{title}",
                                        {touched},
                                        {latest},
                                        "{linked}"
                                       )
                              """.format(
                                  table=s,
                                  id=datum['pageid'] or 'NULL',
                                  title=datum['title'].encode('utf-8') or 'NULL',
                                  touched=datum['touched'] or 'NULL',
                                  latest=datum['lastrevid'] or 'NULL',
                                  linked=(linked and linked.encode('utf-8')) or 'NULL'
                                 )
                  try:
                      with con:
                          cur = con.cursor()

                          cur.execute(query)
                  except Exception as e:
                      logger.error("Insertion into {table} failed".format(
                          table=s))
                      logger.error(query)
                      logger.error(e)

FIELDS = ("id", "title", "touched", "latest")

def query(table, page_id):
    con = sqlite.connect(DATABASE)

    query = """SELECT id, title, touched, latest
               FROM {table}
               WHERE id = "{page_id}"
            """.format(table=table, page_id=page_id)

    # logger.debug(query)
    data = None
    try:
        with con:
            cur = con.cursor()
            cur.execute(query)
            data = cur.fetchone()
    except Exception as e:
        logger.error("Retrieval of code {page_id} from table {table} failed"\
                     .format(page_id=page_id))
        logger.error(query)
        logger.error(e)

    if data:
        data = dict(zip(FIELDS, data))

    return data

if __name__ == '__main__':
    # logging
    LOGFORMAT_STDOUT = {logging.DEBUG: '%(module)s:%(funcName)s:%(lineno)s - %(levelname)-8s: %(message)s',
                        logging.INFO: '%(levelname)-8s: %(message)s',
                        logging.WARNING: '%(levelname)-8s: %(message)s',
                        logging.ERROR: '%(levelname)-8s: %(message)s',
                        logging.CRITICAL: '%(levelname)-8s: %(message)s'
                        }

    # --- root logger
    rootlogger = logging.getLogger('sbnredirect')
    rootlogger.setLevel(logging.DEBUG)

    lvl_config_logger = logging.DEBUG

    console = logging.StreamHandler()
    console.setLevel(lvl_config_logger)

    formatter = logging.Formatter(LOGFORMAT_STDOUT[lvl_config_logger])
    console.setFormatter(formatter)

    rootlogger.addHandler(console)

    # Alessandro Manzoni
    print query_pages('2719455')
    print
    print query_pages('14854')
    print<|endoftext|>


<commit_before># Types which the user should never directly see
RETURN_VALUE = "<!return value>"
FUNCTION     = "<!function>"

NEXT         = "<next>"
BREAK        = "<break>"

# Normal types
NUMBER   = "<number>"
BOOLEAN  = "<boolean>"
STRING   = "<string>"
CHAR     = "<char>"
ARRAY    = "<array>"
NULL     = "<null>"
BLOCK    = "<block>"
TUPLE    = "<tuple>"
MAP      = "<map>"
CLASS    = "<class>"
INIT     = "<init method>"
METH     = "<method>"
INSTANCE = "<instance>"

class InternalObject(object):
    def __eq__(self, other):
        return type(other) == type(self)

    def __ne__(self, other):
        return not self.__eq__(other)

    def __str__(self):
        return ""

    def __repr__(self):
        return self.__str__()

class Collection(InternalObject):
    t = "<any collection>"

    def get_elements(self):
        return []

def compare(prop = "value"):
    return (lambda self, other: getattr(self, prop) == getattr(other, prop) if type(self) == type(other) else False)

def hasher():
    return (lambda self: hash(repr(self)))

class ReturnValue(InternalObject):
    t = RETURN_VALUE

    """represents a value to be returned from a function"""
    def __init__(self, value):
        self.type = RETURN_VALUE
        self.value = value

    __eq__ = compare()

    def __str__(self):
        return str(self.value)

class Number(InternalObject):
    t = NUMBER

    """represents a number object"""
    def __init__(self, value):
        self.type = NUMBER
        self.value = float(value)

    __eq__ = compare()
    __hash__ = hasher()

    def __str__(self):
        return str(self.value)

    def is_integer(self):
        return float(int(self.value)) == self.value

    def is_positive(self):
        return self.value >= 0

class Boolean(InternalObject):
    t = BOOLEAN

    """a boolean object"""
    def __init__(self, value):
        self.type = BOOLEAN
        self.value = value

    __eq__ = compare()
    __hash__ = hasher()

    def __str__(self):
        return str(self.value).lower()

class String(Collection):
    t = STRING

    """a string object"""
    def __init__(self, value):
        self.type = STRING

        if type(value) == list:
            self.value = "".join(str(e) if type(e) != Char else str(e.value) for e in value)
        else:
            self.value = value

    __eq__ = compare()
    __hash__ = hasher()

    def __str__(self):
        return self.value

    def get_elements(self):
        return [Char(ch) for ch in list(self.value)]

class Char(InternalObject):
    t = CHAR

    """a character object"""
    def __init__(self, value):
        self.type = CHAR
        self.value = value

    __eq__ = compare()
    __hash__ = hasher()

    def __str__(self):
        return "'%s'" % self.value

class Tuple(Collection):
    t = TUPLE

    """a tuple object"""
    def __init__(self, value):
        self.type = TUPLE
        self.value = value

    __eq__ = compare()
    __hash__ = hasher()

    def __str__(self):
        return "(%s)" % "".join(str(e) + ", " for e in self.value)[:-2]

    def get_elements(self):
        return self.value

class Map(InternalObject):
    t = MAP

    """an map, similar to a dictionary in python"""
    def __init__(self, pairs):
        self.type = MAP
        self.pairs = dict(pairs)

    __eq__ = compare("pairs")
    __hash__ = hasher()

    def __str__(self):
        if len(self.pairs) == 0:
            return "[:]"
        else:
            return "[%s]" % "".join("%s: %s, " % (str(key), str(value)) for key, value in self.pairs.items())[:-2]

    def __getitem__(self, key):
        return self.pairs.get(String(key), Null())

    def __setitem__(self, key, val):
        self.pairs[String(key)] = val

class Null(InternalObject):
    t = NULL

    """the null object"""
    def __init__(self):
        self.type = NULL

    __hash__ = hasher()

    def __str__(self):
        return "null"

class Array(Collection):
    t = ARRAY

    """an array object"""
    def __init__(self, elements):
        self.type = ARRAY
        self.elements = elements

    __eq__ = compare("elements")
    __hash__ = hasher()

    def __str__(self):
        return "[%s]" % "".join(str(e) + ", " for e in self.elements)[:-2]

    def get_elements(self):
        return self.elements

class Function(InternalObject):
    t = FUNCTION

    """a function object"""
    def __init__(self, pattern, body, context):
        self.type = FUNCTION
        self.pattern = pattern # [id|param]
        self.body = body
        self.context = context

    def __str__(self):
        return "<function instance>"

    def on_call(self, args, ctx, enclosed):
        pass

class Block(InternalObject):
    t = BLOCK

    """a block object"""
    def __init__(self, params, body):
        self.type = BLOCK
        self.params = params
        self.body = body

    def __str__(self):
        return "<block instance>"

class Next(InternalObject):
    t = NEXT

    """result of the next; statement"""
    def __init__(self):
        self.type = NEXT

    def __str__(self):
        return "<next>"

class Break(InternalObject):
    t = BREAK

    """result of the break; statement"""
    def __init__(self):
        self.type = BREAK

    def __str__(self):
        return "<break>"

class InitMethod(InternalObject):
    t = INIT

    """an initialisation method of a class"""
    def __init__(self, fn):
        self.type = INIT
        self.fn = fn

    def __str__(self):
        return "<init method instance>"

class Method(InternalObject):
    t = METH

    """a normal method of a class"""
    def __init__(self, fn):
        self.type = METH
        self.fn = fn

    def __str__(self):
        return "<method instance>"

class Class(InternalObject):
    t = CLASS

    """a class object"""
    def __init__(self, name, parent, methods):
        self.type = CLASS
        self.name = name
        self.parent = parent
        self.methods = methods

    def __str__(self):
        return "<class '%s'>" % self.name

    def get_methods(self):
        methods = []

        if self.parent != None:
            methods = self.parent.get_methods()

        return [meth for meth in self.methods if isinstance(meth, Method)] + methods

class Instance(InternalObject):
    t = INSTANCE

    """an instance of a class """
    def __init__(self, base):
        self.type = INSTANCE
        self.base = base
        self.data = {}

    def __str__(self):
        return "%s: %s" % (self.base.name, "".join(str(o) + " " for o in self.data.values())[:-1])

    def __getitem__(self, key):
        return self.data.get(key, Null())

    def __setitem__(self, key, val):
        self.data[key] = val
<commit_msg>Change default instance __str__<commit_after># Types which the user should never directly see
RETURN_VALUE = "<!return value>"
FUNCTION     = "<!function>"

NEXT         = "<next>"
BREAK        = "<break>"

# Normal types
NUMBER   = "<number>"
BOOLEAN  = "<boolean>"
STRING   = "<string>"
CHAR     = "<char>"
ARRAY    = "<array>"
NULL     = "<null>"
BLOCK    = "<block>"
TUPLE    = "<tuple>"
MAP      = "<map>"
CLASS    = "<class>"
INIT     = "<init method>"
METH     = "<method>"
INSTANCE = "<instance>"

class InternalObject(object):
    def __eq__(self, other):
        return type(other) == type(self)

    def __ne__(self, other):
        return not self.__eq__(other)

    def __str__(self):
        return ""

    def __repr__(self):
        return self.__str__()

class Collection(InternalObject):
    t = "<any collection>"

    def get_elements(self):
        return []

def compare(prop = "value"):
    return (lambda self, other: getattr(self, prop) == getattr(other, prop) if type(self) == type(other) else False)

def hasher():
    return (lambda self: hash(repr(self)))

class ReturnValue(InternalObject):
    t = RETURN_VALUE

    """represents a value to be returned from a function"""
    def __init__(self, value):
        self.type = RETURN_VALUE
        self.value = value

    __eq__ = compare()

    def __str__(self):
        return str(self.value)

class Number(InternalObject):
    t = NUMBER

    """represents a number object"""
    def __init__(self, value):
        self.type = NUMBER
        self.value = float(value)

    __eq__ = compare()
    __hash__ = hasher()

    def __str__(self):
        return str(self.value)

    def is_integer(self):
        return float(int(self.value)) == self.value

    def is_positive(self):
        return self.value >= 0

class Boolean(InternalObject):
    t = BOOLEAN

    """a boolean object"""
    def __init__(self, value):
        self.type = BOOLEAN
        self.value = value

    __eq__ = compare()
    __hash__ = hasher()

    def __str__(self):
        return str(self.value).lower()

class String(Collection):
    t = STRING

    """a string object"""
    def __init__(self, value):
        self.type = STRING

        if type(value) == list:
            self.value = "".join(str(e) if type(e) != Char else str(e.value) for e in value)
        else:
            self.value = value

    __eq__ = compare()
    __hash__ = hasher()

    def __str__(self):
        return self.value

    def get_elements(self):
        return [Char(ch) for ch in list(self.value)]

class Char(InternalObject):
    t = CHAR

    """a character object"""
    def __init__(self, value):
        self.type = CHAR
        self.value = value

    __eq__ = compare()
    __hash__ = hasher()

    def __str__(self):
        return "'%s'" % self.value

class Tuple(Collection):
    t = TUPLE

    """a tuple object"""
    def __init__(self, value):
        self.type = TUPLE
        self.value = value

    __eq__ = compare()
    __hash__ = hasher()

    def __str__(self):
        return "(%s)" % "".join(str(e) + ", " for e in self.value)[:-2]

    def get_elements(self):
        return self.value

class Map(InternalObject):
    t = MAP

    """an map, similar to a dictionary in python"""
    def __init__(self, pairs):
        self.type = MAP
        self.pairs = dict(pairs)

    __eq__ = compare("pairs")
    __hash__ = hasher()

    def __str__(self):
        if len(self.pairs) == 0:
            return "[:]"
        else:
            return "[%s]" % "".join("%s: %s, " % (str(key), str(value)) for key, value in self.pairs.items())[:-2]

    def __getitem__(self, key):
        return self.pairs.get(String(key), Null())

    def __setitem__(self, key, val):
        self.pairs[String(key)] = val

class Null(InternalObject):
    t = NULL

    """the null object"""
    def __init__(self):
        self.type = NULL

    __hash__ = hasher()

    def __str__(self):
        return "null"

class Array(Collection):
    t = ARRAY

    """an array object"""
    def __init__(self, elements):
        self.type = ARRAY
        self.elements = elements

    __eq__ = compare("elements")
    __hash__ = hasher()

    def __str__(self):
        return "[%s]" % "".join(str(e) + ", " for e in self.elements)[:-2]

    def get_elements(self):
        return self.elements

class Function(InternalObject):
    t = FUNCTION

    """a function object"""
    def __init__(self, pattern, body, context):
        self.type = FUNCTION
        self.pattern = pattern # [id|param]
        self.body = body
        self.context = context

    def __str__(self):
        return "<function instance>"

    def on_call(self, args, ctx, enclosed):
        pass

class Block(InternalObject):
    t = BLOCK

    """a block object"""
    def __init__(self, params, body):
        self.type = BLOCK
        self.params = params
        self.body = body

    def __str__(self):
        return "<block instance>"

class Next(InternalObject):
    t = NEXT

    """result of the next; statement"""
    def __init__(self):
        self.type = NEXT

    def __str__(self):
        return "<next>"

class Break(InternalObject):
    t = BREAK

    """result of the break; statement"""
    def __init__(self):
        self.type = BREAK

    def __str__(self):
        return "<break>"

class InitMethod(InternalObject):
    t = INIT

    """an initialisation method of a class"""
    def __init__(self, fn):
        self.type = INIT
        self.fn = fn

    def __str__(self):
        return "<init method instance>"

class Method(InternalObject):
    t = METH

    """a normal method of a class"""
    def __init__(self, fn):
        self.type = METH
        self.fn = fn

    def __str__(self):
        return "<method instance>"

class Class(InternalObject):
    t = CLASS

    """a class object"""
    def __init__(self, name, parent, methods):
        self.type = CLASS
        self.name = name
        self.parent = parent
        self.methods = methods

    def __str__(self):
        return "<class '%s'>" % self.name

    def get_methods(self):
        methods = []

        if self.parent != None:
            methods = self.parent.get_methods()

        return [meth for meth in self.methods if isinstance(meth, Method)] + methods

class Instance(InternalObject):
    t = INSTANCE

    """an instance of a class """
    def __init__(self, base):
        self.type = INSTANCE
        self.base = base
        self.data = {}

    def __str__(self):
        return "%s (%s)" % (self.base.name, "".join(str(o) + ", " for o in self.data.values())[:-2])

    def __getitem__(self, key):
        return self.data.get(key, Null())

    def __setitem__(self, key, val):
        self.data[key] = val
<|endoftext|>


<commit_before>import numpy

import quadpy

def test_u2():
    scheme = quadpy.u2.krylov(3)
    scheme.integrate(lambda x: numpy.exp(x[0]), numpy.array([0.0, 0.3]), 0.7)
    scheme = quadpy.u2.krylov(5)
    scheme.integrate(
        lambda x: [numpy.exp(x[0]), numpy.exp(x[0])],
        numpy.array([[1.0, 1.0], [0.0, 0.3], [2.0, 2.0]]),
        [1.0, 0.7, 0.333],
    )

def test_s2():
    scheme = quadpy.s2.peirce_1957(5)
    scheme.integrate(lambda x: numpy.exp(x[0]), numpy.array([0.0, 0.3]), 0.7)
    scheme = quadpy.s2.peirce_1957(5)
    scheme.integrate(
        lambda x: [numpy.exp(x[0]), numpy.exp(x[1])],
        numpy.array([[1.0, 1.0], [0.0, 0.3], [2.0, 2.0]]),
        [1.0, 0.7, 0.333],
    )

def test_c3():
    scheme = quadpy.c3.product(quadpy.c1.newton_cotes_closed(3))
    val = scheme.integrate(
        lambda x: numpy.exp(x[0]),
        quadpy.c3.cube_points([0.0, 1.0], [0.0, 1.0], [0.0, 1.0]),
    )

    scheme = quadpy.c3.product(quadpy.c1.newton_cotes_closed(3))
    val = scheme.integrate(
        lambda x: [numpy.exp(x[0]), numpy.exp(x[1])],
        numpy.stack(
            [
                quadpy.c3.cube_points([0, 1], [0, 1], [0, 1]),
                quadpy.c3.cube_points([0, 1], [0, 1], [0, 1]),
                quadpy.c3.cube_points([0, 1], [0, 1], [0, 1]),
                quadpy.c3.cube_points([0, 1], [0, 1], [0, 1]),
                quadpy.c3.cube_points([0, 1], [0, 1], [0, 1]),
            ],
            axis=-2,
        ),
    )
    assert val.shape == (2, 5)

def test_c1():
    quadpy.c1.newton_cotes_closed(4).integrate(numpy.exp, [0.0, 1.0])
    quadpy.c1.newton_cotes_closed(4).integrate(numpy.exp, numpy.array([[0.0], [1.0]]))
    quadpy.c1.newton_cotes_closed(4).integrate(
        lambda x: [numpy.exp(x), numpy.sin(x), numpy.cos(x)],
        numpy.array([[0.0, 1.0], [1.0, 2.0]]),
    )

def test_p3():
    scheme = quadpy.p3.felippa_3()
    scheme.integrate(
        lambda x: numpy.exp(x[0]),
        numpy.array([[0, 0, 0], [1, 0, 0], [1, 1, 0], [0, 1, 0], [0, 0, 1]]),
    )

    scheme = quadpy.p3.felippa_5()
    scheme.integrate(
        lambda x: [numpy.exp(x[0]), numpy.exp(x[1])],
        numpy.array(
            [
                [[0, 0, 0], [0, 0, 0], [0, 0, 0], [0, 0, 0], [0, 0, 0]],
                [[1, 0, 0], [1, 0, 0], [1, 0, 0], [1, 0, 0], [1, 0, 0]],
                [[1, 1, 0], [1, 1, 0], [1, 1, 0], [1, 1, 0], [1, 1, 0]],
                [[0, 1, 0], [0, 1, 0], [0, 1, 0], [0, 1, 0], [0, 1, 0]],
                [[0, 0, 1], [0, 0, 1], [0, 0, 1], [0, 0, 1], [0, 0, 1]],
            ]
        ),
    )

def test_c2():
    quadpy.c2.stroud_c2_5_4().integrate(
        lambda x: numpy.exp(x[0]), quadpy.c2.rectangle_points([0.0, 1.0], [0.0, 1.0]),
    )

    val = quadpy.c2.stroud_c2_3_1().integrate(
        lambda x: [numpy.exp(x[0]), numpy.exp(x[1])],
        numpy.stack(
            [
                quadpy.c2.rectangle_points([0.0, 1.0], [0.0, 1.0]),
                quadpy.c2.rectangle_points([0.0, 1.0], [0.0, 1.0]),
                quadpy.c2.rectangle_points([0.0, 1.0], [0.0, 1.0]),
                quadpy.c2.rectangle_points([0.0, 1.0], [0.0, 1.0]),
                quadpy.c2.rectangle_points([0.0, 1.0], [0.0, 1.0]),
            ],
            axis=-2,
        ),
    )
    assert val.shape == (2, 5)

def test_e2r2():
    scheme = quadpy.e2r2.rabinowitz_richter_3()
    scheme.integrate(lambda x: numpy.exp(x[0]))

def test_e2r():
    scheme = quadpy.e2r.rabinowitz_richter_5()
    scheme.integrate(lambda x: numpy.exp(x[0]))

def test_u3():
    quadpy.u3.lebedev_003a().integrate(
        lambda x: numpy.exp(1j * x[0]) + 1j * x[0] ** 2,
        numpy.array([0.0, 0.3, 0.0]),
        0.7,
    )

    # quadpy.u3.lebedev_003a().integrate(
    #     lambda x: [numpy.exp(x[0]), numpy.exp(x[1])],
    #     numpy.array([[1.0, 1.0, 0.0], [0.0, 0.3, 0.0], [2.0, 2.0, 0.0]]),
    #     [1.0, 0.7, 0.333],
    # )

def test_s3():
    scheme = quadpy.s3.hammer_stroud_15_3a()
    scheme.integrate(lambda x: numpy.exp(x[0]), [0.0, 0.0, 0.0], 1.0)

    scheme = quadpy.s3.hammer_stroud_15_3b()
    scheme.integrate(
        lambda x: [numpy.exp(x[0]), numpy.exp(x[1])],
        [[1.0, 1.0, 0.0], [0.0, 0.3, 0.0], [2.0, 2.0, 0.0]],
        [1.0, 0.7, 0.333],
    )

def test_t3():
    quadpy.t3.shunn_ham_3().integrate(
        lambda x: numpy.exp(x[0]),
        numpy.array([[0, 0, 0], [1, 0, 0], [0, 1, 0], [0, 0, 1]], dtype=float),
    )

    quadpy.t3.shunn_ham_3().integrate(
        lambda x: [numpy.exp(x[0]), numpy.exp(x[1])],
        numpy.stack(
            [
                [[0.0, 0.0, 0.0], [1.0, 0.0, 0.0], [0.0, 1.0, 0], [0.0, 0, 1]],
                [[0.0, 0.0, 0.0], [1.0, 0.0, 0.0], [0.0, 1.0, 0], [0.0, 0, 1]],
                [[0.0, 0.0, 0.0], [1.0, 0.0, 0.0], [0.0, 1.0, 0], [0.0, 0, 1]],
                [[0.0, 0.0, 0.0], [1.0, 0.0, 0.0], [0.0, 1.0, 0], [0.0, 0, 1]],
            ],
            axis=-2,
        ),
    )

def test_t2():
    quadpy.t2.cubtri().integrate(
        lambda x: numpy.exp(x[0]), numpy.array([[0.0, 0.0], [1.0, 0.0], [0.0, 1.0]])
    )

    val = quadpy.t2.cubtri().integrate(
        lambda x: [numpy.exp(x[0]), numpy.exp(x[1])],
        numpy.stack(
            [
                [[0.0, 0.0], [1.0, 0.0], [0.0, 1.0]],
                [[0.0, 0.0], [1.0, 0.0], [0.0, 1.0]],
                [[0.0, 0.0], [1.0, 0.0], [0.0, 1.0]],
                [[0.0, 0.0], [1.0, 0.0], [0.0, 1.0]],
                [[0.0, 0.0], [1.0, 0.0], [0.0, 1.0]],
            ],
            axis=-2,
        ),
    )
    assert val.shape == (2, 5)

def test_w3():
    quadpy.w3.felippa_4().integrate(
        lambda x: numpy.exp(x[0]),
        numpy.array(
            [[[0, 0, 0], [1, 0, 0], [0, 1, 0]], [[0, 0, 1], [1, 0, 1], [0, 1, 1]]]
        ),
    )

    val = quadpy.w3.felippa_4().integrate(
        lambda x: [numpy.exp(x[0]), numpy.exp(x[1])],
        numpy.stack(
            [
                [[[0, 0, 0], [1, 0, 0], [0, 1, 0]], [[0, 0, 1], [1, 0, 1], [0, 1, 1]]],
                [[[0, 0, 0], [1, 0, 0], [0, 1, 0]], [[0, 0, 1], [1, 0, 1], [0, 1, 1]]],
                [[[0, 0, 0], [1, 0, 0], [0, 1, 0]], [[0, 0, 1], [1, 0, 1], [0, 1, 1]]],
            ],
            axis=-2,
        ),
    )
    assert val.shape == (2, 3)

def test_tn():
    dim = 4
    quadpy.tn.grundmann_moeller(dim, 3).integrate(
        lambda x: numpy.exp(x[0]),
        numpy.array(
            [
                [0.0, 0.0, 0.0, 0.0],
                [1.0, 2.0, 0.0, 0.0],
                [0.0, 1.0, 0.0, 0.0],
                [0.0, 3.0, 1.0, 0.0],
                [0.0, 0.0, 4.0, 1.0],
            ]
        ),
    )

def test_sn():
    dim = 4
    quadpy.sn.dobrodeev_1970(dim).integrate(
        lambda x: numpy.exp(x[0]), numpy.zeros(4), 1.0
    )

def test_cn():
    dim = 4
    quadpy.cn.stroud_cn_3_3(dim).integrate(
        lambda x: numpy.exp(x[0]),
        quadpy.cn.ncube_points([0.0, 1.0], [0.1, 0.9], [-1.0, 1.0], [-1.0, -0.5]),
    )

def test_un():
    dim = 4
    quadpy.un.dobrodeev_1978(dim).integrate(
        lambda x: numpy.exp(x[0]), numpy.zeros(dim), 1.0
    )

def test_enr2():
    dim = 4
    quadpy.enr2.stroud_enr2_5_2(dim).integrate(lambda x: numpy.exp(x[0]))

def test_e1r():
    scheme = quadpy.e1r.gauss_laguerre(5)
    scheme.integrate(lambda x: x[0] ** 2)

def test_e3r():
    quadpy.e3r.stroud_secrest_09().integrate(lambda x: numpy.exp(x[0]))

def test_e1r2():
    scheme = quadpy.e1r2.gauss_hermite(5)
    scheme.integrate(lambda x: x[0] ** 2)

if __name__ == "__main__":
    test_u3()
<commit_msg>fix example tests<commit_after>import numpy

import quadpy

def test_u2():
    scheme = quadpy.u2.krylov(3)
    scheme.integrate(lambda x: numpy.exp(x[0]), numpy.array([0.0, 0.3]), 0.7)
    scheme = quadpy.u2.krylov(5)
    scheme.integrate(
        lambda x: [numpy.exp(x[0]), numpy.exp(x[0])],
        numpy.array([[1.0, 1.0], [0.0, 0.3], [2.0, 2.0]]),
        [1.0, 0.7, 0.333],
    )

def test_s2():
    scheme = quadpy.s2.peirce_1957(5)
    scheme.integrate(lambda x: numpy.exp(x[0]), numpy.array([0.0, 0.3]), 0.7)
    scheme = quadpy.s2.peirce_1957(5)
    scheme.integrate(
        lambda x: [numpy.exp(x[0]), numpy.exp(x[1])],
        numpy.array([[1.0, 1.0], [0.0, 0.3], [2.0, 2.0]]),
        [1.0, 0.7, 0.333],
    )

def test_c3():
    scheme = quadpy.c3.product(quadpy.c1.newton_cotes_closed(3))
    val = scheme.integrate(
        lambda x: numpy.exp(x[0]),
        quadpy.c3.cube_points([0.0, 1.0], [0.0, 1.0], [0.0, 1.0]),
    )

    scheme = quadpy.c3.product(quadpy.c1.newton_cotes_closed(3))
    val = scheme.integrate(
        lambda x: [numpy.exp(x[0]), numpy.exp(x[1])],
        numpy.stack(
            [
                quadpy.c3.cube_points([0, 1], [0, 1], [0, 1]),
                quadpy.c3.cube_points([0, 1], [0, 1], [0, 1]),
                quadpy.c3.cube_points([0, 1], [0, 1], [0, 1]),
                quadpy.c3.cube_points([0, 1], [0, 1], [0, 1]),
                quadpy.c3.cube_points([0, 1], [0, 1], [0, 1]),
            ],
            axis=-2,
        ),
    )
    assert val.shape == (2, 5)

def test_c1():
    quadpy.c1.newton_cotes_closed(4).integrate(numpy.exp, [0.0, 1.0])
    quadpy.c1.newton_cotes_closed(4).integrate(numpy.exp, numpy.array([[0.0], [1.0]]))
    quadpy.c1.newton_cotes_closed(4).integrate(
        lambda x: [numpy.exp(x), numpy.sin(x), numpy.cos(x)],
        numpy.array([[0.0, 1.0], [1.0, 2.0]]),
    )

def test_p3():
    scheme = quadpy.p3.felippa_3()
    scheme.integrate(
        lambda x: numpy.exp(x[0]),
        numpy.array([[0, 0, 0], [1, 0, 0], [1, 1, 0], [0, 1, 0], [0, 0, 1]]),
    )

    scheme = quadpy.p3.felippa_5()
    scheme.integrate(
        lambda x: [numpy.exp(x[0]), numpy.exp(x[1])],
        numpy.array(
            [
                [[0, 0, 0], [0, 0, 0], [0, 0, 0], [0, 0, 0], [0, 0, 0]],
                [[1, 0, 0], [1, 0, 0], [1, 0, 0], [1, 0, 0], [1, 0, 0]],
                [[1, 1, 0], [1, 1, 0], [1, 1, 0], [1, 1, 0], [1, 1, 0]],
                [[0, 1, 0], [0, 1, 0], [0, 1, 0], [0, 1, 0], [0, 1, 0]],
                [[0, 0, 1], [0, 0, 1], [0, 0, 1], [0, 0, 1], [0, 0, 1]],
            ]
        ),
    )

def test_c2():
    quadpy.c2.all_schemes["stroud_c2_5_4"]().integrate(
        lambda x: numpy.exp(x[0]), quadpy.c2.rectangle_points([0.0, 1.0], [0.0, 1.0]),
    )

    val = quadpy.c2.all_schemes["stroud_c2_3_1"]().integrate(
        lambda x: [numpy.exp(x[0]), numpy.exp(x[1])],
        numpy.stack(
            [
                quadpy.c2.rectangle_points([0.0, 1.0], [0.0, 1.0]),
                quadpy.c2.rectangle_points([0.0, 1.0], [0.0, 1.0]),
                quadpy.c2.rectangle_points([0.0, 1.0], [0.0, 1.0]),
                quadpy.c2.rectangle_points([0.0, 1.0], [0.0, 1.0]),
                quadpy.c2.rectangle_points([0.0, 1.0], [0.0, 1.0]),
            ],
            axis=-2,
        ),
    )
    assert val.shape == (2, 5)

def test_e2r2():
    scheme = quadpy.e2r2.rabinowitz_richter_3()
    scheme.integrate(lambda x: numpy.exp(x[0]))

def test_e2r():
    scheme = quadpy.e2r.rabinowitz_richter_5()
    scheme.integrate(lambda x: numpy.exp(x[0]))

def test_u3():
    quadpy.u3.all_schemes["lebedev_003a"]().integrate(
        lambda x: numpy.exp(1j * x[0]) + 1j * x[0] ** 2,
        numpy.array([0.0, 0.3, 0.0]),
        0.7,
    )

    # quadpy.u3.lebedev_003a().integrate(
    #     lambda x: [numpy.exp(x[0]), numpy.exp(x[1])],
    #     numpy.array([[1.0, 1.0, 0.0], [0.0, 0.3, 0.0], [2.0, 2.0, 0.0]]),
    #     [1.0, 0.7, 0.333],
    # )

def test_s3():
    scheme = quadpy.s3.hammer_stroud_15_3a()
    scheme.integrate(lambda x: numpy.exp(x[0]), [0.0, 0.0, 0.0], 1.0)

    scheme = quadpy.s3.hammer_stroud_15_3b()
    scheme.integrate(
        lambda x: [numpy.exp(x[0]), numpy.exp(x[1])],
        [[1.0, 1.0, 0.0], [0.0, 0.3, 0.0], [2.0, 2.0, 0.0]],
        [1.0, 0.7, 0.333],
    )

def test_t3():
    quadpy.t3.shunn_ham_3().integrate(
        lambda x: numpy.exp(x[0]),
        numpy.array([[0, 0, 0], [1, 0, 0], [0, 1, 0], [0, 0, 1]], dtype=float),
    )

    quadpy.t3.shunn_ham_3().integrate(
        lambda x: [numpy.exp(x[0]), numpy.exp(x[1])],
        numpy.stack(
            [
                [[0.0, 0.0, 0.0], [1.0, 0.0, 0.0], [0.0, 1.0, 0], [0.0, 0, 1]],
                [[0.0, 0.0, 0.0], [1.0, 0.0, 0.0], [0.0, 1.0, 0], [0.0, 0, 1]],
                [[0.0, 0.0, 0.0], [1.0, 0.0, 0.0], [0.0, 1.0, 0], [0.0, 0, 1]],
                [[0.0, 0.0, 0.0], [1.0, 0.0, 0.0], [0.0, 1.0, 0], [0.0, 0, 1]],
            ],
            axis=-2,
        ),
    )

def test_t2():
    quadpy.t2.all_schemes["cubtri"]().integrate(
        lambda x: numpy.exp(x[0]), numpy.array([[0.0, 0.0], [1.0, 0.0], [0.0, 1.0]])
    )

    val = quadpy.t2.all_schemes["cubtri"]().integrate(
        lambda x: [numpy.exp(x[0]), numpy.exp(x[1])],
        numpy.stack(
            [
                [[0.0, 0.0], [1.0, 0.0], [0.0, 1.0]],
                [[0.0, 0.0], [1.0, 0.0], [0.0, 1.0]],
                [[0.0, 0.0], [1.0, 0.0], [0.0, 1.0]],
                [[0.0, 0.0], [1.0, 0.0], [0.0, 1.0]],
                [[0.0, 0.0], [1.0, 0.0], [0.0, 1.0]],
            ],
            axis=-2,
        ),
    )
    assert val.shape == (2, 5)

def test_w3():
    quadpy.w3.felippa_4().integrate(
        lambda x: numpy.exp(x[0]),
        numpy.array(
            [[[0, 0, 0], [1, 0, 0], [0, 1, 0]], [[0, 0, 1], [1, 0, 1], [0, 1, 1]]]
        ),
    )

    val = quadpy.w3.felippa_4().integrate(
        lambda x: [numpy.exp(x[0]), numpy.exp(x[1])],
        numpy.stack(
            [
                [[[0, 0, 0], [1, 0, 0], [0, 1, 0]], [[0, 0, 1], [1, 0, 1], [0, 1, 1]]],
                [[[0, 0, 0], [1, 0, 0], [0, 1, 0]], [[0, 0, 1], [1, 0, 1], [0, 1, 1]]],
                [[[0, 0, 0], [1, 0, 0], [0, 1, 0]], [[0, 0, 1], [1, 0, 1], [0, 1, 1]]],
            ],
            axis=-2,
        ),
    )
    assert val.shape == (2, 3)

def test_tn():
    dim = 4
    quadpy.tn.grundmann_moeller(dim, 3).integrate(
        lambda x: numpy.exp(x[0]),
        numpy.array(
            [
                [0.0, 0.0, 0.0, 0.0],
                [1.0, 2.0, 0.0, 0.0],
                [0.0, 1.0, 0.0, 0.0],
                [0.0, 3.0, 1.0, 0.0],
                [0.0, 0.0, 4.0, 1.0],
            ]
        ),
    )

def test_sn():
    dim = 4
    quadpy.sn.dobrodeev_1970(dim).integrate(
        lambda x: numpy.exp(x[0]), numpy.zeros(4), 1.0
    )

def test_cn():
    dim = 4
    quadpy.cn.stroud_cn_3_3(dim).integrate(
        lambda x: numpy.exp(x[0]),
        quadpy.cn.ncube_points([0.0, 1.0], [0.1, 0.9], [-1.0, 1.0], [-1.0, -0.5]),
    )

def test_un():
    dim = 4
    quadpy.un.dobrodeev_1978(dim).integrate(
        lambda x: numpy.exp(x[0]), numpy.zeros(dim), 1.0
    )

def test_enr2():
    dim = 4
    quadpy.enr2.stroud_enr2_5_2(dim).integrate(lambda x: numpy.exp(x[0]))

def test_e1r():
    scheme = quadpy.e1r.gauss_laguerre(5)
    scheme.integrate(lambda x: x[0] ** 2)

def test_e3r():
    quadpy.e3r.stroud_secrest_09().integrate(lambda x: numpy.exp(x[0]))

def test_e1r2():
    scheme = quadpy.e1r2.gauss_hermite(5)
    scheme.integrate(lambda x: x[0] ** 2)

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


<commit_before>#!/usr/bin/env python3
"""Extract stuff from server configuration."""

# Author: bkramarski

from xml.dom.minidom import *
from pprint import *

def removeWsNodes(dom):
    """Whitespace textnodes are replaced with empty textnodes, so they can be
    easily stripped by normalize method."""
    from xml.dom.minidom import Node
    if dom.hasChildNodes() :
        for ch in dom.childNodes:
            removeWsNodes(ch)
    elif dom.nodeType == Node.TEXT_NODE and dom.data.strip(" \t\n\r") == '' :
        dom.data = ''

def normalizeTotally(dom):
    """Strips whitespace textnodes from DOM Node."""
    removeWsNodes(dom)
    dom.normalize()

# TUS specific functions

def getObjectsAsMap(aFileName, aObjectTagName):
    """Changes dom elements to map elements {key:{arg:val, arg:val}}."""
    root = parse(aFileName)
    normalizeTotally(root)

    xList = root.getElementsByTagName(aObjectTagName);
    oList = {}

    for x in xList:
        h = {}
        for ch in x.childNodes:
            h[ ch._get_localName() ] = ch.firstChild.nodeValue
        oList[ h['key'] ] = h
    return oList

def getPropertiesAsMap(aFileName, aObjectTagName):
    """Extracts properties from the file as map {key: {prop:val}}."""
    root = parse(aFileName)
    normalizeTotally(root)

    bList = root.getElementsByTagName(aObjectTagName)
    oDict = {}
    for b in bList:
        k = b.getAttribute('key');
        props = {}
        for ch in b.childNodes:
            props[ ch._get_localName() ] = ch.getAttribute('value')
        oDict[k] = props
    return oDict

def getCostAsMap(aFileName, aObjectTagName):
    """Extracts cost for given object action from xml file"""
    root = parse(aFileName)
    normalizeTotally(root)

    obCostDict = {}
    obList = root.getElementsByTagName(aObjectTagName)
    for ob in obList:
        k = ob.getAttribute('key')
        obCostDict[k] = {}
        for act in ob.childNodes :
            cost = {}
            if act :
                for r in act.childNodes :
                    cost[r.tagName] = r.getAttribute('value')
            obCostDict[k][act.tagName] = cost
    return obCostDict

def getObjectsWithProperties(aObjectName, aFileName, aPropFileName='',aCostFileName=''):
    """Extracts objects and properties, and combines them in one map"""
    oDict = getObjectsAsMap(aFileName, aObjectName)

    if aPropFileName :
        pDict = getPropertiesAsMap(aPropFileName, aObjectName)
        for i, ob in pDict.items():
            for p, v in ob.items():
                oDict[i][p]=v
    if aCostFileName :
        cDict = getCostAsMap(aCostFileName, aObjectName)
        for i, ob in cDict.items():
            oDict[i]['costs'] = {}
            for c, v in ob.items():
                oDict[i]['costs'][c] = v
    return oDict

def prepareBuildings(aFileName, aPropFileName, aCostFileName):
    """Extracts buildings and its properties from given files"""
    return getObjectsWithProperties('building', aFileName, aPropFileName, aCostFileName)

def prepareHumans(aFileName, aPropFileName, aCostFileName):
    """Extracts humans from given file"""
    return getObjectsWithProperties('human', aFileName, aPropFileName, aCostFileName)

def prepareResources(aFileName):
    """Extracts resources from given file"""
    return getObjectsAsMap(aFileName, 'resource')

# Functions for presenting the data

def costFormatString(aCostDict, aWidth = 3):
    """Prepares map indexed format string for cost displaying"""
    fs = ""
    for c,v in aCostDict.items():
        fs += "{" + c + ":>"+ str(aWidth) +"}"
    return fs

def costString(aCost, aResSet = None, aWidth = 3):
    """Returns cost strings for presenting in one line."""
    fullCost = {}
    if not aResSet:
        aResSet = ['food','gold','wood','iron','rock','mana','coal']

    # Prepare values
    for i in aResSet:
        fullCost[i] = 0
    for c,v in aCost.items():
        fullCost[c] += int(v)
        #^^ indirect check of resource consistency

    fs = costFormatString(fullCost, aWidth)
    return fs.format(**fullCost)

def costHead(aResSet = None, aWidth = 3):
    """Returns cost header string."""
    if not aResSet:
        aResSet = ['food','gold','wood','iron','rock','mana','coal']

    resSet = {}
    # Setup header names
    for c in aResSet:
        resSet[c] = c[0]

    fs = costFormatString(resSet, aWidth)
    return fs.format(**resSet)

def printPossibleResources(aResourceDict):
    """Prints resources' definitions from given file in one line format."""
    # resources.resource.{key|name}
    for i, r in aResourceDict.items():
        print( r['name'])

# TODO: tidy up the format strings mess

def printPossibleHumans(aHumansDict, aEngageableOnly = True):
    """Prints engageable humans' definitions from given file in one line format."""
    # humans.human.{key|class|name|experience}
    print( "{:>10}{:>16}{:>9}{:>6}{:>6}| e:{:->21}| d:{:->21}| l:{:->21}#".format(
        'class', 'name', 'exp.', 'dism.','prod.', costHead(), costHead(), costHead()))
    print( "{:->10}{:->16}{:->9}{:->6}{:->6}|---{:->21}|---{:->21}|---{:->21}#".format(
        '', '', '', '','','','',''))
    for i, h in aHumansDict.items():
        if aEngageableOnly and h['engageable'] != 'true':
            continue
        print( "{:>10}{:>16}{:>9}{:>6}{:>6}|   {}|   {}|   {}".format(
            h['class'], h['name'], h['experience'], h['dismissable'], h['production'],
            costString(h['costs']['engage']),
            costString(h['costs']['dismiss']),
            costString(h['costs']['live'])
        ))

def printPossibleBuildings(aBuildingsDict):
    """Prints buildings' definitions from given files in one line format."""
    # buildings.building.{key|class|name}
    print( "{0:>10}{1:>20}{2:>6}| b:{3:->21}| d:{3:->21}#".format(
        'class', 'name', 'cap.', costHead(), costHead()))
    print( "{0:->10}{1:->20}{2:->6}|---{3:->21}|---{3:->21}#".format(
        '', '', '', '',''))
    for i, b in aBuildingsDict.items():
        print( "{0:>10}{1:>20}{2:>6}|   {3}|   {4}".format(
            b['class'], b['name'], b['capacity'],
            costString(b['costs']['build']),
            costString(b['costs']['destroy'])
        ))

# Common actions
def read_configuration(aDataRoot):
    """Reads all configuration data. Returns (resources, buildings, humans)."""
    hFile =  aDataRoot + '/Human/humans.xml'
    hpFile = aDataRoot + '/Human/properties.xml'
    hcFile = aDataRoot + '/Human/costs.xml'
    bFile =  aDataRoot + '/Building/buildings.xml'
    bpFile = aDataRoot + '/Building/properties.xml'
    bcFile = aDataRoot + '/Building/costs.xml'
    rFile =  aDataRoot + '/Resource/resources.xml'

    humans = prepareHumans(hFile, hpFile, hcFile)
    buildings = prepareBuildings(bFile, bpFile, bcFile)
    resources = prepareResources(rFile)

    return resources, buildings, humans

# Handling usage as script

import sys

def main(argv=None):
    """Handles commandline call.

    First argument should be path to the configurtion files directory.
    Second argument can be [humans|buildings|resources] or empty, and it
    controls what data is being printed"""

    if argv is None:
        argv = sys.argv

    dataRoot =  argv[1]
    type =      argv[2] if len(argv) > 2 else ''

    resources, buildings, humans = read_configuration(dataRoot)

    if type == 'humans' :
        printPossibleHumans(humans)
    elif type == 'allhumans' :
        printPossibleHumans(humans, False)
    elif type == 'buildings':
        printPossibleBuildings(buildings)
    elif type == 'resources':
        printPossibleResources(resources)
    elif type == 'all' or type == 'raw':
        pprint( humans )
        pprint( buildings )
        pprint( resources )
    else:
        print("""
*** Use second parameter to choose data to print.
    Possible targets:
        humans      -- prints engageable humans
        allhumans   -- prints all defined humans
        buildings   -- prints buildings
        resources   -- prints resources
        all|raw     -- prints all data collected by pprint
""")

    return 0

if __name__ == "__main__":
    sys.exit(main())
<commit_msg>Configuration printing uses only the defined resources<commit_after>#!/usr/bin/env python3
"""Extract stuff from server configuration."""

# Author: bkramarski

from xml.dom.minidom import *
from pprint import *

def removeWsNodes(dom):
    """Whitespace textnodes are replaced with empty textnodes, so they can be
    easily stripped by normalize method."""
    from xml.dom.minidom import Node
    if dom.hasChildNodes() :
        for ch in dom.childNodes:
            removeWsNodes(ch)
    elif dom.nodeType == Node.TEXT_NODE and dom.data.strip(" \t\n\r") == '' :
        dom.data = ''

def normalizeTotally(dom):
    """Strips whitespace textnodes from DOM Node."""
    removeWsNodes(dom)
    dom.normalize()

# TUS specific functions

def getObjectsAsMap(aFileName, aObjectTagName):
    """Changes dom elements to map elements {key:{arg:val, arg:val}}."""
    root = parse(aFileName)
    normalizeTotally(root)

    xList = root.getElementsByTagName(aObjectTagName);
    oList = {}

    for x in xList:
        h = {}
        for ch in x.childNodes:
            h[ ch._get_localName() ] = ch.firstChild.nodeValue
        oList[ h['key'] ] = h
    return oList

def getPropertiesAsMap(aFileName, aObjectTagName):
    """Extracts properties from the file as map {key: {prop:val}}."""
    root = parse(aFileName)
    normalizeTotally(root)

    bList = root.getElementsByTagName(aObjectTagName)
    oDict = {}
    for b in bList:
        k = b.getAttribute('key');
        props = {}
        for ch in b.childNodes:
            props[ ch._get_localName() ] = ch.getAttribute('value')
        oDict[k] = props
    return oDict

def getCostAsMap(aFileName, aObjectTagName):
    """Extracts cost for given object action from xml file"""
    root = parse(aFileName)
    normalizeTotally(root)

    obCostDict = {}
    obList = root.getElementsByTagName(aObjectTagName)
    for ob in obList:
        k = ob.getAttribute('key')
        obCostDict[k] = {}
        for act in ob.childNodes :
            cost = {}
            if act :
                for r in act.childNodes :
                    cost[r.tagName] = r.getAttribute('value')
            obCostDict[k][act.tagName] = cost
    return obCostDict

def getObjectsWithProperties(aObjectName, aFileName, aPropFileName='',aCostFileName=''):
    """Extracts objects and properties, and combines them in one map"""
    oDict = getObjectsAsMap(aFileName, aObjectName)

    if aPropFileName :
        pDict = getPropertiesAsMap(aPropFileName, aObjectName)
        for i, ob in pDict.items():
            for p, v in ob.items():
                oDict[i][p]=v
    if aCostFileName :
        cDict = getCostAsMap(aCostFileName, aObjectName)
        for i, ob in cDict.items():
            oDict[i]['costs'] = {}
            for c, v in ob.items():
                oDict[i]['costs'][c] = v
    return oDict

def prepareBuildings(aFileName, aPropFileName, aCostFileName):
    """Extracts buildings and its properties from given files"""
    return getObjectsWithProperties('building', aFileName, aPropFileName, aCostFileName)

def prepareHumans(aFileName, aPropFileName, aCostFileName):
    """Extracts humans from given file"""
    return getObjectsWithProperties('human', aFileName, aPropFileName, aCostFileName)

def prepareResources(aFileName):
    """Extracts resources from given file"""
    return getObjectsAsMap(aFileName, 'resource')

# Functions for presenting the data

def costFormatString(aCostDict, aWidth = 3):
    """Prepares map indexed format string for cost displaying"""
    fs = ""
    for c,v in aCostDict.items():
        fs += "{" + c + ":>"+ str(aWidth) +"}"
    return fs

def costString(aCost, aResSet = None, aWidth = 3):
    """Returns cost strings for presenting in one line."""
    fullCost = {}
    if not aResSet:
        aResSet = ['food','gold','wood','iron','rock','mana','coal']

    # Prepare values
    for i in aResSet:
        fullCost[i] = 0
    for c,v in aCost.items():
        fullCost[c] += int(v)
        #^^ indirect check of resource consistency

    fs = costFormatString(fullCost, aWidth)
    return fs.format(**fullCost)

def costHead(aResSet = None, aWidth = 3):
    """Returns cost header string."""
    if not aResSet:
        aResSet = ['food','gold','wood','iron','rock','mana','coal']

    resSet = {}
    # Setup header names
    for c in aResSet:
        resSet[c] = c[0]

    fs = costFormatString(resSet, aWidth)
    return fs.format(**resSet)

def printPossibleResources(aResourceDict):
    """Prints resources' definitions from given file in one line format."""
    # resources.resource.{key|name}
    for i, r in aResourceDict.items():
        print( r['name'])

# TODO: tidy up the format strings mess

def printPossibleHumans(aHumansDict, aEngageableOnly = True, aResSet = None):
    """Prints engageable humans' definitions from given file in one line format."""
    # humans.human.{key|class|name|experience}
    costStrLen = 3*len(aResSet)
    print( "{0:>10}{1:>16}{2:>9}{3:>6}{4:>6}| e:{5:->{8}}| d:{6:->{8}}| l:{7:->{8}}#".format(
        'class', 'name', 'exp.', 'dism.','prod.', costHead(aResSet), costHead(aResSet), costHead(aResSet), costStrLen))
    print( "{0:->10}{1:->16}{2:->9}{3:->6}{4:->6}|---{5:->{8}}|---{6:->{8}}|---{7:->{8}}#".format(
        '', '', '', '', '', '', '', '', costStrLen ))
    for i, h in aHumansDict.items():
        if aEngageableOnly and h['engageable'] != 'true':
            continue
        print( "{:>10}{:>16}{:>9}{:>6}{:>6}|   {}|   {}|   {}".format(
            h['class'], h['name'], h['experience'], h['dismissable'], h['production'],
            costString(h['costs']['engage'], aResSet),
            costString(h['costs']['dismiss'], aResSet),
            costString(h['costs']['live'], aResSet)
        ))

def printPossibleBuildings(aBuildingsDict, aResSet = None):
    """Prints buildings' definitions from given files in one line format."""
    # buildings.building.{key|class|name}
    costStrLen = 3*len(aResSet)
    print( "{0:>10}{1:>20}{2:>6}| b:{3:->{5}}| d:{4:->{5}}#".format(
        'class', 'name', 'cap.', costHead(aResSet), costHead(aResSet), costStrLen ) )
    print( "{0:->10}{1:->20}{2:->6}|---{3:->{5}}|---{4:->{5}}#".format(
        '', '', '', '', '', costStrLen) )
    for i, b in aBuildingsDict.items():
        print( "{0:>10}{1:>20}{2:>6}|   {3}|   {4}".format(
            b['class'], b['name'], b['capacity'],
            costString(b['costs']['build'], aResSet),
            costString(b['costs']['destroy'], aResSet)
        ))

# Common actions
def read_configuration(aDataRoot):
    """Reads all configuration data. Returns (resources, buildings, humans)."""
    hFile =  aDataRoot + '/Human/humans.xml'
    hpFile = aDataRoot + '/Human/properties.xml'
    hcFile = aDataRoot + '/Human/costs.xml'
    bFile =  aDataRoot + '/Building/buildings.xml'
    bpFile = aDataRoot + '/Building/properties.xml'
    bcFile = aDataRoot + '/Building/costs.xml'
    rFile =  aDataRoot + '/Resource/resources.xml'

    humans = prepareHumans(hFile, hpFile, hcFile)
    buildings = prepareBuildings(bFile, bpFile, bcFile)
    resources = prepareResources(rFile)

    return resources, buildings, humans

# Handling usage as script

import sys

def main(argv=None):
    """Handles commandline call.

    First argument should be path to the configurtion files directory.
    Second argument can be [humans|buildings|resources] or empty, and it
    controls what data is being printed"""

    if argv is None:
        argv = sys.argv

    dataRoot =  argv[1]
    type =      argv[2] if len(argv) > 2 else ''

    resources, buildings, humans = read_configuration(dataRoot)

    if type == 'humans' :
        printPossibleHumans(humans, True, resources.keys())
    elif type == 'allhumans' :
        printPossibleHumans(humans, False, resources.keys())
    elif type == 'buildings':
        printPossibleBuildings(buildings, resources.keys())
    elif type == 'resources':
        printPossibleResources(resources)
    elif type == 'all' or type == 'raw':
        pprint( humans )
        pprint( buildings )
        pprint( resources )
    else:
        print("""
*** Use second parameter to choose data to print.
    Possible targets:
        humans      -- prints engageable humans
        allhumans   -- prints all defined humans
        buildings   -- prints buildings
        resources   -- prints resources
        all|raw     -- prints all data collected by pprint
""")

    return 0

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


<commit_before>from flask import Flask, render_template, g, redirect, request, session, flash, Blueprint
from sqlalchemy.sql import text
from main import db

requestsBP = Blueprint('request', __name__)

@requestsBP.route('/create', methods=('GET', 'POST'))
def create_service_request():
	if request.method == 'POST':
		sql = text('''INSERT INTO service_request(client_username, address, title, description, schedule)
						VALUES(:user, :address, :title, :description, :time);''')
		db.engine.execute(sql, user=session["user"], address=request.form.get('address'), title=request.form.get('title'),\
							description=request.form.get('description'), time=request.form.get("time"))
		flash('Service request created.')
		return redirect('/')
	if not session.get('user'):
		flash('Please login before creating requests.')
		return redirect('/')
	elif session['type'] == 'worker':
		flash('You must be a client to create service requests.')
		return redirect('/')
	else:
		return render_template('create.jade')

@requestsBP.route('/search', methods=('GET', 'POST'))
def searchName():
	if request.method == 'POST':
		search = request.form.get('query')
		print(search)
		#sql = text('''select "sp_awesome_search_service_requests"('{0}')'''.format(search))
		sql = text('''SELECT sr.title, sr.description, sr.client_username, sr.schedule, sr.address
						FROM service_request sr
						WHERE
							UPPER(sr.title) LIKE UPPER('%'||:search||'%') OR
							UPPER(sr.description) LIKE UPPER('%'||:search||'%') OR
							UPPER(sr.address) LIKE UPPER('%'||:search||'%');''')
		results = db.engine.execute(sql, search=search)
		results = results.fetchall()
		return render_template('searchRequests.jade', results=results)
	return render_template('searchRequests.jade')

@requestsBP.route('/requests')
def viewRequests():
	if session.get('user'):
		sql = text('''SELECT * FROM service_request WHERE client_username=:username;''')
		results = db.engine.execute(sql, username=session.get('user'))
		results = results.fetchall()
		return render_template('requests.jade', requests=results)
	else:
		return redirect('/')

def getRequest(id):
	sql=text('''SELECT client_username, title, description, schedule, address FROM service_request WHERE service_id=:id;''')
	result=db.engine.execute(sql, id=id)
	return result

def getWorkers():
	sql=text('''SELECT worker_username FROM worker;''')
	result=db.engine.execute(sql)
	return result

@requestsBP.route('/requests/<service_id>')#check info here
def request(service_id):
	request = getRequest(service_id)
	names = getWorkers()
	if(request):
		return render_template('requests.jade', client_username=result[0], title=result[1], description=result[2], schedule=result[3], address=result[4], worker_names=names)
		#check info above
	return redirect('/')

@requestsBP.route('/requests')
def viewRequests():
	if session.get('user'):
		sql = text('''SELECT * FROM service_request WHERE client_username=:user;''')
		db.engine.execute(sql, user=session["user"])
		return render_template('requests.jade')
	else:
		redirect('/')
<commit_msg>Fixed request.py<commit_after>from flask import Flask, render_template, g, redirect, request, session, flash, Blueprint
from sqlalchemy.sql import text
from main import db

requestsBP = Blueprint('request', __name__)

@requestsBP.route('/create', methods=('GET', 'POST'))
def create_service_request():
	if request.method == 'POST':
		sql = text('''INSERT INTO service_request(client_username, address, title, description, schedule)
						VALUES(:user, :address, :title, :description, :time);''')
		db.engine.execute(sql, user=session["user"], address=request.form.get('address'), title=request.form.get('title'),\
							description=request.form.get('description'), time=request.form.get("time"))
		flash('Service request created.')
		return redirect('/')
	if not session.get('user'):
		flash('Please login before creating requests.')
		return redirect('/')
	elif session['type'] == 'worker':
		flash('You must be a client to create service requests.')
		return redirect('/')
	else:
		return render_template('create.jade')

@requestsBP.route('/search', methods=('GET', 'POST'))
def searchName():
	if request.method == 'POST':
		search = request.form.get('query')
		print(search)
		#sql = text('''select "sp_awesome_search_service_requests"('{0}')'''.format(search))
		sql = text('''SELECT sr.title, sr.description, sr.client_username, sr.schedule, sr.address
						FROM service_request sr
						WHERE
							UPPER(sr.title) LIKE UPPER('%'||:search||'%') OR
							UPPER(sr.description) LIKE UPPER('%'||:search||'%') OR
							UPPER(sr.address) LIKE UPPER('%'||:search||'%');''')
		results = db.engine.execute(sql, search=search)
		results = results.fetchall()
		return render_template('searchRequests.jade', results=results)
	return render_template('searchRequests.jade')

@requestsBP.route('/requests')
def viewRequests():
	if session.get('user'):
		sql = text('''SELECT * FROM service_request WHERE client_username=:username;''')
		results = db.engine.execute(sql, username=session.get('user'))
		results = results.fetchall()
		return render_template('requests.jade', requests=results)
	else:
		return redirect('/')

def getRequest(id):
	sql=text('''SELECT client_username, title, description, schedule, address FROM service_request WHERE service_id=:id;''')
	result=db.engine.execute(sql, id=id)
	return result

def getWorkers():
	sql=text('''SELECT worker_username FROM worker;''')
	result=db.engine.execute(sql)
	return result

@requestsBP.route('/requests/<service_id>')#check info here
def request(service_id):
	request = getRequest(service_id)
	names = getWorkers()
	if(request):
		return render_template('requests.jade', client_username=result[0], title=result[1], description=result[2], schedule=result[3], address=result[4], worker_names=names)
		#check info above
	return redirect('/')

<|endoftext|>