Re: [Tutor] output formatting

2009-04-27 Thread Lie Ryan

Matt Domeier wrote:

Hi Wayne,

Yes, that should work perfectly, but it is in writing files that I'm 
having the problem. I'm still very new to python, so I only know to use 
something like filename.write(str(listname)) to write my list to a file..
Is there a straightforward method of combining the ease of the print 
function setup that you suggested with write? 



Or could I somehow use the 
print function itself to write to the file (rather than just output to 
my shell/prompt)?


First note, print is a statement in python2.x and a function in python3.x.

In both case it is possible to redirect print's output to another file. 
Simply:


python2.x:
print >> f, "Hello World"

python3.x:
print("Hello World", file=f)

also for both versions:

import sys
sys.stdout = f
print("Hello World")
sys.stdout = sys.__stdout__ # return to normal

The universal stdout redirection (3rd method) was discouraged because it 
could mess up things if there are two or more routines that tries to 
override sys.stdout or in multithreading situation (it is there mainly 
for hackish quick and dirty scripts). The python2.x's redirection method 
looks ugly (for me). The python3.x's syntax is better, but is a bit verbose.


In any case, it is usually more recommended to use f.write() or 
f.writelines()


___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] How to run a .py file or load a module?

2009-04-27 Thread spir
Le Sun, 26 Apr 2009 22:35:36 +0100,
Dayo Adewunmi  s'exprima ainsi:

> How can I
> 
> a) Open my shell, and do something like: $ python countdown.py   
> but have it take an argument and pass it to the function, and execute.

When your code is (nicely) organised as a set of funcs or class definitions, 
you also need a "laucher" usually called "main()". Otherwise python only parses 
and records the definitions into live objects that wait for someone to tell 
them what they're supposed to do. I'll stick first at processes without any 
parameter, like if your func would always countdown from 10.
There are several use patterns:

(1) Program launched from command line.
Just add a call to your func:
   countdown(10)

(2) Module imported from other prog
Nothing to add to your module.
Instead, the importing code needs to hold:
   import countdown # the module (file)
   ...
   countdown.countdown(n)   # the func itself
or
   from countdown import countdown  # the func, directly
   ...
   countdown(n)

(3) Both
You need to differenciate between launching and importing. Python provides a 
rather esoteric idiom for that:
   
   if __name__ == "__main__":
  countdown(10)
The trick is that when a prog is launched directly (as opposed to imported), it 
silently gets a '__name__' attribute that is automatically set to "__main__". 
So that the one-line block above will only run when the prog is launched, like 
in case (1). While nothing will happen when the module is imported -- instead 
the importing code will have the countdown func available under name 
'countdown' as expected, like in case (2). Clear?

> b) Import the function in the interactive interpreter, and call it like so:
> 
> countdown(10)
> 
> without getting the abovementioned error.

In the case of an import, as your func definition has the proper parameter, you 
have nothing to change.
While for a launch from command-line, you need to get the parameter given by 
the user.
But how? Python provides a way to read the command-line arguments under an 
attribute called 'argv' of the 'sys' module.
argv is a list which zerost item is the name of the file. For instance if called
   python countdown.py 9
argv holds: ['countdown.py', '9']
Note that both are strings. Then you can catch and use the needed parameter, 
e.g.

from time import sleep as wait
from sys import argv as user_args

def countdown(n=10):
if n <= 0:
print 'Blastoff!'
else:
wait(0.333)
print n
countdown(n-1)

def launch():
if len(user_args) == 1:
countdown()
else:
n = int(user_args[1])
countdown(n)

if __name__ == "__main__":
launch()

(You can indeed put the content of launch() in the if block. But I find it 
clearer that way, and it happens to be a common practice.)

Denis
--
la vita e estrany
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Threading...

2009-04-27 Thread Kent Johnson
On Mon, Apr 27, 2009 at 2:46 AM, A.T.Hofkamp  wrote:
> you are making
> a IO-bound app, so threading doesn't buy you anything in performance and
> gives a big headache in data protection

Please explain. Presumably the single-threaded app is not IO-bound.
Adding threads can push it to the point of being IO-bound. The
requirements for communicating between threads in this app are modest
and not likely to cause a big headache.

Kent
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Threading...

2009-04-27 Thread A.T.Hofkamp

Kent Johnson wrote:

On Mon, Apr 27, 2009 at 2:46 AM, A.T.Hofkamp  wrote:

you are making
a IO-bound app, so threading doesn't buy you anything in performance and
gives a big headache in data protection


Please explain. Presumably the single-threaded app is not IO-bound.
Adding threads can push it to the point of being IO-bound. The


Euh, what?
Suppose I have a CPU-intensive task. Now by adding threads (ie more 
CPU-intensive tasks running at the same time) I can make it an IO-bound 
application?


Wow.
Let's call s...@home and climateprediction.net .



As far as I know, doing DB queries is an IO-bound process, since you must make 
a connection to the remote DB server, and send data back and forth to the 
server over the network (which is o compared to a calculation).
With threading, you will have more network connections that you are waiting 
for. W.r.t. performance you don't gain anything, since the CPU is already 
mostly idle.


> requirements for communicating between threads in this app are modest
> and not likely to cause a big headache.

With respect to the data problem, I don't know what the OP wants, but maybe he 
wants to run different tests at different times (ie first 1 minute test A, 
then 1 minute test B etc) or different mixes of queries. He may also want to 
collect performance results (avg x queries/second).
Such data is shared between the threads, so it must be protected to prevent it 
from getting corrupted.

Maybe it is modest in requirements, but you must do it none the less.

Also note that the protection itself may also become a bottle neck in the 
program (you must constantly claim and release locks which adds overhead even 
when there is no other thread waiting).




With Twisted, you can have at least the same number of IO connections as with 
threading, while you don't get the data protection problems and overhead for 
the simple reason that it is still a single-threaded program.




Maybe threading is the correct solution here. However the OP also asked for 
"...or is there another way to go about it?".
Twisted is definitely another way to go about it, and nobody mentioned it in 
previous posts, so I thought it correct to mention its existence here.



Sincerely,
Albert
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Threading...

2009-04-27 Thread Kent Johnson
On Mon, Apr 27, 2009 at 9:29 AM, A.T.Hofkamp  wrote:
> Kent Johnson wrote:
>>
>> On Mon, Apr 27, 2009 at 2:46 AM, A.T.Hofkamp  wrote:
>>>
>>> you are making
>>> a IO-bound app, so threading doesn't buy you anything in performance and
>>> gives a big headache in data protection

Perhaps I misinterpreted this. Do you mean "threading doesn't buy you
anything in performance" vs Twisted or vs his current solution? Vs
Twisted, I would agree. Vs single-threaded, I disagree, especially
given the OP's stated goal of opening a lot of concurrent connections.

>> Please explain. Presumably the single-threaded app is not IO-bound.
>> Adding threads can push it to the point of being IO-bound. The

> As far as I know, doing DB queries is an IO-bound process, since you must
> make a connection to the remote DB server, and send data back and forth to
> the server over the network (which is o compared to a
> calculation).

>From the point of view of the client, there is likely to be a fair
amount of dead time, waiting to establish a network connection,
waiting to establish a database connection, waiting for the db to do
it's thing. So my assumption is that the original, single-connection,
single-threaded client is neither IO- nor CPU-bound, it is spending at
least some time just waiting.

In this scenario, both threads and Twisted can help to increase the
load on the remote server by more fully utilizing the available IO
bandwidth. That is what I meant by using threads to create an IO-bound
app.

> With threading, you will have more network connections that you are waiting
> for. W.r.t. performance you don't gain anything, since the CPU is already
> mostly idle.

You will get more performance if the threaded app is able to use the
dead time in the IO. ISTM this directly addresses the OP. His stated
goal is "to throw a ton of open connections at" the DB. Certainly he
can do that using threads.

>> requirements for communicating between threads in this app are modest

> With Twisted, you can have at least the same number of IO connections as
> with threading, while you don't get the data protection problems and
> overhead for the simple reason that it is still a single-threaded program.
>
>
>
> Maybe threading is the correct solution here. However the OP also asked for
> "...or is there another way to go about it?".
> Twisted is definitely another way to go about it, and nobody mentioned it in
> previous posts, so I thought it correct to mention its existence here.

I don't dispute that at all.

Kent
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


[Tutor] How many types of the constructor

2009-04-27 Thread sudhanshu gautam
when we work on the oops in the python then we have to pass the first
parameter as a self then the value of the other parameters .

for example:
class Student:
def __init__(self,name):
self.name=name
print 'The name is',self.name

a=student('sudhanshu')
a is an object of the student class
so __init__ is a constructor in which we do not need to call the function
separately .

Now If I placed the name of the constructor rather than the __init__
__baba___ so will it also work as a constructor or constructor has specified
already if yes then give me list of them

ragards
sudhanshu
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] How many types of the constructor

2009-04-27 Thread Noufal Ibrahim

Greetings Sudanshu,

sudhanshu gautam wrote:
when we work on the oops in the python then we have to pass the first 
parameter as a self then the value of the other parameters .


Well, not explicitly. If you're familiar with OOPS in C++, self is 
analogous to the "this" pointer there. Also, you needn't call it self.


Writing something like

class A(object):
  def __init__(me, name):
 me.name = name
 print "The name is %s"%me.name

will work fine as well.

The actual identifier 'self' is just a convention.


for example:
class Student:
def __init__(self,name):
self.name =name
print 'The name is',self.name 

a=student('sudhanshu')
a is an object of the student class
so __init__ is a constructor in which we do not need to call the 
function separately .


__init__ is not *a* constructor as much as it is *the* constructor 
method. It is one of the many class methods that have special meanings.
If you want the entire list, try 
http://docs.python.org/reference/datamodel.html#special-method-names




Now If I placed the name of the constructor rather than the __init__
__baba___ so will it also work as a constructor or constructor has 
specified already if yes then give me list of them


__baba__ is not a special function so it will have no special 
significance. It is treated as any other class method.


I believe the URL above has what you're looking for.

Thanks

--
~noufal
http://nibrahim.net.in/
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] How many types of the constructor

2009-04-27 Thread Emile van Sebille

sudhanshu gautam wrote:



Now If I placed the name of the constructor rather than the __init__
__baba___ so will it also work as a constructor or constructor has 
specified already if yes then give me list of them


I'm not sure I parsed your question as you intended, but __init__ is 
pretty much the constructor method ( with __new__ )


But I suspect you may be asking about that group know as magic methods, 
which you may want to look into.  These include things like __getattr__, 
__len__, __gt__, and lots more.  These make it easy to do things like:


>>> class Test:
... def __init__(self):pass
... def __gt__(self,other): return True
...
>>> t = Test()
>>> t>3
True
>>> t>0
True
>>> t>"hello"
True
>>>

And you may even find out why the following work as well...

>>> t<"hello"
True
>>> t <3
True
>>>


Regards,

Emile

___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] How many types of the constructor

2009-04-27 Thread spir
Le Mon, 27 Apr 2009 22:38:30 +0530,
sudhanshu gautam  s'exprima ainsi:


> Now If I placed the name of the constructor rather than the __init__
> __baba___ so will it also work as a constructor or constructor has specified
> already if yes then give me list of them

Actually __init__ is not the constructor, which is __new__ (much less used), 
but the initialiser. If you try to do their work using other names, it may not 
work as expected because python does some magic in the background. Still, if 
you really know what you're doing it's possible for some of these special 
methods, while not for __new__ I guess. Anyway, it's maybe a bad idea cause 
conventional names help and understand the code.

> ragards
> sudhanshu

Denis
--
la vita e estrany
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] How to run a .py file or load a module?

2009-04-27 Thread Alan Gauld

Offlist response...

Nice answer Denis, you neatly covered all the loose ends from
previous answers that I was just about to pick up on! :-)

"spir"  wrote in message 
news:20090427101523.5fc09...@o...

Le Sun, 26 Apr 2009 22:35:36 +0100,
Dayo Adewunmi  s'exprima ainsi:


How can I

a) Open my shell, and do something like: $ python countdown.py
but have it take an argument and pass it to the function, and execute.


When your code is (nicely) organised as a set of funcs or class
definitions, you also need a "laucher" usually called "main()".


Only question is why, after saying it's usually called "main" that you
chose "launch"?! :-)

Alan G.



___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Lists in a file

2009-04-27 Thread David Holland
Thanks for all the suggestions.  I will have to try this.

--- On Sun, 26/4/09, Kent Johnson  wrote:

From: Kent Johnson 
Subject: Re: [Tutor] Lists in a file
To: "Robert Berman" 
Cc: "David Holland" , "tutor python" 

Date: Sunday, 26 April, 2009, 8:04 PM

On Sun, Apr 26, 2009 at 2:18 PM, Robert Berman  wrote:
> David,
>
> You are processing a text file. It is your job to treat it as a file
> comprised of lists. I think what you are saying is that each line in the
> text file is a list. In that case
>
> for line in fileobject:
>   listline = list(line)
>
> Now, listline is a list of the text items in line.

listline will be a list of all the characters in the line, which is
not what the OP wants.

If the list will just contain quoted strings that don't themselves
contain commas you can do something ad hoc:
In [2]: s = "['a','b','c']"

In [5]: [ str(item[1:-1]) for item in s[1:-1].split(',') ]
Out[5]: ['a', 'b', 'c']

In Python 3 you can use ast.literal_eval().
>>> import ast
>>> s = "['a','b','c']"
>>> ast.literal_eval(s)
['a', 'b', 'c']

You can use these recipes:
http://code.activestate.com/recipes/511473/
http://code.activestate.com/recipes/364469/

Kent



  ___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] How many types of the constructor

2009-04-27 Thread Alan Gauld

"sudhanshu gautam"  wrote


Now If I placed the name of the constructor rather than the __init__
__baba___ so will it also work as a constructor or constructor has 
specified

already if yes then give me list of them


Unlike some other OOP languages Python only has one constructor
(OK As Denis pointed out it has the pair of new and init). It does not
allow you to define multiple different constructors with different 
argument

lists.

We can fake this behaviour by defining the constructor to take an arbitrary
list of arguments and then based on the length of that list calling 
different

methods, something like (untested):

class C:
   def __init__(this, *args):
if len(args) == 0:
this._defaultConstructor()
elif len(args) == 1:
this._oneArg(args[0])
elif len(args) == 2:
this._twoArgs(args[0],args[1])
else: raise ValueError

   def _default(this): print 'default'
   def _oneArg(this, a1): print "Arg - ", a1
   def _ twoArgs(this, a1,a2): print "Args - " a1, a2

c0 = C()  # -> default
c1 = C('foo')   # -> Arg - foo
c2 = C('bar', 42)# -> Args - bar 42
c3 = C(1,2,3) # ->  error

The single _ is just a convention to indicate a "private"
method, that is one not intended for direct use

We can also use named arguments and default values
to achieve similar functionality. This is how the Widgets
in most GUIs work. You call the widget with a specifically
named subset of parameters. For example:

from Tkinter import *
top = Tk()
Label(top, text="OK", foreground="Blue).pack()
Label(top, text="Cancel").pack()
top.mainloop()

The second label just adopts the default colour scheme.

Hopefully this answers your question.

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


___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] How to run a .py file or load a module?

2009-04-27 Thread Alan Gauld


"Alan Gauld"  wrote 


Offlist response...


Oops, not offlist! Just as well I didn't say anything offensive! :-)

Alan G

___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Working with lines from file and printing to another keeping sequential order

2009-04-27 Thread Dan Liang
Hi Bob, Shantanoo, Kent, and tutors,

Thank you Bob, Shantanoo, Kent for all the nice feedback. Exception
handling, the concept of states in cs, and the use of the for loop with
offset helped a lot. Here is the code I now have, based on your suggestions,
and it does what I need:

ListLines = [ line.rstrip() for line in open('test.txt') ]

countYes = 0
countNo = 0

for i in range(len(ListLines)):
 if ListLines[i].endswith('yes'):
 countYes+=1
 print "countYes", countYes, "\t\t", ListLines[i]

 if not ListLines[i].endswith('yes'):
continue

 for offset in (1, 2, 3, 4, 5, 6, 7, 8):
if i+offset < len(ListLines) and ListLines[i+offset].endswith('no'):

   countNo+=1

   print "countNo", countNo, "\t\t", ListLines[i+offset]

Thank you again!

--dan





On Sun, Apr 26, 2009 at 10:55 AM, Kent Johnson  wrote:

> On Sat, Apr 25, 2009 at 2:11 PM, Dan Liang  wrote:
> > Hi Bob and tutors,
> >
> > Thanks Bob for your response! currently I have the current code, but it
> does
> > not work:
> >
> > ListLines= []
> > for line in open('test.txt'):
> > line = line.rstrip()
> > ListLines.append(line)
>
> This could be written with a list comprehension:
> ListLines = [ line.rstrip() for line in open('test.txt') ]
>
> >
> > for i in range(len(ListLines)):
> >
> > if ListLines[i].endswith("yes") and ListLines[i+1].endswith("no") and
> > ListLines[i+1].endswith("no"):
> > print ListLines[i], ListLines[i+1], ListLines[i+2]
> > elif ListLines[i].endswith("yes") and ListLines[i+1].endswith("no"):
> > print ListLines[i], ListLines[i+1]
> > elif ListLines[i].endswith("yes"):
> > print ListLines[i]
> > elif ListLines[i].endswith("no"):
> > continue
> > else:
> > break
>
> You only need to test for ListLines[i].endswith('yes') once. Then you
> could use a loop to test for lines ending with 'no'.
>
> for i in range(len(ListLines)):
>   if not ListLines[i].endswith('yes'):
>continue
>  for offset in (1, 2):
>if i+offset < len(ListLines) and ListLines[i+offset].endswith('no'):
>  print ListLines[i+offset]
>
> You could adapt the above for a variable number of 'no' lines with
> something like
>  for offset in range(1, maxNo+1):
>
> > I get the following error:
> > Traceback (most recent call last):
> >   File "test.py", line 18, in 
> > if ListLines[i].endswith("yes") and ListLines[i+1].endswith("no") and
> > ListLines[i+1].endswith("no"):
> > IndexError: list index out of range
>
> That is because you have a 'yes' line at the end of the file so the
> check for 'no' tries to read past the end of ListLines. In my code the
> test for i+offset < len(ListLines) will prevent that exception.
>
> Kent
>
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor