#๐Ÿ”’ Need Help Refactoring

1231 messages ยท Page 2 of 2 (latest)

jade ledge
#

so u prob gonna come out of it with either too many questions or too many answers

next cove
jade ledge
#

u can ask it i dont mind

#

but this help thread should prob be focused more on python code

next cove
next cove
#

back

jade ledge
#

back with coding problems ? pithink

next cove
#

i watched to vid twice

next cove
#

gonna code now

#

i will code this addItem thing then goto bed

#

i wonder what should i call tommorows thread ๐Ÿ˜‚

jade ledge
next cove
#

im coming up with such complicated ways to make this work when it can be done way easier

oak heath
#

Well:
Time: 100%
Message: 62.5%

next cove
#

last one was 12 hrs

jade ledge
next cove
#
while quantity > 0:

    # fullstacks = quantity // maxs
    # extrastack = quantity % maxs
    # if extrastack > 0: totalstacks = fullstacks + 1
    # else: totalstacks = fullstacks
    
    slot = self.findFreeStackSlot()
    if slot is not None:
        current = (inventory[slot][item])
        available_space = maxs - current
        add_qty = min(quantity, available_space)
        inventory[slot][item] = (current + add_qty)
        quantity -= add_qty
    else:
        slot = self.findEmptySlot()
        if slot is None:
            print(f"No free slot available for remaining {quantity} of '{item}'.")
            return False
        add_qty = min(quantity, maxs)
        inventory[slot] = {item: (add_qty)}
        quantity -= add_qty

comments are new code

#

rest is old

jade ledge
#
    # if extrastack > 0: totalstacks = fullstacks + 1
    # else: totalstacks = fullstacks
#

btw u can do a funny trick here to simplify this

oak heath
oak heath
#

The message count display cap at 100k message (reason ik is I have a 100k+ message post)

jade ledge
# next cove ?

it might not be as readable but bool inherits from int

#

so anything that int can do bool can do too

next cove
#

u mean if extrastack > false?

oak heath
# next cove ...

The method to get the real message count may or may not(I am not disclosing this to get ban) violate the ToS so I am not disclosing how either

jade ledge
jade ledge
#

true to 1

#

but no i dont mean edittiing that

next cove
jade ledge
#
if extrastack > 0:
    totalstacks = fullstacks + 1 # if extrastack is True this runs
else:
    totalstacks = fullstacks     # if extrastack is False this runs
#

this code ya wrote right

next cove
#

ya

jade ledge
# next cove ya
if extrastack > 0:
    totalstacks = fullstacks + 1 # if extrastack is True this runs
else:
    totalstacks = fullstacks + 0 # if extrastack is False this runs
#

if i wrote it like this it changes nothing does it

next cove
#

yea

#

whats the catch

jade ledge
#

well since True evaluates to 1 and False evaluates to 0

oak heath
jade ledge
#

can ya guess where this going pithink

jade ledge
next cove
oak heath
next cove
jade ledge
next cove
#

ye he counted manually

jade ledge
#

im curious now

oak heath
#

I just imaginate the result

next cove
#

hes a good boy

jade ledge
heavy hedgeBOT
next cove
#

what would somenumber + false give us

#

nothing really changes

jade ledge
#

somenumber

next cove
#

1+0 is still 1

jade ledge
#

cause false evaluates to 0 ye

next cove
#

is there somehting here that im not understanding?

#

or youre just trying to teach me that bools can be used as ints too

next cove
#

i knew that

jade ledge
#

cause u can do some funny stuff with it

next cove
#

but still, thanks

oak heath
next cove
jade ledge
#
if extrastack > 0:
    totalstacks = fullstacks + 1 # if extrastack is True this runs
else:
    totalstacks = fullstacks + 0 # if extrastack is False this runs
#
totalstacks = fullstacks + extrastack > 0
#

its most likely not as readable

#

but ye its a funny trick u can do

next cove
#

wow

oak heath
#

Or just demonstration purpose

next cove
#

yea its not readable but i undersatnd it

jade ledge
oak heath
#

Nvm blindness issue ig

next cove
#

that was a cool one

#

we made this whole thing

#

into:

    fullstacks = quantity // maxs
    extrastack = quantity % maxs
    totalstacks = fullstacks + extrastack > 0
jade ledge
#

he using it pithink

#

well thats funny

next cove
#

we still have to add other stuff to make it work

#

but it was intresting indeed

jade ledge
#
    totalstacks = fullstacks + 1 if extrastack > 0 else 0
next cove
#

i know one liners

#

i use them quite often

jade ledge
#
    totalstacks = fullstacks + [0,1][extrastack > 0]
#

or this

#

anyways

#

somewhat esoteric offtopic

next cove
#

yea

#

at this point my brain is doing anything to run away from coding

jade ledge
#
for slot in range(max_slots):
next cove
#

makes no sense

#

lmao

#

do u know about
self.findFreeStackSlot() and self.findEmptySlot()?

jade ledge
#

idk difference btw the 2

next cove
#

lets say you want to add wood

#

and you have 2 wood in slot 1 of inventory

oak heath
next cove
#

self.findFreeStackSlot() returns that slot

oak heath
#

Oh well

next cove
#

and self.findEmptySlot() is self explanatory

#
    def findEmptySlot(self):
        inventory = self.path
        for index, slot in enumerate(inventory):
            if "air" in slot and (slot["air"]) == 0:
                return index
        return None

    def findFreeStackSlot(self):
        inventory = self.path
        item = self.item
        for index, slot in enumerate(inventory):
            if item in slot and (slot[item]) < maxs:
                return index
        return None
oak heath
next cove
#

its just to make sure

#

air is empty slot

#

and its always set to 0

oak heath
#

Hmm
Should just have an item slot call "empty" just in case "air" exist as an item in the future

next cove
#

"empty" is more likely to be an item than "air"

jade ledge
oak heath
#

...?

next cove
#

๐Ÿ˜‚

jade ledge
next cove
#

the hell would air be .. ?

oak heath
#

__internal_not_item_obj__

#

Less likely

jade ledge
next cove
jade ledge
#

shove air into a bottle and call that item air

next cove
oak heath
#

Chemical mixture that can reduce to oxygen, nitrogen, carbon dioxide etc.

next cove
#

๐Ÿซ™

jade ledge
#

anyways

next cove
#

anyways

#

im not coding this shit tonight

jade ledge
#

i think only reason he needs this is to have something in json ?

next cove
#

im procastinating too much

next cove
jade ledge
#

idk why u need stuff in json

next cove
#

nah
useless

jade ledge
#

can ya send the json part that has air

next cove
#

"air": 0

#

i also have:

#

def clearInventory(inventory):
for slot in inventory:
inventory[slot] = {"air": "0"}
print('Inventory Cleared!')

heavy hedgeBOT
#

Hey @next cove!

Please edit your message to use a code block

```py
print('Hello, world!')
```

This will result in the following:

print('Hello, world!')```
jade ledge
next cove
#
    "storage": {
        "inventory": [
            {
                "sword": 1
            },
            {
                "sword": 1
            },
            {
                "log": 99
            },
            {
                "log": 1
            },
            {
                "air": 0
            },
            {
                "air": 0
            },
            {
                "air": 0
            },
            {
                "air": 0
            },
            {
                "air": 0
            },
            {
                "air": 0
            }
        ]
    },
    "backpack": [
        {
            "air": 0
        },
        {
            "air": 0
        },
        {
            "air": 0
        },
        {
            "air": 0
        }
    ]
jade ledge
#

thats definetly a json format ye

#

anyways

next cove
#

my biggest change was moving from "0"s to int

oak heath
#

Just store null maybe? Could be plausible but could also be too complicated

next cove
#

they were everywhere

next cove
jade ledge
#
"storage": {
        "inventory": [
            {"sword": 1},
            {"sword": 1},
            {"log": 99},
            {"log": 1},
            {"air": 0},
            {"air": 0},
            {"air": 0},
            {"air": 0},
            {"air": 0},
            {"air": 0}
        ]
    },
    "backpack": [
        {"air": 0},
        {"air": 0},
        {"air": 0},
        {"air": 0}
    ]
next cove
#

it does

jade ledge
#

i just realized that inventory is more indented

oak heath
next cove
#

its not making it red

oak heath
next cove
jade ledge
next cove
#

is null None or unknown?

oak heath
#
[null, {"test": 1}]
jade ledge
#

should be None

jade ledge
next cove
#

will try that

oak heath
#

None cause unknown isn't a python type

jade ledge
#

thats cursed

next cove
next cove
jade ledge
# next cove i like this indent
"storage": {
    "inventory": [
        {"sword": 1},
        {"sword": 1},
        {"log": 99},
        {"log": 1},
        {"air": 0},
        {"air": 0},
        {"air": 0},
        {"air": 0},
        {"air": 0},
        {"air": 0}
    ]
},
"backpack": [
    {"air": 0},
    {"air": 0},
    {"air": 0},
    {"air": 0}
]
#

this indent should be more correct

next cove
#

how to achieve that

jade ledge
#

i literally just selected a few lines

#

and shift tab

oak heath
#

Well, depends on the configuration

jade ledge
#

vsc supports that

#

discord too for some funny reason

next cove
#

i use vsc
when i do alt shit f
it turns it to like the one i sent

next cove
#

alt shit

#

lmao

jade ledge
#

auto formatter is good for when u got a really messy file

#

but also its most likely way over the top

#

u can always configure it to do what u want tho

next cove
jade ledge
#

son is dumping

next cove
#

lmao

#

lets call it a night here

#

good night brodies

jade ledge
#

same time i imagine

next cove
#

and thanks for helping me โค๏ธ

jade ledge
#

ya welcome

next cove
next cove
jade ledge
#

u can close this now with !close or native discord close function

#

we can finally use that feature

#

after 20h

next cove
#

lol

#

lmao

#

iClose

#

whoops

#

dropped the !

#

it turned upside down

#

my bad

#

IClose

#

whoops

#

the dot fell off

#

!open

#

gn lol

#

!close

heavy hedgeBOT
#
Python help channel closed with !close

This help channel has been closed. 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.