[Tutor] Tutor Digest, Vol 27, Issue 12

Philip Smith philipasmith at blueyonder.co.uk
Fri May 5 08:34:46 CEST 2006


Hi

Thanks for response.

The programme is pure python (the point is to develop a programme which is 
as fast as it can be in  python without any tricks and then to translate to 
C).

I've tried 2.5 alpha - thanks for the suggestion.  It does indeed seem to 
appropriately return memory to OS but my problem remains.

Thanks for the links which have been very useful and for the other tips. 
Will post to comp.lang.python if I can't sort this out.

Phil

----- Original Message ----- 
From: <tutor-request at python.org>
To: <tutor at python.org>
Sent: Thursday, May 04, 2006 5:56 PM
Subject: Tutor Digest, Vol 27, Issue 12


> Send Tutor mailing list submissions to
> tutor at python.org
>
> To subscribe or unsubscribe via the World Wide Web, visit
> http://mail.python.org/mailman/listinfo/tutor
> or, via email, send a message with subject or body 'help' to
> tutor-request at python.org
>
> You can reach the person managing the list at
> tutor-owner at python.org
>
> When replying, please edit your Subject line so it is more specific
> than "Re: Contents of Tutor digest..."
>
>
> Today's Topics:
>
>   1. Re: counting number of inputs (EARLIER VERSION SENT
>      ACCIDENTLY) (MICHELLE EVANS)
>   2. Re: Memory Management etc (Kent Johnson)
>   3. Re: sockets (Matt Richardson)
>   4. Re: sockets (Kent Johnson)
>   5. Python challenge (David Holland)
>   6. Re: Python challenge (Jason Massey)
>
>
> ----------------------------------------------------------------------
>
> Message: 1
> Date: Thu, 04 May 2006 07:53:40 -0400
> From: "MICHELLE EVANS" <evans1018 at verizon.net>
> Subject: Re: [Tutor] counting number of inputs (EARLIER VERSION SENT
> ACCIDENTLY)
> To: "Marc Poulin" <marc_a_poulin at yahoo.com>, <tutor at python.org>
> Message-ID: <001801c66f71$6366b1e0$e1966147 at michelleevans>
> Content-Type: text/plain; charset=iso-8859-1
>
> This is exactly what I am trying to do.  I am so confused with trying to
> write this.  I am not very familiar with any of the functions.  I keep
> reading my book and reading my book, and none of it seems to make sense
> anymore.  I can write extremely simple functions, but when I need to use
> more than one in a code, I'm lost!
>
> Thanks
>
> ----- Original Message ----- 
> From: "Marc Poulin" <marc_a_poulin at yahoo.com>
> To: <tutor at python.org>
> Sent: Thursday, May 04, 2006 12:08 AM
> Subject: Re: [Tutor] counting number of inputs (EARLIER VERSION SENT
> ACCIDENTLY)
>
>
>>
>> Michelle:
>>
>> Are you familiar with writing functions?
>> Here I've created a function named getInputs.
>>
>> I've also created a few test cases to verify that (a)
>> my understanding of the problem is correct, and (b) my
>> solution is correct.
>>
>> It's important to think about how your program is
>> supposed to behave in different situations. Do you
>> think these 3 tests are enough to prove that the code
>> is correct?
>>
>> #########################
>> ## start of code       ##
>> #########################
>> def getInputs():
>>     """
>>     Description:
>>        Collect numbers entered by the user (up to a
>> maximum of 5 values) and
>>        store them in the listOfValues.
>>
>>        Stop collecting numbers if the user enters -1
>> or if 5 numbers have been collected.
>>
>>        If the user entered -1, the -1 is NOT returned
>> as part of the list.
>>     """
>>     listOfValues = [] ## this list holds the values
>> entered by the user
>>
>>     for i in range(5):
>>         newValue = int(raw_input('Enter a number [-1
>> to exit]:'))
>>         if newValue == -1:
>>             # Return right now with whatever is
>> currently in the list.
>>             return listOfValues
>>         else:
>>             # Add this new value to the list and keep
>> looping.
>>             listOfValues.append(newValue)
>>
>>     ## If we got this far, it means the user did not
>> enter a -1 so
>>     ## the list contains 5 values.
>>     return listOfValues
>>
>> """
>> Here are a few test cases to verify the logic of my
>> code.
>>
>> Test Case 1:
>>    INPUTS:
>>       first entered value: -1
>>    RESULT:
>>       function returns empty list
>>
>> Test Case 2:
>>    INPUTS:
>>       first entered value: 1
>>       second entered value: 2
>>       third entered value: -1
>>    RESULT:
>>       returned list contains [1,2]
>>
>> Test Case 3:
>>    INPUTS:
>>       first entered value: 1
>>       second entered value: 2
>>       third entered value: 3
>>       fourth entered value: 4
>>       fifth entered value: 5
>>    RESULT:
>>       returned list contains [1,2,3,4,5]
>> """
>> if __name__ == "__main__":
>>     print getInputs()
>>
>> ###################
>> ## end of code   ##
>> ###################
>>
>>
>> --- Python <python at venix.com> wrote:
>>
>> > On Wed, 2006-05-03 at 15:33 -0400, MICHELLE EVANS
>> > wrote:
>> > > OK, I've tried a different approach to this.
>> > > How do I get this to stop by using -1?
>> > > I do not want this to print until either 5 inputs
>> > have been entered or -1
>> > > has been entered.  See below:
>> > >
>> >
>> > use a "for block" rather than a "while block" to
>> > have a normal limit of
>> > 5 repetitions:
>> >
>> > for x in range(5):
>> >
>> > will repeat 5 times with x running from 0 to 4.
>> > x is ignored - unless some use for it does turn up.
>> >
>> > the break statement allows you to terminate a block,
>> > so
>> >
>> > if number == -1: break
>> >
>> > will end the for block.
>> >
>> >
>> > Now, one of the cute features in Python is the else
>> > clause that goes
>> > with the for and while blocks.  The else block is
>> > executed when there is
>> > no break.  So the skeleton for your program can look
>> > something like
>> >
>> > for x in range(5):
>> > # get inputs and break on -1
>> > else:
>> > # no break so just process the inputs
>> >
>> > Good luck.
>> >
>> > > # Add number of per hour
>> > > numbers = []
>> > > stop = None
>> > > while stop != "-1":
>> > >     number = int(raw_input("Run number(-1 to end)
>> > : "))
>> > >     numbers.append(number)
>> > >     print
>> > >     for number in numbers:
>> > >         print number
>> > >
>> > >
>> > >
>> > >
>> > > ----- Original Message ----- 
>> > > From: "Python" <python at venix.com>
>> > > To: "MICHELLE EVANS" <evans1018 at verizon.net>
>> > > Cc: "Tutor Python" <tutor at python.org>
>> > > Sent: Wednesday, May 03, 2006 12:18 PM
>> > > Subject: Re: [Tutor] counting number of inputs
>> > (EARLIER VERSION SENT
>> > > ACCIDENTLY)
>> > >
>> > >
>> > > > (Tip: Best to use reply-to-all when responding
>> > to an email on the list)
>> > > > On Tue, 2006-05-02 at 21:34 -0400, MICHELLE
>> > EVANS wrote:
>> > > > > number1 = int(raw_input("Run number 1 (-1 to
>> > end) : "))
>> > > > > number2 = int(raw_input("Run number 2 (-1 to
>> > end) : "))
>> > > > > number3 = int(raw_input("Run number 3 (-1 to
>> > end) : "))
>> > > > > number4 = int(raw_input("Run number 4 (-1 to
>> > end) : "))
>> > > > > number5 = int(raw_input("Run number 5 (-1 to
>> > end) : "))
>> > > > Good.  You collect the string from raw_input and
>> > convert it to an
>> > > > integer.
>> > > >
>> > > > This will prompt for 5 inputs, but it is missing
>> > any logic to actually
>> > > > break if -1 is entered.  With a language like
>> > BASIC, you could stick in
>> > > > tests sort of like:
>> > > > if number1 == -1 goto done:
>> > > > BUT Python does not have a goto.  So we actually
>> > need some "flow
>> > > > control" around the block of code where you
>> > collect inputs.
>> > > >
>> > > > while blocks process an indefinite number of
>> > times while a test
>> > > > condition is True.
>> > > >
>> > > > for blocks iterate through a sequence until they
>> > reach the end.  By
>> > > > providing a sequence with the correct count, you
>> > can repeat the block
>> > > > the correct number of times.  The range (and
>> > xrange for big sequences)
>> > > > functions provide a sequence of integers that
>> > can be used conveniently
>> > > > with for.
>> > > >
>> > > > The easiest way to fix your code above would be
>> > something like:
>> > > > ask_for_number = True
>> > > > while ask_for_number:
>> > > > number1 = ....
>> > > > if number1 == -1: break
>> > > > ...
>> > > > number5 = ...
>> > > > ask_for_number = False
>> > > >
>> > > > HOWEVER, that is not a good approach in the long
>> > run.
>> > > >
>> > > > A better approach is to have a single container
>> > to hold all of the
>> > > > inputs.  For this, Python provides lists.
>> > Rather than have 5 separate
>> > > > variables, use a single list variable to hold
>> > all of the inputs.  Then
>> > > > use a "for block" to ask for the input and put
>> > the result into the list.
>> > > > You already know how to convert the input from a
>> > string to a number.
>> > > >
>> > > > If you have trouble figuring out lists and for
>> > blocks, ask for help.
>> > > >
>> > > > (Sorry about the extra email.  I forgot and used
>> > ad editor hot-key combo
>> > > > in my email program which sent the email.)
>> > > >
>> > > >
>> > > > >
>> > > > >
>> > > > > # The following will sum the numbers and then
>> > print the answer
>> > > > > sum = number1 + number2 + number3 + number4 +
>> > number5
>> > > > > print
>> > > > > print "The total number of parts produced
>> > was:", sum,"."
>> > > > >
>> > > > > I need this to ask the user to enter their
>> > number per each run.  That is
>> > > why
>> > > > > I have 5 different input numbers.  I need this
>> > break if a -1 is entered.
>> > > > > Would I use "if-else" to break this if -1 is
>> > entered?  I need to be able
>> > > to
>> > > > > count the number of lines entered.
>> > > > >
>> > > > > Thanks
>> > > > > Rick
>> > > > >
>> > > > >
>> > > > > ----- Original Message ----- 
>> > > > > From: "Python" <python at venix.com>
>> > > > > To: "MICHELLE EVANS" <evans1018 at verizon.net>
>> > > > > Cc: "Tutor Python" <tutor at python.org>
>> > > > > Sent: Tuesday, May 02, 2006 7:56 PM
>> > > > > Subject: Re: [Tutor] counting number of inputs
>> > > > >
>> > > > >
>> > > > > > On Tue, 2006-05-02 at 19:25 -0400, MICHELLE
>> > EVANS wrote:
>> > > > > > > I am trying to count the number of times a
>> > positive number is
>> > > entered
>> > > > > > > from the user.  But, the program must stop
>> > after 5 user inputs or a
>> > > > > > > negative number.
>> > > > > > >
>> > > > > > > Can anyone help.
>> > > > > > Yes, but you need to help yourself also.
>> > > > > >
>> > > > > > Do you know how to get input from the user?
>> > > > > > Do you know how to count things in Python?
>> > > > > > Do you know how to test a number to see if
>> > it is positive or negative?
>> > > > > >
>> > > > > > Why don't you post your code for any part of
>> > this problem and explain
>> > > > > > how it is supposed to work and where you are
>> > having difficulty.  If
>> > > > > > necessary, review some of the tutorials to
>> > get some pointers on
>> > > writing
>> > > > > > Python programs.
>> > > > > >
>> > > > > > We're happy to help you learn, but do not
>> > want to simply write your
>> > > > > > program for you.
>> > > > > >
>> >
>> === message truncated ===
>>
>>
>> __________________________________________________
>> Do You Yahoo!?
>> Tired of spam?  Yahoo! Mail has the best spam protection around
>> http://mail.yahoo.com
>> _______________________________________________
>> Tutor maillist  -  Tutor at python.org
>> http://mail.python.org/mailman/listinfo/tutor
>>
>
>
>
> ------------------------------
>
> Message: 2
> Date: Thu, 04 May 2006 08:33:46 -0400
> From: Kent Johnson <kent37 at tds.net>
> Subject: Re: [Tutor] Memory Management etc
> Cc: tutor at python.org
> Message-ID: <4459F4AA.9000405 at tds.net>
> Content-Type: text/plain; charset=ISO-8859-1; format=flowed
>
> Philip Smith wrote:
>> Hi
>>
>> I use Activestate Python (2.4.3) in a Windows 32 bit environment.
>>
>> I have a problem with a large programme that uses a lot of memory
>> (numerous large arrays which are dynamically extended).  I keep getting
>> unpredictable crashes (on a machine with 1/2 GB memory) whereas on my
>> laptop (1 GB memory) it seems OK.
>
> I'm way out of my expertise here but I'll give it a try...
>
> Is your program pure Python or are you using C extensions (other than
> the ones in the standard library)? If it is pure Python I think this
> would be considered a Python bug and would interest the Python developers.
>
>> The portion of the code which crashes varies a bit but curiously is
>> NEVER a part where any memory allocation is happening.
>
> Can you post any code? If you can make a short test program that shows
> the problem that will dramatically increase your chances of getting
> useful help.
>
>> I have to say I have noticed (the programme is basically a
>> batch-factoring programme for integers) that no matter how I tune gc I
>> can't get it to reliably free memory in between factoring each integer.
>
> I don't think Python will ever release memory back to the OS. This has
> changed in Python 2.5, you might be interested in trying the alpha 
> release:
> http://docs.python.org/dev/whatsnew/section-other.html
> http://www.python.org/download/releases/2.5/
>
>>
>> Because this is a development programme (and for performance reasons) I
>> use global variables some of which refer to the large arrays.
>>
>> My questions are:
>>
>> 1)    Does the mere fact that a function cites a variable as global
>> create a reference which prevents it being collected by gc?
>
> You mean a 'global' statement without actually using the named variable?
> I don't know if this creates a reference, but if so, the reference
> should go out of scope when the function exits.
>
>> 5)    Does anyone have ANY suggestions please?
>
> These links might be interesting:
> http://tinyurl.com/pszzh
> http://evanjones.ca/python-memory.html
> http://pysizer.8325.org/
>
> Asking on comp.lang.python will give you access to many more people
> familiar with Python internals than you will find on this list.
>>
>> I'm keen to solve this because I would like to make my programme
>> generally available - in every other respect its near complete and
>> massively outperforms the only other comparable pure python module
>> (nzmath) which does the same job.
>>
>> Thanks in anticipation.
>>
>> Phil
>>
>>
>> ------------------------------------------------------------------------
>>
>> _______________________________________________
>> Tutor maillist  -  Tutor at python.org
>> http://mail.python.org/mailman/listinfo/tutor
>
>
>
>
> ------------------------------
>
> Message: 3
> Date: Thu, 04 May 2006 09:23:38 -0700
> From: Matt Richardson <marichar at csusb.edu>
> Subject: Re: [Tutor] sockets
> To: Tutor at python.org
> Message-ID: <445A2A8A.40308 at csusb.edu>
> Content-Type: text/plain; charset=ISO-8859-1; format=flowed
>
> I need to send some data, 2 strings and a list, to a remote computer.
> After thinking about it some last night, it wouldn't be hard to just
> send it all as a string and then parse it on the receiving end.
>
> I'm writing a program for work (and for a class project, so no answers!)
> that will provide some info on the network location of a laptop.  The
> client will gather IP address, MAC address, and a traceroute dump (the
> list mentioned above), then send this off to a super simple server that
> receives the data and puts it in a database.  We've had a few laptops
> 'disappear' either through theft or people taking them home to do 'work
> from home' or whatever.  Makes annual inventory a huge pain.
>
> Matt
>
> -- 
> Matt Richardson
> IT Consultant
> College of Arts and Letters
> CSU San Bernardino
> (909)537-7598
>
>
>
> ------------------------------
>
> Message: 4
> Date: Thu, 04 May 2006 12:41:29 -0400
> From: Kent Johnson <kent37 at tds.net>
> Subject: Re: [Tutor] sockets
> Cc: Tutor at python.org
> Message-ID: <445A2EB9.3010604 at tds.net>
> Content-Type: text/plain; charset=ISO-8859-1; format=flowed
>
> Matt Richardson wrote:
>> I need to send some data, 2 strings and a list, to a remote computer.
>> After thinking about it some last night, it wouldn't be hard to just
>> send it all as a string and then parse it on the receiving end.
>>
>> I'm writing a program for work (and for a class project, so no answers!)
>> that will provide some info on the network location of a laptop.  The
>> client will gather IP address, MAC address, and a traceroute dump (the
>> list mentioned above), then send this off to a super simple server that
>> receives the data and puts it in a database.  We've had a few laptops
>> 'disappear' either through theft or people taking them home to do 'work
>> from home' or whatever.  Makes annual inventory a huge pain.
>
> This would be very easy to do with XML-RPC. On the server side, writ a
> function that takes three parameters - the IP address, MAC address, and
> traceroute dump - and saves them to a database. Use SimpleXMLRPCServer
> to expose the function. On the client side, gather the data and use
> xmlrpclib to call the remote function. Easy. Since this function will
> presumably be exposed on the public internet you need to worry about
> security; you should use some kind of authorization. A really simple
> solution would be to add username and password arguments to the function
> you expose.
>
> You could do all this with the socket library but XML-RPC takes care of
> all the details of connections, etc as well as marshalling and
> unmarshalling the data.
>
> Kent
>
>
>
> ------------------------------
>
> Message: 5
> Date: Thu, 4 May 2006 17:50:20 +0100 (BST)
> From: David Holland <davholla2002 at yahoo.co.uk>
> Subject: [Tutor] Python challenge
> To: tutor python <tutor at python.org>
> Message-ID: <20060504165020.15946.qmail at web25915.mail.ukl.yahoo.com>
> Content-Type: text/plain; charset="iso-8859-1"
>
> I looked at this and got stuck on the first one :-
>  http://www.pythonchallenge.com/pc/def/0.html
>  It says try changing the url.
>  I assumed that it either meant 2*38 or 2 to the power of 38 but I can't 
> see how to put either of those in the url.
>
>  What have I missed ?
>
>  Thanks in advance.
>
>  David Holland
>
>
> ---------------------------------
> Yahoo! Photos ? NEW, now offering a quality print service from just 7p a 
> photo.
> -------------- next part --------------
> An HTML attachment was scrubbed...
> URL: 
> http://mail.python.org/pipermail/tutor/attachments/20060504/ec6d3dca/attachment-0001.html
>
> ------------------------------
>
> Message: 6
> Date: Thu, 4 May 2006 11:56:02 -0500
> From: "Jason Massey" <jason.massey at gmail.com>
> Subject: Re: [Tutor] Python challenge
> To: "David Holland" <davholla2002 at yahoo.co.uk>
> Cc: tutor python <tutor at python.org>
> Message-ID:
> <7e3eab2c0605040956o389cf610l8cebfea6cf1acf13 at mail.gmail.com>
> Content-Type: text/plain; charset="windows-1252"
>
> David,
>
> What answer did you get?  One of the things to rember on the challenge is 
> to
> take your answer and put ".html" (no qutoes) on the end of it in the url.
>
> So for the first problem the url would be:
>
> http://www.pythonchallenge.com/pc/def/<your answer>.html
>
> On 5/4/06, David Holland <davholla2002 at yahoo.co.uk> wrote:
>>
>> I looked at this and got stuck on the first one :-
>> http://www.pythonchallenge.com/pc/def/0.html
>> It says try changing the url.
>> I assumed that it either meant 2*38 or 2 to the power of 38 but I can't
>> see how to put either of those in the url.
>>
>> What have I missed ?
>>
>> Thanks in advance.
>>
>> David Holland
>>
>> ------------------------------
>> *Yahoo! 
>> Photos*<http://us.rd.yahoo.com/mail/uk/taglines/default/photos/*http://uk.photos.yahoo.com/>?
>> NEW, now offering a quality print 
>> service<http://us.rd.yahoo.com/mail/uk/taglines/default/photos/*http://uk.photos.yahoo.com/>from 
>> just 7p a photo.
>>
>>
>> _______________________________________________
>> Tutor maillist  -  Tutor at python.org
>> http://mail.python.org/mailman/listinfo/tutor
>>
>>
>>
> -------------- next part --------------
> An HTML attachment was scrubbed...
> URL: 
> http://mail.python.org/pipermail/tutor/attachments/20060504/9ab893b8/attachment.htm
>
> ------------------------------
>
> _______________________________________________
> Tutor maillist  -  Tutor at python.org
> http://mail.python.org/mailman/listinfo/tutor
>
>
> End of Tutor Digest, Vol 27, Issue 12
> *************************************
> 




More information about the Tutor mailing list