Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #104193 > unrolled thread

Struggeling with collections

Started byFaling Dutchman <henriarends92@gmail.com>
First post2016-03-07 00:24 -0800
Last post2016-03-07 22:55 +0100
Articles 8 — 6 participants

Back to article view | Back to comp.lang.python


Contents

  Struggeling with collections Faling Dutchman <henriarends92@gmail.com> - 2016-03-07 00:24 -0800
    Re: Struggeling with collections Vincent Vande Vyvre <vincent.vande.vyvre@telenet.be> - 2016-03-07 09:39 +0100
      Re: Struggeling with collections Faling Dutchman <henriarends92@gmail.com> - 2016-03-07 00:58 -0800
    Re: Struggeling with collections Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2016-03-07 19:52 +1100
    Re: Struggeling with collections Gary Herron <gherron@digipen.edu> - 2016-03-07 01:01 -0800
    Re: Struggeling with collections Jussi Piitulainen <jussi.piitulainen@helsinki.fi> - 2016-03-07 11:31 +0200
      Re: Struggeling with collections Faling Dutchman <henriarends92@gmail.com> - 2016-03-07 01:43 -0800
    Re: Struggeling with collections Roel Schroeven <roel@roelschroeven.net> - 2016-03-07 22:55 +0100

#104193 — Struggeling with collections

FromFaling Dutchman <henriarends92@gmail.com>
Date2016-03-07 00:24 -0800
SubjectStruggeling with collections
Message-ID<241aeea9-2604-4fa4-bf06-822ba5be143b@googlegroups.com>
Hey folks,

I am just starting off in python, but have good knowledge of both Java and C#. Now is the problem that I need to have multiple instances of one dictionary, that is not a problem if you know how many, but now, it is an unknown amount.

Some background info:

I am making a library for an API. This library must be easy to use for the people who are going to use it. So I am making the models for the data, the connections and so on, so they just have to fill in the gaps. In C# and Java I did it with objects, but they do not work alike in python, or at least that is what I have found.

If I do this:

class Item:
    def __init__(self, id, productId, quantity, pageCount, files, option, metadata):
        self.id = id
        self.productId = productId
        self.quantity = quantity
        self.pageCount = pageCount
        self.files = files
        self.option = option
        self.metadata = metadata

itm = Item(1,None,1,1,'asdf',{'asdf': 3, 'ads': 55},None)
print(itm)

it prints: <__main__.Item object at 0x02EBF3B0>

So that is not usefull to me. There can be an infinite amount of objects of Item, and it needs to be easy accessable, just like 
for i in items
    print(i)

and it has to show all the parameters of the class Item and not say "ive got an object  at this memory address, have a nice day"

I hope my question is clear.

[toc] | [next] | [standalone]


#104196

FromVincent Vande Vyvre <vincent.vande.vyvre@telenet.be>
Date2016-03-07 09:39 +0100
Message-ID<mailman.10.1457340581.10335.python-list@python.org>
In reply to#104193
Le 07/03/2016 09:24, Faling Dutchman a écrit :
> Hey folks,
>
> I am just starting off in python, but have good knowledge of both Java and C#. Now is the problem that I need to have multiple instances of one dictionary, that is not a problem if you know how many, but now, it is an unknown amount.
>
> Some background info:
>
> I am making a library for an API. This library must be easy to use for the people who are going to use it. So I am making the models for the data, the connections and so on, so they just have to fill in the gaps. In C# and Java I did it with objects, but they do not work alike in python, or at least that is what I have found.
>
> If I do this:
>
> class Item:
>      def __init__(self, id, productId, quantity, pageCount, files, option, metadata):
>          self.id = id
>          self.productId = productId
>          self.quantity = quantity
>          self.pageCount = pageCount
>          self.files = files
>          self.option = option
>          self.metadata = metadata
>
> itm = Item(1,None,1,1,'asdf',{'asdf': 3, 'ads': 55},None)
> print(itm)
>
> it prints: <__main__.Item object at 0x02EBF3B0>
>
> So that is not usefull to me. There can be an infinite amount of objects of Item, and it needs to be easy accessable, just like
> for i in items
>      print(i)
>
> and it has to show all the parameters of the class Item and not say "ive got an object  at this memory address, have a nice day"
>
> I hope my question is clear.
>
The classes have a dict

Python 3.2.3 (default, Jun 18 2015, 21:46:42)
[GCC 4.6.3] on linux2
Type "help", "copyright", "credits" or "license" for more information.
 >>> class Item:
...     def __init__(self, id, productId, quantity, pageCount, files, 
option, metadata):
...         self.id = id
...         self.productId = productId
...         self.quantity = quantity
...         self.pageCount = pageCount
...         self.files = files
...         self.option = option
...         self.metadata = metadata
...
 >>> i = Item(1,None,1,1,'asdf',{'asdf': 3, 'ads': 55},None)
 >>> i.__dict__
{'files': 'asdf', 'option': {'ads': 55, 'asdf': 3}, 'pageCount': 1, 
'metadata': None, 'productId': None, 'id': 1, 'quantity': 1}
 >>>

[toc] | [prev] | [next] | [standalone]


#104198

FromFaling Dutchman <henriarends92@gmail.com>
Date2016-03-07 00:58 -0800
Message-ID<7687ab5d-edba-4ceb-af03-d6b050503802@googlegroups.com>
In reply to#104196
Op maandag 7 maart 2016 09:49:51 UTC+1 schreef Vincent Vande Vyvre:
> Le 07/03/2016 09:24, Faling Dutchman a écrit :
> > Hey folks,
> > I am just starting off in python, but have good knowledge of both Java and C#. Now is the problem that I need to have multiple instances of one dictionary, that is not a problem if you know how many, but now, it is an unknown amount.
> > Some background info:
> > I am making a library for an API. This library must be easy to use for the people who are going to use it. So I am making the models for the data, the connections and so on, so they just have to fill in the gaps. In C# and Java I did it with objects, but they do not work alike in python, or at least that is what I have found.
> > If I do this:
> > class Item:
> >      def __init__(self, id, productId, quantity, pageCount, files, option, metadata):
> >          self.id = id
> >          self.productId = productId
> >          self.quantity = quantity
> >          self.pageCount = pageCount
> >          self.files = files
> >          self.option = option
> >          self.metadata = metadata
> > itm = Item(1,None,1,1,'asdf',{'asdf': 3, 'ads': 55},None)
> > print(itm)
> > it prints: <__main__.Item object at 0x02EBF3B0>
> > So that is not usefull to me. There can be an infinite amount of objects of Item, and it needs to be easy accessable, just like
> > for i in items
> >      print(i)
> > and it has to show all the parameters of the class Item and not say "ive got an object  at this memory address, have a nice day"
> > I hope my question is clear.
> The classes have a dict
> Python 3.2.3 (default, Jun 18 2015, 21:46:42)
> [GCC 4.6.3] on linux2
> Type "help", "copyright", "credits" or "license" for more information.
>  >>> class Item:
> ...     def __init__(self, id, productId, quantity, pageCount, files, 
> option, metadata):
> ...         self.id = id
> ...         self.productId = productId
> ...         self.quantity = quantity
> ...         self.pageCount = pageCount
> ...         self.files = files
> ...         self.option = option
> ...         self.metadata = metadata
> ...
>  >>> i = Item(1,None,1,1,'asdf',{'asdf': 3, 'ads': 55},None)
>  >>> i.__dict__
> {'files': 'asdf', 'option': {'ads': 55, 'asdf': 3}, 'pageCount': 1, 
> 'metadata': None, 'productId': None, 'id': 1, 'quantity': 1}
>  >>>

Thanks, that is at least part of the problem solved. The rest I can figure out myself though. This was the biggest hurdle.

[toc] | [prev] | [next] | [standalone]


#104197

FromSteven D'Aprano <steve+comp.lang.python@pearwood.info>
Date2016-03-07 19:52 +1100
Message-ID<56dd4167$0$1602$c3e8da3$5496439d@news.astraweb.com>
In reply to#104193
On Monday 07 March 2016 19:24, Faling Dutchman wrote:

> Hey folks,
> 
> I am just starting off in python, but have good knowledge of both Java and
> C#. Now is the problem that I need to have multiple instances of one
> dictionary, that is not a problem if you know how many, but now, it is an
> unknown amount.

Whenever you have an unpredictable number of some object, *any* object, you 
should turn to a list, or a dict.

"I need some integers, but I don't know if there will be 3 or 5."

Wrong solution:

a = 23
b = 24
c = 25
if foo:
    d = 26
    e = 27
# later...
if foo:
    print(a, b, c, d, e)
else:
    print(a, b, c)    



Right solution:


integers = [23, 24, 25]
if foo:
    integers.extend([26, 27])
# later...
print(*foo)


It doesn't matter whether you are dealing with ints, floats, strings, dicts, 
lists, sets, or your own custom-built objects. If you don't know in advance 
how many you want, put them in a list. Or a dict.



> Some background info:
> 
> I am making a library for an API. This library must be easy to use for the
> people who are going to use it. 

Define "easy to use".



> If I do this:
> 
> class Item:
>     def __init__(self, id, productId, quantity, pageCount, files, option,
>     metadata):
>         self.id = id
>         self.productId = productId
>         self.quantity = quantity
>         self.pageCount = pageCount
>         self.files = files
>         self.option = option
>         self.metadata = metadata
> 
> itm = Item(1,None,1,1,'asdf',{'asdf': 3, 'ads': 55},None)
> print(itm)
> 
> it prints: <__main__.Item object at 0x02EBF3B0>
> 
> So that is not usefull to me.

Then create a better __repr__ or __str__ method. All you are seeing is the 
default representation inherited from object. If you don't like it, override 
it.



-- 
Steve

[toc] | [prev] | [next] | [standalone]


#104199

FromGary Herron <gherron@digipen.edu>
Date2016-03-07 01:01 -0800
Message-ID<mailman.11.1457341292.10335.python-list@python.org>
In reply to#104193
On 03/07/2016 12:24 AM, Faling Dutchman wrote:
> Hey folks,
>
> I am just starting off in python, but have good knowledge of both Java and C#. Now is the problem that I need to have multiple instances of one dictionary, that is not a problem if you know how many, but now, it is an unknown amount.
>
> Some background info:
>
> I am making a library for an API. This library must be easy to use for the people who are going to use it. So I am making the models for the data, the connections and so on, so they just have to fill in the gaps. In C# and Java I did it with objects, but they do not work alike in python, or at least that is what I have found.
>
> If I do this:
>
> class Item:
>      def __init__(self, id, productId, quantity, pageCount, files, option, metadata):
>          self.id = id
>          self.productId = productId
>          self.quantity = quantity
>          self.pageCount = pageCount
>          self.files = files
>          self.option = option
>          self.metadata = metadata
>
> itm = Item(1,None,1,1,'asdf',{'asdf': 3, 'ads': 55},None)
> print(itm)
>
> it prints: <__main__.Item object at 0x02EBF3B0>
>
> So that is not usefull to me. There can be an infinite amount of objects of Item, and it needs to be easy accessable, just like
> for i in items
>      print(i)
>
> and it has to show all the parameters of the class Item and not say "ive got an object  at this memory address, have a nice day"
>
> I hope my question is clear.

It's not clear in the slightest.  In fact there isn't even a question 
here.  Ignoring everything about numbers of objects and libraries and 
access (of what?), it seems you want an object of type Item to be able 
to print itself nicely.    Please correct me if I've got that wrong.

You can do so by defining a member __str__ (or __repr__).  Here's a 
small example.  The returned formatted string can be as simple or 
complex as you wish.

 >>> class C:
...   def __init__(self, a, b):
...     self.a = a
...     self.b = b
...   def __str__(self):
...     return "C(a={}, b={})".format(self.a, self.b)  # Modify to suit 
your needs.
...
 >>> print(C(1,2))
C(a=1, b=2)
 >>>


Gary Herron


>


-- 
Dr. Gary Herron
Department of Computer Science
DigiPen Institute of Technology
(425) 895-4418

[toc] | [prev] | [next] | [standalone]


#104200

FromJussi Piitulainen <jussi.piitulainen@helsinki.fi>
Date2016-03-07 11:31 +0200
Message-ID<lf5oaaqfxcc.fsf@ling.helsinki.fi>
In reply to#104193
Faling Dutchman writes:

> I am just starting off in python, but have good knowledge of both Java
> and C#. Now is the problem that I need to have multiple instances of

...

> it prints: <__main__.Item object at 0x02EBF3B0>
>
> So that is not usefull to me. There can be an infinite amount of

...

> and it has to show all the parameters of the class Item and not say
> "ive got an object at this memory address, have a nice day"

Others have now told you that your class has a special method that
defines the string representation of instances. What you are seeing is
the default. You can specify your own.

I just want to add that Java does it the same way. Surely this hasn't
changed? (I've never known C#, so no comment on that.)

[toc] | [prev] | [next] | [standalone]


#104201

FromFaling Dutchman <henriarends92@gmail.com>
Date2016-03-07 01:43 -0800
Message-ID<bd8cd91c-42ca-46bb-9597-576e05ad5345@googlegroups.com>
In reply to#104200
Op maandag 7 maart 2016 10:31:48 UTC+1 schreef Jussi Piitulainen:
> Faling Dutchman writes:
> > I am just starting off in python, but have good knowledge of both Java
> > and C#. Now is the problem that I need to have multiple instances of
> ...
> > it prints: <__main__.Item object at 0x02EBF3B0>
> >
> > So that is not usefull to me. There can be an infinite amount of
> ...
> > and it has to show all the parameters of the class Item and not say
> > "ive got an object at this memory address, have a nice day"
> Others have now told you that your class has a special method that
> defines the string representation of instances. What you are seeing is
> the default. You can specify your own.
> I just want to add that Java does it the same way. Surely this hasn't
> changed? (I've never known C#, so no comment on that.)


At least, I've never encountered it, but well, the __dict__ is very usefull, as I said, I'm verry new to python, but I love programming in it. I hope to get way better in it as I am now :)

[toc] | [prev] | [next] | [standalone]


#104258

FromRoel Schroeven <roel@roelschroeven.net>
Date2016-03-07 22:55 +0100
Message-ID<mailman.52.1457387777.10335.python-list@python.org>
In reply to#104193
Faling Dutchman schreef op 2016-03-07 09:24:
> class Item:
>     def __init__(self, id, productId, quantity, pageCount, files, option, metadata):
>         self.id = id
>         self.productId = productId
>         self.quantity = quantity
>         self.pageCount = pageCount
>         self.files = files
>         self.option = option
>         self.metadata = metadata
> 
> itm = Item(1,None,1,1,'asdf',{'asdf': 3, 'ads': 55},None)
> print(itm)
> 
> it prints: <__main__.Item object at 0x02EBF3B0>

I'm not 100% sure exactly what you need, but namedtuple might be what 
you're looking for:

 >>> import collections
 >>> Item = collections.namedtuple('Item', 'id productId quantity 
pageCount files option metadata')
 >>> itm = Item(1, None, 1, 1, 'asdf', {'asdf': 3, 'ads': 55}, None)
 >>> print(itm)
Item(id=1, productId=None, quantity=1, pageCount=1, files='asdf', 
option={'ads': 55, 'asdf': 3}, metadata=None)

See 
https://docs.python.org/3/library/collections.html?highlight=namedtuple#collections.namedtuple


-- 
The saddest aspect of life right now is that science gathers knowledge
faster than society gathers wisdom.
   -- Isaac Asimov

Roel Schroeven

[toc] | [prev] | [standalone]


Back to top | Article view | comp.lang.python


csiph-web