Re: [Tutor] repeat

2007-11-17 Thread Scott SA
On 11/17/07, Michael ([EMAIL PROTECTED]) wrote:

>This has probably been asked before but can I get some clarification on 
>why Python does not have a repeat...until statement, and does that mean 
>repeat...until is bad practice? I was trying to get Python on the 
>standard langauge list for my state secondary school system but they say 
>a langauge must have a test last structure in it to be considered.

While you may feel your assertion to be true, it will persist until discovered  
false... 



... to date has satisfied my needs for the task. 8^)


Like many things in Python there are alternates, even to this (I expect to be 
learning more of them myself for a good long time 8^)

More examples and detail can be found at:





Simply put:

while [test]:
do something

 or

while True:
do something
if [test]:
break

x = 0

while x < 100:
x += 1

print x
100

Still, the links can elaborate more than I can.

HTH

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


Re: [Tutor] repeat

2007-11-17 Thread Alan Gauld
"Michael" <[EMAIL PROTECTED]> wrote

> This has probably been asked before but can I get some clarification 
> on
> why Python does not have a repeat...until statement,

Because Guido didn't put one in

repeat/until is never needed it is only ever a nice to have.
Indeed some languages don't even have a for loop for the same
reason (Oberon being an example). Some don't even
have any explicit loops since recursion can be used
instead - some Lisp dialects for example.

The important concept here is looping not the syntax
of the loop structure. However in Python the space
sensitive nature of the language does make a do/while
style loop a little harder to implement without breaking
good indentation practice. For example:

repeat:
 # code here
 # more code
 until condition

Now the until is visually part of the block - not nice!
(And if your school board don't understand that then
they have much bigger problems to worry about than
the syntax of loops!)

Whereas

repeat:
 # code
 # code
until condition

Is better structure but completely inconsistent with
every other Python construction (aside from try/except?)

You could of course introduce block markers to define
the block in the manner of Tcl or ruby etc but that too
would be completely inconsistent with Python style.

So instead we have the common Python idiom of

while True
# code
# code
if condition: break

> repeat...until is bad practice?

No, just a variation on a theme that Python does not support
directly. There are plenty other loop structures that Python
(and other languages) does not support, for example ADAs
loop with exit:

loop:
   # code
   # code
   exit when condition
   # code
   # code
end loop

But again that can be modelled using while/break

> a langauge must have a test last structure in it to be considered.

That's a bizarre requirement that rules out many significant
languages from which important lessons can be learnt.

As a matter of interest, how would they view COBOL which
has many more loop constructs than Python but they are all
done via a single command(PERFORM) which requires the
body of the loop to be a list of functions (aka paragraphs
in COBOL).
eg

FOR loop:
PERFORM paragraph val TIMES
PERFORM paragraph VARYING variable FROM val BY val UNTIL condition.

WHILE loop
PERFORM paragraph IF condition

REPEAT
PERFORM paragraph until condition

We can also modify these case to use
WITH TEST BEFORE or WITH TEST AFTER
clauses. All very powerful but very different syntactically from
Java/Pascal/VB etc.

Also, do they have any requirements on languages to support
functional programming constructs or anonymous functions/blocks?
All very important concepts.

-- 
Alan Gauld
Author of the Learn to Program web site
http://www.freenetpages.co.uk/hp/alan.gauld 


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


[Tutor] More logging probs ...

2007-11-17 Thread dave selby
Im having a bad day. The logging module refused to send anything to
syslog no matter what I did, so discovering the syslog module &
thought, for what I need I will write a simple class to do the job.

 class kmotion_logger:

def __init__(self, ident, min_priority):
# min_priority must be one of ...
# EMERG, ALERT, CRIT, ERR, WARNING, NOTICE, INFO, DEBUG

self.case = {'EMERG': syslog.LOG_EMERG,
'ALERT': syslog.LOG_ALERT,
'CRIT': syslog.LOG_CRIT,
'ERR': syslog.LOG_ERR,
'WARNING': syslog.LOG_WARNING,
'NOTICE': syslog.LOG_NOTICE,
'INFO': syslog.LOG_INFO,
'DEBUG': syslog.LOG_DEBUG}

self.ident = ident
print 'log up to & inclusive of ... ', self.case[min_priority]
syslog.setlogmask(syslog.LOG_UPTO(self.case[min_priority]))

def log(self, msg, priority):
print 'sending message at level ...',  self.case[priority]
syslog.openlog(self.ident , syslog.LOG_PID,
(self.case[priority] | syslog.LOG_USER))
syslog.syslog(msg)
syslog.closelog()

And call it with ...

import kmotion_logger
# EMERG, ALERT, CRIT, ERR, WARNING, NOTICE, INFO, DEBUG
logger = kmotion_logger.kmotion_logger("kmotion", "DEBUG")
logger.log("TESTING", "ALERT")

And it worked as I wanted, it logs to syslog (cheers, jumps for joy) :)

Then I noticed several inconsistencys, the following also works AOK ...

import kmotion_logger
# EMERG, ALERT, CRIT, ERR, WARNING, NOTICE, INFO, DEBUG
logger = kmotion_logger.kmotion_logger("kmotion", "INFO")
logger.log("TESTING", "ALERT")

But the next one fails to log ...

import kmotion_logger
# EMERG, ALERT, CRIT, ERR, WARNING, NOTICE, INFO, DEBUG
logger = kmotion_logger.kmotion_logger("kmotion", "NOTICE")
logger.log("TESTING", "ALERT")
ALERT is above NOTICE & should log  I am suspicious of
'(self.case[priority] | syslog.LOG_USER)' although it looks right and
have tried LOG_LOCAL6 etc but still no joy

I have even tried explaining it to my cat, no joy

Any ideas anyone ?

Cheers

A very log frustrated programmer

Dave




-- 

Please avoid sending me Word or PowerPoint attachments.
See http://www.gnu.org/philosophy/no-word-attachments.html
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] repeat

2007-11-17 Thread bob gailer
Michael wrote:
> Hi All
>
> This has probably been asked before but can I get some clarification on 
> why Python does not have a repeat...until statement, and does that mean 
> repeat...until is bad practice? I was trying to get Python on the 
> standard langauge list for my state secondary school system but they say 
> a langauge must have a test last structure in it to be considered.
>   
That rules out FORTRAN!

And I would point out (as one of us already did):

while True:
  blahblah
  if condition: break

Is a test-last structure.

Of course some purists will object to "break" since it is a "goto in 
disguise".
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] More logging probs ...

2007-11-17 Thread Kent Johnson
dave selby wrote:
> Im having a bad day. The logging module refused to send anything to
> syslog no matter what I did, so discovering the syslog module &
> thought, for what I need I will write a simple class to do the job.
> 
>  class kmotion_logger:
> 
> def __init__(self, ident, min_priority):
> # min_priority must be one of ...
> # EMERG, ALERT, CRIT, ERR, WARNING, NOTICE, INFO, DEBUG
> 
> self.case = {'EMERG': syslog.LOG_EMERG,
> 'ALERT': syslog.LOG_ALERT,
> 'CRIT': syslog.LOG_CRIT,
> 'ERR': syslog.LOG_ERR,
> 'WARNING': syslog.LOG_WARNING,
> 'NOTICE': syslog.LOG_NOTICE,
> 'INFO': syslog.LOG_INFO,
> 'DEBUG': syslog.LOG_DEBUG}
> 
> self.ident = ident
> print 'log up to & inclusive of ... ', self.case[min_priority]
> syslog.setlogmask(syslog.LOG_UPTO(self.case[min_priority]))
> 
> def log(self, msg, priority):
> print 'sending message at level ...',  self.case[priority]
> syslog.openlog(self.ident , syslog.LOG_PID,
> (self.case[priority] | syslog.LOG_USER))

| is a bit-wise or, not a logical or.

Logical or is not correct here anyway because self.case[priority] will 
raise KeyError for an unknown priority. Try
   self.case.get(priority, syslog.LOG_USER)
if you want to provide a default value for unknown keys.

Kent

> syslog.syslog(msg)
> syslog.closelog()
> 
> And call it with ...
> 
> import kmotion_logger
> # EMERG, ALERT, CRIT, ERR, WARNING, NOTICE, INFO, DEBUG
> logger = kmotion_logger.kmotion_logger("kmotion", "DEBUG")
> logger.log("TESTING", "ALERT")
> 
> And it worked as I wanted, it logs to syslog (cheers, jumps for joy) :)
> 
> Then I noticed several inconsistencys, the following also works AOK ...
> 
> import kmotion_logger
> # EMERG, ALERT, CRIT, ERR, WARNING, NOTICE, INFO, DEBUG
> logger = kmotion_logger.kmotion_logger("kmotion", "INFO")
> logger.log("TESTING", "ALERT")
> 
> But the next one fails to log ...
> 
> import kmotion_logger
> # EMERG, ALERT, CRIT, ERR, WARNING, NOTICE, INFO, DEBUG
> logger = kmotion_logger.kmotion_logger("kmotion", "NOTICE")
> logger.log("TESTING", "ALERT")
> ALERT is above NOTICE & should log  I am suspicious of
> '(self.case[priority] | syslog.LOG_USER)' although it looks right and
> have tried LOG_LOCAL6 etc but still no joy
> 
> I have even tried explaining it to my cat, no joy
> 
> Any ideas anyone ?
> 
> Cheers
> 
> A very log frustrated programmer
> 
> Dave
> 
> 
> 
> 

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


Re: [Tutor] More logging probs ...

2007-11-17 Thread dave selby
OK so to condense the problem the following works at LOG_INFO default
priority ...

def log(self, msg, priority):
syslog.openlog(self.ident , syslog.LOG_PID)
syslog.syslog('testing message')
syslog.closelog()

But as soon as I try to set a priority API syslog docs, it fails to log ...

def log(self, msg, priority):
syslog.openlog(self.ident , syslog.LOG_PID)
syslog.syslog((syslog.LOG_ALERT | syslog.LOG_USER), 'testing message')
syslog.closelog()

PS tried LOG_LOCAL6 etc, same result.

Trying to change the priority in openlog() logs but does not appear to
change the priority since syslog.setlogmask() indicate it is stuck at
LOG_INFO

def log(self, msg, priority):
syslog.openlog(self.ident , syslog.LOG_PID, (syslog.LOG_ALERT
| syslog.LOG_LOCAL6))
syslog.syslog('testing message')
syslog.closelog()

Still stuck

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


[Tutor] repeat

2007-11-17 Thread Michael H. Goldwasser

On Saturday November 17, 2007, Michael wrote: 

>Hi All
>
>This has probably been asked before but can I get some clarification on 
>why Python does not have a repeat...until statement, and does that mean 
>repeat...until is bad practice? I was trying to get Python on the 
>standard langauge list for my state secondary school system but they say 
>a langauge must have a test last structure in it to be considered.


That is quite an arbitrary rule for a state to use when adopting a
programming language.  In any event, if they are also uncomfortable
with the while(True)/break combination that has been suggested by
others, you can alternatively mimic the repeat/until by using a
well-named boolean variable, as follows.


repeating = True
while repeating:

# ...


if (exitCondition):
repeating = False



or in a negated form (if that reads better), as


done = False
while not done:

# ...

if (exitCondition):
done = True



Perhaps these kinds of examples might help ease their concerns.

With regard,
Michael


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


[Tutor] Web programming

2007-11-17 Thread Dinesh B Vadhia
Hi!  I want to create (for testing purposes) a straightforward web application 
consisting of a client that makes simple queries to a backend which returns 
data from a database (initially pysqlite3).  That's it - really!   I don't need 
a professional web server (eg. Apache) per se.

Are the Python urlparse, urllib, urllib2, httplib, BaseHTTPServer, 
SimpleHTTPServer etc. modules sufficient for the task.  The number of queries 
per second will initially be low, in the 10's/second.

Dinesh



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


Re: [Tutor] repeat

2007-11-17 Thread Scott Sandeman-Allen
On 11/17/07, bob gailer ([EMAIL PROTECTED]) wrote:

>Michael wrote:
>> Hi All
>>
>> This has probably been asked before but can I get some clarification on 
>> why Python does not have a repeat...until statement, and does that mean 
>> repeat...until is bad practice? I was trying to get Python on the 
>> standard langauge list for my state secondary school system but they say 
>> a langauge must have a test last structure in it to be considered.
>>   
>That rules out FORTRAN!
>
>And I would point out (as one of us already did):
>
>while True:
>  blahblah
>  if condition: break
>
>Is a test-last structure.

If one considers the "repeat" statement to have an _implied_ truthfulness there 
is even less difference _except_ for the nesting syntax which IMO is arbitrary 
and therefore moot.

repeat [while true]
perform task
until [proven false by] condition

or  
do [while true]
blah blah blah

VS the explicitly stated

>while True:
>  blahblah
>  if condition: break

Python is a very rich language and provides a great deal of flexibility, even 
with this single flow-control statement i.e.

while [state | condition] maintains truthfulness:
perform task [intrinsically modify state | argument(s)]
[test argument(s) & exit | modify state]

Python is a modern and full-featured language. The test of its sutability 
should be based upon its clarity and capability rather than an arbitrary 
syntactical (sp?) construct.

Can the language clearly perform looping constructs: yes, in many ways.
Does the language have a reasonable level of flow-control: yes.
...

I could see a language like PHP being excluded because of its dependence upon 
environment. But if I were to teach a language to a student, Python would 
probably be one of my first choices as opposed to C/Java/Perl/VB/AppleScript 
and such.

Ultimately, what is the objective of the programming programme? Is it to teach 
students to consider application development at a high-level or incumber them 
with unnecessary syntax at a low level i.e. 

def some_variable as some_type

test some_variable then
...
end test

IMO, the def, then and end are unnecessary cluter; totally superfluious!

Personally, I would rather my students (if I had any) be less incumbered with 
"verbosity" and more focussed on planning and understanding. Letting the 
language work for them rather than enslaving them.

"When the only tool you have is a hammer, all of your problems start looking 
like nails" is a favorite quote of mine (unknown origin). Python is one heck of 
a hammer to have in the bag.

Scott

PS. Interesting thing about Python: in minutes a student could be working with 
the language i.e. 'Hello world', and understand it. Within days they can be 
writing functional code with reasonable complexity. The kicker is, the language 
is deep enough that new approaches can still appear many months and even years 
of regular use. Not only is it tasty, it's nutritious too ;^)
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


[Tutor] [wxPython-users] How to save file name of file opened from wx.FileDialog ?

2007-11-17 Thread Varsha Purohit
Hello Everyone,
In my application i need to select a file using open dialog
box. And then i dont need to open the file. I just need to display the
name of the selected file in a text control. And then open the file in
later part of the program.  But i am not able to get the file name and
display it in the text control. Here is the sample code.

import wx
import os

Fname = '' #Global variable to hold the file name.
class ScrolledWindow(wx.Frame):
def __init__(self, parent, id, title):
wx.Frame.__init__(self, parent, id, title, size=(350, 300))
panel = wx.Panel(self, -1)
txt1 = wx.TextCtrl(panel, -1, pos=(30, 100), size=(150, 20))
button11 = wx.Button(panel, -1, "Open", pos=(200,100))
self.Bind(wx.EVT_BUTTON, self.OnOpen, button11)
txt1.write(Fname)

self.Centre()
self.Show()

def OnOpen(self,event):
self.dirname = ''
dlg = wx.FileDialog(self, "Choose a file", self.dirname,"",
"*.*", wx.OPEN)
if dlg.ShowModal()==wx.ID_OK:
self.filename=dlg.GetFilename()
Fname = self.filename
self.dirname=dlg.GetDirectory()
dlg.Destroy()

app = wx.App()
ScrolledWindow(None, -1, 'Aliens')
app.MainLoop()


Any help is appreciated

thanks,
-- 
Varsha Purohit,
Graduate Student
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Web programming

2007-11-17 Thread Alan Gauld
"Dinesh B Vadhia" <[EMAIL PROTECTED]> wrote

> web application consisting of a client that makes simple queries
> to a backend which returns data from a database (initially 
> pysqlite3).

When you say a client do you mean a robotic browser?
Or do you mean the web application will be a client/server 
architecture?

> Are the Python urlparse, urllib, urllib2,

These will build a robotic client of that's what you need.

> httplib,

You probably won't need this if it's just a simple app.

> BaseHTTPServer, SimpleHTTPServer etc.

These are OK to start with but you will also need the
cgi module to create the server code

> The number of queries per second will initially be low, in the 
> 10's/second.

tens per second is quite high for a simple CGI app
accessing a database. Up to 10 per second should be fine but
if you get up to 20,30 or more I'd consider using a real web
server (Apache or a simpler one like Xitami) and you may even
want to investigate fastCGI or some of the simpler web
frameworks like CherryPy.

Finally watch out for locking issues around the database
if your transactions are writing as well as reading data.

HTH,

-- 
Alan Gauld
Author of the Learn to Program web site
http://www.freenetpages.co.uk/hp/alan.gauld 


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


Re: [Tutor] [wxPython-users] How to save file name of file opened fromwx.FileDialog ?

2007-11-17 Thread Alan Gauld

"Varsha Purohit" <[EMAIL PROTECTED]> wrote

> later part of the program.  But i am not able to get the file name 
> and
> display it in the text control. Here is the sample code.
>
> Fname = '' #Global variable to hold the file name.

You don't need this since its stored in self.filename.

> class ScrolledWindow(wx.Frame):
>def __init__(self, parent, id, title):
>txt1 = wx.TextCtrl(panel, -1, pos=(30, 100), size=(150, 20))

If you want to write to it you need to store it in the object so this
should be self.txt1

>txt1.write(Fname)

You can't write the filename yet as it hasn't been fetched

>
>def OnOpen(self,event):
>self.dirname = ''
>dlg = wx.FileDialog(self, "Choose a file", self.dirname,"",
> "*.*", wx.OPEN)
>if dlg.ShowModal()==wx.ID_OK:
>self.filename=dlg.GetFilename()

Here you get the filename but don't write it to the text control

HTH,

Alan G. 


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


Re: [Tutor] Web programming

2007-11-17 Thread Kent Johnson
Dinesh B Vadhia wrote:
> Hi!  I want to create (for testing purposes) a straightforward web 
> application consisting of a client that makes simple queries to a 
> backend which returns data from a database (initially pysqlite3).  
> That's it - really!   I don't need a professional web server (eg. 
> Apache) per se.

I did something like this once with CherryPy, it worked quite well.

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


Re: [Tutor] Web programming

2007-11-17 Thread Michael Langford
While CherryPy is suited to what you're doing, I personally think
Mod_Python and Apache are easier to use for this sort of thing. Here
is an article showing how to set it up:
http://modpython.org/live/current/doc-html/installation.html

And a good first quick program that clearly explains what errors mean
when you're trying to get it up and going:
http://www.dscpl.com.au/wiki/ModPython/Articles/GettingModPythonWorking

I was amazed how helpful that second page was on setting things up. I
think it took me about 20-25 min.

   --Michael

On Nov 17, 2007 12:14 PM, Dinesh B Vadhia <[EMAIL PROTECTED]> wrote:
>
>
> Hi!  I want to create (for testing purposes) a straightforward web
> application consisting of a client that makes simple queries to a backend
> which returns data from a database (initially pysqlite3).  That's it -
> really!   I don't need a professional web server (eg. Apache) per se.
>
> Are the Python urlparse, urllib, urllib2, httplib, BaseHTTPServer,
> SimpleHTTPServer etc. modules sufficient for the task.  The number of
> queries per second will initially be low, in the 10's/second.
>
> Dinesh
>
>
>
>
> ___
> Tutor maillist  -  Tutor@python.org
> http://mail.python.org/mailman/listinfo/tutor
>
>



-- 
Michael Langford
Phone: 404-386-0495
Consulting: http://www.RowdyLabs.com
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] [wxPython-users] How to save file name of file opened fromwx.FileDialog ?

2007-11-17 Thread Varsha Purohit
Hi Alan,
I am actually calling the binding function and then writing it
into the text value... i tried using simple print in the openfile
function and it shows the filename. I am trying to return the file
name value but even that is not responding...

class ScrolledWindow(wx.Frame):
def __init__(self, parent, id, title):
wx.Frame.__init__(self, parent, id, title, size=(350, 300))
panel = wx.Panel(self, -1)
txt1 = wx.TextCtrl(panel, -1, pos=(30, 100), size=(150, 20))
button11 = wx.Button(panel, -1, "Open", pos=(200,100))
name = self.Bind(wx.EVT_BUTTON, self.OnOpen, button11)
txt1.write(name)

self.Centre()
self.Show()

def OnOpen(self,event):
self.dirname = ''
dlg = wx.FileDialog(self, "Choose a file", self.dirname,"",
"*.*", wx.OPEN)
if dlg.ShowModal()==wx.ID_OK:
self.filename=dlg.GetFilename()
Fname = self.filename
self.dirname=dlg.GetDirectory()
#f=open(os.path.join(self.dirname, self.filename),'r')
#self.control.SetValue(f.read())
   # self.txt1.WriteText(Fname)
f.close()

dlg.Destroy()
return Fname

app = wx.App()
ScrolledWindow(None, -1, 'Aliens')
app.MainLoop()

I donno the alternative to this now... :(

On Nov 17, 2007 3:35 PM, Alan Gauld <[EMAIL PROTECTED]> wrote:
>
> "Varsha Purohit" <[EMAIL PROTECTED]> wrote
>
> > later part of the program.  But i am not able to get the file name
> > and
> > display it in the text control. Here is the sample code.
> >
> > Fname = '' #Global variable to hold the file name.
>
> You don't need this since its stored in self.filename.
>
> > class ScrolledWindow(wx.Frame):
> >def __init__(self, parent, id, title):
> >txt1 = wx.TextCtrl(panel, -1, pos=(30, 100), size=(150, 20))
>
> If you want to write to it you need to store it in the object so this
> should be self.txt1
>
> >txt1.write(Fname)
>
> You can't write the filename yet as it hasn't been fetched
>
> >
> >def OnOpen(self,event):
> >self.dirname = ''
> >dlg = wx.FileDialog(self, "Choose a file", self.dirname,"",
> > "*.*", wx.OPEN)
> >if dlg.ShowModal()==wx.ID_OK:
> >self.filename=dlg.GetFilename()
>
> Here you get the filename but don't write it to the text control
>
> HTH,
>
> Alan G.
>
>
> ___
> Tutor maillist  -  Tutor@python.org
> http://mail.python.org/mailman/listinfo/tutor
>



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


Re: [Tutor] [wxPython-users] How to save file name of file opened from wx.FileDialog ?

2007-11-17 Thread Varsha Purohit
HI Alan,
Thanks for suggestion its working now

import wx
import os

class ScrolledWindow(wx.Frame):
def __init__(self, parent, id, title):
wx.Frame.__init__(self, parent, id, title, size=(350, 300))
panel = wx.Panel(self, -1)
self.txt1 = wx.TextCtrl(panel, -1, pos=(30, 100), size=(150, 20))
self.button11 = wx.Button(panel, -1, "Open", pos=(200,100))
self.button11.Bind(wx.EVT_BUTTON, self.OnOpen)

self.Centre()
self.Show()

def OnOpen(self,event):
self.dirname = ''
dlg = wx.FileDialog(self, "Choose a file", self.dirname,"",
"*.*", wx.OPEN)
if dlg.ShowModal()==wx.ID_OK:
self.filename=dlg.GetFilename()
   self.dirname=dlg.GetDirectory()

self.txt1.write(self.filename)
dlg.Destroy()


app = wx.App()
ScrolledWindow(None, -1, 'Aliens')
app.MainLoop()

On Nov 17, 2007 12:44 PM, Varsha Purohit <[EMAIL PROTECTED]> wrote:
> Hello Everyone,
> In my application i need to select a file using open dialog
> box. And then i dont need to open the file. I just need to display the
> name of the selected file in a text control. And then open the file in
> later part of the program.  But i am not able to get the file name and
> display it in the text control. Here is the sample code.
>
> import wx
> import os
>
> Fname = '' #Global variable to hold the file name.
> class ScrolledWindow(wx.Frame):
> def __init__(self, parent, id, title):
> wx.Frame.__init__(self, parent, id, title, size=(350, 300))
> panel = wx.Panel(self, -1)
> txt1 = wx.TextCtrl(panel, -1, pos=(30, 100), size=(150, 20))
> button11 = wx.Button(panel, -1, "Open", pos=(200,100))
> self.Bind(wx.EVT_BUTTON, self.OnOpen, button11)
> txt1.write(Fname)
>
> self.Centre()
> self.Show()
>
> def OnOpen(self,event):
> self.dirname = ''
> dlg = wx.FileDialog(self, "Choose a file", self.dirname,"",
> "*.*", wx.OPEN)
> if dlg.ShowModal()==wx.ID_OK:
> self.filename=dlg.GetFilename()
> Fname = self.filename
> self.dirname=dlg.GetDirectory()
> dlg.Destroy()
>
> app = wx.App()
> ScrolledWindow(None, -1, 'Aliens')
> app.MainLoop()
>
>
> Any help is appreciated
>
> thanks,
> --
> Varsha Purohit,
> Graduate Student
>
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Read-ahead for large fixed-width binary files?

2007-11-17 Thread Marc Tompkins
Alan Gauld wrote:

"Marc Tompkins" <[EMAIL PROTECTED]> wrote
> realized I can implement this myself, using 'read(bigsize)' -
> currently I'm using 'read(recordsize)'; I just need to add an extra
> loop around my record reads.  Please disregard...
If you just want to navigate to a specific record then it might be
easier to use seek(), that will save you having to read all the
previous records into memory.


No, I need to parse the entire file, checking records as I go.  Here's the
solution I came up with - I'm sure it could be optimized, but it's already
about six times faster than going record-by-record:

def loadInsurance(self):
header = ('Code', 'Name')
Global.Ins.append(header)
obj = Insurance()
recLen = obj.RecordLength
for offNum, offPath in Global.offices.iteritems():
if (offPath.Ref == ''):
offPath.Ref = offPath.Default
with open(offPath.Ref + obj.TLA + '.dat','rb') as inFile:
tmpIn = inFile.read(recLen) # throw away the
header record
tmpIn = inFile.read(recLen*4096)
while not (len(tmpIn) < recLen):
buf = StringIO.StringIO(tmpIn)
inRec = buf.read(recLen)
while not (len(inRec) < recLen):
obj = Insurance(inRec)
if (obj.Valid):
Global.Ins.append(obj.ID, obj.Name)
inRec = buf.read(recLen)
buf.close()
tmpIn = inFile.read(recLen*4096)

Obviously this is taken out of context, and I'm afraid I'm too lazy to
sanitize it (much) for posting right now, so here's a brief summary instead.

1-  I don't want my calling code to need to know many details.  So if I
create an object with no parameters, it provides me with the record length
(files vary from 80-byte records up to 1024) and the TLA portion of the
filename (the data files are named in the format xxTLA.dat, where xx is the
2-digit office number and TLA is the three-letter acronym for what the file
contains - e.g. INS for insurance.)

2-  Using the information I just obtained, I then read through the file one
record-length chunk at a time, creating an object out of each chunk and
reading the attributes of that object.  In the next version of my class
library, I'll move the whole list-generation logic inside the classes so I
can just pass in a filename and receive a list... but that's one for my
copious free time.

3-  Each file contains a header record, which is pure garbage.  I read it in
and throw it away before I even begin.  (I could seek to just past it
instead - would it really be more efficient?)

4-  Now here's where the read-ahead buffer comes in - I (attempt to) read
4096 records' worth of data, and store it in a StringIO file-like object.
(4096 is just a number I pulled out of the air, but I've tried increasing
and decreasing it, and it seems good.  If I have the time, I may benchmark
to find the best number for each record length, and retrieve that number
along with the record length and TLA.  Of course, the optimal number
probably varies per machine, so maybe I won't bother.)

5-  Now I go through the buffer, one record's worth at a time, and do
whatever I'm doing with the records - in this case, I'm making a list of
insurance company IDs and names to display in a wx.CheckListCtrl.

6-  If I try to read past the end of the file, there's no error - so I need
to check the size of what's returned.  If it's smaller than recLen, I know
I've hit the end.
 6a- When I hit the end of the buffer, I close it and read in another 4096
records.
 6b- When I try to read 4096 records, and end up with less than recLen, I
know I've hit the end of the file.

I've only tested on a few machines/client databases so far, but when I added
step 4, processing a 250MB transaction table (256-byte records) went from
nearly 30 seconds down to about 3.5 seconds.  Other results have varied, but
they've all shown improvement.

If anybody sees any glaring inefficiencies, let me know; OTOH if anybody
else needs to do something similar... here's one way to do it.

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


Re: [Tutor] Read-ahead for large fixed-width binary files?

2007-11-17 Thread Kent Johnson
Marc Tompkins wrote:
> My question is this: does anybody know of an equivalent to
> "readlines(sizehint)" for non-delimited, binary files?  I've Googled
> and Googled until I'm groggy, but I don't seem to find what I want.

Have you tried specifying a buffer size in the open() call?

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


Re: [Tutor] Read-ahead for large fixed-width binary files?

2007-11-17 Thread Kent Johnson
I would wrap the record buffering into a generator function and probably 
use plain slicing to return the individual records instead of StringIO. 
I have a writeup on generators here:

http://personalpages.tds.net/~kent37/kk/4.html

Kent

Marc Tompkins wrote:
> Alan Gauld wrote:
> 
> "Marc Tompkins" <[EMAIL PROTECTED]
> > wrote
>  > realized I can implement this myself, using 'read(bigsize)' -
>  > currently I'm using 'read(recordsize)'; I just need to add an extra
>  > loop around my record reads.  Please disregard...
> If you just want to navigate to a specific record then it might be
> easier to use seek(), that will save you having to read all the
> previous records into memory.
> 
> 
> No, I need to parse the entire file, checking records as I go.  Here's 
> the solution I came up with - I'm sure it could be optimized, but it's 
> already about six times faster than going record-by-record:
> 
> def loadInsurance(self):
> header = ('Code', 'Name')
> Global.Ins.append(header)
> obj = Insurance()
> recLen = obj.RecordLength
> for offNum, offPath in Global.offices.iteritems():
> if (offPath.Ref == ''):
> offPath.Ref = offPath.Default
> with open(offPath.Ref + obj.TLA + '.dat','rb') as inFile:
> tmpIn = inFile.read(recLen) # throw away the 
> header record
> tmpIn = inFile.read(recLen*4096)
> while not (len(tmpIn) < recLen):
> buf = StringIO.StringIO (tmpIn)
> inRec = buf.read(recLen)
> while not (len(inRec) < recLen):
> obj = Insurance(inRec)
> if (obj.Valid):
> Global.Ins.append (obj.ID, obj.Name)
> inRec = buf.read(recLen)
> buf.close()
> tmpIn = inFile.read(recLen*4096)
> 
> Obviously this is taken out of context, and I'm afraid I'm too lazy to 
> sanitize it (much) for posting right now, so here's a brief summary 
> instead.
> 
> 1-  I don't want my calling code to need to know many details.  So if I 
> create an object with no parameters, it provides me with the record 
> length (files vary from 80-byte records up to 1024) and the TLA portion 
> of the filename (the data files are named in the format xxTLA.dat, where 
> xx is the 2-digit office number and TLA is the three-letter acronym for 
> what the file contains - e.g. INS for insurance.) 
> 
> 2-  Using the information I just obtained, I then read through the file 
> one record-length chunk at a time, creating an object out of each chunk 
> and reading the attributes of that object.  In the next version of my 
> class library, I'll move the whole list-generation logic inside the 
> classes so I can just pass in a filename and receive a list... but 
> that's one for my copious free time.
> 
> 3-  Each file contains a header record, which is pure garbage.  I read 
> it in and throw it away before I even begin.  (I could seek to just past 
> it instead - would it really be more efficient?)
> 
> 4-  Now here's where the read-ahead buffer comes in - I (attempt to) 
> read 4096 records' worth of data, and store it in a StringIO file-like 
> object.  (4096 is just a number I pulled out of the air, but I've tried 
> increasing and decreasing it, and it seems good.  If I have the time, I 
> may benchmark to find the best number for each record length, and 
> retrieve that number along with the record length and TLA.  Of course, 
> the optimal number probably varies per machine, so maybe I won't bother.)
> 
> 5-  Now I go through the buffer, one record's worth at a time, and do 
> whatever I'm doing with the records - in this case, I'm making a list of 
> insurance company IDs and names to display in a wx.CheckListCtrl .
> 
> 6-  If I try to read past the end of the file, there's no error - so I 
> need to check the size of what's returned.  If it's smaller than recLen, 
> I know I've hit the end.
>  6a- When I hit the end of the buffer, I close it and read in another 
> 4096 records. 
>  6b- When I try to read 4096 records, and end up with less than recLen, 
> I know I've hit the end of the file.
> 
> I've only tested on a few machines/client databases so far, but when I 
> added step 4, processing a 250MB transaction table (256-byte records) 
> went from nearly 30 seconds down to about 3.5 seconds.  Other results 
> have varied, but they've all shown improvement.
> 
> If anybody sees any glaring inefficiencies, let me know; OTOH if anybody 
> else needs to do something similar... here's one way to do it.
> 
> -- 
> www.fsrtechnologies.com 
> 
> 
> 
> 
> ___
> Tutor maillist  -  Tutor@python.org
> http://mail.python.org/mailman/listinfo/tutor

__

Re: [Tutor] Read-ahead for large fixed-width binary files?

2007-11-17 Thread Marc Tompkins
On Nov 17, 2007 8:14 PM, Kent Johnson <[EMAIL PROTECTED]> wrote:

> Marc Tompkins wrote:
> > My question is this: does anybody know of an equivalent to
> > "readlines(sizehint)" for non-delimited, binary files?  I've Googled
> > and Googled until I'm groggy, but I don't seem to find what I want.
>
> Have you tried specifying a buffer size in the open() call?
>
> Kent
>

Yes.
I compared:
-  no buffer size specified
-  any of a wide range of positive numbers (from 1 to 4M)
-  -1
and saw no noticeable difference - as opposed to adding the StringIO
buffering, which kicked things up by a notch of six or so.

By the way, this is really obscurely documented.  It took me a lot of
Googling to find even one mention of it - in Programming Python by Mark Lutz
- and I was very excited... until I tested it and found that it did nothing
for me.  Bummer.  Then I re-read the passage:

> *Buffer size*
>
> The open call also takes an optional third buffer size argument, which
> lets you control stdio buffering for the file -- the way that data is
> queued up before being transferred to boost performance. If passed, means
> file operations are unbuffered (data is transferred immediately), 1 means
> they are line buffered, any other positive value means use a buffer of
> approximately that size, and a negative value means to use the system
> default (which you get if no third argument is passed, and generally means
> buffering is enabled). The buffer size argument works on most platforms, but
> is currently ignored on platforms that don't provide the sevbuf system
> call.
>
I've only tested on Windows XP; is XP one of those that don't provide
sevbuf?  (Actually, I think that's a typo - I think it should be "setvbuf" -
but it exists in both the 2001 and 2006 editions of the book.)  Perhaps
someone who codes closer to the silicon can enlighten me on that score.

I just realized that the one thing I didn't try was passing a value of 0 to
turn buffering OFF -  if, as the above passage seems to suggest, it's always
on by default.  I might try that tomorrow.  But in any case it looks like
all it'll do is make things slower.

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


Re: [Tutor] Read-ahead for large fixed-width binary files?

2007-11-17 Thread Marc Tompkins
L'esprit d'escalier - once again I notice something only after hitting
Send.  How do you reconcile these phrases:

> If passed, means file operations are unbuffered

and

> the system default (which you get if no third argument is passed, and
> generally means buffering is enabled)
>

I think my head's going to explode - my negative capacity is overloaded!

On Nov 17, 2007 9:03 PM, Marc Tompkins <[EMAIL PROTECTED]> wrote:

> On Nov 17, 2007 8:14 PM, Kent Johnson <[EMAIL PROTECTED]> wrote:
>
> > Marc Tompkins wrote:
> > > My question is this: does anybody know of an equivalent to
> > > "readlines(sizehint)" for non-delimited, binary files?  I've Googled
> > > and Googled until I'm groggy, but I don't seem to find what I want.
> >
> > Have you tried specifying a buffer size in the open() call?
> >
> > Kent
> >
>
> Yes.
> I compared:
> -  no buffer size specified
> -  any of a wide range of positive numbers (from 1 to 4M)
> -  -1
> and saw no noticeable difference - as opposed to adding the StringIO
> buffering, which kicked things up by a notch of six or so.
>
> By the way, this is really obscurely documented.  It took me a lot of
> Googling to find even one mention of it - in Programming Python by Mark Lutz
> - and I was very excited... until I tested it and found that it did nothing
> for me.  Bummer.  Then I re-read the passage:
>
> > *Buffer size*
> >
> > The open call also takes an optional third buffer size argument, which
> > lets you control stdio buffering for the file -- the way that data is
> > queued up before being transferred to boost performance. If passed, means
> > file operations are unbuffered (data is transferred immediately), 1 means
> > they are line buffered, any other positive value means use a buffer of
> > approximately that size, and a negative value means to use the system
> > default (which you get if no third argument is passed, and generally means
> > buffering is enabled). The buffer size argument works on most platforms, but
> > is currently ignored on platforms that don't provide the sevbuf system
> > call.
> >
> I've only tested on Windows XP; is XP one of those that don't provide
> sevbuf?  (Actually, I think that's a typo - I think it should be "setvbuf" -
> but it exists in both the 2001 and 2006 editions of the book.)  Perhaps
> someone who codes closer to the silicon can enlighten me on that score.
>
> I just realized that the one thing I didn't try was passing a value of 0
> to turn buffering OFF -  if, as the above passage seems to suggest, it's
> always on by default.  I might try that tomorrow.  But in any case it looks
> like all it'll do is make things slower.
>
> --
> www.fsrtechnologies.com




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


Re: [Tutor] Read-ahead for large fixed-width binary files?

2007-11-17 Thread Marc Tompkins
On Nov 17, 2007 8:20 PM, Kent Johnson <[EMAIL PROTECTED]> wrote:

> I would wrap the record buffering into a generator function and probably
> use plain slicing to return the individual records instead of StringIO.
> I have a writeup on generators here:
>
> http://personalpages.tds.net/~kent37/kk/4.html
>
> Kent
>

I can see the benefit of wrapping the buffering in a generator - after all,
a large point of the exercise is to make this and all future programs
simpler to write and to debug - but I don't understand the second part

> use plain slicing to return the individual records instead of StringIO.

I hope I'm not being obtuse, but could you clarify that?

Meditating further, I begin to see a way through... the generator reads in
chunks 4096 records long; an internal iterator keeps track of how far along
to slice the thing; I need to check to make sure I don't try to slice past
the end of the loaf...  I'm not sure I see how this makes my life better
than using StringIO (especially since I'm actually using cStringIO, with a
"just-in-case" fallback in the import section, and it seems to be pretty
fast.)
Of course, if I load the entire file into memory I only need to check the
size once, but several of my clients have transaction files (to name only
one file) that are larger than their physical RAM, so that approach would
seem... problematic.  I'm trying to enhance performance, not kill it.

Thanks in advance for your insight -

Marc
-- 
www.fsrtechnologies.com
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor