HiveBrain v1.2.0
Get Started
← Back to all entries
patternpythonMinor

Multiprocess Bogosort

Submitted by: @import:stackexchange-codereview··
0
Viewed 0 times
multiprocessbogosortstackoverflow

Problem

I only program by hobby, and I'm trying to improve with Python, so along with practicing coding I decided to venture into multiprocessing. This is a command line and will sort a variable number of list, timing itself. I also tried to make sure I followed all PEP8 rules.

```
#!/usr/bin/env python
# Random Sort
# supernova2468

import sys
import time
import random
import math
import multiprocessing as mp
import argparse

def go_time():
parser = argparse.ArgumentParser(
description='Makes statistics on how terrible random sorting is.')
parser.add_argument('start', help='what length of list to start at',
type=int)
parser.add_argument('end', help='what length of list to end at', type=int)
parser.add_argument('-t', '--tries',
help='specify number of tries per length default: 100',
type=int, default=100)
parser.add_argument('-c', '--cores',
help='specify number of cores to use default: 1',
type=int)
parser.add_argument('-v', '--verbose',
help='increase output, doesn\'t '
'really work in -c mode',
action='count')
args = parser.parse_args()

start = args.start
end = args.end
tries = args.tries
if args.cores:
cores = args.cores
switch = 1
else:
switch = 2
cores = 1

if args.verbose >= 1:
print 'Checking Parameters..'
if end = 1:
print 'Starting Pool'
print ('Warning: Multiple Process Output '
'Causes Console Confusion')

mp.freeze_support()
pool = mp.Pool(processes=cores)
split_tries = math.ceil(tries/cores)
result_list = []
split_stat_list = []
stat_list = {}

if args.verbose >= 2:
print 'split tries {}'.format(split_tries)

Solution

Good job using ArgumentParser.

It is a very, very nice tool that should be used more often in Python.

Near the top of your code where you are doing a bunch of checks on parameters, you have a sys.exit call if the user did something wrong when entering parameters.

However, you don't pass in any exit number.

The method sys.exit takes a single parameter that is the exit code. This exit code describes how execution of the program went to external programs.

By practice, any non-zero number means that something went wrong. Therefore, I recommending doing one of the following:

-
Return 1 if there was any sort of error in the code.

-
Return 1, 2, 3... for each individual and possible error.

The second one is more preferable because, if you create some documentation for the program, you can include a list of all the possible exit codes and what error they are associated with.

A few times in your code, you have these lines:

if args.verbose >= 1:
    [print statement(s)]


Since these repeat so much, you should create a function probably called print_verbose that takes in a string to print that should only be printed if the user chose for a verbose execution.

Here is what I wrote:

def print_verbose(str):
    if verbose:
        print str


Where verbose is a global variable that was set like this by go_time:

verbose = true if args.verbose >= 1 else false


Hint: This used python's ternary operator.

And, by setting global verbose to a boolean rather than keeping it as a number, you've increased efficiency because you now don't have to do conditional checking upon an integer every time you want to print a string.

Note: this does remove the ability of being able to write args.verbose >= 2, however, in my opinion, if the user wanted verbose output, then they wouldn't mind a few extra lines on their screen.

Your code seems to be following PEP8 very well.

However, while this is not required by PEP8, you do not have any docstrings in your code. Docstrings are always a good practice in python.

Code Snippets

if args.verbose >= 1:
    [print statement(s)]
def print_verbose(str):
    if verbose:
        print str
verbose = true if args.verbose >= 1 else false

Context

StackExchange Code Review Q#96604, answer score: 4

Revisions (0)

No revisions yet.