Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
typescript -> python syntax
#1
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!
Reply
#2
Use a class, or named tuple to represent your domain objects.
Reply
#3
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 ...
Reply
#4
Oh, you just want a map. Ok, so what's not right about what you have?
Reply
#5
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?
Reply
#6
poiinfo = [
    {
    'phone': '',
    'waypointID': 0,
    'lang': 1,
    'src': 'HERE',
    },
    {
    'phone': '',
    'waypointID': 1,
    'lang': 1,
    'src': 'HERE',
    }
]
Reply
#7
I mean, you could have looked up how to do basic data structures in Python right?
Reply
#8
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.
Reply
#9
Got it by trying:
print(poiinfo[1]["waypointID"])
Thanks for your help, guys!
Reply


Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020