This question already has answers here:
Clear terminal in Python [duplicate]
(27 answers)
Closed 3 years ago.
So obviously you can clear the console with os.system('clear') but this seems like a very bodged solution to me.
Is there a more elegant way to clear the console?
I feel that this question is different from Clear terminal in Python because I am not asking simply how to clear the terminal, I am asking which is the most pythonic. Python - Clearing the terminal screen more elegantly is not what I am looking for either, as the marked answer there still does not feel very elegant or pythonic. Using escape characters or calling the command and checking the output seems even more like a bodge than an actual solution.
import curses
stdscr = curses.initscr()
stdscr.erase()
Rather than assuming specific character control codes, the curses package was developed for portable full screen character control in the heyday of many different video terminals, with many different and incompatible control sequences. It also turns the use of such codes from cryptic raw escape sequences to more readily understandable named functions, such as "erase." Look at the curses package documentation to see all of the capabilities. Here, we initialize curses to the (default) full terminal window, then erase the contents of that window.
As for this being Pythonic, it's hard to see something more Pythonic than using a standard Python package, initialize, and one line of code...
Related
This question already has an answer here:
Task output encoding in VSCode
(1 answer)
Closed 2 years ago.
I'm having a problem with characters not rendering properly.
Background: I am taking online courses to learn Python. I use VSCode as my IDE along with various python extensions.
Problem: Some of the lessons I solve have characters that are beyond the 128 standard ASCII set.
Sample: For clarity, this is the full script I'm dealing with. The currently lesson has text containing a small e with acute (é) in a painting named Vétheuil in the Fog. Unfortunately that acute-e character is rendered as a placeholder (�) and ends up outputting: V�theuil in the Fog.
Efforts: I have done some searching and thought I found a solution: including an encoding flag at the beginning of the python script like this:
# coding=UTF-8
No joy.
Am I tilting at a windmill / misunderstanding purpose or application?
Money Question: Is there a way to get the character to properly render when I run the script?
If you want Unicode characters to be displayed correctly, you'll need to use a terminal, that supports Unicode or change the terminal settings to work with Unicode, in case the terminal provides that option.
Of course the same way you can make the terminal able to display only extended ascii characters like "é" (as long as you have a terminal that provides this functionality), in case you don't need more characters.
This has been sufficiently addressed.
VSCode Panel displays different things via tabs below the editor region. The OUTPUT tab has the displayed different and therefore becomes a question of my setup; not error as the TERMINAL tab displays characters as expected. So, there is no problem to be solved beyond my misunderstand of what was happening.
As #user2357112supportsMonica also provided "An encoding comment declares what encoding your script itself is written in," not how the output is rendered.
Thank you all for helping me through this.
Just to cap this topic. A PERFECT solution to this is provided here:
https://discuss.codecademy.com/t/general-nerdiness-questions/541682/7?u=coreblaster01537
This question already has answers here:
How do I protect Python code from being read by users?
(29 answers)
Closed 3 years ago.
My project is open sourced, except for a single module where I don't want people to know the implementation. Actually, I don't mind one or two people cracking the thing open if they are determined enough, but I want most to give up at the first sight.
I only want the implementation of that single module to be hidden, the interface still fully usable if people want to contribute to the project. That is to say, I want people to be able to do things like:
import my_hidden_module
my_hidden_module.do_stuff()
My project mostly runs on Windows so Windows-exclusive suggestions are ok.
I'm totally new to this hiding code thing so I don't know where to start. It would be appreciated if someone could give me a direction to look into.
1) You can use classes and private variables or
2) use name =='main', but that will not implement the code on interface
3) looks dumb but you can comment on top of that function to not to be CHANGED
else you can see - How do I protect Python code?
This question already has answers here:
Convert Python program to C/C++ code? [closed]
(8 answers)
Closed 3 years ago.
I have been trying to find a way to convert .py source file to .cpp source (as a time saver from doing it manually). I've never used python before and was hoping for a quick way to convert it, and cleanup any code the converter might not do well.
So far, some of the options that I have found while googling seem to be: nuitka, cython, and pypy/rpython.
However, the documentation I have read only seem to produce executables, and not actual source code.
At this point, I have found py2c, but cannot seem to find any documentation on how to use it. Also, judging by the posted roadmap on the wiki, it does not seem to be a finished product, and so I'm doubtful as to its reliability.
If you can provide other sources on how this can be accomplished, or shed some light on something I may have missed on the above-mentioned possibilities, it would be appreciated. Otherwise, I will simply convert it manually.
Programming languages cannot be easily converted like this. For example, Python has a large standard library, C++ doesn't have the same library. How do you translate those calls?
More fundamentally, the semantics of the language are very different, even a statement as simple as x = 1 means something different in Python and C++.
You are going to have to write C++ while reading the Python.
Have a look at shedskin, if it won't do the whole job,it still might be helpfull.
I'm trying to write some python, and I'm used to the lispy way of doing things, a REPL in EMACS and the ability to send arbitrary code snippets to the REPL. I like this way of developing code, and python's built-in IDLE seems to do it pretty well. However I do like EMACS as an editor.
What's the best thing analogous to SLIME for Python?
So far:
It seems that the trick is to open a python file, and then to use 'Start Interpreter' from the Python menu, after which you get an Inferior Python buffer. You can then use C-c C-c to load the whole buffer you're editing into the 'REPL', and use normal copy and paste to put snippets into the REPL.
This works as far as it goes. Is there any way to say 'reevaluate the big thing that the cursor is in now and display the answer', or 'reevaluate the thing the cursor is just at the end of and display the answer', like M-C-x and C-x-e in SLIME?
And it all seems to work better if you use the python-mode.el from Bozhidar's answer
There is ipython.el which you can use for some extended functionality over the vanilla python-mode. Ropemacs provides a few extra completion and refactoring related options that might help you. This is discussed here. However, I don't expect you're going to get anything close to SLIME.
I think the new python.el is a much better idea. It's under active development, it can spawn a python shell and send function definitions, buffers and files to it. It also has better than average re-indent support. It's rumoured that in Emacs 24 it might become the default python mode in Emacs.
python-mode is the default mode for editing Python in emacs. It includes commands for evaluating the buffer and running an inferior interpreter instance.
I often use ipython (or the regular python shell) to test python code snippets while coding, and it's been very useful. One shortcoming of this, though, is that if I want to test a multi-line segment of code, or want to write multiple lines of code before running, it isn't very convenient to have to do it "line by line". And even going back to change some of the lines is cumbersome because you have to re-type all the code that comes after it.
I'm playing with Groovy right now and I find that it has an excellent solution to this problem: the Groovy Console. You just write all the code you want, it's just like a regular editor; and then you hit run Ctrl+R (Cmd+R actually since I'm on a Mac) and it runs everything at once. If you want to change something (e.g. if there are errors), then that's easy too -- just change it and Ctrl+R again.
Is there an equivalent of this available for python? Or do you have any recommendations on a way to achieve similar behavior? I could just create a new file, save it, and then python <filename>.py from the shell. But that's just too many steps and would be cumbersome. Eclipse may be an option, but it's too heavyweight. I'm really looking for something lightweight that I can just spin up when I want to test something and then get rid of it just as quickly.
I'd be interested to hear any ideas/suggestions!
Thanks
You might give DreamPie a try. As far as I can tell from a quick read of the groovyConsole page you linked to, DreamPie features a similar input area/output area division (they call it "code box" and "history box"). The code you execute is by default cleared from the code box - which groovyConsole apparently doesn't do - but you can easily retrieve it (Ctrl+Up), or change a preference setting to "Leave code in the code box after execution".
Have you tried using IDLE, the standard Python IDE? You'd have to save the code as <filename>.py within IDLE, but after that you can run it using F5.
The Python docs link to this intro to IDLE, which may be helpful even if it's a little outdated.
I am using emacs and its python-mode.
C-c C-c: evals the current buffer
but you can also eval region (ie selection), functions etc ...
You can even make python-mode use ipython (like I do).
See http://ipython.scipy.org/dist/ipython.el . It works nicely
Did you try PyCrust? It has excellent multi-line editing, copy/paste support.
PyCrust can be found in wxPython Docs and Demos.