<commit_before># coding: utf-8
"""pypel.models, models for pypel.

THIS SOFTWARE IS UNDER BSD LICENSE.
Copyright (c) 2012-2013 <NAME> <<EMAIL>>

Read LICENSE for more informations.
"""

import os.path

import six

from gi.repository import GExiv2

XMP_KEY_PREFIX = 'Xmp.pypel.'

SUPPORTED_EXT = ('.jpg', '.jpeg', '.png', '.eps')


class DoesNotExist(IOError):
    """The file or directory does not exist"""


class IsADirectory(IOError):
    """This is a directory so you can't use it as a receipt"""


class ImageNotSupported(IOError):
    """Image is not supported"""


class Field(object):
    """Base class for all field types"""

    casting_function = lambda self, value: value

    def __init__(self):
        self.name = None

    @property
    def key(self):
        return XMP_KEY_PREFIX + self.name

    def __get__(self, instance, owner):
        return self.cast(instance.get_value(self.key))

    def __set__(self, instance, value):
        instance.set_value(self.key, value)

    def __delete__(self, instance):
        instance.delete_value(self.key)

    def cast(self, value):
        if value is not None:
            return self.casting_function(value)


class CharField(Field):
    """A field for character strings"""


class FloatField(Field):
    """A field for float numbers"""

    casting_function = float


class ModelBase(type):
    """Metaclass for models"""

    def __init__(cls, name, bases, attrs):

        for name in attrs:
            if isinstance(attrs[name], Field):
                # Set field name because it is required for key generation
                setattr(attrs[name], 'name', name.title())

        super(ModelBase, cls).__init__(name, bases, attrs)


class Model(six.with_metaclass(ModelBase)):

    def __init__(self, file):
        self.file = file
        self._metadata = GExiv2.Metadata(file)

        if not self._metadata.has_xmp():
            self._metadata.register_xmp_namespace('pypelNamespace/', 'pypel')

    def get_value(self, key):
        try:
            return self._metadata[key]
        except KeyError:
            return None

    def set_value(self, key, value):
        self._metadata[key] = str(value)
        self._metadata.save_file()

    def delete_value(self, key):
        try:
            del self._metadata[key]
        except KeyError:
            pass
        self._metadata.save_file()


class Receipt(Model):
    price = FloatField()
    retailer = CharField()
    note = CharField()


def delete_metadata(receipt, price=None, retailer=None, note=None):<commit_msg>Added __repr__ method for Model class<commit_after># coding: utf-8
"""pypel.models, models for pypel.

THIS SOFTWARE IS UNDER BSD LICENSE.
Copyright (c) 2012-2013 <NAME> <<EMAIL>>

Read LICENSE for more informations.
"""

import os.path

import six

from gi.repository import GExiv2

XMP_KEY_PREFIX = 'Xmp.pypel.'

SUPPORTED_EXT = ('.jpg', '.jpeg', '.png', '.eps')


class DoesNotExist(IOError):
    """The file or directory does not exist"""


class IsADirectory(IOError):
    """This is a directory so you can't use it as a receipt"""


class ImageNotSupported(IOError):
    """Image is not supported"""


class Field(object):
    """Base class for all field types"""

    casting_function = lambda self, value: value

    def __init__(self):
        self.name = None

    @property
    def key(self):
        return XMP_KEY_PREFIX + self.name

    def __get__(self, instance, owner):
        return self.cast(instance.get_value(self.key))

    def __set__(self, instance, value):
        instance.set_value(self.key, value)

    def __delete__(self, instance):
        instance.delete_value(self.key)

    def cast(self, value):
        if value is not None:
            return self.casting_function(value)


class CharField(Field):
    """A field for character strings"""


class FloatField(Field):
    """A field for float numbers"""

    casting_function = float


class ModelBase(type):
    """Metaclass for models"""

    def __init__(cls, name, bases, attrs):

        for name in attrs:
            if isinstance(attrs[name], Field):
                # Set field name because it is required for key generation
                setattr(attrs[name], 'name', name.title())

        super(ModelBase, cls).__init__(name, bases, attrs)


class Model(six.with_metaclass(ModelBase)):

    def __init__(self, file):
        self.file = file
        self._metadata = GExiv2.Metadata(file)

        if not self._metadata.has_xmp():
            self._metadata.register_xmp_namespace('pypelNamespace/', 'pypel')

    def get_value(self, key):
        try:
            return self._metadata[key]
        except KeyError:
            return None

    def set_value(self, key, value):
        self._metadata[key] = str(value)
        self._metadata.save_file()

    def delete_value(self, key):
        try:
            del self._metadata[key]
        except KeyError:
            pass
        self._metadata.save_file()

    def __repr__(self):
        return '<{}: {}>'.format(self.__class__.__name__, self.file)


class Receipt(Model):
    price = FloatField()
    retailer = CharField()
    note = CharField()


def delete_metadata(receipt, price=None, retailer=None, note=None):
{'fraction_non_alphanumeric': 0.11487003058103976, 'fraction_numerical': 0.004204892966360856, 'mean_word_length': 2.8002904865649962, 'pattern_counts': {'":': 0, '<': 10, '<?xml version=': 0, '>': 10, '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': '3342506', 'n_tokens_mistral': 1629, 'n_tokens_neox': 1547, 'n_words': 449}