Re: [Tutor] reading from stdin

2005-03-02 Thread Nick Lunt
Thanks to everyone who helped me with this.
It's certainly given me something to think about :)

Cheers
Nick .


On Tue, 2005-03-01 at 23:13 -0600, David Rock wrote:
> * Nick Lunt <[EMAIL PROTECTED]> [2005-03-01 22:23]:
> > On Tue, 2005-03-01 at 14:14 -0800, Sean Perry wrote:
> > 
> > > 
> > > unless you want the output for some other reason, a more idiomatic way
> > > is:
> > > 
> > > for line in sys.stdin.readlines():
> > >  # handle the line
> > > 
> > > I tend to use xreadlines() which does not read the entire input at once. 
> > >   For stdin this make sense, you have no idea how much data will be 
> > > piped in.
> > 
> > Thanks Sean, I agree with you on both accounts there.
> 
> For another approach to this, I like to use the fileinput module. In the
> case of the original example of mimicing grep, it allows you to easily
> handle both a list of files passed to the command or use it as a pipe.
> The default action if there are no files given is to use stdin.
> 
> http://www.python.org/doc/2.4/lib/module-fileinput.html
> 
> ___
> 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] reading from stdin

2005-03-02 Thread Nick Lunt
Hi Hugo,

many thanks for pointing that out. It all helps :)

Thanks again,
Nick .


On Tue, 2005-03-01 at 17:35 -0600, Hugo GonzÃlez Monteverde wrote:
> Everypne else has answered pretty muh about this. I just want to add 
> that if you want to read noncanonically (less thana line ending in "\n" 
> you'll have to do it char by char =( AFAIK, there's no way to redefine a 
> separator por readlines() (other than \n..)
> 
> Hugo
> 
> Nick Lunt wrote:
> > Hi folks,
> > 
> > I've been pondering how to get python to read from a pipe outside of
> > itself, sort of.
> > 
> > For example I tried a simple python prog to do a grep, eg
> > 
> > # ps -e | myprog.py cron
> > 
> > would give this output
> > 
> > 3778 ?00:00:00 crond
> > 
> > same as 
> > # ps -e | grep cron
> > 
> > The way I did this was to use sys.stdin.readlines() to get the output
> > from the pipe.
> > 
> > Here is the program:
> > 
> > [code]
> > import sys, glob
> > args = sys.stdin.readlines() # found on the net
> > pat = sys.argv[1]
> > for i in args:
> > if (i.find(pat) != -1):
> > print i,
> > [/code]
> > 
> > My question is am I getting the output from the pipe in the correct
> > way ? The way Im doing it works (so far) but should I be doing it
> > another way ?
> > 
> > Many thanks
> > Nick .
> > 
> > 
> > ___
> > 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 to use threads ?

2005-03-02 Thread Terry Carroll
On Tue, 1 Mar 2005, Mark Kels wrote:

> Can anyone give me a very simple example on thread programming ?

I don't think a simple example is possible, given that threads are 
inherently for slightly more complex processing than you ordinarily do.

That being said, here's an example.

This is a made-up process.  It shows both queuing and an arbitrary number 
of threads.

First, the imports:

###
import random, os, time, sys
import threading, Queue
###

No, I'll define a class for the "work" to be performed.  Basically, each 
work unit just specifies how much time the system is supposed to wait, in 
second; pretend that it's doing some amount of work that takes some 
number of seconds to be performed:

###
class workunit(object):
"""
Sample object to be put on a queue similating work to be done.
variables:
  counter: a serial number just for identification purposes.
  waittime: the time in second it uses up.
  Done: a flag used for a special-purpose end-of-queue sentinel,
 in which case waittime is ignored.
"""
counter = 1
def __init__(self, waittime=0, Done=False):
self.counter = workunit.counter
self.waittime = waittime
self.Done = Done
workunit.counter += 1
###

This will be called in one of two ways:

 w = workunit(waittime=20) # to indicate wait for 20 seconds; or
 w = workunit(Done=True) # to add a "dummy" work unit to the queue so 
 # everyone know the queue is empty and finished.


Okay, imagine a queue (or just a list) full of work units like the above.  
Here's a plain old sequential NON-THREAD way of processing this:

###
def ProcessQueue(work_queue):
"""
Method to process an element from the queue 
 (Non-Threaded implementation).
All it does is loop, doing the following:
  pull an element from the queue (break out if it's a "Done" marker)
  print a starting message;
  wait for the specified amount of time;
  print an ending message
"""
while True:
queue_entry = work_queue.get()
if queue_entry.Done:
break
print "%s starting on workunit %d, %d secs" % \
   (time.asctime(), queue_entry.counter, queue_entry.waittime)
time.sleep(queue_entry.waittime)
print "%s ending for workunit %d" % \
   (time.asctime(), queue_entry.counter)


Okay, understand that, first  See what it's doing?  It's just 
popping things off the work_queue, printing a message, waiting for 
the indicated amount of time in the work unit, and printing another 
message; then starting over.

Now, let's try the same approach with threads.  Firrst, the class 
declaration:

#
class ThreadProcessQueue(threading.Thread):
"""
This is a Threaded equivalent to ProcessQueue().
"""
#

Now, here's the ThreadProcessQueue.__init__:

#
def __init__(self, threadname, work_queue, **kwds):
self.tname = threadname
self.work_queue = work_queue
threading.Thread.__init__(self, **kwds)
print "%s Thread %s started" % (time.asctime(), self.tname) 
#

The parameters here are an arbitrary name for the thread, and the queue it 
will process.  All __init__ does is print a message that the thread 
started.

Here's the guts of it, the ThreadProcessQueue.__run__ method.  NOte how 
similar it is to the non-Threaded version:

#
def run(self):
while True:
queue_entry = work_queue.get()
if queue_entry.Done:
break
print "%s Thread %s starting on workunit %d, %d secs" % \
   (time.asctime(), self.tname, queue_entry.counter, 
queue_entry.waittime)
time.sleep(queue_entry.waittime)
print "%s Thread %s ending for workunit %d" % \
   (time.asctime(), self.tname, queue_entry.counter)

print "%s %s thead ending." % (time.asctime(), self.tname)
self.work_queue.put(queue_entry)
#

The only real difference is that the messages produced include an 
identifier so you can see which thread is generating which message; and 
also there's that self.work_queue.put(queue_entry) at the end. I'll 
discuss that at the end of this message.

Now, here's the main program that uses these.  First some setup:


print "MAIN: %s starting..." % (time.asctime())
work_queue = Queue.Queue()
NumWorkUnits=8
NumThreads=3
WaitTimes = [3,6,9,12,1,5,5,1]
lenWaitTimes = len(WaitTimes)
# WaitTimes is just a list of some arbitrary times representing work
# A particular WorkUnit will wait for one of these times.
ThreadList=[]
###

Queue is s specialized type of FIFO list, 

Re: [Tutor] open a socket from a named file on linux

2005-03-02 Thread dm
On Sun, 2005-02-27 at 17:18 -0600, dm wrote:
> Hello, I am trying to open a socket connection to a named file on my
> computer and can not seem to get it working.  Any help or advice would
> be great.

solution

use what are called unix domain sockets, 
in python they are accessed like this


s = socket.socket(socket.AF_UNIX, socket.SOCK_STREAM)
s.connect('/tmp/xfmedia_remote.1001.0')


now s is a normal socket object.



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


[Tutor] Re: Help- Simple recursive function to build a list

2005-03-02 Thread Kent Johnson
actuary77 wrote:
Kent Johnson wrote:
 >>> def rec(n,alist=[]):
 ... _nl=alist[:]
 ... print n,_nl
 ... if n == 0:
 ... print n,_nl
 ... return _nl
 ... else:
 ... _nl=_nl+[n]
 ... return rec(n-1,_nl)
 ...
 >>> _nl = rec(4)
4 []
3 [4]
2 [4, 3]
1 [4, 3, 2]
0 [4, 3, 2, 1]
0 [4, 3, 2, 1]
 >>> print _nl
[4, 3, 2, 1]
Kent
Kent, thank you for the answer.
I just don't understand why.
I only have one return at the point at the test of the end of the 
recursion, n == 0.
You have to return a result from each level of recursion. You have
rec(4) <- this returns nothing to its caller
  rec(3, [4]) <- this returns nothing to its caller
rec(2, [4,3]) <- this returns nothing to its caller
  rec(1, [4,3,2]) <- this returns nothing to its caller
rec(0, [4,3,2,1]) <- this returns [4,3,2,1] to the call above
By adding the missing 'return', you make all the intermediate invocations return the final result up 
the chain to their caller.

This version shows what is happening:
 >>> def rec(n,alist=[]):
 ...  _nl=alist[:]
 ...  if n == 0:
 ...  print n,_nl
 ...  return _nl
 ...  else:
 ...  _nl=_nl+[n]
 ...  print 'calling rec(', n-1, _nl, ')'
 ...  val = rec(n-1,_nl)
 ...  print 'rec(', n-1, _nl, ') returned', val
 ...  return val
 ...
 >>> rec(4)
calling rec( 3 [4] )
calling rec( 2 [4, 3] )
calling rec( 1 [4, 3, 2] )
calling rec( 0 [4, 3, 2, 1] )
0 [4, 3, 2, 1]
rec( 0 [4, 3, 2, 1] ) returned [4, 3, 2, 1]
rec( 1 [4, 3, 2] ) returned [4, 3, 2, 1]
rec( 2 [4, 3] ) returned [4, 3, 2, 1]
rec( 3 [4] ) returned [4, 3, 2, 1]
[4, 3, 2, 1]
Kent
PS Please send followups to the Tutor list so all may benefit from the 
discussion.
I see from the output that the list is being built, up to the point of 
the one and only return,

if n == 0:
print n,_nl   #  0 [4, 3, 2, 1]
return _nl# None, Why doesn't this return _nl = [4, 3, 2, 1]?
I can see that the list is being built without the second return.
And even at the point where the end of the recursion is tested, n == 0,
the value to be returned, _nl is equal to the desired result?
The second return is never used!
The list being built is passed as an argument.
Scope?
Why is the second return required?
Most confused ;(


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


[Tutor] Re: Online Programming Contest (Python solutions accepted)

2005-03-02 Thread Shitiz Bansal
Hi,
some questions need to be addressed.
First, is execution time a factor, bcos admittedly
python sols r much slowere than c/c++.
second, wats the exact conf of python installed on
machines checking our sols.
i.e. which modules r allowed n which r not.
shitiz
--- Sridhar <[EMAIL PROTECTED]> wrote:

> Hi,
> 
> We, the students of CEG, Anna University [1] are
> organizing an online
> programming contest as part of aBaCus [2] 2005.  The
> contest itself
> will start on 6th March 2005 at 1:00 pm IST [3] and
> will end after 5
> hours.  You have to solve the problems posted at the
> start of the
> contest.  Teams ranking high will be awarded the
> prizes.
> 
> As a special note, inspite of C,C++ and Java we also
> allow Python [4]
> this time.  So we hope a lot of Pythonistas also
> could attend the
> contest for fun. :-)
> 
> For more details about the contest, visit the
> contest page
> 
>  --   http://203.197.138.181/OPC  --
> 
> [1] http://en.wikipedia.org/wiki/Anna_University
> [2] http://www.annauniv.edu/abacus/
> [3] Indian Standard Time (IST) is the time zone for
> India. It is 5
> hours and 30 minutes ahead of GMT/UTC.
> [4] http://www.python.org
> 
> --
> Sridhar Ratna - http://srid.bsdnerds.org
> 
> -- 
> http://mail.python.org/mailman/listinfo/python-list
> 


__
Do You Yahoo!?
Tired of spam?  Yahoo! Mail has the best spam protection around 
http://mail.yahoo.com 
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


[Tutor] Re: Python Online Programming Contest

2005-03-02 Thread Varun Soundararajan
Hi,
The results of OPC (online programming contest) is out. The statistics
of python usage is available at
http://www.samhita.info/opc/status.php.
Regards,
-OPC Team
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] How to use threads ?

2005-03-02 Thread Mark Kels
On Tue, 1 Mar 2005 12:25:37 -0800 (PST), Shitiz Bansal
<[EMAIL PROTECTED]> wrote:
> 
> Here is a simple program
> 
> class abc(threading.Thread):
> def __init__(self):
> threading.Thread.__init__(self)
> self.x=1 etc. etc.
> self.cont_flag=1
> def run(self):
> while self.cont_flag:
>print self.x
>(you can set the self.cont_falg to zero
> whenever you wish to end the loop(and
> hence
> the thread., either
> here, or even from outside the thread.
> of course, it isnt necessary that your
> thread is a loop, it can be a finite
> function.)
> 
Thanks alot !!
I think I got it ths time.
The only problem is that when I try to do it my thread doesnt closes.
When does a thread closes ?

Thanks again. 

-- 
1. The day Microsoft makes something that doesn't suck is probably the
day they start making vacuum cleaners.
2. Unix is user friendly - it's just picky about it's friends.
3. Documentation is like sex: when it is good, it is very, very good.
And when it is bad, it is better than nothing. - Dick Brandon
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


[Tutor] Threaded persistance?

2005-03-02 Thread Gwyn Evans
Hi,
  New to Python, but with Java background  I'm interested in
comments/suggestions for something I'm trying...

  I've got a series of events (basically a dictionary of a few
key:value pairs) which I'd like to forward onto a web service.  That
should be no problem, but I'm investigating what I can do when the web
service is down for a bit.
  What I'm considering is to persist the event and trigger a seperate
thread to retry the forwarding.  What I'm wondering about is if
there's a storage method that allows 1 thread writing to the storage
while the other thread reads and then deletes from the storage...

I had a look at using ZODB but hit problems when trying to have two
connections to the same FileStorage DB, but a second look at things
suggested that dbm might well do what I need, if  I ensure only one
thread at a time can access it.

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


Re: [Tutor] Threaded persistance?

2005-03-02 Thread Kent Johnson
Gwyn Evans wrote:
Hi,
  New to Python, but with Java background  I'm interested in
comments/suggestions for something I'm trying...
  I've got a series of events (basically a dictionary of a few
key:value pairs) which I'd like to forward onto a web service.  That
should be no problem, but I'm investigating what I can do when the web
service is down for a bit.
  What I'm considering is to persist the event and trigger a seperate
thread to retry the forwarding.  What I'm wondering about is if
there's a storage method that allows 1 thread writing to the storage
while the other thread reads and then deletes from the storage...
Do you need to allow for the sending thread to be restarted as well? In other words, does the queue 
have to be persistent? If not, you could use Queue.Queue which is intended for this kind of 
inter-thread communication.

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


[Tutor] Better Search and replace method

2005-03-02 Thread Ron Nixon

I'm trying to figure out a better solution to do 
multiple search and replaces in a text file without
having to type:
import re
s = open('filename')
re.sub('vaule1','value2',s)
re.sub('vaule3','value4',s)
etc

I've tried putting all the vaules in a list and doing
the replace, but came up short. Any suggestions?

Thanks in advance

Ron




__ 
Celebrate Yahoo!'s 10th Birthday! 
Yahoo! Netrospective: 100 Moments of the Web 
http://birthday.yahoo.com/netrospective/
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Threaded persistance?

2005-03-02 Thread Gwyn Evans
On Wed, 02 Mar 2005 15:53:17 -0500, Kent Johnson <[EMAIL PROTECTED]> wrote:
> Gwyn Evans wrote:
> > Hi,
> >   New to Python, but with Java background  I'm interested in
> > comments/suggestions for something I'm trying...
> >
> >   I've got a series of events (basically a dictionary of a few
> > key:value pairs) which I'd like to forward onto a web service.  That
> > should be no problem, but I'm investigating what I can do when the web
> > service is down for a bit.
> >   What I'm considering is to persist the event and trigger a seperate
> > thread to retry the forwarding.  What I'm wondering about is if
> > there's a storage method that allows 1 thread writing to the storage
> > while the other thread reads and then deletes from the storage...
> 
> Do you need to allow for the sending thread to be restarted as well? In other 
> words, does the queue
> have to be persistent? If not, you could use Queue.Queue which is intended 
> for this kind of
> inter-thread communication.

Ideally, yes, although the main issue I was concerned about with
Queue.Queue was that I didn't want to be be limited by memory size...

I guess the final fallback would be going straight to a full-blown DB
with a connection from each thread, but I'm not sure if it's a bit
over the top...

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


[Tutor] Re: Threaded persistance?

2005-03-02 Thread Gwyn Evans
On Wed, 2 Mar 2005 20:34:18 +, Gwyn Evans <[EMAIL PROTECTED]> wrote:
> I had a look at using ZODB but hit problems when trying to have two
> connections to the same FileStorage DB, but a second look at things
> suggested that dbm might well do what I need, if  I ensure only one
> thread at a time can access it.

  When I found I had the same sort of issue with dbm I had a closer
look and realised that my test code was tidying up too quickly and
closing the DB before the test had actually finished, so it looks as
if ZODB's still in the picture!

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


Re: [Tutor] How to use threads ?

2005-03-02 Thread Terry Carroll
On Wed, 2 Mar 2005, Mark Kels wrote:

> The only problem is that when I try to do it my thread doesnt closes.
> When does a thread closes ?

You got me.  That's what I meant when I wrote "calls to t.isAlive()
returned True long after the thread referred to had finished up and put
out its final [shutdown] message."

I don't know what it would take to have a t.isAlive() call ever come back 
as False.  I may experiment a bit.  That tutorial and program was put 
together on the fly late last night, and I didn't have the opportunity to 
look into that issue.

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


Re: [Tutor] Better Search and replace method

2005-03-02 Thread Alan Gauld
> I'm trying to figure out a better solution to do 
> multiple search and replaces in a text file without
> having to type:
> import re
> s = open('filename')

s = open(...).read()  # I assume?

> re.sub('vaule1','value2',s)
> re.sub('vaule3','value4',s)

> I've tried putting all the vaules in a list and doing
> the replace, but came up short. Any suggestions?

Without seeing what you tried or what the results were 
I can only guess...

But did you try storing the values as a list of tuples, 
or as a dictionary?

values = [ (vaule1, value2), (vaule3,value4),...]

for target,replace in values:
   re.sub(target,replace,s)

OR

values = { 'vaule1' : 'value2',
   'vaule3' : 'value4', .}

for target in values.keys():
   re,sub(target,values[target],s)

Finally do you need re? Or could the simple string methods work? 
They'd be slightly faster...

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


Re: [Tutor] Better Search and replace method

2005-03-02 Thread Kent Johnson
Ron Nixon wrote:
I'm trying to figure out a better solution to do 
multiple search and replaces in a text file without
having to type:
import re
s = open('filename')
re.sub('vaule1','value2',s)
re.sub('vaule3','value4',s)
etc

I've tried putting all the vaules in a list and doing
the replace, but came up short. Any suggestions?
See this recipe which uses the ability of re.sub() to take a callable as the substitution parameter 
to do what you want.
http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/81330

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


Re: [Tutor] How to use threads ?

2005-03-02 Thread Shitiz Bansal
The thread finishes when:
1.The run method finishes.
2.In case of the loop- you may -

>>> import threading
>>> class abc(threading.Thread):
def __init__(self):
threading.Thread.__init__(self)
self.cont_flag=1
def run(self):
while self.cont_flag:
pass

>>> abcd=abc()
>>> abcd

>>> abcd.start()
>>> abcd

>>> abcd.cont_flag=0
>>> abcd


Also, you may 

>>> del abcd
>>> abcd

Traceback (most recent call last):
  File "", line 1, in -toplevel-
abcd
NameError: name 'abcd' is not defined

I am not very sure about the CPU usage of a stopped
thread.Perhaps someone from the group can enlighten
us.

Hope this makes it clear.

Cheers,
Shitiz
--- Mark Kels <[EMAIL PROTECTED]> wrote:

> On Tue, 1 Mar 2005 12:25:37 -0800 (PST), Shitiz
> Bansal
> <[EMAIL PROTECTED]> wrote:
> > 
> > Here is a simple program
> > 
> > class abc(threading.Thread):
> > def __init__(self):
> > threading.Thread.__init__(self)
> > self.x=1 etc. etc.
> > self.cont_flag=1
> > def run(self):
> > while self.cont_flag:
> >print self.x
> >(you can set the self.cont_falg to zero
> > whenever you wish to end the loop(and
> > hence
> > the thread., either
> > here, or even from outside the thread.
> > of course, it isnt necessary that your
> > thread is a loop, it can be a finite
> > function.)
> > 
> Thanks alot !!
> I think I got it ths time.
> The only problem is that when I try to do it my
> thread doesnt closes.
> When does a thread closes ?
> 
> Thanks again. 
> 
> -- 
> 1. The day Microsoft makes something that doesn't
> suck is probably the
> day they start making vacuum cleaners.
> 2. Unix is user friendly - it's just picky about
> it's friends.
> 3. Documentation is like sex: when it is good, it is
> very, very good.
> And when it is bad, it is better than nothing. -
> Dick Brandon
> 





__ 
Celebrate Yahoo!'s 10th Birthday! 
Yahoo! Netrospective: 100 Moments of the Web 
http://birthday.yahoo.com/netrospective/
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


[Tutor] slow html generation code

2005-03-02 Thread Luis N
This code seems a little slow, is there anything in particular that
jumps out as being not quite right.

The idea is that a file is opened that contains path names to other
files, that are appended and outputed into a directory of choice.

I plan to move this off the filesystem into a database when the
concept is worked out, maybe that will help.

#!/usr/local/bin/python

import sys
import os
from string import Template
from textile import textile

def main(files):
if len(files) < 1:
print 'Feed me with XHTML, String Templates, and Textile'
else:
path = os.path.expanduser('~/')
publish = os.path.join(path, 'publish')
pages = os.path.join(publish, 'pages')
write = os.path.join(path, 'public_html/write')
try:
for i in files:
page = os.path.join(write, i)
f = open(os.path.join(pages, i), 'r')
tmp = ""
for line in f.readlines():
line = line.rstrip()
structure = open(os.path.join(publish, line))
clean = structure.read()
tmp = tmp + clean.rstrip()
txt = textile(tmp) + ''
t = Template(txt)
s = t.safe_substitute(title='Web-siter: %s' % i[:-5])
output = open(page, 'w')
output.write('')
output.write(s)
except:
pass

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


Re: [Tutor] slow html generation code

2005-03-02 Thread Sean Perry
Luis N wrote:
This code seems a little slow, is there anything in particular that
jumps out as being not quite right.
The idea is that a file is opened that contains path names to other
files, that are appended and outputed into a directory of choice.
I plan to move this off the filesystem into a database when the
concept is worked out, maybe that will help.
You are reading entire file contents into memory. Once with 
f.readlines() and then again with structure.read(). If these are 
somewhat large files that could definitely be a place for slowness. If 
nothing else, Python promises garbage collection to be done, but not 
when. So many of your files could be sitting in memory.
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] slow html generation code

2005-03-02 Thread Danny Yoo


On Wed, 2 Mar 2005, Luis N wrote:

> This code seems a little slow, is there anything in particular that
> jumps out as being not quite right.

Hi Luis,

Some comments:


You have an empty 'except' exception-handling block in the code:

##
try:
for i in files:
page = os.path.join(write, i)
##  ... code cut
output.write(s)
except:
pass
##


Try not to do that.  *grin*


Having 'except: pass' is almost always a bad idea because it really makes
it hard to debug problems.  Make sure your exception handler does
something useful: as it is, the code can disguise all sorts of bugs
without giving any feedback to you.

It's not clear at all what the benefit of the custom exception handler is
in your code, so I'd recommend just dropping the try/except.



Another thing is that the code is a big long: it might be worthwhile to
break:

> page = os.path.join(write, i)
> f = open(os.path.join(pages, i), 'r')
> tmp = ""
> for line in f.readlines():
> line = line.rstrip()
> structure = open(os.path.join(publish, line))
> clean = structure.read()
> tmp = tmp + clean.rstrip()
> txt = textile(tmp) + ''
> t = Template(txt)
> s = t.safe_substitute(title='Web-siter: %s' % i[:-5])
> output = open(page, 'w')
> output.write('')
> output.write(s)



off into its own helper function.  Let's pretend that we call this
function 'writePage()':

##
def writePage():   ## FIXME: the argument list here isn't quite right yet
page = os.path.join(write, i)
f = open(os.path.join(pages, i), 'r')
tmp = ""
for line in f.readlines():
line = line.rstrip()
structure = open(os.path.join(publish, line))
clean = structure.read()
tmp = tmp + clean.rstrip()
txt = textile(tmp) + ''
t = Template(txt)
s = t.safe_substitute(title='Web-siter: %s' % i[:-5])
output = open(page, 'w')
output.write('')
output.write(s)
##

This breakup isn't quite right yet, and this needs to take in some
parameters.  So it needs a little work.  But this improves the code by
allowing you to concentrate on what it means to write a single page.



It also makes it easier to see a possible bug in the code: the last few
lines in the 'for' loop look suspicious:

##
txt = textile(tmp) + ''
t = Template(txt)
s = t.safe_substitute(title='Web-siter: %s' % i[:-5])
output = open(page, 'w')
output.write('')
output.write(s)
##

It does look strange that the file is being open and rewritten over and
over, for each line in the file.  Are you sure you want to put that in the
body of the loop?


Best of wishes to you!

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


[Tutor] Re: How to read unicode strings from a binary file and display them as plain ascii?

2005-03-02 Thread Javier Ruere
R. Alan Monroe wrote:
R. Alan Monroe wrote:
I started writing a program to parse the headers of truetype fonts to
examine their family info. But I can't manage to print out the strings
without the zero bytes in between each character (they display as a
black block labeled 'NUL' in Scite's output pane)
I tried:
stuff = f.read(nlength)
stuff = unicode(stuff, 'utf-8')

  If there are embeded 0's in the string, it won't be utf8, it could be 
utf16 or 32.
  Try:
   unicode(stuff, 'utf-16')
or
   stuff.decode('utf-16')

print type(stuff), 'stuff', stuff.encode()
This prints:
 stuff [NUL]C[NUL]o[NUL]p[NUL]y[NUL]r[NUL]i[NUL]g[NUL]

  I don't understand what you tried to accomplish here.

That's evidence of what I failed to accomplish. My expected results
was to print the word "Copyright" and whatever other strings are
present in the font, with no intervening NUL characters.
  Oh but why print type(stuff) or 'stuff'?
Aha, after some trial and error I see that I'm running into an endian
problem. It's "\x00C" in the file, which needs to be swapped to
"C\x00". I cheated temporarily by just adding 1 to the file pointer
:^)
  Ah! Endianness! I completely overlook this issue! I have lost several 
hours of my life to endian problems.
  Glad to see (on another post) there is an encoding which handles 
explicitly the endianness or the encoded string.

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


Re: [Tutor] slow html generation code

2005-03-02 Thread Kent Johnson
Danny Yoo wrote:
It also makes it easier to see a possible bug in the code: the last few
lines in the 'for' loop look suspicious:
##
txt = textile(tmp) + ''
t = Template(txt)
s = t.safe_substitute(title='Web-siter: %s' % i[:-5])
output = open(page, 'w')
output.write('')
output.write(s)
##
It does look strange that the file is being open and rewritten over and
over, for each line in the file.  Are you sure you want to put that in the
body of the loop?
You are also passing the text through textile / Template / safe_substitute repeatedly. You probably 
want to accumulate all the text, process it once and write it to the file.

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