[Tutor] Variables in workspace

2007-10-13 Thread Eli Brosh

Hello
I am working with python interactively using IDLE.

Since starting, I defined some variables:
s='string'
a=1
b=[1,2]
c=1.02

and so on.

Now, I want to know which variables are in my workspace.
That is, is there a command similar to who in MATLAB ?
I want to call who
and get the output:
s a b c
(a listing of all the variables I defined in the session)

Now, is there a way to clear some or all the variables in that list ?


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


Re: [Tutor] Variables in workspace

2007-10-13 Thread Kent Johnson
Eli Brosh wrote:
 I am working with python interactively using IDLE.
 
 Since starting, I defined some variables:
 
 Now, I want to know which variables are in my workspace.

 RESTART 
  dir()
['__builtins__', '__doc__', '__name__']
  s='string'
  a=1
  dir()
['__builtins__', '__doc__', '__name__', 'a', 's']

 Now, is there a way to clear some or all the variables in that list ?

  del a
  del s
  dir()
['__builtins__', '__doc__', '__name__']

To clear all you can Restart Shell from the menu.

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


Re: [Tutor] Variables in workspace

2007-10-13 Thread bob gailer
Eli Brosh wrote:

 Hello
 I am working with python interactively using IDLE.

 Since starting, I defined some variables:
 s='string'
 a=1
 b=[1,2]
 c=1.02

 and so on.

 Now, I want to know which variables are in my workspace.
 That is, is there a command similar to who in MATLAB ?
 I want to call who
 and get the output:
 s a b c
 (a listing of all the variables I defined in the session)

Start IDLE, then enter dir(). That will give you the names already in 
the workspace. Then assign your variables and enter dir() again.


 Now, is there a way to clear some or all the variables in that list ?

Clear? Do you mean set to None or delete them?

The del statement is the way to delete variables. Since dir() gives you 
their names one needs use eval.

for varName in dir():
eval 'del ' + varName

Of course that is overkill as it will delete __builtins__ etc, so you 
want to screen out all the names that you see in the initial dir() call.
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] slice lists and slicing syntax questions

2007-10-13 Thread Michael Langford
On 10/12/07, [EMAIL PROTECTED]  [EMAIL PROTECTED] wrote:

 I have been using Python for years now, in all kinds of environments, but
 example:  x is vector of length 5, with value a,b,c,d,e , then:

 x[3,1,1,1,3,2] # gives [d, b, b, b, d, c]

 What is the python equivalent?

  a.  Am I understanding the situation correctly?

When you call [] on an object, it calls __getitem__The definition for
getitem is __getitem__(self,key), where key can be an integer or a slice
object. Slice objects are either a range or a range+step. You've got the
right picture

  b.  Why was this particular way of doing slices chosen?


Dunno. I'll leave  this to the language historians. I  will say a
differently implemented slice object interface would lend itself to novel
types of slicing, like you're talking about. If, for instance, the slice
object would return an iterator of indicies, you could add this case to the
indexable objects. However, the way it currently is, I don't see that being
possible. Also, the way it currently is, I don't see it working well with
all the code that implements __getitem__ as it's currently written even if
slice was implemented.

  c.  What is the best solution for masking vectors?


I'm with a decorator, you can get to at least x(4,3,3,3,2,1) returning the
type of list you want. At the same time, I'm not sure you're going to be
able to override the __getitem__ prototype without some serious pain.I've
never tried to change the function/method signature with a decorator, but
I'm pretty sure its possible at least for the non-builtin attributes.

You may want to try to write a PEP for python 3000. So much is being changed
with that, you may get it in.

 --Michael

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


Re: [Tutor] slice lists and slicing syntax questions

2007-10-13 Thread Kent Johnson
Michael Langford wrote:

 When you call [] on an object, it calls __getitem__The definition for 
 getitem is __getitem__(self,key), where key can be an integer or a slice 
 object. 

Or a tuple of integers/slices.

 You may want to try to write a PEP for python 3000. So much is being 
 changed with that, you may get it in.

The deadline for PEPs for Python 3 has long passed - it was in April IIRC.

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


Re: [Tutor] Variables in workspace

2007-10-13 Thread Kent Johnson
bob gailer wrote:
 The del statement is the way to delete variables. Since dir() gives you 
 their names one needs use eval.
 
 for varName in dir():
 eval 'del ' + varName

I think del globals()[varName] would work.

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


Re: [Tutor] Variables in workspace

2007-10-13 Thread bob gailer
Kent Johnson wrote:
 bob gailer wrote:
 The del statement is the way to delete variables. Since dir() gives 
 you their names one needs use eval.

 for varName in dir():
 eval 'del ' + varName

 I think del globals()[varName] would work.
Yep. That was nagging a corner of my brain, but not surfacing.


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


Re: [Tutor] Variables in workspace

2007-10-13 Thread Alan Gauld
Eli Brosh [EMAIL PROTECTED] wrote 

 Now, I want to know which variables are in my workspace.

Try dir()

That should give you the names in the current namespace.
You will see some names you didn't define too.

dir()is a really helpful command when you want to see whats possible.

 dir('')

Will list all the methods of a string for example.

 import math
 dir(math)

will list all the math functions and constants.

HTH,

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



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


[Tutor] upgrading Python

2007-10-13 Thread LandSurveyor
I wish to upgrade Python from the [Vers.] 2.3.4 that came packaged with my 
Mandrake 10.1 Linux OS to the current 2.5.1.  The 'hash/bang' line of my python 
scripts is #!/usr/bin/python.  There are two files, both executables, in my 
/usr/bin directory; they are 1)python, and 2)python2.3.

I just simply don't know what to do next!?  The advise I can google to is 
typically overly generous, full of contradictions (sometime within the same 
post...Well you can do this, but if you wanna do that instead...).  Well, I 
don't know why I want to do this, or do that instead.  I just want to know 
where to put my new version of python, and when I unzip/configure/and so on..., 
will I end up with:

1)an application that will pick up seamlessly and run my apps?
2)will pythontutor still be available?
3)will I have an upgraded  accessible package of modules?
4)will I need to modify the hash/bang line?
5)when I type 'python' on a command line, how will I access the new 2.5.1 
rather than the older version?  i.e., where do I go to modify the results of 
that request?

Oh, and could I-as I understand (to a limited degree) Linux- install the entire 
package within my home directory, following the principle that I am a user 
without admin privileges, and then change the hash/bang line to redirect to 
my 'embedded' version, and thus be running 2.5.1 within my own little world?  
And if I did so, the same questions persist.  If I asked for a module, would I 
get the one from my 2.5.1, or the module that exists in the older 2.3.4?  And 
when I entered 'python' from a command line (to access the interpreter) how 
would I get my 2.5.1 version, rather than the older 2.3.4.

It's just amazing, the array of niggling little questions that no one thinks 
about, but that create unmountable stumbling blocks once they pop up in the 
middle of your path, isn't it!?  But, they are enough to completely stop me in 
my tracks, and they are the nitty-gritty questions that no one seems to think 
important enough to address and clarify.

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


Re: [Tutor] upgrading Python

2007-10-13 Thread David Millar
Hi there,

I'm not familiar with Mandrake, but I use Ubuntu Linux and my package
manager does updates for me. It looks like Madrake has a package manager
called urpmi according to Wikipedia - have you tried using that to install a
new RPM package for Python 2.5.1 or simply trying to have it automatically
update for you? If you have some other package manager there may be a
different way to do it though - if you had apt-get or Synaptic I could walk
you through it that way.

Anyway, once you do have it installed, I believe that /usr/bin/python just
has the basic stuff and references everything else in the other executable
script, so you shouldn't have to change the #! line ever ever.

I hope something I said helps with Q45!

Dave

On 10/13/07, LandSurveyor [EMAIL PROTECTED] wrote:

 I wish to upgrade Python from the [Vers.] 2.3.4 that came packaged with my
 Mandrake 10.1 Linux OS to the current 2.5.1.  The 'hash/bang' line of my
 python scripts is #!/usr/bin/python.  There are two files, both
 executables, in my /usr/bin directory; they are 1)python, and 2)python2.3.

 I just simply don't know what to do next!?  The advise I can google to is
 typically overly generous, full of contradictions (sometime within the same
 post...Well you can do this, but if you wanna do that instead...).  Well,
 I don't know why I want to do this, or do that instead.  I just want to
 know where to put my new version of python, and when I unzip/configure/and
 so on..., will I end up with:

 1)an application that will pick up seamlessly and run my apps?
 2)will pythontutor still be available?
 3)will I have an upgraded  accessible package of modules?
 4)will I need to modify the hash/bang line?
 5)when I type 'python' on a command line, how will I access the new 
 2.5.1rather than the older version?
 i.e., where do I go to modify the results of that request?

 Oh, and could I-as I understand (to a limited degree) Linux- install the
 entire package within my home directory, following the principle that I am a
 user without admin privileges, and then change the hash/bang line to
 redirect to my 'embedded' version, and thus be running 2.5.1 within my own
 little world?  And if I did so, the same questions persist.  If I asked for
 a module, would I get the one from my 2.5.1, or the module that exists in
 the older 2.3.4?  And when I entered 'python' from a command line (to
 access the interpreter) how would I get my 2.5.1 version, rather than the
 older 2.3.4.

 It's just amazing, the array of niggling little questions that no one
 thinks about, but that create unmountable stumbling blocks once they pop up
 in the middle of your path, isn't it!?  But, they are enough to completely
 stop me in my tracks, and they are the nitty-gritty questions that no one
 seems to think important enough to address and clarify.

 Thanks, folks.
 ___
 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] slice lists and slicing syntax questions

2007-10-13 Thread Dave Kuhlman
On Sat, Oct 13, 2007 at 10:32:39AM -0400, Michael Langford wrote:
 On 10/12/07, [EMAIL PROTECTED]  [EMAIL PROTECTED] wrote:
 
  I have been using Python for years now, in all kinds of environments, but
  example:  x is vector of length 5, with value a,b,c,d,e , then:
 
  x[3,1,1,1,3,2] # gives [d, b, b, b, d, c]
 
  What is the python equivalent?
 
   a.  Am I understanding the situation correctly?
 
 When you call [] on an object, it calls __getitem__The definition for
 getitem is __getitem__(self,key), where key can be an integer or a slice
 object. Slice objects are either a range or a range+step. You've got the
 right picture

__getitem__() suggests a Pythonic solution: subclass list and override
__getitem__():

class subscriptlist(list):
def __getitem__(self, subscripts):
#print 'type(subscripts):', type(subscripts)
if (isinstance(subscripts, tuple) or
isinstance(subscripts, list)):
vals = []
for subscript in subscripts:
vals.append(list.__getitem__(self, subscript))
return vals
else:
val = list.__getitem__(self, subscripts)
return val

def test():
a = range(10)
b = [x * 5 for x in a]
c = subscriptlist(b)
d = c[2,5,6,8]
print 'd:', d
e = c[2]
print 'e:', e

test()

Which prints out:

d: [10, 25, 30, 40]
e: 10

Was that what you wanted?

Notice that c[2,5,6,8] results in passing a tuple to __getitem__,
because it is the comma that marks a literal representation of a
tuple.

Dave


-- 
Dave Kuhlman
http://www.rexx.com/~dkuhlman
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Variables in workspace

2007-10-13 Thread Dave Kuhlman
On Sat, Oct 13, 2007 at 11:04:05AM +0200, Eli Brosh wrote:
 
 Hello
 I am working with python interactively using IDLE.
 
 Since starting, I defined some variables:
 s='string'
 a=1
 b=[1,2]
 c=1.02
 
 and so on.
 
 Now, I want to know which variables are in my workspace.
 That is, is there a command similar to who in MATLAB ?
 I want to call who
 and get the output:
 s a b c
 (a listing of all the variables I defined in the session)
 
 Now, is there a way to clear some or all the variables in that list ?

What is your purpose?  What is your use case?

Usually, when this question or its spawn come up on this list, the
answer (and the right one, I think) is:  Do not use separate
variables, use keys in a dictionary.

And, you might think: But that's not what I asked for.  However, as
your mother might say: That's what's good for you.

Also, remember, in Python, global variables are just entries in the
dictionary returned by globals(), anyway.

Dave


-- 
Dave Kuhlman
http://www.rexx.com/~dkuhlman
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] upgrading Python

2007-10-13 Thread bhaaluu
Greetings,
On my system I can have several python versions installed because
they install as python2.3, python2.4, etc.

$ which python
/usr/bin/python
$ ls -l /usr/bin/python
... /usr/bin/python - python2.4

Here, we can see that python is just a link to /usr/bin/python2.4
So, I can install python 2.5, and simply change the link so when I
enter python at the prompt, it will start python2.5.

$ man ln
ln [OPTION]... [-T] TARGET LINK_NAME   (1st form)

So, after installing python 2.5, change to /usr/bin, remove /usr/bin/python
$ cd /usr/bin
$ rm python

And now make a new link to python2.5 (you may have to be root!):
# ln -s /usr/bin/python2.5 python

If I'm not mistaken, the library files, and so forth are stored in different
directories, so installing a new version, won't corrupt an older version.
/lib/python2.4/site-packages

See how python 2.4 is in the python2.4 directory?

If, for some reason, you want the other version, just start it with the
absolute pathname:
$ /usr/bin/python2.3

I hope this is helpful? I don't use Mandrake, so I'm unfamiliar with
its package manager, or how to do upgrades for it. The above are
generic *nix commands that should work with any distro.

apt-get and Synaptic rock! =)
-- 
b h a a l u u at g m a i l dot c o m
http://www.geocities.com/ek.bhaaluu/index.html

On 10/13/07, LandSurveyor [EMAIL PROTECTED] wrote:
 I wish to upgrade Python from the [Vers.] 2.3.4 that came packaged with my 
 Mandrake 10.1 Linux OS to the current 2.5.1.  The 'hash/bang' line of my 
 python scripts is #!/usr/bin/python.  There are two files, both 
 executables, in my /usr/bin directory; they are 1)python, and 2)python2.3.

 I just simply don't know what to do next!?  The advise I can google to is 
 typically overly generous, full of contradictions (sometime within the same 
 post...Well you can do this, but if you wanna do that instead...).  Well, I 
 don't know why I want to do this, or do that instead.  I just want to 
 know where to put my new version of python, and when I unzip/configure/and so 
 on..., will I end up with:

 1)an application that will pick up seamlessly and run my apps?
 2)will pythontutor still be available?
 3)will I have an upgraded  accessible package of modules?
 4)will I need to modify the hash/bang line?
 5)when I type 'python' on a command line, how will I access the new 2.5.1 
 rather than the older version?  i.e., where do I go to modify the results of 
 that request?

 Oh, and could I-as I understand (to a limited degree) Linux- install the 
 entire package within my home directory, following the principle that I am a 
 user without admin privileges, and then change the hash/bang line to 
 redirect to my 'embedded' version, and thus be running 2.5.1 within my own 
 little world?  And if I did so, the same questions persist.  If I asked for a 
 module, would I get the one from my 2.5.1, or the module that exists in the 
 older 2.3.4?  And when I entered 'python' from a command line (to access the 
 interpreter) how would I get my 2.5.1 version, rather than the older 2.3.4.

 It's just amazing, the array of niggling little questions that no one thinks 
 about, but that create unmountable stumbling blocks once they pop up in the 
 middle of your path, isn't it!?  But, they are enough to completely stop me 
 in my tracks, and they are the nitty-gritty questions that no one seems to 
 think important enough to address and clarify.

 Thanks, folks.
 ___
 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] File upload from python shell

2007-10-13 Thread Paulino
Hello!


How can I upload a file from python?

If it is a form to fill with values it's simple:


urlopen(http://site.com/action?key1=value1;key2=value2;) and I get the 
form filled.


What about uploading a file programmaticaly?


Thank you

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


[Tutor] [tutor] Formbuffer question in PIL

2007-10-13 Thread Varsha Purohit
Hello all,
  I need some help in using formbuffer function in PIL. Does anybody
have sample program related to this ?
thanks,
-- 
Varsha
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] [tutor] Formbuffer question in PIL

2007-10-13 Thread Varsha Purohit
oops.. i wanted to ask about frombuffer which is in image library of PIL. I
read the documentation but wanted a small sample program to understand its
function...

On 10/13/07, Varsha Purohit [EMAIL PROTECTED] wrote:

 Hello all,
   I need some help in using formbuffer function in PIL. Does anybody
 have sample program related to this ?
 thanks,
 --
 Varsha




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