Posts: 5
Threads: 1
Joined: Apr 2021
Hello,
I am trying to convert a script from JS to PY and it is already working good.
However, here is one I just can't get right (although it doesnt break), maybe someone has an idea?
This is the definition of a structure in Typescript:
interface poiinfo {
phone: string;
waypointID: number;
lang: 1;
src: 'HERE';
coord: {
lat: number;
alt: number;
lon: number;
type: 0;
},
addr: string;
zip: string;
placeid: string;
name: string;
} What would be the correct syntax for it in Python?
Thank you!
Posts: 1,838
Threads: 2
Joined: Apr 2017
Use a class, or named tuple to represent your domain objects.
Posts: 5
Threads: 1
Joined: Apr 2021
I don't know if I understand you correctly ... I am looking for the equivalent Python syntax for the code snipped I have posted.
How to define a structure having the same variables?
I have tried this:
poiinfo = {
'phone': '',
'waypointID': 0,
'lang': 1,
'src': 'HERE',
'coord': {'lat': 0, 'alt': 0, 'lon': 0, 'type': 0},
'addr': '',
'zip': '',
'placeid': '',
'name': ''} But especially the inner part "coord" doesn't seem to be right ...
Posts: 1,838
Threads: 2
Joined: Apr 2017
Oh, you just want a map. Ok, so what's not right about what you have?
Posts: 5
Threads: 1
Joined: Apr 2021
Thank you - so my code is basically not wrong. But how would I write it so I will have an array of poiinfo?
Something like this:
poiinfo [0] = {
'phone': '',
'waypointID': 0,
'lang': 1,
'src': 'HERE' ...}
poiinfo [1] = {
'phone': '',
'waypointID': 1,
'lang': 1,
'src': 'HERE' ... } Doesn't work. What's the correct synatx?
Posts: 6,792
Threads: 20
Joined: Feb 2020
Apr-07-2021, 06:41 PM
(This post was last modified: Apr-07-2021, 06:41 PM by deanhystad.)
poiinfo = [
{
'phone': '',
'waypointID': 0,
'lang': 1,
'src': 'HERE',
},
{
'phone': '',
'waypointID': 1,
'lang': 1,
'src': 'HERE',
}
]
Posts: 1,838
Threads: 2
Joined: Apr 2017
I mean, you could have looked up how to do basic data structures in Python right?
Posts: 5
Threads: 1
Joined: Apr 2021
Thank you very much - so I can access the second one by
print(poiinfo[1].waypointID) Right?
I tried looking it up, all examples I found were simple 1,2,3 arrays.
Posts: 5
Threads: 1
Joined: Apr 2021
Got it by trying:
print(poiinfo[1]["waypointID"]) Thanks for your help, guys!
|