AttributeError: unicode object has no attribute pop

2024/10/5 15:04:21

I have this piece of python code in gae.

def post(self):cases=self.request.get('cases')while cases:logging.info("cases: %s " % cases)case=cases.pop()

Which produces this log.

INFO     2012-09-19 20:23:50,690 views.py:674] cases: [u'court1150'] 
ERROR    2012-09-19 20:23:50,690 webapp2.py:1553] 'unicode' object has no attribute 'pop'
Traceback (most recent call last):File "/Applications/GoogleAppEngineLauncher.app/Contents/Resources/GoogleAppEngine-default.bundle/Contents/Resources/google_appengine/lib/webapp2/webapp2.py", line 1536, in __call__rv = self.handle_exception(request, response, e)File "/Applications/GoogleAppEngineLauncher.app/Contents/Resources/GoogleAppEngine-default.bundle/Contents/Resources/google_appengine/lib/webapp2/webapp2.py", line 1530, in __call__rv = self.router.dispatch(request, response)File "/Applications/GoogleAppEngineLauncher.app/Contents/Resources/GoogleAppEngine-default.bundle/Contents/Resources/google_appengine/lib/webapp2/webapp2.py", line 1278, in default_dispatcherreturn route.handler_adapter(request, response)File "/Applications/GoogleAppEngineLauncher.app/Contents/Resources/GoogleAppEngine-default.bundle/Contents/Resources/google_appengine/lib/webapp2/webapp2.py", line 1102, in __call__return handler.dispatch()File "/Applications/GoogleAppEngineLauncher.app/Contents/Resources/GoogleAppEngine-default.bundle/Contents/Resources/google_appengine/lib/webapp2/webapp2.py", line 572, in dispatchreturn self.handle_exception(e, self.app.debug)File "/Applications/GoogleAppEngineLauncher.app/Contents/Resources/GoogleAppEngine-default.bundle/Contents/Resources/google_appengine/lib/webapp2/webapp2.py", line 570, in dispatchreturn method(*args, **kwargs)File "/Users/brian/googleapps/scheduler/views.py", line 675, in postcase=cases.pop()
AttributeError: 'unicode' object has no attribute 'pop'

On the other hand with this almost identical code in the interactive console.

cases = [u'court1150'] 
while cases:case=cases.pop()print case
print cases

I get no error and the following print out.

court1150
[]

Why am I getting a unicode error in the gae launcher and how can I fix it?

Answer

self.request.get('cases') does not return a list of values, it returns just one value. Try using get_all('cases') instead:

def post(self):cases=self.request.get_all('cases')while cases:logging.info("cases: %s " % cases)case=cases.pop()

There is no need to use a while loop, a for loop works much better:

def post(self):cases=self.request.get_all('cases')logging.info("cases: %s " % cases)for case in cases:# do something with case.
https://en.xdnf.cn/q/119063.html

Related Q&A

How to set platform when using pip download command

I want to download some pacakges(tensorflow, keras, imbalanced, xgboost, lightgbm, catboost) for centos 7.4 and python 3.7 on mac.How should i set platform name and ant other settings?I used below com…

How to split qr codes in separate images?

I have hundreds of pictures with qr codes (sometimes there are 0, 1, 2 or more qr codes on one page...but they are always in one line). I want to decode the qr codes from left to right. My idea is to s…

Python methods from csv

I am working on an assignment where I create "instances" of cities using rows in a .csv, then use these instances in methods to calculate distance and population change. Creating the instance…

Convert string numpy.ndarray to float numpy.ndarray

I have one problem. How can I convert:import numpy as npa = np.array([[0.1 0.2 0.3], [0.3 0.4 0.5], [0.5 0.6 0.7]])To:b = np.array([[0.1,0.2,0.3], [0.3,0.4,0.5], [0.5,0.6,0.7]])

function at the end of code- why do i have to place it?

Im just starting my adventure with Python. Unfortunately, I cant figure out why, at the end of my code, I have to add myfunc(). Without it my code doesnt display. How is it if I use more than one def…

Would like to write code in idle that I have in Jupyter notebook. It is using Timeit

code big_array= np.random.rand(1000000) %timeit sum(big_array) this code above is done in jupyter notebook how do I use this code in idle

How to pass python variable to shell command [duplicate]

This question already has answers here:How to escape os.system() calls?(10 answers)Closed 2 years ago.Is their any way that i can pass variable value of python to my unix command Note : var is a pytho…

A function inside a function Python

This is a problem asked before but I cant really understand the other explain of this kind of problem so Im here to re-write it in more details. While studying I have encountered this kind of code tha…

Accessing a parameter passed to one function in another function

I have two functions:def f1(p1=raw_input("enter data")):...do somethingdef f2(p2=raw_input("enter data")):...do something elsep1 and p2 are the same data, so I want to avoid asking …

SOLVE: AttributeError: ImmutableDenseNDimArray object has no attribute as_independent

I am quite new to python and I have been trying to plot this in a few different ways. If I try using np.vectorize, it crashes. so i wrote this code, which is giving me the error in the title: import ma…