[opensuse-programming] day 1 python question / review
Any python programmers here? I'm am (or was) a c programmer, but I want to learn python. My goal is to leverage a python library I maintain (python-dfVFS). I've just written my first python program and it actually works! I wrote it in python 2.7, but I wonder if should skip straight to learning python 3? The code base I want to call into is currently in python 2.7, so I'm assuming that I should write my code in 2.7, but if it is easy to call 2.7 code from 3.0, I think I'd rather do that. Anyway, can someone one advise as to if I'm doing anything below in a non-python way? Here's my program. It reads a rather strange CSV file format and converts it to a normal CSV. I've attached one line of the strange CSV if someone actually wants to run this. (This is a real world problem for me. I actually have several of these DAT files I need to convert to standard CSV.) ============================= #!/usr/bin/python from __future__ import print_function import sys import os charOld = ['"', '\xfe','\x14'] # find these... charNew = ['""','"',','] # ...and replace with these #-----Need some help?------- def Usage(errMsg): print("Error: ", errMsg, file=sys.stderr) print("Usage:", file=sys.stderr) print(sys.argv[0], " [input filename] [output filename]\n", "NB: Output file will be created, overwriting any existing\n", "file of the same name.", file=sys.stderr) os._exit(0) return if len(sys.argv) < 3: Usage('Arguments are required') input=open(sys.argv[1], 'r') output=open(sys.argv[2], 'w') while 1: line = input.readline() if not line: break line1=line.replace(charOld[0], charNew[0]); line2=line1.replace(charOld[1], charNew[1]); line3=line2.replace(charOld[2], charNew[2]); output.write(line3) ============================================= Should I have the equivalent of main()? Should I have an exit at the bottom of the file? fyi: The next step is to accept multiple input files, concatenate them while stripping the header from all but the first file, and output the result to a single integrated file. Thanks Greg -- Greg Freemyer
On 2015-01-23 19:24:20 -0500, Greg Freemyer wrote:
Any python programmers here?
I'm am (or was) a c programmer, but I want to learn python.
My goal is to leverage a python library I maintain (python-dfVFS).
I've just written my first python program and it actually works!
I wrote it in python 2.7, but I wonder if should skip straight to learning python 3? The code base I want to call into is currently in python 2.7, so I'm assuming that I should write my code in 2.7, but if it is easy to call 2.7 code from 3.0, I think I'd rather do that.
Anyway, can someone one advise as to if I'm doing anything below in a non-python way?
See the comments below. <SNIP>
charOld = ['"', '\xfe','\x14'] # find these... charNew = ['""','"',','] # ...and replace with these
#-----Need some help?------- def Usage(errMsg): print("Error: ", errMsg, file=sys.stderr) print("Usage:", file=sys.stderr) print(sys.argv[0], " [input filename] [output filename]\n", "NB: Output file will be created, overwriting any existing\n", "file of the same name.", file=sys.stderr) os._exit(0)
You should use sys.exit instead of os._exit. Also, it is probably better to indicate the failure with a non zero exit status (e.g. 2 to indicate wrong arguments/usage).
return
You can omit this return statement.
if len(sys.argv) < 3: Usage('Arguments are required')
input=open(sys.argv[1], 'r') output=open(sys.argv[2], 'w')
I would use python's with statement, e.g. with open(sys.argv[1], 'r') as input, open(sys.argv[2], 'w') as output: while 1: ... The advantage is that in case of an exception, the file objects are automatically closed.
while 1: line = input.readline() if not line: break line1=line.replace(charOld[0], charNew[0]); line2=line1.replace(charOld[1], charNew[1]); line3=line2.replace(charOld[2], charNew[2]); output.write(line3)
This could be rewritten as follows: for line in input.readlines(): for old, new in zip(charOld, charNew): line = line.replace(old, new) output.write(line) Note: the comments above are just some suggestions:) Marcus -- To unsubscribe, e-mail: opensuse-programming+unsubscribe@opensuse.org To contact the owner, e-mail: opensuse-programming+owner@opensuse.org
Marcus answered most of the issues. On 01/23/2015 07:24 PM, Greg Freemyer wrote:
Should I have the equivalent of main()? Not necessary. Python begins execution from the start of the file.
Should I have an exit at the bottom of the file? Not necessary.
Note that you do not need a return at the end of functions unless you
want to return a value.
I usually set up my stuff as object oriented. Things like:
#!/usr.bin/python
"""
Some documentation
"""
class MyClass:
def __init__(self):
... init stuff
# more methods
def aMethod(self):
---code--
if __name__ == "__main__":
# instantiate MyClass - this will cause __init__() to run.
# the above if statement is true when the program is executed by
hand. But if you include it as a module
# this will not be executed. Note that the variable, 'myClass', can
be used to access class variables or run class methods.
# normally I use the __init__() function to start everything.
myClass = MyClass()
--
Jerry Feldman
participants (3)
-
Greg Freemyer
-
Jerry Feldman
-
Marcus Hüwe