Python Forum

Full Version: typescript -> python syntax
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
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!
Use a class, or named tuple to represent your domain objects.
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 ...
Oh, you just want a map. Ok, so what's not right about what you have?
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?
poiinfo = [
    {
    'phone': '',
    'waypointID': 0,
    'lang': 1,
    'src': 'HERE',
    },
    {
    'phone': '',
    'waypointID': 1,
    'lang': 1,
    'src': 'HERE',
    }
]
I mean, you could have looked up how to do basic data structures in Python right?
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.
Got it by trying:
print(poiinfo[1]["waypointID"])
Thanks for your help, guys!