Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #107469 > unrolled thread
| Started by | Christopher Reimer <christopher_reimer@icloud.com> |
|---|---|
| First post | 2016-04-21 18:34 -0700 |
| Last post | 2016-04-22 12:22 +1000 |
| Articles | 2 — 2 participants |
Back to article view | Back to comp.lang.python
This discussion starts older than the indexed window; earlier articles aren't shown. The article labeled Started by
below is the oldest one visible, not the original post.
Re: How much sanity checking is required for function inputs? Christopher Reimer <christopher_reimer@icloud.com> - 2016-04-21 18:34 -0700
Re: How much sanity checking is required for function inputs? Steven D'Aprano <steve@pearwood.info> - 2016-04-22 12:22 +1000
| From | Christopher Reimer <christopher_reimer@icloud.com> |
|---|---|
| Date | 2016-04-21 18:34 -0700 |
| Subject | Re: How much sanity checking is required for function inputs? |
| Message-ID | <mailman.0.1461288908.2861.python-list@python.org> |
Greetings,
Thanks to everyone for providing feedback. Here's my revised code to
generate a set of chess pieces.
class PieceFactory(object):
def factory(color, piece, position):
if piece == 'Bishop':
return Bishop(color, position)
if piece == 'King':
return King(color, position)
if piece == 'Knight':
return Knight(color, position)
if piece == 'Pawn':
return Pawn(color, position)
if piece == 'Queen':
return Queen(color, position)
if piece == 'Rook':
return Rook(color, position)
raise PieceException('No valid Piece object for factory,
got {}'
' instead'.format(piece))
factory = staticmethod(factory)
def generate_set(color, pieces, positions):
for piece, position in zip(pieces, positions):
yield getattr(PieceFactory, 'factory')(color, piece, position)
The input values for 'pieces' and 'positions' are 16-item lists zipped
together to produce a piece name and a position coordinate for the
factory method. With slight modifications to the code, the factory
method could also return checker pieces.
I got the factory method from here:
http://python-3-patterns-idioms-test.readthedocs.org/en/latest/Factory.html
I do plan to incorporate a sanity test in each Piece class to validate
the initial position value. Pawns have 16 specific positions. Bishop,
Knight and Rook each have four specific positions. King and Queen each
have two specific positions. An invalid value will raise an exception.
Finally, VARS['VARIABLE_NAME'] got change to const['variable_name'].
Should smell better.
Thanks,
Chris R
[toc] | [next] | [standalone]
| From | Steven D'Aprano <steve@pearwood.info> |
|---|---|
| Date | 2016-04-22 12:22 +1000 |
| Message-ID | <57198ad3$0$1604$c3e8da3$5496439d@news.astraweb.com> |
| In reply to | #107469 |
On Fri, 22 Apr 2016 11:34 am, Christopher Reimer wrote:
> Greetings,
>
> Thanks to everyone for providing feedback. Here's my revised code to
> generate a set of chess pieces.
> class PieceFactory(object):
>
> def factory(color, piece, position):
> if piece == 'Bishop':
> return Bishop(color, position)
> if piece == 'King':
> return King(color, position)
> if piece == 'Knight':
> return Knight(color, position)
> if piece == 'Pawn':
> return Pawn(color, position)
> if piece == 'Queen':
> return Queen(color, position)
> if piece == 'Rook':
> return Rook(color, position)
>
> raise PieceException('No valid Piece object for factory,
> got {}'
> ' instead'.format(piece))
>
> factory = staticmethod(factory)
Eww :-)
Creating an entire class with no state just to hold one method is an abuse
of classes. If your class doesn't include both state (data) and behaviour
(methods), it probably shouldn't be a class.
class King: ...
class Queeen: ...
# etc.
PIECES = dict((piece.__name__, piece) for piece in
[King, Queen, Bishop, Knight, Rook, Pawn])
def make_piece(color, name, position):
name = name.title() # Accept 'king', 'KING', 'King' etc.
P = PIECES.get(name, None)
if P is None:
raise PieceException('unknown name %r' % name)
return P(color, position)
> def generate_set(color, pieces, positions):
> for piece, position in zip(pieces, positions):
> yield getattr(PieceFactory, 'factory')(color, piece, position)
def generate_pieces(color, names, positions):
for name, position in zip(names, positions):
yield make_piece(color, name, position)
--
Steven
[toc] | [prev] | [standalone]
Back to top | Article view | comp.lang.python
csiph-web