Python Forum
Power Shells vs Compile and Run programs?
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Power Shells vs Compile and Run programs?
#1
Good morning,

I decided to take a crack at the edx machine learning course, and I had a question about how to approach writing the code.

They seem to run the programs from a powershell using the command line interface whereas everything I've done so far has been compile and run sort of code.

The code provided in the class is shown below, and I can call it from a powershell by typing in: python maze.py maze.txt

if len(sys.argv) != 2:
    sys.exit("Usage: python maze - DFS.py maze.txt")

m = Maze(sys.argv[1])
print("Maze:")
m.print()
print("Solving...")
m.solve()
print("States Explored:", m.num_explored)
print("Solution:")
m.print()
m.output_image("maze.png", show_explored=True)
Is the code above a better way to approach these types of problems, or should I aim to import the files and compile them together? I mean, I can compile and run it if I do something like this:

#I added this part to make it work
sys.argv = ["maze - DFS.py", "maze.txt"]
#I added this part to make it work

if len(sys.argv) != 2:
    sys.exit("Usage: python maze - DFS.py maze.txt")

m = Maze(sys.argv[1])
print("Maze:")
m.print()
print("Solving...")
m.solve()
print("States Explored:", m.num_explored)
print("Solution:")
m.print()
m.output_image("maze.png", show_explored=True)
Reply
#2
The first solution is more flexible because it lets the user of the program specify the name of the text file contaning the maze, while in the second solution, it can only use the file 'maze.txt' from the current working directory.

Python's standard library has a very good module to write command line interfaces: the argparse module. Here it allows you to specify a default text file while allowing the user to choose another text file
from argparse import ArgumentParser

parser = ArgumentParser(description="Solve a maze and create an image of the solution.")
parser.add_argument(
    "mazefile", nargs="?", default="maze.txt", action="store", metavar="MAZEFILE"
)

args = parser.parse_args()

print("I will use the file:", args.mazefile)
Show help:
Output:
λ python paillasse/pf/mazeargs.py -h usage: mazeargs.py [-h] [MAZEFILE] Solve a maze and create an image of the solution. positional arguments: MAZEFILE options: -h, --help show this help message and exit
Use another file:
Output:
λ python paillasse/pf/mazeargs.py foo.txt I will use the file: foo.txt
Use the default file:
Output:
λ python paillasse/pf/mazeargs.py I will use the file: maze.txt
RockBlok likes this post
« We can solve any problem by introducing an extra level of indirection »
Reply
#3
Oh, that's awesome..

Thank you for the helpful explanation and the example of how to set up the code to choose between files.

I'll read more on the argparse module for sure. I do see what you are saying now that I've run through a few of these different searches. It's definitely easier to switch files on the command line than having to recompile each time.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Using a script to open multiple shells? SuchUmami 9 543 Apr-01-2024, 10:04 AM
Last Post: Gribouillis
  Python Coding in Power BI Visual vjrans 0 278 Jan-31-2024, 07:54 AM
Last Post: vjrans
  How come my python shells are different? SuchUmami 2 612 Jul-10-2023, 08:00 PM
Last Post: snippsat
Question Python and Power BI Desktop dangermaus33 1 1,320 Jan-19-2023, 06:54 AM
Last Post: GetOnData
  Raising numbers to power ** GJG 3 2,455 Mar-23-2021, 03:43 PM
Last Post: deanhystad
  Detecting power plug Narayan 2 2,727 Aug-01-2020, 04:29 AM
Last Post: bowlofred
  NameError: name 'display' is not defined when running code on power bi beginner1 2 19,212 Jul-24-2019, 11:03 AM
Last Post: beginner1
  power spectrum from 2Dfft sdipti596 1 2,327 Mar-30-2019, 12:34 AM
Last Post: scidam
  python result problem of an iteration algorithm for power allocation Jessica 1 2,642 Sep-07-2018, 08:08 PM
Last Post: micseydel

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020