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

Data reduction program

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

Problem

This is my first program in Python and first time I'm writing in dynamically typed language. I have substituted real detectPeaks function with a simple placeholer. I'm aware of several problems with this program.

I still don't quite know how to 'live with' Python scoping, hence the qFixfunction. I didn't bother to find a better name since I want to rewrite whole program.

Doing anything outside of any function seems unclean to me. It seems that it is ok in Python. Am I right? # It did allow me to have global cmdArgs object witch makes logic connected to cmd arguments clean.

I'm also concerned with naming functions and variables and structure of the program. The program does not have to be very flexible I would get away with hardcoding most of the things but prefer not unless it would complicate code significantly.

```
__version__ = dirt3

import sys, os.path, argparse, glob
import numpy as np

def detectPeaks(in_list):
in_list = [s.replace(',', '.') for s in in_list]
in_list.pop() # remove "\r\n"
X = np.array(map(float, in_list[:2]))
Y = np.array(map(float, in_list[2:]))

XminIndex = np.argmin(X)
YminIndex = np.argmin(Y)
val = X[XminIndex] + Y[YminIndex]

return (XminIndex, YminIndex, val)

def processFileLine(line):
No, time, A, B = line.split('\t')[0], line.split('\t')[1], line.split("\t")[2::2], line.split("\t")[3::2]

if cmdArgs.A:
retA = detectPeaks(A)
ansA = No + '\t' + time + '\t' + str(retA[0]) + '\t' + str(retA[1]) + '\t' + str(retA[2]) + '\n'
else:
ansA = "" #to get read of Unbound local var err
if cmdArgs.B:
retB = detectPeaks(B)
ansB = No + '\t' + time + '\t' + str(retB[0]) + '\t' + str(retB[1]) + '\t' + str(retB[2]) + '\n'
else:
ansB = ""

return ansA + ansB

def mkOutFilePath(outDirPath, inFilePath):
inFilename = os.path.basename(inFilePath)
return os.path.join(outDirPath, inFilename + ".peak")

def qFix(inFilePath):
if inFilePat

Solution

Spelling counts

The usability of your programme suffers if a flag is named recuresive, delete the second e.

Repeat again

You repeated a function in the docs of another function, why?

Main should take args

Main should take the command line flags as args and those should appear only in main, as it performs IO and other functions logic.

Automatically closing files

Use with to automatically close files. Not the manual open. open may leave the file open if an exception occurs.

Context

StackExchange Code Review Q#96991, answer score: 3

Revisions (0)

No revisions yet.