how to save python session input and output [duplicate] - python

This question already has answers here:
How to save a Python session, including input and output, as a text?
(4 answers)
Closed 1 year ago.
All of the ways which discussed this question save the history of your commands in a file or you have to use an IDE ,I am using vim with python-mode (no mouse using) what I would like to do is to save my session as code I wrote and the python output ,So I dont have to use paper and pen to write my input and python output all what I have to do is to print out my session , I tried the code (.pystartup)
and it only save my input and I tried to redirect the output to a file and it only save the python output , is there a way to have both in one file ready to be printed out .

I really cherish vim, it'S a fantastic piece of work. Nevertheless, your requirements are easier with other tools.
Probably the best choice, in my oppinion, would be to use the ipython notebook. It offers really rich features, including graphics with mpl and much more, and for me is the perfect tool for "reproducible experiments". The full state of a notebook can be saved to disk, reloaded, exported, printed etc.
You should really give it a try.

The old "I use $EDITOR instead of $IDE because I don't want to use the mouse" bullshit… IDEs have shortcuts for everything and they all allow you to customize them to your liking. Learn them all and forget about your mouse.
Anyway, what you want is neither an editor nor an IDE; you want a REPL like bpython or ipython (possibly with its notebook feature mentioned above). Both tools allow you to save and restore your sessions and are far better at "getting" your python code than Vim.

Related

How best to enable non-programmers to run a Python program

I have written a Python script which models an academic problem which I wish to publish. I will put the source on Github and some academics that just happen to know Python may get my source and play with it themselves. However there are probably more academics that may be interested in the model but that are not python programmers and I would like them to be able to run my model too. Even though they are not programmers they could at least try out editing the values of some of the parameters to see how that affects the results. So now my question is how could I arrange for a non-python programmer to run a Python program as easily (for them) as possible. I would guess that my options may be...
google colab
an online python compiler like this one
compiling the program into an exe (and letting the user set parameters via a config file)
something else?
So now a couple of complications that makes my problem trickier.
The output of the program is graphical and uses matplotlib. As I understand it, the utilities that turn python scripts into exe files struggle or fail altogether when it comes to matplotlib.
The source is split into two separate files, one small neat file which contains the model and the user might like to have a good look at it and get the gist of it even if they're not really a python programmer. And a separate large ugly file which just handles the graphics - an academic would have no interest in this and I'd like to spare them the gory details.
EDIT: I did ask a related question here - but that was all about programmers that won't mind doing things like installing python and using pip... this question is in relation to non-programmers who would not be comfortable doing things like that.
Colab can handle the 2 problems, but you may need to adapt some code.
Matplotlib interface: Colab can display plots just fine. But you may want user to interact with slider, checkbox, dropdown menu. Then, you need to use Colab's own Form UI, or pywidgets. See an example here
2 separate python files: you can convert one of them to a notebook. Then import the other. Or you can create a new notebook that import both files. Here's an example.

python string escape sequence simulation is not working [duplicate]

This question already has an answer here:
python IDLE shell appears not to handle some escapes correctly
(1 answer)
Closed 3 years ago.
There are several folks on here looking for backspace answers in Python. None of the questions I have searched have answered this for me, so here goes:
The Simple Goal: be able to print out a status string on one line, where the next status overwrites the first. Similar to a % complete status, where instead of scrolling a long line of 1%\n, 2%, ... etc. we just overwrite the first line with the newest value.
Now the question. When I type this in idle: print("a\bc") I get this as output: ac with what looks like an odd box with a circle between the 'a' and 'c'. The same thing happens when using sys.stdout.write().
Is this an Idle editor setting/issue? Does anyone even know if what I am trying is possible in the Idle Shell?
Thanks for any insight.
PS: Running Python 3.3.2 Idle on Windows 7, 64-bit system.
EDIT: Copying the output in Notepad++ is revealing that Python is printing out a 'backspace' character, and not actually going back a space. Perhaps what I am trying to accomplish is not possible?
Edit:
Apparently the carriage return \r and the backspace \b won't actually work within Idle because it uses a text control that doesn't render return/backspace properly.
You might be able to write some sort of patch for Idle, but it might be more trouble than it's worth (unless you really like Idle)
This doesn't answer your question in a literal fashion, but I think it might be useful to point out that generally interfaces like the one where you are describing (e.g., where one part of the screen is continuously updated, without newlines), it just generally implemented using a library like ncurses.
Python has a curses library built-in (http://docs.python.org/3.3/library/curses.html), which can more or less achieve your end goal.

Displaying reports for files using Python

Question about general possibilities using Python here, I don't really know enough about programming to know whether it's something that's doable, and if so, how do I go about it.
I have a program which is a simple desktop program, which you load files into. The program can then output various properties of the thing that's in the file, and depending on what you ask it to do will output a report. It outputs the report in text format, but not as a file, and instead actually, just in the program itself displays the report. Like this:
My question is that if I want to get this text output for a large number of files, I'm currently manually loading the files individually into the program making the report, copying this to a text file, and saving the text file.
Basically I want to know whether it's extremely difficult to get Python to do this for me, or not. If it is doable, are the resources available for me to read about how it might be done? Are there conditions about being able to run my program and various commands from the Python command box?
Hope my question's clear enough. Sorry if it's a bit garbled.
The tricky part here is
The program can then output various properties of the thing that's in the file, and depending on what you ask it to do will output a report.
Basically, if the desktop application you use has a command line interface, it is possible and relatively easy.
If this program has command line option to open a document and output a report in any format (print the report on the standard output, write it into a file on the disk, etc.), you can call that commands from a script python for each files you set in a list.
If your software doesn't have a CLI (Command Line Interface), it might be possible but more diffficult. In that case, you have to automate actions by using a library that will emulate clicks on the Window of you software (1. Click on Open 2. Click, click, click to select the file to load 3. Click on the button to generate a report etc.) It's a pain, but it can be considered.
You will find plenty of resources to learn by yourself how to code a python script. You will probably need to learn about lists, loops, files manipulations and maybe the subprocess library which will let you call any command from your python script.
I suggest you to start with Python3 instead of Python2 because it has a better support for unicode that could quickly become an issue if you have non ascii characters in your input files or in reports from your software.
Good luck ;)
If the only way you can get report is selecting and copy/pasting it from program GUI, the situation just begs for AutoIt instead of Python.
With Python it would be much more difficult. Unless you want to improve your python knowledge or course...
Simulating keypresses, you can open specific file in program (through sending ctrl+o or alt and navigating file menu). Simulating mouse or keypress - start report generation. Then simulate a mouse click in text area, and perform something like:
(just a skeleton of script, probably need to be modified to suit your situation and needs)
send("^{a}^{c}") ; to select all and copy (if these keys are supported in this program
$text = ClipGet() ; get contents of clipboard
$fout = FileOpen("somefile.txt",2)
FileWrite($fout,$text)
FileClose($fout)
To fully automate the task, in script you can get a list of source files in specific folder, and run this macro for each of them, automatically naming resulting txt files.

How do I embed an Ipython Notebook in an iframe (new)

I have successfully achieved this using the method documented at Run IPython Notebook in Iframe from another Domain . However, this required editing the user config file. I was really hoping to be able to set this up via the command-line instead (for reasons).
http://ipython.org/ipython-doc/1/config/overview.html indicates that configuration via the command line is possible. However, all the examples are for simple true/false value assignment. To set the server up to allow embedding, it is necessary to set a value inside a dictionary. I can't work out how to pass a dictionary in through the command-line.
Another acceptable option would be a configuration overrides file.
Some people will wonder -- why all this trouble!?!
First of all, this isn't for production. I'm trying to support non-developers by writing a web-based application which integrates Ipython notebooks within it using iframes. Despite being on the same machine, it appears that the different port number used is enough to mean that I can't do simple iframe embedding without setting the x-frame insecurity bit.
Being able to do this via the command line lets me set the behaviour in the launch script rather than having to bundle a special configuration file inside my app, and also write an installer.
I really hope I've make the question clear enough! Thanks for any and all suggestions and help!
Looking over the IPython source for the loaders, it seems like it will execute whatever python code you put on the right hand side. I've not tested it, but based on the link you provided, you can probably pass something like
--NotebookApp.webapp_settings=dict('headers'=dict('X-Frame-Options'='ALLOW-FROM https://example.com/'))

Can I get Python debugger pdb to output with Color?

I'm using PDB a lot and it seems it would be even better if I could add systax highlighting in color.
Ideally, I'd like to have to the path to the code a lighter color.
The line of actual code would be syntax highlighted.
I'm using OS X and the Terminal app.
Python 2.7
pdb doesn't support colorization. However, it's not that hard to get it, even if you're a command-line addict (as I am;-) -- you don't have to switch to GUIs/IDEs just to get colorization while debugging Python. In particular, command-line tools usually work much better when you're accessing a remote machine via SSH, saving a lot of the bandwidth and latency issues that any remote access to GUIs and IDEs can inflict on you;-).
Specifically, for the task you're asking about, consider ipdb (you also need ipython, which offers a far more advanced shell than plain interactive Python, on which ipdb relies). Both offer you good tab completion, enhanced tracebacks, and colorization -- ipython for your normal interactive work, ipdb with the same features when you're debugging (otherwise just about the same as pdb).
You could try pudb, which works in the terminal and looks like this:
I haven't tried some of the options mentioned in other answers, but to judge from the PyPI pages, pudb is better maintained and better documented.
Take a look at pdb++ - it is a drop-in replacement for pdb that fills all your requirements and adds some other nice features such as tab completion and new commands such as watch and sticky.
Here is a sample config file that will enable colours (add this after creating the file: touch ~/.pdbrc.py):
import pdb
class Config(pdb.DefaultConfig):
use_pygments = True
pygments_formatter_class = "pygments.formatters.TerminalTrueColorFormatter"
pygments_formatter_kwargs = {"style": "monokai"}
This might not possible for you, but have you tried using a graphical debugger (like the one in eclipse/pydev)? It will give you your syntax highlighting and much more.
I only use pdb directly if I don't have an option, because a graphical interface is just that much nicer.
In case someone hit the problem with colorization in a console.
My console had white background while ipdb was also adding rather light colors to syntax (for example variables were white). Pressing man ipython shows that we have 3 colors available: 'nocolor', 'linux', 'LightBG'. Ipdb was in my case installed via easy_install into my virtualenv. So it was trivial to look into ipdb source and modify it (hint search for ipdb/init.py in your env). Then I've modified following:
def set_trace():
ip = ipapi.get()
+ def_colors = ip.options.colors
+ def_colors = 'LightBG'
Pdb(def_colors).set_trace(sys._getframe().f_back)
It's a kinda hackish solution but well its for debugging purpose on my working station so its sufficient. But if anyone finds something better. Please send me a message on what to do.

Categories

Resources