#πŸ”’ problem with try-except removing values from my variables

289 messages Β· Page 1 of 1 (latest)

empty gladeBOT
#

@versed 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.

versed glacier
golden flint
#

That's definitely a really bad usage of try:except.

#

Normally in a try/except we only put the singular line that can throw the error. Not a massive block of code

versed glacier
#

i asked chatgpt how to solve it

#

it told me to try try-except

#

bcs else didnt work

empty gladeBOT
#
Error handling

A key part of the Python philosophy is to ask for forgiveness, not permission. This means that it's okay to write code that may produce an error, as long as you specify how that error should be handled. Code written this way is readable and resilient.

try:
    number = int(user_input)
except ValueError:
    print("failed to convert user_input to a number. setting number to 0.")
    number = 0

You should always specify the exception type if it is possible to do so, and your try block should be as short as possible. Attempting to handle broad categories of unexpected exceptions can silently hide serious problems.

try:
    number = int(user_input)
    item = some_list[number]
except:
    print("An exception was raised, but we have no idea if it was a ValueError or an IndexError.")

For more information about exception handling, see the official Python docs, or watch Corey Schafer's video on exception handling.

versed glacier
#

it doesnt send as a text

#

i write 3x "`"

#

it doesnt send

timber beacon
#

!paste

empty gladeBOT
#
Pasting large amounts of code

If your code is too long to fit in a codeblock in Discord, you can paste your code here:
https://paste.pythondiscord.com/

After pasting your code, save it by clicking the Paste! button in the bottom left, or by pressing CTRL + S. After doing that, you will be navigated to the new paste's page. Copy the URL and post it here so others can see it.

versed glacier
#

the problem is that i want the code if it doesnt locate "failed_upload" to skip the part of the code and go straight to look for "clue_post"

timber beacon
versed glacier
#

like this?

timber beacon
#

You should consider reducing the scope of your try block. The larger it is, the more difficult it will be to determine what the error was.

#

especially when you use a bare except like you are

versed glacier
#

yea but i kinda need all the things that are within

#

otherwise its gonna be useless for me

versed glacier
timber beacon
#

use more functions.

#

use a flag for if it failed and check that flag later

versed glacier
tepid bolt
#

use your words.

timber beacon
#
try:
  do_thing()
except SomeError:
  failed = True
else:
  failed = False

do_something_else()
if failed:
  do_if_failed()
versed glacier
#

i see

#

but try devalued the variables as well

#

see the "location_title"

#

its defined well and it worked before try-except

timber beacon
#

try didn't "devalue" them. If the variables are undefined, it means an exception was raised before they could be assigned.

versed glacier
#

oh yeah something like this said chatgpt too

tepid bolt
#

forget about chatgpt for a bit.

versed glacier
#

well how do i assign them bcs i was told to assign them within the try

#

but the problem is my code has 23 processes like the one i showed in example

timber beacon
#

!e ```py
try:
if True:
raise Exception
foo = "bar"
except:
print(foo) # Undefined

versed glacier
#

like 3000 lines of code

empty gladeBOT
#

@timber beacon :x: Your 3.12 eval job has completed with return code 1.

001 | Traceback (most recent call last):
002 |   File "/home/main.py", line 3, in <module>
003 |     raise Exception
004 | Exception
005 | 
006 | During handling of the above exception, another exception occurred:
007 | 
008 | Traceback (most recent call last):
009 |   File "/home/main.py", line 6, in <module>
010 |     print(foo) # Undefined
011 |           ^^^
... (truncated - too many lines)

Full output: https://paste.pythondiscord.com/I3U6RYYQGMD2HJNQOBPDGC3UKU

timber beacon
#

NameError: name 'foo' is not defined

versed glacier
#

so if i wanted to assign them within try

#

the whole code would have to be within try

#

no?

timber beacon
#

Assign them a default value, like None

#

!e ```py
foo = None
try:
if True:
raise Exception
foo = "bar"
except:
print(foo)

empty gladeBOT
#

@timber beacon :white_check_mark: Your 3.12 eval job has completed with return code 0.

None
versed glacier
#

yes i see

#

and there isnt a way to assign them at the top of the code

#

bcs the whole code is little bit long

timber beacon
#

That's what I just did. foo = None

versed glacier
#

oh i see

#

so basically just to recap this, i set the value of foo

#

and in the try if it finds out that the value is different than i set before

#

it will do something different

#

that i set

#

if the value is as i set it before it will continue?

timber beacon
#

maybe?

versed glacier
#

wdym maybe

timber beacon
#

you can check if foo is None if that's what you mean

versed glacier
#

OHHH

#

i understand the example now

timber beacon
#

I would actually suggest wraping everything that might raise in a try/except, setting the default in the except

versed glacier
#

if i set foo as pizza and then the if is gonna be false bcs (in my script the py auto gui might not find the image) it will print pizza but if it finds the image like in my script it will print bar

timber beacon
#

ex: ```py
try:
number = int(x)
except ValueError:
number = 0

#

Gonna be honest with you. Your script (snippet) is way too complex.

tepid bolt
#

i wonder how much of it is chatgpt

tepid bolt
#

yeah....

versed glacier
tepid bolt
#

if you had written it on your own and actually learned how it works, it most likely would be far simpler

tepid bolt
versed glacier
timber beacon
#

!pip praw

versed glacier
empty gladeBOT
#

PRAW, an acronym for "Python Reddit API Wrapper", is a Python package that allows for simple access to Reddit's API.

Released on <t:1689108713:D>.

versed glacier
#

i cant use praw on new created accs

#

so i had to come up with different solution

#

i can use praw on aged ones

tepid bolt
versed glacier
#

but if you use praw to post you get shadowban

versed glacier
#

reddit allows 3 scripts for use and gives you api

timber beacon
versed glacier
#

also

#

praw cant handle flairs

versed glacier
versed glacier
#

this one?

timber beacon
#

I don't know what that is.

#

That's nodejs

versed glacier
#

well thats the thing kinda

#

i need to make bot for reddit

timber beacon
#

Can you please stop with the monkey gifs?

versed glacier
#

okay im sorry

versed glacier
#

also i have like 65 variables there

#

all of them i need to list like that?

timber beacon
#

It means you need to refactor

#

good luck

versed glacier
#

wdym refactor

timber beacon
#

refactor means to change the code to make it better without changing the functionality. This can involve renaming symbols, extracting functions, reorganization, etc

versed glacier
#

yeah i tried to do that with chatgpt

#

but it didnt make it

#

bcs its too long

timber beacon
#

Exactly what is your "bot" going to do?

#

also, your bot probably doesn't get banned because it uses praw. It probably gets banned because you spam.

versed glacier
#

i posted one post

#

BAN

#

i tried on several accs

#

same result

timber beacon
#

What did you post?

versed glacier
versed glacier
timber beacon
versed glacier
#

well

#

i cannot go too much into details but

#

i can say its gonna automate posting into few subreddits to bring more into the reddit community

#

im just lazy posting myself

#

so i wanted something that will do it for me

timber beacon
#

So you're advertizing your community to other subreddits?

versed glacier
#

nah

#

i dont have community

#

im posting images

#

thats why you see the coordinates

timber beacon
#

What do you mean "bring more into the reddit community"?

golden flint
#

So you're spamming

versed glacier
#

i would get banned for spamming

timber beacon
#

It could've been your ip got shadow banned

versed glacier
versed glacier
#

i have proxies

timber beacon
#

the proxies got shadow banned too

golden flint
#

!rule 5

empty gladeBOT
#

5. Do not provide or request help on projects that may violate terms of service, or that may be deemed inappropriate, malicious, or illegal.

versed glacier
#

at what point is this illegal?

timber beacon
#

breaking ToS, ban evasion, etc

golden flint
#

If what you're doing is getting your ips banned then clearly they don't want you doing what you're doing

versed glacier
#

i never got ip banned

timber beacon
#

I'm saying your proxies got banned

versed glacier
#

they didnt wdym

golden flint
#

Anyway doesn't reddit have an API for posts anyway?

versed glacier
#

idk what exactly violates ToS bcs if 200 people post into subreddit r/aww cute picture of their dog, its okay, if i do it - its okay but if i create a bot that will do it for me its illegal?

tepid bolt
timber beacon
versed glacier
#

in apps/prefs

timber beacon
#

He basically said "isn't the sky blue?"

versed glacier
#

yes but this script im writing is not for the accounts that get banned for posting through praw

#

i was saying that new created accs

#

get banned

#

if you post through api

golden flint
versed glacier
#

and also that api cant handle flairs thats why i have this script

golden flint
#

Meaning it violates the rules here. Which you've been told but you seem to disagree so the mods can make a ruling

timber beacon
#

flairs are for users only (probably)

tepid bolt
#

automating bot posting like this is against TOS

versed glacier
#

can you show me the rule?

golden flint
#

I already did

#

!rule 5

empty gladeBOT
#

5. Do not provide or request help on projects that may violate terms of service, or that may be deemed inappropriate, malicious, or illegal.

versed glacier
#

okay i saw this one

#

at what point my script violates ToS and where?

timber beacon
#

I think he means the reddit tos

versed glacier
#

so i can create bot through praw

#

that can spam images fast af

timber beacon
#

no

versed glacier
#

but i cant create a bot that will slowly post images instead of me doing it?

timber beacon
#

that gets you banned

#

you can't possibly post as fast as a bot

versed glacier
#

yes

#

i agree

#

thats why im not doing that

#

but this script is slow

#

and would do what i do

golden flint
versed glacier
#

ye but if this bot literally does what i would do

#

and what im doing

#

for couple months

tepid bolt
#

the difference is that it's being done programatically with the bot.

versed glacier
#

and reddit seems to like it

versed glacier
#

and hows that bad i got things to do

tepid bolt
#

right, that's not you doing it.

versed glacier
#

family needs to be taken care of

timber beacon
#

maybe automod is zapping you?

versed glacier
tepid bolt
timber beacon
#

automod in some communities requires a minimum karma to post.

versed glacier
#

and about 300 post

timber beacon
#

in your new accounts, that would be 0

versed glacier
#

thats why im not doing this for new accs

timber beacon
versed glacier
#

bcs i tried it

timber beacon
#

So you tried it, your posts got removed for no karma, so you assume reddit was banning praw?

versed glacier
timber beacon
#

praw even lets you change the user-agent

timber beacon
#

the only thing they know is that you're using the api.

near jacinth
#

So there is this in reddits terms:

Access, search, or collect data from the Services by any means (automated or otherwise) except as permitted in these Terms or in a separate agreement with Reddit (we conditionally grant permission to crawl the Services in accordance with the parameters set forth in our robots.txt file, but scraping the Services without Reddit’s prior written consent is prohibited)
Under section 7: https://www.redditinc.com/policies/user-agreement

#

If you aren't using the API, you would not be using the services as permitted

versed glacier
#

i meant

#

setting flairs

#

sry

near jacinth
#

Probably because they don't want you to do that automatically

#

You could send a request asking for it to be added

timber beacon
#

Meanwhile lemmy: "I'm going to send everything I know about everything to you at /inbox. Ready? I'm sending it. I hope your bandwidth is enough"

near jacinth
#

Otherwise, you would be bypassing their defined methods of interacting with the service

timber beacon
#

it's technically the api for server-server connections

versed glacier
#

bcs if its allowed for me to get a VA to do it

timber beacon
#

It means whatever is most convenient for reddit's lawyers

versed glacier
#

it doesnt make sense its not allowed for me to get python script to do it

near jacinth
#

I'm not a reddit lawyer, but it seems pretty clear the project you are seeking help with would potentially violate reddits terms. Therefore we won't provide help with it on this server

versed glacier
#

ye i get that but what about the VA argument?

timber beacon
#

We can help with praw, but not pyautogui or selenium

near jacinth
#

I have no idea what abbreviation you are using

versed glacier
#

VA - virtual assistant

near jacinth
#

Is that permitted in the terms?

#

(I doubt it)

versed glacier
#

i think so

#

yeah it is

#

ppl are doing this on average

golden flint
#

Wouldn't that fall under automated?

versed glacier
#

yes

#

i guess

#

but this is were it stops making sense the rules

#

reddit is basically saying if you wanna contribute to our community YOU MUST SPENT YOUR TIME DOING IT

#

like why

#

reddit doesnt see the difference

near jacinth
#

Or use their API appropriately

timber beacon
#

Because bots don't view ads

golden flint
#

Because that's how most communities work

#

Same reason why we don't want bots on this discord

timber beacon
#

(unapproved bots)

versed glacier
#

well i cant tell if im talking to bots 100% and yall cant tell if yall are speakin to one 100%

tepid bolt
#

you just told us.

#

that you're literally doing it.

timber beacon
#

bad bot

tepid bolt
#

that's enough for r5

versed glacier
#

yall dont see the point?

tepid bolt
#

you obviously have personal investment on this so of course you'll be biased and refuse to see the point

versed glacier
#

yee its fun ngl

#

but yall aint got arguments against this one

tepid bolt
#

we literally gave the arguments

timber beacon
#

Try using praw to post to your /u/ page

tepid bolt
#

you ignored them.

versed glacier
#

yes

#

bcs i have better ones

#

that counter

tepid bolt
#

no you don't, you just have biased arguments

versed glacier
#

okay so: can you tell for 100% who are you speaking to?

tepid bolt
#

almost choked on water on that one, saving it

languid musk
timber beacon
golden flint
languid musk
#

For the rarest of legal moments.

versed glacier
#

ye but im explaining to yall that the rule doesnt make sense itself and cannot be even used so why to obey it

languid musk
tepid bolt
versed glacier
languid musk
#

Ok, then I think we're done

versed glacier
#

i wasnt even supposed to argue this whole convo but it was fun

languid musk
#

!close

empty gladeBOT
#
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.