Re: [Tutor] TRULY NEWBIE - MENTOR NEEDED (fwd)

2005-11-24 Thread Danny Yoo


-- Forwarded message --
Date: Wed, 23 Nov 2005 21:43:27 -0400
From: mike donato <[EMAIL PROTECTED]>
To: [EMAIL PROTECTED]
Subject: Re: [Tutor] TRULY NEWBIE - MENTOR NEEDED

Thank You Danny
I changed - and didnt get an executable, not sure what is supposed to be the
output.

Mike


>From: Danny Yoo <[EMAIL PROTECTED]>
>To: mike donato <[EMAIL PROTECTED]>
>CC: tutor@python.org
>Subject: Re: [Tutor] TRULY NEWBIE - MENTOR NEEDED
>Date: Tue, 22 Nov 2005 14:46:14 -0800 (PST)
>
>
>
>On Tue, 22 Nov 2005, mike donato wrote:
>
> > Greetings, I am new student to programming and am experimenting with
> > PYTHON.  From what I have read, seems to be a very versatile language.
> > In the following excercise I am getting an error
> >
> > class String(str, Object):
>
>[class definition cut]
> >
> > Traceback (most recent call last):
> >   File "", line 11, in -toplevel-
> > class String(str, Object):
> > NameError: name 'Object' is not defined
>
>
>Hi Mike,
>
>The error is true: Python doesn't know of any class named 'Object'.  What
>you may be looking for is the 'object' class (lowercase 'o').
>
>Good luck!
>

_
Express yourself instantly with MSN Messenger! Download today it's FREE!
http://messenger.msn.click-url.com/go/onm00200471ave/direct/01/


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


Re: [Tutor] Gurus of one liner- MOVe (blah) from one token to another and REnumber tokens

2005-11-24 Thread Alan
Dear Gurus of python one liner innovator

I have about 150 lines of python extracting text from large file, the
problem I need a few lines to clean first to avoid the problem the
script is facing

Overview
There is large text and I am trying to organize it for the python script
to process, it is badly organized and I attempted to do it like this
which the master script understand

Keywords:
# is number like 1 thru 9
|H paragraphs
|F reFerence
|R Rating

BEFORE I organized by text global and replace
Each set of tokens was like this

#  paragraph
F reference
R rating

Now (where master script understand)

|H## paragraph
|F reference
|R rating

Notice no # in |F |R

PROBLEMS
Phase 1
PROBLEM 1
the |H paragraph (multi lines) has some words between () such as (xyz
blah words) also maybe in multi lines
….( blah blah
blah blah) …

We need to move it to the end of |F reference (xyz blah words)


Example
BEFORE

|H 00100 a friend in need is a friend indeed (author means both young \
and old) so select the best friend as soon as you can blah
|F Old London book
|R Cool

AFTER your process 
|H 00100 "a friend in need is a friend indeed so select the best friend
as soon as you can blah"
|F Old London book
|R Cool

PROBLEM 2
I need to find out if the order is broken so I go and fix it by hand
i.e. |H# |F |R is any other order so it is outputted in
ErrorOrderLogFile

|H# paragraph
|H paragraph
|R rating

or any order like

run new cleaning script and cat ErrorOrderLogFile 
|H00299 paragraph
|F Reference
|H Rating

|H00300 paragraph
|H paragraph
|H rating

cat ErrorOrderLogFile:
bad set orders
|H00300 paragraph


Phase II
PROBLEM 3
Once I fix by the order hand I need to renumber all from say 1 to
9
In this format

|H1 paragraph
|F1 reference
|R1 rating

|H9 paragraph
|F9 reference
|R9 rating


 

---
Outgoing mail is certified Virus Free.
Checked by AVG anti-virus system (http://www.grisoft.com).
Version: 6.0.778 / Virus Database: 525 - Release Date: 10/15/2004
 

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


Re: [Tutor] lil help please - updated (fwd)

2005-11-24 Thread Danny Yoo
[Slightly busy at the moment; can someone else help?

Alan, in the future, don't send replies directly to me: send them to the
Tutor list.  It's an ad-hoc way to load-balance your questions across all
the tutors.]


-- Forwarded message --
Date: Thu, 24 Nov 2005 03:55:00 -0600
From: Alan <[EMAIL PROTECTED]>
To: [EMAIL PROTECTED], [EMAIL PROTECTED]
Subject: RE: lil help please - updated

Sorry
Lil better english

I have about 150 lines of python extracting text from large file, the
problem I need a few lines to clean first to avoid the problem the
script is facing

Overview
There is large text and I am trying to organize it for the python script
to process, it is badly organized and I attempted to do it like this
which the master script understand

Keywords:
# is number like 1 thru 9
|H paragraphs
|F reFerence
|R Rating

BEFORE I organized by text global and replace
Each set of tokens was like this

#  paragraph
F reference
R rating

Now (where master script understand)

|H## paragraph
|F reference
|R rating

Notice no # in |F |R

PROBLEMS
Phase 1
PROBLEM 1
the |H paragraph (multi lines) has some words between () such as (xyz
blah words) also maybe in multi lines
�.( blah blah
blah blah) �

We need to move it to the end of |F reference (xyz blah words)


Example
BEFORE

|H 00100 a friend in need is a friend indeed (author means both young \
and old) so select the best friend as soon as you can blah
|F Old London book
|R Cool

AFTER your process
|H 00100 "a friend in need is a friend indeed so select the best friend
as soon as you can blah"
|F Old London book
|R Cool

PROBLEM 2
I need to find out if the order is broken so I go and fix it by hand
i.e. |H# |F |R is any other order so it is outputted in
ErrorOrderLogFile

|H# paragraph
|H paragraph
|R rating

or any order like

run new cleaning script and cat ErrorOrderLogFile
|H00299 paragraph
|F Reference
|H Rating

|H00300 paragraph
|H paragraph
|H rating

cat ErrorOrderLogFile:
bad set orders
|H00300 paragraph


Phase II
PROBLEM 3
Once I fix by the order hand I need to renumber all from say 1 to
9
In this format

|H1 paragraph
|F1 reference
|R1 rating

|H9 paragraph
|F9 reference
|R9 rating





---
Outgoing mail is certified Virus Free.
Checked by AVG anti-virus system (http://www.grisoft.com).
Version: 6.0.778 / Virus Database: 525 - Release Date: 10/15/2004


---
Outgoing mail is certified Virus Free.
Checked by AVG anti-virus system (http://www.grisoft.com).
Version: 6.0.778 / Virus Database: 525 - Release Date: 10/15/2004


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


[Tutor] getattr of functions

2005-11-24 Thread Negroup -
Hi all! I'm here again with a question about introspection.

My module stores a set of functions. I need to know, from another
script, if a particular function of this module "is enabled" (it
means, if it shall be executed by the caller script). I looked for
some introspective builtin/function, but I didn't find anything useful
(except func_globals, func_dict, func_code, etc, that don't help in my
case).

This is my solution:

mymodule.py
def f1():
  pass
def f2():
  pass

setattr(f1, 'enabled', True)
setattr(f2, 'enabled', False)

[EMAIL PROTECTED]:~/projects/erp/migra$ python
Python 2.3.5 (#1, Sep  6 2005, 12:53:27)
[GCC 3.3.4] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> from mymodule import *
>>> f1.enabled
False
>>> f2.enabled
True

This seems to work.. I wonder for better solutions.

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


Re: [Tutor] getattr of functions

2005-11-24 Thread Liam Clarke
What do you mean enabled?

If it's imported into the namespace you can call it...

Err, can you clarify on the enabling, what context are you using it
in, and what are you trying to achieve?


On 11/25/05, Negroup - <[EMAIL PROTECTED]> wrote:
> Hi all! I'm here again with a question about introspection.
>
> My module stores a set of functions. I need to know, from another
> script, if a particular function of this module "is enabled" (it
> means, if it shall be executed by the caller script). I looked for
> some introspective builtin/function, but I didn't find anything useful
> (except func_globals, func_dict, func_code, etc, that don't help in my
> case).
>
> This is my solution:
>
> mymodule.py
> def f1():
>   pass
> def f2():
>   pass
>
> setattr(f1, 'enabled', True)
> setattr(f2, 'enabled', False)
>
> [EMAIL PROTECTED]:~/projects/erp/migra$ python
> Python 2.3.5 (#1, Sep  6 2005, 12:53:27)
> [GCC 3.3.4] on linux2
> Type "help", "copyright", "credits" or "license" for more information.
> >>> from mymodule import *
> >>> f1.enabled
> False
> >>> f2.enabled
> True
>
> This seems to work.. I wonder for better solutions.
>
> Thanks you all!
> ___
> 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] getattr of functions

2005-11-24 Thread Negroup -
2005/11/24, Liam Clarke <[EMAIL PROTECTED]>:
> What do you mean enabled?
>
> If it's imported into the namespace you can call it...
>
> Err, can you clarify on the enabling, what context are you using it
> in, and what are you trying to achieve?

Hi, sorry. I'll try to present the actors omitting the details.

table.txt: the first line is an header that lists all the table's
fields. Following lines represent data;

skel.py: it is script that get headers in table.txt and write the
module mymodule.py;

mymodule.py: it is a skeleton where are defined a number of functions.
This number depends on the number of headers, and it may vary
(different table.txt files have a different number of headers, which a
different name too). Each function in the module has the name of an
header.

Finally there is another script, modifiers.py: it imports all the
functions in mymodule and applies these functions to the corresponding
fields.

AS I don't want to apply all these functions indistinctly, I need a
way to mark a function like enabled or disabled. If it is enabled, it
will be invoked. It it results disabled, it won't be called (and thus,
the corresponding field won't be modified).

I don't know if it makes sense to you, I should explain better the
whole flow.. However I don't think it is the right place.

Anyway, I hope the scenario is a bit more free from clouds!

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


[Tutor] Modifying Source Code while Program is Running

2005-11-24 Thread Ed Singleton
Is it feasible to change a program's source code whilst it is running
without having to restart the program?  Is it feasible to get a
program to change it's own source code while it is running?

For example, if you have a web server such as CherryPy that will
(hopefully) be running for months at a time and you want to be able to
change classes without having to restart the server.  Or if you want
to allow users of the site to edit a class through the web and see the
changes to the site immediately?

Can a python program change a class, change all the objects already
created by that class and save the modified class definition, so that
if the program were restarted it would return to exactly the same
state? (assuming all objects were saved to a database or somesuch).

Does anyone have any good links to implementations of this?  I assume
someone's already done it before.

Thanks

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


Re: [Tutor] getattr of functions

2005-11-24 Thread Kent Johnson
Negroup - wrote:
> Hi all! I'm here again with a question about introspection.
> 
> My module stores a set of functions. I need to know, from another
> script, if a particular function of this module "is enabled" (it
> means, if it shall be executed by the caller script). I looked for
> some introspective builtin/function, but I didn't find anything useful
> (except func_globals, func_dict, func_code, etc, that don't help in my
> case).
> 
> This is my solution:
> 
> mymodule.py
> def f1():
>   pass
> def f2():
>   pass
> 
> setattr(f1, 'enabled', True)
> setattr(f2, 'enabled', False)

This seems OK to me. You don't need setattr(), you can set the attribute 
directly:
f1.enabled = True

Adding attributes to functions is only supported in recent versions of Python 
so this is not a good solution if you need to work with older versions.

>From your later description I see that you want to do this dynamically and the 
>set of enabled functions is really a property of the client, not of the 
>function itself. You have a list of headers and you want to apply all the 
>functions that match the headers. I would look for a way to have the client 
>figure out which functions to apply. Maybe keep a list of the headers and use 
>it to filter the list of functions. 

For example if fns is the list of functions and headers is a list (or set) of 
header names matching the functions you could do
for fn in fns:
  if fn.__name__ in headers:
fn()

This keeps the responsibility for executing the correct functions with the 
client which seems like the right place to me.

Also if it matters, your solution is not thread-safe because it uses global 
state (the enabled flag in the function). If you have multiple threads doing 
this you will get errors.

Kent

-- 
http://www.kentsjohnson.com

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


Re: [Tutor] Modifying Source Code while Program is Running

2005-11-24 Thread Kent Johnson
Ed Singleton wrote:
> Is it feasible to change a program's source code whilst it is running
> without having to restart the program?  Is it feasible to get a
> program to change it's own source code while it is running?
> 
> For example, if you have a web server such as CherryPy that will
> (hopefully) be running for months at a time and you want to be able to
> change classes without having to restart the server.  Or if you want
> to allow users of the site to edit a class through the web and see the
> changes to the site immediately?

This is hard. IIRC CherryPy has a way to automatically restart the server when 
a module changes, which is not what you ask for but at least it is automatic. A 
couple of recent threads on comp.lang.python have talked about this:

http://groups.google.com/group/comp.lang.python/browse_frm/thread/8bb4efbe726c4ab5/848860f76210be69
http://groups.google.com/group/comp.lang.python/browse_frm/thread/7b34c30c5833a9b0/4ed71bb7c5a97b57

Kent
-- 
http://www.kentsjohnson.com

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


Re: [Tutor] Modifying Source Code while Program is Running

2005-11-24 Thread Ed Singleton
On 24/11/05, Kent Johnson <[EMAIL PROTECTED]> wrote:
> Ed Singleton wrote:
> > Is it feasible to change a program's source code whilst it is running
> > without having to restart the program?  Is it feasible to get a
> > program to change it's own source code while it is running?
> >
> > For example, if you have a web server such as CherryPy that will
> > (hopefully) be running for months at a time and you want to be able to
> > change classes without having to restart the server.  Or if you want
> > to allow users of the site to edit a class through the web and see the
> > changes to the site immediately?
>
> This is hard. IIRC CherryPy has a way to automatically restart the server 
> when a module changes, which is not what you ask for but at least it is 
> automatic. A couple of recent threads on comp.lang.python have talked about 
> this:
>
> http://groups.google.com/group/comp.lang.python/browse_frm/thread/8bb4efbe726c4ab5/848860f76210be69
> http://groups.google.com/group/comp.lang.python/browse_frm/thread/7b34c30c5833a9b0/4ed71bb7c5a97b57
>
> Kent

Hmmm, that's discouraging.  Do you know if it's feasible to just keep
changes to code in memory synchronised with changes nto the source
code?  So rather than reload source code, make sure that the source
code reflects what is running in memory?

For example if your program is running, and you make a change to a
class, is it possible to create the necessary class definition and
save it to a file?

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


Re: [Tutor] sort list alphabetically

2005-11-24 Thread lmac
Ok. That's the point. I think i meant case-sensitive. There are
some ways described here that will me help out.
Yes, the list is sorted when i print it out.
It was my fault, sorry guys.

Thank you a lot.

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


Re: [Tutor] Pretty XML

2005-11-24 Thread Negroup -
2005/11/23, Greg Lindstrom <[EMAIL PROTECTED]>:

> something that would read in my XML file and format it in a similar manner
> to "pretty print" so I can verify the correct information is being pulled.

>  My immediate concern is to read in an XML stream from a file and format it
> with indentation so that I can read and verify the data against out
> database.

I had the same kind of request on September. Probably you will find
useful the answers I got:
http://mail.python.org/pipermail/tutor/2005-September/041759.html

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


Re: [Tutor] Modifying Source Code while Program is Running

2005-11-24 Thread Kent Johnson
Ed Singleton wrote:
> Hmmm, that's discouraging.  Do you know if it's feasible to just keep
> changes to code in memory synchronised with changes nto the source
> code?  So rather than reload source code, make sure that the source
> code reflects what is running in memory?
> 
> For example if your program is running, and you make a change to a
> class, is it possible to create the necessary class definition and
> save it to a file?

I think that would be hard too. How would you be making the changes to the 
class in the running program?

I guess you could have a metaclass that looks for changes to a class, 
decompiles it and writes the result to a file. Of course there would be no 
comments in the result, and I don't think there is a good free Python 
decompiler available...

Kent

-- 
http://www.kentsjohnson.com

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


Re: [Tutor] Modifying Source Code while Program is Running

2005-11-24 Thread Kent Johnson
Ed Singleton wrote:
> Is it feasible to change a program's source code whilst it is running
> without having to restart the program?  Is it feasible to get a
> program to change it's own source code while it is running?

You can change a class while it is running.
> 
> For example, if you have a web server such as CherryPy that will
> (hopefully) be running for months at a time and you want to be able to
> change classes without having to restart the server.  Or if you want
> to allow users of the site to edit a class through the web and see the
> changes to the site immediately?

The auto-restart feature of CherryPy might do this for you. Also if the changes 
to the site are to a template such as Cheetah, those usually autoreload.
> 
> Can a python program change a class, change all the objects already
> created by that class and save the modified class definition, so that
> if the program were restarted it would return to exactly the same
> state? (assuming all objects were saved to a database or somesuch).

You can have persistent objects using for example SQLObject or ZODB,
> 
> Does anyone have any good links to implementations of this?  I assume
> someone's already done it before.

It sounds like maybe you come from a background in Smalltalk, or maybe you 
should look at Smalltalk. In Smalltalk the whole environment is dynamic and can 
be saved and restored easily.

For Python, I think you will do better if you narrow your requirements. Python 
is very dynamic - classes can be changed at runtime, or reloaded if you are 
careful - and there are several good ways to persist state. If you can be more 
specific about what you really need there may be a solution for you.

Kent
-- 
http://www.kentsjohnson.com

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


Re: [Tutor] lil help please - updated (fwd)

2005-11-24 Thread Chris or Leslie Smith
| I have about 150 lines of python extracting text from large file, the
| problem I need a few lines to clean first to avoid the problem the
| script is facing

Hello,

This seems like a well laid out task. If you post what you are trying and the 
problems you are encountering, that would be helpful.

One suggestion that I have is that you switch problems 1 and 2. If the ordering 
is broken (e.g. HHFR instead of HFRH) then knowing where to put the 
parenthetical comment is going to be a problem.  Also, you said that you wanted 
it put after the "F" reference did you mean that is should look like this:

| AFTER your process
|| H 00100 "a friend in need is a friend indeed so select the best
|| friend 
| as soon as you can blah"
|| F Old London book (xyz blah words)  <=== parenthetical here?
|| R Cool

It's a little hard to tell from what you've said, but it looks like the "|" was 
an unnecessary addition. If your record markers were always a single character 
at the beginning of a line, those are easy enough to find--provided there is 
never an H, F, or R that is a NON-record marker at the beginning of a line as a 
single character.

##
>>> text='''H This is the start.
... F here is a reference. 
... Right here is a non-reference R but it's not a single character starting 
the line
... so it won't be matched; and the single one in the middle isn't at the start.
... R cool'''
>>> import re
>>> text = '\n'+text #make the first one like all the others: preceded by 
>>> newline character
>>> re.findall(r'\n([HFR])\b', text)
['H', 'F', 'R']
>>> re.split(r'\n([HFR])\b', text)
['', 'H', ' This is the start.', 'F', " here is a reference. \nRight here is a 
non-reference R but it's not a single character starting the line\nso it won't 
be matched; and the single one in the middle isn't at the start.", 'R', ' cool']

##

That last list has all the groups with the identifier preceding the 
corresponding data.

Finally, I'm not sure how you are checking the correctness of the HFR sequence, 
but the findall used above suggests a way to do it:

-do the findall
-join the results together
-replace 'HFR' with '.'
-if the whole string isn't dots then there was a problem and the number of dots 
before the non-dot tell you how many correct records there were.

##
>>> bad='''
... H
... F
... R
... R
... '''
>>> re.findall(r'\n([HFR])\b', bad)
['H', 'F', 'R', 'R']
>>> ''.join(_)# the _ refers to the last output
'HFRR'
>>> _.replace('HFR', '.')
'.R'
>>> len(_),_.count('.')
(2, 1)

##

Notice that since not all the HFRs were complete, there are not all the 
characters are periods (and so the count of periods is not the same as the 
length of the string). In this case there was one correct record (thus one 
leading dot) before the problem occurred.

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


Re: [Tutor] TRULY NEWBIE - MENTOR NEEDED (fwd)

2005-11-24 Thread Danny Yoo

> I changed - and didnt get an executable, not sure what is supposed to be
> the output.

Hi Mike,

The main way to run a Python program is to run it through its interpreter.
That is, there's no need to build a separate executable: the source is the
distributable.

If you really need to make an .EXE, there are tools like py2exe that help
with this:

http://py2exe.sourceforge.net

but most people don't have to do this to play with Python.


Are you going through a tutorial such as the ones below?

http://wiki.python.org/moin/BeginnersGuide/NonProgrammers

The tutorials there may help you get started.


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

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


[Tutor] Convert integer number to binary representation

2005-11-24 Thread Alberto Troiano
Hey all

I want to know how can I convert an integer number to a binary number. 
Like this example:

+10 (decimal representationinteger) = 1010 (binary representation)

Thanks in advanced

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


Re: [Tutor] reduce with comprehension

2005-11-24 Thread Alan Gauld
Hi John,

> Everything is possible with list comprehensions!

>>> [x for z in a for y in z for x in y]
> [1, 2, 3, 31, 32, 4, 5, 6, 7, 71, 72, 8, 9]

impressive!

But in the general case can you do the same job as 
reduce in a list comprehension? ie apply an operation 
to the first two elements of a sequence and replace 
them with the result, then repeat until the list becomes 
a single value?

I cannot think of a way to do that in the generaln case 
with a comprehension. I'd be interested to see if there is a 
recipe for that.

Alan G

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


Re: [Tutor] TRULY NEWBIE - MENTOR NEEDED (fwd)

2005-11-24 Thread Alan Gauld
> I changed - and didnt get an executable, not sure what is supposed to be 
> the
> output.

I'm not sure what you expect to get, but an executable file is definitely
not going to be it...

>> > class String(str, Object):

If you inherit from str you don't need to inherit from object
since str already does.

Other than that what does happen when you try to run it?
Do you get an error message? If so, what?

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] Modifying Source Code while Program is Running

2005-11-24 Thread Alan Gauld
There are many ways of doing this, few of them very nice IMHO.

> without having to restart the program?  Is it feasible to get a
> program to change it's own source code while it is running?

Yes, you can overwrite an existing module then call reload 
from within the program.

> For example, if you have a web server such as CherryPy that will
> (hopefully) be running for months at a time and you want to be able to
> change classes without having to restart the server.  

Yes that would be feasible., preferrably in response to an admin
page where you could fill in a list of modules to be reloaded...
But you neeed to be very careful not to break any of the interfaces, 
the Liskov Substitution Principle must be strictly observed.

> to allow users of the site to edit a class through the web and 
> see the changes to the site immediately?

Extremely dangerous but the same principle would apply, 
simply load the existing module into an editor pane then save 
the modified version and reload it.

> Can a python program change a class, 

Yes as above.

> change all the objects already created by that class 

Trickier and potentially involves some low level poking that 
should not be encouraged IMHO! :-)

> and save the modified class definition, so that
> if the program were restarted it would return to exactly the same
> state? (assuming all objects were saved to a database or somesuch).

If the LSP is adhered to its feasible but I don't intend to try any 
such thing! It would be full of pitfalls and an almosyt certain recipe 
for reliability problems that would be impossible to find.
Self modifying code sounds like a good idea but there is a very 
good reason why its almost never used in production software!

HTH,

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


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


Re: [Tutor] Modifying Source Code while Program is Running

2005-11-24 Thread Alan Gauld
> For example if your program is running, and you make a change to a
> class, is it possible to create the necessary class definition and
> save it to a file?

You can do that but even if you do follow the LSP closely, you still 
can have problems, especially when you subtract functions or data 
rather than add them. Once they are gone you can't easily put them 
back if things go wrong!

There is nearly always a better solution...

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


Re: [Tutor] reduce with comprehension

2005-11-24 Thread John Fouhy
On 22/11/05, Alan Gauld <[EMAIL PROTECTED]> wrote:
> But in the general case can you do the same job as
> reduce in a list comprehension? ie apply an operation
> to the first two elements of a sequence and replace
> them with the result, then repeat until the list becomes
> a single value?

Well ... probably not.  I may have been exaggerating ever-so-slightly
in my praise of list comprehensions :-)

Although you may be able to cheat:
http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/204297

(but I'm pretty certain that this "feature" is on Guido's hitlist)

Hmm...

>>> def f(x, y):
...  return x + y
...
>>> arr = range(10)
>>> sum(arr)  # Our target
45
>>> tmp = [0]
>>> [f(x, y) for x in arr for y in [tmp[-1]] if tmp.append(f(x, y)) or True][-1]
45

Let's try some more...

>>> def f(x, y):
...  return x*y
...
>>> arr = range(1, 10)# Don't want to include 0!
>>> reduce(f, arr)   # Our target
362880
>>> tmp = [1]
>>> [f(x, y) for x in arr for y in [tmp[-1]] if tmp.append(f(x, y)) or True][-1]
362880
>>> print 'Magic!'
Magic!

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


Re: [Tutor] files - strings - lists

2005-11-24 Thread Andrzej Kolinski

  OK, I made some progress I think.
I added a few lines to Kent's script to get closer what I really am after:

==
lines = open('liga050926.sbk')  
# to get the data from a real file

#lines = iter(data)  # getting
data from a string, you don't need this when reading a file

lines.next()    # skip two
headers
lines.next()

header = lines.next().split()
hands = int(header[2])
rounds = int(header[3])
boards = hands*rounds

lines.next()


allScores = {}  # accumulate scores
into a dictionary whose key is the name

# Now we can process the names and scores
in a loop
try:    # you don't say how
you know the end of the names, I just run to the end of data
    while True:
        names =
lines.next().strip()
        player1
= names.split()[0]
        player2
= names.split()[2]
        
        lines.next()
   # skip line after name
        scores =
[ int(lines.next().split()[2]) for i in range(rounds) ]
        tScores
= 0
        for i in
scores:
           
iScore = float(i)
           
tScores = tScores + iScore

        allScores[player1]
= tScores/boards
        allScores[player2]
= tScores/boards
    
except: # no more lines
    if lines.next() == '1,1':
        pass
    
for player1, tScores in allScores.items():
    print player1, tScores
=
1.        I
singled out the players names.
2.        I
added the players scores and divided by the number of boards played.
3.        The
output contents is what I wanted:

Chrabalowski 0.875
Kowalski -0.
Kolinski 1.2916667
Bohossian 1.2916667
Stankiewicz -1.167
Cwir -0.7083 ...

4.        The
next step for me would be to read the data from more, similar files (from
2 to 10) representing different games and generate an average score for
each participating player (a player does not necessary plays each game
and I would eventually like to calculate averages of best six out of maximum
ten games). Tough! How should I start this next step? (I would like to
keep both options open: 
        final
ranking = (all tScores)/all boards), or
        final
ranking = average(RScores/boards, RScores/boards, RScores/boards, ...)
         
               
              game1
               
       game2        
               game3)

Thanks Kent, Chris and Danny. After
many, many months of using or modifying (and using) existing scripts, with
your invaluable help I feel I can write script that is original and extremely
useful to me!


        _/_/      _/     _/
   _/       _/  _/   _/
  _/_/_/_/  _/ _/
 _/      _/  _/   _/
_/     _/  _/      _/

Andrzej Kolinski






Kent Johnson <[EMAIL PROTECTED]>

Sent by: [EMAIL PROTECTED]
23/11/2005 11:03 AM




To



cc
tutor@python.org


Subject
Re: [Tutor] files - strings - lists








Andrzej Kolinski wrote:
> 
> I want to create a program that uses data from text files, makes 
> appropriate calculations and produces report. First I need to find
out 
> what is the right way to retrieve appropriate information from an
input 
> file. This is a typical format of the input file:
> 
> 1 Polonijna Liga Mistrzow        |from the heading
portion
> 26 wrzesnia 2005              
         |only
>  6 12 *6* *4* 1              
                 |'6' and
'4' will be needed
>  0 1 0
> *Bohossian* - *Kolinski*            
   |all names and
> 1                    
                   |all
scores
>       1.000 9 *13* 19          
     |(3rd column -
>       2.000 2 *4* 16          
     |'13', '4', '8', '6'
>       1.000 10 *8* 17          
     |will be needed
>       0.000 8 *6* 17          
     |
> *Szadkowska* - *Szczurek            
   *|
> 2                    
                   |same
here
>       0.000 11 *16* 20        
       |
>       3.000 1 *-4* 14          
     |
>       3.500 3 *-7* 13
>       2.500 10 *13* 19        
 
> ..
> 
>  1 1                
                     
 |skip the rest
>  1 1 1                
               |(at least for now)

It's pretty simple to make an ad-hoc reader for this data. A couple of
things you need:

- You can get individual lines from a file by treating it as an iterator.
Instead of the usual
  f = open('data.txt')
  for line in f:
you can call f.next() to get a single line. This makes it easy to skip
lines or process lines differently.

The call to f.next() will raise StopIteration when there are no more lines

- You can use split() to break a line into fields, then subscripting to
pull out the data you want:
 >>> line = '      1.000 9 13 19'
 >>> line.split()
['1.000', '9', '13', '19']
 >>> line.split()[2]
'13'
 >>> int(line.split()[2])
13


With these tools the solution is pretty simple. I pull the data from a
string but it will work with a file as well. I save the results in a dictionary
which maps name to a list of scores.

data = '''1 Polonijna Liga Mistrzow
26 wrzesnia 2005
 6 12 6 4 1
 0 1 0
Bohossian - Kolinski
1 
      1.000 9 13 19
      2.000 2 4 16
      1.00

Re: [Tutor] reduce with comprehension

2005-11-24 Thread Nick Lunt


> -Original Message-
> From: [EMAIL PROTECTED] [mailto:[EMAIL PROTECTED]
> >>> def f(x, y):
> ...  return x + y
> ...
> >>> arr = range(10)
> >>> sum(arr)  # Our target
> 45
> >>> tmp = [0]
> >>> [f(x, y) for x in arr for y in [tmp[-1]] if tmp.append(f(x, 
> y)) or True][-1]
> 45
> 
> Let's try some more...
> 
> >>> def f(x, y):
> ...  return x*y
> ...
> >>> arr = range(1, 10)# Don't want to include 0!
> >>> reduce(f, arr)   # Our target
> 362880
> >>> tmp = [1]
> >>> [f(x, y) for x in arr for y in [tmp[-1]] if tmp.append(f(x, 
> y)) or True][-1]
> 362880
> >>> print 'Magic!'
> Magic!

LOL I feel like I've accidentally signed up for the perl-tutor list ;)
 
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] reduce with comprehension

2005-11-24 Thread John Fouhy
On 25/11/05, Nick Lunt <[EMAIL PROTECTED]> wrote:
> > -Original Message-
> > >>> def f(x, y):
> > ...  return x*y
> > ...
> > >>> arr = range(1, 10)# Don't want to include 0!
> > >>> reduce(f, arr)   # Our target
> > 362880
> > >>> tmp = [1]
> > >>> [f(x, y) for x in arr for y in [tmp[-1]] if tmp.append(f(x,
> > y)) or True][-1]
> > 362880
> > >>> print 'Magic!'
> > Magic!
>
> LOL I feel like I've accidentally signed up for the perl-tutor list ;)

Oh no --- I have become that which I hate!

I must quickly go and do twenty 'import this's as penance for my sins...

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


Re: [Tutor] reduce with comprehension

2005-11-24 Thread Alan Gauld
> But in the general case can you do the same job as
> reduce in a list comprehension?
>>> def f(x, y):
...  return x + y
...
>>> tmp = [0]
>>> [f(x, y) for x in arr for y in [tmp[-1]] if tmp.append(f(x, y)) or 
>>> True][-1]
45

Let's try some more...

>>> def f(x, y):
...  return x*y
...
>>> tmp = [1]
>>> [f(x, y) for x in arr for y in [tmp[-1]] if tmp.append(f(x, y)) or 
>>> True][-1]
362880

OK, you've nearly convinced me that its possible but I think I still
prefer reduce()! :-)

Alan G.


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


Re: [Tutor] Convert integer number to binary representation

2005-11-24 Thread John Fouhy
On 25/11/05, Alberto Troiano <[EMAIL PROTECTED]> wrote:
> I want to know how can I convert an integer number to a binary number.
> Like this example:

Hi Alberto,

There's no builtin function to do this (like there is for hex or oct).
 There are a few recipes in the Cookbook on ActiveState that will do
base conversion --- here's a simple one for binary numbers that takes
advantage of the correspondence between binary and hexidecimal
representations of numbers:
http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/440528

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


Re: [Tutor] lil help please - updated (fwd)

2005-11-24 Thread Kent Johnson
> From: Alan <[EMAIL PROTECTED]>
> 
> I have about 150 lines of python extracting text from large file, the
> problem I need a few lines to clean first to avoid the problem the
> script is facing
> 
> Overview
> There is large text and I am trying to organize it for the python script
> to process, it is badly organized and I attempted to do it like this
> which the master script understand

I think I would split this into three phases:
- collect the data into groups of HFR
- process each group by rearranging, renumbering, reporting errors
- output the processed groups

One potential problem is to resynchronize to the next group when there is a 
sequence error. If there is always a blank line between groups it is easy. 
Otherwise maybe just assume an H is the start of a group.

hth,
Kent

> 
> Keywords:
> # is number like 1 thru 9
> |H paragraphs
> |F reFerence
> |R Rating
> 
> BEFORE I organized by text global and replace
> Each set of tokens was like this
> 
> #  paragraph
> F reference
> R rating
> 
> Now (where master script understand)
> 
> |H## paragraph
> |F reference
> |R rating
> 
> Notice no # in |F |R
> 
> PROBLEMS
> Phase 1
> PROBLEM 1
> the |H paragraph (multi lines) has some words between () such as (xyz
> blah words) also maybe in multi lines
> �.( blah blah
> blah blah) �
> 
> We need to move it to the end of |F reference (xyz blah words)
> 
> 
> Example
> BEFORE
> 
> |H 00100 a friend in need is a friend indeed (author means both young \
> and old) so select the best friend as soon as you can blah
> |F Old London book
> |R Cool
> 
> AFTER your process
> |H 00100 "a friend in need is a friend indeed so select the best friend
> as soon as you can blah"
> |F Old London book
> |R Cool
> 
> PROBLEM 2
> I need to find out if the order is broken so I go and fix it by hand
> i.e. |H# |F |R is any other order so it is outputted in
> ErrorOrderLogFile
> 
> |H# paragraph
> |H paragraph
> |R rating
> 
> or any order like
> 
> run new cleaning script and cat ErrorOrderLogFile
> |H00299 paragraph
> |F Reference
> |H Rating
> 
> |H00300 paragraph
> |H paragraph
> |H rating
> 
> cat ErrorOrderLogFile:
> bad set orders
> |H00300 paragraph
> 
> 
> Phase II
> PROBLEM 3
> Once I fix by the order hand I need to renumber all from say 1 to
> 9
> In this format
> 
> |H1 paragraph
> |F1 reference
> |R1 rating
> 
> |H9 paragraph
> |F9 reference
> |R9 rating
> 
> 
> 
> 
> 
> ---
> Outgoing mail is certified Virus Free.
> Checked by AVG anti-virus system (http://www.grisoft.com).
> Version: 6.0.778 / Virus Database: 525 - Release Date: 10/15/2004
> 
> 
> ---
> Outgoing mail is certified Virus Free.
> Checked by AVG anti-virus system (http://www.grisoft.com).
> Version: 6.0.778 / Virus Database: 525 - Release Date: 10/15/2004
> 
> 
> 
> 
> 
> 
> ___
> Tutor maillist  -  Tutor@python.org
> http://mail.python.org/mailman/listinfo/tutor

-- 
http://www.kentsjohnson.com

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


[Tutor] lil help please - updated (|H|F|R) (Chris or Leslie Smith) TASK1 one relocate/move

2005-11-24 Thread Alan
Smiles

Thanks a thousands 

Let us do one task at a time= task 1
relocate/move any words in |H between (...) to the end of |R rating and
before the next line of |H 

sorry the master python script wants one | before H and F and R in any
place  however some |H |F |R do not come in the beginning in error

BEFORE any process
|H 00100 "a friend in need is a friend indeed (xyz words) so select the
best 
 friend as soon as you can"
|F Old London book  
|R Cool

OR

|H 00100 "a friend in need is a friend indeed (xyz 
words) so select
 the best 
 friend as soon as you can"
|F Old London book  
|R Cool


RESULT AFTER task1 process
|H 00100 "a friend in need is a friend indeed so select the best 
 friend
 as soon as you can blah"
|F Old London book (xyz blah words)  <=== parenthetical here? 
|R Cool


Hello,

This seems like a well laid out task. If you post what you are trying
and the problems you are encountering, that would be helpful.

One suggestion that I have is that you switch problems 1 and 2. If the
ordering is broken (e.g. HHFR instead of HFRH) then knowing where to put
the parenthetical comment is going to be a problem.  Also, you said that
you wanted it put after the "F" reference did you mean that is should
look like this:

BEFORE any process
|H 00100 "a friend in need is a friend indeed (xyz blah words) so select
the best 
 friend
 as soon as you can blah"
|F Old London book  
|R Cool

AFTER your process
|H 00100 "a friend in need is a friend indeed so select the best 
 friend
 as soon as you can blah"
|F Old London book (xyz blah words)  <=== parenthetical here? 
|R Cool

It's a little hard to tell from what you've said, but it looks like the
"|" was an unnecessary addition. If your record markers were always a
single character at the beginning of a line, those are easy enough to
find--provided there is never an H, F, or R that is a NON-record marker
at the beginning of a line as a single character.

##
>>> text='''H This is the start.
... F here is a reference. 
... Right here is a non-reference R but it's not a single character
starting the line ... so it won't be matched; and the single one in the
middle isn't at the start. ... R cool'''
>>> import re
>>> text = '\n'+text #make the first one like all the others:
preceded by newline character
>>> re.findall(r'\n([HFR])\b', text)
['H', 'F', 'R']
>>> re.split(r'\n([HFR])\b', text)
['', 'H', ' This is the start.', 'F', " here is a reference. \nRight
here is a non-reference R but it's not a single character starting the
line\nso it won't be matched; and the single one in the middle isn't at
the start.", 'R', ' cool']

##

That last list has all the groups with the identifier preceding the
corresponding data.

Finally, I'm not sure how you are checking the correctness of the HFR
sequence, but the findall used above suggests a way to do it:

-do the findall
-join the results together
-replace 'HFR' with '.'
-if the whole string isn't dots then there was a problem and the number
of dots before the non-dot tell you how many correct records there were.

##
>>> bad='''
... H
... F
... R
... R
... '''
>>> re.findall(r'\n([HFR])\b', bad)
['H', 'F', 'R', 'R']
>>> ''.join(_)# the _ refers to the last output
'HFRR'
>>> _.replace('HFR', '.')
'.R'
>>> len(_),_.count('.')
(2, 1)

##

Notice that since not all the HFRs were complete, there are not all the
characters are periods (and so the count of periods is not the same as
the length of the string). In this case there was one correct record
(thus one leading dot) before the problem occurred.

/c


---
Outgoing mail is certified Virus Free.
Checked by AVG anti-virus system (http://www.grisoft.com).
Version: 6.0.778 / Virus Database: 525 - Release Date: 10/15/2004
 

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