Re: [Tutor] Modules and Python tutorial by S. Thurlow - opinions please

2011-08-21 Thread Lisi
On Saturday 20 August 2011 23:53:53 Alan Gauld wrote:
> On 20/08/11 15:25, Lisi wrote:
> > ridiculous.  I think that I understand how to write a basic function, but
> > I can't work out how to save and call it.
[snip]
> There is a full worked example in the Functions and Modules
> topic in my tutorial...

Bless you, Alan.  Not only is your description great, but it caused me to 
start to read your tutorial.

Bingo.  I think I now understand the conceptual problem I had at the core of 
this.  You actually make it clear in your answer in this thread, but I was so 
bogged down that I didn't take it in.

A function is a sub-routine and not a program. 

That's it.  So simple and I have been stuck on this for weeks!

Now to start again!  When, of course, I may get stuck again.  But I think next 
time I am stuck on something apparently trivial, I'll ask sooner.  And the 
descriptions that you and Steven have given me of how to do it make a lot 
more sense than the ones I was struggling with.

Heartfelt thanks to Alan and Steven.

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


[Tutor] Where is sys.py?

2011-08-21 Thread Lisi
If sys.py is a file, it must be somewhere; but I can't find it.  Where is it?  
I would like to look at it.

The modules within it must surely be somewhere too.  But since I can't find 
sys, I obviously can't find the modules.  Again, I'd like to look at them.

In case it is relevant, I am using Python 2.5.2 on Debian 5.

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


Re: [Tutor] Where is sys.py?

2011-08-21 Thread R. Berman


> -Original Message-
> From: tutor-bounces+bermanrl=cfl.rr@python.org [mailto:tutor-
> bounces+bermanrl=cfl.rr@python.org] On Behalf Of Lisi
> Sent: Sunday, August 21, 2011 7:58 AM
> To: tutor@python.org
> Subject: [Tutor] Where is sys.py?
> 
> If sys.py is a file, it must be somewhere; but I can't find it.
> Where is it?
> I would like to look at it.
> 
> The modules within it must surely be somewhere too.  But since I
> can't find
> sys, I obviously can't find the modules.  Again, I'd like to look at
> them.
> 
> In case it is relevant, I am using Python 2.5.2 on Debian 5.
> 
> Lisi
> 

Try whereis python.sys

Robert


--
I am using the free version of SPAMfighter.
We are a community of 7 million users fighting spam.
SPAMfighter has removed 342 of my spam emails to date.
Get the free SPAMfighter here: http://www.spamfighter.com/len

The Professional version does not have this message


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


Re: [Tutor] Where is sys.py?

2011-08-21 Thread Peter Otten
Lisi wrote:

> If sys.py is a file, it must be somewhere; but I can't find it.  Where is
> it? I would like to look at it.

You are out of luck, there is no sys.py. The sys module is so tightly 
integrated that Python cannot run without it; it has to be part of the 
interpreter and is written in C. 

In general, to see the parts of Python that are written in C you have to 
download the source distribution, but for an occasional glimpse at a 
specific module you can also use an URL like

http://hg.python.org/cpython/file/2.5/Python/sysmodule.c



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


Re: [Tutor] Where is sys.py?

2011-08-21 Thread xDog Walker
On Sunday 2011 August 21 04:57, Lisi wrote:
> If sys.py is a file, it must be somewhere; but I can't find it.  Where is
> it? I would like to look at it.
>
> The modules within it must surely be somewhere too.  But since I can't find
> sys, I obviously can't find the modules.  Again, I'd like to look at them.
>
> In case it is relevant, I am using Python 2.5.2 on Debian 5.

The source is in C, try

locate */Python/sysmodule.c

if you have the source for your Python.

-- 
I have seen the future and I am not in it.
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Where is sys.py?

2011-08-21 Thread Lisi
On Sunday 21 August 2011 12:57:50 Lisi wrote:
> If sys.py is a file, it must be somewhere; but I can't find it.  Where is
> it? I would like to look at it.
>
> The modules within it must surely be somewhere too.  But since I can't find
> sys, I obviously can't find the modules.  Again, I'd like to look at them.
>
> In case it is relevant, I am using Python 2.5.2 on Debian 5.

Thanks, all three.  At least I now know why I can't find it!

Robert -

I had already tried various combimations of whereis, python and sys, all to no 
avail.  But I tried your command and looked at every directory and file under 
the results (and tehre were a lot)!  Still to no avail - so I switched from 
my personal inbox to the Python one - and there we have the explanation.

Thanks Peter and "xDog".

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


Re: [Tutor] Where is sys.py?

2011-08-21 Thread Emile van Sebille

On 8/21/2011 4:57 AM Lisi said...

If sys.py is a file, it must be somewhere; but I can't find it.  Where is it?
I would like to look at it.

The modules within it must surely be somewhere too.  But since I can't find
sys, I obviously can't find the modules.  Again, I'd like to look at them.

In case it is relevant, I am using Python 2.5.2 on Debian 5.




Modules have a __file__ attribute, but not sys as you've heard.

Python 2.6.6 (r266:84292, May 25 2011, 09:51:06)
[GCC 3.4.6 20060404 (Red Hat 3.4.6-11)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import sys
>>> sys.__file__
Traceback (most recent call last):
  File "", line 1, in 
AttributeError: 'module' object has no attribute '__file__'
>>> import os
>>> os.__file__
'/usr/local/lib/python2.6/os.pyc'
>>>

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


[Tutor] Coin Flipping Program

2011-08-21 Thread Joel Preston
Hello Programmers,

I am trying to learn Python on my own through a book called "Python
Programming for the Absolute Beginner".  I am stuck on a challenge at the
end of a chapter and I am hoping for a little help.

The challenge is to write a program that flips a coin 100 times and tells
you the number of heads and tails that come up.  So far I can write a
program that will flip a coin 100 times but it will only come up tails or
heads all 100 times.  I have been stuck on this for about 3 days now.  I
have attached the program and am hoping for some advice.

Oh by the way this is Python 3.2 and I am running Window Vista.

Thanks in advance,
Joel Preston

-- 
Joel Preston
Home Again Restorations, L.L.C.
988 Birdwood Drive
Orange Park, FL  32073
904-505-3306


Coin Flip 6.py
Description: Binary data
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Coin Flipping Program

2011-08-21 Thread xDog Walker
On Sunday 2011 August 21 09:14, Joel Preston wrote:
> Hello Programmers,
>
> I am trying to learn Python on my own through a book called "Python
> Programming for the Absolute Beginner".  I am stuck on a challenge at the
> end of a chapter and I am hoping for a little help.
>
> The challenge is to write a program that flips a coin 100 times and tells
> you the number of heads and tails that come up.  So far I can write a
> program that will flip a coin 100 times but it will only come up tails or
> heads all 100 times.  I have been stuck on this for about 3 days now.  I
> have attached the program and am hoping for some advice.
>


Move coin = random.randint(1, 2) inside the "flipping loop".
-- 
I have seen the future and I am not in it.
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


[Tutor] How have I transgressed??

2011-08-21 Thread Lisi
I have just received the following.*  In what way have I transgressed?  I 
apologise to you all - but as I mentioned, I had been stuck for weeks and am 
running out of time.  And I certainly tried to help myself - I just didn't 
succeed! Anyhow, if someone will tell me in what way I have transgressed, 
I'll try harder in future.

Thanks,
Lisi

*Your message for tutor@python.org, the Python programming tutor list,
has been received and is being delivered.  This automated response is
sent to those of you new to the Tutor list, to point out a few
resources that can help with answering your own questions, or improve
the chances of getting a useful answer from the other subscribers.

If your question is something akin to:

"I've just heard about Python, and it sounds great!  Where can I
 find out more on how to program with Python?"

  or:

 "What's Python?"

please read section 1 below.

On the other hand, if your question is:
  
"I've heard that Python is good for hacking -- I want to know
more!"

  or

"Can you teach me how to break into a computer with Python?"

please read section 2 at the bottom of this email.

Section 1: --

The most comprehensive overview of python.org help resources is at

  http://www.python.org/Help.html

The Python FAQ is available at

  http://www.python.org/doc/FAQ.html 

and it has answers to many questions that people ask, possibly
including your question.  Another wealth of information and experience
can be found via the python.org searches, at

  http://www.python.org/search/

There you'll find comprehensive, easy-to-use searches over the
python.org web site and the Python newsgroup, comp.lang.python.

Python has an online tutorial, available freely from

  http://www.python.org/doc/current/tutorial/index.html

Finally, when you do send email to the Tutor list, be as clear as you
can about the problem, including, when relevant, details like:

 - Precise error messages, including complete tracebacks
 - The hardware platform (available in the Python sys module as
sys.platform)
 - The python version (sys.version) 
 - The python search path (sys.path)

In general, be specific about what was going on connected with the
problem or what specific concept you're having difficulties with.  The
better the info you provide, the more likely the helpers will be able
to glean the answer...

There's a HOWTO that shows how to ask "smart" questions to technical
folks:

http://catb.org/~esr/faqs/smart-questions.html

Although it is provocative, it does have some good points, and is an
interesting read.


Note that no one is paid to read the tutor list or provide answers,
and most readers often have other work that demands their attention.
Well-posed requests for help are usually answered fairly promptly, but
occasionally a request slips by, so if you do not get a response with
one or two working days (it's usually quicker than that), please feel
free to send a followup, asking whether anyone is working on your
question.

Anyway, your message is being delivered to the Tutor list as this one
is being sent.  However, if your question was about as detailed as
"Teach me how to program in Python", do not count on an answer -- this
email contains all the information you need to start.  Come back with
a more precise question, and we'll be glad to help.


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


[Tutor] Store the output of os.system

2011-08-21 Thread ANKUR AGGARWAL
Hey
I am executing a grep command in my python program using os.system. I
want to store the output somewhere but because of GUI's involved
(generated using tkinter and pyzenity) I am unable to store my output
or the result of the executed grep command in some form. Can you guys
tell me how to store the output of a terminal or show the output in a
GUI in python??
Thanks in advance.

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


Re: [Tutor] How have I transgressed??

2011-08-21 Thread Emile van Sebille

On 8/21/2011 10:08 AM Lisi said...

I have just received the following.*  In what way have I transgressed?  I
apologise to you all - but as I mentioned, I had been stuck for weeks and am
running out of time.  And I certainly tried to help myself - I just didn't
succeed! Anyhow, if someone will tell me in what way I have transgressed,
I'll try harder in future.

Thanks,
Lisi

*Your message for tutor@python.org, the Python programming tutor list,
has been received and is being delivered.  This automated response is
sent to those of you new to the Tutor list,


The sender's email address is verified and those 'new to the list' get 
this message -- do you have multiple email accounts or possibly typoed 
in your email address on the message that evoked the 'new' response?


Emile



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


Re: [Tutor] Coin Flipping Program

2011-08-21 Thread Alan Gauld

On 21/08/11 17:14, Joel Preston wrote:


tells you the number of heads and tails that come up.  So far I can
write a program that will flip a coin 100 times but it will only come up
tails or heads all 100 times.


Look again at your code, you only assign a value to coin once, before 
you enter the loop.


> import random
>
> print('Flip the coin 100 times and see how many times tails come up.')
> input('Flip...')
>
> # Set initial values
>
> coin = random.randint(1, 2)

This is the only assignment.

> tries = 0
>
> # flipping loop
> while tries != 100:

In general if you are looping for a fixed number of times its easier to 
use a for loop:


for tries in range(100):
>if coin == 1:
>print("heads")
>else:
>print("tails")
>
>input("Flip again...")

You ask the user to flip but you never assign a new value
So you need to add an assignment line inside the lop.
The easiest way is just to move the existing line above down
to the start of the loop code.


HTH,

--
Alan G
Author of the Learn to Program web site
http://www.alan-g.me.uk/

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


Re: [Tutor] Store the output of os.system

2011-08-21 Thread Emile van Sebille

On 8/21/2011 10:15 AM ANKUR AGGARWAL said...

Hey
I am executing a grep command in my python program using os.system. I
want to store the output somewhere but because of GUI's involved
(generated using tkinter and pyzenity) I am unable to store my output
or the result of the executed grep command in some form. Can you guys
tell me how to store the output of a terminal or show the output in a
GUI in python??


Instead of os.system, try the commands or subprocess module as 
appropriate for your version of python.


HTH,

Emile



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


Re: [Tutor] How have I transgressed??

2011-08-21 Thread Lisi
Thanks for the reply, Emile. :-)

On Sunday 21 August 2011 18:25:44 Emile van Sebille wrote:
> The sender's email address is verified and those 'new to the list' get
> this message -- do you have multiple email accounts

Yes - but I consistently use the same one for this list, and have checked my 
recent emails, and all came from the same (correct) address.

> or possibly typoed 
> in your email address on the message that evoked the 'new' response?

No - I have checked.  So long as it wasn't triggered by a misdemeanour - that 
was what I was worried about!

I have my email client set twice over to be correct for this list.  a) it 
knows that this is a list and what email address I want used for this list 
and b) I use the default address for this list.  Oh - and c) I don't type the 
address - I just chose which one.

I think that it must just remain one of life's little mysteries. :-/

Lisi




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


Re: [Tutor] Coin Flipping Program

2011-08-21 Thread delegbede
Hi Joel, 

Someone has asked this question before and I think it was dealt with. 

That said, in my opinion, starting out with python 3 reduces the number of 
assist you can get on the fly. 

I for one have dealt with this issue before but can't help with the python 3 
areas. 

Have you checked Alan Gauld's website? He dealt with python 3. 

Sorry Mate. 
Sent from my BlackBerry wireless device from MTN

-Original Message-
From: Joel Preston 
Sender: tutor-bounces+delegbede=dudupay@python.org
Date: Sun, 21 Aug 2011 12:14:37 
To: 
Subject: [Tutor] Coin Flipping Program

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


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


Re: [Tutor] How have I transgressed??

2011-08-21 Thread Andre Engels
On Sun, Aug 21, 2011 at 7:25 PM, Emile van Sebille  wrote:

> On 8/21/2011 10:08 AM Lisi said...
>
>  I have just received the following.*  In what way have I transgressed?  I
>> apologise to you all - but as I mentioned, I had been stuck for weeks and
>> am
>> running out of time.  And I certainly tried to help myself - I just didn't
>> succeed! Anyhow, if someone will tell me in what way I have transgressed,
>> I'll try harder in future.
>>
>> Thanks,
>> Lisi
>>
>> *Your message for tutor@python.org, the Python programming tutor list,
>> has been received and is being delivered.  This automated response is
>> sent to those of you new to the Tutor list,
>>
>
> The sender's email address is verified and those 'new to the list' get this
> message -- do you have multiple email accounts or possibly typoed in your
> email address on the message that evoked the 'new' response?
>

I have gotten it several times on this address; It seems the reaction is
also given if you send a message after having been absent for a while.

-- 
André Engels, andreeng...@gmail.com
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] How have I transgressed??

2011-08-21 Thread Luke Paireepinart
Same here as Andre said. I've gotten that email probably a dozen times by now. 
It's based on an amount of absence I believe.

-
Sent from a mobile device. Apologies for brevity and top-posting.
-

On Aug 21, 2011, at 2:37 PM, Andre Engels  wrote:

> On Sun, Aug 21, 2011 at 7:25 PM, Emile van Sebille  wrote:
> On 8/21/2011 10:08 AM Lisi said...
> 
> I have just received the following.*  In what way have I transgressed?  I
> apologise to you all - but as I mentioned, I had been stuck for weeks and am
> running out of time.  And I certainly tried to help myself - I just didn't
> succeed! Anyhow, if someone will tell me in what way I have transgressed,
> I'll try harder in future.
> 
> Thanks,
> Lisi
> 
> *Your message for tutor@python.org, the Python programming tutor list,
> has been received and is being delivered.  This automated response is
> sent to those of you new to the Tutor list,
> 
> The sender's email address is verified and those 'new to the list' get this 
> message -- do you have multiple email accounts or possibly typoed in your 
> email address on the message that evoked the 'new' response?
> 
> I have gotten it several times on this address; It seems the reaction is also 
> given if you send a message after having been absent for a while.
> 
> -- 
> André Engels, andreeng...@gmail.com
> 
> ___
> Tutor maillist  -  Tutor@python.org
> To unsubscribe or change subscription options:
> http://mail.python.org/mailman/listinfo/tutor
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Store the output of os.system

2011-08-21 Thread Alan Gauld

On 21/08/11 18:15, ANKUR AGGARWAL wrote:

Hey
I am executing a grep command in my python program using os.system.


I guess the first question is why?
Python's re module can do pretty much anything that grep can do
and do it inside your program with no extra processes being
started.

Why not just use the re module?


or the result of the executed grep command in some form. Can you guys
tell me how to store the output of a terminal or show the output in a
GUI in python??


If you really must start an external program then
the best solution these days is to use the subprocess module.
The system() call is very rarely the best solution for anything
other than a very basic fire 'n forget command.

The subprocess documentation gives lots of example usage,
one of them should be right for you.


--
Alan G
Author of the Learn to Program web site
http://www.alan-g.me.uk/

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


[Tutor] Please explain TypeError

2011-08-21 Thread D. Guandalino
Python documentation says:

> exception TypeError
>
> Raised when an operation or function is applied to an object of
> inappropriate type. The associated value is a string giving details
> about the type mismatch.

For example:

>>> 'foo' + (1, 2)
Traceback (most recent call last):
  File "", line 1, in 
TypeError: cannot concatenate 'str' and 'tuple' objects

So far so good. But why the following code gives a TypeError too?

>>> class C(object):
... def __init__(self):
... pass
...
>>> C(1)
Traceback (most recent call last):
  File "", line 1, in 
TypeError: __init__() takes exactly 1 argument (2 given)

I'm having hard times understanding why a TypeError is raised here.
Could you explain?

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


[Tutor] Please help understanding unittest fixtures

2011-08-21 Thread D. Guandalino
Suppose I have this TestCase class.

class C(TestCase):
   def setUp():
  # very time consuming and resources intensive stuffs.
  pass

   def test_A(self):
   pass
   def test_B(self):
   pass
   def test_C(self):
   pass

The unittest docs says:

> Each instance of the TestCase will only be used to run a single test
> method, so a new fixture is created for each test.


Does this mean that the setUp() method, which is called to prepare the test
fixture, get called for test_A, test_B and test_C? In this case is there a
way to force just one setUp() call?

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


Re: [Tutor] Please explain TypeError

2011-08-21 Thread Dave Angel

On 08/21/2011 05:35 PM, D. Guandalino wrote:

Python documentation says:


exception TypeError

Raised when an operation or function is applied to an object of
inappropriate type. The associated value is a string giving details
about the type mismatch.

For example:


'foo' + (1, 2)

Traceback (most recent call last):
   File "", line 1, in
TypeError: cannot concatenate 'str' and 'tuple' objects

So far so good. But why the following code gives a TypeError too?


class C(object):

... def __init__(self):
... pass
...

C(1)

Traceback (most recent call last):
   File "", line 1, in
TypeError: __init__() takes exactly 1 argument (2 given)

I'm having hard times understanding why a TypeError is raised here.
Could you explain?

You didn't supply a parameter in the __init__() to receive the value of 
1.  The self argument is automatically supplied by Python for all 
ordinary method calls including initial object construction.  So when 
you say   C(1)  you're actually going to get 2 arguments in the 
__init__() call.


Add another parm to the definition:

  def __init__(self, startvalue):
 pass
--

DaveA

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


Re: [Tutor] Please explain TypeError

2011-08-21 Thread D. Guandalino
On 22 August 2011 00:17, Dave Angel  wrote:

> On 08/21/2011 05:35 PM, D. Guandalino wrote:
>
>> Python documentation says:
>>
>>  exception TypeError
>>>
>>> Raised when an operation or function is applied to an object of
>>> inappropriate type. The associated value is a string giving details
>>> about the type mismatch.
>>>
>>
>>

>  You didn't supply a parameter in the __init__() to receive the value of 1.
>  The self argument is automatically supplied by Python for all ordinary
> method calls including initial object construction.  So when you say   C(1)
>  you're actually going to get 2 arguments in the __init__() call.
>
> Add another parm to the definition:
>
>  def __init__(self, startvalue):
> pass
>

Thanks and apologies for not being clear.

I missed to say that I'm more interested in understanding which error I did
with types (given that a TypeError is raised) than in fixing the code.
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Please explain TypeError

2011-08-21 Thread Walter Prins
On 21 August 2011 22:35, D. Guandalino  wrote:

> >>> class C(object):
> ... def __init__(self):
> ... pass
> ...
> >>> C(1)
> Traceback (most recent call last):
>  File "", line 1, in 
> TypeError: __init__() takes exactly 1 argument (2 given)
>
> I'm having hard times understanding why a TypeError is raised here.
> Could you explain?
>

The more obvious answer was given by David, but I'd like to also add that
the reason why Python complains in this instance with *TypeError* (as
opposed to say something like "ArgumentError" or "ValueError" imagining for
the moment such things existed) is becuase, looking function objects, fn(a)
is type-incompatible with fn(a,b).   Differently put, the argument list is
part of the type of a function, and the different argument lists makes the
expected function and the actual provided incompatible, hence Python raises
TypeError.  (Note this is somewhat speculative on my part so take it for
what it's worth.)

Regards

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


Re: [Tutor] How have I transgressed??

2011-08-21 Thread Steven D'Aprano

Lisi wrote:
I have just received the following.*  In what way have I transgressed?  I 
apologise to you all - but as I mentioned, I had been stuck for weeks and am 
running out of time.  And I certainly tried to help myself - I just didn't 
succeed! Anyhow, if someone will tell me in what way I have transgressed, 
I'll try harder in future.


Thanks,
Lisi

*Your message for tutor@python.org, the Python programming tutor list,
has been received and is being delivered.  This automated response is

[...]

I get these occasionally. As far as I can tell, the mailing list 
software gets confused sometimes and thinks you are posting for the 
first time, when you're not.


It's annoying but meaningless.




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


Re: [Tutor] Where is sys.py?

2011-08-21 Thread Steven D'Aprano

Lisi wrote:
If sys.py is a file, it must be somewhere; but I can't find it.  Where is it?  
I would like to look at it.


Others have already answered this, but consider how you might explore 
the answer at the interactive interpreter:


>>> import os
>>> os.__file__
'/usr/lib/python2.5/os.pyc'
>>> import sys
>>> sys.__file__
Traceback (most recent call last):
  File "", line 1, in 
AttributeError: 'module' object has no attribute '__file__'


So the sys module has no __file__ attribute, and therefore there is no 
sys.py. Looking deeper:



>>> import inspect
>>> inspect.getfile(sys)
Traceback (most recent call last):
  File "", line 1, in 
  File "/usr/lib/python2.5/inspect.py", line 347, in getfile
raise TypeError('arg is a built-in module')
TypeError: arg is a built-in module


Hmmm, not the most user-friendly of messages ("arg" is a built-in 
module? how about *naming* it, all modules have a name!) but the answer 
is clear: sys is part of the Python virtual machine itself, and does not 
live in a file.


(Except, of course, that Python itself is compiled from files, and the 
code creating sys will be in those.)


Furthermore:

>>> import math
>>> math.__file__
'/usr/lib/python2.5/lib-dynload/mathmodule.so'


So here we have a module that does live in a file, but it is a C 
library, not a .py python file. (On Windows, it may be a .dll.)



The modules within it must surely be somewhere too.  But since I can't find 
sys, I obviously can't find the modules.  Again, I'd like to look at them.



I don't understand what you mean by "modules within it".

Do you mean sys.modules?

That's easy:

>>> sys.modules['inspect']




--
Steven

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


Re: [Tutor] Please help understanding unittest fixtures

2011-08-21 Thread Steven D'Aprano

D. Guandalino wrote:

Suppose I have this TestCase class.

class C(TestCase):
   def setUp():
  # very time consuming and resources intensive stuffs.
  pass

   def test_A(self):
   pass
   def test_B(self):
   pass
   def test_C(self):
   pass

The unittest docs says:


Each instance of the TestCase will only be used to run a single test
method, so a new fixture is created for each test.



Does this mean that the setUp() method, which is called to prepare the test
fixture, get called for test_A, test_B and test_C? 


That's easy enough to find out:

def setUp(self):
print("calling setUp")
...


See how many times "calling setUp" is printed.



In this case is there a
way to force just one setUp() call?



I don't know if this is the best way, but the first way that comes to 
mind is this:



class C(TestCase):
initialised = False
def setUp(self):
if self.initialised:
return
# Make sure you save the flag on the class, not the instance!
self.__class__.initialised = True
...


--
Steven

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


Re: [Tutor] Please explain TypeError

2011-08-21 Thread Steven D'Aprano

D. Guandalino wrote:


class C(object):

... def __init__(self):
... pass
...

C(1)

Traceback (most recent call last):
  File "", line 1, in 
TypeError: __init__() takes exactly 1 argument (2 given)

I'm having hard times understanding why a TypeError is raised here.
Could you explain?


Because it is an error involving a type (class). Type C doesn't take any 
arguments. You gave it an argument. Therefore, it's an error regarding a 
type, hence, a TypeError.


If you feel that this is a fairly dubious line of reasoning, I agree 
with you! I think that such failures would be best given their own 
dedicated exception for "wrong number of arguments". But they don't:



>>> import math
>>> math.sin(1.1, 2.2)
Traceback (most recent call last):
  File "", line 1, in 
TypeError: sin() takes exactly 1 argument (2 given)


No types involved here, math.sin is just a function.

Oh well.




--
Steven

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