Re: [virtualcommons-svn] Sanitation experiment
Status: Beta
Brought to you by:
alllee
From: Allen L. <All...@as...> - 2011-06-15 22:07:17
|
I think this should work as a more efficient version of pollutify (doesn't scan the entire length of the string and uses list insertion instead of string concatenation). I've stubbed out the group round data value and parameter stuff so it can be tested independently. def pollutify(resource_string, pollution_amount, pollution_symbol, group=None): resource_index = xrange(1,(len(resource_string) + 1)) pollution_locations = sorted(random.sample(resource_index, pollution_amount)) # pollution_parameter = Parameter.objects.get(name='sanitation.pollution') resource_string_list = list(resource_string) offset = len(pollution_symbol) for i, location in enumerate(pollution_locations): # generate a GroupRoundDataValue for this location (this can be shortened) # grdv = GroupRoundDataValue.objects.create(group=group, round_data=group.current_round_data, parameter=pollution_parameter, value=location) # logger.debug("grdv is %s", grdv) # FIXME: since we're inserting more than one character we need to be careful not to insert into a location where # we appended text.. resource_string_list.insert(location + (i * offset), pollution_symbol) return ''.join(resource_string_list) On Wed, Jun 15, 2011 at 2:50 PM, Allen Lee <All...@as...> wrote: > Oh wait, nevermind about the inserting strings into other strings. > The solution I had in mind (iterating over the locations and inserting > them into the string) has that problem, but yours doesn't. > > On Wed, Jun 15, 2011 at 2:42 PM, Allen Lee <All...@as...> wrote: >> We can discuss the custom template tag and other issues the next time >> you come in - are you available on Friday? A few comments: >> >> Is your filename really called 'initial_jason.py'? That won't work >> the way you want it to, it needs to be named 'initial_data.json'. >> You're really close with the pollution symbol parameter, let me try to >> fully explain how this should work. Since we want the pollution >> symbols to be a group parameter we need to give a scope of "group", >> not "participant". For example, in >> forestry/fixtures/initial_data.json >> >> { >> "fields": { >> "name":"resource_level", >> "experiment_metadata": 1, >> "creator": 1, >> "type": "int", >> "date_created": "2011-01-01 15:13:07", >> "scope": "group" >> }, >> "model": "core.parameter", >> "pk": 2 >> }, >> >> You'll need at least two Parameters, the first is the Group parameter >> for pollution (and I'd name it "pollution" instead of "symbol") and >> the second is a Participant parameter (i.e., "scope": "participant"). >> You use these Parameters to construct and save GroupRoundDataValues >> and ParticipantDataValues whose data values are the location of a unit >> of pollution and the id of the pollution removed by a given >> participant participant, respectively. >> >> Also, your pollutify function needs some changes but we can discuss >> that in person. One of the problems is that since you're inserting >> strings longer than 1 character ("<a href=...></a>"), you have a >> chance of inserting that very string into one of the previously >> inserted "<a href=...></a>" strings, which would be bad. So you could >> get something like "<a href='<a href=''>@</a>'>@</a>" >> >> On Wed, Jun 15, 2011 at 5:55 AM, Marcel Hurtado >> <mar...@gm...> wrote: >>> The filter worked, thank you. I am not sure what you mean by custom template >>> tag/ filter can you explain to me why you would do that instead of using a >>> method? >>> Here is what I have so far: >>> views.py: >>> from vcweb.sanitation.models import pollutify_randomly >>> ... >>> resource_string = "Sanitation is vital for ... by more than 40 percent." >>> pollution_amount = random.randint(1,200) >>> symbol_url = str("<a href=''>" + symbol + "</a>") >>> game_state = pollutify_randomly(resource_string, pollution_amount, >>> symbol_url) >>> models.py >>> >>> import random >>> ... >>> def pollutify_randomly(resource_string, pollution_amount, symbol_url): >>> resource_index = range(1,(len(resource_string) + 1)) >>> pollution_location = random.sample(resource_index, pollution_amount) >>> game_state = "" >>> for i, char in enumerate(resource_string): >>> if i in pollution_location: >>> game_state = game_state + symbol_url >>> game_state = game_state + char >>> return game_state >>> Also, I have been looking at a way to track the symbols like you suggested. >>> This is what I have so far: >>> initial_jason.py >>> { >>> "fields": { >>> "name":"symbol", >>> "experiment_metadata": 1, >>> "creator": 1, >>> "type": "int", >>> "active": True, >>> "date_created": "2011-01-01 15:13:03", >>> "date_inactive": "2011-01-01 15:13:03", >>> "scope": "participant" >>> }, >>> "model": "core.parameter", >>> "pk": 1 >>> }, >>> I'm also attempting to create the "current_location" >>> { >>> "fields": { >>> "name":"", >>> "current_location":"consent" >>> }, >>> "model": "core.participantexperimentrelationship", >>> "pk": 1 >>> }, >>> >>> >>> >>> On Tue, Jun 14, 2011 at 11:04 AM, Allen Lee <All...@as...> wrote: >>>> >>>> Ah ok. That's much more useful information. You should probably also >>>> add href='#' to your a tags; some browsers don't like it when the href >>>> is blank. >>>> >>>> Next, there's two ways to get around this - the first is a quick & >>>> easy solution and the second is probably what we would go with >>>> longer-term. >>>> >>>> 1. Turn off autoescaping via {%autoescape off %} ... {% endautoescape >>>> %} or via the safe filter, e.g., {{game_state|safe}} (Try the safe >>>> filter first, it's more concise). >>>> >>>> 2. I think we'll eventually want to create a custom template tag / >>>> filter (pollutify?) that takes two parameters - a string resource >>>> ("Sanitation is blah blah blah ...") and a list of active pollution >>>> (basically character positions) and returns a new string with hrefs >>>> inserted into the text at each position specified by the active >>>> pollution list. I can show you how to do this the next time you come >>>> in. >>>> >>>> On Tue, Jun 14, 2011 at 2:07 AM, Marcel Hurtado >>>> <mar...@gm...> wrote: >>>> > Thank you. >>>> > Here's what I meant. >>>> > The sanitation text is in views.py at the moment saved to a variable >>>> > called >>>> > game_state as a global: >>>> > game_state = "Sanitation is .... by more than 40 percent." >>>> > The symbols are inserted as urls with html tags into that text, like >>>> > this: >>>> > game_state = "Sanitation <a href=''>@</a>is .... by more than <a >>>> > href=''>@</a>40 percent." >>>> > The method for "play" looks like this, still using the globals. >>>> > def play(request, experiment): >>>> > treatment = "In-group" >>>> > logger.debug("handling play") >>>> > return render_to_response('sanitation/play.html', globals(), >>>> > context_instance=RequestContext(request)) >>>> > When play.html loads the sanitation text is shown on the page like this: >>>> > "Sanitation <a href=''>@</a>is .... by more than <a href=''>@</a>40 >>>> > percent." >>>> > but it should be like this: >>>> > "Sanitation @is .... by more than @40 percent." >>>> > >>>> > >>>> > On Mon, Jun 13, 2011 at 10:05 PM, Allen Lee <All...@as...> wrote: >>>> >> >>>> >> current_location should probably be an extra column on >>>> >> ParticipantExperimentRelationship in core, but you'd get & set it in >>>> >> your sanitation game code. I can push that out soon, but am working >>>> >> on some other bugs first. Not sure what you mean by passing strings >>>> >> from views to templates not rendering as html, can you clarify or show >>>> >> code examples that aren't working? >>>> >> >>>> >> On Mon, Jun 13, 2011 at 6:50 PM, Marcel Hurtado >>>> >> <mar...@gm...> wrote: >>>> >> > Allen, >>>> >> > I have some questions. Does the "current_location" variable get >>>> >> > initiallized >>>> >> > in the sanitation directory, or in the core? >>>> >> > Also, when I pass a string from views to template it does not render >>>> >> > it >>>> >> > as >>>> >> > html. That is to say, the string for the resource passes with no >>>> >> > problem >>>> >> > but >>>> >> > the symbols which should be links show up as <a href=''>@</a>. >>>> >> > Marcel >>>> >> >>>> >> >>>> >> >>>> >> -- >>>> >> Allen Lee >>>> >> Center for the Study of Institutional Diversity [http://csid.asu.edu] >>>> >> School of Human Evolution and Social Change [http://shesc.asu.edu] >>>> >> College of Liberal Arts and Sciences >>>> >> Arizona State University | P.O. Box 872402 | Tempe, Arizona 85287-2402 >>>> >> 480.727.0401 | Fax: 480.965.7671 | e-mail: all...@as... >>>> > >>>> > >>>> >>>> >>>> >>>> -- >>>> Allen Lee >>>> Center for the Study of Institutional Diversity [http://csid.asu.edu] >>>> School of Human Evolution and Social Change [http://shesc.asu.edu] >>>> College of Liberal Arts and Sciences >>>> Arizona State University | P.O. Box 872402 | Tempe, Arizona 85287-2402 >>>> 480.727.0401 | Fax: 480.965.7671 | e-mail: all...@as... >>> >>> >> >> >> >> -- >> Allen Lee >> Center for the Study of Institutional Diversity [http://csid.asu.edu] >> School of Human Evolution and Social Change [http://shesc.asu.edu] >> College of Liberal Arts and Sciences >> Arizona State University | P.O. Box 872402 | Tempe, Arizona 85287-2402 >> 480.727.0401 | Fax: 480.965.7671 | e-mail: all...@as... >> > > > > -- > Allen Lee > Center for the Study of Institutional Diversity [http://csid.asu.edu] > School of Human Evolution and Social Change [http://shesc.asu.edu] > College of Liberal Arts and Sciences > Arizona State University | P.O. Box 872402 | Tempe, Arizona 85287-2402 > 480.727.0401 | Fax: 480.965.7671 | e-mail: all...@as... > -- Allen Lee Center for the Study of Institutional Diversity [http://csid.asu.edu] School of Human Evolution and Social Change [http://shesc.asu.edu] College of Liberal Arts and Sciences Arizona State University | P.O. Box 872402 | Tempe, Arizona 85287-2402 480.727.0401 | Fax: 480.965.7671 | e-mail: all...@as... |