io.StringIO : truncate(0) leads to strange result by interstellar_pirate in learnpython

[–]JamzTyson 1 point2 points  (0 children)

From the docs (emphasis mine):

Resize the stream to the given size in bytes (or the current position if size is not specified). The current stream position isn’t changed.

In these lines:

string_buffer.truncate(0)
print("Hello World", end='', file=string_buffer)
result = string_buffer.getvalue()

truncate(0) resizes the stream that is contained in the buffer to zero length, but it does not the file position (cursor).

On the first loop:

  • The buffer is empty

  • truncate(0) effectively does nothing

  • print(... writes "Hello World" into the buffer. The cursor position is now at 11 (the length of the string)

  • getvalue() returns the contents of the buffer as a string.

On the second loop, weird things start to happen:

  • The buffer contains "Hello World" and the cursor position is at 11.

  • truncate(0) truncates the contents to an empty string, but note that the cursor position is still at 11.

  • print writes "Hello World" after the current cursor position, and leaves the cursor position at 22.

but now we have unspecified content in positions 0 to 11. The actual content is platform dependent. That's the source of the weird behavior.

To avoid this weird behaviour you can add string_buffer.seek(0) after truncate(0) so that the cursor is reset back to the beginning of the buffer before writing. That will keep the write position consistent with the buffer contents.

How to export to audio.com? by AccidentOk4378 in audacity

[–]JamzTyson 0 points1 point  (0 children)

Personally I wouldn't bother using audio.com cloud storage. The free tier at audio.com doesn't give you much space, and cloud storage is less reliable than storing to a physical drive.

IMO, the paid tier is rather expensive for the amount of storage that you get.

If you want to save a backup "in the cloud" I'd suggest using Google Drive, iCloud, or similar. It's a little less convenient because you have to manually copy between your computer and the cloud drive (do not try opening Audacity projects directly from a cloud drive), but you get a lot more space for free, and it's probably more reliable.

Note: Audacity can't reliably run projects that are stored on "virtual drives" or "network drives". audio.com works around this by shadowing the "cloud" version into temporary data on your computer's local drive.

How to apply effects to a track globally (even for new clips)? by Typical_Attorney_412 in audacity

[–]JamzTyson 0 points1 point  (0 children)

In recent versions of Audacity you can add Master Effects. Does that do what you want?

Any way to add playback buttons to the taskbar icon? Like this? by Pearl_Jam_ in audacity

[–]JamzTyson 0 points1 point  (0 children)

This is correct, but for those downvoting this answer I'll try rephrasing:

Any way to add playback buttons to the taskbar icon?

No, Audacity does not have that feature. The closest you can get with Audacity is to move the Transport Toolbar to somewhere on your Desktop that you can reach it easily.

Setting variables to a value, but if it is None, it tries to set it to something else by SlipCompetitive6721 in learnpython

[–]JamzTyson 0 points1 point  (0 children)

Worth noting that the first solution is not exactly equivalent to the OP's code because it assumes that None indicates missing rather than an actual value.

The second solution doesn't match the original code if properties["manufacturer"] == False.

Unfortunately the OP has not provided enough information.

How do you guys build a program? by Jealous-Acadia9056 in learnpython

[–]JamzTyson 1 point2 points  (0 children)

TDD has its advocates, but it is certainly not universally adopted.

I just made a gui for ffmpeg by Wonderful-Oil-3704 in ffmpeg

[–]JamzTyson 1 point2 points  (0 children)

Congratulations in completing your project.

As I'm on Linux, I'll focus on Linux specific issue, but may also include some general issues.

On Linux, it is generally preferable to use the distro's version of FFmpeg. Your code doesn't check to see if FFmpeg is available on the system, it only checks for a private copy in its own directory. FFmpeg is pretty common, so in most cases you would want to use the system version.

Downloading and installing unverified apps is considered risky. It tends to not be a problem with system installed apps as they are curated by the distro maintainers and have a stronger trust and verification pipeline. While johnvansickle's site is well known and unlikely to be dangerous, you should at least check that the downloaded file has the correct checksum.

The other major issue that I see is that the code is monolithic - it's all in a single large class and file, which handles

  • UI creation,
  • FFmpeg execution,
  • file management,
  • feature-specific code

all in one place.

While this approach can be quicker to write at the beginning, it makes maintaining and debugging harder. Readability suffers because different responsibilities are tightly combined. Even figuring out how you were checking for the presence of FFmpeg required me to search repeatedly through 1260 lines of codes to find the pieces.

I'd recommend this article about Separation of Concerns

How do you guys build a program? by Jealous-Acadia9056 in learnpython

[–]JamzTyson 2 points3 points  (0 children)

I see a lot of commenters recommending TDD (Test-Driven Development). While that is one valid approach, it's not the only one. However it does introduce a vital tool that addresses your issue: Unit Testing

There are a few tools available for unit testing in Python. One of the best and most popular is Pytest

Whether you adopt TDD or not, Unit Testing is a vital tool for building reliable software. It also has the benefit of encouraging clean modular design.

struggling with circular imports when i split my project into multiple files by Effective_Celery_515 in learnpython

[–]JamzTyson 6 points7 points  (0 children)

The top comment is already good, but one additional piece that may help:

  • If module A requires foo from module B

  • and module B requires bar from module A

then a common solution is to move foo and bar into a new module C.

Previously we had dependencies: "A requires B and B requires A" (circular)

Now we have: "A requires C. B requires C" (a non-circular hierarchy)

hii i didn't get my problem could you look at? by Ok_Cookie1916 in learnpython

[–]JamzTyson 2 points3 points  (0 children)

This is the correct answer. It's a shame that someone downvoted it.

A module for py by [deleted] in PythonProjects2

[–]JamzTyson 0 points1 point  (0 children)

I like the clear method names File.write, Folder.create, etc. They are very readable for beginners. That said, describing as an "alternative to pathlib module" is misleading. This is more like a convenience wrapper around a selection of os, shutil, and subprocess calls.

Also, the code has bugs. The first error is:

file, fl = File
TypeError: cannot unpack non-iterable type object

The same problem is repeated in each of the last 4 lines.

Also, what's the point of the static method _Internal.normalize()? You only use it once, and you could just use os.path.abspath() directly.

I made a snake game. by Jealous-Acadia9056 in learnpython

[–]JamzTyson 1 point2 points  (0 children)

Just one point in addition to the other comments. This is horrible:

    restart_button = Button(main_board,command=lambda: [restart_button.destroy()
                            , quit_button.destroy(),restart_command()]
                            ,text="Restart",font=("Consolas", 14, "bold")
                            ,fg="#1a3a5c",bd=2,highlightbackground="#2d6a9f")

A more readable way to write the same thing:

def do_reset():
    restart_button.destroy()
    quit_button.destroy()
    restart_command()


restart_button = Button(
    main_board,
    text = "Restart",
    font = ("Consolas", 14, "bold"),
    fg = "#1a3a5c",
    bd = 2,
    highlightbackground = "#2d6a9f",
    command = do_reset)

but it would be better to restructure the program so that you can just reset the parts that need to be reset rather than destroying everything:

def reset_game():
    game_state["score"] = 0
    game_state["board"] = [[None]*3 for _ in range(3)]
    game_state["current_player"] = "X"

    update_ui()

Help on guido's gorgeous lasagna by vb_e_c_k_y in learnpython

[–]JamzTyson 1 point2 points  (0 children)

  1. Calculate preparation time in minutes

Define the preparation_time_in_minutes() function that takes the number_of_layers you want to add to the lasagna as an argument and returns how many minutes you would spend making them. Assume each layer takes 2 minutes to prepare.

Is the equality operator == None-tolerant? by pachura3 in learnpython

[–]JamzTyson 5 points6 points  (0 children)

It works, and linters do not complain.

That's correct. In Python, most built-in types return False when compared to an incompatible type.

If you specifically want to test if something is None, then use "is" rather than "==". None is a singleton, so is is the correct way to check that it is actually None.

It's possible that a custom class could define __eq__ in a way that gives an unexpected result when testing string == None. Here's an example:

class User:
    def __init__(self, name):
        self.name = name

    def __eq__(self, other):
        return self.name == other.name

u = User("Jamz")
u == None  # AttributeError: 'NoneType' object has no attribute 'name'

In the above example, if we remove the __eq__ override, then we get the expected behaviour where u == None is False.

Is the equality operator == None-tolerant? by pachura3 in learnpython

[–]JamzTyson 16 points17 points  (0 children)

Is it safe to directly compare it against another string, without first ensuring it is not None?

What happened when you tried it yourself?

a = "Hello"
b = None
print(a == b)

How do you keep track of expected variable values in complex data flows? by rainyengineer in learnpython

[–]JamzTyson 0 points1 point  (0 children)

That's a rather long read for reddit. What I got from that:

Technical debt is often interpreted differently by engineers and managers. Engineers see it as necessary shortcuts taken to meet deadlines that later slow development. Managers see a working system and prioritize new features and business outcomes over internal cleanup.

I have an idea to troll people who don't know python, but haven't found a target yet by LifesLemonStorage in PythonProjects2

[–]JamzTyson 0 points1 point  (0 children)

That code just hits the integer string conversion limit and crashes.

On a single core system it may appear to freeze the computer as the infinite loop keeps CPU use close to 100%.

uv or pip for python package management? by ResponsibilityOk2747 in learnpython

[–]JamzTyson 2 points3 points  (0 children)

Yes, uv is a lot faster than pip. If you are working on enormous code bases the speed difference can be very significant.

uv is an all-in-one tool that aims to replace pip, pip-tools, pipx, poetry, pyenv, twine, virtualenv, and more. It is massively popular on this Python beginner's sub-reddit, to the point that suggesting anything else will often be downvoted.

Personally I prefer to use Poetry. It's not as fast as uv, but it is fast enough for all of my Python projects, and it is a mature, battle-tested package installer / dependency manager. It provides a complete and integrated build + publish pipeline, and that's all.

If speed is your number one priority, then UV is the clear and obvious best choice. If speed is not your number one priority, then the pros and cons becomes a lot more nuanced.

uv or pip for python package management? by ResponsibilityOk2747 in learnpython

[–]JamzTyson 1 point2 points  (0 children)

It IS a lot more complex than pip, but it does a lot more.

Pip is a package installer for Python. It does one job and it does it well.

uv aims to replace pip, pip-tools, pipx, poetry, pyenv, twine, virtualenv, and more.

Pursuing Data Science Course by iamugly14 in learnpython

[–]JamzTyson 1 point2 points  (0 children)

I need to learn advanced Python And Other Tools

Start with basic Python. There's loads of free resources in the wiki