[Tutor] Good approach regarding classes attributes

Juan Christian juan0christian at gmail.com
Sun Sep 7 19:07:48 CEST 2014


>
> > On Sun, Sep 7, 2014 at 5:04 AM, Peter Otten <__peter__ at web.de> wrote:
>
> > I would call it with ID only and them the API server would return me
> > all the info, and then I would set them. I didn't learn '@classmethod'
> > decoration yet, but I presume it would work as a 'get()', right? The
> thing
> > is, where 'user with id 43' is stored? You get it using 'from_id' but we
> > don't have any list in there the store users, I got confused in that
> part.
>
> Maybe it becomes clearer with a small change. Instead of the classmethod
> you
> could use a normal function:
>
> class User:
>     def __init__(self, id, personname, ...):
>         self.id = id
>         self.personname = personname
>         ...
>
> def fetch_user_from_server(id):
>     json_user = fetch data_from_server(id)
>     return User(id, json_user["personname"], ...)
>
> jim = fetch_user_from_server(42)
>

I'll definitely use the '@property' decoration. Thanks for the tip, so, a
different module to accommodate all the API requests and one for the
logic/code itself is a better approach, right?



> If you should later decide that you want to provide a way to allow entering
> new users you could use the User class for that, too:
>
> def create_new_user():
>     personname = input("Name: ") # in real code this would rather be a
>                                  # gui dialog or web page
>     ...
>     return User(None, personname, ...)
>
> new_guy = create_new_user()
> save_user_to_server(new_guy)
>
> You don't have to worry that the __init__() method tries to load data for
> an
> inexistent user.
>
> But even if you are sure you'll never do that it is still a good idea to
> keep concerns separate, if only to write independent unit tests for the
> User
> class and the server access.


Creating new users won't exist, my program is like a Home Broker using the
Steam API. This 'User' class will be mainly used to properly store the
users data, I'll automatically and recursively go trough my (or any person)
friend-list and fetch users info and inventory and properly use it in my
favor.

Here is an example of JSON response from the Steam API:

{
	"response": {
		"players": [
			{
				"steamid": "76561198067618735",
				"communityvisibilitystate": 3,
				"profilestate": 1,
				"personaname": "wildee14",
				"lastlogoff": 1410065399,
				"commentpermission": 1,
				"profileurl": "http://steamcommunity.com/id/wildee14/",
				"avatar": "http://media.steampowered.com/steamcommunity/public/images/avatars/da/da259bfaef7fe7c2521de78433977a6c006217c5.jpg",
				"avatarmedium":
"http://media.steampowered.com/steamcommunity/public/images/avatars/da/da259bfaef7fe7c2521de78433977a6c006217c5_medium.jpg",
				"avatarfull":
"http://media.steampowered.com/steamcommunity/public/images/avatars/da/da259bfaef7fe7c2521de78433977a6c006217c5_full.jpg",
				"personastate": 0,
				"realname": "wildee",
				"primaryclanid": "103582791429571843",
				"timecreated": 1342807007,
				"personastateflags": 0,
				"loccountrycode": "US"
			}
		]
		
	}
}
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/tutor/attachments/20140907/c99c0b64/attachment.html>


More information about the Tutor mailing list