So I finished writing a Python program that handles some sample banking info.
I was told to create a makefile for the assignment that will have build,view and clean targets. I've gotten that much done and it works as anticipated.
However, I was told that my instructor would run the program similar to
accounts -i
where accounts is the program and -i is the arg.
However, I have only been able to use
./accounts -i
to run the program.
I looked around and I found something about adding the file to PATH but I am really lost as to what I am doing. Is there something wrong on my end or is my instructor telling me to do something wrong?
Thanks.
You can add . (the current working directory) to your path with something like (bash/ksh syntax) :
export PATH="$PATH:."
...but this is widely regarded as a security problem.
You're probably best off just typing ./accounts -i. Your instructor will probably already know the issue.
Personally, I would imagine that this was just a simple typo or something like it by your instructor.
In shell scripting, the PATH variables denotes a set of directories (separated by colons) that will be searched for the executable that you are referencing.
So assuming your PATH is equal to ".:/usr/bin", when you type "accounts -i" at the command line, your shell with first search for "./accounts" and then (assuming it does not exist) check for "/usr/bin/accounts". If it finds either it will then execute the one that exists and pass the given arguments (i.e. "-i").
If you would like to try this out, PATH is generally modified like this:
export PATH="directory_name:$PATH"
(as it is most likely that you will be wanting to have "directory_name" take precedence over the rest of what is current in $PATH).
However, "." is generally not included in PATH by default, which is considered to be bad practice by many for reasons detailed here.
So my guess is your instructor either has "." in his or her PATH, or will quickly realize that using "./acccounts" is more appropriate.
Related
For example, let's say I wanted to set a variable in powershell (or command line), and then get that variable later using something like subprocess.check_output("echo $var", shell=True) (this isn't something I need to do, just an example). Or, lets say I wanted to have a user 'cd' to a directory, and be able to run commands from that directory. I would be able to have some python variable that saves the current directory, and always run "cd {dir};{command}", but that would be inefficient, and still wouldn't work for every situation, I would need to add some special bit of code for every possible situation where a user could want to run a command, then run another command which depends on the first command.
Sorry if I phrased this question badly, let me know if I should clarify. TIA!
Ok, after some searching, I found a way to do this. Using a module called pexpect, MarkBaggett on GitHub has made a simple way to do this: pxpowershell. here's an example:
ps = pxpowershell()
ps.start_process()
ps.run("$x = 10")
print(ps.run("echo $x"))
ps.stop_process()
The only small problems are that 1. colors don't work, and 2. you need to decode() and strip() the output, though you can just add that into the pxpowershell.py.
I have a problem and a solution, but frankly I'm not very happy with my solution and think there might be something better.
What I want to achieve:
I start as root (this will be executed from cron eventually). I want to copy a file with a given path, which belongs to a user over to a space that root controls (which implies a change in permissions). This file may be large. I also want to obey the file access permissions of the particular given user while I read the file, as the script will be acting on behalf of the user (and not a different user). I'd also like to do some sensible debugging should some part of this copy fail.
The rest of my code is Python, so ideally I'd like a pure Python way to do this.
In BASH, I can do it like this:
sudo -u <user> dd if=<in_file> | dd of=<out_file>
I have missed out some other flags to simplify things. Sudo looses scope after the pipe so the copied file is written out as root, which is what I want.
After that command, I can query ${PIPESTATUS[*]} to see if the first part or the second part failed, without having to try and parse the error messages.
What I have done to Pythonize it
templateDD="\
sudo -u {user} \
dd if=\'{inFile}\' bs={blockSize} status=none | \
dd of=\'{outFile}\' status=none ; \
echo ${{PIPESTATUS[*]}} \
"
subprocess.run(
templateDD.format(**fileCopyD),
shell=True,
executable='/usr/bin/bash',
stdout=subprocess.PIPE,
stderr=subprocess.PIPE
)
I haven't included fileCopyD, but I'm sure imagination can fill that gap.
Returned is a CompletedProcess object with a single bytestring of the two pipe error states as stdout.
This is a bit ugly, in fact having to make system calls at all is unpleasant as one would rather control everything within Python. Secondly, this command is very dependant on BASH as the PIPESTATUS array is specific to that. In general it is good for code to be more portable, even if BASH is most places.
My thoughts on better solutions
The first part of this that I suspect can be improved is that I could probably get rid of the second dd and the querying of PIPESTATUS. The first dd would then be writing to stdout, which could probably be somehow captured, read into a buffer and written out to a file within the scope of Python code. I don't know how to do that though as even the subprocess.run command that I have used is at the edge of my experience.
I wonder if the sudo -u <user> could also be replaced by something in python though? I had a hint that I might be able to use os.setuid and os.setgid. My perceived problems with this are:
I need to look up the uid of the user, but the UIDs are not in passwd but come from SSSD.
I need to make sure that all group affiliations of the user are taken into account (also from SSSD).
I need to be confident that root has properly become the user with regard to all matters related to read access. I may not properly understand all environmental variables and such which must be set.
If problem 2 was solved, I guess that solves my BASH problem, but I need to make sure that the copied file is written with root permission.
Many thanks in advance for help on this.
I want a function that programmatically returns completion options from either bash or zsh. There are lots of examples of related questions on stackoverflow but no proper, generic answers anywhere. I do NOT want to know how to write a specific completer function for bash.
I've already tried implementing this by reading debian /etc/completion shell code, by echoing control-codes for tab into "bash -i", and even tried using automated subprocess interaction with python-pexpect. Every time I thought I was successful, I find some small problem that invalidates the whole solution. I'd accept a solution in any language, but ideally it would be python. Obviously the exact input output would vary depending on systems, but take a look at the example I/O below:
function("git lo") returns ["log","lol","lola"]
function("apt-get inst") returns ["apt-get install"]
function("apt-get") returns []
function("apt-get ") returns ["apt-get autoclean","apt-get autoremove", ...]
function ("./setup") returns ["./setup.py"]
If you are thinking of a solution written in shell, it would ideally be something I can execute without "source"ing. For instance bash "compgen" command looks interesting (try "compgen -F _git"), but note that "bash -c 'compgen -F _git'" does not work because the completion helper "_git" is not in scope.
This gist is my best solution so far. It meets all the requirements, works well for multiple versions of bash on multiple OS's but it requires a subprocess call and it's so complicated it's absurd. The comments includes full documentation of all the outrageous slings and arrows. I'm still hoping for something more reasonable to come along, but unless it does.. this is it!
While writing an application parsing command line arguments I would like to run it with various parameters.
I don't want to create a Run Configuration for every possible command line argument that I want my script to test with. Is there a way in PyCharm (and I guess with any JetBrains IDE) to make a Run Configuration that asks for the Script parameters when executed?
I am currently using PyCharm 3.1 (EAP).
Currently the only possibility is to use the "Before launch | Show this page" option.
I've found today that now is possible to ask for parameters using the "Prompt" macro on the "Run configuration" parameters field.
https://www.jetbrains.com/help/pycharm/code-running-assistance-tutorial.html#parameter-with-macros
Although yole's answer is the de facto way to be prompted for thw arguments before running a program, it is slightly annoying because:
the dialog is visually overwhelming and cluttered instead of focused on what you want to do;
you have to tab to reach the arguments field if you want to use the keyboard exclusively (and why not?);
Nothing you could do about that. (Except maybe file a ticket. Have you done that?)
I'm just adding what I used to do before I knew about Googled for this option for the sake of completeness (obvously, this is a hack in the least glamorous sense of the term). But it did suit my workflow as I often only had discrete lines to test with, and didn't switch that often.
Create a new configuration set to the same file, but with a special 'magic' parameter;
Add code to your script to check if the magic is there;
Use a string variable instead of sys.argv (pass it through lambda args: [__name__] + args.split() to reduce the boilerplate);
???
Profit;
I'm doing this on a Mac, but hopefully this will be helpful for Windows or Linux.
Go to Run > Edit Configurations
There will be a dialog box that opens.
Script: file you want to run (ending with .py)
Script Parameters: the command line arguments
Working Directory: directory where your project is.
My simple answer is adding another wrapper as the cover in the source code which will run on the selection you made through code branch or external command or file, so choosing different branch is just a 'ddp' tap distance in vim(line change for parameter settings). You dont have to depend on pycharm updating by building your own code world:)
I've been all over the web trying to find a way to get VIM to have code completion similar to PyDev. It doesn't seem like it is possible!
-I have tried to use the omnicompletion suggested at this link: http://blog.dispatched.ch/2009/05/24/vim-as-python-ide/ .
-I have tried several addons to alleviate the problem, none work.
The "omnicomplete" functionality is NOT what I am looking for. It just takes all the words in the file you are working on and uses those to try and complete what I am doing. For example if I wrote:
import numpy
a_single_array = range(100)
np.a#[then I hit cntrl+n to code complete]
It would spit out "a_single_array" as a possible completion -- but that is absurd! That is not a valid completion for "numpy.a ..."
What is the issue here? All the addon would have to do is run a dir(work you want to find) from the folder you are in and then filter the output! This cannot be that difficult! (I suppose you would also have to read the file you are currently editing and filter that as well to take note of name changes... but that's pretty much it!)
Speaking of how easy it would be... if there isn't anything already made, I was thinking of writing the script myself! Any guides on how to do THAT?
No, the omni completion functionality is EXACTLY what you are looking for.
You are using <C-n> instead of <C-x><C-o>:
type <C-n> & <C-p> to complete with words from the buffer (after and before the cursor respectively)
type <C-x><C-o> to complete method/properties names
It's specifically explained in the article you linked:
In V7, VIM introduced omni completion – given it is configured to recognize Python (if not, this feature is only a plugin away) Ctrl+x Ctrl+o opens a drop down dialog like any other IDE – even the whole Pydoc gets to be displayed in a split window.
Ctrln is insert-completion.
Ctrlx Ctrlo is omni-completion.
I remap omnicompletion to CtrlSpace:
inoremap <C-Space> <C-x><C-o>
You could also try SuperTab.
I have no idea about the various completion options for Python in Vim. But if you want to roll your own you'd be well advised to study and modify one of the existing ones, like this:
http://www.vim.org/scripts/script.php?script_id=1542
Also, if all your omnicompletion is doing is listing words in current file then you don't have it set up properly for Python-specific completion. . . . Not sure how good the specialized Python completion systems get, but they certainly does compete based on Python units external to your current file. . . .