Re: [Tutor] updating Unix config file

2009-10-20 Thread Tiago Saboga
On Tue, Oct 20, 2009 at 2:44 PM, Matt Herzog  wrote:
> Yes, thanks. What failed was the invocation of PIPE. Apparently I had to 
> explicitly import PIPE from subprocess or python had no clue as to what PIPE 
> was!
>
> Dare I say, "wtf?" since when fo I have to specify such? Shouldn't importing 
> the subprocess module make all its methods available? I can't remember having 
> to do this before.

It is really like any other module. If you just import subprocess, you
can access all its methods and attributes by spelling it out:

import subprocess
handler = subprocess.Popen(['cat'], stdout=subprocess.PIPE,
stdin=subprocess.PIPE)

Or you can import just the names you will use in the global namespace:

from subprocess import Popen, PIPE
handler = Popen(['cat'], stdout=PIPE, stdin=PIPE)

HTH,

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


Re: [Tutor] re module / separator

2009-06-25 Thread Tiago Saboga
Thanks Kent! Once more you go straight to the point!

Kent Johnson  writes:
> On Wed, Jun 24, 2009 at 2:24 PM, Tiago Saboga wrote:
>> In [33]: re.search("(a[^.]*?b\.\s?){2}", text).group(0)
>> Out[33]: 'a45453b. a325643b. '
>
> group(0) is the entire match so this returns what you expect. But what
> is group(1)?
>
> In [6]: re.search("(a[^.]*?b\.\s?){2}", text).group(1)
> Out[6]: 'a325643b. '
>
> Repeated groups are tricky; the returned value contains only the first
> match for the group, not the repeats.

The problem was exactly that. I had seen that findall got the first
group of the match, but not that this would not span repeats. But it
makes sense, as the repeat count is after the parens. 

> If you change the inner parentheses to be non-grouping then you get
> pretty much what you want:
>
> In [8]: re.findall("((?:a[^.]*?b\.\s?)+)", text)
> Out[8]: ['a2345b. ', 'a45453b. a325643b. a435643b. ']

And the trick of the non-grouping parens is great too. Thanks again!

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


Re: [Tutor] re module / separator

2009-06-24 Thread Tiago Saboga
Serdar Tumgoren  writes:

> Hey Tiago,
>
>> text = "a2345b. f325. a45453b. a325643b. a435643b. g234324b."
>>
>> Of this line of text, I want to take out strings where all words start
>> with a, end with "b.". But I don't want a list of words. I want that:
>>
>> ["a2345b.", "a45453b. a325643b. a435643b."]
>>
>
> Are you saying you want a list of every item that starts with an "a"
> and ends with a "b"? If so, the above list is not what you're after.
> It only contains two items:
>   a2345b.
>   a45453b. a325643b. a435643b.

Yes, I want to find only two items. I want every sequence of words where
every word begins with an "a" and ends with "b.".

> Try reading this:
> http://www.amk.ca/python/howto/regex/

I have read several times, and I thought I understood it quite well ;)

I have not the time right now to do it, but if it turns out to be
useful, I can show why I came to the patterns I sent to the list.

Thanks,

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


[Tutor] re module / separator

2009-06-24 Thread Tiago Saboga
Hi!

I am trying to split some lists out of a single text file, and I am
having a hard time. I have reduced the problem to the following one:

text = "a2345b. f325. a45453b. a325643b. a435643b. g234324b."

Of this line of text, I want to take out strings where all words start
with a, end with "b.". But I don't want a list of words. I want that:

["a2345b.", "a45453b. a325643b. a435643b."]

And I feel I still don't fully understand regular expression's logic. I
do not understand the results below:

In [33]: re.search("(a[^.]*?b\.\s?){2}", text).group(0)
Out[33]: 'a45453b. a325643b. '

In [34]: re.findall("(a[^.]*?b\.\s?){2}", text)
Out[34]: ['a325643b. ']

In [35]: re.search("(a[^.]*?b\.\s?)+", text).group(0)
Out[35]: 'a2345b. '

In [36]: re.findall("(a[^.]*?b\.\s?)+", text)
Out[36]: ['a2345b. ', 'a435643b. ']


What's the difference between search and findall in [33-34]? And why I
cannot generalize [33] to [35]? Out[35] would make sense to me if I had
put a non-greedy +, but why do re gets only one word?

Thanks,

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


Re: [Tutor] subprocess Popen

2009-04-03 Thread Tiago Saboga
On Fri, Apr 03, 2009 at 03:12:08PM -0700, Weidner, Ronald wrote:
> I have a long running py script that I'm trying to kick off from
> another long running py script as a separate process... If either
> script fails or terminates, I don't want the other script to be
> effected as a result. In other words I need a separate process not a
> new thread. In any case, this code I thought would work but it
> doesn't.
> 

> someLongRunningScript= ( "%s/someLongRunningScript.py" % ( os.getcwd() ) )
> someLongRunningScriptArgs= ( '--verbose --directory=%s --link=%s %s' % ( 
> directory_name, link_name, span_option ) )
> longCommand = ( '%s %s' % ( someLongRunningScript, someLongRunningScriptArgs) 
> )
> pid = subprocess.Popen ( ["/usr/bin/python", longCommand ] ).pid
> print ( "/usr/bin/python %s " % ( longCommand ) )

> 
> What's interesting is that if I copy the text printed in the 5th
> line, paste into my shell, then run it -- the process I'm trying to
> start works perfectly.
> 
> The problem seems to have something to do with the way arguments are
> being passed to the python script named someLongRunningProcess.py.

Yes, the arguments are not passed to the shell. There is a Popen
option to pass them to a shell, perhaps it would work. But the simpler
way is to write:

pid = subprocess.Popen(["/usr/bin/python"] + longCommand.split()).pid

It will not work if some arguments are quoted strings with spaces. It
would be better if longCommand was already a list.

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


Re: [Tutor] how to run a process forever

2008-12-10 Thread Tiago Saboga
On Wed, Dec 10, 2008 at 11:29:10AM -0800, Steve Willoughby wrote:
> On Wed, Dec 10, 2008 at 01:15:18PM -0600, shawn bright wrote:
> > i am running this on a linux computer. How would i check that it is
> > running? (from cron part)
> 
> Typically you have your program write its PID to a file.  The cron
> script can check that file and see if that process is still alive before
> deciding to start another.

If you are in a debian-based system, see start-stop-daemon (possibly
available in other distros too).

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


Re: [Tutor] Checking the desktop environment

2008-06-20 Thread Tiago Saboga
On Sat, Jun 21, 2008 at 01:00:36AM +0100, Alan Gauld wrote:
> "Timo" <[EMAIL PROTECTED]> wrote
>
>> graphical box for root-password.
>
> This is the sort of thing that I meant about modern Unices messing
> things up. Why on earth should anyone need a user environment
> specific su box. That should be a standard dialog controlled by the
> app and the user environment controlling the GUI aspects. Too
> many Windows programmers are working on Linux IMHO! ;-)

FWIW, Debian tries to address this mess with a su-to-root script which
tries to do the right thing, launching gksu, kdesu or simply su in a
new xterm. 

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


Re: [Tutor] Replace sequence in list - looking for direction

2008-06-16 Thread Tiago Saboga
On Mon, Jun 16, 2008 at 11:41:25AM -0400, GTXY20 wrote:
> Hello all,
> 
> Thanks in advance for any thoughts, direction, and guidance.
> 
> Let's say I have a list like the following:
> 
> a = ['a1','a2','a3','a4','a5','a6']
> 
> and then I have dictionary like the following:
> 
> b = {'a1,a2,a3':'super'}
> 
> I need some direction and thoughts on how I might seach the list for the
> string (the key in dict b) sequence in list a and replace the occurance with
> the value from dict b. At the end I would like to have list a represented
> as:
> 
> a = ['super', 'a4', 'a5', 'a6']

The following works for your example. I assume the values in the a
list are unique.

for key in b:
keylist = key.split(',')
if keylist < a:
i = a.index(keylist[0])
print a[:i] + [b[key]] + a[i+len(keylist):]

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


Re: [Tutor] for loop

2008-06-10 Thread Tiago Saboga
On Tue, Jun 10, 2008 at 09:36:47PM -0400, Sean Novak wrote:
> I know I'm going to feel stupid on this one..
>
> I would normally write this in PHP like this:
>
> for($i=1; i< count($someArray); $i++)
> {
>   print $someArray[i]
> }
>
> essentially,, I want to loop through an array skipping "someArray[0]"
>
> but in python the for syntax is more like foreach in PHP..
>
> I've tried this to no avail
>
>  count = 0
>  for i in range(1,10):
>if count == 0:
>   continue
>   else:
>   count += 1
>   print i
>   continue
>
> it prints absolutely nothing.

The count var is never updated. What about:

for i in someArray[1:]:
print i


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


Re: [Tutor] if ... else shorthand form ?

2008-02-09 Thread Tiago Saboga
On Sat, Feb 09, 2008 at 09:21:41AM +, dave selby wrote:
> Hi all,
> 
> Returning to python after a brief affair with C++, I have the following code 
> ...
> 
> if (items.has_keys('snapshot_interval')):
> self.snap_init[i] = items['snapshot_interval']
> else:
> self.snap_init[i] = 0
> 
> I thought Python had a shorthand version of something like 
> 
> self.snap_init[i] =
> (items.has_keys('snapshot_interval'))?items['snapshot_interval']:0

self.snap_init[1] = items.get('snapshot_interval', 0)

Or, if you also want to set the value on items,

...   = items.setdefault('snapshot_interval', 0)

[]s

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


Re: [Tutor] designing POOP

2008-02-06 Thread Tiago Saboga
On Wed, Feb 06, 2008 at 08:58:09AM -, Alan Gauld wrote:
> Thus always base inheriotance on common mbehaviour not on
> common data.
> 
> I hope that makes it clearer.

Thanks Alan, it's clear now. And now I know that this is not one of
the mistakes I am making ;)

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


Re: [Tutor] designing POOP

2008-02-05 Thread Tiago Saboga
On Tue, Feb 05, 2008 at 08:02:19PM -, Alan Gauld wrote:
> "bhaaluu" <[EMAIL PROTECTED]> wrote
> 
> > Now I am interested in learning how to DESIGN an object-oriented 
> > version

The question was very interesting, and so where the answers.

> Don't at this stage worry too much about inheritance.
> Focus on function. If you find that several classes have
> the same or similar methods then consider if they are
> sub types of an abstract superclass. Do NOT use data
> attributes for this, always base inheritance heirarchies
> on behaviour.

Could you please elaborate on this last sentence? I don't understand
what you mean, and I feel I may be on this track.

Thanks,

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


Re: [Tutor] Choice of GUI builders

2008-01-03 Thread Tiago Saboga
On Thu, Jan 03, 2008 at 02:11:03AM -0800, Marc Tompkins wrote:
> on, and its idiom felt more comfortable to me than the others.  Also, unlike
> Qt, it's free... I hate to be a cheapskate, but I'm a very small business
> and I need to put food on my family, so the Qt license is a major hurdle.

But since 2005, according to wikipedia, the Qt Windows is also
licensed under the GPL. Am I missing something?

> 
> I have to say, though, that as a recovering Visual Studio user there is one
> thing - believe me, it's the only thing! - I miss, and that's an
> honest-to-goodness WYSIWYG GUI designer.  For the love of Pete, if Microsoft
> can get it right - and it's the only thing they did get right, IMHO - why
> can't we?  Why can't you just draw your GUI on the screen and concentrate on
> the actual functionality in peace?

It's why I finally started using Qt. In fact I do not really like
WYSIWYG designers - I always remember html produced by Dreamweaver and
co. - but qtdesigner + pyuic (see pyqt) always gave me readable code. 

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


Re: [Tutor] Learning about callbaks

2007-12-30 Thread Tiago Saboga
On Sun, Dec 30, 2007 at 09:17:42AM -, Alan Gauld wrote:
> Yes, the Python tutor list is one of the best features of Python.

This expresses exactly the way I feel about python. Everytime I have
to work in another language, I keep asking myself: but where is the
tutor mailing list for this language?

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


Re: [Tutor] ipython / readline problem

2007-12-13 Thread Tiago Saboga
On Thu, Dec 13, 2007 at 07:41:08AM -0500, Kent Johnson wrote:
> Tiago Saboga wrote:
>> : 'ascii' codec can't encode
>> character u'\xe7' in position 2: ordinal not in range(128)
>>
>> ===
>>
>> What's happening? Why do the readline methods accept a multibyte
>> string ('ação') but not a unicode (u'ação')?
>
> I don't know what is happening with readline but this error is usually the 
> result of converting a Unicode string to a plain string without specifying 
> encoding, either explicitly by calling str() or implicitly such as in a 
> print statement:

I already knew that, but it helped somehow ;) 

Apparently the problem is that ipython converts the input to unicode,
while the readline module wants a string object. With single line
input, ipython doesn't interfere with readline, but for multiline
input, it updates readline's history, but it tries to to that with the
unicode object. I've sent a patch to the ipython bug to reencode the
string to sys.stdin.encoding before submitting it to readline.

But in general, in the python libraries, I thought it would be safe to
assume that one can equally send a string or a unicode object, and
that otherwise there would be a warning in the docs. Is this
assumption plain wrong, is this info really missing in the docs, or
it's just me that have missed it?

Thanks,

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


[Tutor] ipython / readline problem

2007-12-13 Thread Tiago Saboga
Hi,

I am facing what seems like a bug, and the docs are not enough to tell
me where the bug lies. In ipython, I can't type a multiline unicode
string. The bug is reported here [1], and the output is like that
(except that the commented out lines are modifications I have done):

===
In [1]: a = '''ç
   ...: '''
---
Traceback (most recent call last)

/var/lib/python-support/python2.5/IPython/iplib.py in raw_input(self, prompt, 
continue_prompt)
   2041 self.readline.remove_history_item(histlen-1)
   2042 # newhist = newhist.encode('ascii', 'replace')
-> 2043 
self.readline.replace_history_item(histlen-2,newhist)
   2044 #self.input_hist_raw[-1] = newhist
   2045 except AttributeError:

: 'ascii' codec can't encode character 
u'\xe7' in position 7: ordinal not in range(128)

===

So I went to the ipython source code to see what was wrong and finally
I discovered that one can't pass unicode strings to readline. To be
clearer, the behaviour above is found only in ipython, but the errors
below can be reproduced also in the python console:

===

In [6]: readline.add_history('ação')

In [7]: readline.get_current_history_length()
Out[7]: 1008

In [8]: readline.replace_history_item(1008, 'ação')

In [9]: readline.replace_history_item(1008, u'ação')
---
Traceback (most recent call
last)

/home/tiago/ in ()

: 'ascii' codec can't encode
characters in position 1-2: ordinal not in range(128)

In [10]: readline.add_history(u'caça')
---
Traceback (most recent call
last)

/home/tiago/ in ()

: 'ascii' codec can't encode
character u'\xe7' in position 2: ordinal not in range(128)

===

What's happening? Why do the readline methods accept a multibyte
string ('ação') but not a unicode (u'ação')?

Thanks,

Tiago.

[1] - http://projects.scipy.org/ipython/ipython/ticket/201
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] thread and os.pipe

2007-12-13 Thread Tiago Saboga
On Sun, Dec 09, 2007 at 12:25:59AM -, Alan Gauld wrote:
> > updated to show the output from the commands. What I want to do
> > is make the doit() method of the Converter class return a pipe,
> > through which it will then send the output of the programs.
> 
> I'm still not convinced that a pipe is the best solution here.
> You could simply register the UI View with the converter (in usual MVC
> design pattern) and get the converter to either send updates to the
> UI (or notify the UI to request updates) as needed. That way you
> only need a regular Python method in the Converter to deliver the
> data and another in the UI code to respond to the notification.
> No pipes or queues required.

Thanks Alan, but finally I got it working without pipes, but with a
queue. I was afraid of getting into a new world, but it turned out to
be easier than I thought. OTOH, I am really not comfortable with MVC;
I am using some MVC patterns as I don't have the choice, but I still
can't really understand the concepts. But it's another point...

Thanks again,

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


Re: [Tutor] logic for a tree like structure

2007-12-09 Thread Tiago Saboga
On Sat, Dec 08, 2007 at 04:13:19PM -0800, Jeff Younker wrote:
> Pipes and IO channels are buffered.   The buffers are much larger than
> the amount of information you are writing to them, so they're never getting
> flushed while the program is running.   The child program completes, the
> IO channel closes, and it flushes out the output.

Yes! I've just noticed that if I replaced the python writer.py with an
equivalent bash script it worked as expected (my original example).
And then, I could make it work with writer.py if I flush sys.stdout
after each print statement. It is a little frustrating to see
that I was not able to see something that already bit me other times,
but... c'est la vie...

> My advice is to forget about all the file descriptor and pipes stuff.  
> Getting
> incremental IO from them via the subprocess module (or any standard IO
> module) is painful.   You're probably better off getting the nonstandard
> pexpect module and using that instead.

Thanks, it sounds like a useful module (and I don't mind it being
nonstandard, as long as it is in debian - and it is ;) ) but I still
can't see how to do something like that in my case. As I mentioned in
another mail, I have two independent classes, one for the gui and the
other for the converter. The converter has to spawn several processes
depending on the return code of each of them and then do some clean
up. In this mean time (it can take hours), the gui must be alive and
updated with the stdout of the processes.

Tiago.

> Here's your program using pexpect.
>
> #/usr/bin/python2.5
>
> import pexpect
>
> def main():
>cmd = pexpect.spawn('./writer.py')
>try:
>while True:
># wait for the end of the line or ten seconds
>cmd.expect('\r\n', timeout=10)
># get output preceding the EOF matched above
>line = cmd.before
>print '[main]', line
>except pexpect.TIMEOUT:
>  print "No output received for ten seconds"
>except pexpect.EOF:
>  pass
>finally:
> cmd.close()
>
> if __name__ == '__main__':
>   main()
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] thread and os.pipe

2007-12-08 Thread Tiago Saboga
On Sat, Dec 08, 2007 at 11:54:07AM -, Alan Gauld wrote:
> "Tiago Saboga" <[EMAIL PROTECTED]> wrote
> 
> > what's happening in this simple example. I want to establish a
> > connection between my two threads by a os.pipe,
> 
> While its possible to use a pipe to communicate within a process
> its not very helpful and very rarely done. Usially queues are used
> for communication between threads

I find - and while googling I discovered I am not alone - very
difficult to grasp all details of thread programming. I am trying to
keep it simple, it's why I have chosen this way. 

> > I was hoping to see the output of writer.py come out in real time
> 
> But you aren't really using a thrwad in your code you are
> spawning a new subprocess using Popen and reading the
> output of that from a pipe. are you sure you really need to
> do that in a thread? 

This was just a sandbox; I am trying to make something very simple
work before applying it to my program.

> You can simply dip into the pipe and
> read it on demand. The usual case for a thread is when
> you want to perform two tasks concurrently within the same
> process, not to run another parallel process.,
> 
> > ... but it is coming out all together when writer.py returns. Why?
> 
> Dunno, it depends on what writer.py is doing. If its writing
> to stdout then the puipe should be able to read the rewsults
> as you go.

The writer.py file was included in my mail. ;)
It just prints out a test line every few seconds. As I said, it's just
a test.

> 
> >> from __future__ import with_statement
> >> import thread
> >> import subprocess
> >> import os
> >>
> >> def run(out):
> >> subprocess.Popen(['./writer.py'], stdout=os.fdopen(out, 'w'))
> >>
> >> def main():
> >> out_r, out_w = os.pipe()
> >> thread.start_new_thread(run, (out_w,))
> 
> Why not just set up outw to the output of Popen?

Because in the more complicated case, the run function would have
several steps, including some external processes with Popen. The run()
function will take care of determining which processes will run,
according to the returncode of the first process), and will have to do
some cleaning in the end. But as this happens, I want my my main()
function to analyse and print the output from the Popen while the
external processes are running. This is what I wanted in this simple
test. I am sorry if I am not clear enough, I am still learning both
python and english!

As for the real program I am making, it's a gui for ffmpeg and several
other little programs to convert video files. The gui is written in a
single class, subclassing the qt-designer/pyuic generated one. I am trying
to make my two auxiliary classes totally independent, as I would love
to make a curses version later. So I have a Converter class which does
the actual job of calling the back-end, but I want the gui to be
updated to show the output from the commands. What I want to do
is make the doit() method of the Converter class return a pipe,
through which it will then send the output of the programs. 

Thanks for your answer, as for the excellent tutorial.

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


[Tutor] thread and os.pipe (was: logic for a tree like structure)

2007-12-08 Thread Tiago Saboga
I started a message, changed my mind, wrote about some other thing and
forgot to change the subject. Sorry for the noise.

Tiago.


On Sat, Dec 08, 2007 at 07:25:16AM -0200, Tiago Saboga wrote:
> Hi,
> 
> I need an additional thread in my app, and I am trying to understand
> how it works. Alas, all I found about threads is too simple (does not
> fit in my real life problem) or too complicated. The best thing I've
> read until now is the great tutorial by Norman Matloff, but it isn't
> enough. If you could point me to some more docs, it would be great.
> The python docs have been of no use for me in this case.
> 
> Anyway, I am trying to play a little with it, and I can't understand
> what's happening in this simple example. I want to establish a
> connection between my two threads by a os.pipe, but it seems like I
> can only read the pipe when everything was done. I was hoping to see
> the output of writer.py come out in real time, but it is coming out
> all together when writer.py returns. Why?
> 
> Thanks,
> 
> Tiago Saboga.
> 
> =writer.py===
> 
> #!/usr/bin/python2.5
> 
> import time
> 
> for i in range(10):
> print 'Line number %s.' % i
> time.sleep(1)
> 
> ==threadit.py===
> #!/usr/bin/python2.5
> 
> from __future__ import with_statement
> import thread
> import subprocess
> import os
> 
> def run(out):
> subprocess.Popen(['./writer.py'], stdout=os.fdopen(out, 'w'))
> 
> def main():
> out_r, out_w = os.pipe()
> thread.start_new_thread(run, (out_w,))
> with os.fdopen(out_r) as f:
> while True:
> line=f.readline()
> if line:
> print '[main]', line
> 
> if __name__=='__main__':
> main()
> ___
> 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] logic for a tree like structure

2007-12-08 Thread Tiago Saboga
Hi,

I need an additional thread in my app, and I am trying to understand
how it works. Alas, all I found about threads is too simple (does not
fit in my real life problem) or too complicated. The best thing I've
read until now is the great tutorial by Norman Matloff, but it isn't
enough. If you could point me to some more docs, it would be great.
The python docs have been of no use for me in this case.

Anyway, I am trying to play a little with it, and I can't understand
what's happening in this simple example. I want to establish a
connection between my two threads by a os.pipe, but it seems like I
can only read the pipe when everything was done. I was hoping to see
the output of writer.py come out in real time, but it is coming out
all together when writer.py returns. Why?

Thanks,

Tiago Saboga.

=writer.py===

#!/usr/bin/python2.5

import time

for i in range(10):
print 'Line number %s.' % i
time.sleep(1)

==threadit.py===
#!/usr/bin/python2.5

from __future__ import with_statement
import thread
import subprocess
import os

def run(out):
subprocess.Popen(['./writer.py'], stdout=os.fdopen(out, 'w'))

def main():
out_r, out_w = os.pipe()
thread.start_new_thread(run, (out_w,))
with os.fdopen(out_r) as f:
while True:
line=f.readline()
if line:
print '[main]', line

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


Re: [Tutor] Questions about wxEvents

2007-12-06 Thread Tiago Saboga
On Thu, Dec 06, 2007 at 09:12:59AM -0800, Marc Tompkins wrote:
> I have a specific question - how can I generalize a
> FileBrowseButtonWithHistory - and I realized, as I was trying to word my
> question, that my real question is a bit more generic.
> 
> First, the specific question:  The FileBrowseButtonWithHistory requires a
> callback override and some custom code to straighten out handling the
> history.  So far, so good.  However, I wish to use more than one FBBWH on my
> form, and I can't figure out how to re-use the custom callback so it will
> work for both controls.  (It makes me sick to my stomach when I look at my
> code and see duplicate blocks!)   Don't get me wrong - it's working right
> now, it's just that my code is fugly and I want to clean it up.
> 
> In more general terms, how can I set more than one control to use the same
> block of code as a custom callback, and figure out at runtime which control
> I'm responding to?  Doesn't the Event or CommandEvent carry any information
> about itself?
> I've tried this:
> 
> [snip]
> self.fp1 = filebrowse.FileBrowseButtonWithHistory(pnl, -1,
> size=(300, -1),
> labelText='', fileMask='*.*', fileMode=wx.OPEN,
> dialogTitle='Select the file containing UCF claims',
> changeCallback=self.fp1Callback)
> self.fp2 = filebrowse.FileBrowseButtonWithHistory(pnl, -1,
> size=(300, -1),
> labelText='', fileMask='FI*.*', fileMode=wx.OPEN,
> dialogTitle='Select the form-image file - generally starts with
> FI', changeCallback=self.fp2Callback)
> [snip]
> def fp1Callback(self, evt):
> print evt.__dict__
> print help(evt)
> value = evt.GetString()
> [snip]
> def fp2Callback(self, evt):
> print evt.__dict__
> print help(evt)
> value = evt.GetString()
> [snip]

I have never used wx, but I faced the same question with the qt
toolkit, and I solved it with a function that returns a function. In
my case, I have several widgets to connect to a single function
(setValue), and in some cases they have to be connected also with a
particular function (always named set_AttributeName).

Here's my snippet:

self.connect(widget, QtCore.SIGNAL(signal),
 self.get_setValue_func(option))
[...]
def get_setValue_func(self, option):
def setIt(value):
   try:
   method = self.__getattribute__("set_%s" % option)
   method(value)
   except AttributeError:
   pass
self.setValue(option, value)
return setIt

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


Re: [Tutor] PyQt segfault

2007-12-01 Thread Tiago Saboga
On Sat, Dec 01, 2007 at 01:04:04AM +0100, David Boddie wrote:
> Something like that, yes. The internal character data becomes invalid, but
> there's still a reference to the QString object.
> 
> Here's the original code posted by Tiago:
> 
> > class Combobox(QtGui.QDialog):
> > def __init__(self):
> > QtGui.QDialog.__init__(self)
> > self.ui = Ui_Dialog()
> > self.ui.setupUi(self)
> > 
> > self.connect(self.ui.comboBox, QtCore.SIGNAL("activated(QString)"),
> >  self.save)
> > def save(self, qstring):
> > # Here it works:
> > #Aux.mystring = unicode(qstring)
> > Aux.mystring = qstring
> 
> The correct way to handle this is commented out: to take a copy of the data,
> as you pointed out. This could be done by converting it to a Python unicode
> object, as shown, or by copying the QString:
> 
>   Aux.mystring = QString(string)
>
> [Off topic, but PyQt-related: You need to explicitly copy value types with
> PyQt because the semantics of copying objects with Python are different to
> those for copying Qt's value classes in C++. In Python, you just bind an
> object to a name, but the equivalent assignment in C++ is basically just
> creating an additional reference to the same object.]

If I understand that correctly, my Aux.mystring is pointing to the
same object passed by QtCore.SIGNAL, which is being garbage-collected?
But the reference in Aux.mystring should not be enough to keep the
object around?

Thanks,

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


Re: [Tutor] lstrip removes '/' unexpectedly

2007-11-30 Thread Tiago Saboga
Read this:


In [50]: print str.lstrip.__doc__
S.lstrip([chars]) -> string or unicode

Return a copy of the string S with leading whitespace removed.
If chars is given and not None, remove characters in chars instead.
If chars is unicode, S will be converted to unicode before stripping
---

The point is "characters in chars", not strings.

---
In [52]: 'abababbbaacaaba'.lstrip('ab')
Out[52]: 'caaba'
---

Cheers,

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


Re: [Tutor] PyQt segfault

2007-11-30 Thread Tiago Saboga
First, I just noticed I sent a non-working version of the code. The line

print ("The same" if Aux.mystring==Aux.mystring2 else "Different")

should be 

print ("The same" if Aux.mystring==Aux.compare else "Different")

Sorry for that.

On Fri, Nov 30, 2007 at 01:13:39PM -, Alan Gauld wrote:
> "Tiago Saboga" <[EMAIL PROTECTED]> wrote
> > I am making a front-end to ffmpeg with pyqt and I am stuck in a
> > segmentation fault I can't understand. I have written a little
> 
> > def main(args):
> >app = QtGui.QApplication(args)
> >win = Combobox()
> >win.show()
> >app.exec_()
> >
> >print type(Aux.mystring)
> ># If uncommented segfaults
> ># mystring = unicode(Aux.mystring)
> >print ("The same" if Aux.mystring==Aux.mystring2 else 
> > "Different")
> 
> It looks from this that it has nothing to do with Qt per se.
> Does unicode() work OK outside of Qt?

Not only outside of Qt, but even with a constructed QString. That's
why I have put in the example the compare() function. It wasn't clear
enough, but I have a segfault a few lines later:

print type(Aux.mystring)
# If uncommented segfaults
# mystring = unicode(Aux.mystring)
print ("The same" if Aux.mystring==Aux.mystring2 else "Different")
print Aux.compare 
# segfault
print Aux.mystring

Aux.compare is a QString constructed directly (QString("Second")) and
Aux.mystring constructed read from ComboBox, but originally with the
same string:

self.comboBox.addItem(QtGui.QApplication.translate("Dialog", "Second",
None, QtGui.QApplication.UnicodeUTF8))

I have also tried constructing Aux.compare with
QtGui.QApplication.translate, with the same args, and still no
problems! 

> Or is Aux.mystring an insance of a QtString class that you need to
> deference to get the real string that unicode will understand?

Both Aux.mystring and Aux.compare have the same type,
PyQt4.QtCore.QString.

> All I can think of, not knowing anything much of Qt.

Thank you anyway. I am sending a new version of my test case attached,
and I will try to run it in another machine to see if it's a local
problem.

Tiago.
#!/usr/bin/python2.5
# -*- coding: utf-8 -*-

from PyQt4 import QtCore, QtGui
from comboboxsegfault import Ui_Dialog
import sys

class Globals:
pass

class Combobox(QtGui.QDialog):
def __init__(self):
QtGui.QDialog.__init__(self)
self.ui = Ui_Dialog()
self.ui.setupUi(self)

self.connect(self.ui.comboBox, QtCore.SIGNAL("activated(QString)"),
 self.save)
self.connect(self, QtCore.SIGNAL("finished(int)"),
 self.finish)
def save(self, qstring):
# With a unicode invocation here it works:
#Aux.mystring = unicode(qstring)
Globals.changedtext = qstring
def finish(self):
Globals.currenttext = self.ui.comboBox.currentText()

def main(args):
app = QtGui.QApplication(args)
win = Combobox()
win.show()
app.exec_()
print "Constructed with QString: %s" % Globals.qst
print "Constructed with QApp.translate: %s" % Globals.qtr
print "Got from combobox.currentText(): %s" % Globals.currenttext
try:
print "Passed by combobox activated() signal: %s" % Globals.changedtext
except AttributeError:
print "Change the choice in combobox to trigger the segfault."
def compare(args):
Globals.qst = QtCore.QString("Second")
Globals.qtr = QtGui.QApplication.translate("Dialog", "Second",
None, QtGui.QApplication.UnicodeUTF8)

if __name__=='__main__':
compare(sys.argv)
main(sys.argv)
# -*- coding: utf-8 -*-

# Form implementation generated from reading ui file 'comboboxsegfault.ui'
#
# Created: Fri Nov 30 11:55:12 2007
#  by: PyQt4 UI code generator 4.3
#
# WARNING! All changes made in this file will be lost!

from PyQt4 import QtCore, QtGui

class Ui_Dialog(object):
def setupUi(self, Dialog):
Dialog.setObjectName("Dialog")
Dialog.resize(QtCore.QSize(QtCore.QRect(0,0,400,300).size()).expandedTo(Dialog.minimumSizeHint()))

self.buttonBox = QtGui.QDialogButtonBox(Dialog)
self.buttonBox.setGeometry(QtCore.QRect(30,240,341,32))
self.buttonBox.setOrientation(QtCore.Qt.Horizontal)
self.buttonBox.setStandardButtons(QtGui.QDialogButtonBox.Cancel|QtGui.QDialogButtonBox.NoButton|QtGui.QDialogButtonBox.Ok)
self.buttonBox.setObjectName("buttonBox")

self.comboBox = QtGui.QComboBox(Dialog)
self.comboBox.set

[Tutor] PyQt segfault

2007-11-30 Thread Tiago Saboga
Hi!

I am making a front-end to ffmpeg with pyqt and I am stuck in a
segmentation fault I can't understand. I have written a little
dialogbox just to show the problem. I have already found a workaround,
commented out in the code, but I would like to know where is the
problem.

I am sending the pyuic-generated file in attachment, as well as ui file.

I am working in a debian/lenny system, with py 2.4 and 2.5.

cbox.py
===

#!/usr/bin/python2.5
# -*- coding: utf-8 -*-

from PyQt4 import QtCore, QtGui
from comboboxsegfault import Ui_Dialog
import sys

class Aux:
pass

class Combobox(QtGui.QDialog):
def __init__(self):
QtGui.QDialog.__init__(self)
self.ui = Ui_Dialog()
self.ui.setupUi(self)

self.connect(self.ui.comboBox, QtCore.SIGNAL("activated(QString)"),
 self.save)
def save(self, qstring):
# Here it works:
#Aux.mystring = unicode(qstring)
Aux.mystring = qstring
 
def main(args):
app = QtGui.QApplication(args)
win = Combobox()
win.show()
app.exec_()

print type(Aux.mystring)
# If uncommented segfaults
# mystring = unicode(Aux.mystring)
print ("The same" if Aux.mystring==Aux.mystring2 else "Different")
print Aux.compare
# segfault
print Aux.mystring
def compare(args):
st = QtCore.QString("Second")
Aux.compare = st

if __name__=='__main__':
compare(sys.argv)
main(sys.argv)

# -*- coding: utf-8 -*-

# Form implementation generated from reading ui file 'comboboxsegfault.ui'
#
# Created: Fri Nov 30 09:43:39 2007
#  by: PyQt4 UI code generator 4.3
#
# WARNING! All changes made in this file will be lost!

from PyQt4 import QtCore, QtGui

class Ui_Dialog(object):
def setupUi(self, Dialog):
Dialog.setObjectName("Dialog")
Dialog.resize(QtCore.QSize(QtCore.QRect(0,0,400,300).size()).expandedTo(Dialog.minimumSizeHint()))

self.buttonBox = QtGui.QDialogButtonBox(Dialog)
self.buttonBox.setGeometry(QtCore.QRect(30,240,341,32))
self.buttonBox.setOrientation(QtCore.Qt.Horizontal)
self.buttonBox.setStandardButtons(QtGui.QDialogButtonBox.Cancel|QtGui.QDialogButtonBox.NoButton|QtGui.QDialogButtonBox.Ok)
self.buttonBox.setObjectName("buttonBox")

self.comboBox = QtGui.QComboBox(Dialog)
self.comboBox.setGeometry(QtCore.QRect(60,30,231,31))
self.comboBox.setObjectName("comboBox")

self.retranslateUi(Dialog)
QtCore.QObject.connect(self.buttonBox,QtCore.SIGNAL("accepted()"),Dialog.accept)
QtCore.QObject.connect(self.buttonBox,QtCore.SIGNAL("rejected()"),Dialog.reject)
QtCore.QMetaObject.connectSlotsByName(Dialog)

def retranslateUi(self, Dialog):
Dialog.setWindowTitle(QtGui.QApplication.translate("Dialog", "Dialog", None, QtGui.QApplication.UnicodeUTF8))
self.comboBox.addItem(QtGui.QApplication.translate("Dialog", "First", None, QtGui.QApplication.UnicodeUTF8))
self.comboBox.addItem(QtGui.QApplication.translate("Dialog", "Second", None, QtGui.QApplication.UnicodeUTF8))


 Dialog
 
  
   
0
0
400
300
   
  
  
   Dialog
  
  
   

 30
 240
 341
 32

   
   
Qt::Horizontal
   
   

QDialogButtonBox::Cancel|QDialogButtonBox::NoButton|QDialogButtonBox::Ok
   
  
  
   

 60
 30
 231
 31

   
   

 First

   
   

 Second

   
  
 
 
 
  
   buttonBox
   accepted()
   Dialog
   accept()
   

 248
 254


 157
 274

   
  
  
   buttonBox
   rejected()
   Dialog
   reject()
   

 316
 260


 286
 274

   
  
 

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


Re: [Tutor] using python to execute from Dir A in Dir B

2007-10-04 Thread Tiago Saboga
On Thu, Oct 04, 2007 at 03:15:41PM -0400, Andre Walker-Loud wrote:
> what I need to do is have my python script run another executable,  
> but it must do it from a directory different from the one I am in, it  
> needs to run in the /scratch/ directory (again not my choice)
> 
> Is this possible, and is it easy?
> 
> the way I have been using python to run other executables is
> 
> os.system('path/my.executable input')

Have a look at the subprocess module. You should be able to do
something like:

subprocess.Popen(['path/my.executable'], cwd='path')

I am not sure of the actual keyword to be used, so please look at the
docs...

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


Re: [Tutor] linux terminal coloring in python

2007-09-29 Thread Tiago Saboga
On Sat, Sep 29, 2007 at 08:32:37AM -0700, Robert Jackson wrote:

> I'm trying to get some pretty colored output for a Linux console /
terminal window.  Google searches only reveal the curses module to
colorize output.

> Is there a simpler way?  Curses seems to be FAR too powerful for
what it is I want to do (simply to colorize a few 'print' outputs to
the console).

I have looked for that last week and the best I could find was
ColorANSI module, distributed with ipython (in a debian system, it's
/usr/share/python-support/ipython/IPython/ColorANSI.py).

I just bookmarked it, and don't know how to make it work. But it seems
quite simple.

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


Re: [Tutor] detecing palindromic strings

2007-09-28 Thread Tiago Saboga
On Fri, Sep 28, 2007 at 04:59:26PM -0700, Christopher Spears wrote:
> How do I get around this?  Is there a better way to
> write this script?  I can't figure out how to loop
> through a string starting from the last character.

A string is a sequence of chars, and you can use slices with it [1].

In [23]: a = 'abcdef'

In [24]: a[::-1]
Out[24]: 'fedcba'

Tiago.

[1] - http://docs.python.org/lib/typesseq.html

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


Re: [Tutor] Please comment on this code...WORD FREQUENCY COUNTER

2006-10-23 Thread Tiago Saboga
Em Segunda 23 Outubro 2006 18:03, Asrarahmed Kadri escreveu:
> Folks,
>
> I have written a program which calculates teh frequency of each word that
> appears in a file. I would like to have your feedback.

Sorry, I couldn't resist. Others talked about your code, but here's what I use 
(no, it's not python, it's just linux utils):

$ sed -e 's/[[:space:]]/\n/g' -e 's/.*/\L&/g' -e 's/[][,;.:()]//g' 
myfile.txt | sort -d | uniq -c | sort -n

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


Re: [Tutor] Variable help

2006-10-09 Thread Tiago Saboga
Hi!

I think your problem was solved, and that the list of lists is the better 
approach indeed. But I'd like to add an answer to a direct question you 
asked:

Em Domingo 08 Outubro 2006 07:47, Tom R. escreveu:
> Basically I want to be able to integrate the value of one variable into
> another variables name. 

I think you would like to do something like: 

=
#!/usr/bin/env python
# use of __get_attribute__() function
class players(object):
player1 = [2,4,8]
player2 = [3,9,27]
player3 = [4,16,64]

count = 2
pl = players()
actual_player_string = "player%s" % count

if pl.__getattribute__(actual_player_string)[2]==9:
print 'It worked.'
===

I don't know how to do that if player{1,2,3} are not in a class, or if the 
class is not instanciated. But somebody else can possibly show a way. 

Anyway, as I said, I think you don't need it right now.

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


Re: [Tutor] re-reading file-like objects

2006-10-09 Thread Tiago Saboga
Em Segunda 09 Outubro 2006 10:41, Kent Johnson escreveu:
> Tiago Saboga wrote:
> > Hi!
> >
> > I have a problem with file-like objects for months now, and I hoped I
> > could cope with it, but I keep using what seems to me like a newbie
> > workaround...
> >
> > The question is: how can I read a file (more precisely, a file-like
> > object) more than one single time?
> >
> > In the following example, I want to generate a file in memory and save it
> > with ten different names. But I can't do it with shutil.copyfileobj,
> > AFAICS because it reads the file with read() method, which can only be
> > used once. If it's a real file, on disk, I agree it would not be a clever
> > strategy, reading it once for each copy, and I would be happy keeping its
> > content in a variable. But if the file is in memory, why can't I simply
> > read it again (or better, how can I...)?
> >
> > successful==
> > import subprocess
> >
> > FILESIZE = 20
> > NUMBER = 10
> > DIR = '/tmp/pytest'
> > FILENAME = 'treco.x'
> >
> > basefilecontents =
> > subprocess.Popen(['dd', 'if=/dev/zero', 'count=1', 'bs=%s' % FILESIZE],
> > stdout=subprocess.PIPE, bufsize=FILESIZE).stdout.read()
> >
> > for i in range(NUMBER):
> > print "File number %s" % i
> > newfile = open('%s/%s%s' % (DIR, FILENAME, i), 'w')
> > newfile.write(basefilecontents)
> > newfile.close()
>
> Your input file isn't 'in memory', it is the output of a subprocess. To
> read the input again, the subprocess would have to create the output again.
>
> This solution seems fine to me - you read the input file *contents* into
> memory, then write it to multiple files. Why don't you like this approach?

I'm afraid I don't fully understand file objects. I thought I could use it 
just as a file: if I have one, and I want several copies, I just save it with 
several names. So, if Popen.stdout already is a file object, I thought the 
easier way would be to save it with another name. I see that it is more 
complicated.
For this simple example, this solution is fine, and I think the solution 
proposed by Alan (StringIO) is not going to add anything. Would it be faster?

Thanks,

Tiago.
> 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] re-reading file-like objects (SOLVED?)

2006-10-09 Thread Tiago Saboga
I keep the original question:

Em Segunda 09 Outubro 2006 09:21, Tiago Saboga escreveu:
> Hi!
>
> I have a problem with file-like objects for months now, and I hoped I could
> cope with it, but I keep using what seems to me like a newbie workaround...
>
> The question is: how can I read a file (more precisely, a file-like object)
> more than one single time?
>
> In the following example, I want to generate a file in memory and save it
> with ten different names. But I can't do it with shutil.copyfileobj, AFAICS
> because it reads the file with read() method, which can only be used once.
> If it's a real file, on disk, I agree it would not be a clever strategy,
> reading it once for each copy, and I would be happy keeping its content in
> a variable. But if the file is in memory, why can't I simply read it again
> (or better, how can I...)

But I strip the code, and post a new one. The solution I found is in the 
seek() method of the file object. And the problem I had is that not all the 
file-like objects have a functional seek() method. It's not the case for the 
object referenced by the stdout attribute of the subprocess.Popen class. So I 
have to copy it in a (seekable) temporary file. As I have already bothered 
you with my question, let's try to make it still useful: is it the "right"  
(or pythonic) way to do this?

Thanks,

Tiago.

solution
import subprocess, shutil, tempfile

FILESIZE = 20
NUMBER = 10
DIR = '/tmp/pytest'
FILENAME = 'treco.x'

cmd = ['dd', 'if=/dev/zero', 'count=1', 'bs=%s' % FILESIZE]
tempbasefile = tempfile.TemporaryFile()

basefile = subprocess.Popen(
cmd, 
stdout=subprocess.PIPE, 
bufsize=FILESIZE
   ).stdout

shutil.copyfileobj(basefile, tempbasefile)

for i in range(NUMBER):
tempbasefile.seek(0)
print "File number %s" % i
newfile = open('%s/%s%s' % (DIR, FILENAME, i), 'w')
shutil.copyfileobj(tempbasefile, newfile)
newfile.close()
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


[Tutor] re-reading file-like objects

2006-10-09 Thread Tiago Saboga
Hi!

I have a problem with file-like objects for months now, and I hoped I could 
cope with it, but I keep using what seems to me like a newbie workaround...

The question is: how can I read a file (more precisely, a file-like object) 
more than one single time? 

In the following example, I want to generate a file in memory and save it with 
ten different names. But I can't do it with shutil.copyfileobj, AFAICS 
because it reads the file with read() method, which can only be used once. If 
it's a real file, on disk, I agree it would not be a clever strategy, reading 
it once for each copy, and I would be happy keeping its content in a 
variable. But if the file is in memory, why can't I simply read it again (or 
better, how can I...)?

successful==
import subprocess

FILESIZE = 20
NUMBER = 10
DIR = '/tmp/pytest'
FILENAME = 'treco.x'

basefilecontents = 
subprocess.Popen(['dd', 'if=/dev/zero', 'count=1', 'bs=%s' % FILESIZE], 
stdout=subprocess.PIPE, bufsize=FILESIZE).stdout.read()

for i in range(NUMBER):
print "File number %s" % i
newfile = open('%s/%s%s' % (DIR, FILENAME, i), 'w')
newfile.write(basefilecontents)
newfile.close()

=unsuccessful==

import subprocess, shutil

FILESIZE = 20
NUMBER = 10
DIR = '/tmp/pytest'
FILENAME = 'treco.x'

basefile = subprocess.Popen(['dd', 'if=/dev/zero', 'count=1', 'bs=%s' % 
FILESIZE], stdout=subprocess.PIPE, bufsize=FILESIZE).stdout

for i in range(NUMBER):
print "File number %s" % i
newfile = open('%s/%s%s' % (DIR, FILENAME, i), 'w')
shutil.copyfileobj(basefile, newfile)
newfile.close()

=
The program runs fine, but only the first file has the appropriate content. 
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] importing module with non-standard name

2006-09-28 Thread Tiago Saboga
Em Quarta 27 Setembro 2006 14:51, Kent Johnson escreveu:
> Tiago Saboga wrote:
> > Hi!
> >
> > I'm still playing with man pages parsing, and following advices here, I'm
> > using doclifter. But I want to use the installed version on my debian
> > system, and not make it a part of my package. The problem is it isn't
> > installed as a python module, but rather as an executable file lying in
> > /usr/bin, and - there's the problem - with no .py extension. I've
> > google'd about it, but I could not found any clear answer.
> >
> > Any ideas?
>
> I think either execfile() or imp.load_source() or the suggestions here -
> http://tinyurl.com/mnzoo - will do what you want.

Great! I'd found this thread before, but in another page, and I couldn't reach 
the last messages... 
Thanks,

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


[Tutor] importing module with non-standard name

2006-09-27 Thread Tiago Saboga
Hi!

I'm still playing with man pages parsing, and following advices here, I'm 
using doclifter. But I want to use the installed version on my debian system, 
and not make it a part of my package. The problem is it isn't installed as a 
python module, but rather as an executable file lying in /usr/bin, and - 
there's the problem - with no .py extension. I've google'd about it, but I 
could not found any clear answer. 

Any ideas?

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


Re: [Tutor] Input mask for console?

2006-09-14 Thread Tiago Saboga
Em Quarta 13 Setembro 2006 21:55, Chris Hengge escreveu:
> nevermind.. figured it out.. Thanks.

Hi Chris,

It's not just for you, but I'd like to make a comment. When you write to this 
list, remember that other people read your questions too, and may be 
interested in the answers. By the way, I've learned a lot here just by 
reading the q&a. But if we want it to be really useful to everybody, we 
should try to post good questions (in your case, it was ok, but it could be 
better: you could have given the example of the password in the first mail) 
*and* post the choosen solution.

Thanks!

Tiago.


>
> On Wed, 2006-09-13 at 19:48 -0400, Kent Johnson wrote:
> > Chris Hengge wrote:
> > > I need either a way to mask the input from a console, or a method to
> > > not display the typed characters to the screen. Someone point me in the
> > > right direction?
> >
> > getpass.getpass() ?
> >
> > 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
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] man pages parsing (still)

2006-09-12 Thread Tiago Saboga
Em Segunda 11 Setembro 2006 19:45, Kent Johnson escreveu:
> Tiago Saboga wrote:
> > Ok, the guilty line (279) has a "©" that was probably defined in the
> > dtd, but as it doesn't know what is the right dtd... But wait... How does
> > python read the dtd? It fetches it from the net? I tried it
> > (disconnected) and the answer is yes, it fetches it from the net. So
> > that's the problem!
> >
> > But how do I avoid it? I'll search. But if you can spare me some time,
> > you'll make me a little happier.
> >
> > [1] - The line is as follows:
> >  >   
> > "http://www.oasis-open.org/docbook/xml/4.1.2/docbookx.dtd";>
>
> I'm just guessing, but I think if you find the right combination of
> handlers and feature settings you can at least make it just pass through
> the external entities without looking up the DTDs.

I got it! I just set the feature_external_ges to false and it doesn't fetch 
the dtd any more. Thanks!!! ;-)

>
> Take a look at these pages for some hints:
> http://www.cafeconleche.org/books/xmljava/chapters/ch07s02.html#d0e10350
> http://www.cafeconleche.org/books/xmljava/chapters/ch06s11.html

It looks very interesting, and it was exactly what I needed. But I couldn't 
grab it at first, I need some more time to understand it all.

Thanks again!!!

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


Re: [Tutor] man pages parsing (still)

2006-09-11 Thread Tiago Saboga
Em Segunda 11 Setembro 2006 12:59, Kent Johnson escreveu:
> Tiago Saboga wrote:
> > Em Segunda 11 Setembro 2006 12:24, Kent Johnson escreveu:
> >> Tiago Saboga wrote:
> >>> Em Segunda 11 Setembro 2006 11:15, Kent Johnson escreveu:
> >>>> Tiago Saboga wrote:
> >>>> How big is the XML? 25 seconds is a long time...I would look at
> >>>> cElementTree (implementation of ElementTree in C), it is pretty fast.
> >>>> http://effbot.org/zone/celementtree.htm
> >>>
> >>> It's about 10k. Hey, it seems easy, but I'd like not to start over
> >>> again. Of course, if it's the only solution... 25 (28, in fact, for the
> >>> cp man page) isn't really acceptable.
> >>
> >> That's tiny! No way it should take 25 seconds to parse a 10k file.
> >>
> >> Have you tried saving the file separately and parsing from disk? That
> >> would help determine if the interprocess pipe is the problem.
> >
> > Just tried, and - incredible - it took even longer: 46s. But in the
> > second run it came back to 25s. I really don't understand what's going
> > on. I did some other tests, and I found that all the code before
> > "parser.parse(stout)" runs almost instantly; it then takes all the
> > running somewhere between this call and the first event; and the rest is
> > almost instantly again. Any ideas?
>
> What did you try, buffering or reading from a file? If parsing from a
> file takes 25 secs, I am amazed...

I read from a file, and before you ask, no, I'm not working in a 286 and 
compiling my kernel at the same time... ;-)

In fact, I decided to strip down both my code and the xml file. I've stripped 
the code to almost nothing, having yet a 23s time. And the same with the xml 
file... until I cut out the second line, with the dtd [1]. And surprise: I've 
a nice time. So I put it all together again, but have the following caveat: 
there's an error that did not raise previously:]

Traceback (most recent call last):
  File "./liftopy.py", line 130, in ?
parser.parse(stout)
  File "/usr/lib/python2.3/site-packages/_xmlplus/sax/expatreader.py", line 
109, in parse
xmlreader.IncrementalParser.parse(self, source)
  File "/usr/lib/python2.3/site-packages/_xmlplus/sax/xmlreader.py", line 123, 
in parse
self.feed(buffer)
  File "/usr/lib/python2.3/site-packages/_xmlplus/sax/expatreader.py", line 
220, in feed
self._err_handler.fatalError(exc)
  File "/usr/lib/python2.3/site-packages/_xmlplus/sax/handler.py", line 38, in 
fatalError
raise exception
xml.sax._exceptions.SAXParseException: 
/home/tiago/Computador/python/opy/manraw/doclift/cp.1.xml.stripped:279:16: 
undefined entity

Ok, the guilty line (279) has a "©" that was probably defined in the dtd, 
but as it doesn't know what is the right dtd... But wait... How does python 
read the dtd? It fetches it from the net? I tried it (disconnected) and the 
answer is yes, it fetches it from the net. So that's the problem!

But how do I avoid it? I'll search. But if you can spare me some time, you'll 
make me a little happier. 

[1] - The line is as follows:
http://www.oasis-open.org/docbook/xml/4.1.2/docbookx.dtd";>

Thanks!

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


Re: [Tutor] man pages parsing (still)

2006-09-11 Thread Tiago Saboga
Em Segunda 11 Setembro 2006 12:24, Kent Johnson escreveu:
> Tiago Saboga wrote:
> > Em Segunda 11 Setembro 2006 11:15, Kent Johnson escreveu:
> >> Tiago Saboga wrote:
> >> How big is the XML? 25 seconds is a long time...I would look at
> >> cElementTree (implementation of ElementTree in C), it is pretty fast.
> >> http://effbot.org/zone/celementtree.htm
> >
> > It's about 10k. Hey, it seems easy, but I'd like not to start over again.
> > Of course, if it's the only solution... 25 (28, in fact, for the cp man
> > page) isn't really acceptable.
>
> That's tiny! No way it should take 25 seconds to parse a 10k file.
>
> Have you tried saving the file separately and parsing from disk? That
> would help determine if the interprocess pipe is the problem.

Just tried, and - incredible - it took even longer: 46s. But in the second run 
it came back to 25s. I really don't understand what's going on. I did some 
other tests, and I found that all the code before "parser.parse(stout)" runs 
almost instantly; it then takes all the running somewhere between this call 
and the first event; and the rest is almost instantly again. Any ideas?

By the way, I've read the pages you indicated at effbot, but I don't see where 
to begin. Do you know of a gentler introduction to this module 
(cElementTree)? 

Thanks,

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


Re: [Tutor] man pages parsing (still)

2006-09-11 Thread Tiago Saboga
Em Segunda 11 Setembro 2006 11:15, Kent Johnson escreveu:
> Tiago Saboga wrote:
> > I'm still there, trying to parse man pages (I want to gather a list of
> > all options with their help strings). I've tried to use regex on both the
> > formatted output of man and the source troff files and I discovered what
> > is already said in the doclifter man page: you have to do a number of
> > hints, and it's really not simple. So I'm know using doclifter, and it's
> > working, but is terribly slow. Doclifter itself take around a second to
> > parse the troff file, but my few lines of code take 25 seconds to parse
> > the resultant xml. I've pasted the code at http://pastebin.ca/166941
> > and I'd like to hear from you how I could possibly optimize it.
>
> How big is the XML? 25 seconds is a long time...I would look at
> cElementTree (implementation of ElementTree in C), it is pretty fast.
> http://effbot.org/zone/celementtree.htm

It's about 10k. Hey, it seems easy, but I'd like not to start over again. Of 
course, if it's the only solution... 25 (28, in fact, for the cp man page) 
isn't really acceptable.

> In particular iterparse() might be helpful:
> http://effbot.org/zone/element-iterparse.htm

Ok, I'll look that.

> I would also try specifying a buffer size in the call to os.popen2(), if
> the I/O is unbuffered or the buffer is small that might be the bottleneck.

What's appropriate in that case? I really don't understand how I should 
determine a buffer size. Any pointers?

Thanks,

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


[Tutor] man pages parsing (still)

2006-09-11 Thread Tiago Saboga
I'm still there, trying to parse man pages (I want to gather a list of all 
options with their help strings). I've tried to use regex on both the 
formatted output of man and the source troff files and I discovered what is 
already said in the doclifter man page: you have to do a number of hints, and 
it's really not simple. So I'm know using doclifter, and it's working, but is 
terribly slow. Doclifter itself take around a second to parse the troff file, 
but my few lines of code take 25 seconds to parse the resultant xml. I've 
pasted the code at http://pastebin.ca/166941
and I'd like to hear from you how I could possibly optimize it.

Thanks,

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


Re: [Tutor] re syntax

2006-08-29 Thread Tiago Saboga
Em Segunda 28 Agosto 2006 15:54, Kent Johnson escreveu:
> A 'space character' is the single character \x20. A 'whitespace
> character' is any character in a some set of non-printing (white)
> characters. I guess it is a subtle distinction but it's common usage,
> not just Python; it even has a Wikipedia entry:
> http://en.wikipedia.org/wiki/Whitespace_%28computer_science%29
>
> (Not to be confused with Whitespace the programming language, in which
> all *non* white space characters are ignored:
> http://en.wikipedia.org/wiki/Whitespace_programming_language :-))

Ok, you won. ;-)

Thanks.

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


Re: [Tutor] re syntax

2006-08-28 Thread Tiago Saboga
Em Segunda 28 Agosto 2006 11:12, Kent Johnson escreveu:
> Ahem. Which part of "Whitespace within the pattern is ignored" do you
> not understand? :-)

It's easy ;-) It's the "whitespace" part. I read it as the space character, 
not as any blank character. I should have noted that new lines are ignored 
too, but I didn't. 

(I just looked a little around in my system and it seems like it's not just my 
own opinion, the ascii(7), regex(7) and wctype(3) man pages talk about spaces 
and blank characters, and the latter means what is meant by "whitespace" in 
python docs).

Anyway, you (and Michael, who replied off-list) made it really clear.

Thanks,

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


[Tutor] re syntax

2006-08-28 Thread Tiago Saboga
A couple of weeks ago I asked about a way to parse man pages, and Danny and 
Alan answered me with some tips (thanks). I've tryed doclifter, which I 
already knew, but could not yet master it; in fact, it just doesn't work with 
many of the man-pages I tried in my debian system. Anyway, I'm refreshing my 
mind about regular expressions in python, and while trying to document my 
steps, I found a weird syntax problem. The problem is: why the hell do I need 
to escape special caracters when in verbose mode? And why isn't it made clear 
on the re module doc page ( http://docs.python.org/lib/module-re.html ).

I'll just paste below (or above? I never know how to say this in english) my 
ipython's session, you'll see what I mean.

But before that, an extra question to tutors: do you have some advice for 
people like me who like to program but can't spend enough time doing it?

...it's a joke...

In [18]: re.compile("ab").match("abc").group()
Out[18]: 'ab'

In [19]: re.compile("ab", re.X).match("abc").group()
Out[19]: 'ab'

In [20]: re.compile("a\tb").match("a\tbc").group()
Out[20]: 'a\tb'

In [21]: re.compile("a\tb", re.X).match("a\tbc").group()
---
exceptions.AttributeErrorTraceback (most recent 
call last)

/home/tiago/

AttributeError: 'NoneType' object has no attribute 'group'

In [22]: re.compile("a\tb", re.X).match(r"a\tbc").group()
---
exceptions.AttributeErrorTraceback (most recent 
call last)

/home/tiago/

AttributeError: 'NoneType' object has no attribute 'group'

In [23]: re.compile(r"a\tb", re.X).match("a\tbc").group()
Out[23]: 'a\tb'

In [24]: re.compile("a\\tb", re.X).match("a\tbc").group()
Out[24]: 'a\tb'
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


[Tutor] man pages parser

2006-08-18 Thread Tiago Saboga
Hi!

I'm working on a front-end to various unix command-line utilities, and I want 
to be able to retrieve informations from man pages, specially existing 
options and their help strings. Do you know a good way to handle this? I 
could parse directly the troff sources (is there already a parser for that?), 
the resulting man pages, or I could first translate it to docbook (with 
doclifter) or html (mantohtml) and than use a xml or html parser. 

All these options require a good amount of time for me, as I don't know any of 
the parsers, and I don't know the languages, except for html. It's why I'm 
asking here first if there is a better solution or other advices...

Thanks.

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


Re: [Tutor] about copy.copy

2006-07-20 Thread Tiago Saboga
Em Quinta 20 Julho 2006 04:51, linda.s escreveu:
> But in the following example, a change in a spread to both b and c:
> >>> a=[[1,2,3], [4,5,6]]
> >>> b=a
> >>> c=copy.copy(a)
> >>> a[0][0]='a'
> >>> a
>
> [['a', 2, 3], [4, 5, 6]]
>
> >>> b
>
> [['a', 2, 3], [4, 5, 6]]
>
> >>> c
>
> [['a', 2, 3], [4, 5, 6]]

Linda,

re-read Danny's mail, it's all there! But I'll try again. Your first line,

>>> a = [ [1,2,3], [4,5,6] ]

could as well be written as:

>>> a0 = [1,2,3]
>>> a1 = [4,5,6]
>>> a = [a0, a1]

so:

>>> b=a
>>> import copy

And now what is a[0][0]? It's obviously a0[0]. And even if c is a different 
object, c[0][0] is also a0[0]. If you say now that c[0] is no more a0, but 
any other thing, a will not be changed. But if you change a0, directly or 
through the c or a lists, c *and* a first item will be changed. See it in 
action:

In [1]: a0 = [1,2,3]

In [2]: a1 = [4,5,6]

In [3]: import copy

In [4]: a = [a0,a1]

In [5]: b=a

In [6]: c = copy.copy(a)

In [7]: a0[0] = 25

In [8]: a
Out[8]: [[25, 2, 3], [4, 5, 6]]

In [9]: b
Out[9]: [[25, 2, 3], [4, 5, 6]]

In [10]: c
Out[10]: [[25, 2, 3], [4, 5, 6]]

In [11]: c[0] = [7,8,9]

In [12]: a
Out[12]: [[25, 2, 3], [4, 5, 6]]

In [13]: b
Out[13]: [[25, 2, 3], [4, 5, 6]]

In [14]: c
Out[14]: [[7, 8, 9], [4, 5, 6]]

In [15]: a[0][0] = 92

In [16]: a
Out[16]: [[92, 2, 3], [4, 5, 6]]

In [17]: b
Out[17]: [[92, 2, 3], [4, 5, 6]]

In [18]: c
Out[18]: [[7, 8, 9], [4, 5, 6]]

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


Re: [Tutor] python file browser with urwid

2006-07-18 Thread Tiago Saboga
Thank you Kent, I've done the modifications you suggested, and the code 
follows. I splitted the __init__ method in four methods (__init__, header, 
footer, dir_browser) and took the get_file_names out of pyrun class. Now I 
have two more general questions, if you don't mind.

1) (first a philosophical/methodological question) I see now that we can use 
classes in at least two ways. In my code, the class, as you said, is used as 
a container. In this case, the distinction class/instance isn't really 
important, as I will never have several instances of the same class. OTOH, I 
suppose it's not really OO programming. These statements are ok?

2) (a technical question, now) How do I test if a variable has been declared? 
In my first code, I used the try/except clause for that, but now you showed 
me a way of doing it without testing. But now I would like to add a cache in 
get_file_names(). For that, I would create a dir_cache variable of type dict, 
and store in it {'cwd': 'dirlist + filelist'} pairs. But how do I know if 
it's the first time, if I have to create the variable or just read from it?

Thanks,

Tiago.

#!/usr/bin/python

import urwid
import urwid.curses_display
import os

ui = urwid.curses_display.Screen()

ui.register_palette( [
('splash', 'black', 'dark red'),
('bg_splash', 'black', 'dark blue'),
('header', 'white', 'black'),
('footer', 'dark red', 'light gray'),
('browser', 'white', 'dark blue'),
('selected', 'white', 'dark red'),
('file', 'light gray', 'dark blue'),
('dir', 'light magenta', 'dark blue')
  ])

def run():
size = ui.get_cols_rows()
inst = pyrun()
inst.main()

class pyrun:
def __init__(self):
self.initial_cwd = os.getcwd()
self.cwd = self.initial_cwd

def main(self):
size = ui.get_cols_rows()

while True:
self.draw_screen( size )
keys = ui.get_input()
if "f10" in keys:
break
for k in keys:
if k == "window resize":
size = ui.get_cols_rows()
continue
elif k == "left":
self.cwd = os.path.split(self.cwd)[0]

def header(self):
menu_txt = urwid.Text("F1 - Help F2 - Options F10 - 
Quit
Now: %s" % self.cwd)
return urwid.AttrWrap( menu_txt, 'header')

def footer(self):
down_txt = urwid.Text("pybrowser. Left Arrow: Parent.")
return urwid.AttrWrap( down_txt, 'footer')

def dir_browser(self):
self.items = self.get_file_names ( self.cwd )
return urwid.AttrWrap ( urwid.ListBox( self.items ), 'browser')

def top_frame(self):
return urwid.Frame( self.dir_browser(), self.header(), 
self.footer() )

def draw_screen( self, size ):
canvas = self.top_frame().render( size, focus=True )
ui.draw_screen( size, canvas )

def get_file_names(self, cwd):
desc_list = os.listdir( cwd )
dir_list = []
file_list = []

for f in desc_list:
if os.path.isdir(os.path.join(cwd, f)):
dir_list.append(f)
elif os.path.isfile(os.path.join(cwd,f)):
file_list.append(f)

file_list.sort()
dir_list.sort()

file_list = [ urwid.AttrWrap ( urwid.Text(f) , 'file') for f in 
file_list ]
dir_list = [ urwid.AttrWrap ( urwid.Text(f) , 'dir')  for f in 
dir_list ]

return ( dir_list + file_list )

ui.run_wrapper( run )
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


[Tutor] python file browser with urwid

2006-07-17 Thread Tiago Saboga
I'm trying to write a file browser with urwid. In the future, I want to make a 
light front-end for cds burning, but for now I'm doing it as an exercise. The 
problem is I'm a little stuck now. I'm sending my actual working code, with 
some working functionality, but I would like to know if I should go ahead or 
if I stop now to make it better.

In fact, I don't know why I'm using a pyrun class, so I think I should get rid 
of it. And I think my try/except clause in pyrun's __init__ method is ugly, 
but I couldn't find nothing better.

Ah, and thanks everybody who helped me some months ago; I had some personal 
problems and couldn't answer, but it really helped.

Tiago.

The code:

#!/usr/bin/python

import urwid
import urwid.curses_display
import os

ui = urwid.curses_display.Screen()

ui.register_palette( [
('splash', 'black', 'dark red'),
('bg_splash', 'black', 'dark blue'),
('header', 'white', 'black'),
('footer', 'dark red', 'light gray'),
('browser', 'white', 'dark blue'),
('selected', 'white', 'dark red'),
('file', 'light gray', 'dark blue'),
('dir', 'light magenta', 'dark blue')
  ])

def run():
size = ui.get_cols_rows()
inst = pyrun()
inst.main()

class pyrun:
def __init__(self):
try:
self.items = self.get_file_names( self.cwd )
except AttributeError:
self.initial_cwd = os.getcwd()
self.cwd = self.initial_cwd
self.items = self.get_file_names( self.cwd )
self.listbox = urwid.AttrWrap ( urwid.ListBox( self.items ), 
'browser')
menu_txt = urwid.Text("F1 - Help F2 - Options F10 - 
Quit
Now: %s" % self.cwd)
header = urwid.AttrWrap( menu_txt, 'header')
down_txt = urwid.Text("pybrowser. Left Arrow: Parent.")
footer = urwid.AttrWrap( down_txt, 'footer')
self.top = urwid.Frame( self.listbox, header, footer )

def main(self):
size = ui.get_cols_rows()

while True:
self.draw_screen( size )
keys = ui.get_input()
if "f10" in keys:
break
for k in keys:
if k == "window resize":
size = ui.get_cols_rows()
continue
elif k == "left":
self.cwd = os.path.split(self.cwd)[0]
self.__init__()
continue
def draw_screen( self, size ):
canvas = self.top.render( size, focus=True )
ui.draw_screen( size, canvas )

def get_file_names(self, cwd):
desc_list = os.listdir( cwd )
dir_list = []
file_list = []

for f in desc_list:
if os.path.isdir(os.path.join(cwd, f)):
dir_list.append(f)
elif os.path.isfile(os.path.join(cwd,f)):
file_list.append(f)


file_list = [ urwid.AttrWrap ( urwid.Text(f) , 'file') for f in 
file_list ] 
dir_list = [ urwid.AttrWrap ( urwid.Text(f) , 'dir')  for f in 
dir_list ]

return ( dir_list + file_list )

ui.run_wrapper( run )
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


[Tutor] string formatting (%s)

2006-04-15 Thread Tiago Saboga
Can I use string formatting in my own functions, or is it stuck with builtins? 
It doesn't make much sense for me, but I don't understand the following 
error:

In [1]: var = 456

In [2]: def printd(arg):
   ...: print('>>> %s') % arg
   ...:

In [3]: printd(var)
>>> 456

In [4]: printd('Variable is %s') % var
>>> Variable is %s
---
exceptions.TypeError Traceback (most recent 
call last)

/home/tiago/

TypeError: unsupported operand type(s) for %: 'NoneType' and 'int'


Thanks,

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


[Tutor] sqlobject behavior

2006-04-13 Thread Tiago Saboga
Hi!

I don't understand why sqlobject's ForeignKey attribute has to have the same 
name of the linked class. The following example doesn't work (the traceback 
is just after) unless I rename the 'p' attribute of 'Address' class as 
'person'.

Thanks,

Tiago.

class Person(SQLObject):
name = StringCol()
add = MultipleJoin('Address')

class Address(SQLObject):
street = StringCol()
p = ForeignKey('Person')

def initdb():
connection_string = 'mysql://[EMAIL PROTECTED]/test'
connection = connectionForURI(connection_string)
sqlhub.processConnection = connection

def main():
initdb()
createandfeedTables()
search(raw_input('Search? '))

def createandfeedTables():
try: 
Person.createTable()
Address.createTable()
for i, j in (('tiago', 'rua ceara'), ('lucas', 'rua sergipe')):
nam = Person(name=i)
Address(street=j, p=nam)
except:
print 'Assuming tables are created and fed.'

def search(searchexp):
gr = Person.select(CONTAINSSTRING(Person.q.name, searchexp))
for l in gr:
print l.add

if __name__=='__main__':
main()




$ ./searchjoin2.py
Search? uc
Traceback (most recent call last):
  File "./searchjoin2.py", line 47, in ?
main()
  File "./searchjoin2.py", line 29, in main
search(raw_input('Search? '))
  File "./searchjoin2.py", line 44, in search
print l.add
  File "", line 1, in 
  File "/usr/lib/python2.3/site-packages/sqlobject/joins.py", line 131, in 
performJoin
inst.id)
  File "/usr/lib/python2.3/site-packages/sqlobject/dbconnection.py", line 592, 
in _SO_selectJoin
return self.queryAll("SELECT %s FROM %s WHERE %s = %s" %
  File "/usr/lib/python2.3/site-packages/sqlobject/dbconnection.py", line 316, 
in queryAll
return self._runWithConnection(self._queryAll, s)
  File "/usr/lib/python2.3/site-packages/sqlobject/dbconnection.py", line 217, 
in _runWithConnection
val = meth(conn, *args)
  File "/usr/lib/python2.3/site-packages/sqlobject/dbconnection.py", line 309, 
in _queryAll
self._executeRetry(conn, c, s)
  File "/usr/lib/python2.3/site-packages/sqlobject/mysql/mysqlconnection.py", 
line 60, in _executeRetry
return cursor.execute(query)
  File "/usr/lib/python2.3/site-packages/MySQLdb/cursors.py", line 137, in 
execute
self.errorhandler(self, exc, value)
  File "/usr/lib/python2.3/site-packages/MySQLdb/connections.py", line 33, in 
defaulterrorhandler
raise errorclass, errorvalue
_mysql_exceptions.OperationalError: (1054, "Unknown column 'person_id' in 
'where clause'")
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] mysql store directory information

2006-04-06 Thread Tiago Saboga
Em Qui 06 Abr 2006 23:10, R. Alan Monroe escreveu:
> > Em Qui 06 Abr 2006 15:41, Danny Yoo escreveu:
> >
> > model for that. But I want also to able to find these files, which are on
> > removable media (cds and dvds). As I'll have to store where's the file, I
> > thought I could as well store some other basic infos, at least the size
> > (I really don't know yet what else could be useful later).
>
> You could cheat and just use "Cathy" from this site:
> http://rvas.webzdarma.cz/

Looks like an interesting piece of software, but as far as I can see it's only 
for windows and it's not open-source (and hence, not portable). And it still 
lacks some nice features I'd like to have. Finally, I chose this project 
because I'll use tools I want to learn. So I'll stick with it ;-)

Thanks!

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


Re: [Tutor] mysql store directory information

2006-04-06 Thread Tiago Saboga
Em Qui 06 Abr 2006 15:41, Danny Yoo escreveu:
> On Thu, 6 Apr 2006, Tiago Saboga wrote:
> > As a first part of a project, I need to store a directory tree in a
> > mysql db.  I'm wondering if there is a canonical way of doing that. I
> > don't know if it's an appropriate question for this list, but I think
> > it's not only a choice of db design, but also a choice of appropriate
> > python tools.
>
> Hi Tiago,
>
> What are the interesting features of a directory?  You might want to first
> model what you want, and then figure out an appropriate database table
> structure to represent that model.

Hi, and thank you.

I knew I had to explain longer what I wanted, but I was kind of frustrated by 
the difficulty of writing in english. But I'll try to think less about it, 
and go further.

As I said, this is part of a project. For each of these files, I'll have a 
database of informations, and this is what really matters, and I have a data 
model for that. But I want also to able to find these files, which are on 
removable media (cds and dvds). As I'll have to store where's the file, I 
thought I could as well store some other basic infos, at least the size (I 
really don't know yet what else could be useful later).

> > My first approach is a table with the following columns:
> > id - path - file name - size
>
> So maybe we can say that a Directory can be modeled as:
>
> ##
> class Directory:
> def __init__(self, id, path, file_name, size):
> self.id = id
> self.path = path
> self.file_name = file_name
> self.size = size
> ##
>
> But why will you want to store this structure in the database, if it's
> already available on disk?  Why not query the directory directly?

See above.

>
> > I'm starting to code it, and I'd like to know if you have a better
> > suggestion...
>
> I'd flesh out a few more of the requirements first; the requirement to
> store the directory in the database is slightly vague, so you probably
> will want to ask more questions about what the problem's really about.
>
>
>
> You might find something like SQLObject useful:
>
> http://www.sqlobject.org/
> http://www.turbogears.org/about/sqlobject.html
>
> where you go fairly directly from data model to SQL table structure with
> SQLObject providing the default mapping strategy.

Hey, this is *really* great ;-)

Hey, I love it.

OK, but now why would I use such a directory class as you proposed above? 
(preliminar question: isn't it rather a file class, as it has only one 
filename? Anyway, I see your point.) I would make a SQLobject class for 
files, and feed it with something like 

def get_dirtree(path):
  tree = []
  for dirpath, dirnames, filenames in os.walk(path):
if filenames:
  for file in filenames:
size = os.path.getsize(os.path.join(dirpath,file))
tree.append((dirpath,file,size))
  return tree

What do you think? Of course, it could be the __init__ function of class, but 
I don't see why.

Thanks, again.

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


[Tutor] mysql store directory information

2006-04-06 Thread Tiago Saboga
Hi!

As a first part of a project, I need to store a directory tree in a mysql db. 
I'm wondering if there is a canonical way of doing that. I don't know if it's 
an appropriate question for this list, but I think it's not only a choice of 
db design, but also a choice of appropriate python tools.

My first approach is a table with the following columns:
id - path - file name - size

I'm starting to code it, and I'd like to know if you have a better 
suggestion...

thanks.

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


Re: [Tutor] MySQLdb: at a complete loss

2006-02-18 Thread Tiago Saboga
Em Sáb 18 Fev 2006 17:51, Danny Yoo escreveu:
> > db=MySQLdb.connect(host='localhost',user='root',passwd='081300',db='tut')
> > cursor=db.cursor()
> > name = raw_input("Please enter a Name: ")
> > color =raw_input("Please enter a Color: ")
> > cursor.execute("""INSERT INTO horses(name,color)
> > VALUES("%s","%s")"""%(name,color))
>
>  ^
>
> Hi Servando,
>
> Don't do this: don't try to interpolate the variables yourself, but pass
> the parameters as part of the execution call:
>
> http://mail.python.org/pipermail/tutor/2003-April/022010.html

It's interesting, but I'm missing something. What does it mean, a "prepared 
statement"? I can see *why" to use it, but not *what* it is. But wait... is 
it simply a difference between concatenating variables and putting them 
"inline" in the string? So the on the second line of code below mysql will 
know for sure that c is the wanted value for xname, even if it is something 
that should be quoted?

a="select xname,sname from people where xname='"+c+"'"

cursor.execute("select xname,sname from people where xname=%s", c)


> > this script runs without error and does not affect the database in any
> > manner.  The database does exist. User/Password are valid. I have run
> > the above script , unchanged, on both Macintosh and Ubuntu with success.
>
> Are you familiar with database transactions?  Look at the description of
> close() in the Python DB API:
>
> """Close the connection now (rather than whenever __del__ is
>called)...  Note that closing a connection without
>committing the changes first will cause an implicit
>rollback to be performed."""
>
> (http://www.python.org/peps/pep-0249.html)
>
> Does this last sentence ring some bells?  *grin*
>
> Newest versions of the MySQLdb module do NOT autocommit anymore. You have
> to commit at the end or else you're not going to see any mutation affect
> the database: every change you make will just roll back.

Even with MyIsam tables, which are not transactional?

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


Re: [Tutor] unable to insert data into database

2006-02-18 Thread Tiago Saboga
Em Sex 17 Fev 2006 13:15, Wolfram Kraus escreveu:
> Wolfram Kraus wrote:

[SNIP]
> Sorry for asking the obvious (Database/Table exists)! You are missing
>
> db.commit()
> db.close()
>
> in your code. IIRC auto-commit was switched of for MySQLdb long time ago.

Hi!

I'm Tiago, from Brazil, I'm new to this list and, err, I'm getting into 
python.

I'm very interested on the mysql module, and this thread sounds interesting. 
I've tried pretty much the same code from the original code, and I don't need 
to commit changes. All I have is a cursor.close(), and it works ok. So, I 
think it have a sort of auto-commit.

But I was wondering if it's the same behavior for all database engines, as 
I've read somewhere on the mysql reference that Innodb tables need a commit 
command. And it could even be an explanation to the different behavior 
observed by Servando: if you create different sorts of tables on windows, mac 
and linux, you'll have different behaviors...

I hope you'll understand my poor english...

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


Re: [Tutor] problems with the shebang line and linux

2006-02-16 Thread Tiago Saboga
Em Qui 16 Fev 2006 12:23, Adam escreveu:
> > [EMAIL PROTECTED]:/media/windata$ ./testerlybar.py
> > bash: ./testerlybar.py: /usr/bin/python^M: bad interpreter: No such
> > file or directory
>
> It seems to me that that ^M is your problem although I'm not quite sure
> where it came from there seems to be an extra character on the end of the
> copied one. 

I'm pretty sure the problem is the different way linux and windows/dos mark 
the end of line (IIRC, linux -> LF, windows -> CR+LF). Just try (it's 
installed on my debian box, I can check which package it is, if you want):

$ fromdos -b testerlybar.py

Of course, reading the manual page for fromdos is a good thing.

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