Node scripting in command line for testing? - python

I'm using Node for a lot of small JSON manipulation scripts, similarly to how one would use Python.
However, every time I want to change my output, I obviously have to edit my script file, save it, and run it using the node command.
Is there perhaps a way to directly write scripts in a "live" command prompt, similarly to a python shell / jupyter notebook?

Yes - there is.
You can use node REPL.
REPL stands for Read-Eval-Print-Loop and is the tool you are looking for.
See here for more info.

Related

Change directory in Jupyter Lab not working

I ran the commands attached below in my command line and it works, as it should, but not in JupyterLab. It seems odd but I was wondering what's going on?
The !cd datasets command did work. However, you aren't understanding what is going on with the use of the exclamation point. What the exclamation point does is open a separate temporary shell instance and does work returning what gets returned. The separate shell instance goes away. Poof
What you were trying to do was change the working directory within your notebook. And so you wanted to use:
%cd dataset
You'll want to learn about the many IPython/ Jupyter magic commands and use them. See here for the IPython ones that Jupyter inherits as Jupyter grew out of the IPython notebook project and so when using a Python-based kernel, you have those utilities. There are some others that are cell and line specific and specific to Jupyter. Here looks like good overview of this.
Note that generally in modern Jupyter, auto-magics is enabled by default. For all the shell commands you show in your post, you want to use the magic command equivalents. And so you are better off trying without any symbol first. Auto-magics will usually add the % behind the scenes. If that fails, then add the % if you are sure there is a magic equivalent that is the same as a shell command because maybe automagics aren't on by default on the system you are on. Finally, some of the similar tasks you'd perform in the shell have different syntax as a magic command in IPython/Jupyter. The example of the %store line magic comes to mind. It makes sense it is different though because the main thing it does is different than shell. However, there is some overlap with abilities the shell has. For example %store foo >a.txt that lets you send a value of a Python variable directly into a text file. Note that it doesn't allow a space after the redirect symbol! Unlike the typical shell redirect. Such a syntax difference can be puzzling when you first encounter it.

iteratively and interactively writing and testing python code

I'd like to iteratively write and test my python code in the Python console. I can't find a way to easily load what i've got written in the editor (somefile.py) into the Python console in Pycharm. Is there a trick to doing so?
Pycharm will let me run the entire script but that's not useful because i want to build up my state in the shell by experimenting with the functions and data i've got in the environment (kind of like how a lisp programmer would use a REPL).
Pycharm have this alt-shift-E option.
This way you can write in the editor, select the lines you want to run and run them by alt-shift-E.
I use it all the time, although I find a lot of people not fameliar with this option.

How to configure Ipython to automatically revert back to the directory it was opened in after running a script, even if the script errors out

End goal: After running a script regardless of ending perfectly or erroring out, I would like to have my ipython environment revert back to the directory it was in before running the script.
I have successfully used the advice given in the SO post,
how-do-i-change-directory-back-to-my-original-working-directory-with-python.
This works great if the script runs without failing.
Ideas:
1) I have considered wrapping my code in one large try: and except: utilizing the advice in the post mentioned above. As this would surely handle the issue. However, it seems tedious and unnecessary to have to write this into all my scripts.
2) I was thinking a good a solution would be to have ipython automatically run a "revert directory" script after every user ran script, that would change the directory back to the original one. I have looked around a bit to see how one would configure ipython to run a script automatically after every run. From my search so far I have not found this to be an option. I was expecting to find a method to accomplish this through the ipython config file.
3) Another solution that would be easy would be to somehow use the information found by using the line magic command %dhist. As the first result given by this command is the original directory that ipython was opened in. However, other than having the results print from executing the command %dhist... I am not sure how to work with the printed information.
4) I must be not searching the right terms, as I feel like this is probably something that is possible or has been asked. If indeed someone has asked this, please point me in the right direction and I will remove this question promptly.
Relevant information:
Working on os x
Using Jupyter QtConsole 4.3.1
IPython 6.2.1

Batch Rendering file from a python script without openeing Maya

I have one Maya scene and a Python script where import obj files into it. I need to create a batch render file which calls the maya file and applies the script without opneing maya.
I have this code in a .sh file:
#!/bin/bash
"/Applications/Autodesk/maya2016/Maya.app/Contents/bin/Render" -r file -s 1 -e 4 -cam camera1 -rd "/Users/MyComp/Documents/maya/projects/default/images" "/Users/MyComp/Documents/maya/projects/default/Scenes/test1.mb"
But I have this code into the script which can be an issue or maybe not:
def renderFile(i):
cmds.setAttr("defaultRenderGlobals.imageFilePrefix", i, type="string")
cmds.render(batch=True)
If I execute this .sh file it renders without the python script. How can I add the python script?
I need that file for a renderfarm purposes
I know it's an old thread but thought I'd jump in just incase someone finds this thread in a search.
The comments seem a little confused. This comes from the fact that there are two different Python interpreters being talked about. The first is the system level one, which the original question seems to be talking about. In that case, you can use any of the various shell command launchers (like, subprocess/Popen) that suit your need. Here you are looking to run the render command like you would any other command in in the shell.
In the responses, people there are referring to the other interpreter, the custom Maya Python interpreter (mayapy.exe). In that case you are working with actual Maya libraries and it's the same as working with Python in it's shell, with the added Maya libraries/environment.
The two have different uses, the first is to control things like they were in the shell and the second is controlling things inside of a Maya context. Hope that clarifies things.

Link Visual Basic with Python

i've been searching about how to link visual basic with python file
i've tried so hard through using shell in Visual Basic but nothing happend
i have python file called Go.py and i want to link Visual Basic button with it and get the return into variable
any idea ?
First, you can use Shell, although it's unfortunately probably more complicated than you imagined.
Your current problem is a simple one - Shell can't run a python file directly, so you need to have Shell call cmd /c python.exe Go.py, and you may need to provide a full path to python.exe as well.
However, you also want to capture the result, and Shell only returns the process ID, not any kind of process output. You can check out some examples of external process invocation, although they don't explicitly cover how to capture output. If Go.py outputs to the terminal, you can probably capture the output into a file using standard Windows output redirection, and then open the file in VisualBasic and read the values.
You can also use System.Diagnostics.Process() instead of jumping through all the hoops of trying to get more functionality out of Shell. (Specifically, review the ProcessStartInfo class properties related to output redirection which give you much more control than anything using Shell will).

Categories