-2

I am working with someone else's code and they have defined this function:

def QExpr (name):
    length = len(name)
    code = name [length-10:]
    exp='"TOWN_ID"='+ str(int(code[1:4]))
    return exp

I don't quite understand what this function does. I just know that it is used to calculate a value using a table in a database. I appreciate any help!

Kawi
  • 19
  • 5
  • 5
    Now is a great opportunity to learn to use your debugger. You can run this function, step through each line, and inspect the values of variables to see exactly what it is doing and how it works. – 0x5453 Mar 08 '21 at 15:36
  • [How to debug small programs.](//ericlippert.com/2014/03/05/how-to-debug-small-programs/) | [What is a debugger and how can it help me diagnose problems?](//stackoverflow.com/q/25385173/843953) – Pranav Hosangadi Mar 08 '21 at 15:37
  • Which bit, exactly, are you having trouble with? – quamrana Mar 08 '21 at 15:37
  • This code is pretty straightforward; the only tricky thing is the [list slicing syntax](https://stackoverflow.com/q/509211/3282436). – 0x5453 Mar 08 '21 at 15:37
  • Thanks. I cannot run it since this is actually inside an ArcGIS model that I am trying to understand. A bunch of previous steps cannot be ran. So I need to understand this function in order to squeeze what it's trying to do inside some other lines. – Kawi Mar 08 '21 at 15:39
  • I am very new to python - trying to understand what exp='"TOWN_ID"='+ str(int(code[1:4])) actually does. – Kawi Mar 08 '21 at 15:40
  • Everyone, thanks a lot. Can anyone tell me in a few sentences what this does? Again - I don't really know python – Kawi Mar 08 '21 at 15:43
  • Find out what each of those functions do. 0x5453 has already given you a link about list slicing syntax. These things are easy to find with a quick google search, and you'll probably find lots of answers already on Stack Overflow. [Asking on Stack Overflow is not a substitute for doing your own research](//meta.stackoverflow.com/a/261593/843953), and [explain my code to me](https://meta.stackoverflow.com/q/271468/843953) is too broad for SO. – Pranav Hosangadi Mar 08 '21 at 15:59

1 Answers1

-1

It seems like it does the same as this code:

def Qexpr(name):
    return '"TOWN_ID"=' + str(int(name[-9:-5]))

for example "TOWN_ID"=3245