7 Replies - 428 Views - Last Post: 25 July 2012 - 08:28 PM Rate Topic: -----

#1 carnivroar  Icon User is offline

  • D.I.C Regular

Reputation: 28
  • View blog
  • Posts: 382
  • Joined: 18-September 11

Running program from command line with options

Posted 25 July 2012 - 02:09 PM

Is this possible?

I want to be able to write this on the command line:

$ crypt.py e matrix.txt input.txt output.txt

where:

crypt.py is the program that encodes/decodes messages
e tells the program I want to encode (d would be decode)
matrix.txt is a file containing the matrix I'm using to encode/decode
input.txt is the file I want to encode/decode
output.txt is the output file with the encoded/decoded message

Sort of like what compilers do... is it doable?

This post has been edited by carnivroar: 25 July 2012 - 02:09 PM


Is This A Good Question/Topic? 0
  • +

Replies To: Running program from command line with options

#2 atraub  Icon User is offline

  • Pythoneer
  • member icon

Reputation: 759
  • View blog
  • Posts: 2,010
  • Joined: 23-December 08

Re: Running program from command line with options

Posted 25 July 2012 - 02:19 PM

absolutely doable! The simplest way is using sys.argv. However, if you want to get into more advance techniques, you can use the argparse module.
Was This Post Helpful? 2
  • +
  • -

#3 carnivroar  Icon User is offline

  • D.I.C Regular

Reputation: 28
  • View blog
  • Posts: 382
  • Joined: 18-September 11

Re: Running program from command line with options

Posted 25 July 2012 - 02:35 PM

View Postatraub, on 25 July 2012 - 02:19 PM, said:

absolutely doable! The simplest way is using sys.argv. However, if you want to get into more advance techniques, you can use the argparse module.


Thanks! Will certainly look into those...

Alternatively I could do this

$ crypt.py
(programming is waiting for input)
$ e matrix.txt input.txt output.txt

But that's not as cool. :alien2:
Was This Post Helpful? 0
  • +
  • -

#4 carnivroar  Icon User is offline

  • D.I.C Regular

Reputation: 28
  • View blog
  • Posts: 382
  • Joined: 18-September 11

Re: Running program from command line with options

Posted 25 July 2012 - 07:54 PM

Like this? That was very simple, thanks.

from LinearAlgebra import *
import math
import sys

def encrypt(A, msg):
	M = [[0 for i in range(A.getRows())] for j in range(int(math.ceil(len(msg)/A.getRows())))]
	for i in range(len(M)):
		for j in range(len(M[i])):
			try:		
				M[i][j] = Rational(ord(msg[i*A.getRows()+j]))		
			except IndexError: 
				M[i][j] = Rational(ord(" "))
	return Matrix(M)*A

def decrypt(A, M):
	return M*A.inverse()

def main():
	args = ["" for i in range (5)]
	for i in range(1, 5):
		 args[i] = sys.argv[i]
	A = Matrix.load(args[2])
	if (args[1][0] == "e" or args[1][0] == "E"):
		encrypted = encrypt(A, open(args[3], "r").read())
		encrypted.save(args[4])
	elif (args[1][0] == "d" or args[1][0] == "D"):
		decrypted = decrypt(A, Matrix.load(args[3]))
		open(args[4], "a").write(decrypted.message())
	else:
		raise Exception("Invalid option: " + str(args[1]))

main()


This post has been edited by carnivroar: 25 July 2012 - 07:58 PM

Was This Post Helpful? 0
  • +
  • -

#5 atraub  Icon User is offline

  • Pythoneer
  • member icon

Reputation: 759
  • View blog
  • Posts: 2,010
  • Joined: 23-December 08

Re: Running program from command line with options

Posted 25 July 2012 - 08:04 PM

Nice, you're gettin' it :)

Question for ya though, your matrix class uses a lot of slicing, why not use a little here?
	args = ["" for i in range (5)]
	for i in range(1, 5):
		 args[i] = sys.argv[i]


This post has been edited by atraub: 25 July 2012 - 08:06 PM

Was This Post Helpful? 0
  • +
  • -

#6 carnivroar  Icon User is offline

  • D.I.C Regular

Reputation: 28
  • View blog
  • Posts: 382
  • Joined: 18-September 11

Re: Running program from command line with options

Posted 25 July 2012 - 08:09 PM

Oh yeah, that was trivial.

args = [sys.argv[i] for i in range (5)]
Was This Post Helpful? 0
  • +
  • -

#7 atraub  Icon User is offline

  • Pythoneer
  • member icon

Reputation: 759
  • View blog
  • Posts: 2,010
  • Joined: 23-December 08

Re: Running program from command line with options

Posted 25 July 2012 - 08:14 PM

Haha I was going with slicing. So I was thinking something like,args = sys.argv[1:6], but a comprehension also works.
Was This Post Helpful? 0
  • +
  • -

#8 carnivroar  Icon User is offline

  • D.I.C Regular

Reputation: 28
  • View blog
  • Posts: 382
  • Joined: 18-September 11

Re: Running program from command line with options

Posted 25 July 2012 - 08:28 PM

View Postatraub, on 25 July 2012 - 08:14 PM, said:

Haha I was going with slicing. So I was thinking something like,args = sys.argv[1:6], but a comprehension also works.


You beat me!
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1