Re: Ordering Products

2005-07-18 Thread Bernhard Holzmayer
Kay Schluehr wrote:

 
 Now lets drop the assumption that a and b commute. More general: let be
 M a set of expressions and X a subset of M where each element of X
 commutes with each element of M: how can a product with factors in M be
 evaluated/simplified under the condition of additional information X?
 
 It would be interesting to examine some sorting algorithms on factor
 lists with constrained item transpositions. Any suggestions?
 

Hello Kay,

take this into account:
Restrictions like commutativity, associative, distributive and flexibility
laws don't belong neither to operands nor to operators themselves.
Instead these are properties of fields (set of numbers with respect to a
certain operation).
For a famous example for a somewhat alternative behaviour look at the
Octonions (discovered in 1843 by Graves and 1845 by Cayley), which are not
associative with respect to addition and/or multiplication.
(http://en.wikipedia.org/wiki/Octonions) or the Quarternions, which are
non-commutative (http://en.wikipedia.org/wiki/Quaternion)

Obviously, it's not correct to say: addition is associative, or, that
multiplication is. With the same right, you could say, multiplication is
not associative.
With the same reasoning, we can show that it's not easy to generalize
sorting, commutation, association or distribution mechanisms.

Maybe it would be a very fascinating goal to solve your algorithmic approach
in such a limited environment like the Quarternions.
A solution for this set of numbers, if achieved in a clean, mathematically
abstract way, should hold for most other numbers/fields too, natural and
real included.

I guess that the approach might be this way:
- define/describe the fields which shall be handled
- define/describe the rules which shall be supported
- find methods to reduce sequences of operations to simple binary or unary
operations (tokens) - this may introduce brackets and stacking mechanisms
- a weighing algorithm might be necessary to distinguish between plain
numbers and place holders (variables)
- application of the distributivity (as far as possible) might help to find
a rather flat representation and a base for reordering according to the
weights of the individual sub-expressions

Nevertheless, there are lots of commercial programs which do such sort of
symbolic mathematics, and which would badly fail when it would come to such
awkward fields like Quarternions/Octonions.


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


Re: Ordering Products

2005-07-18 Thread Bernhard Holzmayer
I see, you're sensitive for the difficulties which might arise.
That's the thing I wanted to point out.
Maybe I was looking too far forward...

My first thought was to add attributes/qualifiers to the operands to improve
the sorting.
Then I realized that these attributes/qualifiers were related to the
operators, since multiplication and division use the same operands, but
while in one case it is associative and commutative, it isn't in the other.

I agree that all this leads too far.
But one thing creeps into my mind again:

I guess you'll always need an inverse operation:
A class which can handle multiplication will certainly require an inverse
operation like division.

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


Re: more newbie list questions

2005-07-14 Thread Bernhard Holzmayer
googleboy wrote:

 Hi there.
Hi googleboy!
 
 I am doing a bunch of processing over a list of lists,  and am
 interested in doing several things taht don't seem to be working for me
 just at the moment.
 
 I have a list of books with several fields (Title, Author1, Author2,
 Publisher, ISBN) in a csv.
 
 I have a cell.txt file that looks like this:
 
 ++
 The title is %title%.  brbr
 The author is %author1% %author2% brbr
 The Publisher is %publisher1% %publisher2% brbr
 The ISBN is %ISBN% brbr
 ++

This looks like a DOS-batch-file. Maybe you'd better just leave it to
DOS to populate it, just by exec-uting it  in an environment which 
has title, authort1, ... set. ??

On the other hand, if you need not relate to the cell.txt file, 
you could just use something like
 
sAuth = The author is %s % author1

 
 I know how to do something like sAuth = re.sub('%author1%', author1,
 sTemplate), but I can't figure out to populate variables within python
 from the list of fields.

I guess, that you open the csv-file and then use readline for each record.
Since csv files are ; separated (or ,), just do
recordlist = current_line.split(';')
which provides all entries from the read line in a list.
Or even better:
(title,author1,author2,publisher1,publisher2,ISBN) = current_line.split(';')
Now you can do something like
sAuth = The author is %s % recordlist[1]  in the first case, or
sAuth = The author is %s % author1in the second

 
 The second thing I am trying to figure out is how to add a field (or
 populate an empty field) called filename with the value of ISBN +
 '.txt', though I hope this becomes pretty self evident after someone
 helps me with the above.

You're certainly right about the evidence of the following:
filename=%s.txt % ISBN

 
 The last thing that is vexing me at the moment is something I am not
 sure if it is possible.  Once I have populated this filename field, I
 will want to try to do something like:
 
 for book in all_books
 open(r'd:\path\to\files\%filename%', 'a')
 some_function
 
 
 Where I replace %filename% with the value of the filename field for
 each book.  Is it possible to do some sort of search and replace like
 that over a command within python?
 
 Or is there another way entirely different to accomplish such a task,
 like maybe assigning the path/filename to a variable... ?
 
I'd try like this:
0. Initialize an empty list   all_books=[]
1. While reading in the csv file line by line, you received the ISBN
   for that line(book). (Look above the line with the split command)
   Now add this ISBN to the list by:
all_books.append(ISBN)

2. After the list is complete (all records read, file closed),
   you can do this as you assumed:
for book in all_books:
f=open(rd:\path\to\files\%s.txt % book, 'a')
some_function...
f.close()
 TIA!
 
 Googleboy
Bernhard

P.S: 
1. Looking into the Python tutorial which is available online at
www.python.org, would have answered most of your questions.

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


Re: Splitting on a word

2005-07-14 Thread Bernhard Holzmayer
[EMAIL PROTECTED] wrote:

 Hi all,
 I am writing a script to visualize (and print)
 the web references hidden in the html files as:
 'a href=web reference underlined reference/a'
 Optimizing my code, I found that an essential step is:
 splitting on a word (in this case 'href').
 
 I am asking if there is some alternative (more pythonic...):

Sure. The htmllib module provides HTMLparser.
Here's an example, run it with your HTML file as argument
and you'll see a list of all href's in the document.

#
#!/usr/bin/python
import htmllib

def test():
import sys, formatter

file = sys.argv[1]
f = open(file, 'r')
data = f.read()
f.close()

f = formatter.NullFormatter()
p = htmllib.HTMLParser(f)
p.feed(data)

for a_link in p.anchorlist:
print a_link

p.close()

test()
#

I'm sure that this is far more Pythonic!

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


Re: Splitting on a word

2005-07-14 Thread Bernhard Holzmayer
[EMAIL PROTECTED] wrote:

 Bernard... don't get angry, but I prefer the solution of Joe.  

Oh. If I got angry in such a case, I would have stopped responding to such
posts long ago 
You know the background... and you'll have to bear the consequences. ;-) 

 ... 
 for me pythonic means simple and short (I may be wrong...).

It's your definition, isn't it? 
One of the most important advantages of Python (for me!) besides its
readability is that it comes with Batteries included, which means, that I
can benefit of the work others did before, and that I can rely on its
quality.

The solution which I proposed is nothing but the test code from htmllib,
stripped down to the absolut minimum, enriched with the print command
to show the anchor list.

If I had to write production-level code of your sort, I'd take such an
off-the-shelf solution, because it minimizes the risk of failures.

Think only of such issues like these:
- does your code find a tag like A HREF= (capital letters)?
- does your code correctly handle incomplete tags like
   a href=linkadr/a or references with/without  ...?
- does it survive ill-coded html after all?

I've made the experience that it's usually better to rely on such
library code than to reinvent the wheel.

There's often a reason to take another approach.
I'd agree that a simple and short solution is fascinating.
However, every simple and short solution should be readable.
As a terrific example, here's a very tiny piece of code,
which does nothing but calculate the prime numbers up to 1000:

print filter(None,map(lambda y:y*reduce(lambda x,y:x*y!=0,
   map(lambda x,y=y:y%x,range(2,int(pow(y,0.5)+1))),1),
   range(2,1000)))

- simple (depends on your familiarity with stuff like map and lambda) 
- short (compared with different solutions) 
- and veeeyyy pythonic!

Bernhard

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


Re: Windows distribution suggestions?

2005-05-17 Thread Bernhard Holzmayer
Paul Rubin wrote:

 As what must be penance for something or other, I'm needing to release
 a Python app for use under Windows XP.  Please be gentle with me since
 I'm a Un*x weenie and the only thing I've had much practice with under
 Windows is rebooting it.

I can feel your unhappiness, and I share it. Hi Paul!
Here are some hints...

 My app contains three different programs (say alice.py, bob.py, and
 carol.py) that need to be independently launchable, and a dozen or so
 other .py files that get imported into those first three.  What I'd
 really really like is to make a single installer called (say)
 app.exe.  Launching app.exe should completely install Python, unpack
 all the necessary modules, and make three icons (alice, bob, carol) on
 the desktop.

Since your installer needs to run before Python is installed, you need
something else. Go to http://www.wisesolutions.com
You'll find a Windows installer program, with a demo-version (30days
license). Try this, it would be my best recommendation.
 
 I know there's various ways of building Windows distros like that, but
 am not sure what's currently preferred.  Gordon McMillan's site
 www.mcmillan-inc.com has had its domain expire (he really should renew
 it before some squatter grabs it!) and the mirror that I've found
 indicates that it was last updated for Python 2.3.  I wrote my app
 under 2.4 and while I don't think I depend heavily on any 2.4
 features, I'd rather not have to downgrade just to make this exe
 installer.  

You'll have to test your application on Windows, in any case. 
I've built lots of applications which run in both worlds (Linux/Windows).
Usually this works, but occasionally you'll have to deal with strange
things. One is, if you use ODBC. There's good chance that you'll have to
deal with access restrictions in the %SYSTEM% directory, unless you require
an administrator's account for your application.

If you test your app on a Windows machine, you can easily install Python 2.3
and Python 2.4 on the same machine without conflicts. Just try out, if your
app works with either. If it works with both, why not use 2.3?


 There's also py2exe--is that as good? 
Depends on your application, if that is acceptable. 
I have no experiences with that.
 
 Also, what's the preferred way of releasing updates?  That is, let's
 say I want to update my .py files and release a new version fairly
 frequently--should I just make a new .exe every time?   

Providing an installation program called Setup.exe or Install.exe which does
everything (guided by a sequence of dialog boxes where the user presses
only the return key to get through the successful installation), is
state-of-the-art. Usually, one of the Wise or InstallShield installers, are
used. Or simple WinZip-Packages.

 Would launching the new one cleanly overwrite or uninstall the old one? 

If you've done it in the proper way with the proper tool, yes.

 Total coolness would be a way to ship an update.py along with the app,
 that syncs the app up to a Subversion repository, but that may be
 asking a bit much.

Who are your customer's (the recipients of your program releases)?
Usually, you cannot rely that they have online access to your repository.
Then, they are potentionally interested in security issues and/or stability
of the program. I'd not like a program which is said to require frequent
updates - I'd guess that there's not much quality inside...

If your application has a GUI, a menu entry might fetch your update files
and place them wherever they must reside. your update.py might feed that
menu entry. Quite easy. 
If you have command line apps, why not provide an additional update.py 
and then, if you provide updates, just install them using your installed
update.py. The only thing: How do you keep track of path/desktop/file
information and how do you deal with user rights etc.?
Are you aware of how this is done under WindowsXP? It's quite different from
older Windows platforms! Therefore it might be easier, to leave this to
professional installation tools.

 
 I do have Visual C++ installed on the development machine, if that helps.

Isn't Visual C++ still coming with a restricted version of InstallShield?
If so, check if you can use that. It might give your release a sort of
professional shine...

You'll certainly find out, that managing the registry/desktop file system
will cost most energy, if you want to release your apps to WindowsXP.
Once Python is running your apps, the rest is peanuts.

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


Re: Windows distribution suggestions?

2005-05-17 Thread Bernhard Holzmayer
Paul Rubin wrote:

 Since your installer needs to run before Python is installed, you need
 something else. Go to http://www.wisesolutions.com
 You'll find a Windows installer program, with a demo-version (30days
 license). Try this, it would be my best recommendation.
 
 Hmm, ok, it looks like the paid version is around 500 USD.  Since this
 is a Windows thing I'm ok in principle with using a payware packaging
 app, but that's a bit on the expensive side.  I'll see what the
 customer says but I think they might resist.

1) Maybe you can start with the 30days demo license to find out.
2) The high price is paid for the benefit, that you won't get lost inside
the terrific installation jungle like registry, access restrictions, dlls
etc. In the end, it's your benefit, not your customer's.
If I were the customer, I'd demand a fluent installation process at no cost
and without any risk and/or conflict with other installed software. 
If I were the developer (you), I'd compare all costs/times with that price.
If you calculate one hour for let's say 25 USD, the question is: will it
save you 20 hrs. I guess so. If you have spare time, it doesn't get paid.

 The app works ok under Windows now.  
Congratulations.
 I don't use ODBC.  I might need some database connectivity later and will
probably use MySQL.  
Then you'll probably use ODBC, since the MySQL server will probably not be
run under Windows and on the same machine. 
 I don't want to need an admin account.  
Nobody wants that. It just happens...
 Right now the app just has a Tkinter gui, 
That's safe in general, and probably a good choice.
 reads and writes a few files and talks to some sockets.  
Not that trivial! Plan enough time for testing.
And, if you install without sophisticated tool, reserve enough time for
troubleshooting and modifications...
 
 The program is under active development and sprouting new features
 every few minutes.  The idea is to release a new development build once
 a week or so, for internal use by the customer.
If the customer runs it on different machines, make sure that you always
have one installation around where you work, which is exactly as the one at
the customer's site. If the customer is able to run different versions at
the same time, keep one living copy of every possible version.
Hunting bugs while you have lost the overview of the versions, will haunt
you in the end...
 
 If your application has a GUI, a menu entry might fetch your update files
 and place them wherever they must reside. your update.py might feed that
 menu entry. Quite easy.
 
 Hmm, that's another thing I'd have to code--maybe it's feasible.  I'll
 think about it.
Have a look at one of the Adobe Acrobat Reader's newest versions. They do
this. Indeed, it doesn't work on my computer, but that's the idea...

 No I'm really ignorant about Windows.  I just put the app's files in
 C:/Program Files/appname because that's what one of the other developers
 said to do.

Don't do that, despite what the others say. You may propose that, but in any
case give the installer the choice to change it.
Lots of people use different partitions which separate Windows / Programs /
Data. In other countries even Windows uses different path names (e.g. here
in Germany C:/Programme). According to Microsoft's Technical Reference,
you'd better ask the registry to find out where applications usually
reside. (That's what professional installation software does.)

I guess that neither your apps nor Python requires special folders or paths,
so don't impress this disadvantage artificially.
Vice versa: don't ask for another drive/partition, because some users have
only C:/... 

 
  I do have Visual C++ installed on the development machine, if that
  helps.
 
 Isn't Visual C++ still coming with a restricted version of InstallShield?
 If so, check if you can use that. It might give your release a sort of
 professional shine...
 
 Hmm, that's really really interesting.  How would I find out?
Check the installation. Your StartMenu should show the appropriate entry
under VisualStudio/...tools... If not, maybe you forgot to install it.
Or, it's gone away like some other precious tools...
 
 You'll certainly find out, that managing the registry/desktop file system
 will cost most energy, if you want to release your apps to WindowsXP.
 Once Python is running your apps, the rest is peanuts.
 
 Oh man, I was hoping to avoid that level of hassle.  
In contrary, that's what really makes fun. The registry is like a bomb in
your hand - it provides mighty power over the user!!!

 My app doesn't use any registry keys of its own, but I guess the 
 Python executable does, and that stuff presumably has to be updated 
 with every reinstall, 
Probably no. Since your update will certainly only affect py-files, there's
no reason to worry about the Python's registry activities.
The other thing is, that your update must know where to put the updated
files, and that information is usually kept in the registry.

Re: query progress bar

2005-05-17 Thread Bernhard Holzmayer
Timothy Smith wrote:

 i'm using pypgsql

Assuming you work with PostgreSQL, then:
You know the EXPLAIN command?

EXPLAIN will give you a very accurate estimation for the expense for the
query. 
(You'll have to find out what cost means in terms of your progress.)

I did never try this using pypgsql, but if it works, this might be your
solution...

The precision of EXPLAIN, however, depends on the modifications since the 
last time you ran VACUUM on the DB.
If the size of tables or indices didn't suffer considerable changes since
then, the estimation should be quite accurate.

Bernhard

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