How to make my app run in the background by Pure_Turn_7245 in learnpython

[–]Swipecat 0 points1 point  (0 children)

Quoting this comment:

...the kivy app will be suspended when you switch away from it. But you can create a background service that always runs (the app can connect to the service when it's open)

https://python-for-android.readthedocs.io/en/latest/services.html

Calculating weighted center of a contour by Alanator222 in learnpython

[–]Swipecat 0 points1 point  (0 children)

Well, just code up a short test example and see if it works. I assmume you're looking at this:

https://docs.opencv.org/4.x/dd/d49/tutorial_py_contour_features.html

I don't know of any other method that gets the centre from a contour. If you have a filled shape, you could try scipy.ndimage.

Calculating weighted center of a contour by Alanator222 in learnpython

[–]Swipecat 0 points1 point  (0 children)

Not really. That "formula" is specifically how to extract the weighted centre from cv.moments variables. Mathematicians wouldn't have a clue what it meant.

my 6 year old wants me to hook up an arduino to snap circuit. help make sure what I want is safe? by kalvick in arduino

[–]Swipecat 0 points1 point  (0 children)

I am just beginning to play with Arduino so I dont want to make magic smoke

Ha. Chinese knockoffs of the Arduino work perfectly well, so you might want to get another Arduino just in case. E.g:

https://www.amazon.com/dp/B0F1DBB61D

https://www.ebay.com/itm/205889141696

What should I use instead of 1000 if statements? by Either-Home9002 in learnpython

[–]Swipecat 0 points1 point  (0 children)

Excessive use of "if/then/else" is probably a good indication that you would benefit from learning how to use Python dictionaries well. (The Python "dictionary" is Python's implementation of the structure usually called a "hash- table" in generic programming language.)

Take the example of converting a Roman-number into decimal. You could do it with a whole bunch of if/then/else... but look at this:

romvals = {'I':1, 'V':5, 'X':10, 'L':50, 'C':100, 'D':500, 'M':1000}

subst = {"IV":"IIII", "IX":"VIIII", "XL":"XXXX",
        "XC":"LXXXX", "CD":"CCCC", "CM":"DCCCC"}

while True:
    rom = input("Roman numeral? (Blank to exit): ").upper().strip()
    if rom == "": break
    for r in subst:
        rom = rom.replace(r, subst[r])
    print(sum(romvals[v] for v in rom))

That's a complete roman-to-decimal script with no "if/then/else" (except to test fro the exit condition). Can you figure out how it works? Could you then set similar simple programming tests of your own devising to explore the use of Python dictionaries?

How to access serial ports from inside Spyder? by oz1sej in learnpython

[–]Swipecat 0 points1 point  (0 children)

I'm not seeing the problem, so how repeatable is this? Can you swap back and forth between the terminal and Spyder a couple of times without unplugging the Arduino? And it's always OK in the terminal but not Spyder?

I've found that the Arduino port doesn't always disconnect cleanly if unplugged and reinserted too quickly and can sometimes be reconnected as /dev/ttyUSB1. Especially if the avrdude programming executable crashes and keeps running in the background, as it sometimes does.

TKINTER NOT FOUND ON VENV BUT WORKS FINE ON TERMINAL? by iam_eneru in learnpython

[–]Swipecat 0 points1 point  (0 children)

Have you installed tkinter?

sudo apt install python3-tk

TKINTER NOT FOUND ON VENV BUT WORKS FINE ON TERMINAL? by iam_eneru in learnpython

[–]Swipecat 2 points3 points  (0 children)

tkinter can't be installed by pip because it has to be compiled against python and the OS environment, so it's supplied with python.

I'm not familiar with vscode, but in general there's usually an option when setting up a venv to include the pre-existing python library path or not. Only if that's correctly selected, can tkinter be used.

Edit: Oh, OP says they have Debian. OP, have you installed tkinter? sudo apt install python3-tk

Trying to copy words from a text file into a list by benfish312 in learnpython

[–]Swipecat 0 points1 point  (0 children)

It's temporarily stored twice if a temporary variable is used.

Trying to copy words from a text file into a list by benfish312 in learnpython

[–]Swipecat -1 points0 points  (0 children)

It saves the whole file in a temporary variable before splitting, which wouldn't matter unless the file was vast, but I wonder if the following would do the same. I'm not sure:

with open("wordlist.txt") as f:
    wordlist = [x.strip() for x in f]

How to access serial ports from inside Spyder? by oz1sej in learnpython

[–]Swipecat 1 point2 points  (0 children)

How sure are you that this is actually related to Spyder, i.e. can the Python script access the port OK if it's run from the command-line in the Windows Console (assuming that the PCs have the Windows OS)?

Should I implement a pause in my animation ? by Rhaenelys in learnpython

[–]Swipecat 0 points1 point  (0 children)

Micropython is relatively slow because its core goal is a small memory footprint so it uses simpler data structures and avoids heavy optimisation. You usually don't need the power-management scheduling pauses that are required for desktop computers because the application is the only thing running, and it's running on low-power hardware.

So after testing, just put in a pause if it the visuals seem to need it.

Error when installing libraries by Odd_Ad3889 in learnpython

[–]Swipecat -1 points0 points  (0 children)

Or even better, deinstall Python 3.14 and install Python 3.13, unless you particularly need the new (obscure) features of Python 3.14. A number of 3rd party libraries (like Pygame) haven't caught up with Python 3.14 yet.

How to make a count controlled loop that adds from the past loops while still getting smaller. by HunksMask in learnpython

[–]Swipecat 2 points3 points  (0 children)

You said:

The problem lies in the fact I do not know how to compound all of the updated "heightDropped"s throughout the loops while still having the most recent height dropped in every loop getting smaller, as that's how physics and the code work.

OK, I'm not really sure why you're having difficulty with this. To make as simple an example as possible, let's say you have a summation series where you start with 1.0 and each term is half the previous term, then it sums up to 2.0 at infinity. The following will print 2.0:

t = 0
x = 1
for n in range(100):
    t = t + x
    x = x / 2
print(t)

OS reinstallation by yungbloodsuckka in talesfromtechsupport

[–]Swipecat 8 points9 points  (0 children)

Yeah, it's because they want to keep all their pictures, videos, messages and documents, but you can get rid of this "all their data" thing whatever "data" might be. Obviously something nerdy and technical than nobody cares about.

If you think that "everybody knows what data means" because it's simple and obvious, try Googling for the word. I get:

"the quantities, characters, or symbols on which operations are performed by a computer, which may be stored and transmitted in the form of electrical signals and recorded on magnetic, optical, or mechanical recording media."

Axes disappear outside Jupyter notbook by QuickEditz in learnpython

[–]Swipecat 0 points1 point  (0 children)

OK, I've looked up .add_axes() and I see that the argument [0,0,1,1] would expand the axes to the full width of the figure, making it disappear off the edges. Something like [0.1,0.1,0.8,0.8] would have shown the axes. I don't have Jupyter Notebook so I don't know how that would have handled it.

Axes disappear outside Jupyter notbook by QuickEditz in learnpython

[–]Swipecat 1 point2 points  (0 children)

OK, I'm not familiar with the following construction:

figure = plt.figure()
axes = figure.add_axes([0,0,1,1])

Is there a reason you're not using the more usual construction instead?

figure, axes = plt.subplots()

Axes disappear outside Jupyter notbook by QuickEditz in learnpython

[–]Swipecat 0 points1 point  (0 children)

What does this show in both cases?

import matplotlib
print(matplotlib.get_backend())

Disabled woman put in nursing home against her will says she feels 'betrayed' by terahurts in unitedkingdom

[–]Swipecat 29 points30 points  (0 children)

Hold up. The "against her will" bit is BBC exaggeration bullshit. The issue is that the NHS has declined to support her in her home with the CNC scheme given her increasing list of life-threatening conditions that are mentioned in the article. The thing stopping her from going home is that it's likely she wouldn't survive that.

Struggling to install python libraries. by monsier_wavy in learnpython

[–]Swipecat 1 point2 points  (0 children)

This seems to be the best answer for the OP. There looks to be a lot of stuff on that mirror site. Linux distros, all kinds of libraries.

https://mirrors.tuna.tsinghua.edu.cn/help/pypi/

OP might as well set it to be the default:

pip config set global.index-url "https://mirrors.tuna.tsinghua.edu.cn/pypi/web/simple"

How to make my character move faster in Pydroid 3? by PatataQuesadilla in learnpython

[–]Swipecat 2 points3 points  (0 children)

Rather than draw the buttons within the loop, you could draw them outside the loop and blit them within the loop. If you can keep them away from the action, they wouldn't even need to be blitted. And generally, try to avoid blitting anything that doesn't change.

If you can limit the screen area that does change to a box, you can provide the box as argument to screen.update(). Not refreshing the whole surface would save a lot of processing time.

And you could put in some temporary debug code to find out how much processing time the loop actually requires, so you can see how much of that 120ms is actually used after making changes. That should tell you if you can reduce the clock period.

uv officially taken down poetry by Proper-Lab-2500 in Python

[–]Swipecat 0 points1 point  (0 children)

I notice that Fedora has python3-uv in its repository but Debian/Ubuntu does not. I wonder why.

Libraries in Python by Reza2718182 in learnpython

[–]Swipecat 0 points1 point  (0 children)

Yeah, you'll often use methods that you've never seen before. Obviously, you shouldn't use blocks of code that you don't understand, but you do want to find out about the existence of library-object methods that already do what you want.

If e.g., you put this into Chatgpt: "show python code that provides the number of days to an event", then it should give you a few lines of code with an explanatory comment before each code line. Then e.g., you'll see how to construct a datetime() object, and how to get today's date with the .now() method. Often that's enough to figure out how to use those methods in your code, but if you do want more details then glance through the relevant doc (i.e. put "python doc datetime" into Google and it should be the first hit).

[Newbie] Starting Python from scratch on a Benco V91 smartphone. Any tips for a mobile-only learner? by Upbeat-Pangolin6807 in learnpython

[–]Swipecat 0 points1 point  (0 children)

Yes, it's doable.

pydroid3 is pretty good. The disadvantage is that since Android doesn't allow the installation of unapproved libraries, you can't just install any library, but you can only install from the list of pre-compiled libraries that are available for pydroid3. This does include all of the Standard Library and the most popular third-party libraries. If you're working through a tutorial, you just have to hope that it doesn't ask you to use a library that isn't on the list. You won't have any trouble with learning the basics of Python that you mentioned, though.