#๐Ÿ”’ pls rate my code

211 messages ยท Page 1 of 1 (latest)

zinc galeBOT
#

@young bay

Python help channel opened

Remember to:

  • Ask your Python question, not if you can ask or if there's an expert who can help.
  • Show a code sample as text (rather than a screenshot) and the error message, if you've got one.
  • Explain what you expect to happen and what actually happens.

:warning: Do not pip install anything that isn't related to your question, especially if asked to over DMs.

young bay
#

main ```py
import time
from threading import Thread

import Modules.DataModule as DataModule
import Modules.UtilsModule as UtilsModule

def playtime() -> None:
stats = DataModule.get_stats()

while True:
    time.sleep(1)

    stats["time_spent_listening"] += 1

def main() -> None:
DataModule.load_data()

playtime_thread = Thread(target=playtime)
playtime_thread.start()

UtilsModule.check_for_mode()

UtilsModule.handle_users_input()

input()

if name == 'main':
main()

tall plume
#

You managed to package it into a wheel file or exe?

young bay
#

ye

#

wheel

#

im just asking if my code is good now tho

#

this is not packaged version

tall plume
#

its simple

#

simple is good

young bay
#

its the main script thats what its suposed to be

#

let me get the modules

tall plume
#

why not push it to your Github and share the repo?

young bay
#

good idea

#

gimme 1 sec

#

actual like a few min

#

im not used to github yet

tall plume
#

its not but its a start

young bay
#

so what did i do wrong?

tall plume
zinc galeBOT
#

Grawify%201.0%20Release/Modules/DataModule.py lines 23 to 24

global settings
global stats```
young bay
#

is it bad?

#

oh ye i forgot abt that

tall plume
#

!global

zinc galeBOT
#
Globals

When adding functions or classes to a program, it can be tempting to reference inaccessible variables by declaring them as global. Doing this can result in code that is harder to read, debug and test. Instead of using globals, pass variables or objects as parameters and receive return values.

Instead of writing

def update_score():
    global score, roll
    score = score + roll
update_score()

do this instead

def update_score(score, roll):
    return score + roll
score = update_score(score, roll)

For in-depth explanations on why global variables are bad news in a variety of situations, see this Stack Overflow answer.

young bay
#

i was told i shoudnt use it

#

ups mb

tall plume
#

๐Ÿ’ฏ

young bay
#

so i was thinking

#

insted of using globals

#

what if i made a function for that?

#

like you ask and it returns that

#

or changes

#

would that be better?

tall plume
#

I don't understand why you would make it a function. Its already a function. You just need to pass it in as a parameter/arg

young bay
#

oh

#

me dumb

#

ye i get it

tall plume
# zinc gale

As explained and recommended here โ˜๏ธ

young bay
#

but i was thinking i was told

#

i shouldnt put things in outer most scope

#

or is it global scope

tall plume
#

you can put things in outer scope

young bay
#

ok

tall plume
#

just pass it in as parameters

#

not as global

young bay
#

so its just the keywork global that you dont like

#

i just have 1 quesiotn?

tall plume
#

no thats not why I don't like it

tall plume
# zinc gale

This explains why I don't like it โ˜๏ธ

young bay
#

if i change something that is send as a parameter will it create a new copy of that or just change it

tall plume
#

I don't understand

young bay
#

so you said pass as parameter right

#

so if i change that like idk setings[dsds] ) = sasas

#

will it create a new version of settings

#

or update the old one

tall plume
#

I dont think you understand what you're asking. There's no "version" when you pass them in as parameters

#

They're the same

young bay
#

ok

#

that was my question

tall plume
#

Yea but there's no such thing as "old version" or "new version".

#

anyways

young bay
#

oh so there isnt?

#

gottcha

#

ye ima remove use of global keyword

zinc galeBOT
#

Grawify%201.0%20Release/Modules/PlaySongsModule.py line 129

play_random_song_you_have_not_played()```
young bay
#

wdym?

#

ah recursion

tall plume
#

Its calling the function within in the function

#

Why?

young bay
#

yes

#

so

#

when a song finishes

#

it doesnt return anything

#

and it just calls itself

#

so its like a loop

#

my logic

tall plume
#

I guess

#

A bunch of your functions don't return anything

young bay
#

dont ask why i did that in play_random_song_you_have_not_played func and not in play_random_song func

tall plume
#

Which is gonna be challenging if you ever need to write unit tests

young bay
tall plume
young bay
#

im from lua we do that alot there

zinc galeBOT
#

Grawify%201.0%20Release/Modules/UtilsModule.py lines 8 to 21

def convert_seconds_into_hours_minutes_and_seconds(seconds: int) -> str:
    if seconds >= 3600:
        h = seconds // 3600
        m = (seconds % 3600) // 60
        s = seconds % 60

        string = f"{h} hours, {m} minutes and {s} seconds"
        return string
    else:
        m = seconds // 60
        s = seconds % 60

        string = f"{m} minutes and {s} seconds"
        return string```
young bay
#

oh yep i see that

tall plume
young bay
#

what is unit

#

explain

tall plume
# young bay what is unit

๐™Ž๐™ฉ๐™–๐™ฎ ๐™ž๐™ฃ ๐™ฉ๐™๐™š ๐™ก๐™ค๐™ค๐™ฅ ๐™„๐™‰๐™๐™„๐™‰๐™„๐™๐™€๐™‡๐™”: https://snu.socratica.com/python Unit tests are a way to make sure your code is correct. Python comes with a built-in unit test framework that makes it easy to write a lot of tests for your software. In fact, many engineers will write a series of tests before they begin coding. This approach is called โ€œtest driven...

โ–ถ Play video
#

The "music" files you have in your Songs folder... I think you should replace them with public licensed music files.

#

You don't wanna get suspected for copyright

young bay
#

i mean

#

so

#

those are place holder

#

you are supposed to put your own

tall plume
#

I get it. Just replace them with public licensed ones

young bay
#

ah gottcha

#

so like copy right free

tall plume
#

exactly

young bay
#

i kinda forgot abt that

#

wait

#

i watched the vid you sent me

#

and why is j i????

tall plume
#

timestamp?

young bay
#

1:17

#

since when is that a thing

tall plume
young bay
#

yep

#

i hate this

tall plume
#

its just math

young bay
#

its i in math

tall plume
#

what i?

young bay
#

i = square root of -1

tall plume
#

oh that

#

yeah its normal math

young bay
#

ok but why j?

tall plume
#

but that's not the point of the video

young bay
#

ye ik

#

i just hate it

#

im at 3min rn

tall plume
#

the point of the video is to test your function continuously works as you develop new features or expand existing functions

young bay
#

ye

#

did i not do that?

tall plume
#

j is a complex number

young bay
#

ik that

#

ik

#

its i in math tho

tall plume
young bay
#

umm deleted all my test

#

am i supposed to keep em?

tall plume
#

yea

young bay
#

i didnt know that

#

i never got a bug with my code yet

tall plume
#

It helps as time goes by, new Python upgrades, new versions to libraries, etc. You can quickly test which new version breaks your current which functions

young bay
#

ah

#

i mean everything works so far

tall plume
#

I'll take your word for that

young bay
#

ok

#

i mean works for me

tall plume
#

Theres no requirements.txt file?

young bay
#

whats that?

tall plume
#

do you know what you need to pip install all the time?

young bay
#

ye

#

i says

#

im missing this so i insatall

tall plume
#

requirements.txt file helps so you don't have to guess

young bay
#

huh why would i guess

#

me confused

tall plume
#

pip freeze > requirements.txt is a quick way to set it up

#

Then push that text file to Github

young bay
#

i do that in pycharm or cmd?

tall plume
#

Then later, in another computer or whatever, you can pip install -r requirements.txt

young bay
#

oh ok

tall plume
zinc galeBOT
#

Grawify%201.0%20Release/Modules/PlaySongsModule.py lines 4 to 5

from mutagen.mp3 import MP3
from audioplayer import AudioPlayer```
young bay
#

i got it

#

ye thats it

#

hey it put alot of things i dont use here:

altgraph==0.17.4
audioplayer==0.6
certifi==2024.8.30
Grawify @ file:///C:/Users/Ziga/OneDrive/Desktop/Grawify/dist/Grawify-1.0-py3-none-any.whl#sha256=b4b6c9873f8ec0ec67b28a08a0a14bc2870cc8ace0bf1d1682f2542e1955c640
mutagen==1.47.0
packaging==24.1
pefile==2024.8.26
playsound3==2.2.2
pyinstaller==6.10.0
pyinstaller-hooks-contrib==2024.8
pywin32-ctypes==0.2.3
setuptools==75.1.0

tall plume
#

I guess remove the ones you don't need

young bay
#

i dont even know alot of those

#

do i keep the py insatller

#

?

tall plume
#

alright thats all of the faults I can find in your code. 5/10 its good practice project.

young bay
#

thats decent

tall plume
young bay
#

i mean is it required?

#

or no idk

tall plume
#

depends

young bay
#

or that certifi

tall plume
#

!pip certifi

zinc galeBOT
#

Python package for providing Mozilla's CA Bundle.

Released on <t:1724982904:D>.

tall plume
#

thats a packaging lib

#

just imagine if there's another dev and they clone your repo. Do you want them to install that to help work on new features with you?

young bay
#

no

#

i dont think i want em to steal my code

tall plume
#

why would they?

young bay
#

didnt you say that?

#

(im stupid pls no judge)

tall plume
#

they wouldnt

young bay
#

ok

tall plume
#

If they wanted a music player, theres hundreds of other options

young bay
#

ye ik

#

so keep?

tall plume
#

wdym?

young bay
#

i think i keep it then?

#

if i undrestand you

tall plume
#

again, thats up to you. Do you believe certifi or pyinstaller lib is required to install in order to continue working on your Grawify project or not?

#

If its not, delete it

#

If it is, keep it.

young bay
#

ima keep it then

#

this is it then ig:

audioplayer==0.6
certifi==2024.8.30
Grawify @ file:///C:/Users/Ziga/OneDrive/Desktop/Grawify/dist/Grawify-1.0-py3-none-any.whl#sha256=b4b6c9873f8ec0ec67b28a08a0a14bc2870cc8ace0bf1d1682f2542e1955c640
mutagen==1.47.0
pyinstaller==6.10.0
pyinstaller-hooks-contrib==2024.8
tall plume
young bay
#

oh ok

#

i upload to github now?

tall plume
#

Did you also fix the global thing?

young bay
#

not yet

#

gimme 1 min

#

not entierly sure how to fix it tho

tall plume
young bay
#

i deleted em

tall plume
#

Make a new one

young bay
#

that takes a while

tall plume
#

I guess you got work to do

#

Gambatte

young bay
#

ye

zinc galeBOT
#
Python help channel closed

This help channel has been closed and it's no longer possible to send messages here. If your question wasn't answered, feel free to create a new post in #1035199133436354600. To maximize your chances of getting a response, check out this guide on asking good questions.

#

๐Ÿ”’ pls rate my code