<commit_before>import traceback
import json
import copy
from dateutil import parser as datetimeparser
from urllib import parse

from cinp.common import URI
from cinp.readers import READER_REGISTRY

__CINP_VERSION__ = '0.9'
__MULTI_URI_MAX__ = 100

FIELD_TYPE_LIST = ( 'String', 'Integer', 'Float', 'Boolean', 'DateTime', 'Map', 'Model', 'File' )


class InvalidRequest( Exception ):
  def __init__( self, message=None, data=None):
    self.data = data or { 'message': message } or 'Unknown'

  def asResponse( self ):
    return Response( 400, data=self.data )

  def __str__( self ):
    return 'InvalidRequest: "{0}"'.format( self.data )


class ServerError( Exception ):
  def __init__( self, message ):
    self.message = message

  def asResponse( self ):
    return Response( 500, data={ 'message': self.message } )

  def __str__( self ):
    return 'ServerError: "{0}"'.format( self.message )


class ObjectNotFound( Exception ):
  def __init__( self, model_path, object_id ):
    self.model_path = model_path
    self.object_id = object_id

  def asResponse( self ):
    return Response( 404, data={ 'model_path': self.model_path, 'object_id': self.object_id } )

  def __str__( self ):
    return 'ObjectNotFound: "{0}":"{1}":'.format( self.model_path, self.object_id )


class NotAuthorized( Exception ):
  pass


class AnonymouseUser():
  @property
  def isSuperuser( self ):
    return False

  @property
  def isAnonymouse( self ):
    return True


def _dictConverter( value ):
  _fromPythonMap( value )
  return value

MAP_TYPE_CONVERTER = {
                       'NoneType': lambda a: None,
                       'str': str,
                       'int': str,
                       'float': str,
                       'bool': lambda a: True if a else False,
                       'datetime': lambda a: a.isoformat(),
                       'timedelta': lambda a: a.total_seconds(),
                       'dict': _dictConverter
                     }


def _fromPythonMap_converter( value ):
  try:
    return MAP_TYPE_CONVERTER[ type( value ).__name__ ]( value )
  except KeyError:
    raise ValueError( 'no converter for type "{0}" in map converter'.format( type( value ).__name__ ) )


def _fromPythonMap( value ):
  for key in value.keys():
    if isinstance( value[ key ], tuple ):  # convert tuple to list before iterating
      value[ key ] = list( value[ key ] )<commit_msg>fixed some type casing bugs when converting dicts to maps<commit_after>import traceback
import json
import copy
from dateutil import parser as datetimeparser
from urllib import parse

from cinp.common import URI
from cinp.readers import READER_REGISTRY

__CINP_VERSION__ = '0.9'
__MULTI_URI_MAX__ = 100

FIELD_TYPE_LIST = ( 'String', 'Integer', 'Float', 'Boolean', 'DateTime', 'Map', 'Model', 'File' )


class InvalidRequest( Exception ):
  def __init__( self, message=None, data=None):
    self.data = data or { 'message': message } or 'Unknown'

  def asResponse( self ):
    return Response( 400, data=self.data )

  def __str__( self ):
    return 'InvalidRequest: "{0}"'.format( self.data )


class ServerError( Exception ):
  def __init__( self, message ):
    self.message = message

  def asResponse( self ):
    return Response( 500, data={ 'message': self.message } )

  def __str__( self ):
    return 'ServerError: "{0}"'.format( self.message )


class ObjectNotFound( Exception ):
  def __init__( self, model_path, object_id ):
    self.model_path = model_path
    self.object_id = object_id

  def asResponse( self ):
    return Response( 404, data={ 'model_path': self.model_path, 'object_id': self.object_id } )

  def __str__( self ):
    return 'ObjectNotFound: "{0}":"{1}":'.format( self.model_path, self.object_id )


class NotAuthorized( Exception ):
  pass


class AnonymouseUser():
  @property
  def isSuperuser( self ):
    return False

  @property
  def isAnonymouse( self ):
    return True


def _dictConverter( value ):
  _fromPythonMap( value )
  return value

MAP_TYPE_CONVERTER = {
                       'NoneType': lambda a: None,
                       'str': lambda a: a,
                       'int': lambda a: a,
                       'float': lambda a: a,
                       'bool': lambda a: True if a else False,
                       'datetime': lambda a: a.isoformat(),
                       'timedelta': lambda a: a.total_seconds(),
                       'dict': _dictConverter
                     }


def _fromPythonMap_converter( value ):
  try:
    return MAP_TYPE_CONVERTER[ type( value ).__name__ ]( value )
  except KeyError:
    raise ValueError( 'no converter for type "{0}" in map converter'.format( type( value ).__name__ ) )


def _fromPythonMap( value ):
  for key in value.keys():
    if isinstance( value[ key ], tuple ):  # convert tuple to list before iterating
      value[ key ] = list( value[ key ] )
{'fraction_non_alphanumeric': 0.11341263330598851, 'fraction_numerical': 0.007793273174733388, 'mean_word_length': 2.792379471228616, 'pattern_counts': {'":': 4, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2058311', 'n_tokens_mistral': 1514, 'n_tokens_neox': 1427, 'n_words': 463}