librelist archives

« back to archive

why does manipulation on one route function affects other

why does manipulation on one route function affects other

From:
Chengi Liu
Date:
2013-02-18 @ 21:54
Hi,
 The following is my code snippet:

@app.route("/tabledata")
def tabledata():
   f = open(os.path.join(os.path.dirname(__file__), graph_bip),"r")
   data = []
   for line in f:
       tokens = line.split()
      * data.append({"doc_id":tokens[0], "word_id":tokens[1]})*
   jsn = json.dumps(data)
   return jsn

@app.route("/new_tabledata")
def tabledata():
   f = open(os.path.join(os.path.dirname(__file__), graph_bip),"r")
   data = [["car","year"]]
   for line in f:
       tokens = line.split()
       *data.append([tokens[0], tokens[1]])*
   jsn = json.dumps(data)
   return jsn


But now /new_tabledata and /tabledata
both have same output ??
[["car", "year"], ["Nissan", "2009"], ["Nissan", "2012"], ["Nissan",
"2013"]]
Why??

Re: [flask] why does manipulation on one route function affects other

From:
Yukihiro Okada
Date:
2013-02-19 @ 01:58
Hi

Funtion " tabledata" is defined twice in your code.
rename it to either function.

Yukihiro

(13/02/19 6:54), Chengi Liu wrote:
> Hi,
>  The following is my code snippet:
>
> @app.route("/tabledata")
> def tabledata():
>    f = open(os.path.join(os.path.dirname(__file__), graph_bip),"r")
>    data = []
>    for line in f:
>        tokens = line.split()
> * data.append({"doc_id":tokens[0], "word_id":tokens[1]})*
>    jsn = json.dumps(data)
>    return jsn
>
> @app.route("/new_tabledata")
> def tabledata():
>    f = open(os.path.join(os.path.dirname(__file__), graph_bip),"r")
>    data = [["car","year"]]
>    for line in f:
>        tokens = line.split()
> *data.append([tokens[0], tokens[1]])*
>    jsn = json.dumps(data)
>    return jsn
>
>
> But now /new_tabledata and /tabledata
> both have same output ??
> [["car", "year"], ["Nissan", "2009"], ["Nissan", "2012"], ["Nissan", 
> "2013"]]
> Why??
>

Re: [flask] why does manipulation on one route function affects other

From:
Ned Jackson Lovely
Date:
2013-02-19 @ 01:53
The first function definition is being overwritten by the second
function definition because they have the same name.

For example

=====
def foo():
    print "first function"

def foo():
    print "second function"

foo()
=====

...will print "second function".

Rename one of your tabledata functions, and it should work fine.

Regards,

Ned

On Mon, Feb 18, 2013 at 4:54 PM, Chengi Liu <chengi.liu.86@gmail.com> wrote:
> Hi,
>  The following is my code snippet:
>
> @app.route("/tabledata")
> def tabledata():
>    f = open(os.path.join(os.path.dirname(__file__), graph_bip),"r")
>    data = []
>    for line in f:
>        tokens = line.split()
>        data.append({"doc_id":tokens[0], "word_id":tokens[1]})
>    jsn = json.dumps(data)
>    return jsn
>
> @app.route("/new_tabledata")
> def tabledata():
>    f = open(os.path.join(os.path.dirname(__file__), graph_bip),"r")
>    data = [["car","year"]]
>    for line in f:
>        tokens = line.split()
>        data.append([tokens[0], tokens[1]])
>    jsn = json.dumps(data)
>    return jsn
>
>
> But now /new_tabledata and /tabledata
> both have same output ??
> [["car", "year"], ["Nissan", "2009"], ["Nissan", "2012"], ["Nissan",
> "2013"]]
> Why??
>



-- 
njl

Re: [flask] why does manipulation on one route function affects other

From:
Joe Esposito
Date:
2013-02-19 @ 02:12
I used to do this by mistake every now and then. Sublime Text lets you know
when you overwrite a function. I recommend checking it out, it's prevented
quite a few bad bugs like this for me.

On Monday, February 18, 2013, Ned Jackson Lovely wrote:

> The first function definition is being overwritten by the second
> function definition because they have the same name.
>
> For example
>
> =====
> def foo():
>     print "first function"
>
> def foo():
>     print "second function"
>
> foo()
> =====
>
> ...will print "second function".
>
> Rename one of your tabledata functions, and it should work fine.
>
> Regards,
>
> Ned
>
> On Mon, Feb 18, 2013 at 4:54 PM, Chengi Liu 
<chengi.liu.86@gmail.com<javascript:;>>
> wrote:
> > Hi,
> >  The following is my code snippet:
> >
> > @app.route("/tabledata")
> > def tabledata():
> >    f = open(os.path.join(os.path.dirname(__file__), graph_bip),"r")
> >    data = []
> >    for line in f:
> >        tokens = line.split()
> >        data.append({"doc_id":tokens[0], "word_id":tokens[1]})
> >    jsn = json.dumps(data)
> >    return jsn
> >
> > @app.route("/new_tabledata")
> > def tabledata():
> >    f = open(os.path.join(os.path.dirname(__file__), graph_bip),"r")
> >    data = [["car","year"]]
> >    for line in f:
> >        tokens = line.split()
> >        data.append([tokens[0], tokens[1]])
> >    jsn = json.dumps(data)
> >    return jsn
> >
> >
> > But now /new_tabledata and /tabledata
> > both have same output ??
> > [["car", "year"], ["Nissan", "2009"], ["Nissan", "2012"], ["Nissan",
> > "2013"]]
> > Why??
> >
>
>
>
> --
> njl
>

Re: [flask] why does manipulation on one route function affects other

From:
Chengi Liu
Date:
2013-02-19 @ 02:50
Oh.. Thanks for pointing that out.
I feel so stupid.
Many thanks


On Mon, Feb 18, 2013 at 6:12 PM, Joe Esposito <espo58@gmail.com> wrote:

> I used to do this by mistake every now and then. Sublime Text lets
> you know when you overwrite a function. I recommend checking it out, it's
> prevented quite a few bad bugs like this for me.
>
>
> On Monday, February 18, 2013, Ned Jackson Lovely wrote:
>
>> The first function definition is being overwritten by the second
>> function definition because they have the same name.
>>
>> For example
>>
>> =====
>> def foo():
>>     print "first function"
>>
>> def foo():
>>     print "second function"
>>
>> foo()
>> =====
>>
>> ...will print "second function".
>>
>> Rename one of your tabledata functions, and it should work fine.
>>
>> Regards,
>>
>> Ned
>>
>> On Mon, Feb 18, 2013 at 4:54 PM, Chengi Liu <chengi.liu.86@gmail.com>
>> wrote:
>> > Hi,
>> >  The following is my code snippet:
>> >
>> > @app.route("/tabledata")
>> > def tabledata():
>> >    f = open(os.path.join(os.path.dirname(__file__), graph_bip),"r")
>> >    data = []
>> >    for line in f:
>> >        tokens = line.split()
>> >        data.append({"doc_id":tokens[0], "word_id":tokens[1]})
>> >    jsn = json.dumps(data)
>> >    return jsn
>> >
>> > @app.route("/new_tabledata")
>> > def tabledata():
>> >    f = open(os.path.join(os.path.dirname(__file__), graph_bip),"r")
>> >    data = [["car","year"]]
>> >    for line in f:
>> >        tokens = line.split()
>> >        data.append([tokens[0], tokens[1]])
>> >    jsn = json.dumps(data)
>> >    return jsn
>> >
>> >
>> > But now /new_tabledata and /tabledata
>> > both have same output ??
>> > [["car", "year"], ["Nissan", "2009"], ["Nissan", "2012"], ["Nissan",
>> > "2013"]]
>> > Why??
>> >
>>
>>
>>
>> --
>> njl
>>
>