#🔒 how sould i annotate this / how should i define this?

79 messages · Page 1 of 1 (latest)

worldly wyvern
#

so i wanted to make an automat simulator.
as the input to the init of the automat class i need some kind of description.
rn im using this way to describe it:

automat = (
    ((1,), (5,), "-0", True),
    ((0,), (2,), "+0", True),
    ((3,), (1,), "-1", False),
    ((2,), (4,), "+1", False),
    ((5,), (3,), "-2", False),
    ((4,), (0,), "+2", False)
)

(this is parsed into a way the code can manage better)
what this means (so an alternative way can be made if thats simpler)
the rows are nodes
the last 2 are the name of the node and if it is accepting or not
the cols are the options of the automat
points can be either None or a list of indexes showing that on node,input which nodes to go to
the shape can have any amount of nodes/inputs so how would i annotate this?

slim troutBOT
#

@worldly wyvern

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.

#

Hey @cosmic sky!

It looks like you're trying to paste code into this channel.

Discord has support for Markdown, which allows you to post code with full syntax highlighting. Please use these whenever you paste code, as this helps improve the legibility and makes it easier for us to help you.

To do this, use the following method:
```py
print('Hello, world!')
```

This will result in the following:

print('Hello, world!')```
You can **edit your original message** to correct your code block.
cosmic sky
#

Wait ı test

#

exactly what do you want to do?

worldly wyvern
#

main problem of mine is tuple[tuple[int, ...], ..., str, bool] is not valid

cosmic sky
#

I started wait

#

I will write it in my notebook and send it to you, it would take a long time to write it from here.

cosmic sky
#

follow my steps practically

quick cape
#

if you really want type hinting on that, i'd make it an object

cosmic sky
#

Why not

quick cape
#

arbitrary types of nested iterables can be a pain to work with

worldly wyvern
#

the class reworks it into a shape code can handle good but its hard to define it in that shape

quick cape
#

then i'd put the arbritrary elements inside a list

#

and type that list

worldly wyvern
#

basically i put a node in a node and the node into the same node i put into it so i always just choose the node from the node and even loops are handled
but thats impossible to readibly define as an input

quick cape
#

(so replace all your ellipsis)

teal ingot
#

should be ```py
tuple[tuple[tuple[int], tuple[int], str, bool], ...]

or ideally, use a list instead of a tuple for elements with a variable length

list[tuple[tuple[int], tuple[int], str, bool]]

cosmic sky
#

Did you set up a database for this?

worldly wyvern
worldly wyvern
quick cape
teal ingot
quick cape
#

that isn't supported by tuple type hinting syntax yet

cosmic sky
quick cape
#

what id recommend is changing your input to be typed like that

tuple[tuple[int, list[T]], tuple[int, list[T]], list[T], str, bool]```
or
```py
tuple[tuple[int, ...], tuple[int, ...], list[T], str, bool]```
so that you only have 1 list to make
cosmic sky
#

I got help from my professor friend, I will send you a file, open it and read it

quick cape
#

(can use an ellipsis for the 2 inside tuples but not for the main one)

worldly wyvern
quick cape
#

that's what i meant

worldly wyvern
quick cape
#

you can keep the last 2

#

but put all elements between 2 first and 2 last inside a list

#

tuples require homogenous strict typing

worldly wyvern
quick cape
#

do you have a full automat input example ?

#

with irregular lengths

#

so we can try if the type hinting works

worldly wyvern
quick cape
#

i see, that doesn't change much

worldly wyvern
#

like :
[
[0,1,2,0,1,2,0,1,2,0,"0",True],
[1,2,0,1,2,0,1,2,0,1,"1",False],
[2,0,1,2,0,1,2,0,1,2,"2",False],
]
its an old definition tho but i think u can see how it would look like

quick cape
#

wait where are the tuple of ints in there ?

#

also can you edit that input freely ?

#

else what i'd do is some kind of destructuring assignment which itself is typed

#

(so i wouldn't type the whole object, or i'd only do something like tuple[tuple[Any]] and i'd type the values while parsing)

teal ingot
#

do not invade other people's help channels to ask for help with your own issue

cosmic sky
#

Wrong Channel sorry

worldly wyvern
#

Its the definition of the last one i made that cant handle undeterministic automats

quick cape
#

ye cause tuple[stuff, stuff, ..., stuff, stuff] isnt supported yet
tuple[stuff, ...] is tho

cosmic sky
#

Did you follow the my friends steps?

worldly wyvern
#

Not for the any letter abc

cosmic sky
#

Did it work for you?

worldly wyvern
cosmic sky
#

Ok
^___^

quick cape
#

then again, here's what i'd do :

automat: tuple[tuple[Any, ...], ...] = (
    ((1,), (5,), "-0", True),
    ((0,), (2,), "+0", True),
    ((3,), (1,), "-1", False),
    ((2,), (4,), "+1", False),
    ((5,), (3,), "-2", False),
    ((4,), (0,), "+2", False)
)


firstElem: tuple[int, ...]
secondElem: tuple[int, ...]
inBetween: list[int] # or whatever type inside
beforeLastElem: str
lastElem: bool

for row in automat: # or any way you're parsing that
  firstElem, secondElem, *inBetween, beforeLastElem, lastElem = row
  # do your stuff
worldly wyvern
#

So just make the annotation less strict until its possible to define?

quick cape
#

yep

#

tuple[tuple[Any, ...], ...] is the strictest you can do on the whole input object

#

except if you can edit it beforehand

#

actually even stricter would be to replace Any with an Union of all possible types inside

#

so currently tuple[int, ...] | str | bool

#

but idk what types you can have

worldly wyvern
#

Only that

quick cape
#

so the type could be ```py
tuple[tuple[tuple[int, ...] | str | bool, ...], ...]

#

but remember that doesn't handle position

#

(and only works from python 3.10 onward, but that should be a non-issue, if you need to support earlier version you can find alternative syntax with the typing module)

worldly wyvern
#

But i hoped it could

quick cape
#

id use a totally different data type if you want to handle position, like maybe a pandas dataframe could do it (although you'd need dynamic columns)

slim troutBOT
#
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.