From: Jerry Rocteur on
> -----BEGIN PGP SIGNED MESSAGE-----
> Hash: SHA1
>
> On 06/22/2010 01:32 PM, Jerry Rocteur wrote:
>> My input is NOT CSV, I used this format to try and make the question shorter. Although I could create a CSV file,
>> I'd
>> like to learn how to code a class to work the way I described in the question.
>
> Sorry for misunderstanding. Can you maybe give one example that's not
> CSV? Both your demonstrated files actually are CSV like. Can you be more
> specific on what actually changes constantly. (Fields given in the
> files, or the users in the files, or...)
> Can you tell us, why you want to use classes if the dict approach works
> great?

As part of learning Python, I'm also learning OOP! That is why I want to know if this is doable using classes.

The input is not important, I end up with the dictionary as described in the question and as I asked in the question,
I'd like to access the dictionary as a class and I don't know how or if it is possible.


Jerry

From: James Mills on
On Tue, Jun 22, 2010 at 9:56 PM, Jerry Rocteur <macosx(a)rocteur.cc> wrote:
> As part of learning Python, I'm also learning OOP! That is why I want to know if this is doable using classes.
>
> The input is not important, I end up with the dictionary as described in the question and as I asked in the question,
> I'd like to access the dictionary as a class and I don't know how or if it is possible.

I suggest you start playing around with python classes and objects.

It's entirely possible you can create your own class that
represents your data and store this in some fashion.

You could also subclass (you'll learn about this) the dict class
creating your own customized dict (if you will).

The former approach may be better suited however instead of
diving into things you may not yet come to fully understand
until you really learn the inner workings of python :)

cheers
James

--
--
-- "Problems are solved by method"
From: Bruno Desthuilliers on
Jerry Rocteur a �crit :
(snip)
> As part of learning Python, I'm also learning OOP! That is why I want to know if this is doable using classes.
>
> The input is not important, I end up with the dictionary as described in the question and as I asked in the question,
> I'd like to access the dictionary as a class

I assume you mean "as an object" (or "as an instance of a class" - which
is exactly the same thing). If you don't understand what this means,
then you should first learn the difference between a class and an
instance !-)

> and I don't know how or if it is possible.

Well, Python being 100% object, your dict is already an object (an
instance of the builtin 'dict' class).


From: Jean-Michel Pichavant on
Jerry Rocteur wrote:
>> -----BEGIN PGP SIGNED MESSAGE-----
>> Hash: SHA1
>>
>> On 06/22/2010 01:32 PM, Jerry Rocteur wrote:
>>
>>> My input is NOT CSV, I used this format to try and make the question shorter. Although I could create a CSV file,
>>> I'd
>>> like to learn how to code a class to work the way I described in the question.
>>>
>> Sorry for misunderstanding. Can you maybe give one example that's not
>> CSV? Both your demonstrated files actually are CSV like. Can you be more
>> specific on what actually changes constantly. (Fields given in the
>> files, or the users in the files, or...)
>> Can you tell us, why you want to use classes if the dict approach works
>> great?
>>
>
> As part of learning Python, I'm also learning OOP! That is why I want to know if this is doable using classes.
>
> The input is not important, I end up with the dictionary as described in the question and as I asked in the question,
> I'd like to access the dictionary as a class and I don't know how or if it is possible.
>
>
> Jerry
>
>
Dictionary is already a class in python, everything is an object
actually (even class are objects).

A python OOP approach to your problem would be to subclass the builtin
'dict' class and override / add the methods.

Here is an example how you can for your User dictionary to accept only
some types of values.

class User(object):
def __init__(self, user, secnum, name):
self.user = user
self.secnum = secnum
self.name = name

def __str__(self):
return "%(name)s <%(user)s> -- secnum :%(secnum)d" % self.__dict__

def __repr__(self):
return self.__str__()

class Users(dict):
def __init__(self):
dict.__init__(self) # call the baseclass constructor

def __setitem__(self, user, value):
if isinstance(value, User):
dict.__setitem__(self, user, value)
else:
try:
dict.__setitem__(self, user, User(user, value[0], value[1]))
except KeyError:
raise ValueError ("Invalid user format, it shoudl be
either a User object or a tuple (secnum, name)")

users = Users()

users['jro'] = (1325,'john') # the overriden __setitem__ method wil
ltake care of creating the User object for you.
users['dma'] = User('dma', 654968, 'dominic')

for user in users:
print users[user]

# Users objects inherit all dict method, values() for instance
for user in users.values():
print user



For more information: http://docs.python.org/reference/datamodel.html
Read carefully the 3.4.6 section (Emulating container type)


JM
From: Andreas Waldenburger on
On Tue, 22 Jun 2010 13:56:43 +0200 (CEST) "Jerry Rocteur"
<macosx(a)rocteur.cc> wrote:

> As part of learning Python, I'm also learning OOP! That is why I want
> to know if this is doable using classes.

Everything[1] is doable using classes. The question is: Do you *need* to
do it with classes? If your problem is best described as "mappings from
keys to values", then you have already implemented a fine solution
with dicts.

Think of "objects" as "things with behavior". Basically, if you have
many top-level data items (such as dicts, lists, whatever), and many
functions operating on these, grouping these data items with the
appropriate functions within classes might clean up your code
conceptually (It might! I didn't say it will!).

If you can categorize these data/function groups into a hierarchical
structure of ever more specialized functionality, then classes and
inheritance are pretty much your friends by default.

So the takeaway is this: Will you (and possibly others) be able to
understand my code better by organizing it in this-or-that structure?
If so, use this-or-that structure, if not, then don't.


And to even more evade answering your specific question: If you have
the time, go and rewrite your code with classes. That'll teach you more
than theorizing about *maybe doing* it here.

/W

[1] For sufficiently small values of "everything".

--
INVALID? DE!