Re: [Tutor] PLC communication with python?

2006-03-08 Thread Kent Johnson
Jeff Peery wrote:
> Hello, I would like to write a python script that communicates with a 
> PLC (programmable logic controller) as well as instruments like calipers 
> and things. I'm completely in the dark here and not sure where to start 
> reading and learning. could someone point me in the right direction for 
> learning this stuff? thanks!

How does the equipment connect to the computer? If it connects to the 
serial port then try pyserial:
http://pyserial.sourceforge.net/

If it connects to the parallel port there is
http://pyserial.sourceforge.net/pyparallel.html

or for Windows there is winioport:
http://www.geocities.com/dinceraydin/python/indexeng.html

So the first step is to find out what kind(s) of interface you are 
using, and collect software that allows Python to talk to that 
interface. Then try some really simple tests - reading a single value or 
setting a simple setting on the instrument. From there you can build a 
library of functions that do interesting things and build your programs 
on that.

Kent

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


Re: [Tutor] how to write a string into a specific line in a file

2006-03-08 Thread andrew clarke
On Tue, Mar 07, 2006 at 11:18:27AM -, Alan Gauld wrote:

> > I was wondering if it is possible to write a string to a specific line
> > in a file without reading in the whole file in as the below.
> 
> Some languages, such as COBOL and some BASICs etc support 
> random access files, unfortunately Python doesn't (Although I'll be 
> amazed if somebody hasn't cooked up (or is cooking up) a module 
> that does it)

You then go on to mention file.seek().  I thought that seek() provided
"random access", ie. to seek to anywhere at random in a file.  Can you
clarify what you mean by "random access files"?

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


[Tutor] how does this list comprehension work?

2006-03-08 Thread Christopher Spears
I copied this program from Learning Python and got it
to work on Windows XP:

import sys, glob
print sys.argv[1:]
sys.argv = [item for arg in sys.argv for item in
glob.glob(arg)]
print sys.argv[1:]

What the program does is first print the glob and then
a list of everything caught by the glob.  For example:

['*.py']
['showglob.py']

The key to the script is the list comprehension, which
I am having trouble dissecting.  How do I go about
trying to figure out how it works?


"I'm the last person to pretend that I'm a radio.  I'd rather go out and be a 
color television set."
-David Bowie

"Who dares wins"
-British military motto

"I generally know what I'm doing."
-Buster Keaton
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] how does this list comprehension work?

2006-03-08 Thread Kent Johnson
Christopher Spears wrote:
> I copied this program from Learning Python and got it
> to work on Windows XP:
> 
> import sys, glob
> print sys.argv[1:]
> sys.argv = [item for arg in sys.argv for item in
> glob.glob(arg)]
> print sys.argv[1:]
> 
> What the program does is first print the glob and then
> a list of everything caught by the glob.  For example:
> 
> ['*.py']
> ['showglob.py']
> 
> The key to the script is the list comprehension, which
> I am having trouble dissecting.  How do I go about
> trying to figure out how it works?

A list comprehension is a shortcut for a series of one or more nested 
for loops and if statements with a list append in the middle of it.

The list comp pulls the value to be appended out of the loops but 
otherwise the order is not affected. Looking at

[item for arg in sys.argv for item in glob.glob(arg)]

item is the thing that will be appended to the list. The nested for 
loops are
   for arg in sys.argv:
 for item in glob.glob(arg):

If you initialize a list outside the loop, and append to it inside the 
loop, you have the equivalent loops:

   vals = []
   for arg in sys.argv:
 for item in glob.glob(arg):
   vals.append(item)

Kent

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


Re: [Tutor] how does this list comprehension work?

2006-03-08 Thread Danny Yoo


On Wed, 8 Mar 2006, Christopher Spears wrote:

> I copied this program from Learning Python and got it to work on Windows
> XP:
>
> import sys, glob
> print sys.argv[1:]
> sys.argv = [item for arg in sys.argv for item in
> glob.glob(arg)]
> print sys.argv[1:]
>
> The key to the script is the list comprehension, which I am having
> trouble dissecting.  How do I go about trying to figure out how it
> works?


Hi Chris,


Hmmm.  Let me stare at it again.

> sys.argv = [item for arg in sys.argv for item in
> glob.glob(arg)]

... Yikes.  to tell the truth, I can't read this either.


But it might help if we translated it as this:

##
expandedArguments = []
for arg in sys.argv:
for item in glob.glob(arg):
expandedArguments.append(item)
##

Does this code make more sense?

But I'm not so sure myself that list comprehensions are so good when they
get so deeply nested; the nested structure is deceptively obscured in the
flatness of that list comprehension.

I would have just written the code above instead.  Actually, I'd write a
function to capture that abstraction of a mapping and appending function.
Let's call it "mappend()":

##
def mappend(f, L):
"""mappend applies f to each elements in L, and assumes that f returns
a list.  It collects those results in a single flattened list."""
results = []
for x in L:
results.extend(f(x))
return results
##

(The function name is common to folks who've done Lispy kinds of coding.)


And we can see this mappend() function in action:

##
>>> import glob
>>> mappend(glob.glob, ['*.txt', '*.doc'])
['sequences.txt', 'foo.txt', 'pub2ara-pipeline_20060216.doc',
 'Admission Conditions.doc', 'PhD Offer.doc']
##


If you have more questions, please feel free to ask.  Good luck!

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


Re: [Tutor] how to write a string into a specific line in a file

2006-03-08 Thread Danny Yoo


On Wed, 8 Mar 2006, andrew clarke wrote:

> On Tue, Mar 07, 2006 at 11:18:27AM -, Alan Gauld wrote:
>
> > > I was wondering if it is possible to write a string to a specific line
> > > in a file without reading in the whole file in as the below.
> >
> > Some languages, such as COBOL and some BASICs etc support
> > random access files, unfortunately Python doesn't (Although I'll be
> > amazed if somebody hasn't cooked up (or is cooking up) a module
> > that does it)
>
> You then go on to mention file.seek().  I thought that seek() provided
> "random access", ie. to seek to anywhere at random in a file.  Can you
> clarify what you mean by "random access files"?

Hi Andrew,

I think Alan's referring to be able to go to any arbitrary line in a file
or insert arbitrary lines in a file in those other languages.

The Python core language gives us random access to a file's byte content
by using seek(), but we can't do arbitrary insertion without pushing all
the other bytes to the right.


If it helps, imagine a list that doesn't allow for insert(), but does
allow for appends().

L = ['a', 'c', 'd']

If we wanted to get a 'b' into there in the right place, we could go about
it this way:

L.append('b')## ['a', 'c', 'd', 'b']
L[2], L[3] = L[3], L[2]  ## ['a', 'c', 'b', 'd']
L[1], L[2] = L[2], L[1]  ## ['a', 'b', 'c', 'd']

That is, we bubble the 'b' down.  Or looking at it another way, we push
and shove everything to the right of our insertion point to make room for
the insertion.

This impovershed interface is pretty much what we have with files.  We're
allowed to seek(), read(), and write(), but we're not given insert() as a
primitive operation, because that's not an operation that's easy to do
with files directly.  It's doable, but it's painful and expensive.

That's why programs that have to do things like insertion or deletion on a
text file will load the file's contents into a list, work on the list, and
then spit that list back into the file. It's just easier and faster to use
a list as our intermediary.


I hope this helps!

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


Re: [Tutor] how to write a string into a specific line in a file

2006-03-08 Thread Alan Gauld
>> Some languages, such as COBOL and some BASICs etc support
>> random access files, unfortunately Python doesn't
> You then go on to mention file.seek().  I thought that seek() provided
> "random access", ie. to seek to anywhere at random in a file.  Can you
> clarify what you mean by "random access files"?

Sure.
The languages I'm describing provide the ability to deftine a data record
and associate that with a file. The file is declared to be of random access
type and this automatically associates an index file with it.

The programmer can then go to specific lines directly, read fields out of
the line, write new lines directly to the file, delete lines out of the file 
etc.
More or less treating the file as if it were a python list of tuples.

The nearest in Python is probably shelve but with the disadvantage that
you can only search on the key rather than on any field in the record.

To see some examples in MALLARD Basic (one of the ones that
I have used in the past - on CP/M!) look here:

http://www.fvempel.nl/basic.html

(Its got German(or Dutch mebbe given the .nl?)  variables and comments
but the keywords are in English!)

Essentially it is possible to create a Python module to do the same, you
just need the index file to hold the byte position for every field of every
record. Inserting lines into the middle is actually done by appending and
just adding a new index entry. Deletions are done by blanking the file
and index entries (maintaining the byte count) Usually a housekeeping
routine will tidy things up when the files are closed.

Could be an interesting excercise. It could even be based on the bsd
database stuff as a starter. The main reason its not popular now is
simply that with massive RAM its usually faster and easier to just read
the data into memory. If its bigger than that a fuill blown database is
a faster option - although several RDBMS are built with ISAM
underneath

PS. For the really curious you can still buy a commercial version of
Mallard BASIC from its creators Locomotive Software. I'm not clear
whether it is CP/M only or whether they now have a PC version.
But there are PC CP/M emulators! More info from Wikipedia:

http://en.wikipedia.org/wiki/Mallard_BASIC

And for a more coherent explanation of ISAM file access thee is also this:

http://en.wikipedia.org/wiki/ISAM

Ah! 1984 business computing:
BASIC, CP/M, 128K RAM,180K floppies and graphics free text screens...
Happy days! :-)

HTH, Bet you wish you never asked! :-)

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] PLC communication with python?

2006-03-08 Thread Alan Gauld
> If it connects to the parallel port there is
> http://pyserial.sourceforge.net/pyparallel.html
> 
> or for Windows there is winioport:
> http://www.geocities.com/dinceraydin/python/indexeng.html

Ooh. I didn't know about these. Thanks for the pointers Kent.

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


Re: [Tutor] PLC communication with python?

2006-03-08 Thread Alan Gauld

> Hello, I would like to write a python script that communicates with
> a  PLC (programmable logic controller)

Assuming your PLC is on a development board on a PC then it might
be possible using the serial module interface.

Otherwise you probably need to wrap the C API into a Python API
using something like SWIG. But thats not a task for the faint harted,
especially if you are a novice. If you know a little C then its worth 
invetigating.

> as well as instruments like  calipers and things.

That will require an interface board and again probably wrapping
the API as python using SWIG. You might be able to use serial access
or ioctl but it will depend on the interface board and the protocol
in use (HPIB for example is probably viable)

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


[Tutor] How to monitor streaming data from the internet via modem?

2006-03-08 Thread Ars



Say I want to monitor the data that comes through 
my modem when I'm running a trading platform (like for stocks) so I can send an 
alert when a certain condition has been met like the price of a stock.  

 
If the trading program is in java or perl, can I 
just tap into the raw data that trading program is recieving through 
the modem without interfering with the trading program's own 
information stream?
 
-Jack
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Tutor Digest, Vol 25, Issue 18

2006-03-08 Thread victorf
I need help to connect to db on Linux machine.
When I do it manually -all right, try to execute script does not work.
My script is simple:
---
def Connect3():
#arg=os.system('sql.py --prompt qa2:adsdb
inbl27,inbl27,inbl27:root:adsgoogle:qa2ads0,qa2ads1')
arg=os.popen('sql.py --prompt qa2:adsdb
inbl27,inbl27,inbl27:root:adsgoogle:qa2ads0,qa2ads1')
   
Connect3() 
-
Thanks in advance,
Vic
-Original Message-
From: [EMAIL PROTECTED] [mailto:[EMAIL PROTECTED] On Behalf
Of [EMAIL PROTECTED]
Sent: Tuesday, March 07, 2006 3:00 AM
To: tutor@python.org
Subject: Tutor Digest, Vol 25, Issue 18

Send Tutor mailing list submissions to
tutor@python.org

To subscribe or unsubscribe via the World Wide Web, visit
http://mail.python.org/mailman/listinfo/tutor
or, via email, send a message with subject or body 'help' to
[EMAIL PROTECTED]

You can reach the person managing the list at
[EMAIL PROTECTED]

When replying, please edit your Subject line so it is more specific than
"Re: Contents of Tutor digest..."


Today's Topics:

   1. Re: search and replace (Danny Yoo)
   2. Re: search and replace (Alan Gauld)
   3. Re: Functions and random buttons (Alan Gauld)
   4. Re: [OT] Shells (Alan Gauld)
   5. how to write a string into a specific line in a file (tak)
   6. Re: how to write a string into a specific line in a file (tak)


--

Message: 1
Date: Tue, 7 Mar 2006 00:13:07 -0800 (PST)
From: Danny Yoo <[EMAIL PROTECTED]>
Subject: Re: [Tutor] search and replace
To: tak <[EMAIL PROTECTED]>
Cc: tutor@python.org
Message-ID:
<[EMAIL PROTECTED]>
Content-Type: TEXT/PLAIN; charset=US-ASCII



> I have a problem finding specific words.
> I would like to filter out a word or replace it in a file.
> I notices that the re module is good for finding patterns.

Hi Tak,

Although regular expressions might be overkill for this problem, it can't
hurt to know about the Regex HOWTO:

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


Note that strings can already do simple replacement:

##
>>> 'this is a test hi world'.replace('hi', 'hello')
'thellos is a test hello world'
##

As this example shows, we need to be a bit careful with it.


Regexes allow us to do a slightly smarter, word boundary-specific
substitution:

##
>>> import re
>>> re.sub(r'\bhi\b', 'hello', 'this is a test hi world')
'this is a test hello world'
##

The Regex HOWTO link above is a tutorial on how to use the module
effectively.  If you have questions, please feel free to bring them up.

Good luck!



--

Message: 2
Date: Tue, 7 Mar 2006 09:33:03 -
From: "Alan Gauld" <[EMAIL PROTECTED]>
Subject: Re: [Tutor] search and replace
To: "tak" <[EMAIL PROTECTED]>, 
Message-ID: <[EMAIL PROTECTED]>
Content-Type: text/plain; format=flowed; charset="iso-8859-1";
reply-type=original

Hi tak,

> hello, Othello. # just the hello and not Othello

One simplistic approach that does not use regex is to search for spaces as
part of the string. But that doesn't work for punctuation nor at the start
or end of lines.

So that leaves us, as you observed, with regular expressions.

regex allows us to specify certain conditions in the patterns such as
whether the characters are digits etc, and also whether we are kooking for a
word which is wat you want.
Specifically \W signifies a word boundary so

\Whello\W

will find hello as a word.

Take a look at my tutorial topic on regex for more detail, or go to the
excellent regex How-To linked from the Python site.

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





--

Message: 3
Date: Tue, 7 Mar 2006 09:42:00 -
From: "Alan Gauld" <[EMAIL PROTECTED]>
Subject: Re: [Tutor] Functions and random buttons
To: "Simon Stoltze" <[EMAIL PROTECTED]>, 
Message-ID: <[EMAIL PROTECTED]>
Content-Type: text/plain; format=flowed; charset="iso-8859-1";
reply-type=original

Simon,

> ...I want to make each button clickable

The buttons are clickable so I'm not absolutely sure what you mean?
Do you mean you want to add some action to them when they are clicked?
Thats done with the command option in Tkinter.

define a function and assign it to the button.

In this case it will likely be the same function for all buttons so you
might want to do a wee bit of trickery like:

def func(x,y):#x,y the button coords
# do something here

for i in range(length):
for j in range(length):
self.dict['%s%s' % (i, j)] = Button(sel.frame, text = ' 
'
 command = lambda
x=i,y=j: func(x,y))

That uses the default values of the lambda function to pass the coords of
the button being pressed to your generic function. func can then use those
coord

[Tutor] module imports

2006-03-08 Thread kevin parks
i have a module called foo.py

foo.py imports random

kp.py imports foo.py ... but in kp.py i also need to use stuff from 
random...

so kp.py also imports random

but i prolly shouldn't do that right?

Cause now, haven't i needlessly copied the same 
names/attributes/methods/functions to 2 namespaces...

I get very confused about imports... and accessing names from other 
spaces and such...
and efficiency.

i have 2 or more modules all of which need to access a given function 
or method, what is the best way to do that.

i am so cornfused

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


Re: [Tutor] How to monitor streaming data from the internet via modem?

2006-03-08 Thread Alan Gauld
> If the trading program is in java or perl, can I just tap into the raw 
> data that trading program is recieving through the modem without 
> interfering with the trading program's own information stream?

Assuming it turns into IP data at some satage then you can get several 
programs - some are even free! - that will sniff IP packets and either 
log them to a file or display them on a screen. This can run in 
background with only slight impact on performance. So you can 
use Popen to capture the output and analyze it.

Search for 'packet sniffer' on Google. Ethereal is one well known 
and powerful one, but there are others which may be simpler to 
use. Here is one web site that lists a few:

http://netsecurity.about.com/cs/hackertools/a/aafreepacsniff.htm

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


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


[Tutor] Connecting to DB

2006-03-08 Thread Alan Gauld
Please use meangingful subject lines. I've changed this one.

>I need help to connect to db on Linux machine.
> When I do it manually -all right, try to execute script does not work.

I'm not quite sure what you expect here but...

> My script is simple:
> ---
> def Connect3():
>#arg=os.system('sql.py --prompt qa2:adsdb
> inbl27,inbl27,inbl27:root:adsgoogle:qa2ads0,qa2ads1')
>arg=os.popen('sql.py --prompt qa2:adsdb
> inbl27,inbl27,inbl27:root:adsgoogle:qa2ads0,qa2ads1')
>   
> Connect3() 

Your script is simple but does nothing useful. 
It calls popen with a command line(which is the one I assume you 
used at the command line?) and stores the pipe returned by popen 
in arg. It then exits the function which deletes arg and therefore 
the pipe and its connection.

At no point do you try to read the output from your commandline, 
nor do you return anything to the outer part of your script. And of 
course we have no idea what sql.py does inside, so I'll just assume 
it works as intended for now

What did you think this would do?

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


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


[Tutor] Using os.popen()

2006-03-08 Thread Danny Yoo


Hi Victor,

Meta notes: if you're replying to a message, please make sure that message
is relevant to what you're asking.  You just replied to the digest and
included its whole contents back at the list, which is not useful.  In
this particular case, it makes more sense to send a new message to the
mailing list, not a reply to the archive.

Furthermore, please try to make the subject line relevant to your
question.  I've changed it to 'Connection to a database', since that's
what you're asking about.

I'm a stickler for these things because doing these things makes the
archive at:

http://mail.python.org/pipermail/tutor/2006-March/thread.html#start

more pleasant.  If all the messages in that archive were labeled as
"Re: [Tutor] Tutor Digest, Vol 25, Issue 18", that would greatly reduce
the usefulness of the archive.


Anyway, to your question:



> I need help to connect to db on Linux machine.
> When I do it manually -all right, try to execute script does not work.
> My script is simple:
> ---
> def Connect3():
> arg=os.popen('sql.py --prompt qa2:adsdb
> inbl27,inbl27,inbl27:root:adsgoogle:qa2ads0,qa2ads1')

Can you be more specific than "does not work"?  Do you see a syntax or
runtime error, or nothing, or...?  Do you see any output at all?

I'm trying to impress the idea that many of us on the list are not
psychic.  You have to help us with our shortcomings!  *grin*



I'll assume for the moment that you're using some kind of Unix.  What
happens if you type:

$ sql.py --prompt ...

at that directory?  I would be surprised if this worked, because your path
should not include your current working directory unless you've changed it
explicitly.


Anyway, good luck to you!

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


Re: [Tutor] how does this list comprehension work?

2006-03-08 Thread Anna Ravenscroft
On 3/8/06, Christopher Spears <[EMAIL PROTECTED]> wrote:
I copied this program from Learning Python and got itto work on Windows XP:import sys, globprint sys.argv[1:]sys.argv = [item for arg in sys.argv for item inglob.glob(arg)]print sys.argv[1:]
What the program does is first print the glob and thena list of everything caught by the glob.  For example:['*.py']['showglob.py']The key to the script is the list comprehension, whichI am having trouble dissecting.  How do I go about
trying to figure out how it works?

Hi Christopher,

Others have ably deciphered this particular list comprehension (LC) for
you. I'm curious though, are you having trouble because this is a
nested list comprehension or do you not have a good grasp on LCs in
general? They can be a little tricky to grok at first. 

In case you're not completely clear on LCs in general, here's a bit
more description. If you are clear, just skip to the next email. ;-)


First off - a list comprehension creates a new list. It does so by
pulling items from an existing list or other iterable object.  So,
for example:

newlist = [item for item in oldlist]
print newlist

This example wasn't terribly useful - it just made a new list. You can,
however, *do* stuff to the item as you're passing it to the new list.
For example, if you want the squares of the items in the old list, you
coudl do:

oldlist = [1,2,3]
sqlist = [item*item for item in oldlist]

Another thing you can do is filter the items in the old list in some way. So, if you only want the even numbers you could do:

elist = [item for item in oldlist if item%2==0] # using modulo, which returns the remainder

You can use LCs with files, with dicts, with strings, with any iterable
object. It's really slick, really easy, pretty fast.  LCs can be a
little tricky, like I mentioned (especially when nested) but are quite
handy and fun once you get to know them. Just don't get so tricky that
it's hard to read your code. If it's getting hard to read, use a for
loop. 

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


Re: [Tutor] module imports

2006-03-08 Thread Bob Gailer
kevin parks wrote:
> i have a module called foo.py
>
> foo.py imports random
>
> kp.py imports foo.py ... but in kp.py i also need to use stuff from 
> random...
>
> so kp.py also imports random
>
> but i prolly shouldn't do that right?
>   
Wrong.
> Cause now, haven't i needlessly copied the same 
> names/attributes/methods/functions to 2 namespaces...
>   
The first import of a module runs its top level code, and creates a 
module object. Subsequent imports simply place a reference to that 
object in the current namespace.
> I get very confused about imports... and accessing names from other 
> spaces and such...
> and efficiency.
>
> i have 2 or more modules all of which need to access a given function 
> or method, what is the best way to do that.
>   
Put the function in a module and import it as needed.
> i am so cornfused
>   
Apparently. Even your spell checker is addled.
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] module imports

2006-03-08 Thread kevin parks

On Mar 8, 2006, at 7:09 PM, Bob Gailer wrote:

> kevin parks wrote:
>> i have a module called foo.py
>>
>> foo.py imports random
>>
>> kp.py imports foo.py ... but in kp.py i also need to use stuff from 
>> random...
>>
>> so kp.py also imports random
>>
>> but i prolly shouldn't do that right?
>>
> Wrong.

so it is okay to do that?

>> Cause now, haven't i needlessly copied the same 
>> names/attributes/methods/functions to 2 namespaces...
>>
> The first import of a module runs its top level code, and creates a 
> module object. Subsequent imports simply place a reference to that 
> object in the current namespace.
>> I get very confused about imports... and accessing names from other 
>> spaces and such...
>> and efficiency.
>>
>> i have 2 or more modules all of which need to access a given function 
>> or method, what is the best way to do that.
>>
> Put the function in a module and import it as needed.

but i mean a function from a standard module.

>> i am so cornfused
>>
> Apparently. Even your spell checker is addled.
[that was a joke]


so let's say i have a script called foo.py.
foo.py uses some things from the random module and therefore has

import random
import kp
import sys
import time

etc. & co.  in it

but foo.py also imports a module kp which also
happens to import random.

Should i only be importing random once or are you saying it
is just fine to import a module that imports another module that
you actually have already imported.


-kp--





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


[Tutor] how to read a text file, and find items in it

2006-03-08 Thread adam
hey guys!

I am trying to learn a bit of python, and thought it would be a challenge to 
make a script that reads a text file and returns all the instances of a word I 
specify. Basically a find and then a list of all the finds.

I am using a text file with some random words in it to test it out on

Thanks!

Adam



My script so far is this:

-

import fileinput

print
print "Welcome to LIST YOUR FILEINS script"

print
print "STEP 01"

print "drag'n'drop your script from the Finder here"
script = raw_input(">")

print
print "The location of your script is"
print script

print

file=open(script)
text=file.readlines()
f = 'file'
for f in text: print f



The test file:

random
words
file in
filein
mac
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] module imports

2006-03-08 Thread Kent Johnson
kevin parks wrote:
> so let's say i have a script called foo.py.
> foo.py uses some things from the random module and therefore has
> 
> import random
> import kp
> import sys
> import time
> 
> etc. & co.  in it
> 
> but foo.py also imports a module kp which also
> happens to import random.
> 
> Should i only be importing random once or are you saying it
> is just fine to import a module that imports another module that
> you actually have already imported.

It's fine. Each module that needs to use random (for example) should 
import it. Only the first import to be executed has any appreciable 
cost; the subsequent ones just put a reference to the already-loaded and 
cached module into the current module namespace.

Kent

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


[Tutor] Python interpreter/Xemacs and the py-execute-buffer

2006-03-08 Thread Francesco Queirolo
Greetings,I have been using Xemacs 21.4 Patch 13 on Windows XP with Python 2.4.2 (final). Whenever I try to run a script inside the xemacs window using: C-c C-c I get the following: Opening output file: Invalid argument, C:\Documents and Settings\jkat\Local Settings\Temp;C:\Devel\emacsk2AS2xand of course my code doesn't pop the output up to the xemacs screen. The last part of the error message "k2AS2x" is different every time. I have already re-installed xemacs and am rather baffled after creating folders called Devel and temp as well.Thanks,FrancescoFrancesco Queirolo(+1)505.661.2670[EMAIL PROTECTED]
		Yahoo! Mail
Bring photos to life! New PhotoMail  makes sharing a breeze. 
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor