Re: [Tutor] Firefox Bookmarks

2008-08-14 Thread Hugo González Monteverde


Are there any modules I should know about? Specifically How can I 
read/write from places.sqlite?


Hi,

pysqlite3 is the python wrapper for the SQLite libs. It is included with 
Python 2.5 and newer.


Docs are at 
http://oss.itsystementwicklung.de/download/pysqlite/doc/usage-guide.html


Have fun

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


Re: [Tutor] os.path.exists(path) returns false when the path actually exists!

2007-07-28 Thread Hugo González Monteverde
Iyer wrote:
 
 Adam wrote:
 
  From the library documentation:
 Return True if path refers to an existing path. Returns False for
 broken symbolic links. On some platforms, this function may return
 False if permission is not granted to execute os.stat() on the
 requested file, even if the path physically exists.
 
 So the better question is, does is this file a broken symbolic link or
 can os.stat() be executed on it?
 
 
 How do I find if it is a broken symbolic link in Windows 2000 ?
 
 os.stat(path) returns an OSError saying that there is no such file or 
 directory

Just to check, try to do away with the backslashes. Windows will accept 
a path with forward slashes just as well:

os.path.exists(c:/winnt/file_name)

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


Re: [Tutor] Python Browser based?

2007-04-17 Thread Hugo González Monteverde
Hi,

Do you want to:

1) use Python from a website or server without installing it in a computer?

2) use Python to do things with the Internet or servers, website, etc?

Hugo

[EMAIL PROTECTED] wrote:
 How can I used python online.  I'm getting my hoster to install python and
 I'm wondering how Do I use python online?
 ___
 Tutor maillist  -  Tutor@python.org
 http://mail.python.org/mailman/listinfo/tuto
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] File storage vs DB storage.

2007-04-10 Thread Hugo González Monteverde
Hi,

Managaing a lot of files is not a nightmare if you use/build the right 
tools.

Why not saving filenames in the DB and then just using the ordinary web 
server to deliver them? Getting the blob out of the DB and then serving 
it through the web app is going to be a lot slower than letting, say, 
Apache do it.

I wrote a system that kept 15000 files in a directory tree. You just let 
your software deal with them

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


Re: [Tutor] Movies from jpg files

2007-04-10 Thread Hugo González Monteverde
János Juhász wrote:

 
 Thanks your response.
 It should works well with some images, but I would do it with a scheduled 
 way
 with some 1000 files.

Hi,

I previously have used python to control mencoder. Mencoder does the 
combining and putting together into a video.

You script the arguments and the command line switches to mencoder. 
Maybe use the subprocess module? or the commands module for very 
simple tasks?

http://www.mplayerhq.hu/
http://en.wikipedia.org/wiki/MEncoder

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


Re: [Tutor] how to run a text file in an interpreter?

2007-04-09 Thread Hugo González Monteverde
Hi Tonu,

Tonu Mikk wrote:
 I do not know what the author means by running a text file with the 
 interpreter. I noticed that I came across an obstacle early on in trying 
 out the code.

What plattform are you in? Linux, Windows, Mac?

Check out the faqs at:

http://www.python.org/doc/faq/windows/#how-do-i-run-a-python-program-under-windows

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


Re: [Tutor] Quicktime Python

2007-03-13 Thread Hugo González Monteverde
Miguel Oliveira, Jr. wrote:
 Hello,
 
 Just wondering: is there a way to play quicktime .mov files from  
 python? I am trying to run an experiment and would like to have  
 Python to play the .mov files I have in a given sequence (or in  
 random), in full screen and to record a log of the files that were  
 played, the order and the time. Any help will be very much appreciated.
 

I would use mplayer. You can control mplayer through a simple text 
interface called slave mode and log what you send to it.

I've got some code you could use, using the subprocess module if you 
want it.

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


Re: [Tutor] IOError: decoder jpeg not available

2007-02-28 Thread Hugo González Monteverde
Hi,

It looks like there is a capability you don't have installed in your 
image processing lib. but you really give us nothing. Please tell us:

  - Was this working before in any other operating system or python version?
  - What module are you using for working with jpeg?
  - Show us the code that throws this exception, or how you use it..

And maybe we'll have some info to help you out.


Tsila Hassine wrote:
 Dear fellow Pythoneers,
 
 I have recently upgraded to Mac 10.4, and since then this error appears 
 when trying to manipulate an image (resize it actually),
 can anyone help me out ?
 
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] howto call DOM with python

2007-02-28 Thread Hugo González Monteverde
I too wish it worked. In javascript, it works because the browser has a 
Javascript implementation. There's no stock browser with a Python 
implementation, and I've looked for any client side implementation with 
no success.

Hugo

Ismael Farfán Estrada wrote:
 hi there
 
 I was wondering wheter someone knows how to use python to write
 a client-side scripts like if it were java, I want to get someting like
 this working
 html
 script type=text/python
 window.alert(python)
 /script
 /html
 
 but nothing happens. I don't know javascript but this code works
 if I put javascript instead of python, I've also tried with pythonscript
 python-source, etc
 
 hope you can help me, I'm using GNU/Suse, Opera, and Firefox for test
 
 _
 El mejor destino, con los mejores contenidos http://www.prodigy.msn.com
 
 ___
 Tutor maillist  -  Tutor@python.org
 http://mail.python.org/mailman/listinfo/tutor
 
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Running an exe from Python

2007-02-22 Thread Hugo González Monteverde
Hi,

Take a lok=ok at the module named subprocess. You can catch the output 
of invoked programs with this.


There is also a module called commands but it its not available in 
windows.


Hugo

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


Re: [Tutor] Python code to connect using PPPoE

2007-01-26 Thread Hugo González Monteverde
Hi Johan,

PPPoE is both in the Linux Kernel (for the low level work) and as some 
executables and scripts.

It fou want to establish a connection from a Python script (E.G. ehn the 
computer has no internet access and you need it) The best way would be 
to call the appropriate utilities, and parse their output. This means 
calling adsl-connect, ficonfig, and the like, maybe parsing the output 
from ifconfig. Take a look at the module named subprocess to do that.

Reimplementing these utilities in Python is some work, but doable. 
Implementing PPPoE in Python, reading the device files and all that... 
well... it's almost nonsense. For this, you have to use the kernel.

Johan Geldenhuys wrote:
 Kent,
 I want to establish the connection with Python. I think in Linux you can use
 a PPPoE package to make life easier, but I don't want to use the Kernel to
 do that.
 
 Johan 
 

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


Re: [Tutor] Cause-Effect, Isikawa, fishbone diagram

2007-01-26 Thread Hugo González Monteverde
Hi János,

Reportlab is a very complete PDF library, but it is not very simple, and 
does not have shortcuts for what you need to do. You'd have to come up 
with the code to get a fishbone diagram from the textfile. But it does 
help you with the PDF.

There are some graphing packages, but they're most for scientific 
graphs. Like http://matplotlib.sourceforge.net/

Hope that helps,

Hugo

János Juhász wrote:
 Dear All,
 
 does someone know any python based solution to draw a 
 cause-effect diagram into PDF from a simple textfile ?
 
 It is also called a Fishbone Diagram, because of its shape, 
 or an Ishikawa Chart, after its originator, Kaoru Ishikawa
 I feel, it can be converted from a structure like this.
 
 Title
 Effect
   Cause1
 Secundary
   Tertiary
   Tertiary
   Cause2
 Secundary
   Tertiary
   Cause3
 Secundary
   Tertiary
 
 It is probably a simple function.
 
 
 
 Yours sincerely,
 __
 Janos Juhasz
 ___
 Tutor maillist  -  Tutor@python.org
 http://mail.python.org/mailman/listinfo/tutor
 
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Perfect Python web stack?

2007-01-18 Thread Hugo González Monteverde
Hi,

Check the thread in the archive. This has been discussed recently.

I have been exploring Django, and since I don't want to go into SQL now, 
I like it. It generates DB code from your model and you don't have to do 
  SQL. It helps to do your app FAST!

But that's just my opinion.

Hugo

OkaMthembo wrote:
 Please help me to decide. I might have asked some of you before, so 
 please bear with me.
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] python web dev

2007-01-11 Thread Hugo González Monteverde
OkaMthembo wrote:

 this is my first post. please could you tell me which is the best 
 lightweight python web framework? also, which is the best templating 
 language for python? (which can handle other data formats in addition to 
 text). so far im lured by Cheetah, although i havent done any web dev 
 with python yet.

If you really want to go light, you may want to try Albatross, as others 
like Django are pretty heavy.

Hugo

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


Re: [Tutor] problems pickling functions

2007-01-11 Thread Hugo González Monteverde
Arild B. Næss wrote:
 I haven't found out how to change the working directory in IDLE,  
 though – and for some reason it seems to be a different one this  
 session from the last one. Does anyone know?
   (I use a mac by the way.)


take a look at os.chdir()

This changes the interpreter's working dir (and thus whatever you're 
writing into the prompt in IDLE)

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


Re: [Tutor] XML-RPC data transfers.

2007-01-11 Thread Hugo González Monteverde
Luke Paireepinart wrote:
   But the main strategy is to get the data out of the ImageGrab object.
 one way is stated above - use the save method to write to a file.
 another possible way is to create a filelike class, implementing 'tell' 
 'seek' and 'write' methods, that just collects all the data written to 
 it and keeps it in memory
 rather than writing it out to a file.
 This is hinted at in the documentation for im.save():
 You can use a file object instead of a filename. In this case, you must 
 always specify the format. The file object must implement the *seek*, 
 *tell*, and *write* methods, and be opened in binary mode.
 (I assume it means you can use a file-like object, also, but it's 
 possible that you can't, and I leave it to you to test that :) )


hmmm StringIO module maybe?

Hugo

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


Re: [Tutor] Python and rpy

2007-01-11 Thread Hugo González Monteverde
Geoframer wrote:
 
 However i switched to Ubuntu 6.10 today (from WinXP) and to my suprise 
 it does work under linux! :-)

Probably Numeric is included in Ubuntu's  Python distro.

Hugo

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


Re: [Tutor] is gotchas?

2007-01-08 Thread Hugo González Monteverde

   Hmmm! Hmmm!
   Lookee here:
   ## console session
   a=[1,2]
   b=[1,2]
   a is b
 False
   c='1'  ## one byte
   d='1'  ## one byte
   c is d
 True
   c='1,2'
   d='1,2'
   c is d
 False
 
 The Hmmm! is emmitted because I'm thinking that if everything is an
 object in python, then why does `c is d` evaluate to True when
 the assigned value is 1 byte and evaluate to False when the assigned
 value is more that 1 byte?

One and two byte strings are currently optimized in cPython as the same 
object, referenced multiple times.

Note that this is not to be relied upon! Jython, Ironpython, Python3000 
or cPython itself may break it!

Still cannot find a reference doc for this

Hugo

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


Re: [Tutor] How to read content in a tar file with tarfile module

2006-10-10 Thread Hugo González Monteverde
Magnus Wirström wrote:
 I have written a app that makes a tar file and all works well... Now i 
 want to expand that app so it can read read the tar and give me the 
 contents of the tar file. How is the best way to do this ? I can't find 
 a listdir like function in tarfile. Can anyone point me in the right 
 direction?

Hi, is this what you're looking for?? (tarfile Object docs)

getmembers( )

Return the members of the archive as a list of TarInfo objects. The list 
has the same order as the members in the archive.

getnames( )

Return the members as a list of their names. It has the same order as 
the list returned by getmembers().

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


Re: [Tutor] question about looping.

2006-10-09 Thread Hugo González Monteverde
Doug Potter wrote:

 for i in routers:
 os.system('/bin/touch' %s) % i
 
 of course this dosn't work.
 
 Is there a simple way to get this done?

Yep, someone already answered that you can create the file in write mode 
and then close it, but I wanted to add a little more in why your 
solution doesn't work.

You're not interpolating the filename INTO the os.system string


The following would work

  for i in routers:
  os.system('/bin/touch %s'%1)

Granted, you should also provide a filename with a full path for simplicity.

HTH

Hugo

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


Re: [Tutor] revisiting struct and bytes again.

2006-10-09 Thread Hugo González Monteverde

 i can make a byte a string or number or whatever now. Up to the number 255.
 I now have a problem with something that comes in as a 4 byte 
 representation of
 the number of seconds since 1970.

Hi Shawn,

That's an epoch time as used in UNIX. Are you sure that what you're 
trying to do cannot be done with the Python time module?

What is it exactly that you're trying to do?

Hugo

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


Re: [Tutor] omnicomplete vim python

2006-08-28 Thread Hugo González Monteverde


anil maran wrote:
 do u guys know how to enable omnicomplete using vim
 for python thanks

Google is your friend, here's a page:

http://www.vim.org/tips/tip.php?tip_id=1311

Actually, I didn't ven know this existed.

Hugo

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


Re: [Tutor] python

2006-08-08 Thread Hugo González Monteverde
Hi David,

Have you read the grat explanation in Alan's tutorial? What problem 
specifically are you having with functions?

http://www.freenetpages.co.uk/hp/alan.gauld/

Get back to us with any questions...

David Wilson wrote:
 can you explain functions in python to me and give me some examples
 
 ___
 Tutor maillist  -  Tutor@python.org
 http://mail.python.org/mailman/listinfo/tutor
 
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Is there a method like this already?

2006-07-24 Thread Hugo González Monteverde
Nathan Pinno wrote:
 Is there a method in Python like this already:
  
 [code]
 #This program calculates how many days it has been from one day to the 
 other.

Hi,

Check the datetime module, it will give you distance between dates in 
the units you specify. It is included in the standard distribution.

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


Re: [Tutor] OT: any good newsgroup or other forum for MP3 internals discussions?

2006-05-22 Thread Hugo González Monteverde
Terry Carroll wrote:
 I apologize for this off-topic question -- the only real connection to 
 Python is that I'm using Python as my programming language.
 
 I'm writing a program to analyze an MP3 file's contents.  I need to 
 find a newsgroup or other forum to ask some questions about file 
 format internals.  Nothing's jumping out at me, looking through the 
 list of usenet newsgroups.
 
 I've found dozens of sites with relevant specifications, but am looking at
 one or two rogue files where either I'm misunderstanding the specs, or the
 file is nonconformant (but some MP3 programs can read them, so I'm betting
 it's me).

Are you using any kind of prefabricated Python module for this? Maybe 
their implementation of MP3 tagging is enlightening to you.

Remember that ID3 in MP3 was a dirty hack at the beginning, so it is 
likely that may files are nonconformant or that many different programs 
simply follow their gut, as there was no specification.

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


Re: [Tutor] sockets

2006-05-04 Thread Hugo González Monteverde
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.

Well if the excercise itself is not parsing a string, you can just use 
pickle to serialize your objects. Look up the documentation for the 
pickle module. You can transform many objects into byte streams and then 
send them over the network.

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


Re: [Tutor] Halting execution

2006-05-03 Thread Hugo González Monteverde
It would be similar to Perl's die() commmand. But no, in Python the 
argument is the error status of your script.

You'd have to do something like

def exitnow(arg):
 print arg
 #maybe sys has not been imported
 import sys
 sys.exit(1)

Hugo

Ertl, John wrote:
 Matthew,
 
 Not sure if ipython is different but have you tried sys.exit(stoping now)
 
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] question about run time

2006-05-03 Thread Hugo González Monteverde
I have made scripts that work on many files (sometimes just some tens) 
and appears that filesystem structure caching in Linux is very 
efficient.  That's why it runs very fast later.

I've seen this in Slackware, Debian, and RH, so I guess it's just a 
linux/FS/disk thing.

Try doing 'find' combined with md5sum or something disk-intensive, but 
unrelated with python to see if it exhibits the same problem.

Hugo

Ertl, John wrote:
 I have been using python for sometime...and occasionally I noticed
 significant delay before the code would run but unitl now I have been able
 to write it off to other things.  Now I have a short script that I wrote to
 check some files and print out a few lines.
 
 I have noticed that usually the first time I fire it up in the morning or
 after a long time of not running it, it takes 10-15 seconds to run and the
 output to the screen is very slow...maybe 1 second per line.  If I run it
 soon after that it runs and the output is on the screen in less then a
 second.  I would think this has to do with compiling but I am not sure.  Any
 ideas how to speed this up?  
 
 I am running python 2.4 on a RHE3.0 cluster. 
 
 Thanks,
 
 John Ertl
 ___
 Tutor maillist  -  Tutor@python.org
 http://mail.python.org/mailman/listinfo/tutor
 
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] How Big Is Too Big?

2006-05-03 Thread Hugo González Monteverde
 Just wondering how many lines of code is the maximum to post in the list 
 to have it critiqued. I realise people are using their own time to help 
 others in here for no real personal gain and I would hate to impose on 
 their goodwill. Would about 100 lines of code be considered too much?

My 2 cents:

Length is really no problem, the problem would be:

1) inconvenience of too long an email. Post a link to a webpage with the 
code if it is longer than, say, 100 lines.

2) It is much more important for me that the poster seems to be really 
working on the issue, and willing to learn. Then I'd have a look at code 
any size :)

But this is just my own opinion.

Greetings,

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


Re: [Tutor] cycle w/ shuffle

2006-04-27 Thread Hugo González Monteverde
Hi Kevin,

kevin parks wrote:
 I am trying to write a loop that iterates over a sequence and do 
 something x number of times. But sometimes i will need more events 
 (larger number x) than i have have items in the sequence, so if i need 
 more events that i have stuff in my sequence i would like to have the 
 loop reload and shuffle the deck and start all over again, reloading as 
 many times as necessary to get the number of events needed. I suppose i 
 could go around cyclically modulo the list size but i would want to 
 shuffle the deck before doing that again...


Why not save the iterable it in the generator, then just reuse it? You 
can copy the iterable like this:

import random
def cycle(iterable):
 saved = list(iterable)

 #notice how I saved into a list, for I could not shuffle a TUPLE!
 while True:
for element in saved:
yield element

 random.shuffle(saved)

def test():
seq = ('one', 'two', 'three', 'four', 'five', 'six', 'seven',
'eight', 'nine', 'ten')
loop = cycle(seq)
count = 1
for item in range(25):
print count, loop.next()
count = count + 1
#you could use enumerate() instead of manually making the loop here.
#for i, v in enumerate(loop):
#print i, v

Hope that helps,

Hugo



if __name__ == '__main__':
test()
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Avoiding the use of files to store intermediate results

2006-04-24 Thread Hugo González Monteverde
Remember duck typing. An object just needs to look like a file in order 
to be used like one.

Guido's time machine has already forseen your problem. Take a look at 
the StringIO module.  It allows you to use a string where you would 
normally pass a file object.

Hope that helps,

Hugo

 ##
 While this works, I find it messy, as it creates some intermediate
 files.  I was wondering if there was a better way to do things all in
 memory, in an OS independent way.
 
 [Note that the complete application is approximately 665 lines long
 ... a bit too much
 to post all here :-)]
 
 André
 ___
 Tutor maillist  -  Tutor@python.org
 http://mail.python.org/mailman/listinfo/tutor
 
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] GUI

2006-04-19 Thread Hugo González Monteverde
 Ok,
 If I can get it for free, I might as well go with say wxPython. Thanks

Yes, free as in beer, as in speech, and cross platform. Oh, and better 
documented.

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


Re: [Tutor] Raw Bits! (Control characters ahoy!)

2006-04-17 Thread Hugo González Monteverde
doug shawhan wrote:
 I am in the middle of a project that requires me to send and retrieve 
 information from a machine connected to a serial port. My problems are 
 these:
 
 1. I cannot send control characters
 2. I cannot read data streaming from the serial port
 
 
 I have been doing fine with:
 
 os.system(echo '5' /dev/tty00)
 os.system(echo '8' /dev/tty00)
 

Hi Doug,

Echo just opens the file and writes to it, closing it afterwards. if you 
have used setserial correctly on the portt, then the equivalent Python 
commands should work.

Note that the echo command adds a carriage return at the end.

 Reading from the serial port has also been problematic. I find that 
 simply opening the port /dev/tty00 file and using readlines(), read() or 
 whatever gives me empty strings or lists. Pbth.

If you use blocking, then reading should not give you anything empty, 
but data, no matter how long it has to wait.

 
 So first: how should I go about adding the hex escape sequences (\x0a 
 and \x50) to the above strings in their raw form?

fileo = open('/dev/ttyS0', 'w')
fileo.write('5' + '\x0a\x50')
#\x0a is a linefeed (do you want to do that?)
fileo.close()

You may use fileo.flush() instead of close, for avoiding buffering and 
not having to close the file.

 second: what method should I use to read a raw stream from the serial 
 port for a given period?
 


fileo = open('/dev/ttyS0')  #read only
data =  fileo.read(1)   #just one char
fileo.close()


#This waits forever. For a given period, use the select module (UN*X only)

import select
select([fileo.fileno()],[],[], 1.0)

#This will wait for 1 second for output from the file. Then the return 
value will tell you if the filehandles have data. ([], [], [])  means no 
data to read.


 I have looked carefully at the neat and tidy miniterm.py example in the 
 pyserial examples directory, and see how one can continuously read via a 
 thread from the port, but have not been able to replicate it thus far.
 

I never use threads for that, I always use select(). Then  again, I'm a 
unixhead.


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


Re: [Tutor] encoding

2006-04-12 Thread Hugo González Monteverde
kakada wrote:
  LookupError: unknown encoding: ANSI
 
  so what is the correct way to do it?
 

stringinput.encode('latin_1')

works for me.

Do a Google search for Python encodings, and you will find what the 
right names for the encodings are.

http://docs.python.org/lib/standard-encodings.html

Hugo

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


Re: [Tutor] dictionary datatype

2006-04-12 Thread Hugo González Monteverde
kakada wrote:

 I assign:
 n = 0x2018
 print dict1[n]
 
 Then:
 KeyError: '0x2018'
 

Notice the error menstions a *string*. You probably mistyped it, not in 
the email, but in your actual program.

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


Re: [Tutor] Tuple (Section 9.3)

2006-04-11 Thread Hugo González Monteverde
Hi Kaushal,

I might have to do a little guessing and see what is not clear from the
explanation.

The whole point of returning a tuple as opposed to, say, returning a
list, is the fact that  tuples are NON mutable. That is, *apparently*
you would not be returning a reference, but the values themselves.

There is no better example I can think of, than the one you already
read. If you want a function tu return multiple parameters so that you
can do.

ret1, ret2 = func()

then have func return a tuple. That's the whole point of the chapter you
read.

Maybe there is too much information in the discussion that does not look
useful to you if you have not run into a problem where you need it.

I hope this is not some kind of homework :)

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


Re: [Tutor] Python performance resources resouce usage hints

2006-04-07 Thread Hugo González Monteverde
Liam Clarke wrote:

 Each thread's run() method basically looks like this -
 
 while True:
 try:
 data = self.queue.get(False)
 self.DAO.send_data(data)
 except Empty:
 if self.shutdown:
 print \DAO closing
 return
 continue
 
 

Hi Liam,

I checked the response by Kent. I completely agree with him in that the 
problem is the endless polling you have.

However, you do not have to implement a sleep in your loop; the main 
issue may be that you are not using the facilities of the 'get' method 
in the queue. From the queue docs:

get([block[, timeout]])
 Remove and return an item from the queue. If optional args block is 
true and timeout is None (the default), block if necessary until an item 
is available. If timeout is a positive number, it blocks at most timeout 
seconds and raises the Empty exception if no item was available within 
that time. Otherwise (block is false), return an item if one is 
immediately available, else raise the Empty exception (timeout is 
ignored in that case).


You are not using the optional timeout and blocking which 'get' provides (!)

Try setting it and see your CPU usage go down. This will implement 
blocking, and the queue will be used as soon as data is there.  Set block 
to True and a timeout if you need to use it (looks like you only need 
blocking)

 while True:
  try:
 data = self.queue.get(True)
 self.DAO.send_data(data)

Hope that helps,

Hugo

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


Re: [Tutor] function caller

2006-04-07 Thread Hugo González Monteverde
josip wrote:

 Can someone explain me function and caller relationship when passing 
 arguments?

Hi... what do you mean? Is there something specific you're not getting 
if you take a look at:

http://www.ibiblio.org/obp/thinkCSpy/chap03.htm

http://docs.python.org/tut/node6.html#SECTION00660

We'd be glad to help with that, but we need the doubt to be more specific.

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


Re: [Tutor] Quick question,

2006-04-07 Thread Hugo González Monteverde
Carlos Benevides wrote:
 Hi,
 
 Can someone tell me what the r before the expression means, as in 
 re.compile(r'(\.exe|\.zip|\.pif|\.scr|\.ps|\.pdf|\.ppt)$')?

It is not specific to regular expressions, it is called a raw string.

http://docs.python.org/tut/node5.html
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] preliminary app design question

2006-04-05 Thread Hugo González Monteverde
Richard Querin wrote:

 My question before I start is whether or not using an XML format for the 
 individual file formats is the way to go, and if I have to anticipate 
 every little thing in the file formats before hand. I don't want to do 
 this, I would rather like to be able to just add and modify things as I 
 go with the least amount of hassle along the way. Any ideas on how to 
 generally approach the file formats?
 

To quote PJE in http://dirtsimple.org/2004/12/python-is-not-java.html 
(Python is not JAVA)
---
XML is not the answer. It is not even the question. To paraphrase Jamie 
Zawinski on regular expressions, Some people, when confronted with a 
problem, think I know, I'll use XML. Now they have two problems.
---
Now seriously. Are there file formats meant to be used and understood by 
other programs in principle (we know it is a nice feature, but is it 
necessary?)?

If the answer is yes, then XML may be the solution. Looks like what you 
are trying to accomplish can be done wrapping functionality into 
modules, then adding them together (classes, functions, etc) using a GUI 
master program.

Saving complex data structures to files in Python is easy. Let it be 
pickling or using a Python database, like Kirbybase or Gadfly. If you 
need to communicate this to other applications, you may use simple 
extensions like those in XML-RPC(still no XML design and/or parsing).


As for anticipating every single thing in the file format, this depends 
on whether you need to have fancy things like backwards compatibility. I 
would guess some dynamic data structure that allows for extension (like 
a dictionary of attributes, where you can always add new attributes) 
will do.

hope this rant helps a bit

Hugo
 Any suggestions would be greatly appreciated.
 
 I have zero experience with XML at this point, and very modest 
 experience with Python/wxPython, but necessity is the mother of 
 invention and I can learn what I need to know I think.
 
 
 
 
 ___
 Tutor maillist  -  Tutor@python.org
 http://mail.python.org/mailman/listinfo/tutor
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Question about large numbers of arguments

2006-04-05 Thread Hugo González Monteverde
Dana Robinson wrote:

 
 Have the functions take large numbers of parameters.
 
 or
 
 Create an options class to pass the options around.
 
 

Pythonically, I heard the distinct scream of DICTIONAR in my head.

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


Re: [Tutor] defined()

2006-04-03 Thread Hugo González Monteverde
Alan Gauld wrote:

 Which language(s) do you know that has such a feature?
 And why do you consider it so useful that you expect to find
 it in Python?

I'm not the original poster, but being a perlhead before, I can say it 
exists in Perl. It is very often used too.

I used to miss it at first, but normally I now do the right thing 
semantically. Counting on the variable being defined or not is simply 
another bit of information. Assigning a value for that case is much cleaner.

It is not used for black magic in Perl, AFAIK. Just normal testing.

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


Re: [Tutor] get a character of an ascii-value

2006-03-29 Thread Hugo González Monteverde
Hi,

Python strings are binary strings as they can contain any value, 
including 0 (NULL) Depending on the encoding of the string, this may or 
may not be printable, and characters over ASCII 127 will mean different 
letters and symbols.

Check the docs for strings and encodings:

http://docs.python.org/lib/standard-encodings.html
http://python.active-venture.com/api/stringObjects.html

Hugo

In a
 
 I just got the answer from Pujo. How about r is bigger than 127. How can i 
 get 
 the character? For example, r = 191.
 
 Thanks,
 Sophon
 ___
 Tutor maillist  -  Tutor@python.org
 http://mail.python.org/mailman/listinfo/tutor
 
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Program for outputing the letter backward

2006-03-28 Thread Hugo González Monteverde
Hoffmann wrote:

 while index = index_zero:
letter=vehicle[index]
print letter
index -= 1
 
 The problem is that I get no output here. Could I hear
 from you?

Hi, remember that the condition for the while has to be true. When does
index = index_zero stop being true???

Hope that gets you going,

Hugo

Ps: there are easier ways of doing this in Python, consider the for 
statement.
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Python help

2006-03-28 Thread Hugo González Monteverde
Why not post where you are stuck or what you are trying to understand? 
and we'll give you help and direction. What we cannot do is solve your 
homework for you.

Hugo

Natasha Menon wrote:
 hi,
 
 i need help on a terrible homework assignment. do ul offer hw help?
 
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] get a character of an ascii-value

2006-03-28 Thread Hugo González Monteverde
Keo Sophon wrote:
 Hi all,
 
 I wonder how to get a character of an Ascii-value.  For example, i have r = 
 37. So i wanna print the character of 37. Is there any function?
 
Hi ..yeah, it's called chr(), and its opposite is ord()

  ord('r')
114
  chr(114)
'r'
 

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


Re: [Tutor] TypeError: dict objects are unhashable

2006-03-24 Thread Hugo González Monteverde
Ben Vinger wrote:

 But I get:
 TypeError: dict objects are unhashable
 Unfortunately, I just can't see what I'm doing wrong
 
 InnerDict = {}
 InnerDict[U] = P
 accounts[InnerDict] = U

Your logic is not right somewhere around this. I do not have a lot of 
context, nor the full error message, but what it says is that you 
*cannot* use a dictionary as a key for a dictionary.

In this line:

accounts[InnerDict] = U

this is what you're doing.

You should be using a dictionary with the tuple, something like this:

 InnerDict = {}
 InnerDict[U] = P
 accounts[(U, P])] = U

Again, I do not wuite get why you want to do it like that. If you could 
explain a bit more to us

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


Re: [Tutor] Mysql BLOB strangeness?

2006-03-20 Thread Hugo González Monteverde

 However, there is still a learning point that might be missed here -
 how does Python grab BLOB data-types, and how do you manipulate them?
 If it were a file, would you just be able to grab the file without the
 array?


The basic issue is: what form is used for representing arbitrary byte 
data of arbitrary length.

 1) strings: strings in python are arbitrarily large and can store 
any byte values (compare this with other languages like C where the 
number 0 signals the end of a string)  Strings are OK, and that's what 
the standard library writes to files.

 2) arrays: there is but one problem with strings, and that they are 
inmutable: you have to create a new string everytime you want to change 
an old one. Long data chunks are inefficiently worked when using 
strings, so you may use arrays (array.array) which can be changed in place.


Of course there are many more options, like a tuple or a list of 255 or 
less ints, but these are the main contenders for byte valued sequences.

Hope that clears thing a bit,

Hugo


 
 
 --
 http://www.monkeez.org
 PGP key: 0x7111B833
 ___
 Tutor maillist  -  Tutor@python.org
 http://mail.python.org/mailman/listinfo/tutor
 
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Help with re.sub()

2006-03-16 Thread Hugo González Monteverde
Hi John,

I would just like to suggest a different approach. Like the old saying goes:

Some people, when confronted with a problem, think “I know, I’ll 
use regular expressions.” Now they have two problems.
— Jamie Zawinski, in comp.lang.emacs

If the delimiter is always the same ('@') you can use split() to get the 
data. Then you can arrange the data in a dictionary of lists, like this.

collapsed_data = {}

for line in mydata:
 id_part, data_part = line[:-1].split('@')

 try:
 collapsed_data[id_part].append(data_part)
 except KeyError:
 #first time insert for that key
 collapsed_data[id_part] = [data_part]

for id, data in collapsed_data.iteritems():
 print '@'.join([id] + data)

That should be it. Python's data types are very powerful.  Of course you 
could just build a huge list comprehension that does it...


Hope that helps,

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


Re: [Tutor] scaling values

2006-03-14 Thread Hugo González Monteverde
Hi Kevin,

Do you mean:?

1)take the highest value in the list hval, take the lowest value in the 
list lval

2) pass top and bottom NEW values for the list: ntop nbot

3) then build another list where hval is replaced by ntop, lval is 
replaced by nbot, and everything else is geometrically scaled in bewteen?

AFAIK, there is no such function in the standard lib (maybe someone 
knows better) but it is not hard to build such a function. Have you tried?

It would be something like:

def scaled(x, ntop, nbot)

See that you can use min(L) and max(L) to get lval and hval

Try it, and if you run into problems or have questions, don't hesitate 
to ask.

Hope that helps,

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


Re: [Tutor] problems with numbers in my python code

2006-03-10 Thread Hugo González Monteverde
Hi,


 However, I can't seem to get the program to treat the numbers as numbers. If
 I put them in the dictionary as 'THE' = int(0.965) the program returns 1.0
 and if I put 'THE' = float(0.965) it returns 0.9655549 or something
 similar. Neither of these are right! I basically need to access each item in
 the string as a number, because for my last function I want to multiply them
 all together by each other. 

The latter is right. Floating point numbers have limited precision 
(limited by the number of bits used in the representation) The exact 
number .965  does not exist in the representation for your computer, but 
  smart displaying can be done:

  .965
0.96497
  print .965
0.965
  var = .965
  var
0.96497
  print var
0.965
  str(var)
'0.965'
  repr(var)
'0.96497'
 

Take a look at apprendix B in the tutorial: Floating Point Arithmetic: 
Issues and Limitations

http://docs.python.org/tut/node16.html

Also the decimal module may be the solution to your problem:
http://docs.python.org/tut/node13.html#SECTION001380

Hope this clarifies things a bit.

Hugo


 
 I have tried two bits of code for this last bit, but neither are working
 (I'm not sure about the first one but the second one should work I think if
 I could figure out how to return the values as numbers):
 
 1st code 
 value = codons[x] * codons[x+1]
 x = (int)
 x = 0
 
 print value
 
 x +=2
 
 if (xr):
 new_value = value * codons[x]
 value = new_value
 x +=1
 else:
 print new_value
 
 This gives the error message 
 Traceback (most recent call last):
   File C:\Python24\code2, line 88, in -toplevel-
 value = codons[x] * codons[x+1]
 NameError: name 'x' is not defined
  
 Code 2 - the most likely code
 prod = 1
 for item in (codons): prod *= item
 prod
 print prod
 
 Gives this error message:
 Traceback (most recent call last):
   File C:\Python24\code2, line 90, in -toplevel-
 for item in (codons): prod *= item
 TypeError: can't multiply sequence by non-int
 
 
 Can anyone help me solve this problem?
 Thanks.
 
 --
 View this message in context: 
 http://www.nabble.com/problems-with-numbers-in-my-python-code-t1259271.html#a3339964
 Sent from the Python - tutor forum at Nabble.com.
 
 ___
 Tutor maillist  -  Tutor@python.org
 http://mail.python.org/mailman/listinfo/tutor
 
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] execute an OS command, get the output

2006-03-10 Thread Hugo González Monteverde
Hi Terry,

 abc = executeit(commandline)or
 executeit(commandline, abc)
 

Looks like you are looking for the commands module, it provides:

getstatusoutput( cmd)

getoutput( cmd)

getstatus( file)


Which do exactly what tou want. Take a look at the docs at:

http://python.active-venture.com/lib/module-commands.html


Hope that helps,

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


Re: [Tutor] Are we the same?

2006-03-06 Thread Hugo González Monteverde
I believe it is just spam pretending to be a legit invitation, that made 
it to the list.


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


Re: [Tutor] saving .csv file as an xl worksheet

2006-03-06 Thread Hugo González Monteverde
Hello,

.xls is a proprietary format used by Microsoft office, and unless you 
have the specs for that format and are willing to implement them (very 
very hard) it would not make much sense.

If you absolutely need that, then maybe a macro in VBA from Excel itself 
may be a better option.


Changing the file extension only (just the filename, not the contents of 
the file) can be done using the shutil module in Python (high level file 
manipulation)

Hugo


arun wrote:
 Hi ,
   Can i save a file with a desired extension??
 for ex:  I have a .csv file (test.csv) and i want to save this file as 
 test.xls from a python script.
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Analysing genetic code (DNA) using python

2006-03-06 Thread Hugo González Monteverde
Hi Anna,

Let's see one thing at a time.

wrote:
 ** If the three base pairs were UUU the value assigned to it (from the 
 codon value table) would be 0.296 

This can be done in Python, and one appropriate tool may be a dictionary 
such as:
 
dna_table = {
 UUU : 0.296,
 GGG : 0.3
}
  print dna_table[UUU]
0.296
 

You'd use the table to look the corresponding value.

Of course, you'd have to so some programming to get your ASCII 
translation tables converted to a dictionary.


 The program has to read all the sequence three pairs at a time, then I 
 want to get all the values for each codon, multiply them together and 
 put them to the power of 1 / the length of the sequence in codons 
 (which is the length of the whole sequence divided by three). 

Rational powers are supported by the pow() function in module math, see:

  import math
  math.pow(10,0.5)
3.1622776601683791

 
 
 However, to make things even more complicated, the notebook sequences 
 are in lowercase and the codon value table is in uppercase, so the 
 sequences need to be converted into uppercase. Also, the Ts in the DNA 
 sequences need to be changed to Us (again to match the codon value 
 table). And finally, before the DNA sequences are read and analysed I 
 need to remove the first 50 codons (i.e. the first 150 letters) and the 
 last 20 codons (the last 60 letters) from the DNA sequence.

These problems are very straightforward in python with string methods, 
take a looks at the docs at:

http://docs.python.org/lib/string-methods.html

  I've also
 been having problems ensuring the program reads ALL the sequence 3 
 letters at a time. 
 

Line endings may be causing you problems if you try to read line by line.

Something like this should do:

#read 100 characters from your file
buff = fileo.read(100)

#eliminate line endings
buff = buff.replace(\n, )

#read three new characters
word = buff[0:4]

#consume part of the buffer
buff = buff[4:]

This is slow and can be polished. One approach could be reading 
characters into a list and then consuming that list (strings are a bit 
harder since you cannot change them) Also, the last expressions will 
throw an exception when the list is too short, and you'll have to read 
another chunk from the file.

Hope this all helps, please let us know how far have you got and when 
you get stuck so we can help.

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


Re: [Tutor] Finding items in a list

2006-02-27 Thread Hugo González Monteverde
As others have said, a concrete example would be great. For the time 
being, sounds like maybe regular expressions could do it. We don't know 
if your data is already in a list of small strings, or if it is just a 
huge string.

Take a look here for an intro to RE, though:

http://www.amk.ca/python/howto/regex/

Try to send a bit more specifics, we'll be here...

Hugo

Emily Patek wrote:
 Hi - 
 I am trying to break apart a list into several smaller lists based on 
 repeating nearly identical entries.  For example, every so often there is the 
 word CLUSTER with a number after it, like CLUSTER 1.  I can find them one by 
 one, but would like to
 do a while loop for while item in list = CLUSTER X.  Is there a key that I 
 can put in that means any text that comes after that would go from cluster 
 to cluster without my typing in each cluster and number?  I am a very 
 beginner programmer and am
 trying to parse a file of gene-related information that is in a not-so-easy 
 format.  I put it into a list based on lines through the splitlines() method. 
  I also thought about splitting the text by CLUSTER and then making each item 
 of the list into
 a string that I could then split, but couldn't get that to work either...
 
 Thanks! 
 Emily
 
 ___
 Tutor maillist  -  Tutor@python.org
 http://mail.python.org/mailman/listinfo/tutor
 
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Tkinter, Frame and Canvas question.

2006-02-23 Thread Hugo González Monteverde

 
 Alas, there is some strange anomaly: it only does so (i. e. doesn't
 display that gray bar between the canvases) when run from IDLE or
 by double-clicking the renamed file test.pyw or run from the
 command window with the command: python test.py.
 Double-clicking test.py performs differently(!) and still shows the 
 border between the two canvases. Who understands? (Or do you think my
 python installation is in a corrupted state?)

No, it does the same on my 2.3  installation in windows. I will test on 
Linux.

Actually, it displays the grey border, but if you scroll any window in 
front of it, everything except those borders are redrawn... (!)

I'll check what it does on Linux and then report back...

Thanks,

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


Re: [Tutor] Bug in python

2006-02-21 Thread Hugo González Monteverde
Hi, just a recommendation: try not to assume a bug beforehand, that's a 
quick way to get flames or just get plain ignored, especially in a list 
for beginners.

 The * is being given equal  priority to %.
 
 Why isn't % given higher priority than *?

Why should it? Doesn't it make sense that as '/'
  and * have the same precedence, '%' should have the same?

Check the precedence for python operators here:
http://www.byteofpython.info/read/operator-precedence.html
http://docs.python.org/ref/summary.html

Check the precedence for C operators here:
http://publications.gbdirect.co.uk/c_book/chapter2/expressions_and_arithmetic.html

So everything alright, also, left to right, as expected.


 Also,  why am I getting a syntax error  in the following?
 

To get more informative messages, save all those statements to a file, 
and then try and run the file. It is extremely easy to mess up 
indentantion and make mistakes in the interactive prompt. Aslo you'll be 
getting information about the line number and such.

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


Re: [Tutor] pdf syntax

2006-02-20 Thread Hugo González Monteverde
Hi,

Syntax for composing a PDF is ugly PostScript. However, there are a 
couple of ways to use Python libraries to avoid the details.

You can draw a canvas in Tkinter and then print it, then use something 
linke ps2pdf to do the conversion.
http://effbot.org/tkinterbook/canvas.htm

You can use ReportLab, which is intended specifically for generating 
PDF. Docs are very complete:

http://www.reportlab.org/

The best language is of course Python, as this is the Python Tutors list  ;)

Hope that gets you started,

Hugo
saurabh akshekar wrote:
 Hi all
 my name is saurabh
 i want create n manipulate pdf files.
 can anybody tell me what is pdf syntax n how it works?
 also best or efficient language in which i should write program
 please reply
  
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] using popen(n) to intercept stdout

2006-02-15 Thread Hugo González Monteverde
Hi Tim,

You  are correctly using the filehandles you have, but the man command 
must be sending the response into STDERR instead of STDOUT. This makes 
sense, since it is an error message.

Take a look at popen4() and popen3() which both let you catch the 
standard error of a process. Also, in more recent versions of Python, 
take a look at the module named Subprocess.

Hope that helps,

Hugo

 
 Question:
 How do I intercept the first response?
 I.E. How do I get No manual entry for nothere
 into a variable?
 
 The object is to call another process on the same machine and capture
 the output of that process, assuming that the child process is writing
 to stdout.
 
 thanks
 tim
 
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] using popen(n) to intercept stdout

2006-02-15 Thread Hugo González Monteverde
I also noticed that if you want to get the manpage, you will be hurt by 
the interactivity of 'man' (it uses less for paging) One way to get the 
whole manpage as text without paging is to do:

man -P /bin/cat

So you won't have to do strange stuff in stdin to get it to give you the 
whole text.

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


Re: [Tutor] using popen(n) to intercept stdout

2006-02-15 Thread Hugo González Monteverde
Hi Tim,

Yes, you're on the right track.

a,b,c = os.popen3('wt *.py','r')
res = a.read()

The error you're getting has nothing to do with the result of the 
command, but check, from the docs:

popen3( cmd[, mode[, bufsize]])

Executes cmd as a sub-process. Returns the file objects (child_stdin, 
child_stdout, child_stderr). Availability: Unix, Windows. New in version 
2.0.


 
  Traceback (most recent call last):
File stdin, line 1, in ?
  IOError: [Errno 9] Bad file descriptor
 
 

The 'a' variable is supposed to be the process' STDIN, you are supposed 
to write to it. So if you want to read, it is the wrong file object, and 
it is in write mode, therefore the IOError.

Also check that the above docs are for os.popen(), popen2.popen() 
returns filehandles in a different order.

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


Re: [Tutor] Tkinter, widgets not displaying...

2006-02-10 Thread Hugo González Monteverde
Hi Kent and John,

Thanks a lot for the advice on how to improve my program. I will look 
into separating the data and model as John suggests here. I didn't know 
about after_idle() and after(); seems that dir()'ing the classes 
sometimes gives out a lot more information than you can chew at any 
given time :)

Thanks,

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


Re: [Tutor] cgi script: how to continue a process in the background and return from cgi script

2006-02-10 Thread Hugo González Monteverde

 So, could some give me a pointer to possible solutions ? Do I have to 
 make the last part of my program a seperate program and go through a 
 system call ?

Hi,

I have had this problem before. The timeout problem with Apache remains 
because STDOUT of both child and the parent are open. Apache keeps 
thinking the CGI is still sending data until the filehandle is closed.

I have solved it in the pas using this recipe, which is similar to the 
one you used before:

http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/278731

Hope that helps,

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


[Tutor] Tkinter, widgets not displaying...

2006-02-09 Thread Hugo González Monteverde
Hi All,

I wrote a small turn delivering graphical app that is supposed to 
display turns in a queue.

If I instantiate the class and call its methods, thus displaying strings 
in several canvases, from the interactive prompt, everything works fine. 
  If I do it when running the script as a programs, nothing gets displayed.

If I call mainloop() at the end of my program, everything gets 
displayed, but then I cannot use my own function classes. This program 
will never respond to events, but just get called from a queue manager 
script for display...

Here's the code:

#!/usr/bin/env python
from __future__ import division
import Tkinter
import tkFont
from Tkconstants import *


from time import sleep

class TurnQueue(Tkinter.Frame):
 def __init__(self, master=None, pw=60, ph=60, panels=4):
 self.ph = ph
 self.pw = pw
 self.panels = panels

 Tkinter.Frame.__init__(self, master)
 self.createWidgets()
 self.pack()

 def createWidgets(self):
 Create four canvases and a warning window.
 self.canvases =  [Tkinter.Canvas(height=self.ph, width=self.pw, 
borderwidth=1, background = '#FF') for i in range(self.panels)]
 self.textbox_ids = []

 # display them all, initialized
 for i in self.canvases:
 id = i.create_text(self.pw//2, self.ph//2, text='', 
font=('bitstream charter', self.ph, 'bold'), fill='red')
 self.textbox_ids.append(id)
 i.pack()

 def write_panel(self, panel_no, newtext):
 writepanel(self, panel, newtext)
 Set panel panel to text newtext
 
self.canvases[panel_no].itemconfigure(self.textbox_ids[panel_no], 
text=newtext)

 def read_panel(self, panel_no):
 textcontents - read_panel(self, panel_no)
 Read panel text contents
 
return(self.canvases[panel_no].itemcget(self.textbox_ids[panel_no], 'text'))

 def insert(self, turn_string):
 Insert a new turn into the queue, move the rest upwards, 
delete oldest.
 current_values = [self.read_panel(i) for i in 
range(len(self.canvases))]
 next_values = current_values[:]
 next_values[:-1] = current_values[1:]
 next_values[-1] = turn_string

 # update all panel values
 for i in range(len(self.canvases)):
 self.write_panel(i, next_values[i])

if __name__ == __main__:
 myturns = TurnQueue(pw=200, ph=100, panels=4)

 for i in range(100):
 myturns.insert(str(i))
 sleep(0.2)
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] IDE - Editors - Python

2006-02-09 Thread Hugo González Monteverde

 I'm programming under Windows and I haven't found anything better than
 Stani's Python Editor (spe).  It should be cross-platform.
 

I second SPE under Windows, though under linux I keep using vim.  The 
included utilities are great (I love Kiki)

The only caveat would be that running wxpython programs could get you 
into trouble, as SPE itself is written in wxPython.

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


Re: [Tutor] Tkinter, widgets not displaying...

2006-02-09 Thread Hugo González Monteverde
Sorry for replying to myself, but I found a page that says that it can 
be done without threads... so it hit me: use threads!!! (duh)

Ok, for future reference, this is the part where I use the classes I 
defined:

if __name__ == __main__:
 top = Tkinter.Tk()
 myturns = TurnQueue(top, pw=200, ph=100, panels=4)

 #importing here is ugly, but just for testing.
 from thread import start_new_thread

 start_new_thread(Tkinter.Tk.mainloop, (top,))
 for i in range(10):
 myturns.insert(str(i))
 sleep(0.2)

So now I see my line of canvases displaying numbers going upwards..

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


Re: [Tutor] mapping problem

2006-02-01 Thread Hugo González Monteverde
What about choosing a native Python data structure for this?

A list of attributes comes to mind, as presence of one can be easily 
tested with

if x in attributes:
 do_something

Here's a test:

  elements= {}
  elements['boat'] = ['a', 'b', 'j', 'k']
  'a' in elements['boat']
True
 

Then it is just a matter of getting the fiels into the structure, and 
then recursing through  the elements, like this.


elements = {}

for line in table_fileo:
 fields = line.split()

 #get rid of linefeed
 fields[-1] = fields[-1][:-1]

 elements[fields[-1]] = fields[:-1]

Then you get a dictionary with the last name in the table as key and a 
list of the other attributes as value.

Then, if you want to look for an attribute, just do:
for element in elements.items():
 if 'j' in element[1]:
 print element[0]

What do you think?

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


Re: [Tutor] webcam secrets?

2006-01-22 Thread Hugo González Monteverde
Hi Johnny,

As someone already said, it could be possible to do that without going 
into any code, but assuming you want to do it by means of Python...

I assume you're using Windows, are you trying to control another 
application (the one that came with your webcam)? are you trying to 
write a program that accesses the driver for your webcam?

In any case, whatever you're trying to accomplish will have a lot to do 
with what the operating system allows you. May I suggest you take 
another program or exercise more related to programming before you 
become complicated with so mucho weird operating system interfaces?

I suppose webcams are accessed through the TWAIN infterface. There is 
(old) support for python, here:

http://twainmodule.sourceforge.net/docs/

Hugo

johnny` walker wrote:
 i was wondering if there was a way to automatically take a picture from 
 my webcam every oh say 5 minutes?
  
 is there anyway of doing this (im very new to programming languages.)
 
 
 
 
 ___
 Tutor maillist  -  Tutor@python.org
 http://mail.python.org/mailman/listinfo/tutor
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] I'm puzzled

2006-01-22 Thread Hugo González Monteverde
Hi Vincent,

 the program works with any input except when you just hit the enter key.


To be  able to understand why is the crash, take a look at what the 
interpreter tells you:

   File untitled.py, line 12, in ?
 elif a[0] == '-' and a[1:].isdigit():
  IndexError: string index out of range

IndexError is raised whan you try to access an element in a list or 
string, an element that does not exist. In the case where you only press 
enter, what is the content of a How many characters? (hint, you may 
try to

print a

before any evaluation...

Hope that gets you going,

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


Re: [Tutor] python-based system programming and admin?

2006-01-22 Thread Hugo González Monteverde
MMM strange needs...

I'm thinking that perhaps allowing him to run idle and exporting X 
display to the Mac could be an option?

I used to do perl on linux until I found Python, I find it very easy to 
run quick scripts and system stuff without having to learn BASH, sed, 
awk, etc separately, taht way I can do regexp, listings, recursion on 
directories, batch jobs, etc.

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


Re: [Tutor] timeout

2006-01-16 Thread Hugo González Monteverde
Hi Frank,

  how can I break a loop after a certain amount of time has passed?
 


If the loop actually does something, I've used time.time() in the past:

start_time = time.time()
timeout = 60 #seconds


while True:
 do_something()
 if time.time() - start_time = timeout:
 break

time.time() just keeps increasing as it reports the system's clock (in 
UNIX epoch time, just increasing seconds)

Hope that helps,

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


Re: [Tutor] CPU Utilization

2006-01-11 Thread Hugo González Monteverde
Hi,

top is very interactive so it would not be easy at all to parse.

I can suggest reading /proc/loadavg if you're in Linux.

proc(5) ('man 5 proc') says that /proc/loadavg is
 The load average numbers give the number of jobs in
 the run queue averaged over 1, 5 and 15 minutes.
 They are the same as the load average numbers given
 by uptime(1) and other programs.

It can be easily parsed with split()

Hope that helps,

Hugo


DS wrote:
 I am implementing a computational cluster and am using a job controller
 (for want of a better term) that contacts the worker computers,
 determines how busy they currently are, and then assigns more work if
 the worker computer is below a certain cpu utilization threshhold. 
 
 For the cpu threshhold, I've been thinking I'd have the worker computer
 shell out to perform the top command, parse the results, and report
 those values to the job controller.
 
 Is there a better way, or more pythonic, way to do this?
 
 Thanks
 
 ds
 ___
 Tutor maillist  -  Tutor@python.org
 http://mail.python.org/mailman/listinfo/tutor
 
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] bnf

2005-12-19 Thread Hugo González Monteverde
  mystring = 'laLA'
  mystring.upper()
'LALA'
  mystring.lower()
'lala'
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] python-ldap

2005-11-22 Thread Hugo González Monteverde
Hi,

Looks like they are just constants. There is really no point in printing 
or making any sense of their content, just use them as they are defined 
in the module. This is like a C #define

But this is all LDAP specific... let's see:

 
 ## The next lines will also need to be changed to support your search 
 requirements and directory
 baseDN = ou=Customers, ou=Sales, o=anydomain.com
 searchScope = ldap.SCOPE_SUBTREE

SCOPE_SUBTREE is one of the avaliable search scopes. According to 
this(http://linuxjournal.com/article/6988) it should search for the 
object and its descendants..

 if result_type == ldap.RES_SEARCH_ENTRY:
 result_set.append(result_data)

Reading it as it is, looks like the result may be something different, 
but only those results that are of type RES_SEARCH_ENTRY are meaningful. 
That's why you check it.

read, read, read. This is what I understand, not knowing crap about 
LDAP, and just using Google.

Check the following docs:
`
http://www.ldapman.org/ldap_rfcs.html

http://www.cse.ohio-state.edu/cs/Services/rfc/rfc-text/rfc1823.txt

Bingo! This is the LDAP application programming interface from which the 
  Python LDAP implementation has been 'stolen' All these definitions are 
explained. For instance:


Parameters are:

ldThe connection handle;

base  The dn of the entry at which to start the search;

scope One of LDAP_SCOPE_BASE, LDAP_SCOPE_ONELEVEL, or
  LDAP_SCOPE_SUBTREE, indicating the scope of the search;

Upon successful completion, ldap_result() returns the type of the
result returned in the res parameter. This will be one of the
following constants.

  LDAP_RES_BIND
  LDAP_RES_SEARCH_ENTRY
  LDAP_RES_SEARCH_RESULT
  LDAP_RES_MODIFY
  LDAP_RES_ADD
  LDAP_RES_DELETE
  LDAP_RES_MODRDN
  LDAP_RES_COMPARE



Google is your friend, and going through you may find most of your 
answers...

Hope that help,

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


Re: [Tutor] Lists with just an item

2005-11-21 Thread Hugo González Monteverde

 Probably due to the lacking of experience, but each time I code a
 datastructure I am never satisfied, and I have the feeling that I'm
 not on the right way..

The same thing happens to me. I actually have a very similar problem to 
yours now, and I'm rather considering a list of two-element tuples.

Greetings,

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


[Tutor] Ellipsis syntax

2005-11-21 Thread Hugo González Monteverde
Hi all,

I have looked for any references of ellipsis '...' syntax for Python 
slices, but I keep finding the same BNF grammar in the 
LanguageRreference. Is there any page with examples on how to do this, 
or is this just very obscure?

I remember reading something about this syntax somewhere, long ago. I 
don't even know how it is supposed to be used, but it is in the grammar...

Any help would be appreciated.

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


Re: [Tutor] Ellipsis syntax

2005-11-21 Thread Hugo González Monteverde
Ok, now I know I don't need them just now *grin* Next time I will 
remember to search  the newsgroup

Now I think it was the perlish skeletons in my closet coming back to me.

Thanks a lot for that,

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


[Tutor] Overloading comparisons

2005-11-15 Thread Hugo González Monteverde
Hi all,

I defined an object wich describes a video clip, like this

class VideoSegment:
 def __init__(self, filename):
 # Attributes that have to be present
 self.filename = filename


The thing is, I will define an array of these objects for a cache, and 
would like to keep that array having less than 1000 elements. 
Periodically I will check it, and remove the oldest element.

suppose I have the modification time as an argument.

self.time = os.stat(self.filename).st_mtime

I can define the following for sorting the array:

 def __cmp__(self, other):
 polymorph for comparisons
 if self.time  other.time:
 return -1
 elif self.time  other.time:
 return 1
 else:
 return 0

But then I want to test for existance doing

if 'lala.avi' in myarray:

and that has to be done by name. Is there any way this could work? My 
current __cmp__ of course treats objects with the same timestamp as 
identical objects.

Any comments are greatly appreciates, maybe my OOP is just sloppy...

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


Re: [Tutor] Overloading comparisons

2005-11-15 Thread Hugo González Monteverde
I know it is rude to reply to myself. But I found the answer and wanted 
to share it.

I did the following test:

  class Lala:
... def __eq__(self, other):
... if self.name == other.name:
... return True
...
... def __cmp__(self, other):
... if self.time  other.time:
... return -1
... elif self.time  other.time:
... return 1
... else:
... return 0
...
  ins0 = Lala()
  ins0.time = 11
  ins0.name = 'myname'
 
 
  ins1 = Lala()
  ins1.time = 10
  ins1.name = 'myname'
 
  ins1 == ins0
True
 
  ins0ins1
False
  ins1ins0
True
 
  #See? They test correctly for greater and lesser than (time)
... #but they test equal as to time.
...
  #I even tried sort, it works fine.



The thing is to use the rich comparison functions.

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


Re: [Tutor] Overloading comparisons

2005-11-15 Thread Hugo González Monteverde
Hi Danny,

That's nice. I do think I'm going this way

I don't want the container to grow too big in memory, not bigger than 1k 
elements(it is a cache), but when deleting an element I need it to be 
the oldest files (again, because it is a cache)

Thanks for the advice, I'm going this way. My OOP skills *are* sloppy...

Hugo

Danny Yoo wrote:
 
I defined an object wich describes a video clip, like this

class VideoSegment:
 def __init__(self, filename):
 # Attributes that have to be present
 self.filename = filename
 
 
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] question about serial coms

2005-11-14 Thread Hugo González Monteverde
Hi Nephish,

Are you using pyserial or rolling your own? Normally you can write and 
read to the /dev/ttySXX file at the same time; since they're special 
files, not ordinary files, the driver handles that.

Handling both writing and reading in your program's flow control is a 
wholly different matter, though. You might  need to use select()  to 
avoid blocking.

Are you using two completely different scripts for reding and writing?

There is some valuable info, if not about python, in the Serial 
Programming howto, at:

http://www.tldp.org/HOWTO/Serial-Programming-HOWTO/


Hugo

nephish wrote:
 Hey there,
   i am developing on a linux computer with the serial module. Now, i
 already am able to recieve info from a serial RS232 device and process
 everything ok. What i need to do now is write to the serial device, but
 i also need to be able to not interrupt the script that is reading from
 it. 
   I guess my question is, do i have to interrupt the reading script to
 write to the same RS232 device ?
   and if so, how do i do that?
 
   thanks,
   shawn
 
 ___
 Tutor maillist  -  Tutor@python.org
 http://mail.python.org/mailman/listinfo/tutor
 
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] What does Python gain by having immutable types?

2005-11-14 Thread Hugo González Monteverde

 Quite often the only answer is just because. Some features are
 the way they are because that's Guido's pesonal preference. Others
 may disagree with him but it's his language and he gets to pick
 what he thinks is best - the benign dictator syndrome.

There's also the issue that immutability is Python's way of dealing with 
  passing by value and passing by reference.  And that removing the 
difference between mutable and immutable objects would mutate (no pun 
intended) the language in a big way.

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


Re: [Tutor] How do I display a changing thing in text?

2005-11-14 Thread Hugo González Monteverde
Hi Nathan,

Take a look at string interpolation in the tutorial. I don't know what 
method you're following for learning Python, but I've found the 
'official'  tutorial a great resource, even when you already know Python,

http://docs.python.org/tut/node9.html#SECTION00910

Nathan Pinno wrote:
 How do I display a changing thing (i.e.. the score of the game) in text? 
 I think it has something to do with this key: %, but I forget how to do 
 it. Can someone show me how to do it again?
  
 Thanks,
 Nathan Pinno
 
 
 
 
 ___
 Tutor maillist  -  Tutor@python.org
 http://mail.python.org/mailman/listinfo/tutor
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Do I have to initialize TKInter before I can use it?

2005-11-14 Thread Hugo González Monteverde
Hi Nathan,

It is always better if  you include what you're actually trying to do. I 
see you included one traceback error, but I see no reference to what 
you're coding or any of your code.

In your case , it's likely that you did not import the tkMessageDialog 
module or something like this. But we had to find that out after two 
more posts, because neither your code example or error messages were in 
the original.

Please, please read:

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

Keep hacking away :)

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


Re: [Tutor] saving project

2005-11-14 Thread Hugo González Monteverde
If you do

File... Save

Then select the location for your file and just give it a name, a '.py' 
extension will be added to the file.

It is not in a specific format, it is just a text file with a '.py' 
extension. From Explorer you will likely see just the name of the file. 
  And after you save you'll see the coloring on your program, which 
helps you avoid programming mistakes.

I'm assuming you're using Windows and not Linux.

Hugo

sener wrote:
 hi everyone,
 
 Firstly I am a new programmer on python. I am using Python 2.4.1 
 with IDLE 1.1.1   
 I can't save my poject in my computer. how can I save my projects and 
 in  which format I must save my projects.
 
 thanks a lot.
 ___
 Tutor maillist  -  Tutor@python.org
 http://mail.python.org/mailman/listinfo/tutor
 
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] What does Python gain by having immutable types?

2005-11-14 Thread Hugo González Monteverde
I believe  the deal is that it is impossible (or at least extremely 
inefficient)to keep track of any places where a mutable object  was 
hashed at the time of mutating it, so that ALL hashes can be updated. 
When the object is immutable, it implies that the hash will not change 
unless the id changes.

Hugo

 Would you expect dicta[tupb] to produce 34 or 45?  And what of 
 dicta[tupc]?  I believe this issue is one of whether to hash by identity 
 or content.  When hashing, the implied promise is that all objects whose 
 contents are the same should produce the same value, but one also 
 expects one key to map to one value.  Without immutable types, where the 
 identity and the content are intertwined, you get counterintuitive 
 behavior a la the above mini-example (in reality, when you put two 
 immutable objects with the same content in a dictionary, they both map 
 to the same value - the last one assigned).  I'm sure someone more 
 knowledgeable will come along and shed the bright light of understanding 
 where my feeble candle flickers, but till then, HTH.
 
 -Orri
 
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Do I have to initialize TKInter before I can use it?

2005-11-14 Thread Hugo González Monteverde
Hi Nathan,

The interactive interpreter is your friend. I just displayed a simple 
dialog without creating a  Tkinter App, complete with a mailoop or 
anything.

But I won't tell you how... you need to import the module and then use 
one of its functions. But you need to try it yourself (be aware that 
this module only gives you basic dialogs, like a text message ona 
little window with an 'Accept' button...)

Remember that you can use the dir() function and the help() function on 
the interactive interpreter.

Hugo

Nathan Pinno wrote:
 John,
 
 I learned it from the docs on pythonware.com - the introduction to TKInter -
 Standard Dialogs.
 Since it doesn't exist, how can I show a info box before the main part of my
 program?
 
\
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] How do I display a changing thing in text?

2005-11-14 Thread Hugo González Monteverde
The interactive intepreter is you friend. Of course I'm using actual 
numbers instead of variables for the examples. Let's see

IDLE 1.0.3
  Computer = %1.0d Player = %1.0d (1,2)

Traceback (most recent call last):
   File pyshell#0, line 1, in -toplevel-
 Computer = %1.0d Player = %1.0d (1,2)
TypeError: 'str' object is not callable

H what is the problem?


  Computer = %1.0d Player = %1.0d%(1,2)
'Computer = 1 Player = 2'
 


Here you go. You were missing the % sign after the string, with in this 
case is the operator...

Hugo
Nathan Pinno wrote:
 Hugo and all,
 So in order to write what I wanted the correct code would be:
 Computer = %1.0d Player = %1.0d (a,b)
 
 Am I correct or is this wrong?
 
 Nathan Pinno 
 
 -Original Message-
 From: Hugo González Monteverde [mailto:[EMAIL PROTECTED] 
 Sent: November 14, 2005 8:27 PM
 To: Nathan Pinno
 Cc: Tutor mailing list
 Subject: Re: [Tutor] How do I display a changing thing in text?
 
 Hi Nathan,
 
 Take a look at string interpolation in the tutorial. I don't know what
 method you're following for learning Python, but I've found the 'official'
 tutorial a great resource, even when you already know Python,
 
 http://docs.python.org/tut/node9.html#SECTION00910
 
 Nathan Pinno wrote:
 
How do I display a changing thing (i.e.. the score of the game) in text? 
I think it has something to do with this key: %, but I forget how to 
do it. Can someone show me how to do it again?
 
Thanks,
Nathan Pinno


--
--

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


Re: [Tutor] Question about resetting values

2005-11-11 Thread Hugo González Monteverde
There may be more than one way to have it get a zero value. But  in 
languages like Python readability and clarity are valued, and clearly

var = 0

is the clearest way to do it, although

var = var^var

comes to mind, *grin*

Hugo

Nathan Pinno wrote:
 Never mind all. I was just being stupid as usual. I thought that there might 
 be a Python command for resetting and wanted to find out if that was the 
 case.
 - Original Message - 
 From: Alan G [EMAIL PROTECTED]
 To: Nathan Pinno [EMAIL PROTECTED]; Tutor mailing list 
 tutor@python.org
 Sent: Tuesday, August 02, 2005 2:45 PM
 Subject: Re: [Tutor] Question about resetting values
 
 
 
I am writing a Blackjack program, and was wondering how to reset the 
values to zero, e.g. cash = 0?

Yes, that's how you do it. Now what is the bit you don't understand?

Alan G
Author of the Learn to Program web tutor
http://www.freenetpages.co.uk/hp/alan.gauld

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


Re: [Tutor] [Fwd: Re: Spanish text in BS problem]

2005-11-10 Thread Hugo González Monteverde
Hi Ismael,

I'm glad you found the answer. It is very enlightening as I thought I 
had to do with locale and did some tests without getting the problem (I 
do not have BS installed now)

As I speak Spanish this had got me worried. :/

Hugo



Ismael Garrido wrote:
 Found the problem myself.
 (look down)
 
 Ismael Garrido wrote:
 
 
This is the script:

import BeautifulSoup
import os

a = open(zona.htm)
text = a.readlines()
a.close()

BS = BeautifulSoup.BeautifulSoup(str(text))
 
 
 Apparently, str(text) is the cause of the problem. If instead I do: 
 .join(text) it all works allright. I guess this is because str 
 converts 'ó' to '\xf3' while .join() does not change the strings in 
 any way. Now the output from BS makes sense.
 
 Bye,
 Ismael
 
 
for ed in BS('span', {'class':'ed_ant_fecha'}):
   fecha = ed.next.split( )[1].replace(.,-)
   urlynombre = ed.findNextSibling().findNextSibling().findNextSibling()
   url = 'http://espectador.com/' + urlynombre.get('href')
   nombre = urlynombre.next.next

   print url
   print D:/dolina/+fecha, nombre
   print
###end
 
 
 
 ___
 Tutor maillist  -  Tutor@python.org
 http://mail.python.org/mailman/listinfo/tutor
 
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] python watchfolder as daemon

2005-11-10 Thread Hugo González Monteverde
Hi, I do this all the time. I use a recipe, and import a daemon module.

The module is here:
http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/278731

in your script, just do

import daemon

and

daemon.createDaemon()

Works like a charm.

Hugo


Frank Hoffsümmer wrote:
 Hello all,
 I found this gem of a python recipe that allows me to monitor a hierarchy of
 folders on my filesystem for additions / changes / removals of files: 
 http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/215418
 
 I would like to monitor a folder hierarchy using this script but now I
 wonder what would be the best way to have this watchfolder script running at
 all times in the background?
 
 nohup python watchfolder.py myfolder 
 
 ? would that be suitable?? or is there a way to launch a process like that
 from python that detaches itself from the shell?
 what would be the best way, any insight or recommendation is very much
 appreciated
 thanks,
 -frank
 
 
 
 
 
 
 ___
 Tutor maillist  -  Tutor@python.org
 http://mail.python.org/mailman/listinfo/tutor
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] lists strings and files

2005-11-09 Thread Hugo González Monteverde
Have you tried pickling them?

If you want it to be flattened, so that result is:

[fields[1], fields[2], fields[3]]

instead of

[[fields[1], fields[2], fields[3]]]

you should use the extend method instead of the append method.


Hugo
Michael Haft wrote:
 Hello,
  thanks for all the help with lists and strings, the list I have has
 nested sublists in it though. How do I remove these or is there a way
 to convert from lists to strings without removing them?
 
 The code looks something like:
 
 result = []
 data = [fields[1] fields[3] fields[7]]
 result.append(data)
 which gives a list containing 12 sublists
 
 Appologies if the code isn't quite right I'm stuck on a library computer
 in a strange German university and can't use my memory stick to check the
 exact code.
 
 Mike
 
 
 ___
 Tutor maillist  -  Tutor@python.org
 http://mail.python.org/mailman/listinfo/tutor
 
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Python persistent webserver?

2005-11-09 Thread Hugo González Monteverde
Looks like CherryPy is a good candidate. Take a look at:

www.cherrypy.org`

it can be plugged into Apache, and if persistency is what you need, 
you'll get it. There are nice examples for getting started.

Hugo

Howard Kao wrote:
 Hi all,
 
 I know the subject is confusing but I don't know how to describe what
 I would like to ask concisely.
 
 Basically I would like to have a program (or server or whatever) take
 an HTTP POST method (which contains some information, maybe even an
 XML file) from a client, process these XML/information, and then
 generate an XML to send back to the client.
 
 Currently the environment it has to be done under is Apache on Linux. 
 I am thinking that it may have to be a persistent running program... 
 However it seems a daunting task for a Python noob like me.
 
 Preferably it doesn't need threading and need not to be sophiscated at
 all, as long as it can take the request, process info and send stuff
 back to a client.
 
 I have no idea how to do this at all and couldn't find much
 information.  The Base, Simple, CGI HTTPServer modules all seem a
 little lacking , but that's probably due to my lack of py-fu to start
 with...   Any suggestions would be appreciated.  Many thanks!
 ___
 Tutor maillist  -  Tutor@python.org
 http://mail.python.org/mailman/listinfo/tutor
 
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] lists and strings

2005-11-08 Thread Hugo González Monteverde
Hi Mike,

Converting an (almost)arbitrary object into a string is what the Pickle 
module does. CPickle is faster. Take a look into into it in the docs.

Here's an example:

  import cPickle
  lala = [1, 2, 3, 'four', 'V']
  lala
[1, 2, 3, 'four', 'V']


  fileo = open('lala.pkl', 'w')
  cPickle.dump(lala, fileo)
  fileo.close()


  fileo = open('lala.pkl', 'r')
  serialized_data = fileo.read()
  serialized_data
(lp1\nI1\naI2\naI3\naS'four'\np2\naS'V'\na.

  fileo.seek(0)
  recovered = cPickle.load(fileo)
  recovered
[1, 2, 3, 'four', 'V']
 

See the docs and feel free to ask if there is some part oyu do not 
understand.

Hope it helps!

Hugo

Mike Haft wrote:
 Hello,
  I've been working on a problem and have now sorted most of it (thanks
 to some help from the list).
 
 All the ways of writing data to a file I know keep telling me that lists
 can't be written to the file. I'm trying to convert data from one set of
 files into files of a different format. But the easiest way to get the
 data from the first set of files is in a list(s).
 
 So, is there any way to convert lists to strings? Or a way to write lists
 to a file?
 
 Thanks
 
 Mike
 
 ___
 Tutor maillist  -  Tutor@python.org
 http://mail.python.org/mailman/listinfo/tutor
 
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Exit from program early

2005-11-04 Thread Hugo González Monteverde
or

raise SystemExit

Hugo

Johan Geldenhuys wrote:
 Try using 'sys.exit()' where you want the script to stop if you haven't 
 supplied enough arguments.
 in you example, it looks like it will go on to the else anyway AND print 
 the string at the end.
 
 HTH,
 Johan
 
 
 Roy Bleasdale wrote:
 
 
Hi

In the example below I would like the program to stop if I forgot to 
provide an argument . Though I could do all my processing indented under 
the else statement, I was wondering if there was a command that would allow 
me to halt the program execution early.

Regards,

Roy


# Example program - Test for valid argument

import sys

if len(sys.argv)  2:
print opps missing an argument
# Nice if I could stop and exit program here
else:
print Argument provided!!
# Looking good so go do some stuff

print Done some stuff

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

 

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


Re: [Tutor] pack

2005-11-04 Thread Hugo González Monteverde
pack is a method of Tkinter widget that needs to be called in order to 
have it displayed with the current geometry manager(which per default is 
  the pack method)

A geometry manager is just a way for arranging widgets into a window. 
Another geometry manager is grid

If you don't specify anything, the widget will simply not be displayed.. 
see here:

http://effbot.org/tkinterbook/pack.htm


Hugo

Shi Mu wrote:
 what does pack mean in the following code?
 
 # File: hello1.py
 
 from Tkinter import *
 
 root = Tk()
 
 w = Label(root, text=Hello, world!)
 w.pack()
 
 root.mainloop()
 ___
 Tutor maillist  -  Tutor@python.org
 http://mail.python.org/mailman/listinfo/tutor
 
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Capture command output

2005-11-03 Thread Hugo González Monteverde
Hi,

Here's the docs for the popen2 module in python 2.2, the Popen2 object 
allows you to get the pid. The popen2() call allows you to get the 
output in a filehandle.

http://www.python.org/doc/2.2.3/lib/module-popen2.html

You cannot run any function at all in your script after you do the exec 
call, it is no longer your program, but the one you exec-ed. popen 
actually runs the  subprocess, you don't have to call it manually. Popen 
is the most used way to get the output, and looks like it is the only 
one in Python 2.2.

Here's an example.


  import popen2
  myobj = popen2.Popen3('/bin/ls')
  dir(myobj)
['__doc__', '__init__', '__module__', '_run_child', 'childerr', 
'fromchild', 'pid', 'poll', 'sts', 'tochild', 'wait']
  myobj.pid
3033
  output = myobj.fromchild.read()
  output
'Changelog\nblankcursor\ndaemon.py\ndaemon.pyc\nl_index.txt\nl_sec.txt\nloadlin16c.txt\nloadlin16c.zip\nmypipa\nnewlooper.py\nxorg.conf.new\n'
  myobj.wait()
0



Johan Geldenhuys wrote:
 The version of python that I have is 2.2.2 and I can't upgrade, sorry. 
 As far as I know this only from version 2.4.2??
 
 Johan
 
 Kent Johnson wrote:
 
Johan Geldenhuys wrote:
  

I've been musy with the os command on how to kill a process. That's been 
sorted out to an extend. Many thanks for your input.

Now I have a question in the same direction:

I use os.execpv(cmd, [cmd, args]). That executes the command that have 
output. This was the best way of getting the pid and killing the process 
after a certain time.

How do I capture that output to a file?
Can I use os.popen() or something like that after I did the os.execvp() 
execution or is there a standard way of putting any output in a file/



Have you tried using subprocess.Popen()? It supports capturing output of the 
child process and it gives access to the pid of the child.

Kent

  

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


Re: [Tutor] os command

2005-11-02 Thread Hugo González Monteverde
In UNIX, you use the fork() exec() technique for starting a new process, 
  from the very first process(init) onwards. The os.system() uses  a shell
to do that, or you may do it yourself (in your script)

A command is just an executable file running(process), unless you've 
got a library function that does just that, then you could do what you 
must without creating a new process.

Probably your best bet would be to use the subprocess module. It was 
designed around the limitations of the previous methods.

Take a look at what oyu can do with it at:

http://www.python.org/doc/2.4.2/lib/module-subprocess.html

For example, the pid of the command would be available at the pid 
atribute of a subprocess instance.

p.pid

Hugo

Johan Geldenhuys wrote:
 Sorry for the late reply,
 But is it necessary to use a child process? I don't want to execute the 
 command in another process. What happens with the parent process and how 
 do I execute my command in the parent process?
 
 Thanks
 
 Hugo González Monteverde wrote:
 
 Hi,

 os.system will return the errorval of the application. You need to

 1) get the pid of the child process
 2) kill it using os.kill(os.SIGTERM)
 3) reap the killed process

 This is all in unix/linux, of course.

 what I do (untested, please check order of args and correct usage of 
 exec):

 pid = os.fork()

 if pid == 0: #child process
 os.execvp(tcpdump, tcpdump, -n,  -i,  eth0)

 else:   #parent
 time.sleep(5)
 os.kill(pid, os.SIGTERM)
 os.waitpid(pid, 0)   #wait for process to end



 Johan Geldenhuys wrote:

 I have script that calls a system command that I want to run for 5 
 minutes.
 
 import os
 cmd = 'tcpdump -n -i eth0'
 os.system(cmd)
 

 I can start a timer after the cmd is issued, but I don't know how to 
 send a control signal to stop the command after I issued it. This is 
 normally from the shell  ^c.

 This cmd my run in more than one thread on different interfaces and I 
 don't whant all of then stopped at once. I think the best way is to 
 make a thread for each interface where the cmd can be issued and 
 stopped without the danger of stopping he wrong thread.

 Can anybody help?

 Thanks

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


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


Re: [Tutor] os command

2005-11-02 Thread Hugo González Monteverde
Hi

Johan Geldenhuys wrote:
 Hugo,
 
 I see that 'os.execcvp()' doesn't actually make two seperate processes. 
 This is what I have working now:
 
 
 import os, signal, time
 pid = os.fork()
 print 'pid; ',pid
 if pid == 0: #child process
#os.execvp('tcpdump', ['tcpdump', '-i modem0  cap1.txt'])
   os.execvp('ping', ['ping', '127.0.0.1 -c 30  cap.txt'])

The following line (sleep)is superluous, as  it is no longer your python
script after calling exec()
Your process (in this case the child process)is replaced with  a copy of
ping in memory.

  time.sleep(5)
 kill(pid)
 
 def kill(pid, signal=signal.SIGTERM):
print trying to kill pid..., pid
os.kill(pid, signal)
os.waitpid(pid, 0)
print Killed %d%pid
 
 
 The ping command is executed but the part where it must put the replies 
 in the file is not recognized. How do I use the addisional arguments to 
 be seen?
 

Mmm no, this line:

   os.execvp('ping', ['ping', '127.0.0.1 -c 30  cap.txt'])

uses *shell redirection*.. guess who has to interpret the whole  thing..

As you are using no shell, the '' argument is passed to the ping
program, which knows nothing of handling it the way you expect it.

What you're doing is equivalent to (in my workstation)

[EMAIL PROTECTED]:~# ping  192.168.0.8   cap.txt
ping: unknown host 

You have to call a shell if you want to use redirection, otherwise you
will have to use python to send all of ping's output to a file (which
would be a nice exercise, IMHO ;) )

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


  1   2   >