#๐Ÿ”’ idk how to simplify this code

7 messages ยท Page 1 of 1 (latest)

fluid grail
#
def decompile_block(ctx: Ctx, block: Block) -> None:
    signature = deepcopy(BLOCKS[block.opcode])
    if signature.menu:
        ast.flatten_menu(ctx, block, signature.menu)
    if field := block.fields._.get(signature.field or ""):
        if opcode := unwrap(signature.overloads).get(field[0]):
            signature.opcode = opcode
            with contextlib.suppress(ValueError):
                signature.inputs.remove(unwrap(signature.field))
        else:
            block.inputs._[unwrap(signature.field)] = [1, [4, field[0]]]
    ctx.iprint(signature.opcode)
    if signature.inputs:
        ctx.print(" ")
        ctx.commasep(signature.inputs, decompile_input, pass_self=True, block=block)
    ctx.println(";")
restive marlinBOT
#

@fluid grail

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.

fluid grail
#
def flatten_menu(ctx: Ctx, block: Block, menu_name: str) -> None:
    menu_id = inputs.block_id(block.inputs._.get(menu_name))
    if menu_id is None:
        return
    menu = ctx.blocks[menu_id]
    block.fields._.update(menu.fields._)
#
def block_id(input: list[Any] | None) -> str | None:
    if input is None:
        return None
    if isinstance(input[1], str):
        return input[1]
    return None


def menu(ctx: Ctx, block: Block, menu_name: str | None) -> str | None:
    if menu_name is None:
        return None
    menu_id = block_id(block.inputs._.get(menu_name))
    if menu_id is None:
        return None
    menu = ctx.blocks[menu_id]
    return next(iter(menu.fields._.values()))[0]
#
@dataclass
class Signature:
    opcode: str
    inputs: list[str]
    menu: str | None = None
    field: str | None = None
    overloads: dict[str, str] | None = None
restive marlinBOT
#

@fluid grail

Python help channel closed for inactivity

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.