Re: [Tutor] Creating lists with definite (n) items without repetitions

2015-09-18 Thread Alan Gauld

On 18/09/15 16:17, marcus lütolf wrote:

dear pythonistas,

in the code below:


how can I solve my task wit n items ?
Thank you for help, Marcus.


I see no code...


--
Alan G
Author of the Learn to Program web site
http://www.alan-g.me.uk/
http://www.amazon.com/author/alan_gauld
Follow my photo-blog on Flickr at:
http://www.flickr.com/photos/alangauldphotos


___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Creating lists with 3 (later4) items occuring only once

2015-09-18 Thread Alan Gauld

On 18/09/15 16:41, marcus lütolf wrote:


s = ['ab','ac','bc','ad','ae','de']
for startlist in itertools.combinations(s, 3):



How can I concatenate the 20 lists in oder to get one count for each of the 
items in s , for example 10 for 'ab'?



If I understand you correctly, something like this:

>>> counts = {'ab':0,'ac':0,'bc':0,'ad':0,'ae':0,'de':0}
>>> for combo in it.combinations(counts.keys(),3):
... for pair in combo:
...counts[pair] += 1
...
>>> counts
{'ac': 10, 'ab': 10, 'ae': 10, 'ad': 10, 'bc': 10, 'de': 10}

Is that what you want?

--
Alan G
Author of the Learn to Program web site
http://www.alan-g.me.uk/
http://www.amazon.com/author/alan_gauld
Follow my photo-blog on Flickr at:
http://www.flickr.com/photos/alangauldphotos


___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


[Tutor] Creating lists with 3 (later4) items occuring only once

2015-09-18 Thread marcus lütolf
dear pythonistas

in the code below

>>> import string, itertools
>>> s = ['ab','ac','bc','ad','ae','de']
>>> count = 0
>>> for startlist in itertools.combinations(s, 3):
>>>  count = count + 1
>>>  stl = list(startlist)
 >>> print count, stl
>>>  for pair in s:
>>>  x = stl.count(pair)
>>>  print x, pair

the variable stl produces 20 lists. The variable x counts how many times the 
items of s occur in each of the 20 lists.
How can I concatenate the 20 lists in oder to get one count for each of the 
items in s , for example 10 for 'ab'?
My further task will be expand s to all 26 letters oft he alphabet (giving 325 
items) and the to eliminate all list in which the items in s occur more than 
once.

Thank you for help, Marcus.



---
Diese E-Mail wurde von Avast Antivirus-Software auf Viren geprüft.
https://www.avast.com/antivirus

___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


[Tutor] Creating lists with definite (n) items without repetitions

2015-09-18 Thread marcus lütolf
dear pythonistas,

in the code below:


how can I solve my task wit n items ?
Thank you for help, Marcus.



---
Diese E-Mail wurde von Avast Antivirus-Software auf Viren geprüft.
https://www.avast.com/antivirus

___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Tutor Digest, Vol 139, Issue 29 Calling instance method in IDLE magically calls __len__?

2015-09-18 Thread Dino Bektešević
Hello all,

> Hi Dino,
>
>
> I'm afraid I cannot replicate that behaviour. Also the output seems
> strange -- the "len is called" is printed on the same line as the
> prompt, and e.toFile( afterwards.
>
> If you quit idle and restart it, do you get the same behaviour? What's
> the exact version of Python?
>
> import sys
> print sys.version
>
>
>
> --
> Steve

The exact version is 2.7.6 (2.7.6 (default, Jun 22 2015, 17:58:13) [GCC
4.8.2])
The behaviour is replicable even after OS restarts. As you've noticed the
print statement gets executed and pushes the typing command into a
new-line, this behaviour is not replicable when the file is imported as a
module or run the terminal.

I have tried figuring out what was happening by using the inspect module to
see who calls the function __len__  with some radically big context but I
can't seem to interpret the output myself (bit too deep in pythons belly
for my level). Code is shown bellow:

import inspect

class Errors:
def __init__(self):
pass
def toFile(self):
pass
def __len__(self):
curframe = inspect.currentframe()
calframe = inspect.getouterframes(curframe, context=15)
for i in range(15):
print "caller: ",calframe[i][3]
print "len is called"
return 0

What I get as output now:

>>> len(e)
caller:  __len__
caller:  
caller:  runcode
caller:  main
caller:  
len is called
0
>>> caller:  __len__
caller:  get_arg_text
caller:  fetch_tip
caller:  get_the_calltip
caller:  localcall
caller:  pollresponse
caller:  _getresponse
caller:  getresponse
caller:  handle
caller:  __init__
caller:  __init__
caller:  finish_request
caller:  process_request
caller:  _handle_request_noblock
caller:  handle_request
caller:  manage_socket
caller:  run
caller:  __bootstrap_inner
caller:  __bootstrap
len is called
e.toFile(

Maybe that can help? I know it doesn't really, it would be better to print
the class that owns the methods called, however I don't know how to do
that, so here's at least the file names:

>>> len(e)
caller:  /home/dino/Desktop/bitbucket/refactor/errors/errors.py
caller:  
caller:  /usr/lib/python2.7/idlelib/run.py
caller:  /usr/lib/python2.7/idlelib/run.py
caller:  
len is called
0
>>> caller:  /home/dino/Desktop/bitbucket/refactor/errors/errors.py
caller:  /usr/lib/python2.7/idlelib/CallTips.py
caller:  /usr/lib/python2.7/idlelib/CallTips.py
caller:  /usr/lib/python2.7/idlelib/run.py
caller:  /usr/lib/python2.7/idlelib/rpc.py
caller:  /usr/lib/python2.7/idlelib/rpc.py
caller:  /usr/lib/python2.7/idlelib/rpc.py
caller:  /usr/lib/python2.7/idlelib/rpc.py
caller:  /usr/lib/python2.7/idlelib/run.py
caller:  /usr/lib/python2.7/SocketServer.py
caller:  /usr/lib/python2.7/idlelib/rpc.py
caller:  /usr/lib/python2.7/SocketServer.py
caller:  /usr/lib/python2.7/SocketServer.py
caller:  /usr/lib/python2.7/SocketServer.py
caller:  /usr/lib/python2.7/SocketServer.py
caller:  /usr/lib/python2.7/idlelib/run.py
caller:  /usr/lib/python2.7/threading.py
caller:  /usr/lib/python2.7/threading.py
caller:  /usr/lib/python2.7/threading.py
len is called
e.toFile(


Thanks for the help,
Dino
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Calling instance method in IDLE magically calls __len__?

2015-09-18 Thread Peter Otten
Dino Bektešević wrote:

> Hello,
> 
> For full disclosure, I'm using Python2.7 on Ubuntu 14.04. MWE bellow and
> at https://bpaste.net/show/3d38c96ec938 (until 2015-09-25 06:29:54, in the
> case spaces get messed up).
> 
> class Errors:
> def __init__(self):
> pass
> def toFile(self):
> pass
> def __len__(self):
> print "len is called"
> return 0
> 
> Which is just fine if I call it over terminal, however calling it in IDLE:
> 
 e = Errors()
 len(e)
> len is called
> 0
> 
> as expected, but when try to call the method toFile, "len is called" gets
> printed as soon as I put parenthesis "(" behind the toFile.
> 
 len is called
> e.toFile(
> 
> Now I recognize that I shouldn't use __len__ to print stuff, I should use
> __string__ or at least __repr__, but I found it weird that __len__ would
> get called in that situation. So out of a stupid mistake an interesting
> question!
> 
> Why does the "len is called" get printed to IDLE when you try to call
> toFile? How does the interpreter handle this? Any kind of clarification
> would be greatly appreciated.

This is a bug in Idle. In idlelib.CallTips.get_arg_text() there is a check

if ob.im_self:
...

to decide if the first argument (i. e. self) to the function should be 
ignored. This implicitly calls __len__() if im_self (in your case the Errors 
instance associated with the bound toFile method) has a __len__ and no 
__nonzero__ method:

>>> class Errors:
... def __init__(self):
... pass
... def toFile(self):
... pass
... def __len__(self):
... print "len is called"
... return 0
... 
>>> e = Errors()
>>> if e: pass
... 
len is called
>>> if e.toFile.im_self:
... pass
... 
len is called

Normally this is intended as it allows you to write the idiomatic

if some_list:
print "the list is not empty"

instead of the long-winded

if len(some_list) > 0:
...

Therefore I recommend that you write test methods like __len__() without any 
side effects. 

However, the problem with Idle is that it decides that the method is unbound 
and therefore includes self into the list of arguments to be supplied by the 
user:

>>> import idlelib.CallTips
>>> idlelib.CallTips.get_arg_text(e.toFile)
len is called
'(self)'

For comparison the result when len(e) != 0:

>>> class Foo: # class with user-supplied len
... def __init__(self, len): self._len = len
... def __len__(self):
... print "len =", self._len
... return self._len
... def bar(self, one, two): pass
... 
>>> idlelib.CallTips.get_arg_text(Foo(0).bar)
len = 0
'(self, one, two)'
>>> idlelib.CallTips.get_arg_text(Foo(1).bar)
len = 1
'(one, two)'
>>> idlelib.CallTips.get_arg_text(Foo(42).bar)
len = 42
'(one, two)'


This can be fixed by changing the check to the stricter

if ob.im_self is not None:
...

As this has already been done (see http://bugs.python.org/issue21654) you 
don't need to report the bug.

___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] syntax error

2015-09-18 Thread Nym City via Tutor
-Perfect. Thank you. 


 On Tuesday, September 15, 2015 5:00 AM, Alan Gauld 
 wrote:
   
 

 On 15/09/15 02:41, Nym City via Tutor wrote:
> Hello,
> I am also just trying to understand this code and could not understand the 
> print statement.I would have just used:
>  print('cost:$',gross_cost)
> Do you mind telling little bit about what:
> print('cost: ${:.2f}'.format(gross_cost))
> does do and why one would use this over my plain version?

When in doubt use the >>> prompt:

 >>> gross_cost = 4.6
 >>> print('cost: $',gross_cost)
cost: $4.6
 >>> print('cost: ${:.2f}'.format(gross_cost))
cost: $4.60

So the .2f forces the output to be formatted as a floating
point number with 2 digits after the decimal point.
And the format() inserts the values into the {} markers
in the string to which its attached. Another longer example:

 >>> quantity = 4
 >>> unit_cost = 4
 >>> tax = 0.1
 >>> print('''I bought {} items at ${:.2f} with {}% tax,
... making a total cost of: ${:.2f}
... '''.format(quantity,
...          unit_cost,
...          int(tax*100),
...          quantity * unit_cost * (1+tax)))
I bought 4 items at $4.00 with 10% tax
making a total cost of: $17.60

Notice this time that :.2f forced the integer unit_cost(4)
to be shown as a float with 2 decimal places(4.00).

There are lots of other codes you can use to modify the
formatting.

Check the language reference in the docs under 'formatting':

https://docs.python.org/3/library/string.html#formatstrings

-- 
Alan G
Author of the Learn to Program web site
http://www.alan-g.me.uk/
http://www.amazon.com/author/alan_gauld
Follow my photo-blog on Flickr at:
http://www.flickr.com/photos/alangauldphotos


___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


 
  
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Calling instance method in IDLE magically calls __len__?

2015-09-18 Thread Steven D'Aprano
Hi Dino,

On Fri, Sep 18, 2015 at 09:10:00AM +0200, Dino Bektešević wrote:
> Hello,
> 
> For full disclosure, I'm using Python2.7 on Ubuntu 14.04. MWE bellow and at
> https://bpaste.net/show/3d38c96ec938 (until 2015-09-25 06:29:54, in the
> case spaces get messed up).
> 
> class Errors:
> def __init__(self):
> pass
> def toFile(self):
> pass
> def __len__(self):
> print "len is called"
> return 0
> 
> Which is just fine if I call it over terminal, however calling it in IDLE:
> 
> >>> e = Errors()
> >>> len(e)
> len is called
> 0
> 
> as expected, but when try to call the method toFile, "len is called" gets
> printed as soon as I put parenthesis "(" behind the toFile.
> 
> >>> len is called
> e.toFile(


I'm afraid I cannot replicate that behaviour. Also the output seems 
strange -- the "len is called" is printed on the same line as the 
prompt, and e.toFile( afterwards.

If you quit idle and restart it, do you get the same behaviour? What's 
the exact version of Python?

import sys
print sys.version



-- 
Steve
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


[Tutor] Calling instance method in IDLE magically calls __len__?

2015-09-18 Thread Dino Bektešević
Hello,

For full disclosure, I'm using Python2.7 on Ubuntu 14.04. MWE bellow and at
https://bpaste.net/show/3d38c96ec938 (until 2015-09-25 06:29:54, in the
case spaces get messed up).

class Errors:
def __init__(self):
pass
def toFile(self):
pass
def __len__(self):
print "len is called"
return 0

Which is just fine if I call it over terminal, however calling it in IDLE:

>>> e = Errors()
>>> len(e)
len is called
0

as expected, but when try to call the method toFile, "len is called" gets
printed as soon as I put parenthesis "(" behind the toFile.

>>> len is called
e.toFile(

Now I recognize that I shouldn't use __len__ to print stuff, I should use
__string__ or at least __repr__, but I found it weird that __len__ would
get called in that situation. So out of a stupid mistake an interesting
question!

Why does the "len is called" get printed to IDLE when you try to call
toFile? How does the interpreter handle this? Any kind of clarification
would be greatly appreciated.

Hopefully I managed to get the right mailing list,
Dino
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor