How to tidy VS terminal? - python

I'm new and I just started coding so I'm allowed to ask dumb questions.
I don't know how to phrase this question but best way I can explain it is my terminal is cluttered and shows where the code is being saved ig? Idk what it's showing but every time I run it a line added before and after the program says
"a.b#ABs-MacBook-Pro CS shit % /usr/bin/python3 "/Users/a.b/Desktop/CS shit/Start.py"
Literally at the start and end every time I run the code, HELPPP
Tried google, not sure how to describe problem

I guess this is normal, and it should be like this. I have the same thing every time I run the code.
PS C:\Users\user1\programs> & C:/Users/user1/AppData/Local/Programs/Python/Python311/python.exe c:/Users/user1/programs/test.py # at the start of the code
Hello World
PS C:\Users\user1\programs> # at the end of the code
I'm not sure if you can hide this, but you can try searching for some extensions or smth.

Related

Is it possible to make pycharm's run display section display the same as actually running the program would?

Disclaimer: Sorry if the question is stupid or repeated, I've tried to find similar ones that answer what I need to know but I couldn't. I've started to learn programming about 1 month ago and this is my second time on this website. Feel free to point out any errors or better ways to formulate my questions on stackoverflow, I'll be grateful.
Context:
I was trying to find out a way to print a string - in this case ' º ' - after my user's input - an angle -, on the same line.
I need an alternative way to do this, or help with the one I'm using.
What I got from my research is that, apparently, using the command os.system(cls) should erase the previous line, and putting \033[A before the string should move the cursor up one line. So using these two together should erase the previous line and then overwrite it.
Here's my try:
from os import system
cls = lambda: system('cls')
angle = float(input(f'Insert an angle:'))
cls()
print(f'\033[AInsert an angle:{angle}º')
Desired result on run:
Insert an angle: *60*º # being 60 the user's input
Actual result on pycharm:
Insert an angle:60
Insert an angle:60.0º # for some reason, you can't see it when paste it here, but there's a symbol of a crossed rectangle on the beginning of this line on Pycharm's run
How it looks on pycharm's run terminal
As you can see, the line isn't getting overwritten, only repeated.
What is weird is that when I run this program with Python 3.8 instead of Pycharm, it works as intended, but, on Pycharm, the line isn't overwritten. Instead, Pycharm just prints a crossed rectangle symbol.
Why does it work when executing the file with Python 3.8, but not when pressing "run" on pycharm?
Is there a way to avoid it?
Are there better alternatives to printing a string on the same line as an input?
In cases where I need special printing (ANSII escape codes, backspacing...), I use the actual Terminal, not the Python Console.
For whatever reason, interactive consoles, regardless of IDE, seem to have issues with handing specialties like that. With the normal Terminal, it works as expected:
I have never found a way of having the interactive console handle cases like this.

Call a function in a running program (and a code review)

Call a function in a running program
I am fairly new to programming and recently decided that I want to expand my Python knowledge and practice a bit. For that reason I decided that I create a little Weather Station with a Raspberry PI.
The program I am currently creating takes the output from a thermometer, parses it and writes it into a database. At the time the program is started every minute and after completing the aforementioned procedure, the program ends and all the instances get deleted (is that how you say it?).
I think that restarting it every minute wastes resources and time is getting lost so I want my program to run all the time and be accessible via command line commands (bash).
For example I have the class Thermometer:
class Thermometer():
def measure():
# do stuff
# return stuff
An instance of this class is created like this:
if __name__ == "__main__":
thermo = Thermometer
while True:
pass
Is it possible that I could call the function measure like this?:
sudo python3 < thermo.measure()
Or is there an other way to achieve this or am I doing a completely wrong approach? Also how could one describe this problem? I tried searching for "Python call function from outside" or "Python call function from bash" but I didn't find anything useful except Call a function from a running process
StackOverflow but it seems that this is the wrong Python version.
You can find my code here: github Jocomol/weatherstation
or if you don't trust the link go to github and search for "Jocomol/weatherstation".
Code review
As I already mentioned I am quite new to python and programming itself, so I make a lot of mistakes or writing useless code that could be resolved otherwise. So I am thankful for any feedback I can get on my code. If you guys could take a look at my code and point out places that aren't optimal or where I did something completely useless, I would be very happy.

Killing a program with SIGINT is rendering my shell unusable, why?

I'm working on bash in Ubuntu and I have a python program using some threading code. It works well, but after I kill it pressing CTRL+C, the shell just breaks. No input is shown to me (although it still is interpreted in the background) and the only thing that works is the enter key, albeit in a weird fashion.
thunder#machine:~/server/api$ thunder#machine:~/server/api$
Well actually it doesn't work, it just places a new prompt line right next to the previous one.
I think something is messing up with my shell, so I was wondering is there a way to "reset" it? right now I have to open a new shell session and that is very annoying.
To know what is causing this problem to begin with would be great, but I don't think that's possible with the little information I just provided here. And frankly I have no idea of where in my code I could be messing things up, since like I said before the program does work correctly.
Nelson
You need to reset the terminal by typing
resetEnter
into the bash shell (you might need to do it blind, i.e. without seeing the characters being echoed).
See man 1 reset.

python, while loop, at the start of each hour call method

OK, I have a IRC bot that has been my on going project while I pickup python, and thanks to everyone here who has helped when I have hit a wall.
ok so I have a while loop, I want a way to find if a new hour has started and if so, to run a method to update any settings, and to make sure that it is still connected to channels. I just have no idea how to go about, checking the time, and then to call a method.
there are many other things happening if this while loop, so doing a sleep for an hour isn't the best way to do this.
As I know questions with out code get marked down, here is some code.
while 1:
if(newhour() == 1):
run_Method()
To answer your question, you could do something like:
current_hour = datetime.now().hour
while 1:
if(datetime.now().hour != current_hour):
run_Method()
current_hour = datetime.now().hour
WARNING: This is a very simplistic solution.
Don't use an infinite while loop, you'll be wasting a processor core just to run a silly while loop. At least put a sleep() inside the while loop if you do use it anyway for a casual test code.
Cron service is already running in the system for this task. Place your script there (or create a script that calls your main script.)
$ cd /etc/cron.hourly/
Let's say proj_x.sh calls your main script. Create proj_x.sh:
$ vi proj_x.sh
Place the following code into proj_x.sh
#!/usr/bin/env python
/home/user/path_to_proj_x/cron_hourly.py
Make it executable:
$ chmod +744 proj_x.sh
Now, /home/user/path_to_proj_x/cron_hourly.py is the main script that carries your run_method()

Package only works properly in IDLE or Interpreter, but not independantly?

I have been trying to add audio cues to a program I am working on using Py-Audiere. However, the sounds only play when I use the run command within IDlE (or manually put it into the interpreter), and then only if the calls are done in certain ways. This is the code snippet:
import audiere
filename = 'Bell'
d = audiere.open_device()
filename += '.mp3'
bell = d.open_file(filename)
bell.repeating = True
def play_tone():
bell.play()
play_tone()
If I run it outside of IDLE, nothing happens. If I run it in IDLE, but move all of it into the function and pass the filename, nothing happens.
I am very confused as to what is going on with this library, and at this point I am not sure I should even be using this one. Does anyone know what is going on or have an alternative (I considered GStreamer, but I can't figure out their documentation)?
I've experimented a bit with audiere (never heard of this library before) and it looks like it plays the sound in a new thread in the background.
In your code snippet you tell audiere to start playing your bell, but you never give it enough time to completely play it. When you run this in Idle or the Python interpreter this doesn't matter as the Python process is still running and so the sound just plays in the background.
Their website has a small snippet that shows how you can accomplish this:
import time
def play_tone():
bell.play()
while bell.playing:
time.sleep(0.1)
As for gstreamer, the documentation can be a bit complex if you never used it before. The best way I found out to learn how to use it is just to experiment with it. Some good points to start is this tutorial that explains the basics of how gstreamer works. This code snippet shows how to play a single sound file with it. And this irc conversation is helpful too, if you feel like reading over it.

Categories

Resources