Python Access Nested Functions Sql