Re: [Tutor] while/if/elif/else loops

2005-11-01 Thread Norman Silverstone

> #Coin Toss Game
> 
> print "This game will simulate 100 coin tosses and then tell you the
> number of head's and tails"
> 
> import random
> 
> tosses = 0
> heads = 0
> tails = 0
> 
> while tosses = 100<0:
>coin = randrange(1)
>tosses +=1
>if coin == 0:
>   heads +=1
>   print "Heads"
> else:
>   tails +=1
>   Print "Tails"

This problem was covered only a few days ago and I suggest the replies
are looked up. Incidentally coin = random.randrange(1) can surely only
return 0 and, if one is to look at a random result between heads and
tails then coin should = random.randrange(2).

Norman

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


Re: [Tutor] Random Q on Python's internals?

2005-11-01 Thread Liam Clarke
Thanks Danny.


On 11/1/05, Danny Yoo <[EMAIL PROTECTED]> wrote:
>
>
> > I was perusing the standard library, and was wondering if anyone knew
> > how the internals of Python work, roughly or no.
> >
> > Basically, I assume os.pipe() resides in a DLL somewhere, yet when I
> > open up Python24.DLL in PEexplorer I can't find mention of pipes
> > anywhere...
>
> Hi Liam,
>
>
> os.pipe() comes from the block of code near the top of os.py:
>
> http://cvs.sourceforge.net/viewcvs.py/python/python/dist/src/Lib/os.py?rev=1.58.2.4&view=markup
>
> where it starts to talk to platform-specific modules like 'nt' and
> 'posix'.  The 'os.py' module is a sponge that absorbs the content of
> platform-specific modules in an attempt to make things look platform
> independent.  *grin*
>
> pipe() really comes from the posix/nt module, whose implementation can be
> found here:
>
> http://cvs.sourceforge.net/viewcvs.py/python/python/dist/src/Modules/posixmodule.c?view=markup
>
>
> Best of wishes!
>
>
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Turning kwargs into scalars

2005-11-01 Thread bob
At 08:52 PM 10/31/2005, Steve Bergman wrote:
>Say I have a function:
>def f(self, **kwargs) :

FWIW you don't have a function at this point. You have a def statement 
which must be followed by at least one indented statement, which in turn 
must be executed. Then you have a function.

>and I want to take the key/value pairs and create a set of variables with 
>the names of the keys.
>For example, if I say:
>f(x=5, y=2)
>I want to create local variables 'x' and 'y' in the function, with values 
>of 5 and 2 respectively.
>How could I do this?

This is an FAQ. It can be done. However there are better ways to accomplish 
the same thing. Namely use the dictionary kwargs and look things up by key.

Would you give an example of what you'd do with variables x and y after 
creating them? 

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


Re: [Tutor] Rename files with numbers

2005-11-01 Thread Ed Singleton
Sorry, wrong list.

Many apologies.

Ed

On 01/11/05, Ed Singleton <[EMAIL PROTECTED]> wrote:
> The best free app I've found for this is MusicBrainz [www.musicbrainz.com].
>
> This has a huge database of obsessively correct details of albums
> which can be formatted in anyway you choose.  It can automatically
> recognise which song an MP3 is!
>
> This is a similar script I wrote to renumber files in sequential
> order.  It assumes that everything before the first underscore can be
> replaced.
>
> from path import path
> import re
>
> def renumberfiles(filesdir, startnum=1):
> d = path(filesdir)
> print d
> x = startnum
> for f in d.files():
> fname = f.name.split('_', 1)
> fname = str(x).zfill(2) + "_" + fname[-1]
> fname = re.sub(r" ",r"_",fname)
> fname = re.sub(r"__",r"_",fname)
> x = x + 1
> print f.name, "==>",
> f.rename(f.parent + "\\" + fname)
> print fname
>
> As you can see, I use the path module rather than os.path.  it's a
> much more intuitive way of handling files.
>
> http://www.jorendorff.com/articles/python/path/
>
> Ed
>
> On 01/11/05, Dave Benjamin <[EMAIL PROTECTED]> wrote:
> > On Mon, 31 Oct 2005, Micah Elliott wrote:
> >
> > > On Oct 31, Micah Elliott wrote:
> > >> Now I need to go beautify my collection.  :-)
> > >
> > > While a fun exercise, there are probably already dozens (or
> > > thousands?) of utilities in existence that do this and much more.
> >
> > Seconded. I initially considered writing a script to rename a huge pile of
> > MP3 files, but halfway through, I thought, "there's *got* to be a better
> > way". I found this app: http://www.softpointer.com/tr.htm
> >
> > Bought it the next day. Holy crap, what a gigantic timesaver. It looks up
> > albums based on track lengths, downloads titles from freedb and Amazon,
> > does ID3 tagging, renaming, moves files into separate directories... I
> > busted through about 20 gigs of MP3s in three days. I kid you not.
> >
> > If this is just a fun toy Python project, don't let me discourage you, but
> > if you have more than a handful of albums to rename, trust me. This
> > problem has been solved.
> >
> > Here's a list of apps, including Tag&Rename, that can query freedb:
> > http://www.freedb.org/freedb_aware_apps.php
> >
> > --
> >   .:[ dave benjamin: ramen/[sp00] ]:.
> >\\ "who will clean out my Inbox after I'm dead[?]" - charles petzold
> > --
> > http://mail.python.org/mailman/listinfo/python-list
> >
>
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Rename files with numbers

2005-11-01 Thread Ed Singleton
The best free app I've found for this is MusicBrainz [www.musicbrainz.com].

This has a huge database of obsessively correct details of albums
which can be formatted in anyway you choose.  It can automatically
recognise which song an MP3 is!

This is a similar script I wrote to renumber files in sequential
order.  It assumes that everything before the first underscore can be
replaced.

from path import path
import re

def renumberfiles(filesdir, startnum=1):
d = path(filesdir)
print d
x = startnum
for f in d.files():
fname = f.name.split('_', 1)
fname = str(x).zfill(2) + "_" + fname[-1]
fname = re.sub(r" ",r"_",fname)
fname = re.sub(r"__",r"_",fname)
x = x + 1
print f.name, "==>",
f.rename(f.parent + "\\" + fname)
print fname

As you can see, I use the path module rather than os.path.  it's a
much more intuitive way of handling files.

http://www.jorendorff.com/articles/python/path/

Ed

On 01/11/05, Dave Benjamin <[EMAIL PROTECTED]> wrote:
> On Mon, 31 Oct 2005, Micah Elliott wrote:
>
> > On Oct 31, Micah Elliott wrote:
> >> Now I need to go beautify my collection.  :-)
> >
> > While a fun exercise, there are probably already dozens (or
> > thousands?) of utilities in existence that do this and much more.
>
> Seconded. I initially considered writing a script to rename a huge pile of
> MP3 files, but halfway through, I thought, "there's *got* to be a better
> way". I found this app: http://www.softpointer.com/tr.htm
>
> Bought it the next day. Holy crap, what a gigantic timesaver. It looks up
> albums based on track lengths, downloads titles from freedb and Amazon,
> does ID3 tagging, renaming, moves files into separate directories... I
> busted through about 20 gigs of MP3s in three days. I kid you not.
>
> If this is just a fun toy Python project, don't let me discourage you, but
> if you have more than a handful of albums to rename, trust me. This
> problem has been solved.
>
> Here's a list of apps, including Tag&Rename, that can query freedb:
> http://www.freedb.org/freedb_aware_apps.php
>
> --
>   .:[ dave benjamin: ramen/[sp00] ]:.
>\\ "who will clean out my Inbox after I'm dead[?]" - charles petzold
> --
> http://mail.python.org/mailman/listinfo/python-list
>
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Talking to UDPServer

2005-11-01 Thread Carroll, Barry








Johan, et al:

 

Yes, that is exactly what I want.  I
need to write a program that communicates with an existing server, using the
UDP protocol.  This is my first time writing such a program, and I need
help getting started.  

 

Thanks again.  

 

Barry

 











From: Johan Geldenhuys [mailto:[EMAIL PROTECTED] 
Sent: Monday, October 31, 2005
5:35 PM
To: Carroll, Barry
Cc: 'tutor@python.org'
Subject: Re: [Tutor] Talking to
UDPServer



 

Maybe you could tel us if you already have the server
listening on the socket that you expec connections on? If, yes, do you want an
example of how a client connects to that socket?

Johan

Carroll, Barry wrote: 

Greetings:

 

I am writing a browser-based interface to a server
program which extends SocketServer.UDPServer.  The program listens on a
well-known socket, receiving commands, verifying them and using their content
to drive our test hardware, returning status to the client.  The current
client interface is a command line interface that allows the user to type in a
command and sends it to the server, receives the status response and displays
it for the user.  My job is to duplicate the client functionality in a set
of web pages.  

 

My problem is that, while I can find documentation on
UDPServer, I cannot find anything on how a remote client talks to the
server.  How is this done in Python?  Can someone point me to a
how-to, tutorial or the like?  

 

Thanks in advance.

 

Barry

 

 

 

 



 ___Tutor maillist  -  Tutor@python.orghttp://mail.python.org/mailman/listinfo/tutor  






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


Re: [Tutor] while/if/elif/else loops

2005-11-01 Thread Carroll, Barry
Greetings:

I tried running first just the expression, then the statement.  Here are the
results:

>>> tosses = 100<0
>>> tosses
False
>>> while tosses = 100<0:
  File "", line 1
while tosses = 100<0:
 ^
SyntaxError: invalid syntax
>>>

Coming from the C/C++ world as I do,  I was a little surprised by the error.
C accepts the assignment expression and uses the result as the while
condition, so that the loop never executes.  Nice to see that Python avoids
that trap.

Regards,

Barry

> Date: Mon, 31 Oct 2005 22:10:34 -0500
> From: "R. Alan Monroe" <[EMAIL PROTECTED]>
> Subject: Re: [Tutor] while/if/elif/else loops
> To: tutor@python.org
> Message-ID: <[EMAIL PROTECTED]>
> Content-Type: text/plain; charset=us-ascii
> 
> 
> > while tosses = 100<0:
> 
> I didn't run the program, but this immediately caught my eye as
> looking kind of suspect. Was it a typo? Most programs usually
> have a need for comparisons of equal, or greater/less, but it's really
> rare to need both combined in one statement...
> 
> Also if you really _do_ want to check two things being equal, don't
> forget to use double equal ( == ).
> 
> Alan
> 

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


Re: [Tutor] Talking to UDPServer

2005-11-01 Thread Kent Johnson
Carroll, Barry wrote:
> Yes, that is exactly what I want.  I need to write a program that 
> communicates with an existing server, using the UDP protocol.  This is 
> my first time writing such a program, and I need help getting started. 

Here is an example from Python Network Programming, by John Goerzen. It opens a 
UDP port, sends a message, then echoes any received text to the console. There 
is no higher-level support built in to Python, you just open a datagram socket 
and push data out. Google for "Python udp" for more examples.

Kent

#!/usr/bin/env python
# UDP Example - Chapter 2

import socket, sys, time

host = sys.argv[1]
textport = sys.argv[2]

s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
try:
port = int(textport)
except ValueError:
# That didn't work.  Look it up instread.
port = socket.getservbyname(textport, 'udp')

s.connect((host, port))
print "Enter data to transmit: "
data = sys.stdin.readline().strip()
s.sendall(data)
s.shutdown(1)
print "Looking for replies; press Ctrl-C or Ctrl-Break to stop."
while 1:
buf = s.recv(2048)
if not len(buf):
break
print "Received: %s" % buf

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


Re: [Tutor] Talking to UDPServer

2005-11-01 Thread Johan Geldenhuys
I've done some network programming mostly with TCP and I don't think 
that the way the client connects to the server is a lot different (if 
any), The only difference is when you must decide the protcol family. 
"socket.SOCK_DGRAM" will be for UDP and "socket.SOCK_STREAM" will be for 
TCP. After this, the client can connect the same way.

Here is a simpler sample than the one Kent gave:

"""
from socket import *

HOST = 'localhost'
PORT = 3001
BUFSIZ = 1024
ADDR = (HOST, PORT)

tcpCliSock = socket(AF_INET, SOCK_STREAM) # change here for UDP
tcpCliSock.connect(ADDR)

while 1:
data = raw_input('>') # Enter text to be transmitted to the server.
if not data: break
tcpClisock.send(data)
data = tcpCliSock.recv(BUFSIZ)
if not data: break
print data
   
tcpCliSock.close()
"""
HTH,

Johan


Kent Johnson wrote:

> Carroll, Barry wrote:
>
>> Yes, that is exactly what I want.  I need to write a program that 
>> communicates with an existing server, using the UDP protocol.  This 
>> is my first time writing such a program, and I need help getting 
>> started. 
>
>
> Here is an example from Python Network Programming, by John Goerzen. 
> It opens a UDP port, sends a message, then echoes any received text to 
> the console. There is no higher-level support built in to Python, you 
> just open a datagram socket and push data out. Google for "Python udp" 
> for more examples.
>
> Kent
>
> #!/usr/bin/env python
> # UDP Example - Chapter 2
>
> import socket, sys, time
>
> host = sys.argv[1]
> textport = sys.argv[2]
>
> s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
> try:
>port = int(textport)
> except ValueError:
># That didn't work.  Look it up instread.
>port = socket.getservbyname(textport, 'udp')
>
> s.connect((host, port))
> print "Enter data to transmit: "
> data = sys.stdin.readline().strip()
> s.sendall(data)
> s.shutdown(1)
> print "Looking for replies; press Ctrl-C or Ctrl-Break to stop."
> while 1:
>buf = s.recv(2048)
>if not len(buf):
>break
>print "Received: %s" % buf
>
>
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


[Tutor] Does a module for DirectFB exist?

2005-11-01 Thread Roger Merchberger
DirectFB is short for Direct Frame Buffer, and allows access to a graphical 
frame buffer system in *nix (and I think maybe MacOSX) from a text prompt 
without going through X.

Anyone know of a module to access this through Python? I've googled for it, 
but didn't know if anyone here knew about something along these lines.

Thanks,
Roger "Merch" Merchberger

--
Roger "Merch" Merchberger   | A new truth in advertising slogan
SysAdmin, Iceberg Computers | for MicroSoft: "We're not the oxy...
[EMAIL PROTECTED]  | ...in oxymoron!"

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


Re: [Tutor] Talking to UDPServer

2005-11-01 Thread Carroll, Barry
Kent, Johan:

Thank you.  These examples will be a great help.  I also found some links
via Google (I always forget that resource for some reason).  I have enough
to go forward, now.  

Barry


> -Original Message-
> From: Johan Geldenhuys [mailto:[EMAIL PROTECTED]
> Sent: Tuesday, November 01, 2005 11:39 AM
> To: Kent Johnson
> Cc: Carroll, Barry; 'tutor@python.org'
> Subject: Re: [Tutor] Talking to UDPServer
> 
> I've done some network programming mostly with TCP and I don't think
> that the way the client connects to the server is a lot different (if
> any), The only difference is when you must decide the protcol family.
> "socket.SOCK_DGRAM" will be for UDP and "socket.SOCK_STREAM" will be for
> TCP. After this, the client can connect the same way.
> 
> Here is a simpler sample than the one Kent gave:
> 
> """
> from socket import *
> 
> HOST = 'localhost'
> PORT = 3001
> BUFSIZ = 1024
> ADDR = (HOST, PORT)
> 
> tcpCliSock = socket(AF_INET, SOCK_STREAM) # change here for UDP
> tcpCliSock.connect(ADDR)
> 
> while 1:
> data = raw_input('>') # Enter text to be transmitted to the server.
> if not data: break
> tcpClisock.send(data)
> data = tcpCliSock.recv(BUFSIZ)
> if not data: break
> print data
> 
> tcpCliSock.close()
> """
> HTH,
> 
> Johan
> 
> 
> Kent Johnson wrote:
> 
> > Carroll, Barry wrote:
> >
> >> Yes, that is exactly what I want.  I need to write a program that
> >> communicates with an existing server, using the UDP protocol.  This
> >> is my first time writing such a program, and I need help getting
> >> started.
> >
> >
> > Here is an example from Python Network Programming, by John Goerzen.
> > It opens a UDP port, sends a message, then echoes any received text to
> > the console. There is no higher-level support built in to Python, you
> > just open a datagram socket and push data out. Google for "Python udp"
> > for more examples.
> >
> > Kent
> >
> > #!/usr/bin/env python
> > # UDP Example - Chapter 2
> >
> > import socket, sys, time
> >
> > host = sys.argv[1]
> > textport = sys.argv[2]
> >
> > s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
> > try:
> >port = int(textport)
> > except ValueError:
> ># That didn't work.  Look it up instread.
> >port = socket.getservbyname(textport, 'udp')
> >
> > s.connect((host, port))
> > print "Enter data to transmit: "
> > data = sys.stdin.readline().strip()
> > s.sendall(data)
> > s.shutdown(1)
> > print "Looking for replies; press Ctrl-C or Ctrl-Break to stop."
> > while 1:
> >buf = s.recv(2048)
> >if not len(buf):
> >break
> >print "Received: %s" % buf
> >
> >

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


[Tutor] Python and sock

2005-11-01 Thread ciclope
Hi, I have a question about the use of a sock server under python. 
I wanna a little program I'm writing to use a sock server, either version 4 or 5.

 How can I make it communicating through a such SOCKS server under
python, without using a SOCKScap like software, but easily using
something like a socket module that implements an interface to those
servers? I found a very old implementation for python 1.x (If I
remember correctly) but obviously it doesnt work for me.

I know I can socksify the sourcecode of the socket module, but before doing this I would know if there is already something.

Thanks in Advance

PS: sorry for the English, but it isnt my mother language ;-P

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


Re: [Tutor] while/if/elif/else loops

2005-11-01 Thread Zameer Manji
Ok after looking at everyones replies my program looks like this:

#Coin Toss Game
#Zameer Manji
import random

print "This game will simulate 100 coin tosses and then tell you the
number of head's and tails"

tosses = 0
heads = 0
tails = 0

while tosses<100:
if tosses<100:
coin = random.randrange(2)
tosses +=1
if coin == 0:
heads +=1
print "Heads"
else:
tails +=1
print "Tails"
else:
print "100 tosses have been simulated. Please wait for your results"

print "Out of", tosses, ",", heads, "were heads and", tails, "were tails."


This is the results:

>>>  RESTART

>>>
This game will simulate 100 coin tosses and then tell you the number of
head's and tails
Heads
...  (To save space the results have been stripped)
Heads
Out of 100 , 48 were heads and 52 were tails.

Thank you for your help everyone.
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor