Newer
Older
####################################################################################################
#
# Patro - A Python library to make patterns for fashion design
# Copyright (C) 2018 Fabrice Salvaire
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
#
####################################################################################################
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
__all__ = ['ObjectNameMixin', 'ObjectGlobalIdMixin']
####################################################################################################
from .AtomicCounter import AtomicCounter
####################################################################################################
class ObjectNameMixin:
"""Mixin for object with name"""
##############################################
def __init__(self, name=None):
self.name = name
##############################################
@property
def name(self):
return self._name
@name.setter
def name(self, value):
if value is None:
self._name = None
else:
self._name = str(value)
##############################################
def __repr__(self):
return self.__class__.__name__ + ' {0._name}'.format(self)
##############################################
def __str__(self):
return self._name
####################################################################################################
class ObjectGlobalIdMixin:
"""Mixin for object with a global id"""
__object_counter__ = AtomicCounter(-1)
##############################################
# Note: sub-classes share the same counter !
if id is not None:
ObjectGlobalIdMixin.__object_counter__.set(id)
self._id = id
else:
self._id = ObjectGlobalIdMixin.__object_counter__.increment()
##############################################
@property
def id(self):
return self._id
##############################################
def __int__(self):
return self._id
##############################################
def __repr__(self):
return self.__class__.__name__ + ' {0._id}'.format(self)
####################################################################################################
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
"""Mixin for object with id"""
##############################################
def __init__(self, id=None):
if id is None:
self._id = self.new_id()
else:
self.check_id(id)
self._id = id
##############################################
@property
def id(self):
return self._id
##############################################
def __repr__(self):
return self.__class__.__name__ + ' {0._id}'.format(self)
##############################################
def __int__(self):
return self._id
##############################################
def new_id(self):
raise NotImplementedError
##############################################
def check_id(self, id):
return True