Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Does Python have a toString() equivalent, and can I convert a class to String?

I'm writing a ToDo list app to help myself get started with Python. The app is running on GAE and I'm storing todo items in the Data Store. I want to display everyone's items to them, and them alone. The problem is that the app currently displays all items to all users, so I can see what you write, and you see what I write. I thought casting my todo.author object to a string and seeing if it matches the user's name would be a good start, but I can't figure out how to do that.

This is what I have in my main.py

...  user = users.get_current_user()  if user:     nickname = user.nickname()     todos = Todo.all()     template_values = {'nickname':nickname, 'todos':todos} ...  def post(self):      todo = Todo()     todo.author = users.get_current_user()     todo.item = self.request.get("item")     todo.completed = False      todo.put()           self.redirect('/') 

In my index.html I had this originally:

<input type="text" name="item" class="form-prop" placeholder="What needs to be done?" required/> ...  <ul> {% for todo in todos %}   <input type="checkbox"> {{todo.item}} <hr /> {% endfor %} </ul> 

but I'd like to display items only to the user who created them. I thought of trying

{% for todo in todos %}     {% ifequal todo.author nickname %}   <input type="checkbox"> {{todo.item}} <hr />     {% endifequal %} {% endfor %} 

to no avail. The list turns up blank. I assumed it is because todo.author is not a string. Can I read the value out as a string, or can I cast the object to String?

Thanks!

Edit: Here is my Todo class

class Todo(db.Model):     author = db.UserProperty()     item = db.StringProperty()     completed = db.BooleanProperty()     date = db.DateTimeProperty(auto_now_add=True) 

Will changing my author to a StringProperty effect anything negatively? Maybe I can forgo casting altogether.

like image 372
Amru E. Avatar asked May 27 '13 07:05

Amru E.


People also ask

Does Python have To_string?

Python does not have a tostring method to convert a variable to a string. Instead, Python has the str() function which will change an object into a string. str() converts any object into a string.

Does string class have toString method?

String is a type of object, therefore, it also need a method toString() for implementing method from its base class.

What is the difference between toString and string?

Both these methods are used to convert a string. But yes, there is a difference between them and the main difference is that Convert. Tostring() function handles the NULL while the . ToString() method does not and it throws a NULL reference exception.

How do you convert an object to a string in Python?

Python is all about objects thus the objects can be directly converted into strings using methods like str() and repr(). Str() method is used for the conversion of all built-in objects into strings. Similarly, repr() method as part of object conversion method is also used to convert an object back to a string.


2 Answers

In python, the str() method is similar to the toString() method in other languages. It is called passing the object to convert to a string as a parameter. Internally it calls the __str__() method of the parameter object to get its string representation.

In this case, however, you are comparing a UserProperty author from the database, which is of type users.User with the nickname string. You will want to compare the nickname property of the author instead with todo.author.nickname in your template.

like image 123
Jeff Lockhart Avatar answered Oct 01 '22 03:10

Jeff Lockhart


In Python we can use the __str__() method.

We can override it in our class like this:

class User:      def __init__(self):         self.firstName = ''         self.lastName = ''         ...              def __str__(self):         return self.firstName + " " + self.lastName 

and when running

print(user) 

it will call the function __str__(self) and print the firstName and lastName

like image 25
Amirouche Zeggagh Avatar answered Oct 01 '22 03:10

Amirouche Zeggagh