<commit_before>import json
import datetime, numbers
from UserDict import UserDict
from dateutil.parser import parse as to_datetime
from base_constants import XSD, LDP
import urlparse

_invalid_uri_chars = '<>" {}|\\^`'

def _is_valid_uri(uri):
    for c in _invalid_uri_chars: 
        if c in uri: return False
    return True

class URI():
    def __init__(self, uri_string):
        if isinstance(uri_string, URI):
            uri_string = str(uri_string)
        else:
            if not isinstance(uri_string, basestring) or not _is_valid_uri(uri_string): raise ValueError(repr(uri_string))
        self.uri_string = uri_string   
        
    def __eq__(self, other):
        return isinstance(other, URI) and self.uri_string == other.uri_string

    def __ne__(self, other):
        return not self.__eq__(other)
        
    def __hash__(self):
        return self.uri_string.__hash__()
        
    def __repr__(self):
        return 'URI(%s)' % self.uri_string
        
    def __str__(self):
        return self.uri_string

class BNode():
    def __init__(self, bnode_string):
        self.bnode_string = bnode_string   
        
    def __eq__(self, other):
        return isinstance(other, BNode) and self.bnode_string == other.bnode_string

    def __ne__(self, other):
        return not self.__eq__(other)
        
    def __hash__(self):
        return self.bnode_string.__hash__()
        
    def __repr__(self):
        return 'BNode(%s)' % self.bnode_string
        
    def __str__(self):
        return self.bnode_string
        
class RDF_JSON_Document(UserDict):
    """
    This class is designed to avoid mapping between RDF-JSON and objects.
    The data is kept in its RDF-JSON format as it will be exchanged with the server.
    The purpose of the class is to wrap the RDF-JSON to provide accessor methods to make it easy to work with the RDF-JSON.
    """
    def __init__(self, aSource, graph_url=None, default_subject_url=None):
        if hasattr(aSource, 'status_code'):
            if aSource.status_code == 201 or aSource.status_code == 200:
                try:
                    self.data = json.loads(aSource.text, object_hook=rdf_json_decoder)
                except ValueError:
                    return None
            if aSource.status_code == 201 :
                self.graph_url = aSource.headers['Location']
            elif aSource.status_code == 200:
                self.graph_url = aSource.headers['Content-Location']
            else: 
                self.graph_url = None
            self.default_subject_url = default_subject_url
        else:
            self.graph_url = graph_url<commit_msg>improve error handling when creating an RDF_JSON_Document from a request result<commit_after>import json
import datetime, numbers
from UserDict import UserDict
from dateutil.parser import parse as to_datetime
from base_constants import XSD, LDP
import urlparse

_invalid_uri_chars = '<>" {}|\\^`'

def _is_valid_uri(uri):
    for c in _invalid_uri_chars: 
        if c in uri: return False
    return True

class URI():
    def __init__(self, uri_string):
        if isinstance(uri_string, URI):
            uri_string = str(uri_string)
        else:
            if not isinstance(uri_string, basestring) or not _is_valid_uri(uri_string): raise ValueError(repr(uri_string))
        self.uri_string = uri_string   
        
    def __eq__(self, other):
        return isinstance(other, URI) and self.uri_string == other.uri_string

    def __ne__(self, other):
        return not self.__eq__(other)
        
    def __hash__(self):
        return self.uri_string.__hash__()
        
    def __repr__(self):
        return 'URI(%s)' % self.uri_string
        
    def __str__(self):
        return self.uri_string

class BNode():
    def __init__(self, bnode_string):
        self.bnode_string = bnode_string   
        
    def __eq__(self, other):
        return isinstance(other, BNode) and self.bnode_string == other.bnode_string

    def __ne__(self, other):
        return not self.__eq__(other)
        
    def __hash__(self):
        return self.bnode_string.__hash__()
        
    def __repr__(self):
        return 'BNode(%s)' % self.bnode_string
        
    def __str__(self):
        return self.bnode_string
        
class RDF_JSON_Document(UserDict):
    """
    This class is designed to avoid mapping between RDF-JSON and objects.
    The data is kept in its RDF-JSON format as it will be exchanged with the server.
    The purpose of the class is to wrap the RDF-JSON to provide accessor methods to make it easy to work with the RDF-JSON.
    """
    def __init__(self, aSource, graph_url=None, default_subject_url=None):
        if hasattr(aSource, 'status_code'):
            if aSource.status_code == 201 or aSource.status_code == 200:
                try:
                    self.data = json.loads(aSource.text, object_hook=rdf_json_decoder)
                except ValueError:
                    raise ValueError("No JSON object could be decoded from: %s" % aSource.text)
            if aSource.status_code == 201 :
                self.graph_url = aSource.headers['Location']
            elif aSource.status_code == 200:
                self.graph_url = aSource.headers['Content-Location']
            else: 
                self.graph_url = None
            self.default_subject_url = default_subject_url
        else:
            self.graph_url = graph_url
{'fraction_non_alphanumeric': 0.08094186902133922, 'fraction_numerical': 0.004415011037527594, 'mean_word_length': 2.3376304481276855, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 5, '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': '2596639', 'n_tokens_mistral': 1611, 'n_tokens_neox': 1566, 'n_words': 445}