newbie how do I interpret this?

2005-11-12 Thread bobueland
Here's a small session

>>> import re
>>> p=re.compile('[a-z]+')
>>> m=p.match('abb1a')
>>> dir(m)
['__copy__', '__deepcopy__', 'end', 'expand', 'group', 'groupdict',
'groups', 'span', 'start']
>>> help(m.groups)
Help on built-in function groups:

groups(...)

>>>

My question is. How do I interpret the hiven help info
groups(...)

alt1. Tough luck. There's no help. People are to busy to write
reference manuals.
alt 2. There are no parameters. The only possibility is m.groups()
alt3. Something else

Thanks Bob

-- 
http://mail.python.org/mailman/listinfo/python-list


Re: newbie how do I interpret this?

2005-11-12 Thread bobueland
Thanks for the reply

I didn't realise the meaning of ""built-in function". Also I thought
that help( ) mirrored the info in reference manual, which it obviously
does not. Thanks again for a good answer and the link.
Bob

-- 
http://mail.python.org/mailman/listinfo/python-list


newbie - How do I import automatically?

2005-11-15 Thread bobueland
When I start Python Shell I can see that some names or loaded
automatically, ready for me to use

>>> dir()
['__builtins__', '__doc__', '__name__']

I have written some functions in a file called btools.py. I would like
to import them automatically when I start up Python shell. Today I must
do it by hand like this

>>> from btools import *
>>> dir()
['__builtins__', '__doc__', '__name__', 'func1', 'func2', 'func3']
>>>

Is there a way to do this automatically so that I always have 'func1',
'func2' and so on available, without me having to do it by hand?

Bob

-- 
http://mail.python.org/mailman/listinfo/python-list


Re: newbie - How do I import automatically?

2005-11-15 Thread bobueland
I've tried as you said but it doesn't work. I'm working with Windows
XP. I right click at my computer go to Advanced, choose Environment
Variables and set PYTHONSTARTUP variable to C:\Python24\binit.py. It
looks like this

# binit.py
from btools import *

I've restarted the computer and started IDLE but I only get

IDLE 1.1.1
>>> dir()
['__builtins__', '__doc__', '__name__']
>>> 

What could be wrong?

-- 
http://mail.python.org/mailman/listinfo/python-list


Re: newbie - How do I import automatically?

2005-11-15 Thread bobueland
I also checked in command prompt, and there it works!, but not in IDLE.
And it's in IDLE that I work all the time. Can anything be done to get
it to work there?

Bob

-- 
http://mail.python.org/mailman/listinfo/python-list


newbie - Does IDLE care about sitecustomize.py?

2005-11-16 Thread bobueland
I have the following test script in the file customize.py

# C:\Python24\Lib\site-packages\sitecustomize.py
print "test text from sitecustomize"

If start Python from command prompt I get

C:\Python24>python
test in sitecustomize
Python 2.4.1 (#65, Mar 30 2005, 09:13:57) [MSC v.1310 32 bit (Intel)]
on win32
Type "help", "copyright", "credits" or "license" for more information.

which shows that sitecustomize.py works


Now if I start IDLE I only get

IDLE 1.1.1
>>>

which shows that that IDLE doesn't care about sitecustomize.py

Am I missing something or how do you customize if you are using IDLE?

-- 
http://mail.python.org/mailman/listinfo/python-list


Re: newbie - How do I import automatically?

2005-11-16 Thread bobueland
I tried to put the line

from btools import *

in several places in PyShell.py

but to now avail. It does not work, IDLE does not execute it???

Bob

-- 
http://mail.python.org/mailman/listinfo/python-list


Re: newbie - How do I import automatically?

2005-11-16 Thread bobueland
Where do I put

 def runsource(self, source):
if(source == ''):
   source = 'from btools import *'
"Extend base class method: Stuff the source in the line cache
first"
filename = self.stuffsource(source)

Do I put it in Pyshell.py or somewhere else?

Bob

-- 
http://mail.python.org/mailman/listinfo/python-list


Python Library Reference - question

2005-11-17 Thread bobueland
The "Python LIbrary Reference" at
http://docs.python.org/lib/contents.html seems to be an important
document. I have two questions

Q1. How do you search inside "Python LibraryReference" ? Does it exist
in pdf or chm form?

Q2. In some other languages (for instance PHP if I recall correctly)
readers can add comments and give examples to the various headings in
the reference manual. This gives valuable information to the reader. Is
there such a version of "Python Library Reference", and if not would it
be a good idea to have such a version.

Thanks
Bob Ueland

-- 
http://mail.python.org/mailman/listinfo/python-list


IDLE question

2005-11-17 Thread bobueland
IDLE doesn't seem to honor PYTHONSTARTUP environment variable nor
sitecustomize.py

How do you then customize in IDLE?

(basically I want to execute the statement
   from btools import *
each time I restart IDLEs Python Shell)

-- 
http://mail.python.org/mailman/listinfo/python-list


Re: newbie - How do I import automatically?

2005-11-17 Thread bobueland
I tried to do it on my computer (win XP). I put an extra line in
PyShell.py

#! /usr/bin/env python

import os
import os.path
import sys
import string
import getopt
import re
import socket
import time
import threading
import traceback
import types
import exceptions

# test
sys.modules['__main__'].__dict__['os'] = os

import linecache

Then when I start idle I get

IDLE 1.1.1
>>> dir()
['__builtins__', '__doc__', '__name__']
>>>

So I don't see 'os'

Do you see 'os' on your computer. If yes, what could be the difference?

Bob

-- 
http://mail.python.org/mailman/listinfo/python-list


Re: IDLE question

2005-11-17 Thread bobueland
Okey I tried what you recommended

My C:\Python24\sitecustomize.py looks like this:

# sitecustomize.py
import os
from btools import *

When I enter
C:\Python24>C:\Python24\pythonw.exe C:\Python24\Lib\idlelib\idle.py -r
C:\Python24\sitecustomize.py

in commad propmt IDLE starts up and automatically imports 'os' and all
names from my btools folder. So far so good. But when I hit Ctrl+F6 and
restart Python Shell both 'os' and names from btools are gone!

When I work with IDLE I typically start a new file (File/New Window)
and when I have written the code I hit (Ctrl+S) to Save and F5 to run.
This restarts Python Shell and my names from btools are gone. It's no
solution if I'm forced to go back to command prompt and enter the long
sequance again. 

Bob

-- 
http://mail.python.org/mailman/listinfo/python-list


Re: newbie - How do I import automatically?

2005-11-17 Thread bobueland
There is one C:\Python24\Lib\site-packages\sitecustomize.py but no
C:\Python24\Lib\sitecustomize.py

However I created one as you suggested but then I started IDLE nothing
showed up. Have you tested that it works on your computer?

Bob

-- 
http://mail.python.org/mailman/listinfo/python-list


Re: IDLE question

2005-11-17 Thread bobueland
I did as you suggested, however

after I make a new File (File/New Window) and save and then run (F5) I
get the following alert

The Python Shell is already executing a command; please waith until it
is finished

I also get the error message

Traceback (most recent call last):
  File "", line 1, in -toplevel-
import os; from path import path
ImportError: No module named path
>>> 

Bob

-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Python Library Reference - question

2005-11-17 Thread bobueland
I found the answer to Q1. One can download all Python docs in pdf from
http://www.python.org/doc/current/download.html

-- 
http://mail.python.org/mailman/listinfo/python-list


Re: IDLE question

2005-11-17 Thread bobueland
This works!

Thanks Claudio

For complete happiness I would also like to know what's hapening. Is
there anywhere I can read about PyShell.py.

Bob

-- 
http://mail.python.org/mailman/listinfo/python-list


Re: IDLE question

2005-11-17 Thread bobueland
I made bat file called startidle.bat which I put in C:\Python24
directory. This is how it looks

@echo off
start C:\Python24\pythonw.exe C:\Python24\Lib\idlelib\idle.py -r
C:\Python24\sitecustomize.py
exit

Now I put a shortcut of startidle.bat in Quick Launch. The only thing I
have to do now is to click on the shortcut icon in the Quick Launch and
I'm up and running. 

Alla Tua Salute Claudio

Bob

-- 
http://mail.python.org/mailman/listinfo/python-list


newbie-question about a list

2005-11-19 Thread bobueland
I've seen this construct in a script

>>> [x.capitalize() for x in ['a','b', 'c']]
['A', 'B', 'C']

I tried another myself

>>> [x+1 for x in [1,2,3]]
[2, 3, 4]
>>>

Apparently you can do
[function(x) for x in list]

I tried to find a description of this in "Library Reference" but
couldn't find it. Could somebody direct me where this type of construct
is described.

Bob

-- 
http://mail.python.org/mailman/listinfo/python-list


Re: newbie-question about a list

2005-11-19 Thread bobueland
This type of construct seems to be called "list comprehension".
Googling for

Python "list comprehension"

gives a lot of hints that describe the construct.

-- 
http://mail.python.org/mailman/listinfo/python-list


Can a function access its own name?

2005-11-19 Thread bobueland
Look at the code below:

# mystringfunctions.py

def cap(s):
print s
print "the name of this function is " + "???"

cap ("hello")


Running the code above gives the following output

>>>
hello
the name of this function is ???
>>>

I would like the output to be

>>>
hello
the name of this function is cap
>>> 

Is that possible ?

-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Can a function access its own name?

2005-11-19 Thread bobueland
Thanks Diez and Peter,

Just what I was looking for. In "Library Reference" heading

3.11.1 Types and members

I found the info about the method you described. I also made a little
function to print out not just the name of the function but also the
parameter list. Here it is

# fname.py
import sys, string

def cap(s, n):
  print string.replace("".join([sys._getframe().f_code.co_name, \
repr(sys._getframe().f_code.co_varnames)]), "\'", "")

cap('Hello', 'Bob')

Running this yields the result

cap(s, n)

-- 
http://mail.python.org/mailman/listinfo/python-list


Where can I find string.translate source?

2005-11-19 Thread bobueland
The module string has a function called translate. I tried to find the
source code for that function.  In:

C:\Python24\Lib

there is one file called

string.py

I open it and it says

"""A collection of string operations (most are no longer used).
Warning: most of the code you see here isn't normally used nowadays.
Beginning with Python 1.6, many of these functions are implemented as
methods on the standard string object. They used to be implemented by
a built-in module called strop, but strop is now obsolete itself."""

Inside the file string.py I couldn't find the source code for
translate. Where could it be?

-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Where can I find string.translate source?

2005-11-20 Thread bobueland
Thanks Mike and Fredrik. In my Python installation there is no
directory called Objects.

I use Windows and I downloaded Python from
http://www.python.org/download/

As I looked closer I saw that the link
# Python 2.4.2 Windows installer (Windows binary -- does not
include source)

which clearly says that it doesn't include source. So in order to see
the source I had to download
# Python 2.4.2 source (for Unix or OS X compile)

And in that download there is a directory called Objects and there is
file called
stringobjects.c
where one can find the implementation of translate.

-- 
http://mail.python.org/mailman/listinfo/python-list


newbie - needing direction

2005-12-04 Thread bobueland
I'm a newbie, just got through van Rossum's tutorial and I would like
to try a small project of my own. Here's the description of my project.

When the program starts a light blue semi-transparent area, size 128 by
102,  is placed in the middle of the screen. The user can move this
area with arrow the keys. When the user hits the Enter key, a magnified
picture of the chosen area is shown on the screen (10 times
magnification on a 1280 by 1024 monitor). When the user hits the Enter
key the program exits leaving the screen as it was before the program
started.

Could someone direct me what libraries and modules I should study in
order to accomplish this.

Thanks Bob

-- 
http://mail.python.org/mailman/listinfo/python-list


Re: newbie - needing direction

2005-12-04 Thread bobueland
I should maybe mention that I want to this on a win XP computer

Bob

-- 
http://mail.python.org/mailman/listinfo/python-list


Re: newbie - needing direction

2005-12-04 Thread bobueland
Thanks for the advice,
The reason for the choice of my particular test project is that it is
in the direction that I want to go in so choosing some other won't do.
I've looked briefly at PyGame but this means I have to learn a lot
besides what I want to do.

I thought that maybe my project could be accomplishied using Tkinter
(or possibly wxPython) and PIL. Am I on the wrong track, or should I
invest my time into PyGame even if I don't plan to make games?

Bob

-- 
http://mail.python.org/mailman/listinfo/python-list


double underscore attributes?

2005-12-10 Thread bobueland
Entering
>>>dir(5)

I get
['__abs__', '__add__', '__and__', '__class__', '__cmp__', '__coerce__',
'__delattr__', '__div__', '__divmod__', '__doc__', '__float__',
'__floordiv__', '__getattribute__', '__getnewargs__', '__hash__',
'__hex__', '__init__', '__int__', '__invert__', '__long__',
'__lshift__', '__mod__', '__mul__', '__neg__', '__new__',
'__nonzero__', '__oct__', '__or__', '__pos__', '__pow__', '__radd__',
'__rand__', '__rdiv__', '__rdivmod__', '__reduce__', '__reduce_ex__',
'__repr__', '__rfloordiv__', '__rlshift__', '__rmod__', '__rmul__',
'__ror__', '__rpow__', '__rrshift__', '__rshift__', '__rsub__',
'__rtruediv__', '__rxor__', '__setattr__', '__str__', '__sub__',
'__truediv__', '__xor__']

Every time I use dir(some module) I get a lot of attributes with double
underscore, for example __add__. Ok, I thought __add__ must be a method
which I can apply like this
>>> 5.__add(8)

However Python responded
SyntaxError: invalid syntax

I tried
>>> help(5.__add__)

but got
SyntaxError: invalid syntax

However when I tried with a list
>>> help([5,6].__add__)

I got
Help on method-wrapper object:

__add__ = class method-wrapper(object)
 |  Methods defined here:
 |
 |  __call__(...)
 |  x.__call__(...) <==> x(...)
 |
 |  __getattribute__(...)
 |  x.__getattribute__('name') <==> x.name

Not that I understand much of this but at least I got some response.

Now I went to Python Library Reference and searched for "__add__" but
got zero hits.

Could someone explain the use of __add__ (and similar double underscore
attributes) and what their use is.

Bob

-- 
http://mail.python.org/mailman/listinfo/python-list


how does exception mechanism work?

2005-12-12 Thread bobueland
Sometimes the best way to understand something is to understand the
mechanism behind it. Maybe that is true for exceptions. This is a model
I have right now (which probably is wrong)

1. When a runtime error occurs, some function (probably some class
method) in Python is called behind the scenes.
2. That function goes into a global table having error types in one
column and corresponding flags in another (and probably other columns
used for trace back information). It sets the flag for the error type
that occured.
3. Then the function looks if the error occured inside a try statement
(in the current function or the function that called the current
function, or the one that called that function and so on). If it did
occur inside a try statement it looks if the corresponding exception
handles the occured error type and if so it executes the statements
under the exception clause. The function also resets the type error
flag.
4. If no try statement is found in 3. (or no exception handling the
occured type error) then it is an unhandled error and the Python stops
the execution, prints an error message and resets the global type error
flag)

Is this model correct or wrong? Where can I read about the mechanism
behind exceptions?

Bob

-- 
http://mail.python.org/mailman/listinfo/python-list


newbie-name mangling?

2005-12-13 Thread bobueland
I'm reading van Rossum's tutorial. Mostly it is well written and
examples are given. However sometimes I get lost in a text, when it
doesn't give any examples and no clues. There are several examples of
this in chapter 9 about classes. Here's one from 9.6 (Private
Variables). I quote

"There is limited support for class-private identifiers. Any identifier
of the form __spam (at least two leading underscores, at most one
trailing underscore) is textually replaced with _classname__spam, where
classname is the current class name with leading underscore(s)
stripped.
This mangling is done without regard to the syntactic position of the
identifier, so it can be used to define class-private instance and
class variables, methods, variables stored in globals, and even
variables stored in instances. private to this class on instances of
other classes. ...
Outside classes, or when the class name consists of only underscores,
no mangling occurs. Name mangling is intended to give classes an easy
way to define "private" instance variables and methods, without
having to worry about instance variables defined by derived classes, or
mucking with instance variables by code outside the class. Note that
the mangling rules are designed mostly to avoid accidents;"

Could someone provide an example of the above or direct me to a page
where it is used.

Bob

-- 
http://mail.python.org/mailman/listinfo/python-list


Re: newbie-name mangling?

2005-12-13 Thread bobueland
Thanks Diez,

Everything becomes very clear from this example
Bob

-- 
http://mail.python.org/mailman/listinfo/python-list


newbie-one more example of difficulty in van Rossum's tutorial

2005-12-13 Thread bobueland
This is from 9.6 (Private variables). I quote

- "Notice that code passed to exec, eval() or evalfile() does not
consider the classname of the invoking class to be the current class;
this is similar to the effect of the global statement, the effect of
which is likewise restricted to code that is byte-compiled together.
The same restriction applies to getattr(), setattr() and delattr(), as
well as when referencing __dict__ directly."

I've read the text so far but there has been no explaination of exec,
eval() and evalfile() so far. I suspect they give ways for dynamic
execution of text strings containing Python statements.
But what does it mean that "code passed to exec, eval() or evalfile()
does not consider the classname of the invoking class to be the current
class"? What "invoking class"? What "current class".

Now there's a hint:

- "this is similar to the effect of the global statement, the effect of
which is likewise restricted to code that is byte-compiled together."

But what does this mean? So far global statement was only mention on
page 64 by the sentence

- "If a name is declared global, then all references and assignments go
directly to the middle scope containing the module's global names."

What has this to do with

- " the effect of which is likewise restricted to code that is
byte-compiled together."

At last I learn that

- "The same restriction applies to getattr(), setattr() and delattr(),
as well as when referencing __dict__ directly."

But  getattr(), setattr() and delattr() and  __dict__ has not been
mentioned as far as I recollect.

Since no examples are given I don't know what to do with this and what
the intention was putting the text above in the tutorial.. Of course I
can go to library reference and try to uncover the details there but
it's not an easy job if you are a newbie (which is why I started with
the tutorial to begin with).

Well, I don't want to complain to much about a tutorial which is rather
good but would appreciate some hint or reference which would help me to
understand the quoted text.

Bob

-- 
http://mail.python.org/mailman/listinfo/python-list


Re: newbie-name mangling?

2005-12-13 Thread bobueland
Good example Brian

It shows clearly the special role that two underscores play in a class
definition.

Thanks Bob

-- 
http://mail.python.org/mailman/listinfo/python-list


what does this mean?

2005-12-13 Thread bobueland
In van Rossum's tutorial there is a paragraph in chapter 9.6 which says

"Notice that code passed to exec, eval() or evalfile() does not
consider the classname of the invoking class to be the current class;
this is similar to the effect of the global statement, the effect of
which is likewise restricted to code that is byte-compiled together.
The same restriction applies to getattr(), setattr() and delattr(), as
well as when referencing __dict__ directly."

What does this mean? Could someone give a simple example or point to a
web page explaining this.

Bob

-- 
http://mail.python.org/mailman/listinfo/python-list


Re: newbie-one more example of difficulty in van Rossum's tutorial

2005-12-13 Thread bobueland
Thanks Brian, now I get it. BTW there is no fuzzuness in your
explanaition it is crystal clear.

Bob

-- 
http://mail.python.org/mailman/listinfo/python-list


Re: what does this mean?

2005-12-13 Thread bobueland
Brian van den Broek has answered this in the topic

newbie-one more example of difficulty in van Rossum's tutorial

-- 
http://mail.python.org/mailman/listinfo/python-list


Can you pass functions as arguments?

2005-12-18 Thread bobueland
I want to calculate f(0) + f(1) + ...+ f(100) over some function f
which I can change. So I would like to create a function taking f as
argument giving back the sum. How do you do that in Python?

-- 
http://mail.python.org/mailman/listinfo/python-list


Announcement Study Group "Essentials of Programming Languages"

2006-01-19 Thread bobueland
Since there have been some interest, a reading group has been started
at http://groups.yahoo.com/group/csg111

I must warn you that the programming language used in "Essentials of
Programming Languages" is Scheme, which is variant of Lisp. Now this
course is not a course in Scheme but about powerful programming
techniques, but Scheme is used to illustrate many points. Also it can't
harm to know a bit of Lisp. As Eric Steven Raymond said in "How To
Become A Hacker" (http://www.catb.org/~esr/faqs/hacker-howto.html):

"LISP is worth learning for a different reason - the profound
enlightenment experience you will have when you finally get it. That
experience will make you a better programmer for the rest of your days,
even if you never actually use LISP itself a lot."

Cheers Bob

--
This is a part of the Welcome mesage  given at the link above

Hello fellow programmers :)

We are starting the virtual "Principles of Programming Languages"
study group. The physical course is given at neu (short for
Northeastern University) is described on
http://www.ccs.neu.edu/course/csg111/index.html
The course is starting 17:th January 2006. We, the unfortunate ones,
which do not have the possibility to attend the physical course will
study virtually at distance. The instructor at neu is Mitchell Wand,
the co-author of "Essentials of Programming Languages" (EoPL for
short), the textbook that will be used.

The book is not free so you have to buy it :(. Edition 3 is not
available but it's OK with edition 2. It seems that some material will
be put on the neu web-site, and maybe it will be
possible to follow along without the book, but I doubt it. The textbook
is a classic.

Here's what one reader (Ravi Mohan) of the book said:

"By the time you finish the book you will have built interpreters which
demonstrate recursion, call-by-value/reference/need and name semantics,
class based and prototype based OO, type inference, continuations etc .

Very "Hands on" . You are taught how programming languages work by
actually building intrepreters (in other words an Operational Semantics
is used). This is the best way to learn .

This is an incredible book and should be part of the library of every
programmer interested in learning how languages work. As far as i know
there isn't a single other book that can do better in conveying how
various features of languages really work and interact .

While this book may not be suitable for an undergraduate course of
study (withoout an excellent teacher to help students get over the
difficult bits) it is ideal for the self taught programmer."

The reading group pace will be at least one week behind of the physical
class. We do *not* want to discuss/share homework solutions until after
the homework is due. That way students in the physical class won't be
tempted to get answers from the reading group. If the professor has a
problem with students getting answers on the Internet, then he might
stop making his course public.

-- 
http://mail.python.org/mailman/listinfo/python-list


problems with documentation

2006-01-27 Thread bobueland
I'm using standard widows xp installation of Python 2.4.2. I tried to
find some help for print and entered

>>> help()

and then chose

help> print

Sorry, topic and keyword documentation is not available because the
Python
HTML documentation files could not be found.  If you have installed
them,
please set the environment variable PYTHONDOCS to indicate their
location.

help>

I did set the environment variable PYTHONDOCS to C:\Python24\Doc (in
that folder I have Python24.chm) but got the same error. I then
downloaded html files and put them in PYTHONDOCS to C:\Python24\Doc
folder and then changed PYTHONDOCS to
C:\Python24\Doc\Python-Docs-2.4.2\doc but to no avail. What should I do
to get this to work?

Any ideas?

-- 
http://mail.python.org/mailman/listinfo/python-list


Re: problems with documentation

2006-01-27 Thread bobueland
Thanks, that works :)

-- 
http://mail.python.org/mailman/listinfo/python-list


Decoupling the version of the file from the name of the module.

2006-01-28 Thread bobueland
I'm a newbie experimenting with Python. I want to incrementally develop
a module called 'circle'. The problem is now that the file name is used
for two purposes. To keep track of the version number and as the name
for the module. So when I develop the first version of my file I have
to call it circle_a.py. The name of the module then automatically
becomes circle_a. But when I develop the next increment and call my
file circle_b.py the module name changes as well.

Basically I want to decouple the version of my file from the name of
the module.

Is there a *simple* way out of this dilemma.

-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Decoupling the version of the file from the name of the module.

2006-01-28 Thread bobueland
 Xavier Morel wrote:
> Just get rid of the version number in the name (what's the point) and
>define a __version__ attribute in the module, that's what is usually done.

Thanks Xavier, but as I said I'm newbie and I'm not sure how to do
that. Here's my module

# circle.py
from math import pi

__version__ = '1.0'

def disk(r):
"""Returns the area of the disk with radius r."""
return (pi * r**2)

def test():
print disk(1)
print disk(2)

# end of the module


Now suppose I have make a new version with __version__ = 1.1. What
shall I call this file and (I don't want to overwrite the old file if I
need to go back to it) how do I import it from the shell. Your advice
sounds nice, but I would appreciate if you could give me (or point me
to) a simple example.

Thanks

-- 
http://mail.python.org/mailman/listinfo/python-list