Re: [Tutor] Please look at my wordFrequency.py

2005-10-12 Thread Dick Moores
John Fouhy wrote at 15:09 10/11/2005:
>On 12/10/05, Dick Moores <[EMAIL PROTECTED]> wrote:
>
>Hi Dick,
>
>Glad you're making progress :-)
>
> > Yes, that's about the difference I was seeing. Thanks for taking the
> > trouble. I went from 30 to 27. With no regex use (don't understand it 
> yet).
>
>Regular expressions are a power tool of text processing.  They are a
>bit tricky to learn, but once you've got the hang of them you'll find
>they can save you a lot of effort.  I'm not sure of a good tutorial,
>but if you look through the archives of this list, there's been a lot
>of discussion of them in the past :-)

Yes, but I think I need to go at something complex systematically. In my 
bookcase I have the 1st edition of Friedl's _Mastering Regular 
Expressions_ O'Reilly, 1997. Because there is now a 2nd edition (2002) 
I'd been ignoring what I have, but in the Python 2.4 Python Library 
Reference section 4.2 re -- Regular expression operations I find:

"
See Also:Mastering Regular Expressions Book on regular expressions by 
Jeffrey Friedl, published by O'Reilly. The second edition of the book no 
longer covers Python at all, but the first edition covered writing good 
regular expression patterns in great detail. "

So I'd like to ask if there is agreement about the value of this book, 
and if the 1st is really better that the 2nd ed. for regex in Python.

In Python Library Reference 4.2.1 Regular Expression Syntax there is this:

"A brief explanation of the format of regular expressions follows. For 
further information and a gentler presentation, consult the Regular 
Expression HOWTO, accessible from 
http://www.python.org/doc/howto/.";

So I thought I'd start with that. What  do you think? I'm not a complete 
regex beginner, but I've forgotten most of what I once knew. A long time 
ago (12 years) I had a well-equipped dial-up shell (tcsh) account with 
helpful users (Netcom) and enjoyed learning something of the power of 
grep, etc.

The rest of what you wrote below is very clear and enlightening, and just 
right for my level. Are you a teacher? Ever thought about writing a 
Python book?

> > WOW! I didn't implement John's change because I didn't understand it.
> > Haven't dealt with dictionaries yet.
>
>Sorry about that.
>
>I think, basically, your project is in two parts: a "tokenization"
>part and a "counting" part.
>
>By tokenization, I mean the act of splitting up your input text into
>words (or "tokens").  This is the bit where there's no obvious right
>way, and you will probably need some ad-hoc code.
>
>Once you've done that, you move to the counting step: You have a list
>of words, and you want to count occurrences of each word.
>
>Think about how you would do such a task by hand with pen and paper.
>One approach you could take is to find the first word, then look
>through the document counting how many times the first word occurs.
>Then move to the second word, and look through the document again.
>You will end up looking through the entire document many times.
>
>But then you might think to yourself --- "When I count the first word,
>I have to _look at_ every other word in the document.  Why can't I
>count them at the same time?"  So you grab a piece of paper, write the
>words along the top, and underneath each word you keep a tally.  You
>look through your document, and for each word, you increase the
>corresponding tally.  When you're done, you have a count of every
>word, and you've only gone through your document once.

#My code
for word in L:
 k = L.count(word)
 if (k,word) not in F:
 F.append((k,word))

I wrote this quickly and it worked correctly--I was even proud of myself; 
I didn't think about how it did what it did--that it would repeatedly and 
unnecessarily look at each element of L and do too much with F as well 
(as Kent pointed out). Take L.count(word). I had this vague notion that 
it would take a quick look at all of L at once and come up with a count 
without actually counting, the way I can see a group of starlings in my 
back yard and know there are 4 without actually counting them. However, 5 
or more I count. But computers are magical, and not limited to 4, was my 
non-thinking thinking.

>This is the basic idea of the dictionary-based approach.  A dictionary
>is an efficient data structure for keeping these tallies.  In Big-O
>notation, it is O(1).  Any python tutorial can tell you more about
>dictionaries.
>
>Hope this helps :-)
>
> > Ah. But how can I know what is in C code and what isn't? For example, in
> > a previous post you say that L.extend(e) is in C, and imply that
> > L.append(e) isn't, and that therefore L.extend(e) should be used.
>
>The first rule about optimization is --- don't.  Unless you're really
>sure that you need to.
>
>I think a good general strategy is:
>  - If there is a builtin function that does what you want, use it.
>This does require you to be familiar with the standard library.  The
>most im

[Tutor] another error in tagging program

2005-10-12 Thread enas khalil
hello all
i have run  the following code :# the taggerI interfacefrom nltk.tagger import *from nltk.tokenizer import WhitespaceTokenizerfrom nltk.tokenizer import *from nltk.token import *
# Using UnigramTagger##Before aUnigramTagger can be used to tag data ,it must be trained on training corpus,# it uses this corpus to determine which tags are most common for each word . UnigramTaggers# are trained using the train method which takes a tagged corpus:# tag2.txt is a tagged trainning corpustagged_txt_str=open('tag2.txt').read()tagged_txt_token=Token(TEXT=tagged_txt_str)WhitespaceTokenizer().tokenize(tagged_txt_token)#TaggedTokenizer().tokenize(tagged_txt_token)#Tagger().tokenize(tagged_txt_token)tagger=UnigramTagger()tagger.train(tagged_txt_token) 
 
and got the following error :
Traceback (most recent call last):  File "F:\MSC first Chapters\error correct.py", line 19, in -toplevel-    tagger.train(tagged_txt_token)  File "C:\Python24\Lib\site-packages\nltk\tagger\__init__.py", line 332, in train    tag = subtok[TAG]KeyError: 'TAG'>>> 
please help 
thanks 
enas
		 Yahoo! Music Unlimited - Access over 1 million songs. Try it free.___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Please look at my wordFrequency.py

2005-10-12 Thread Alan Gauld
> See Also:Mastering Regular Expressions Book on regular expressions by 
> Jeffrey Friedl, published by O'Reilly. The second edition of the book no 
> longer covers Python at all, but the first edition covered writing good 
> regular expression patterns in great detail. "
>
> So I'd like to ask if there is agreement about the value of this book,

This is the bible of regular expressions, without peer.

> and if the 1st is really better that the 2nd ed. for regex in Python.

DEbateable, the second covers some more modern additions to the RE catalog
but misses Python out. The first inclyudes Python but misses the 
atest/greatest.
Either edition is invaluable, you only need one so if you have the first 
don't
worry about getting the second IMHO.

> further information and a gentler presentation, consult the Regular 
> Expression HOWTO, accessible from 
> http://www.python.org/doc/howto/.";

If you want an even gentler intro - really an intro to the intro - look at 
the
RE page on my tutorial. It covers the very basic concepts.

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


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


Re: [Tutor] another error in tagging program

2005-10-12 Thread w chun
On 10/12/05, enas khalil <[EMAIL PROTECTED]> wrote:
>
> and got the following error :
> Traceback (most recent call last):
>   File "F:\MSC first Chapters\error correct.py", line 19, in -toplevel-
> tagger.train(tagged_txt_token)
>   File
> "C:\Python24\Lib\site-packages\nltk\tagger\__init__.py",
> line 332, in train
> tag = subtok[TAG]
> KeyError: 'TAG'
> >>>
> please help
> thanks
> enas


this error is due to TAG = 'TAG' and this key not being in your subtok
dictionary.  what are you trying to accomplish in your application? 
it looks like you are following a lesson plan of a course in natural
language processing.

-- wesley
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
"Core Python Programming", Prentice Hall, (c)2006,2001
http://corepython.com

wesley.j.chun :: wescpy-at-gmail.com
cyberweb.consulting : silicon valley, ca
http://cyberwebconsulting.com
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


[Tutor] launching and monitor from make

2005-10-12 Thread Ertl, John
All,

I have a very simple python program that reads one file and overwrites
anouther text file.  This workes great from the command line and it has
error checking but just outputs messages and the like to the screen.

The CM team here would like to have the script run each week as part of the
automated rebuilds.  The suggestion was to make the execution of the scritp
part of the bigger programs make file.  How would I handel errors when the
program is run from a makefile?  I hate make so I have not done a lot of
playing with what make can do in this respect but can python get an error
message to make?

Thanks,

John Ertl

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


[Tutor] slide show won't display the images in right order

2005-10-12 Thread Joseph Quigley
Hi,
Here's my directory listing as a list:
['ga041001.gif', 'ga041002.gif', 'ga041003.gif', 'ga041004.gif',
'ga041005.gif', 'ga041006.gif', 'ga041007.gif', 'ga041008.gif',
'ga041009.gif', 'ga041010.gif', 'ga041011.gif', 'ga000619.gif',
'ga050101.gif', 'ga050102.gif', 'ga050103.gif', 'ga050104.gif',
'ga050105.gif', 'ga050106.gif', 'ga050107.gif', 'ga050108.gif',
'ga050109.gif', 'ga050111.gif', 'ga050112.gif', 'ga050113.gif',
'ga050801.gif', 'ga050114.gif', 'ga050802.gif', 'ga050115.gif',
'ga050803.gif', 'ga050116.gif', 'ga050804.gif', 'ga050117.gif',
'ga050805.gif', 'ga050118.gif', 'ga050806.gif', 'ga050119.gif',
'ga050807.gif', 'ga050121.gif', 'ga050122.gif', 'ga050123.gif',
'ga050124.gif', 'ga050125.gif',  'ga780825.gif', 'ga780826.gif',
'ga780827.gif', 'ga780828.gif', 'ga780829.gif', 'ga780830.gif']

When I use:
files = os.listdir()
x = 0
direction = 1
while True:
ShowImg(files[x]) # An image display function
x += -1

it doesn't display the images in the right order! It will skip from
ga050123.gif and go to ga050120.gif!

Is there any way to counter this?
Thanks,
Joe

PS> This is a console version of the Tkinter version I want to make
(where I posted earlier). Console is just way easier! :-)


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


Re: [Tutor] launching and monitor from make

2005-10-12 Thread Ertl, John
Ewald,

That easy...If you know.  Thanks for the example and the help.  Now lets see
if I can modify the make file without upsetting the make god.

Thanks again.

John Ertl 

 -Original Message-
From:   Ewald Ertl [mailto:[EMAIL PROTECTED] 
Sent:   Wednesday, October 12, 2005 7:40 AM
To: Ertl, John
Subject:Re: [Tutor] launching and monitor from make

Hi John

Ertl, John wrote:
> All,
> 
> I have a very simple python program that reads one file and overwrites
> anouther text file.  This workes great from the command line and it has
> error checking but just outputs messages and the like to the screen.
> 
> The CM team here would like to have the script run each week as part of
the
> automated rebuilds.  The suggestion was to make the execution of the
scritp
> part of the bigger programs make file.  How would I handel errors when the
> program is run from a makefile?  I hate make so I have not done a lot of
> playing with what make can do in this respect but can python get an error
> message to make?


I think your Python-Script is an executeable "Shell"-Script with
"#!/usr/bin/env python ... "
So you can just insert your script in the makefile

When the exit-Code of the script is 0, than make assumes that everything is
ok,
otherwise an error occured.


Here's a short example with "ls"

Here the mkfile want's to list the file "hugo" which does not exist

mkfile:
all:
ls -l hugo
--
#gmake -f mkfile
ls -l hugo
hugo: Datei oder Verzeichnis nicht gefunden
gmake: *** [all] Error 2



Here the mkfile itself is listed with ls
mkfile:
all:
ls -l mkfile
--
#gmake -f mkfile
ls -l mkfile
-rw-rw-r--   1 ewer entw  19 Okt 12 16:35 mkfile


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


Re: [Tutor] slide show won't display the images in right order

2005-10-12 Thread dataw0lf
Joseph Quigley wrote:

> When I use:
> files = os.listdir()
> x = 0
> direction = 1
> while True:
> ShowImg(files[x]) # An image display function
> x += -1
> 
> it doesn't display the images in the right order! It will skip from
> ga050123.gif and go to ga050120.gif!

Why not do something like this? :

files = os.listdir()
for file in files:
ShowImg(file)

I'm also curious as to what context the 'direction' variable was being
used in.

Cheers.

-- 

Joshua Simpson -- dataw0lf.org
Lead Network Administrator/Engineer Aero-Graphics Inc.
[EMAIL PROTECTED]
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Please look at my wordFrequency.py

2005-10-12 Thread Dick Moores
Alan Gauld wrote at 06:00 10/12/2005:
> > See Also:Mastering Regular Expressions Book on regular expressions by
> > Jeffrey Friedl, published by O'Reilly. The second edition of the book no
> > longer covers Python at all, but the first edition covered writing good
> > regular expression patterns in great detail. "
> >
> > So I'd like to ask if there is agreement about the value of this book,
>
>This is the bible of regular expressions, without peer.
>
> > and if the 1st is really better that the 2nd ed. for regex in Python.
>
>DEbateable, the second covers some more modern additions to the RE catalog
>but misses Python out. The first inclyudes Python but misses the
>atest/greatest.
>Either edition is invaluable, you only need one so if you have the first
>don't
>worry about getting the second IMHO.

Thanks for the advice.

> > further information and a gentler presentation, consult the Regular
> > Expression HOWTO, accessible from
> > http://www.python.org/doc/howto/.";
>
>If you want an even gentler intro - really an intro to the intro - look at
>the
>RE page on my tutorial. It covers the very basic concepts.

Looks great! http://www.freenetpages.co.uk/hp/alan.gauld

The regex HOWTO, above, mentions, in section 3.3, a tool that comes with 
Python 2.4 for practicing/testing regex. Tools/scripts/redemo.py . Can 
you recommend this? And how about Kodos? ()

Dick


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


Re: [Tutor] slide show won't display the images in right order

2005-10-12 Thread Kent Johnson
Joseph Quigley wrote:
> Hi,
> Here's my directory listing as a list:
> ['ga041001.gif', 'ga041002.gif', 'ga041003.gif', 'ga041004.gif',
> 'ga041005.gif', 'ga041006.gif', 'ga041007.gif', 'ga041008.gif',
> 'ga041009.gif', 'ga041010.gif', 'ga041011.gif', 'ga000619.gif',
> 'ga050101.gif', 'ga050102.gif', 'ga050103.gif', 'ga050104.gif',
> 'ga050105.gif', 'ga050106.gif', 'ga050107.gif', 'ga050108.gif',
> 'ga050109.gif', 'ga050111.gif', 'ga050112.gif', 'ga050113.gif',
> 'ga050801.gif', 'ga050114.gif', 'ga050802.gif', 'ga050115.gif',
> 'ga050803.gif', 'ga050116.gif', 'ga050804.gif', 'ga050117.gif',
> 'ga050805.gif', 'ga050118.gif', 'ga050806.gif', 'ga050119.gif',
> 'ga050807.gif', 'ga050121.gif', 'ga050122.gif', 'ga050123.gif',
> 'ga050124.gif', 'ga050125.gif',  'ga780825.gif', 'ga780826.gif',
> 'ga780827.gif', 'ga780828.gif', 'ga780829.gif', 'ga780830.gif']
> 
> When I use:
> files = os.listdir()
> x = 0
> direction = 1
> while True:
> ShowImg(files[x]) # An image display function
> x += -1
> 
> it doesn't display the images in the right order! It will skip from
> ga050123.gif and go to ga050120.gif!
> 
> Is there any way to counter this?

os.listdir() doesn't guarantee any particular order for the results. How about 
sorting the list?
files = os.listdir(...)
files.sort()
...etc

Kent

> Thanks,
> Joe
> 
> PS> This is a console version of the Tkinter version I want to make
> (where I posted earlier). Console is just way easier! :-)
> 
> 
> ___
> 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] Please look at my wordFrequency.py

2005-10-12 Thread Kent Johnson
Dick Moores wrote:
> The regex HOWTO, above, mentions, in section 3.3, a tool that comes with 
> Python 2.4 for practicing/testing regex. Tools/scripts/redemo.py . Can 
> you recommend this? And how about Kodos? ()

Yes, it's very handy. Try it! Kodos is similar. I think there are versions for 
just about every Python GUI toolkit but they all have more or less the same 
features. Kodos requires PyQT, redemo.py uses Tkinter.

Kent

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


[Tutor] Problem with Winpdb.

2005-10-12 Thread Steve Robb
Can anyone point me to what the problem may be with running Winpdb?

I am running Win98, unfortunately, Mozilla/5.0 (Windows; U; Win98; 
en-US; rv:1.7.12) Gecko/20050915 Firefox/1.0.7.
I have python 2.4, Python 2.4 (#60, Nov 30 2004, 09:34:21) [MSC v.1310 
32 bit (Intel)] on win32.
I have Winpdb, WINPDB_VERSION = "WINPDB_1_0_4".
I have Crypto, print Crypto.__version__
2.0.1.
I have wxPython, print wxPython.__version__
2.6.1.0.
I have thread, Directory of 
C:\Python24\Lib\site-packages\pythonwin\pywin\mfc

THREAD   PY541  09-14-05 11:53a thread.py
THREAD   PYC 1,140  09-14-05 11:53a thread.pyc
THREAD   PYO 1,140  09-14-05 11:53a thread.pyo

I seem to be able to execute _rpdb2.py in a MS-DOS command window by 
using the following,
..\python _rpdb2.py, while in the \python24\scripts directory.

However, when I try,
..\python _winpdb.py, from the same directory, I get the following 
traceback.

Traceback (most recent call last):
  File 
"C:\Python24\Lib\site-packages\pythonwin\pywin\framework\scriptutils.py", 
line 310, in RunScript
exec codeObject in __main__.__dict__
  File "C:\Python24\Scripts\_winpdb.py", line 31, in ?
winpdb.main()
  File "C:\Python24\Lib\site-packages\winpdb.py", line 3269, in main
return rpdb2.main(StartClient)
  File "C:\Python24\Lib\site-packages\rpdb2.py", line 6844, in main
StartClient_func('', fAttach, fchdir, pwd, fAllowUnencrypted, 
fRemote, host)
  File "C:\Python24\Lib\site-packages\winpdb.py", line 3259, in StartClient
app = CWinpdbApp(sm, fchdir, command_line, fAttach, fAllowUnencrypted)
  File "C:\Python24\Lib\site-packages\winpdb.py", line 1526, in __init__
wx.App.__init__(self, redirect = False)
  File "C:\Python24\Lib\site-packages\wx-2.6-msw-ansi\wx\_core.py", line 
7473, in __init__
self._BootstrapApp()
  File "C:\Python24\Lib\site-packages\wx-2.6-msw-ansi\wx\_core.py", line 
7125, in _BootstrapApp
return _core_.PyApp__BootstrapApp(*args, **kwargs)
  File "C:\Python24\Lib\site-packages\winpdb.py", line 1531, in OnInit
self.m_settings.load_settings()
  File "C:\Python24\Lib\site-packages\winpdb.py", line 632, in load_settings
path = self.calc_path()
  File "C:\Python24\Lib\site-packages\winpdb.py", line 607, in calc_path
app_data = os.environ['APPDATA']
  File "C:\PYTHON24\lib\os.py", line 422, in __getitem__
return self.data[key.upper()]
KeyError: 'APPDATA'

Thanks in advance for any help,
Steve
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] launching and monitor from make

2005-10-12 Thread Alan Gauld
> The CM team here would like to have the script run each week as part of 
> the
> automated rebuilds.  The suggestion was to make the execution of the 
> scritp
> part of the bigger programs make file.  How would I handel errors when the
> program is run from a makefile?  I hate make so I have not done a lot of
> playing with what make can do in this respect but can python get an error
> message to make?

You can write the errors to stderr instead of stdout.
You can also exit with an error code using sys.exit().
make should detect the non standard error code because it uses standard
shell processing..

Alan G 

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


Re: [Tutor] slide show won't display the images in right order

2005-10-12 Thread Alan Gauld
> When I use:
> files = os.listdir()

Have you tried printing 'files'?
Are they in order there?

> it doesn't display the images in the right order! It will skip from
> ga050123.gif and go to ga050120.gif!

I suspect listdir doesn't use name order.
You probably need to do a sort() on the files list.

hth,

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



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


Re: [Tutor] launching and monitor from make

2005-10-12 Thread Ertl, John
Alan,

Thanks,  just as you and Ewald said it works great.  Almost too
easy...I have this feeling something will turn around a bite me once it goes
to ops.

Thanks again

John Ertl 

 -Original Message-
From:   Alan Gauld [mailto:[EMAIL PROTECTED] 
Sent:   Wednesday, October 12, 2005 10:01 AM
To: Ertl, John; tutor@python.org
Subject:Re: [Tutor] launching and monitor from make

> The CM team here would like to have the script run each week as part of 
> the
> automated rebuilds.  The suggestion was to make the execution of the 
> scritp
> part of the bigger programs make file.  How would I handel errors when the
> program is run from a makefile?  I hate make so I have not done a lot of
> playing with what make can do in this respect but can python get an error
> message to make?

You can write the errors to stderr instead of stdout.
You can also exit with an error code using sys.exit().
make should detect the non standard error code because it uses standard
shell processing..

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


Re: [Tutor] Please look at my wordFrequency.py

2005-10-12 Thread Alan Gauld
> The regex HOWTO, above, mentions, in section 3.3, a tool that comes with 
> Python 2.4 for practicing/testing regex. Tools/scripts/redemo.py . Can you 
> recommend this? And how about Kodos? ()

Yes I can recommend it, its a great tool for trying REs out

Alan G. 

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


[Tutor] (no subject)

2005-10-12 Thread Jeff Peery
hello, I am trying to find the source code for FFT. I found the FFT.py module athough I cannot find the actual code that runs the FFT. Is it in a compiled form or can I view its source?
 
thanks.___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] (no subject)

2005-10-12 Thread Kent Johnson
Jeff Peery wrote:
> hello, I am trying to find the source code for FFT. I found the FFT.py 
> module athough I cannot find the actual code that runs the FFT. Is it in 
> a compiled form or can I view its source?

Which FFT (where did you get fft.py)? you asked this same question in August, 
here is what I said then:
http://aspn.activestate.com/ASPN/Mail/Message/2765231

Kent

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


Re: [Tutor] Please look at my wordFrequency.py

2005-10-12 Thread Dick Moores
Alan Gauld wrote at 10:04 10/12/2005:
> > The regex HOWTO, above, mentions, in section 3.3, a tool that comes with
> > Python 2.4 for practicing/testing regex. Tools/scripts/redemo.py . 
> Can you
> > recommend this? And how about Kodos? ()
>
>Yes I can recommend it, its a great tool for trying REs out
>
>Alan G.

Sorry, I assume you mean redemo.py? Do you know Kodos?

Thanks,

Dick


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


[Tutor] how to extract number of files from directory

2005-10-12 Thread Marc Buehler
hi.

i'm new to Python ...

i would like to extract the number of JPG files
from the current directory and use that number
as a parameter in my python script.
i tried:
 a = os.system('ls *JPG | wc -l')
when i do:
 print a
i get '0'.

what am i missing?

marc

---
The apocalyptic vision of a criminally insane charismatic cult leader 

   http://www.marcbuehler.net




__ 
Yahoo! Music Unlimited 
Access over 1 million songs. Try it free.
http://music.yahoo.com/unlimited/
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


[Tutor] Can anyone teach me...?

2005-10-12 Thread Nathan Pinno
Hey all,
When I said that I might go to Visual Basic in an earlier message, someone replied by saying that I should ask here how to do it in Python. Well, I'm asking now: Can anyone teach me how to make a simple program that uses a GUI?

Thanks,
Nathan Pinno Crew, McDonalds Restaurant and fan extraordinare of the Oilers. 

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


Re: [Tutor] how to extract number of files from directory

2005-10-12 Thread Kent Johnson
Marc Buehler wrote:
> i would like to extract the number of JPG files
> from the current directory and use that number
> as a parameter in my python script.
> i tried:
>  a = os.system('ls *JPG | wc -l')
> when i do:
>  print a
> i get '0'.
> 
> what am i missing?

os.system() returns the exit code of the program, not the output. You can run 
an external program and capture the output, but generally you should look for a 
solution within Python before resorting to external programs. In this case, try

import glob
a = len(glob.glob('*.JPG'))

Kent

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


Re: [Tutor] how to extract number of files from directory

2005-10-12 Thread Ertl, John
Marc,

You can do most system type stuff in Python and it makes it much easier.  
import glob
jpgList = glob.glob("*.jpg") # the glob allows you to use wild cards in the
search
jpgCount = len(jpgList)
This gives you a list of all files that end in .jpg.  You can then do a
len(jpgList) to get the number of files
John


 -Original Message-
From:   Marc Buehler [mailto:[EMAIL PROTECTED] 
Sent:   Wednesday, October 12, 2005 11:10 AM
To: tutor@python.org
Subject:[Tutor] how to extract number of files from directory

hi.

i'm new to Python ...

i would like to extract the number of JPG files
from the current directory and use that number
as a parameter in my python script.
i tried:
 a = os.system('ls *JPG | wc -l')
when i do:
 print a
i get '0'.

what am i missing?

marc


---
The apocalyptic vision of a criminally insane charismatic cult leader 

   http://www.marcbuehler.net





__ 
Yahoo! Music Unlimited 
Access over 1 million songs. Try it free.
http://music.yahoo.com/unlimited/
___
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 extract number of files from directory

2005-10-12 Thread Eric Walker
I think os.system returns status of commands

Try this:
w = os.popen('ls -l | wc -l').readlines()

Python Newbie...

On Wednesday 12 October 2005 12:10 pm, Marc Buehler wrote:
> hi.
>
> i'm new to Python ...
>
> i would like to extract the number of JPG files
> from the current directory and use that number
> as a parameter in my python script.
> i tried:
>  a = os.system('ls *JPG | wc -l')
> when i do:
>  print a
> i get '0'.
>
> what am i missing?
>
> marc
>
> ---
> The apocalyptic vision of a criminally insane charismatic cult
> leader
>
>http://www.marcbuehler.net
> ---
>-
>
>
>
> __
> Yahoo! Music Unlimited
> Access over 1 million songs. Try it free.
> http://music.yahoo.com/unlimited/
> ___
> 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] comiling python to microchip?

2005-10-12 Thread Jeff Peery
is it possible to take python code and compile it for use in a microprocessor?___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] how to extract number of files from directory

2005-10-12 Thread w chun
On 10/12/05, Ertl, John <[EMAIL PROTECTED]> wrote:
You can do most system type stuff in Python and it makes it much easier.import globjpgList = glob.glob("*.jpg") # the glob allows you to use wild cards in thesearchjpgCount = len(jpgList)This gives you a list of all files that end in .jpg.  You can then do a
len(jpgList) to get the number of filesJohnglob seems to be a good idea, but is there a way to do it case-insensitively, i.e., .jpg and .JPG?it may be slower than glob, but i'm finding myself thinking more like...
len([i for i in os.listdir('tmp') if i.lower().endswith('.jpg')])now, anyone with a solution for .jpg, .JPG, .jpeg, *and* .JPEG?-- wesley- - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
"Core Python Programming", Prentice Hall, (c)2006,2001http://corepython.comwesley.j.chun :: wescpy-at-gmail.com
cyberweb.consulting : silicon valley, cahttp://cyberwebconsulting.com
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] how to extract number of files from directory

2005-10-12 Thread Kent Johnson
w chun wrote:
> glob seems to be a good idea, but is there a way to do it 
> case-insensitively, i.e., .jpg and .JPG?
> 
> it may be slower than glob, but i'm finding myself thinking more like...
> 
> len([i for i in os.listdir('tmp') if i.lower().endswith('.jpg')])
> 
> now, anyone with a solution for .jpg, .JPG, .jpeg, *and* .JPEG?

glob.glob('*.[jJ][pP][eE]?[gG]') ??

or maybe (not sure this is legal; I'm pretty sure it's ugly :-)

len([i for i in os.listdir('tmp') for suffix in ['.jpg', '.jpeg'] if 
i.lower().endswith(suffix)])

Kent

> 
> -- wesley
> - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
> "Core Python Programming", Prentice Hall, (c)2006,2001
> http://corepython.com
> 
> wesley.j.chun :: wescpy-at-gmail.com 
> cyberweb.consulting : silicon valley, ca
> http://cyberwebconsulting.com
> 
> 
> 
> 
> ___
> Tutor maillist  -  Tutor@python.org
> http://mail.python.org/mailman/listinfo/tutor

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


Re: [Tutor] how to extract number of files from directory

2005-10-12 Thread Alan Gauld
> a = os.system('ls *JPG | wc -l')
> when i do:
> print a
> i get '0'.

os.system only returns the exitcode of the command not the output.

Look at the new Subprocess  module and its Popen class. It can 
look intimidating at first but read the examples and you should find 
it easy enough.

Altrernatively try the commands module although subprocess is 
intended to replace it...

However you might find it easier to use the python os tools like listdir 
to do the job directly. 

a = len(os.listdir(mydir, '*JPG')

Should be about right...

HTH,

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


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


Re: [Tutor] Can anyone teach me...?

2005-10-12 Thread Alan Gauld
Hello again Nathan,

> When I said that I might go to Visual Basic in an earlier message, 
> someone replied by saying that I should ask here how to do it in Python. 
> Well, I'm asking now: Can anyone teach me how to make a simple 
> program that uses a GUI?

Take a look at the GUI section of my tutorial and then look at the case 
study topic which turns a command-line program into a GUI. Then go 
and read the specialist TKinter or wxPython documentation and tutorials.

You might also like projects like PythonCard which provide basic 
graphical GUI building tools.

However I have to be honest and say that building GUIs in VB 
(or Borland's Delphi/Kylix) is a lot easier than in Python IMHO. 
I use Delphi for most of my real-world heavy duty GUI work.

HTH,

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


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


Re: [Tutor] how to extract number of files from directory

2005-10-12 Thread Kent Johnson
Alan Gauld wrote:
> a = len(os.listdir(mydir, '*JPG')
> 
> Should be about right...

No, os.listdir() only takes one argument, the directory. See the solutions with 
glob.glob().

Kent

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


Re: [Tutor] slide show won't display the images in right order

2005-10-12 Thread Murtog
The correct code looks like this:

files = os.listdir()
files.sort()x = 0direction = 1while True:     ShowImg(files[x]) # An image display function     x += -1-- Cheers, Murtog
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


[Tutor] __slots__

2005-10-12 Thread Liam Clarke
If you've been wondering about how useful this is...

Well, all I can say is, don't use them unless you need them, they're a
lot of hassle.
I created two versions of the same object, same attribute values,  one
with slots, one without.

With slots, it pickled to a string of 163 characters. Without,
341.That's 178 bytes saved, rough approximation. So, if I create 5000
objects, I've saved somewhere around 900Kb of RAM.

At 500,000 objects I'd be saving roughly 86Mb of RAM, assuming that a
pickled object corresponds to one in memory. Or, 86Mb of HD space.

Of course, having to learn what __getstate__ was in order to pickle
the slotted instance wasn't much fun. Remember, premature optimisation
is bad...

This was a smaller object, so it'll be interesting to see what economy
an object with 70ish attributes makes. FWIW, I'm doing the exact
opposite of XP, and building to handle 300,000 to 600,000 objects...

Regards,

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


Re: [Tutor] comiling python to microchip?

2005-10-12 Thread Alan Gauld
From: "Jeff Peery" <[EMAIL PROTECTED]>
> is it possible to take python code and compile it for use in a 
> microprocessor?
>

In the same way that Sun have been threatening to build a chip that has Java
as its machine code then it would be possible to build a chip that ran 
Python
bytecode but it would be a huge investment.

Its certainly not possible woth current Python to generate code that can run
outside of an operating system on a microchip. ON the otherhand if that
chop is running even a basic operating system that supports a C compiler
then it should be possible to build the Python interpreter for the chip/OS
combo and use python that way.

In other words, no, you can't currently get a completely standalone
machine code program from Python code at present. It would be
feasible to port Python to most micro chip OSs such as CP/M,
PSOS, QXL(?) etc and its possible that some such port has already
been done but I don't know of it.

HTH,

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


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


Re: [Tutor] how to extract number of files from directory

2005-10-12 Thread Alan Gauld
> glob seems to be a good idea, ...

Yep, I got my glob and listdir mixed up earlier...

> it may be slower than glob, but i'm finding myself thinking more like...
> len([i for i in os.listdir('tmp') if i.lower().endswith('.jpg')])
>
> now, anyone with a solution for .jpg, .JPG, .jpeg, *and* .JPEG?

Sounds like a job for a regex?

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


Re: [Tutor] how to extract number of files from directory

2005-10-12 Thread Alan Gauld
>> a = len(os.listdir(mydir, '*JPG')
>>
>> Should be about right...
>
> No, os.listdir() only takes one argument, the directory. See the solutions 
> with glob.glob().

Oops! Quite right, my mistake. (As is the missing right paren!)

Alan g. 

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


[Tutor] Struct headspinner

2005-10-12 Thread Liam Clarke
Hi all,

Erm, can someone please aid me? I'm using Windows XP, haven't tested
this code on Linux yet, but, well watch this...

'<' indicates little-endian, @ indicates native. i is an integer, q is a long.

>>> struct.calcsize('<3i')
12
>>> struct.calcsize('@3i')
12
>>> struct.calcsize('<3iq')
20
>>> struct.calcsize('@3iq')
24
>>> struct.calcsize('@4iq')
24

Is this a feature I don't understand? Is a long preceded by 3 integers
really 12 bytes long?
Surely Microsoft wouldn't do that?

Regards,

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


Re: [Tutor] __slots__

2005-10-12 Thread Alan Gauld
> an object with 70ish attributes makes. FWIW, I'm doing the exact
> opposite of XP, and building to handle 300,000 to 600,000 objects...

As a matter of interest why do you need so many in RAM at once?
Its very unusual to require that many and there are techniques to 
minimise the space by using 'phantom' objects and only pulling in 
the data as needed (so called 'late evaluation').

If you tell us more about the problem context we might be able to 
help make the solution simpler.

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


Re: [Tutor] Can anyone teach me...?

2005-10-12 Thread luke p
nathan,
there are a few GUI options available to you.
the one that is bundled with the python installation is TKInter.  that's an "i" not an "L".
you can also use the python interface for WxWindows, I forget what it's called.
anyway,
TKInter looks fairly nice, some say it doesn't have the windows "feel" to it as much as WxWindows
but who really cares because it's pretty easy to learn etc.
There's a tutorial somewher eon the web written by some New Mexico thing I'm not sure
but I have to run to class so I can't look it up.
anyway,
Give google a workout and learn as much as you can on your own.
I don't think you should ask any questions that you could solve yourself,
or you may aggravate the list.
so try as much as you can and then if you get stuck ask someone.
Go for TKInter I think you'll like it.
Alan Gauld might have a tutorial for TKInter, you could check that out too.
good luck. 
On 10/12/05, Nathan Pinno <[EMAIL PROTECTED]> wrote:


Hey all,
When I said that I might go to Visual Basic in an earlier message, someone replied by saying that I should ask here how to do it in Python. Well, I'm asking now: Can anyone teach me how to make a simple program that uses a GUI?


Thanks,
Nathan Pinno Crew, McDonalds Restaurant and fan extraordinare of the Oilers. ___Tutor maillist  -  
Tutor@python.orghttp://mail.python.org/mailman/listinfo/tutor

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


Re: [Tutor] Struct headspinner

2005-10-12 Thread Tim Peters
[Liam Clarke]
> Erm, can someone please aid me? I'm using Windows XP, haven't tested
> this code on Linux yet, but, well watch this...
>
> '<' indicates little-endian, @ indicates native. i is an integer,

Yes x 3.

> q is a long.

No.  q in native mode is C "long long" on Linux, or "_int64" on
Windows.  It's an 8-byte integer in standard modes..  "i" is a 4-byte
integer in standard modes, same as C "int" in native modes.

"l" is the format code for long, but none of your examples use that.

> >>> struct.calcsize('<3i')
> 12

3 std integers x 4 bytes each = 12.

> >>> struct.calcsize('@3i')
> 12

Same thing.

> >>> struct.calcsize('<3iq')
> 20

12 + 1 8-byte standard int = 20.

> >>> struct.calcsize('@3iq')
> 24

_int64 is 8-byte aligned on Windows, so that's 12 bytes + 4 bytes
padding + 8 bytes = 24 bytes.

> >>> struct.calcsize('@4iq')
> 24

Same thing; the additional native i fills the padding in the preceding example.

> Is this a feature I don't understand?

Sorry, I don't know what you're asking.  What specifically surprises you here?

> Is a long preceded by 3 integers really 12 bytes long?

No (and nothing above suggests that it is, either ...).

> Surely Microsoft wouldn't do that?

And they didn't ;-)
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] comiling python to microchip?

2005-10-12 Thread Adam
On 12/10/05, Jeff Peery <[EMAIL PROTECTED]> wrote:
is it possible to take python code and compile it for use in a microprocessor?
___Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor

Building on what Alan said this may be of some use uClinux it's an embedded linux operating system for microcontrollers.

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


Re: [Tutor] comiling python to microchip?

2005-10-12 Thread Kent Johnson
Jeff Peery wrote:
> is it possible to take python code and compile it for use in a 
> microprocessor?

I don't think you can compile python code but python itself has been ported to 
many tiny architectures. Take a look at 
http://www.python.org/download/download_other.html if you don't see what you 
want there then ask on comp.lang.python and be specific about your target 
environment.

Kent

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


Re: [Tutor] __slots__

2005-10-12 Thread Kent Johnson
Liam Clarke wrote:
> Well, all I can say is, don't use them unless you need them, they're a
> lot of hassle.

I think the conventional advice is something like, if you don't understand why 
using slots is a bad idea, you have no business using them :-)

> I created two versions of the same object, same attribute values,  one
> with slots, one without.
> 
> With slots, it pickled to a string of 163 characters. Without,
> 341.That's 178 bytes saved, rough approximation. So, if I create 5000
> objects, I've saved somewhere around 900Kb of RAM.

Does the size of the pickle correlate to the size of the runtime object? I 
don't know...

Kent

> 
> At 500,000 objects I'd be saving roughly 86Mb of RAM, assuming that a
> pickled object corresponds to one in memory. Or, 86Mb of HD space.
> 
> Of course, having to learn what __getstate__ was in order to pickle
> the slotted instance wasn't much fun. Remember, premature optimisation
> is bad...
> 
> This was a smaller object, so it'll be interesting to see what economy
> an object with 70ish attributes makes. FWIW, I'm doing the exact
> opposite of XP, and building to handle 300,000 to 600,000 objects...
> 
> Regards,
> 
> Liam Clarke
> ___
> 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] Struct headspinner

2005-10-12 Thread Danny Yoo


On Wed, 12 Oct 2005, Liam Clarke wrote:

> Erm, can someone please aid me? I'm using Windows XP, haven't tested
> this code on Linux yet, but, well watch this...
>
> '<' indicates little-endian, @ indicates native. i is an integer, q is a long.
>
> >>> struct.calcsize('<3i')
> 12
> >>> struct.calcsize('@3i')
> 12
> >>> struct.calcsize('<3iq')
> 20
> >>> struct.calcsize('@3iq')
> 24
> >>> struct.calcsize('@4iq')
> 24
>
> Is this a feature I don't understand? Is a long preceded by 3 integers
> really 12 bytes long? Surely Microsoft wouldn't do that?


Hi Liam,

What you're seeing really has little to do with Microsoft: it has to do
with the "alignment" of data structures against your computer's hardware
and the underlying C compiler for your system.


Most computer architectures will try to make sure that integers and words
in memory are always "aligned" on some predefined boundary.  It's easier
on the decoder hardware if it know exactly where those primitive data
types can start.

This is mentioned in the comment in the 'struct' documentation:

"""By default, C numbers are represented in the machine's native format
and byte order, and properly aligned by skipping pad bytes if necessary
(according to the rules used by the C compiler)."""

If the bytes that represent an integer are out of frame, then the hardware
might be slower about decoding the values in a good case, and might just
not be able to decode them at all in a bad case.  *grin*


'struct' will introduce "padding" bytes to make sure things are framed up
nicely.  And we can see this padding in action: a single integer takes up
four bytes:

##
>>> struct.calcsize("i")
4
##

but if we preceed that with a single byte character:

##
>>> struct.calcsize("ci")
8
##

rather than see that this takes five bytes, we see that it takes eight!
The 'struct' module has transparently added three "padding" bytes to make
sure the integer aligns in a way that's compatible with the underlying
computer hardware.


Order matters.  Here's another example of an integer followed by a
character:

##
>>> struct.calcsize("ic")
5
##

Nothing follows that character, so no padding is necessary.  But again, if
we put an integer after that, then we see padding in action:

###
>>> struct.calcsize("ici")
12
>>> struct.calcsize("icci")
12
>>> struct.calcsize("iccci")
12
>>> struct.calcsize("ii")
12
###

All of this is meant to guarantee that the second integer is on a memory
location that can be efficiently destructured by the computer hardware.


Does this make sense?  Please feel free to ask more questions on this;
it's a bit of a low-level topic.

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


Re: [Tutor] Can anyone teach me...?

2005-10-12 Thread Terry Carroll
On Wed, 12 Oct 2005, Alan Gauld wrote:

> However I have to be honest and say that building GUIs in VB ... is a
> lot easier than in Python IMHO.

I have to say, I agree with that.  I just started using VB a month ago 
(for a course I'm taking, all projects need to be in VB), and it's really 
a pleasure setting up a GUI.

But, man, VB as a language and library set is horrible; and pointlessly
horrible, in many cases.

(I remain astonished by VB's lack of min/max functions, not to mention the
lack of inverse cosine and inverse sine functions.  I've only been using
it for a month, but I'll bet I could find more if I used it more often.  
VB's slogan should be "Batteries Excluded"!)

The best of both worlds, to me, would be the ability to use a VB-like GUI
and the Python language and libraries.  You would be able to do some
pretty powerful work in just minutes.  Right now, I find creating a 
Tkinter app to be pretty tedious work.

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


Re: [Tutor] Can anyone teach me...?

2005-10-12 Thread w chun
On 10/12/05, Terry Carroll <[EMAIL PROTECTED]> wrote:
On Wed, 12 Oct 2005, Alan Gauld wrote:> However I have to be honest and say that building GUIs in VB ... is a> lot easier than in Python IMHO.But, man, VB as a language and library set is horrible; and pointlessly
horrible, in many cases.The best of both worlds, to me, would be the ability to use a VB-like GUIand the Python language and libraries.  You would be able to do somepretty powerful work in just minutes.  Right now, I find creating a
Tkinter app to be pretty tedious work.i haven't personally, but i heard that using Glade with GTK+ and Boa with wxWindows really simplify GUI development.  anyone with experience here?
--wesley - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -"Core Python Programming", Prentice Hall, (c)2006,2001http://corepython.comwesley.j.chun
 :: wescpy-at-gmail.comcyberweb.consulting : silicon valley, cahttp://cyberwebconsulting.com
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] comiling python to microchip?

2005-10-12 Thread Michael Sparks
On Wednesday 12 October 2005 23:19, Kent Johnson wrote:
> I don't think you can compile python code

Pypy can compile a restricted subset of python... 

More accurately it translates the restricted subset to C and then *that* can 
be compiled. Some code can get quite dramatic speed improvements - though 
interpreted pypy itself (or translated) is still *a lot* slower than regular 
C-Python).

Pypy isn't ready (last time I looked) for general consumption yet, and 
certainly not for beginners :)


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


Re: [Tutor] Can anyone teach me...?

2005-10-12 Thread Ismael Garrido
Alan Gauld wrote:

>However I have to be honest and say that building GUIs in VB 
>(or Borland's Delphi/Kylix) is a lot easier than in Python IMHO. 
>I use Delphi for most of my real-world heavy duty GUI work.
>  
>
Have you tried Boa Constructor? It is quite similar to Delphi. It builds 
wxPython.

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


Re: [Tutor] how to extract number of files from directory

2005-10-12 Thread Michael Sparks
On Wednesday 12 October 2005 19:10, Marc Buehler wrote:
> i would like to extract the number of JPG files
> from the current directory and use that number

I've looked through the thread, and the following strikes me as simpler than 
the suggestions so far.

path = "." # Current directory, unix at least.
extns = ["jpg", "jpeg"]  # add in any others you like :-)
piccies = [ f for f in os.listdir(path) if f.split(".")[-1].lower() in extns ]
num_files = len(piccies)


What does this do?

os.listdir(path) -- gives you a list of files in that directory.
The condition:
 f.split(".")[-1].lower() in extns

Selects which filenames get put in the resulting list files.

This expression says "split the filename on dots '.' ", take the last thing
after the last dot, and make it lower. Finally check to see if that extension
is in the list of required extensions. 


I suppose if you prefer a more verbose version of the same thing:

path = "." # Current directory, unix at least.
extns = ["jpg", "jpeg"]  # add in any others you like :-)
piccies = []
for filename in os.listdir(path):
extension = f.split(".")[-1]
if extension.lower() in extns:
piccies.append(filename)

num_files = len(piccies)

Regards,


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


Re: [Tutor] comiling python to microchip?

2005-10-12 Thread Kent Johnson
Michael Sparks wrote:
> On Wednesday 12 October 2005 23:19, Kent Johnson wrote:
> 
>>I don't think you can compile python code
> 
> 
> Pypy can compile a restricted subset of python... 

There is also Shed Skin - "an experimental Python-to-C++ compiler. It can 
convert many Python programs into optimized C++ code"
http://shed-skin.blogspot.com/

Kent

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


Re: [Tutor] comiling python to microchip?

2005-10-12 Thread R. Alan Monroe
> From: "Jeff Peery" <[EMAIL PROTECTED]>
>> is it possible to take python code and compile it for use in a 
>> microprocessor?
>>

> In the same way that Sun have been threatening to build a chip that has Java
> as its machine code then it would be possible to build a chip that ran 
> Python
> bytecode but it would be a huge investment.

> Its certainly not possible woth current Python to generate code that can run
> outside of an operating system on a microchip. ON the otherhand if that
> chop is running even a basic operating system that supports a C compiler
> then it should be possible to build the Python interpreter for the chip/OS
> combo and use python that way.

> In other words, no, you can't currently get a completely standalone
> machine code program from Python code at present. It would be
> feasible to port Python to most micro chip OSs such as CP/M,
> PSOS, QXL(?) etc and its possible that some such port has already
> been done but I don't know of it.

Something I noticed while playing with the dis module is that the
python virtual machine appeared to use the same opcode for adding both
integers and strings, for instance. It would be tricky, or slow, to
translate this into other languages, I bet.

Alan

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


[Tutor] help with elif statements

2005-10-12 Thread andrade1
hello

below is my code and everytime I input a value of 16 or more it keeps
returning sophomore. could anyone help me figure out what to change so
that it won't return sophmore for things greater than or equal to 16?

def getcredits(num):
if num < 7:
return 'Freshman'
elif num >= 7:
return 'Sophomore'
elif num <16:
return 'Sophomore'
elif num >= 16:
return 'Junior'
elif num < 26:
return 'Junior'
else:
return 'Senior'

def main():
g = input('Enter number of credits:')
print 'Your standing is %s' % (getcredits(int(g)))

main()

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


Re: [Tutor] help with elif statements

2005-10-12 Thread John Fouhy
On 13/10/05, [EMAIL PROTECTED] <[EMAIL PROTECTED]> wrote:
> below is my code and everytime I input a value of 16 or more it keeps
> returning sophomore. could anyone help me figure out what to change so
> that it won't return sophmore for things greater than or equal to 16?

Which is the first branch of your IF statement to be true if you enter, say 18?

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


Re: [Tutor] help with elif statements

2005-10-12 Thread Hans Dushanthakumar
U might wanna change the code to something along the lines of

 def getcredits(num):
if num < 7:
return 'Freshman'
elif num >= 7 and num <16
return 'Sophomore'
elif num >= 16 and num < 26:
return 'Junior'

Etc...

Or even 

 def getcredits(num):
if num < 7:
return 'Freshman'
elif num <16
return 'Sophomore'
elif num < 26:
return 'Junior' 
...



-Original Message-
From: [EMAIL PROTECTED] [mailto:[EMAIL PROTECTED] On
Behalf Of [EMAIL PROTECTED]
Sent: Thursday, 13 October 2005 2:05 p.m.
To: tutor@python.org
Subject: [Tutor] help with elif statements

hello

below is my code and everytime I input a value of 16 or more it keeps
returning sophomore. could anyone help me figure out what to change so
that it won't return sophmore for things greater than or equal to 16?

def getcredits(num):
if num < 7:
return 'Freshman'
elif num >= 7:
return 'Sophomore'
elif num <16:
return 'Sophomore'
elif num >= 16:
return 'Junior'
elif num < 26:
return 'Junior'
else:
return 'Senior'

def main():
g = input('Enter number of credits:')
print 'Your standing is %s' % (getcredits(int(g)))

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


Re: [Tutor] help with elif statements

2005-10-12 Thread Danny Yoo


On Wed, 12 Oct 2005 [EMAIL PROTECTED] wrote:

> below is my code and everytime I input a value of 16 or more it keeps
> returning sophomore. could anyone help me figure out what to change so
> that it won't return sophmore for things greater than or equal to 16?

If we have the following:

### Pseudocode
if A:   [bodyA]
elif B: [bodyB]
elif C: [bodyC]
else:   [bodyD]
###

only of those bodies will be entered, regardless if more than one
condition is true.  That is, we'll enter the body of the first condition
that ends up true, and ignore the rest.  We call this an "exclusive"
branching point for this reason.

If it helps, the above pseudocode could also be rewritten as the following
(although it would not be idiomatic):

if A:
[bodyA]
else:
if B:
[bodyB]
else:
if C:
[bodyC]
else:
[bodyD]

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


Re: [Tutor] launching and monitor from make

2005-10-12 Thread Michael P. Reilly
On 10/12/05, Ertl, John <[EMAIL PROTECTED]> wrote:
I have a very simple python program that reads one file and overwritesanouther text file.  This workes great from the command line and it haserror checking but just outputs messages and the like to the screen.
The CM team here would like to have the script run each week as part of theautomated rebuilds.  The suggestion was to make the execution of the scritppart of the bigger programs make file.  How would I handel errors when the
program is run from a makefile?  I hate make so I have not done a lot ofplaying with what make can do in this respect but can python get an errormessage to make?
In general, you want error messages to go to sys.stderr and you want to return an error code other than zero.

Send messages to stderr:
  print >>sys.stderr, message

Return error code
  raise SystemExit(1)

At this point, make will respond to errors correctly.  You can use
make's -k option to ignore them, but the error messages still go the
same place as other programs.

HTH,
  -Arcege-- There's so many different worlds,So many different suns.And we have just one world,But we live in different ones.
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] help with elif statements

2005-10-12 Thread bob
At 06:04 PM 10/12/2005, [EMAIL PROTECTED] wrote:
>hello
>
>below is my code and everytime I input a value of 16 or more it keeps
>returning sophomore. could anyone help me figure out what to change so
>that it won't return sophmore for things greater than or equal to 16?
>
>def getcredits(num):
> if num < 7:
> return 'Freshman'
> elif num >= 7:
> return 'Sophomore'
> elif num <16:
> return 'Sophomore'
> elif num >= 16:
> return 'Junior'
> elif num < 26:
> return 'Junior'
> else:
> return 'Senior'

I have a preference for separating data and control structure. So I would:

def getcredits(num):
 rank = ((7, 'Freshman'), (16, 'Sophomore'), (26, 'Junior'), (999, 
'Senior')
 for limit, class in rank:
 if num < limit:
 return class

>def main():
> g = input('Enter number of credits:')
> print 'Your standing is %s' % (getcredits(int(g)))
>
>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


Re: [Tutor] Can anyone teach me...?

2005-10-12 Thread Raduz
On Thursday 13 of October 2005 00:27, Terry Carroll wrote:
>
> The best of both worlds, to me, would be the ability to use a VB-like GUI
> and the Python language and libraries.  You would be able to do some
> pretty powerful work in just minutes.  Right now, I find creating a
> Tkinter app to be pretty tedious work.
>

Try the Qt then. It works under Linux and Windows, has Python bindings via 
PyQt, it's signal/slot system is really great, and designing the GUI with Qt 
Designer is a breeze.

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


[Tutor] how to speed up this code?

2005-10-12 Thread Pujo Aji
I have code like this: 
def f(x,y):     return math.sin(x*y) + 8 * x  
def main():     n = 2000     a = zeros((n,n), Float)     xcoor = arange(0,1,1/float(n))     ycoor = arange(0,1,1/float(n)) 
    for i in range(n):         for j in range(n):             a[i,j] = f(xcoor[i], ycoor[j])  # f(x,y) = sin(x*y) + 8*x 
    print a[1000,1000]     pass 
if __name__ == '__main__':     main() 
I try to make this run faster even using psyco, but I found this still slow, I tried using java and found it around 13x faster... 
public class s1 { 
        /**          * @param args          */         public static int n = 2000;         public static double[][] a = new double[n][n];         public static double [] xcoor = new double[n]; 
        public static double [] ycoor = new double[n]; 
        public static void main(String[] args) {                 // TODO Auto-generated method stub                 for (int i=0; i                        xcoor[i] = i/(float)(n);                         ycoor[i] = i/(float)n; 
                } 
                for (int i=0; i                        for (int j=0; j                                a[i][j] = f(xcoor[i], ycoor[j]);                         }                 } 

                System.out.println(a[1000][1000]); 
        }         public static double f(double x, double y){                 return Math.sin(x*y) + 8*x;         } 

}  Can anybody help? 
pujo 
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


[Tutor] PLC programming with Python?

2005-10-12 Thread Johan Geldenhuys
Hi all,
Can Python be used to do PLC (programmable logic controller) 
programming? I'm not sure because Python needs some platform to run on?

Thanks,

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