Re: [Tutor] GUI Buttons

2009-10-14 Thread Albert-Jan Roskam
Hi Alan and Kent,

Thank you for helping me. This is a lot simpler than the solution I had in 
mind. The button names in my program always start with "button_", so I wanted 
to use locals() in a list comprehension to compile a list of the buttons. But 
then I'd still have the names and not the instances. 

But when is the configure() method more appropriate to change e.g. the button 
status?

Cheers!!
Albert-Jan

~~
Before you criticize someone, walk a mile in their shoes, that way 
when you do criticize them, you're a mile away and you have their shoes! 
~~


--- On Tue, 10/13/09, Alan Gauld  wrote:

> From: Alan Gauld 
> Subject: [Tutor] GUI Buttons
> To: tutor@python.org
> Date: Tuesday, October 13, 2009, 10:06 AM
> Please provide a subject when sending
> mail to the list.
> And please create a new message so it doesn't get lost in
> an old thread...
> 
> "Albert-Jan Roskam" 
> wrote in message
> 
> > I'm using Tkinter to program my very frist GUI.
> > Each button grays out after it has been used so the
> user knows what next steps to take.
> > Now I want to define a Reset button to 'ungray' all
> buttons (state='normal').
> > How can I programmatically create a list of all
> available buttons?
> 
> Just add your buttons to a list when you create them
> Then in your reset method do
> 
> for button in ButtonList:
>    button['state'] = NORMAL
> 
> HTH,
> 
> 
> -- Alan Gauld
> Author of the Learn to Program web site
> http://www.alan-g.me.uk/ 
> 
> ___
> Tutor maillist  -  Tutor@python.org
> To unsubscribe or change subscription options:
> http://mail.python.org/mailman/listinfo/tutor
> 


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


Re: [Tutor] GUI Buttons

2009-10-14 Thread ALAN GAULD


> But when is the configure() method more appropriate to change e.g. the button 
> status?


configure is better when you need to change more than one attribute of a 
widget at a time. The dictionary style access is just a convenience feature. 
If you prefer you can use configure() all the time.

Alan G.

> > > How can I programmatically create a list of all
> > available buttons?
> > 
> > Just add your buttons to a list when you create them
> > Then in your reset method do
> > 
> > for button in ButtonList:
> >button['state'] = NORMAL
> > 
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] writing sample program that zips files and saves in a backup directory

2009-10-14 Thread David

David Eric wrote:



On Tue, Oct 13, 2009 at 5:17 PM, Kent Johnson > wrote:


On Tue, Oct 13, 2009 at 4:38 PM, David Eric mailto:cii...@gmail.com>> wrote:

 > as far as print zip_command, i would add that to the program
however, doesnt
 > just declaring it actually trigger it..thus it would executed and the
 > command line would get printed as well?

I'm not sure what you mean by "declaring", but assigning a value to
zip_command, as you do, and printing it, as I suggest, do just that -
assign and print. It is the call to os.system() that executes the
command.

 


oh i see the if os.sytem() command in the program is what ran the
zip_command

 


 > the actual program i was looking to write would take:
 >  file1,file2,and file3, in /Users/davidteboul/bin/python
 > and condense them into individual files; file1.gz, file2.gz and
file3.gz and
 > save them in a directory already made - (/Users/davidteboul/backups)

I think that doing this with gzip, you have to first copy the files to
the backup directory, then run gzip on the backup.

 


 > ps..did i post and reply all corretly?

You can intersperse your comments with the original email, as I have
done above, rather than putting your entire reply at the end. The
reply all is fine.

Kent


thanks for the help, ill try some stuff and see if i can get it to work
 





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

Has anyone mentioned the module zipfile;
http://www.doughellmann.com/PyMOTW/zipfile/index.html

--
Powered by Gentoo GNU/Linux
http://linuxcrazy.com
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Switching to other version of Python

2009-10-14 Thread Lie Ryan
Alan Gauld wrote:
> 
> "Corey Richardson"  wrote
>> My school has python 2.4. I have python 2.6, and I find it perfectly
>> wonderful. However, I am contemplating the switch, and am wondering,
>> what is your personal opinion on why or why not to use 3.x? Thanks for
>> the help in advance,
> 
> Well your Python 3 programs won't run as expected on your school v2.4
> and your schoool programs won;t run at all on your python v3.
> 
> So I'd stay well clear of v3 for now. There is no compelling reason to
> switch yet.
> 
> You could keep 2.6 on your own PC and use v3 of course but most of the
> v3 stuff is available in 2.6. Also you could upgrade to 2.7 for even
> more 3 compatibility but still better with 2.4 than using 3 would be.
> 
> Resist the urge to upgrade just for the sake of it.
> (Advice I fail to follow in HiFi but try to follow in everything else! :-)
> 

You also have the option to install both and ignore one. It's not like a
black-or-white choice.

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


Re: [Tutor] Recursive user input collection problem

2009-10-14 Thread Lie Ryan
William Witteman wrote:
> I need to collect a couple of integers from a user, but I want to make
> sure that I actually get integers.  I tried this, but subsequent calls
> to the function don't update variable.  I'm not sure this is terribly
> clear - here's the code:
> 
> 
> def getinput(variable,prompt):
>   """
>   Get the input by prompting the user and collecting the response - if it is 
>   a non-integer, try again.
>   """
>   variable = 0

the source of your confusion is misunderstanding of python's calling
model. Here is an article: http://effbot.org/zone/call-by-object.htm

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


[Tutor] namespaces and global

2009-10-14 Thread Jose Amoreira
Hello
I'm new to this list. I tried to find the answer to my question but found 
nothing I could really use. I'm sorry if this is a FAQ.

I want to use a variable defined in an interactive session with the python 
interpreter inside a function imported from a module.

For instance, imagine that my module (call it defs.py, for instance) consists 
of:
#coding=utf-8
def f(x):
return a*x

In an interactive session, I import my module:
>>> from defs import f
and I define a global variable a:
>>> a=3
Now I call my module function, expecting to get the triple of the argument I 
feed it with, but, instead, I get an error:
>>> f(2)
Traceback (most recent call last):
  File "", line 1, in 
  File "defs.py", line 3, in f
return a*x
NameError: global name 'a' is not defined
>>>

I tried using the global command in the module, both before the function 
definition and inside the function definition, but none worked as I expected.
Of course I could redefine my module function, including the parameter a in 
the list of arguments, but I'd rather not.

So, my question: is there any way of telling the interpreter to use the value 
of parameters defined in the interactive session when computing the value of a 
module function?
Thanks
ljma

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


Re: [Tutor] namespaces and global

2009-10-14 Thread Kent Johnson
On Wed, Oct 14, 2009 at 7:26 AM, Jose Amoreira  wrote:

> I want to use a variable defined in an interactive session with the python
> interpreter inside a function imported from a module.
>
> For instance, imagine that my module (call it defs.py, for instance) consists
> of:
> #coding=utf-8
> def f(x):
>    return a*x
>
> In an interactive session, I import my module:
 from defs import f
> and I define a global variable a:
 a=3
> Now I call my module function, expecting to get the triple of the argument I
> feed it with, but, instead, I get an error:
 f(2)
> Traceback (most recent call last):
>  File "", line 1, in 
>  File "defs.py", line 3, in f
>    return a*x
> NameError: global name 'a' is not defined

>
> I tried using the global command in the module, both before the function
> definition and inside the function definition, but none worked as I expected.
> Of course I could redefine my module function, including the parameter a in
> the list of arguments, but I'd rather not.

Python 'global' variables are not really global (everywhere
accessible), they have module scope. So your function f() looks for
the name 'a' first in its local scope, then in the scope of its
containing module defs. It will not look in the namespace of the
caller unless it is also in defs.

> So, my question: is there any way of telling the interpreter to use the value
> of parameters defined in the interactive session when computing the value of a
> module function?

Why do you want to do this? One option is to pass locals() in to the
function. You can also access the caller's context through a stack
frame. But both of these are unusual.

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


Re: [Tutor] Help or Advice on MySQL, Python and test data storage

2009-10-14 Thread greg whittier
On Thu, Oct 8, 2009 at 4:29 AM, David Jamieson wrote:

> Hi All,
> looking for some advice on using Python with MySQL for test data
> storage.
>

While not a relational database, you might also look at
http://www.pytables.org.  It provides a python interface for writing to and
reading from hdf5 files (http://www.hdfgroup.org/HDF5/).
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Finding and Inserting missing dates in a date range.

2009-10-14 Thread Glen Zangirolami
I went ahead and used a date range generator: http://dpaste.com/107140/ to
iterate over the range.I then compared the original list with the generated
one and inserted the missing dates.

I was comparing the generated dates to dates in a list list of tuples:
[(date, count), (date, count) . (date, count)]

Here is the code: http://dpaste.com/107148/

Worked perfectly. Thanks for pointing me in the correct direction.
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Recursive user input collection problem

2009-10-14 Thread William Witteman
Thanks to all who responded.  There were several good points about the
code itself, all of which both helped and work.

I will likely use Alan's example because I find it the most lucid, but
the other suggestions are good signposts to other ways to do the same
thing (but right, as opposed to how I was doing it).

Lie's suggestion that I didn't understand the calling structure of
Python was right on the money, and his included link helps with that,
too.  Thanks again.
-- 

yours,

William

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


[Tutor] Not storing the PATH in ZipFile?

2009-10-14 Thread Nick Hird
I was reading another thread and decided to work on a little project
to backup some files on a regular basis. Since this will mostly be on
windows i am using zipfile and i can create my zip file just fine and
all the files are there, it works great! My question is however, is
there a way to NOT store the path in the zip file? When i decompress
the files, i would like them to not be associated with a particular
folder or path, just the files. I looked through the docs but didn't
see anything to disable the path.
Thanks,
-Nick
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Not storing the PATH in ZipFile?

2009-10-14 Thread Kent Johnson
On Wed, Oct 14, 2009 at 11:38 AM, Nick Hird  wrote:
> I was reading another thread and decided to work on a little project
> to backup some files on a regular basis. Since this will mostly be on
> windows i am using zipfile and i can create my zip file just fine and
> all the files are there, it works great! My question is however, is
> there a way to NOT store the path in the zip file? When i decompress
> the files, i would like them to not be associated with a particular
> folder or path, just the files

ZipFile.write() takes an optional arcname parameter, this is the name
the file wll have in the archive. If you pass the plain file name (no
path components) as arcname I think it will do what you want.

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


Re: [Tutor] Not storing the PATH in ZipFile?

2009-10-14 Thread Nick Hird
That was it. Thanks so much! I was looking at the docs and didn't
think that applied to the path that was stored.
Thanks Again!
-Nick

On Wed, Oct 14, 2009 at 12:16 PM, Kent Johnson  wrote:
> On Wed, Oct 14, 2009 at 11:38 AM, Nick Hird  wrote:
>> I was reading another thread and decided to work on a little project
>> to backup some files on a regular basis. Since this will mostly be on
>> windows i am using zipfile and i can create my zip file just fine and
>> all the files are there, it works great! My question is however, is
>> there a way to NOT store the path in the zip file? When i decompress
>> the files, i would like them to not be associated with a particular
>> folder or path, just the files
>
> ZipFile.write() takes an optional arcname parameter, this is the name
> the file wll have in the archive. If you pass the plain file name (no
> path components) as arcname I think it will do what you want.
>
> Kent
>



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


Re: [Tutor] PyWin32 - Library of functions to interact with windows?

2009-10-14 Thread Katt

Hello all,

I am currently using WinXP and python 2.6.2
I just installed PyWin32 v214 for python 2.6 from the following link:

http://sourceforge.net/projects/pywin32/files/pywin32/Build%20214/pywin32-214.win32-py2.6.exe/download

Installation seemed to go good as it walked me through while locating my 
python directory and checking my path.  I tried to first to download/save 
and then run it, but encountered an error and it would not install.  When I 
went back to the link and then clicked "run" instead of save the 
installation worked.


Now I find myself scratching my head and saying to myself: "now what?"

Your probably thinking - Dive into the documentation, Search the web, Ask 
questions.


I have looked a bit at the documentation.  It definitely seems to believe 
that I am already a seasoned programmer.


Searching the web I have found a lot of general information or overly 
specific with no way to narrow my search to what I am looking for.  I am not 
sure, but it seems very difficult just to search for a way to change the 
color of my text.  Don't get me wrong.  I don't want someone to hand me some 
code snippet of what I need.  I am just looking for an interpreter to 
translate from computer scientist to beginner.


Thus I am hopeful that Asking questions will yield information that I can 
understand.
I am not saying that there is anything wrong with what I have found.  I am 
only saying that my comprehension is not there yet to that level.


Looking at the included compiled documentation I think I found what I am 
looking for.  Please let me know if I am incorrect.


Searching under "change the color" revealed several topics.  I singled out 
win32gui.SetTextColor.  This brought me to the topic "win32api.RGB" and

shows the next line as "int = RGB(red,green,blue).

Question 1:  Did I locate the correct function that I need to use in order 
to change the color of text within a print ""?


Question 2: In order to use this would I just import it? (ie - from win32api 
import RGB)


Question 3: If the above 2 are correct is it just a matter of doing the 
following:

   print "The remaining number of apples is: "RGB(red),number_apples

   The above print statement would yield just the number_apples variable in 
red text.


Thanks in advance,

Katt 


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


Re: [Tutor] namespaces and global

2009-10-14 Thread Alan Gauld


"Jose Amoreira"  wrote

Of course I could redefine my module function, including the parameter a 
in

the list of arguments, but I'd rather not.


Why not? That would be good computer science practice and the
most reliable way to do it. Why do you not want to go down that
route? Is there a specific reason to use a global variable when
use of globals is normally considered bad practice?

I'm curious?


--
Alan Gauld
Author of the Learn to Program web site
http://www.alan-g.me.uk/ 



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


Re: [Tutor] PyWin32 - Library of functions to interact with windows?

2009-10-14 Thread Alan Gauld


"Katt"  wrote


I am currently using WinXP and python 2.6.2
I just installed PyWin32 v214 for python 2.6 from the following link:


Well done! :-)


Now I find myself scratching my head and saying to myself: "now what?"


Good question. Why did you download and install it?
Did you have a specific need or did it just seem like a good idea at the 
time?


If the latter then the best thing to do is just ignore it unttil you have a 
need.

Except for the Pythonwin editor which is far superior to IDLE IMHO.
Definitely have a play with that.

I have looked a bit at the documentation.  It definitely seems to believe 
that I am already a seasoned programmer.


Yes, pywin is a set of modules to let you access the Win32 API - the
raw Operating System calls that are used by C programmers writing
things like MS Word and IE etc.

The typical Windows Python user rarely needs access to that stuff.
The exceptions are if you are writing sys admin type tasks on Windows.

Searching the web I have found a lot of general information or overly 
specific with no way to narrow my search to what I am looking for.  I am 
not sure, but it seems very difficult just to search for a way to change 
the color of my text.


That would normally be done using the GUI tookit you are using - Tkinter,
wxPython etc - you don't need the Win32 API for that the toolkit does that
for you.

code snippet of what I need.  I am just looking for an interpreter to 
translate from computer scientist to beginner.


The best bet is to find a book on basic Windows programming.
Pwetzold's "Programming Windows" used to be the Bible for this
but I suspect it has now been superceded. There are probably online
tutorials to writing MFC applications that you could translate fairly 
directly.


The other area that you can and should use pyWin32 is in interacting
with Windows apps via COM. Mark Hammond's book "Python Programming
on Win32" is the best source for that, but the docs that come with pyWin32
also cover it. BUt again they expect a certain amount of preknowledge.

I am not saying that there is anything wrong with what I have found.  I 
am only saying that my comprehension is not there yet to that level.


pyWin32 is a powerful tool but it is not an easy one to use.

Looking at the included compiled documentation I think I found what I am 
looking for.  Please let me know if I am incorrect.


Searching under "change the color" revealed several topics.  I singled 
out win32gui.SetTextColor.  This brought me to the topic "win32api.RGB" 
and

shows the next line as "int = RGB(red,green,blue).

Question 1:  Did I locate the correct function that I need to use in 
order to change the color of text within a print ""?


Probably not.
print goes to stdout which on Windows is usually in a CMD shell window.
the Win32API does not directl;y control that output. What you found is the
way tochange the colour of a font inside a window - such as a data entry
field  in a form that you created.

Question 2: In order to use this would I just import it? (ie - from 
win32api import RGB)


No. You need to import the function as well as the values.
Normally you impotrt the module and access the rest via that:

import win32gui
win32gui.SetTextColor(hdc, win32gui.RGB)

or similar. Note that the first parameter is hdc which stahnds for
Hamndle to a DeviceContext.
A DeviceContext is the window you want to change the color of.
So you first need to find a way of obtaining the hdc value.
In your own app thats fairtly easy since you assign it to a variable
when you create the window/widget. But trying to find the hdc for
a cmd shell is very tricky, especially programatically at run time!

Question 3: If the above 2 are correct is it just a matter of doing the 
following:

   print "The remaining number of apples is: "RGB(red),number_apples


No, thats no good, sorry.
If you want to change the colour of text in a command window I think
you are best investigating wconio or some of the other modules
mentioned earlier. But it may not be possible for cmd.exe.

The next step is to write a very simple GUI program with a single
Text widget on which you write your output. Then you can colour it
any way you like.

Learning how to build GUIs wioll take a bit of effort but a lot less
than trying to use the Win32API to modify a cmd window!
Take a look at the Event driven oprogramming topic in my
tutorial for an example of just that kind of GUI... You can use
EasyGUI to capture user input.

HTH,

--
Alan Gauld
Author of the Learn to Program web site
http://www.alan-g.me.uk/ 



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


Re: [Tutor] PyWin32 - Library of functions to interact with windows?

2009-10-14 Thread Scott Nelson
If I may chime in...

As Alan said, pywin is basically a thin wrapper around the Win32 API.  The
Win32 API is very complex.  Thus pywin is, by necessity, also very complex.
There is documentation for pywin, but it is very minimal as you've probably
noticed.  If you are a *very* bold beginner with lots of patience, the best
documentation for pywin is actually the documentation for the Win32 API
itself found on Microsoft's "MSDN Library" website [1].  Navigating and
using this documentation requires a knowledge of the Windows architecture
and C programming. Once you find what you want in the MSDN documentation,
you can usually find the same function in pywin.  Again, this is not going
to be easy if you are a beginner (it is difficult even for more experienced
programmers), but I wanted to let you know the info is out there.  If you
want to dive into Win32 programming and learn the basics, the books Alan
mentioned are good.  But again, Win32 programming is pretty low-level
and complex.  There is usually an easier way to do most things you need to
do.  Especially if you want to create GUI's in Python, don't start with
pywin and Win32.  Use EasyGUI, Tkinter, or wxPython (in order of easiest to
most powerful)

Now, back to your specific task.  To clarify, I'm assuming you want to color
the text that shows up in Window's console when you do "print" from Python,
correct?  It *is* possible to color console text with Python and pywin.
But, it is tricky and not obvious.  I've been wondering how to do this
myself and I recently found some C code on the web [2] that does this and I
translated that into to Python and pywin.  It can be done in about 4 lines
of Python.

To get you started, here is the link [3] to the MSDN documentation that
tells you what you need to know about coloring text in a Windows console
window (what Python's print command uses).  Then, it is up to you to
translate this into Python and pywin.  The link [2] could also help.  If you
are up for a challenge, give it a shot.  If you get stuck or it takes too
long, write back and I/we can nudge you in the right direction with a code
snippet.

(While writing this email, I also discovered that you can control the cursor
position in a win32 console with pywin!  Fun!)

Best of luck, and feel free to ask for more help!

-Scott

[1] - http://msdn.microsoft.com/en-us/library/default.aspx
[2] - http://www.dreamincode.net/forums/showtopic23272.htm
[3] - http://msdn.microsoft.com/en-us/library/ms682088(VS.85).aspx
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] PyWin32 - Library of functions to interact with windows?

2009-10-14 Thread Kent Johnson
On Wed, Oct 14, 2009 at 12:38 PM, Katt  wrote:

> Searching the web I have found a lot of general information or overly
> specific with no way to narrow my search to what I am looking for.  I am not
> sure, but it seems very difficult just to search for a way to change the
> color of my text.

Did you look at the console and wconio links Tim Golden provided? They
both include methods to set the console text color. Looking at the
source for wconio, I see it calls SecConsoleTextAttribute() to set the
color. That function is documented here:
http://msdn.microsoft.com/en-us/library/ms686047%28VS.85%29.aspx

I can't help you figure out how to use this from Python using win32 though...

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


[Tutor] Masking operation

2009-10-14 Thread Wayne
I've searched "Python masking" and various other terms in Google and the
python docs and came up with nothing useful.
This is just a bit of a hobby project that was inspired by some assembly
homework that one of my friends is now doing and I did last semester. The
assignment was to create a program that took two strings and XOR'ed the one
by the other.

For example:

mask: secret
data: This is a secret message!

and you would have:
s XOR T
e XOR h

e XOR i
t XOR s

s XOR ' '

So I basically want a mask the same length as my data (in this case, 25), so
I would have:

mask = 'secretsecretsecretsecrets'

The way I'm currently doing it is:
mask = mask * (len(message)/len(mask))

for l, m in zip(message, mask):
word += chr(ord(l) ^ ord(m))

but I'm wondering if there are any easier/more pythonic ways.

Thanks,
Wayne

-- 
To be considered stupid and to be told so is more painful than being called
gluttonous, mendacious, violent, lascivious, lazy, cowardly: every weakness,
every vice, has found its defenders, its rhetoric, its ennoblement and
exaltation, but stupidity hasn’t. - Primo Levi
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] namespaces and global

2009-10-14 Thread Jose Amoreira
Alan, Kent, hello!
Thanks for your help. As for your "curiosity", I'm teaching elementary physics 
to undergraduates in computer engineering. Trying to speak my students' 
language, I wanted to show them simple applications that compute numerical 
values for the kinematics formulas of uniformly accelerated motion (the weaker 
students call these formulas Chinese gibberish). 
What I had in mind was to have a module define vectors and vector algebra 
operations using a simple class definition, and another with kinematics 
formulas, like (for uniformly accelerated motion)

def pos(t):
return r0+v0*t+0.5*a*t**2

Here r0 (initial position) v0 (initial velocity) and a (acceleration) are 
global vector parameters which I'd define in an interactive session with the 
students, but I'd rather not include them in the formal parameter list of 
function pos, just because they aren't usually displayed as such in physical 
formulae. I'd like to keep the look and feel of those formulas in the 
interactive python session, without having to type the function definitions in 
that session, but rather import them from a pre-prepared module file.

Anyway, thanks again!
Best Regards,
Jose


On Wednesday 14 October 2009 06:18:28 pm Alan Gauld wrote:
> "Jose Amoreira"  wrote
>
> > Of course I could redefine my module function, including the parameter a
> > in
> > the list of arguments, but I'd rather not.
>
> Why not? That would be good computer science practice and the
> most reliable way to do it. Why do you not want to go down that
> route? Is there a specific reason to use a global variable when
> use of globals is normally considered bad practice?
>
> I'm curious?

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


Re: [Tutor] Masking operation

2009-10-14 Thread Rich Lovely
2009/10/14 Wayne :
> I've searched "Python masking" and various other terms in Google and the
> python docs and came up with nothing useful.
> This is just a bit of a hobby project that was inspired by some assembly
> homework that one of my friends is now doing and I did last semester. The
> assignment was to create a program that took two strings and XOR'ed the one
> by the other.
> For example:
> mask: secret
> data: This is a secret message!
> and you would have:
> s XOR T
> e XOR h
> 
> e XOR i
> t XOR s
> 
> s XOR ' '
> So I basically want a mask the same length as my data (in this case, 25), so
> I would have:
> mask = 'secretsecretsecretsecrets'
> The way I'm currently doing it is:
> mask = mask * (len(message)/len(mask))
> for l, m in zip(message, mask):
>     word += chr(ord(l) ^ ord(m))
> but I'm wondering if there are any easier/more pythonic ways.
> Thanks,
> Wayne
> --
> To be considered stupid and to be told so is more painful than being called
> gluttonous, mendacious, violent, lascivious, lazy, cowardly: every weakness,
> every vice, has found its defenders, its rhetoric, its ennoblement and
> exaltation, but stupidity hasn’t. - Primo Levi
>
> ___
> Tutor maillist  -  tu...@python.org
> To unsubscribe or change subscription options:
> http://mail.python.org/mailman/listinfo/tutor
>
>
Look up the itertools module:

http://docs.python.org/library/itertools.html

Specifically, cycle and izip.  itertools.cycle is the trick here, izip
is a more efficient (in this case) version of zip.

-- 
Rich "Roadie Rich" Lovely

There are 10 types of people in the world: those who know binary,
those who do not, and those who are off by one.
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] PyWin32 - Library of functions to interact with windows?

2009-10-14 Thread Alan Gauld


"Scott Nelson"  wrote

myself and I recently found some C code on the web [2] that does this and 
I
translated that into to Python and pywin.  It can be done in about 4 
lines

of Python.


Yes, its much easier than I expected. I had never seen the
SetConsoleTextAttribute API call before nor the GetStdHandle
for accessing stdout. Thanks for the link.

(While writing this email, I also discovered that you can control the 
cursor

position in a win32 console with pywin!  Fun!)


I'd still go for wconio for that kind of stuff - including
changing colors...

http://newcenturycomputers.net/projects/wconio.html

Alan G 



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


Re: [Tutor] Masking operation

2009-10-14 Thread Alan Gauld


"Wayne"  wrote


I've searched "Python masking" and various other terms in Google and the
python docs and came up with nothing useful.


Try the Using the OS topic in my tutorial. There is a sidebar in 
there about using the bitwise operators to do masking about half way down.


HTH,

--
Alan Gauld
Author of the Learn to Program web site
http://www.alan-g.me.uk/

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


Re: [Tutor] Masking operation

2009-10-14 Thread Wayne
On Wed, Oct 14, 2009 at 6:31 PM, Rich Lovely wrote:

>
> Specifically, cycle and izip.  itertools.cycle is the trick here, izip
> is a more efficient (in this case) version of zip.


Ahah! Cycle is exactly what I wanted - I thought there was something like
this but I forgot where I found it.

Using zip is redundant for me, this is what my function looks like now:

def crypt(msg, mask):
m = itertools.cycle(mask)
word = ''
for l in msg:
word += chr(ord(l) ^ ord(m.next()))

return word

and works quite perfectly.

Alan - I wish I had known this a few days ago when I was trying to use masks
to set debug levels. Oh well - having experimented with what I've learned in
my discrete structures class about and, or, xor, I've learned what I need to
know, and reviewing your tutorial really helps put some of the pieces
together that I wasn't able to come up with on my own.

Thanks all!
-Wayne
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Changing the color of text in the windows shell (WinXP/python 2.6.2)

2009-10-14 Thread Katt

Hello All,


Message: 4
Date: Wed, 14 Oct 2009 18:38:31 +0100
From: "Alan Gauld" 
To: tutor@python.org
Subject: Re: [Tutor] PyWin32 - Library of functions to interact with
windows?

"Katt"  wrote


Now I find myself scratching my head and saying to myself: "now what?"


Good question. Why did you download and install it?
Did you have a specific need or did it just seem like a good idea at the
time?


I installed the pywin32 as to use the color changing capacity.


Except for the Pythonwin editor which is far superior to IDLE IMHO.
Definitely have a play with that.


Actually I am using "Programmer's Notepad" to create my python scripts.



HTH,



Thanks both to Alan G. and Scott N. and Kent J. for your responses.  I think 
that I will eventually learn all aspects of python programming (if I am 
lucky).  So I will pursue all three types of changing color.


First I will try WConio, next to be followed by EasyGUI, and last as Scott 
has pointed out the more difficult pywin32.


Below please find my first basic attempt at using the WConio:

code
from WConio import textcolor

apples_left = 5

print "There are",(textcolor(4)),apples_left,(textcolor(7)),"left in the 
basket."

-

The output works, but there is one snag.  I think that I am leaving 
something out because it has the word "None" on both sides of the variable.


There are None 5 None left in the basket.

I tried both textcolor(4 | 0) and textcolor (4,0).  The first has no change 
and the second has an error:


---
Traceback (most recent call last)
   File "changecolor.py", line 6, in 
   print "There 
are",(textcolor(4,0)),apples_left,(textcolor(7,0)),"left in the basket."

SyntaxError: invalid syntax
---

Could someone let me know what I am missing?

Thanks in advance,

Katt 


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