Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #107388 > unrolled thread

Re: How much sanity checking is required for function inputs?

Started byChristopher Reimer <christopher_reimer@icloud.com>
First post2016-04-19 19:20 -0700
Last post2016-04-19 19:20 -0700
Articles 1 — 1 participant

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.


Contents

  Re: How much sanity checking is required for function inputs? Christopher Reimer <christopher_reimer@icloud.com> - 2016-04-19 19:20 -0700

#107388 — Re: How much sanity checking is required for function inputs?

FromChristopher Reimer <christopher_reimer@icloud.com>
Date2016-04-19 19:20 -0700
SubjectRe: How much sanity checking is required for function inputs?
Message-ID<mailman.9.1461122454.12923.python-list@python.org>
On 4/19/2016 1:02 AM, Michael Selik wrote:

> Why relocate rather than remove? What message would you provide that's
> better than ``KeyError: 42`` with a traceback that shows exactly which
> dictionary is being used and how?

I think you misread my code. No dictionary exception occurs in the 
sanity checks. Below is the full function with the revised sanity check 
for positions that compares the input list with the two valid lists of 
board positions.


def generate_set(color, positions):

     if positions not in [VARS['COORDINATES'][VARS['BOARD_BOTTOM']],
                          VARS['COORDINATES'][VARS['BOARD_TOP']]]:
         raise Exception("List for positions contains no valid 
coordinates, "
                         "got {} instead.".format(positions))

     # generate objects according to color and position
     for position in positions:
         rank, file = position
         if rank in VARS['RANK_NOBILITY']:
             if file in VARS['FILE_ROOK']:
                 yield Rook(color, position)
             elif file in VARS['FILE_BISHOP']:
                 yield Bishop(color, position)
             elif file in VARS['FILE_KNIGHT']:
                 yield Knight(color, position)
             elif file is VARS['FILE_QUEEN']:
                 yield Queen(color, position)
             elif file is VARS['FILE_KING']:
                 yield King(color, position)
         else:
             yield Pawn(color, position)


> I meant, what goes wrong if the number of positions input is other than
> 16? Without these "sanity" checks, your functions might be reusable in,
> say, a checkers game.

Chess has 16 pieces (six types) on each side that are set up on the 
board in a particular order. Checkers has 12 pieces (one type) on each 
side that are set up on alternating squares in no particular order. 
Since I'm writing a chess engine because it has endless supply of 
programming changes, I have no desire to write reusable code for two 
similar but different games at the same time.

Thanks,

Chris R.

[toc] | [standalone]


Back to top | Article view | comp.lang.python


csiph-web