Skip to main content
Code Review

Return to Answer

Commonmark migration
Source Link

#Generic comments

Generic comments

When dealing with command-line arguments, it is often more efficient to reuse an existing framework rather than trying to reinvent the weel. I’ll use argparse as an example but there is also getopt in the standard library as well as many others available on PyPi. Using these framework will let you separate input validation and actual computation.

Next you should write function that performs small tasks and combine them together to achieve your goal. This way, it is simpler to test and debug if anything goes wrong.

You should also get familiar with the str.format syntax to avoid all those str() calls and strings concatenation.

Lastly, using if __name__ == '__main__' you can make your program works both as a command line tool or an importable module. Usefull for debug or reusability purposes.

#Optimizations

Optimizations

  • A regex used several times should be compiled to improve its efficiency.
  • You can use sum and generator expressions to improve your computation
  • You should use randint instead of randrange.

#Proposed improvements

Proposed improvements

#!/usr/bin/env python
"""roll D&D dice"""
import re
import argparse
from random import randint
DICE_PATTERN = re.compile(r'^(\d*)d(\d+)$')
def roll(sides):
 value = randint(1, sides)
 print '\troll: {}'.format(value)
 return value
def rolls(die):
 for count, sides in die:
 print '{}d{}:'.format(count, sides)
 total = sum(roll(sides) for _ in xrange(count))
 print '\ttotal: {}/{}'.format(total, count*sides)
def dice(string):
 matched, = re.findall(DICE_PATTERN, string)
 try:
 return map(int, matched)
 except ValueError:
 # Catching the case of 'd6' which will put ('', '6') in matched
 return (1, int(matched[1]))
if __name__ == '__main__':
 parser = argparse.ArgumentParser('Dice roller', description=__doc__)
 parser.add_argument('dice', nargs='+', type=dice)
 args = parser.parse_args()
 rolls(args.dice)

#Generic comments

When dealing with command-line arguments, it is often more efficient to reuse an existing framework rather than trying to reinvent the weel. I’ll use argparse as an example but there is also getopt in the standard library as well as many others available on PyPi. Using these framework will let you separate input validation and actual computation.

Next you should write function that performs small tasks and combine them together to achieve your goal. This way, it is simpler to test and debug if anything goes wrong.

You should also get familiar with the str.format syntax to avoid all those str() calls and strings concatenation.

Lastly, using if __name__ == '__main__' you can make your program works both as a command line tool or an importable module. Usefull for debug or reusability purposes.

#Optimizations

  • A regex used several times should be compiled to improve its efficiency.
  • You can use sum and generator expressions to improve your computation
  • You should use randint instead of randrange.

#Proposed improvements

#!/usr/bin/env python
"""roll D&D dice"""
import re
import argparse
from random import randint
DICE_PATTERN = re.compile(r'^(\d*)d(\d+)$')
def roll(sides):
 value = randint(1, sides)
 print '\troll: {}'.format(value)
 return value
def rolls(die):
 for count, sides in die:
 print '{}d{}:'.format(count, sides)
 total = sum(roll(sides) for _ in xrange(count))
 print '\ttotal: {}/{}'.format(total, count*sides)
def dice(string):
 matched, = re.findall(DICE_PATTERN, string)
 try:
 return map(int, matched)
 except ValueError:
 # Catching the case of 'd6' which will put ('', '6') in matched
 return (1, int(matched[1]))
if __name__ == '__main__':
 parser = argparse.ArgumentParser('Dice roller', description=__doc__)
 parser.add_argument('dice', nargs='+', type=dice)
 args = parser.parse_args()
 rolls(args.dice)

Generic comments

When dealing with command-line arguments, it is often more efficient to reuse an existing framework rather than trying to reinvent the weel. I’ll use argparse as an example but there is also getopt in the standard library as well as many others available on PyPi. Using these framework will let you separate input validation and actual computation.

Next you should write function that performs small tasks and combine them together to achieve your goal. This way, it is simpler to test and debug if anything goes wrong.

You should also get familiar with the str.format syntax to avoid all those str() calls and strings concatenation.

Lastly, using if __name__ == '__main__' you can make your program works both as a command line tool or an importable module. Usefull for debug or reusability purposes.

Optimizations

  • A regex used several times should be compiled to improve its efficiency.
  • You can use sum and generator expressions to improve your computation
  • You should use randint instead of randrange.

Proposed improvements

#!/usr/bin/env python
"""roll D&D dice"""
import re
import argparse
from random import randint
DICE_PATTERN = re.compile(r'^(\d*)d(\d+)$')
def roll(sides):
 value = randint(1, sides)
 print '\troll: {}'.format(value)
 return value
def rolls(die):
 for count, sides in die:
 print '{}d{}:'.format(count, sides)
 total = sum(roll(sides) for _ in xrange(count))
 print '\ttotal: {}/{}'.format(total, count*sides)
def dice(string):
 matched, = re.findall(DICE_PATTERN, string)
 try:
 return map(int, matched)
 except ValueError:
 # Catching the case of 'd6' which will put ('', '6') in matched
 return (1, int(matched[1]))
if __name__ == '__main__':
 parser = argparse.ArgumentParser('Dice roller', description=__doc__)
 parser.add_argument('dice', nargs='+', type=dice)
 args = parser.parse_args()
 rolls(args.dice)
replaced http://stackoverflow.com/ with https://stackoverflow.com/
Source Link

#Generic comments

When dealing with command-line arguments, it is often more efficient to reuse an existing framework rather than trying to reinvent the weel. I’ll use argparse as an example but there is also getopt in the standard library as well as many others available on PyPi. Using these framework will let you separate input validation and actual computation.

Next you should write function that performs small tasks and combine them together to achieve your goal. This way, it is simpler to test and debug if anything goes wrong.

You should also get familiar with the str.format syntax to avoid all those str() calls and strings concatenation.

Lastly, using if __name__ == '__main__' if __name__ == '__main__' you can make your program works both as a command line tool or an importable module. Usefull for debug or reusability purposes.

#Optimizations

  • A regex used several times should be compiled to improve its efficiency.
  • You can use sum and generator expressions to improve your computation
  • You should use randint instead of randrange.

#Proposed improvements

#!/usr/bin/env python
"""roll D&D dice"""
import re
import argparse
from random import randint
DICE_PATTERN = re.compile(r'^(\d*)d(\d+)$')
def roll(sides):
 value = randint(1, sides)
 print '\troll: {}'.format(value)
 return value
def rolls(die):
 for count, sides in die:
 print '{}d{}:'.format(count, sides)
 total = sum(roll(sides) for _ in xrange(count))
 print '\ttotal: {}/{}'.format(total, count*sides)
def dice(string):
 matched, = re.findall(DICE_PATTERN, string)
 try:
 return map(int, matched)
 except ValueError:
 # Catching the case of 'd6' which will put ('', '6') in matched
 return (1, int(matched[1]))
if __name__ == '__main__':
 parser = argparse.ArgumentParser('Dice roller', description=__doc__)
 parser.add_argument('dice', nargs='+', type=dice)
 args = parser.parse_args()
 rolls(args.dice)

#Generic comments

When dealing with command-line arguments, it is often more efficient to reuse an existing framework rather than trying to reinvent the weel. I’ll use argparse as an example but there is also getopt in the standard library as well as many others available on PyPi. Using these framework will let you separate input validation and actual computation.

Next you should write function that performs small tasks and combine them together to achieve your goal. This way, it is simpler to test and debug if anything goes wrong.

You should also get familiar with the str.format syntax to avoid all those str() calls and strings concatenation.

Lastly, using if __name__ == '__main__' you can make your program works both as a command line tool or an importable module. Usefull for debug or reusability purposes.

#Optimizations

  • A regex used several times should be compiled to improve its efficiency.
  • You can use sum and generator expressions to improve your computation
  • You should use randint instead of randrange.

#Proposed improvements

#!/usr/bin/env python
"""roll D&D dice"""
import re
import argparse
from random import randint
DICE_PATTERN = re.compile(r'^(\d*)d(\d+)$')
def roll(sides):
 value = randint(1, sides)
 print '\troll: {}'.format(value)
 return value
def rolls(die):
 for count, sides in die:
 print '{}d{}:'.format(count, sides)
 total = sum(roll(sides) for _ in xrange(count))
 print '\ttotal: {}/{}'.format(total, count*sides)
def dice(string):
 matched, = re.findall(DICE_PATTERN, string)
 try:
 return map(int, matched)
 except ValueError:
 # Catching the case of 'd6' which will put ('', '6') in matched
 return (1, int(matched[1]))
if __name__ == '__main__':
 parser = argparse.ArgumentParser('Dice roller', description=__doc__)
 parser.add_argument('dice', nargs='+', type=dice)
 args = parser.parse_args()
 rolls(args.dice)

#Generic comments

When dealing with command-line arguments, it is often more efficient to reuse an existing framework rather than trying to reinvent the weel. I’ll use argparse as an example but there is also getopt in the standard library as well as many others available on PyPi. Using these framework will let you separate input validation and actual computation.

Next you should write function that performs small tasks and combine them together to achieve your goal. This way, it is simpler to test and debug if anything goes wrong.

You should also get familiar with the str.format syntax to avoid all those str() calls and strings concatenation.

Lastly, using if __name__ == '__main__' you can make your program works both as a command line tool or an importable module. Usefull for debug or reusability purposes.

#Optimizations

  • A regex used several times should be compiled to improve its efficiency.
  • You can use sum and generator expressions to improve your computation
  • You should use randint instead of randrange.

#Proposed improvements

#!/usr/bin/env python
"""roll D&D dice"""
import re
import argparse
from random import randint
DICE_PATTERN = re.compile(r'^(\d*)d(\d+)$')
def roll(sides):
 value = randint(1, sides)
 print '\troll: {}'.format(value)
 return value
def rolls(die):
 for count, sides in die:
 print '{}d{}:'.format(count, sides)
 total = sum(roll(sides) for _ in xrange(count))
 print '\ttotal: {}/{}'.format(total, count*sides)
def dice(string):
 matched, = re.findall(DICE_PATTERN, string)
 try:
 return map(int, matched)
 except ValueError:
 # Catching the case of 'd6' which will put ('', '6') in matched
 return (1, int(matched[1]))
if __name__ == '__main__':
 parser = argparse.ArgumentParser('Dice roller', description=__doc__)
 parser.add_argument('dice', nargs='+', type=dice)
 args = parser.parse_args()
 rolls(args.dice)
Typo + added comment
Source Link

#Generic comments

When dealing with command-line arguments, it is often more efficient to reuse an existing framework rather than trying to reinvent the weel. I’ll use argparse as an example but there is also getopt in the standard library as well as many others available on PyPi. Using these framework will let you separate input validation and actual conputationcomputation.

Next you should write function that performs small tasks and combine them together to achieve your goal. This way, it is simpler to test and debug if anything goes wrong.

You should also get familiar with the str.format syntax to avoid all those str() calls and strings concatenation.

Lastly, using if __name__ == '__main__' you can make your program works both as a command line tool or an importable module. Usefull for debug or reusability purposes.

#Optimizations

  • A regex used several times should be compiled to improve its efficiency.
  • You can use sum and generator expressions to improve your computation
  • You should use randint instead of randrange.

#Proposed improvements

#!/usr/bin/env python
"""roll D&D dice"""
import re
import argparse
from random import randint
DICE_PATTERN = re.compile(r'^(\d*)d(\d+)$')
def roll(sides):
 value = randint(1, sides)
 print '\troll: {}'.format(value)
 return value
def rolls(die):
 for count, sides in die:
 print '{}d{}:'.format(count, sides)
 total = sum(roll(sides) for _ in xrange(count))
 print '\ttotal: {}/{}'.format(total, count*sides)
def dice(string):
 matched, = re.findall(DICE_PATTERN, string)
 try:
 return map(int, matched)
 except ValueError:
 # Catching the case of 'd6' which will put ('', '6') in matched
 return (1, int(matched[1]))
if __name__ == '__main__':
 parser = argparse.ArgumentParser('Dice roller', description=__doc__)
 parser.add_argument('dice', nargs='+', type=dice)
 args = parser.parse_args()
 rolls(args.dice)

#Generic comments

When dealing with command-line arguments, it is often more efficient to reuse an existing framework rather than trying to reinvent the weel. I’ll use argparse as an example but there is also getopt in the standard library as well as many others available on PyPi. Using these framework will let you separate input validation and actual conputation.

Next you should write function that performs small tasks and combine them together to achieve your goal. This way, it is simpler to test and debug if anything goes wrong.

You should also get familiar with the str.format syntax to avoid all those str() calls and strings concatenation.

Lastly, using if __name__ == '__main__' you can make your program works both as a command line tool or an importable module. Usefull for debug or reusability purposes.

#Optimizations

  • A regex used several times should be compiled to improve its efficiency.
  • You can use sum and generator expressions to improve your computation
  • You should use randint instead of randrange.

#Proposed improvements

#!/usr/bin/env python
"""roll D&D dice"""
import re
import argparse
from random import randint
DICE_PATTERN = re.compile(r'^(\d*)d(\d+)$')
def roll(sides):
 value = randint(1, sides)
 print '\troll: {}'.format(value)
 return value
def rolls(die):
 for count, sides in die:
 print '{}d{}:'.format(count, sides)
 total = sum(roll(sides) for _ in xrange(count))
 print '\ttotal: {}/{}'.format(total, count*sides)
def dice(string):
 matched, = re.findall(DICE_PATTERN, string)
 try:
 return map(int, matched)
 except ValueError:
 return (1, int(matched[1]))
if __name__ == '__main__':
 parser = argparse.ArgumentParser('Dice roller', description=__doc__)
 parser.add_argument('dice', nargs='+', type=dice)
 args = parser.parse_args()
 rolls(args.dice)

#Generic comments

When dealing with command-line arguments, it is often more efficient to reuse an existing framework rather than trying to reinvent the weel. I’ll use argparse as an example but there is also getopt in the standard library as well as many others available on PyPi. Using these framework will let you separate input validation and actual computation.

Next you should write function that performs small tasks and combine them together to achieve your goal. This way, it is simpler to test and debug if anything goes wrong.

You should also get familiar with the str.format syntax to avoid all those str() calls and strings concatenation.

Lastly, using if __name__ == '__main__' you can make your program works both as a command line tool or an importable module. Usefull for debug or reusability purposes.

#Optimizations

  • A regex used several times should be compiled to improve its efficiency.
  • You can use sum and generator expressions to improve your computation
  • You should use randint instead of randrange.

#Proposed improvements

#!/usr/bin/env python
"""roll D&D dice"""
import re
import argparse
from random import randint
DICE_PATTERN = re.compile(r'^(\d*)d(\d+)$')
def roll(sides):
 value = randint(1, sides)
 print '\troll: {}'.format(value)
 return value
def rolls(die):
 for count, sides in die:
 print '{}d{}:'.format(count, sides)
 total = sum(roll(sides) for _ in xrange(count))
 print '\ttotal: {}/{}'.format(total, count*sides)
def dice(string):
 matched, = re.findall(DICE_PATTERN, string)
 try:
 return map(int, matched)
 except ValueError:
 # Catching the case of 'd6' which will put ('', '6') in matched
 return (1, int(matched[1]))
if __name__ == '__main__':
 parser = argparse.ArgumentParser('Dice roller', description=__doc__)
 parser.add_argument('dice', nargs='+', type=dice)
 args = parser.parse_args()
 rolls(args.dice)
Source Link
Loading
lang-py

AltStyle によって変換されたページ (->オリジナル) /