473,396 Members | 2,147 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

Dictonary persistance weirdness

Hi, I've posted this to mo********@modpython.org and got no answers, so
i'm reposting it here, I really hope someone can help me out.

------

Hi,
I'm building my first application with mod_python. I am using Ian
Bicking's SQLObject, validators and HTMLfill among other tools.
I built a custom handler that parses the url and loads an external
module, passing it a request object and eventually, an ID integer that
is extracted from the URI.

Example:
http://site.com/race.py/4/signup will trigger "import race;
race.signup(4,req)" and that function will provide a form in order for
people to sign themselves up for race 4.

Form handling and data parsing (for POST or GET data) is done in
separate modules that are imported when needed, so race.py actually
imports formgen.py and formgen.py imports VarsWrapper (from wrappers.py)
in order to parse data for his form and tell whether he needs to print
an error message, or the input is valid.

The process works fine up to races.py: it gets the POSTed data fine, but
then if I try to pass the variables received to the form handler, the
handler will only receive them correctly on the first call to it after a
server restart. For later calls, the form handler sticks to the values
received on his first request.

Code snippets:

-- race.py -------------------------
def signup(id,req):
from formgen import FormInscripcion

v = VarsWrapper(req)
c = v.get_vars()

f = FormInscripcion(id)

tpl.locals.contenido = f.handle(c)
return tpl.get_html()
-- formgen.py -------------------------

class FormInscripcion:
(...)
def handle(self,vars):
# check for sent data
if ('id_regata',str(self.id_regata)) in vars.items():
return self._post(vars) # triggers a data handler
else:
return self._form() # shows the clean form
(...)
-- wrappers.py -------------------------
class VarsWrapper:
def __init__(self,req):
self.fs = util.FieldStorage(req)

def get_vars():
d = {}
for each in self.fs.list:
self.d[x.name] = x.value
return d
So again, the problem arises when calling FormInscripcion.handle() for
the 2nd time or more: it will stick to the data received on the 1st
request passed to it. If i print the variable dictionary (c in race.py,
vars in formgen.py) after the form, c will change with new POST data, as
vars will stay constant after the 1st request.
The weird thing is that i'm passing data from pyhandler.py to race.py in
the same way that i'm passing it from race.py to formgen.py, and it
refuses to work correctly in the latter.
I have already tried pretty much every single possible cause for this
oddity with no consistent results, so i'm pretty confident that there's
something i'm missing that's not inherent to my app's logic, but to my
app's login combined with some mod_python specific complication.
In short: i'm going nuts!
Sorry for all the writing, but I really hope somebody can help me.
Thanks in advance,

Gonzalo Sainz-Trápaga (GomoX)
Jul 18 '05 #1
3 1229
On Sun, 10 Apr 2005 19:07:23 -0300, Gonzalo Sainz-Trápaga (GomoX)
<go*********@datafull.com> wrote:
-- wrappers.py -------------------------
class VarsWrapper:
def __init__(self,req):
self.fs = util.FieldStorage(req)

def get_vars():
d = {}
for each in self.fs.list:
self.d[x.name] = x.value
return d


Are you sure that is the code that you are executing? What is "x"?
What is "self.d"? "each" is not used. Even if "x" and "self.d" do
exist, the for loop will do nothing, and the method will return an
empty dictionary.

You weren't trying to "optimise" the references to self.d inside the
loop, were you?

Making the charitable assumptions that d and self.d are the same thing
and so are "x" and "each":

Assuming self.d does exist, why does it exist? I.e for what reason do
you want the data to persist? Could its persistence be the cause of
your problem? Are you sure you are not accidentally always referring
to the same VarsWrapper instance?

Is that all that VarsWrapper contains? Seems to me like an unnecessary
and possibly confusing convolution. If you "own" util.FieldStorage,
then giving it an as_dictionary() method might be a good idea.
Otherwise a simple wrapper *function* that replaces your two-step
__init__ and get_vars would be a not unreasonable idea; there's no law
that says that everything must be a class.

Are you sure you don't have a dict as a default arg somewhere, like in
def handle(self, vars={}) ??

HTH,

John

Jul 18 '05 #2
On Sun, 10 Apr 2005 19:07:23 -0300, Gonzalo Sainz-Trápaga (GomoX)
<go*********@datafull.com> wrote:

def get_vars():
d = {}
for each in self.fs.list:
self.d[x.name] = x.value
return d


.... and didn't you mean

def get_vars(self):

?
Jul 18 '05 #3
Hi John,
Thanks for your reply, I was feeling pretty desperate already with no one
talking to me :(

On Mon, 11 Apr 2005 11:46:20 +1000, John Machin wrote:
Are you sure that is the code that you are executing? What is "x"? What is
"self.d"? "each" is not used. Even if "x" and "self.d" do exist, the for
loop will do nothing, and the method will return an empty dictionary.
You are right, I'm sorry, my code has comments and variable names in
Spanish, so i made a translation of it. Obviously i was more stressed out
than i can remember and i came up with that nasty mess. The actual code is:
def get_vars(self):
if self.d == {}:
for x in self.fs.list:
self.d[x.name] = x.value
return self.d
Assuming self.d does exist, why does it exist? I.e for what reason do you
want the data to persist? Could its persistence be the cause of your
problem? Are you sure you are not accidentally always referring to the
same VarsWrapper instance?
self.d is initialized as "{}" on __init__(), and i'm using self.d this way
so that the parsing is done only once, upon get_vars() first call. After
this, self.d will be different than {} and i will just get the values.

Referring to the same VarsWrapper instance (or the same instance of some
other object) sounds like a plausible cause for my problem, since it works
the first time (upon the first instantiation) and keeps the same values
later. The problem is, I really don't see any way this could be happening.
I've checked the corresponding parts of the code a few hundred times rigth
now and I've made sure that every object is new and not reused. This is,
of course, unless there's some unexpected behaviour in mod_python (thing
like variables staying "alive" between executions of a script, or
something like this).
Is that all that VarsWrapper contains? Seems to me like an unnecessary and
possibly confusing convolution. If you "own" util.FieldStorage, then
giving it an as_dictionary() method might be a good idea. Otherwise a
simple wrapper *function* that replaces your two-step __init__ and
get_vars would be a not unreasonable idea; there's no law that says that
everything must be a class.
I guess you are right on this, actually the util.FieldStorage
implementation just replaced an older util.parse_qs() one that provided
with separated get_getvars() and get_postvars() methods, aside from the
get_vars() one. I went back to util.FieldStorage when I found this issue,
in order to reduce the possible origins for this behaviour.
Are you sure you don't have a dict as a default arg somewhere, like
in def handle(self, vars={}) ??


I'm positive about this: my code works fine right after a restart of the
server, but keeps seeing the same data after the first request, so the
bug is about unwanted persistence of the first request data, not about
defaults.

Anyway, I will try replacing VarsWrapper with a simple function in order
to avoid yet another possible bug cause.

Thanks again for your help, and if something else comes to your mind I
would really appreciate to hear about it, since i've pretty much ran out
of ideas by now. Anyhow, I will work on it some more tomorrow afternoon,
I'm taking a well deserved break now :D

Gonzalo
Jul 18 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Leo | last post by:
Hi, I'm back to ASP for a short while and I was wondering how I could save the entire Application/Session-state to a file or database and how to read it back afterwards. I'd like to preserve...
0
by: Michael.McD | last post by:
Is there any consensus on the way to go when implementing object persistance to a dB? For example MHibernate v. DataObjects (x-tensive). Cheers, Michael McD
15
by: Corne' Cornelius | last post by:
Hi, I'm experiencing some weirdness in a program, when subtracting 2 (double)'s which should result in 0, but instead it returns -1.11022e-16. It looks to me that changing the double x_step...
1
by: (Pete Cresswell) | last post by:
TabControl on the right side of a form with two tabs: Tab #1 contains two subforms that show some dynamic query results. Tab #2 contains a ListView that gets dynamically populated as the user...
0
by: Bruce B | last post by:
Hi group, I'm experiencing some extreme weirdness over the last week with IIS related to it's Mappings and how .NET is behaving. I can't explain the behavior as it seems very random. Our...
1
by: VB Programmer | last post by:
My development machine has been working perfectly, writing ASP.NET apps, etc... I just went to open a project and it said: "Visual Studio .NET has detected that the specified web server is not...
2
by: Kiran | last post by:
Hi, This is something weird I noticed recently in ASP.Net. I have created typed dataset and I have filled the data. when a postback happens, the data of the dataset is lost. Is there a...
5
by: David Thielen | last post by:
Hi; I am creating png files in my ASP .NET app. When I am running under Windows 2003/IIS 6, the file is not given the security permissions it should have. It does not have any permission for...
45
by: bigdadro | last post by:
I've created a new class using prototype.js. After I make the ajax.request all references to this.myClassMethodorVariable are lost. Does the ajax method blow out the object persistance? I'm fairly...
2
by: JYA | last post by:
Hi. I was writing an xmltv parser using python when I faced some weirdness that I couldn't explain. What I'm doing, is read an xml file, create another dom object and copy the element from...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.