#๐Ÿ”’ How to make code smaller and more efficient to read? 2531 lines of mess.

143 messages ยท Page 1 of 1 (latest)

hollow glacier
#

Help, I have been self-taught in all of my python learning and have never been efficient with how I code, please help me with advice on how to shorten my code..

shell trellisBOT
#

@hollow glacier

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.

hollow glacier
#

Theres code to review

lone meteor
#

Have you used "for in range()" or "def ..."

hollow glacier
#

def yep

#

lots of functions

lone meteor
#

I don't have much to offer as I have this problem myself...

hollow glacier
#

ahahaha dw man my code is 'Ahh I just about understand this..'

mellow forum
#

make it modular

#

whatever that word means

#

put stuff into functions

hollow glacier
#

how do i call the script when its exe..

mellow forum
#

what

#

you're compiling python scripts into an executable?

hollow glacier
#

how do i cntl printscreen lol

#

and yeh

mellow forum
#

word of advice don't do that

hollow glacier
#

it works as is tho and it sorta feels more official ykwim?

mellow forum
#

-_-

hollow glacier
#

just me?

mellow forum
#

it doesn't sound very official

#

python scripts aren't made to do this

hollow glacier
#

it does bro its got animations and stuff

#

they wernt? actually what was the purpose for python, just algorithms?

mellow forum
#

if you really need to get an executable

which sometimes, I do too,

either

  1. make a batch file to execute the command
    python filename.py

  2. translate code hand to hand into C++ ( I do this when I need executables, fast ones especially)

#

if you don't want much hassle

#

just put it into a batch file

#

a batch file is a file that ends in .bat

hollow glacier
#

i use pyinstaller lol and like the batch file, is it customisable?

#

ik what it is dw

mellow forum
#

you want to send this code to someone?

hollow glacier
#

not necessarily they would need the programs it launches tbh

#

theres an assets folder I can send em but they would need to have everything here

hollow glacier
mellow forum
#

but you can create a shortcut of a batch file

hollow glacier
#

thats hurtful. It launches apps bro

mellow forum
#

and give it any icon you want

hollow glacier
#

yeah I could do that..

mellow forum
#

and best of all

#

you can pin the shortcut to the start menu

hollow glacier
#

yeh ik you can do that with nearly anything

mellow forum
#

so you can run it with a click

hollow glacier
#

but like code.. how to shorten it?

mellow forum
#

oh right

#

I'll share an example

hollow glacier
#

I was thinking parameters an just using loads of them but.. its confusing

mellow forum
#

wait a sec my head is spinning

hollow glacier
#

Its in a class so..

hollow glacier
mellow forum
#

would you mind waiting about 30 seconds, my head is spinning wildly

hollow glacier
#

do not look at the code then.

mellow forum
#

okay I'm back

#

sorry for the delay

#

Let's get back to the code

#

I'll share an example of my project

hollow glacier
#

your name is interesting.. a transitional metal compound?

mellow forum
mellow forum
#

anyways

#

I don't have much code for now

hollow glacier
#

I am better at biology science personally..

mellow forum
#

but here's a project that has been divided into 4 files

#

it's a simple version control software

#

what you can learn from this is

#
  1. I have broken individual commands, that my program can execute
#

into individual files

hollow glacier
#

would you import [python file name].py?

mellow forum
#

and then in each of the files, I have implemented the code for that particular command

#

you don't need .py

#

after the file name

#

just

import file
hollow glacier
#

and howcome? wouldnt it try to import a module?

mellow forum
#

a module

#

is also a file

#

stored somewhere

hollow glacier
#

if you were trying to get a py file from a different directory would you have to include the path?

mellow forum
#

yes

hollow glacier
#

how? import C:\ and so on?

mellow forum
#

this is a webpage containing the names of python modules

#

if you don't name your files after the module names listed here

#

they won't be imported

mellow forum
hollow glacier
#

i.. dont get it tbh but very good information I might try figure out and understand when I get back from picking someone up, Thank you very much have a good day..

mellow forum
#

okay

#

I'll just summarise it for you

#
  1. Break the file into several files
hollow glacier
#

plz dm me it, might not be back

mellow forum
#

that's it

#

that's the solution

#

break it

hollow glacier
#

what if I had to pass in variables to the py file?

#

config file the mf?

mellow forum
#

mf?

hollow glacier
#

variable

mellow forum
#

did you just swear at me

#

oh

hollow glacier
#

no lol im british were always frustrated and angry

mellow forum
#

like passing in arguments to the file?

hollow glacier
#

yeh

#

no like variables

mellow forum
#

well I think you could use sys.argv

hollow glacier
#

data from the previous file

mellow forum
#

oh

#

it's imported

#

I'll show ya

hollow glacier
#

import [variable name]?

mellow forum
#
# a.py
a = 12
b = 13
hollow glacier
#

OR from [python file] import [variable name]

#

that would make so much sense lol

mellow forum
#
# b.py

import a
a = 14
print(a) # prints 14
print(a.a) # prints 12
mellow forum
#

but that would import only one variable

hollow glacier
#

that is sick.. would it import a variable within a class?

mellow forum
#

a class variable?

hollow glacier
#

yeh

#

so mine would be self.[variable]

mellow forum
#

I suppose so?
I never use OOP so I need to try

hollow glacier
#

honestly I wanna learn java or c+

mellow forum
#

if you really want to

#

then learn C++

hollow glacier
#

its scary moving lang tho..

mellow forum
#

โ€œC makes it easy to shoot yourself in the foot; C++ makes it harder, but when you do it blows your whole leg off.โ€

#

it's actually not that scary

hollow glacier
#

in java the language works on most platforms tho..

mellow forum
#

it's just that sometimes the newer syntax can be a bit confusing

mellow forum
hollow glacier
#

no it aint lol hard af

mellow forum
#

although a lot of people would disagree with my statement

hollow glacier
#

they use classes right at the start and its like whaa

mellow forum
#

especially die hard java fans

mellow forum
hollow glacier
#

yeah man thanks im gonna be late for her but oh well, have a good day

mellow forum
#

for her?

#

๐Ÿ˜

#

good luck mate

#

type !close to close the channel

shell trellisBOT
#
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.