Re: Using python for writing models: How to run models in restricted python mode?

2005-11-07 Thread Mike Meyer
vinjvinj [EMAIL PROTECTED] writes:
 1. restrict the user from doing any file io, exec, import, eval, etc. I
 was thinking of writing a plugin for pylint to do all the checks? Is
 this is a good way given that there is no restricted python. What are
 the things I should serach for in python code

Um - I've got a restricted python module: rexec.py. Of course, it
doesn't work correctly, in that it isn't really secure. Python is very
powerful, and creating a secure sandbox is difficult - so much so that
the task has never been accomplished. If you want something that will
keep the obvious things from working, rexec.py might be for you - but
don't kid yourself that it's secure. If you need real security, I'd
consider switching to Jython, which at least has a VM which was
designed with building such sandboxes as a possibility.

 2. restrict the amount of memory a module uses as well. For instance
 how can I restrict a user from doing a = range(100) or similar
 tasks so that my whole compute farm does not come down.

This is equivalent to trying to limit the amount of CPU time the
module uses, which is better known as the halting problem. There's no
algorithmic solution to that. If you want verify that some module will
only use so much memory before executing it, the best you can do is
verify that they don't do anything obvious. If you want to restrict
them while they are running, you can probably get the OS to
help. Exactly how will depend on your requirements, and the OS
involved.

Mike
-- 
Mike Meyer [EMAIL PROTECTED]  http://www.mired.org/home/mwm/
Independent WWW/Perforce/FreeBSD/Unix consultant, email for more information.
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Web automation

2005-11-07 Thread Mike Meyer
[EMAIL PROTECTED] writes:
 but I supposed the everyone knew that web automation (and in general
 automation) is only a problem in Linux.

I don't know it. I don't believe it, either. I automate web tasks on
Unix systems (I don't use many Linux systems, but it's the same tool
set) on a regular basis. In fact, I automate *lots* of tasks on Unix
systems on a regular basis, and have been doing it for decades. Most
Unix tools are very amenable to automation, much more so than I've
found either Windows or OS X tools to be.

  mike
-- 
Mike Meyer [EMAIL PROTECTED]  http://www.mired.org/home/mwm/
Independent WWW/Perforce/FreeBSD/Unix consultant, email for more information.
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Using python for writing models: How to run models in restricted python mode?

2005-11-07 Thread Mike Meyer
vinjvinj [EMAIL PROTECTED] writes:

 While I understand 2 is very hard (if not impossible) to do in single
 unix process. I'm not sure why 1 would be hard to do. Since I have
 complete control to what code I can allow or not allow on my grid. Can
 i not just search for certain strings and disallow the model if it
 fails certain conditions. It might not be 100% secure but will it not
 get me at 90%...

Sure you can search for certain strings. Python lets you build strings
dynamically, so you'd have to search for every possible way to create
those strings. Further, Python provides lots of tools for
introspection, meaning there are lots of ways to find these
forbidden objects other than mentioning their name.

You can get to *every* builtin function through any python module. For
instance, are you going to prevent them from using regular
rexpressions? If not, consider:

 getattr(re, ''.join([chr(x + 1) for x in [94, 94, 97, 116, 104, 107, 115, 
 104, 109, 114, 94, 94]]))['fi' + 'le'] is open
True
 

String searches only prevent the most obvious abuses, and may well
miss things that are merely not quite so obvious. If you think of your
security as a notice to the end user that they are doing something
wrong, as opposed to a tool that will prevent them from doing it, then
you'll have the right idea. In which case, I'd still recommend looking
into the rexec module.

 mike
-- 
Mike Meyer [EMAIL PROTECTED]  http://www.mired.org/home/mwm/
Independent WWW/Perforce/FreeBSD/Unix consultant, email for more information.
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Application monitor

2005-11-07 Thread Mike Meyer
dcrespo [EMAIL PROTECTED] writes:
 Main application starts
 This one starts the App Monitor of itself. (This means that if Main
 Application closes, the App Monitor restart it)

 And viceversa means that if App Monitor closes, the Main application
 restart it.

 How can I accomplish this?

 I've been thinking in wx.SingleInstanceChecker (from wxPython GUI
 library). If it doesn't exist, then call os.system(program_name). But
 I don't know if there's a better way.

Better depends on what your requirements are. Personally, I use init
as an app monitor. It doesn't need monitoring - if it's dead, your
Unix has a lot worse problems than one application being down.

 mike
-- 
Mike Meyer [EMAIL PROTECTED]  http://www.mired.org/home/mwm/
Independent WWW/Perforce/FreeBSD/Unix consultant, email for more information.
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Map of email origins to Python list

2005-11-07 Thread Mike Meyer
Claire McLister [EMAIL PROTECTED] writes:
 Thanks, Alan. You are absolutely right, we are not using the
 NNTP-Posting-Host header for obtaining the IP address.

Yes, but what are you using?

 The Python list is unique among the lists that we have handled so far,
 in that it has a cross-posting mechanism with a net news. Hence, it
 seems we are getting many more wrong locations here than any other
 email list maps we've done so far. We've done them for Linux kernel,
 postresql, apache, tomcat, etc. You can find them by searching their
 names in the 'find' box. Not many people reported wrong locations on
 those maps.

Hmm. Are you using a different method than you used for the mail
lists? Because my mail and news follows the same path, using the same
host name. The only difference is that my ISP uses supernews.com news
servers, so my postings appear to go direct from my domain to
supernews - but the only place this shows up is in the Path: header.

For the record - I (and my servers) are in Virginia, the domain name I
use is registered to an address in Oklahoma, and everything is relayed
through my ISP in Berkeley. Your map has me in San Francisco. Ok, you
nearly got my ISPs hardware.

 So, we'll have to go back and fix the script that is extracting the IP
 address (which is written in Python, btw). Let me know if someone is
 interested in taking a look at it and I can post it somewhere.

What IP address it is extracting? Well, if you post it, I'll look at
it and figure it out from that.

   mike
-- 
Mike Meyer [EMAIL PROTECTED]  http://www.mired.org/home/mwm/
Independent WWW/Perforce/FreeBSD/Unix consultant, email for more information.
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: [OT] Map of email origins to Python list

2005-11-07 Thread Mike Meyer
Claire McLister [EMAIL PROTECTED] writes:
 An interesting perspective. Not to increase your sense of 'creepy',
 but a lot of big corporations now have access to this kind of
 information and more.

You mean my creditors are going to be looking for me in San Francisco,
even though I'm in Virginia? Cool.

 mike
-- 
Mike Meyer [EMAIL PROTECTED]  http://www.mired.org/home/mwm/
Independent WWW/Perforce/FreeBSD/Unix consultant, email for more information.
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: how to modify code while debugging it without having to stop and then restart debugger

2005-11-07 Thread Mike Meyer
python [EMAIL PROTECTED] writes:
 i am a long time windows user and have had a great way to learn new api.

There's a better way. See below.

 to write some code and then run it.
 if there is an error, the debugger will load.
 then i can figure out what the eror is, just touch up the ocde and continue 
 to run the code.
 i do not have to stop the code, modify the code, rerun  the code.
 often an error will only happen after a complex set of conditions and not 
 have to completely stop the app is a fantastic
 way to debug.

Yup. It's been around for decades. The cooler implementations will
interpose a stage that offers you a set of proposed fixes as well as
the ability to get to the debugger.

 so how can i use python to debug code and change that code without having to 
 restart the code.

Well, as James mentioned, you can use reload to reload your
modules. But in general, this kind of thing doesn't work very well in
OO languages in general, and in Python in particular. Here's an
example of why:

 def f():
...  print Version 1
... 
 fp = f:
 def f():
...  print Version 2
... 
 f()
Version 2
 fp()
Version 1
 

I changed the function f on the fly - just like you want to - but all
the existing references to it will still refer to the *old* version of
the function(*). To do the right thing, you need to fix all the
references to the old code to refer to the new code as well. Unless
your language has some ability to capture a code reference - first
class functions, closures, or objects - that won't be a problem. But
Python ha all those things, and every one of them causes problems like
this. So you really can't continue your program from the debugger;
you need to restart it to get everything that references code to
reference the edited code.

But if the point is to learn an API, then there's something a lot
better than tweaking code inside a debugger. That's testing code
inside the interpreter. When writing web scraping software with
BeautifulSoup, writing the first guess at the scrape sequence and then
tweaking it in the debugger would be ok. But being able to get the
soup object in the interpreter, and try scrape sequences to see what
they return directly is even better. Doing it in an Emacs buffer means
I have a complete log of what I did to create the scrape, so I can
reconstruct even complicated sequences if the need arises.

If you hang out in this group long enough, you'll hear a lot about
unit testing. It's an excellent idea, and I recommend it
highly. However, it's always struck me as a very batch oriented
approach. A more interactive approach is exploratory proramming,
which is closer to what you're describing, and is appropriate when you
don't know the problem space very well, or if - as when learning a new
api - you don't know the tool set very well. Python is an excellent
tool for this. It's not exactly like what you described, but different
isn't necessarily inferior.

  mike

*) This is a feature. Classic use is:
oldfoo = foo
def foo(*args, *kwds):
# preprocess the arguments
return oldfoo(*args, *kwds)

-- 
Mike Meyer [EMAIL PROTECTED]  http://www.mired.org/home/mwm/
Independent WWW/Perforce/FreeBSD/Unix consultant, email for more information.
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Web automation

2005-11-07 Thread Mike Meyer
Paul Boddie [EMAIL PROTECTED] writes:
 Mike Meyer wrote:
 [EMAIL PROTECTED] writes:
  but I supposed the everyone knew that web automation (and in general
  automation) is only a problem in Linux.
 I don't know it. I don't believe it, either. I automate web tasks on
 Unix systems (I don't use many Linux systems, but it's the same tool
 set) on a regular basis.
 I imagine that Web automation is taken here to mean the automation of
 Web browsers

Yeah, I know. It still seems like an ass-backwards approach to
me. It's not at all clear that emulating user actions makes a sane
model for scripting. I know the non-Unix systems I've seen that did
things like that were clumsy compared to scripting interfaces that
were designed from the ground up to be scripting interfaces. But that
kind of thing varies from application to application on the systems
that support scripting.

 The problem on non-Windows systems is the lack of a common (or
 enforced) technology for exposing application object models

OS X has AppleScript. VM/CMS has Rexx. The Amiga had ARexx when MS was
still peddling DOS. Plan 9 has files. I don't think any of them are
enforced - then again, I don't think anything enforces exporting
objects from Windows applications, either. The thing that sets all
these apart from Unix is that the technology to export objects from
applications came bundled as a core part of the OS. Unix still doesn't
have that; instead it has a collection of languages that can be
embedded in the application. Lots of applications do that, meaning
they are automatable - but maybe not in the language you want to use
for the project.

Classic Unix (meaning pre-X) was automatable because programs were
expected to produce output that could be processed by other
programs. Those tools are still around and in daily use, and make task
automation on Unix a common and easy thing. It's only when you
restrict the meaning of automation to be driving a GUI app that
you run into problems.

mike
-- 
Mike Meyer [EMAIL PROTECTED]  http://www.mired.org/home/mwm/
Independent WWW/Perforce/FreeBSD/Unix consultant, email for more information.
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: problem generating rows in table

2005-11-07 Thread Mike Meyer
[EMAIL PROTECTED] writes:
 hi
 i wish to generate a table using cgi
 toprint = [('nickname', 'justme', 'someplace')]
 print '''table  border=1
 tr
 tdUser/td
 tdName/td
 tdAddress/td
 /tr
tr
   '''

 for i in range(0,len(toprint)-1):
   for j in range(0,len(toprint[0])-1):
 print td %s /td % toprint[i][j]

 print '''/tr
 /table'''

 but it only prints out a table with User | Name | address
 it didn't print out the values of toprint

 is there mistake in code? please advise 

You're not calling range right. It's designed for dealing with lists,
so range(n) returns [0, ..., n - 1], and range(to, bottom) returns
[top, ..., bottom - 1]. len(toprint) is 1, so len(toprint) - 1 is 0,
so you're calling range(0, 0), which is an empty list. So you make no
passes through the outer loop. Those two calls to range should be
range(len(toprint)) and range(len(toprint[i])) (n.b.: i, not 0, is
the index).

Of course, for is for walking elements of a list. You don't need the
indices at all. The pythonic way to write this loop would be:

for tup in toprint:
for el in tup:
print td %s /td % el

But your HTML is also broken. The loop as you have it will print one
row containing all the elements in toprint concatenated together. You
need to put each tuple in toprint into a separate row, like so:

for tup in toprint:
print tr
for el in tup:
print td %s /td % el
print /tr
print /table

and of course leave out the trailing tr in the print statement that
precedes the loop.

 mike
-- 
Mike Meyer [EMAIL PROTECTED]  http://www.mired.org/home/mwm/
Independent WWW/Perforce/FreeBSD/Unix consultant, email for more information.
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Using Which Version of Linux

2005-11-06 Thread Mike Meyer
Terry Hancock [EMAIL PROTECTED] writes:
 Similarly, I see RPMs by ones and twos all over the place,
 and only a few places with DEBs. But the DEB repositories
 are HUGE.

Try rpmfind.net. It's not clear where the rpms reside, but it's not
really important - it's a huge collection of RPMs.

   mike
-- 
Mike Meyer [EMAIL PROTECTED]  http://www.mired.org/home/mwm/
Independent WWW/Perforce/FreeBSD/Unix consultant, email for more information.
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: re sub help

2005-11-05 Thread Mike Meyer
[EMAIL PROTECTED] writes:
 i am still interested about using re, i find it useful. am still
 learning it's uses.
 so i did something like this for a start, trying to get everything in
 between [startdelim] and [enddelim]

 a =
 this\nis\na\nsentence[startdelim]this\nis\nanother[enddelim]this\nis\n

 t = re.compile(r\[startdelim\](.*)\[enddelim\])

 t.findall(a)
 but it gives me []. it's the \n that prevents the results.
 why can't (.*) work in this case? Or am i missing some steps to read
 in the \n..?
 thanks.

Newlines are magic to regular expressions. You use the flags in re to
change that. In this case, you want . to match them, so you use the
DOTALL flag:

 a = this\nis\na\nsentence[startdelim]this\nis\nanother[enddelim]this\nis\n
 t = re.compile(r\[startdelim\](.*)\[enddelim\], re.DOTALL)
 t.findall(a)
['this\nis\nanother']
 

mike
-- 
Mike Meyer [EMAIL PROTECTED]  http://www.mired.org/home/mwm/
Independent WWW/Perforce/FreeBSD/Unix consultant, email for more information.
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Using Which Version of Linux

2005-11-05 Thread Mike Meyer
blahman ([EMAIL PROTECTED]) writes:
 ok, i m going to use Linux for my Python Programs, mainly because i 
 need to see what will these fork() and exec() do. So, can anyone tell 
 me which flavour of linux i should use, some say that Debian is more 
 programmer friendly, or shold i use fedora, or Solaris. Because these 
 three are the only ones i know of that are popular and free.

You seem a bit confused. Solaris isn't a Linux distribution, it's
(System V) Unix. Linux isn't Unix - it's a Unix look-like. *BSD is
Unix, but they can't call it that for licensing reasons.

Programmer-friendly is pretty vague. Gentoo is the only Linux distro
I've run into (which excludes a *lot* of Unix distros) that I'd
consider programmer friendly, because it doesn't split packages up
into user stuff and developer stuff. That means you have to
install two packages instead of one if you want to build things
against that software. On the other hand, it uses it's own package
manager - emerge - so you can't take advantage of rpms/debs from other
systems (or you couldn't last time I looked into it). It also installs
the least amount of bundled software, which I consider a programmer
friendly behavior.

Personally, I run FreeBSD - and I like gentoo because it has a lot in
common with a BSD distribution. FreeBSD is the most popular of the
BSDs. BSDs differ from Linuxen in that a BSD distribution is an
integrated whole - the kernel and userland are maintained by the same
group, in the same repository. So the number of BSD kernels to choose
from is much greater than the number of Linux kernels, but the number
of BSD distributions is much fewer.

   mike
-- 
Mike Meyer [EMAIL PROTECTED]  http://www.mired.org/home/mwm/
Independent WWW/Perforce/FreeBSD/Unix consultant, email for more information.
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Using Which Version of Linux

2005-11-05 Thread Mike Meyer
Grant Edwards [EMAIL PROTECTED] writes:
 On 2005-11-05, Mike Meyer [EMAIL PROTECTED] wrote:
 Programmer-friendly is pretty vague. Gentoo is the only Linux distro
 I've run into (which excludes a *lot* of Unix distros) that I'd
 consider programmer friendly, because it doesn't split packages up
 into user stuff and developer stuff. That means you have to
 install two packages instead of one if you want to build things
 against that software. On the other hand, it uses it's own package
 manager - emerge - so you can't take advantage of rpms/debs from other
 systems (or you couldn't last time I looked into it). It also installs
 the least amount of bundled software, which I consider a programmer
 friendly behavior.
 I just switched one of my computers to gentoo, and I like it a
 lot.  It's very no-nonsense, but there are alot of available
 packages and everything (so far) just works.  However, it's not
 for the impatient (or at least not for the poor and impatient).
 Since it compiles packages from source, a full-featured desktop
 install on a slow machine can take days to finish.

This is one of the things I love about the *BSD systems. The package
system is two-headed. You an do pkg_add, and it'll act like yum or
apt-get, and install binaries for the package and all the
requirements for it. Or you can cd to /usr/ports/category/pkg-name
and do make install, and it will download, compile and install all
the required software and the port you're building (I do that to
change the isntalltion prefix on the packages). If you want to create
customized packages, you just do make package. I found creating a
port (and hence package) to be much easier than creating a .deb or
.rpm, but that may just be me. For real control, you can install the
portupgrade package.

That said, the author of the BSD ports system thinks the architecture
is wrong. It handles the building, installation, fetching and
requirements all by itself. He thinks the yum/apt-get approach, where
one tool handles package installation duties, and another deals with
requirements fetching is much saner.

   mike
-- 
Mike Meyer [EMAIL PROTECTED]  http://www.mired.org/home/mwm/
Independent WWW/Perforce/FreeBSD/Unix consultant, email for more information.
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Python doc problem example: gzip module (reprise)

2005-11-05 Thread Mike Meyer
Andrea Gavana [EMAIL PROTECTED] writes:
 I tend to agree with you, Rick. I usually don't like Xah's posting
 behavior, but  this time he has done, in my opinion, a constructive
 criticism (and he didn't even say *fuck* once in the whole post!!!). I am
 quite a newbie in Python, I usually deal with wxPython that is sometimes not
 very pythonic, or not enough pythonic. As a newbie (in whatever language
 you wish), I would like to open the docs about a particular library, class
 or method and find a small sample application of it. I think many newbies
 learn by examples. I don't mean to say anything negative about the docs or
 the docs' authors, but I would imagine that having a simple example of use
 for all the methods in a class/library would be a nice and valuable
 improvement for the docs.

The thing is, the library documentation that Xah Lee is complaining
about is a *reference document*. It says so right in the title:
Python Library Reference. As such, it makes lousy tutorial
documentation. Xah correctly points this out, which is akin to
pointing out that the sky is not green.

Now, if he wanted to call the PSF to task for not providing sufficient
tutorial documentation, he might have a point. On the other hand, the
PSF has limited resources, and:

 OTOH, usually by using some simple google-fu, it is easy to find a sample
 application for a particular method.

The Python Cookbook should show up a lot in this search. If other
people are providing tutorial documentation, then it's not at clear
that the PSF should be duplicating that effort. If they had infinite
resources, it's clear they should be providng the documentation Xah
Lee wants. It doesn't, so letting third parties provide tutorial
documentation is probably the best use of the limited resources that
they have available.

And, since we're talking about Mr. Lee, let's add the obligatory
request that you help google find him by description by adding this
link to your page: a href=http://xahlee.org/;stupider than spam/a

 mike
-- 
Mike Meyer [EMAIL PROTECTED]  http://www.mired.org/home/mwm/
Independent WWW/Perforce/FreeBSD/Unix consultant, email for more information.
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Python doc problem example: gzip module (reprise)

2005-11-05 Thread Mike Meyer
Paul Rubin http://[EMAIL PROTECTED] writes:
 Mike Meyer [EMAIL PROTECTED] writes:
 The thing is, the library documentation that Xah Lee is complaining
 about is a *reference document*. It says so right in the title:
 Python Library Reference. As such, it makes lousy tutorial
 documentation.
 I'm not sure which particular library Xah Lee was complaining about
 but lots of the lib docs are awful even as references.

That's true, but Xah Lee's proposed fixes do nothing to address this
problem.

 The Python Cookbook should show up a lot in this search. If other
 people are providing tutorial documentation, then it's not at clear
 that the PSF should be duplicating that effort. 
 It seems to me that since the PSF is so persnickety about code
 licenses (and that is a good thing), it should be persnickety about
 documentation licenses too.  Lots of FSF documentation projects were
 undertaken because while there were good docs in existence for
 whatever it was, there were none that the FSF could include in its
 distros.  It's similarly not so great if Python users have to rely on
 proprietary docs.  Of course the PSF has to prioritize its tasks and
 some things will necessarily stay far down on the list for quite a
 while, but they should at least BE on the list.

To my knowledge the PSF isn't doing anything about including the
documentation with their distribution, so they shouldn't care about
the licenses. Wanting to bundle a good tutorial for everything in the
library might be on the list, but the licenses on third-party
tutorials shouldn't matter until you are considering bundling them. In
that light, the only major omission I can think of is Tkinter, as the
only good docs - tutorial, reference, or otherwise - is the Grayson's
book.

mike
-- 
Mike Meyer [EMAIL PROTECTED]  http://www.mired.org/home/mwm/
Independent WWW/Perforce/FreeBSD/Unix consultant, email for more information.
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Python doc problem example: gzip module (reprise)

2005-11-05 Thread Mike Meyer
Paul Rubin http://[EMAIL PROTECTED] writes:

 To my knowledge the PSF isn't doing anything about including the
 documentation with their distribution, so they shouldn't care about
 the licenses. Wanting to bundle a good tutorial for everything in
 the library might be on the list, but the licenses on third-party
 tutorials shouldn't matter until you are considering bundling them. 
 It's only -because- of those licenses that there's any reason not to
 bundle.

Actually, there are other reasons, just as there are reasons besides
licensing for not simply including third party libraries into the
standard library. Most important is the issue of maintenance: is
someone going to commit to keeping an added document up to date with
the distribution? Bundling out of date documentation is a bad
thing. Bundling documentation that is going to be left out of the next
release because nobody updated it isn't much better. The author of the
documentation is the logical person to do this, but if they wanted the
documentation bundled with Python, they probably would have submitted
it as a PR (I know that's what I do for OSS projects).

 In that light, the only major omission I can think of is Tkinter, as
 the only good docs - tutorial, reference, or otherwise - is the
 Grayson's book.
 I found 
   http://infohost.nmt.edu/tcc/help/lang/python/tkinter.html
 to be a pretty good tutorial, though incomplete as a reference.

Thanks for the URL, but that's just a short list of links, most of
which I've already seen.

  mike
-- 
Mike Meyer [EMAIL PROTECTED]  http://www.mired.org/home/mwm/
Independent WWW/Perforce/FreeBSD/Unix consultant, email for more information.
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Python doc problem example: gzip module (reprise)

2005-11-05 Thread Mike Meyer
Paul Rubin http://[EMAIL PROTECTED] writes:
 Mike Meyer [EMAIL PROTECTED] writes:
  It's only -because- of those licenses that there's any reason not to
  bundle.
 Actually, there are other reasons, just as there are reasons besides
 licensing for not simply including third party libraries into the
 standard library.

 I'm not talking about 3rd party libraries, I'm talking about 3rd party
 documentation for modules that are already in the Python standard
 library.

So am I. My point is that many of the considerations as to why you
don't simply include a module in the library also apply when it comes
to including documentation in the distribution. I gave some examples,
including why they were important for *documentation*, but you
carefully elided those.

 For example, if someone wrote a good Tkinter manual that
 were licensed in a way that the PSF could drop it into the Python
 distro, then PSF should certainly consider including it.  The same
 goes for good docs about urllib2, or various other modules that
 currently have lousy docs.

The key word is consider. They have to deal with all the issues I
pointed out before, of which licensing is just the beginning.

 Sorry, I meant:
   http://infohost.nmt.edu/tcc/help/pubs/tkinter/ (html)
   http://www.nmt.edu/tcc/help/pubs/tkinter.pdf   (pdf of same)
 You've probably seen this manual already.

Yes, I have. I still say the only good documentation is Grayson's
book.

mike
-- 
Mike Meyer [EMAIL PROTECTED]  http://www.mired.org/home/mwm/
Independent WWW/Perforce/FreeBSD/Unix consultant, email for more information.
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Class Variable Access and Assignment

2005-11-04 Thread Mike Meyer
Antoon Pardon [EMAIL PROTECTED] writes:
 Op 2005-11-03, Mike Meyer schreef [EMAIL PROTECTED]:
 Antoon Pardon [EMAIL PROTECTED] writes:
 What would you expect to get if you wrote b.a = b.a + 2?
 I would expect a result consistent with the fact that both times
 b.a would refer to the same object.
 Except they *don't*. This happens in any language that resolves
 references at run time.
 Python doesn't resolve references at run time. If it did the following
 should work.

You left out a key word: all.

 a = 1
 def f():
   a = a + 1

 f()

If Python didn't resolve references at run time, the following
wouldn't work:

 def f():
...  global a
...  a = a + 1
... 
 a = 1
 f()
 

 But letting that aside. There is still a difference between resolving
 reference at run time and having the same reference resolved twice
 with each resolution a different result.

The second is a direct result of the first. The environment can change
between the references, so they resolve to different results.





 Changing that would be changing a fundamental
 - and *important* - feature of Python. Arbitrary restrictions to
 prevent a single case of this from doing something people who aren't
 used to suvh behavior are kludges, and would constitute a wart on the
 language, pure and simple.
 Python already has its warts. If you want to argue that fixing this
 would make a bigger wart then the wart it is now. Fine I will accept
 that.

I've already argued that the kludges suggested to solve this problem
create worse problems than this. This is a simple case of something
being unexpected to those used to less dynamic languages. The other
solutions break useful functionality, and require adding special cases
to the language - which aren't special enough to break the rules.

mike
-- 
Mike Meyer [EMAIL PROTECTED]  http://www.mired.org/home/mwm/
Independent WWW/Perforce/FreeBSD/Unix consultant, email for more information.
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Class Variable Access and Assignment

2005-11-04 Thread Mike Meyer

 Would it be too much to ask that in a line like.
 
   x = x + 1.
 
 both x's would resolve to the same namespace?

Yes. That's to much bondage for programmers who've become accustomed
to freedom. Explain why this should be illegal:

 class C:
...  def __getattr__(self, name):
...   x = 1
...   return locals()[name]
...  def __setattr__(self, name, value):
...   globals()[name] = value
... 
 o = C()
 o.x = o.x + 1
 x
2
 


mike
-- 
Mike Meyer [EMAIL PROTECTED]  http://www.mired.org/home/mwm/
Independent WWW/Perforce/FreeBSD/Unix consultant, email for more information.
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Class Variable Access and Assignment

2005-11-04 Thread Mike Meyer
Paul Rubin http://[EMAIL PROTECTED] writes:
 Mike Meyer [EMAIL PROTECTED] writes:
 I've already argued that the kludges suggested to solve this problem
 create worse problems than this.
 The most obvious solution is to permit (or even require) the
 programmer to list the instance variables as part of the class
 definition.  Anything not in the list is not an instance variable,
 i.e. they don't get created dynamically.  That's what most other
 languages I can think of do.  Some Python users incorrectly think this
 is what __slots__ does, and try to use __slots__ that way.  That they
 try to do that suggests that the approach makes some sense.

That breaks the ability to add attributes dynamically, which is
usefull. If you need an extra piece of data with some existing class,
it's much easier to just add an attribute to hold it than to create a
subclass for the sole purpose of adding that attribute.

  mike
-- 
Mike Meyer [EMAIL PROTECTED]  http://www.mired.org/home/mwm/
Independent WWW/Perforce/FreeBSD/Unix consultant, email for more information.
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Class Variable Access and Assignment

2005-11-04 Thread Mike Meyer
Antoon Pardon [EMAIL PROTECTED] writes:

 Op 2005-11-04, Mike Meyer schreef [EMAIL PROTECTED]:

 Would it be too much to ask that in a line like.
 
   x = x + 1.
 
 both x's would resolve to the same namespace?

 Yes. That's to much bondage for programmers who've become accustomed
 to freedom. Explain why this should be illegal:

 class C:
 ...  def __getattr__(self, name):
 ...   x = 1
 ...   return locals()[name]
 ...  def __setattr__(self, name, value):
 ...   globals()[name] = value
 ... 
 o = C()
 o.x = o.x + 1
 x
 2

 I'll answer with a contra question.

 Please explain why this is illegal.

 x = 1
 def f():
   x += 1

 f()

It isn't illegal, it just requires a different syntax.

   mike
-- 
Mike Meyer [EMAIL PROTECTED]  http://www.mired.org/home/mwm/
Independent WWW/Perforce/FreeBSD/Unix consultant, email for more information.
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Class Variable Access and Assignment

2005-11-04 Thread Mike Meyer
Antoon Pardon [EMAIL PROTECTED] writes:
 Op 2005-11-04, Mike Meyer schreef [EMAIL PROTECTED]:
 Antoon Pardon [EMAIL PROTECTED] writes:
 Op 2005-11-03, Mike Meyer schreef [EMAIL PROTECTED]:
 Antoon Pardon [EMAIL PROTECTED] writes:
 What would you expect to get if you wrote b.a = b.a + 2?
 I would expect a result consistent with the fact that both times
 b.a would refer to the same object.
 Except they *don't*. This happens in any language that resolves
 references at run time.
 Python doesn't resolve references at run time. If it did the following
 should work.

 You left out a key word: all.

 a = 1
 def f():
   a = a + 1

 f()

 If Python didn't resolve references at run time, the following
 wouldn't work:

 def f():
 ...  global a
 ...  a = a + 1
 ... 
 a = 1
 f()
 

 Why do you think so? I see nothing here that couldn't work with
 a reference resolved during compile time.

a - in the global name space - doedn't exist when f is compiled, and
hence can't be dereferenced at compile time. Of course, sufficiently
advanced analysis can figure out that a would exist before f is run,
but that's true no matter how a is added. That isn't the way python
works.

 But letting that aside. There is still a difference between resolving
 reference at run time and having the same reference resolved twice
 with each resolution a different result.
 The second is a direct result of the first. The environment can change
 between the references, so they resolve to different results.
 No the second is not a direct result of the first. Since there is
 only one reference, I see nothing wrong with the environment
 remebering the reference and reusing it if it needs the reference
 a second time.

Please stay on topic: we're talking about a = a + 1, not a += 1.
The former has two references, not one. I've already agreed that the
semantics of += are a wart.

 mike
-- 
Mike Meyer [EMAIL PROTECTED]  http://www.mired.org/home/mwm/
Independent WWW/Perforce/FreeBSD/Unix consultant, email for more information.
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Class Variable Access and Assignment

2005-11-04 Thread Mike Meyer
Steven D'Aprano [EMAIL PROTECTED] writes:
 equal? Some things are a matter of objective fact: should CPython use a
 byte-code compiler and virtual machine, or a 1970s style interpreter that
 interprets the source code directly?

For the record, I've only seen one interpreter that actually
interpreted the source directly. Pretty much all of the rest of them
do a lexical analysis, turning keywords into magic tokens (dare I say
byte codes) and removing as much white space as possible. Or maybe
that's what you meant?

   mike
-- 
Mike Meyer [EMAIL PROTECTED]  http://www.mired.org/home/mwm/
Independent WWW/Perforce/FreeBSD/Unix consultant, email for more information.
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Class Variable Access and Assignment

2005-11-04 Thread Mike Meyer
[EMAIL PROTECTED] (Bengt Richter) writes:
 On Thu, 03 Nov 2005 13:37:08 -0500, Mike Meyer [EMAIL PROTECTED] wrote:
 [...]
 I think it even less sane, if the same occurce of b.a refers to two
 different objects, like in b.a += 2

That's a wart in +=, nothing less. The fix to that is to remove +=
from the language, but it's a bit late for that.

 Hm, the fix? Why wouldn't e.g. treating augassign as shorthand for a source 
 transformation
 (i.e., asstgt op= expr  becomes by simple text substitution asstgt = asstgt 
 op expr)
 be as good a fix? Then we could discuss what

 b.a = b.a + 2

 should mean ;-)

The problem with += is how it behaves, not how you treat it. But you
can't treat it as a simple text substitution, because that would imply
that asstgt gets evaluated twice, which doesn't happen.

 OTOH, we could discuss how you can confuse yourself with the results of b.a 
 += 2
 after defining a class variable a as an instance of a class defining 
 __iadd__ ;-)

You may confuse yourself that way, I don't have any problems with it
per se.

 Or point out that you can define descriptors (or use property to make it easy)
 to control what happens, pretty much in as much detail as you can describe 
 requirements ;-)

I've already pointed that out.

 mike
-- 
Mike Meyer [EMAIL PROTECTED]  http://www.mired.org/home/mwm/
Independent WWW/Perforce/FreeBSD/Unix consultant, email for more information.
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Class Variable Access and Assignment

2005-11-04 Thread Mike Meyer
[EMAIL PROTECTED] (Bengt Richter) writes:
 On 04 Nov 2005 17:53:34 -0800, Paul Rubin http://[EMAIL PROTECTED] wrote:
[EMAIL PROTECTED] (Bengt Richter) writes:
 Hm, the fix? Why wouldn't e.g. treating augassign as shorthand for
 a source transformation (i.e., asstgt op= expr becomes by simple
 text substitution asstgt = asstgt op expr) be as good a fix? Then
 we could discuss what

Consider a[f()] += 3.  You don't want to eval f() twice.

 Well, if you accepted macro semantics IWT you _would_ want to ;-)

Another one of those throw-away lines.

I'd say that was true only if the macro was poorly written. Unless a
macros is intended as a tool to repeatedly evaluate an argument, it
should only evaluate it at most once.

Of course, if you're using some rock-stupid textual macro system, you
really don't have much choice in the matter.

   mike
-- 
Mike Meyer [EMAIL PROTECTED]  http://www.mired.org/home/mwm/
Independent WWW/Perforce/FreeBSD/Unix consultant, email for more information.
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Class Variable Access and Assignment

2005-11-04 Thread Mike Meyer
Paul Rubin http://[EMAIL PROTECTED] writes:
 Steven D'Aprano [EMAIL PROTECTED] writes:
 It also allows you to do something like this:
 class ExpertGame(Game):
 current_level = 100

 and then use ExpertGame anywhere you would have used Game with no problems.
 Well, let's say you set, hmm, current_score = 100 instead of current_level.
 Scores in some games can get pretty large as you get to the higher
 levels, enough so that you start needing long ints, which maybe are
 used elsewhere in your game too, like for the cryptographic signatures
 that authenticate the pieces of treasure in the dungeon.  Next you get
 some performance gain by using gmpy to handle the long int arithmetic,
 and guess what?  Eventually a version of your game comes along that
 enables the postulated (but not yet implemented) mutable int feature
 of gmpy for yet more performance gains.  So now, current_score += 3000
 increments the class variable instead of creating an instance
 variable, and whoever maintains your code by then now has a very weird
 bug to track down and fix.

I'd say that's a wart with +=, not with Python's inheritance
mechanisms. += is neither + nor =, but takes on different aspects of
each depending on what it's operating on. While it's true that python
is dynamic enough that the you can create classes that make this true
for any operator, += is the only one that acts like that on the
builtin types.

mike
-- 
Mike Meyer [EMAIL PROTECTED]  http://www.mired.org/home/mwm/
Independent WWW/Perforce/FreeBSD/Unix consultant, email for more information.
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: re sub help

2005-11-04 Thread Mike Meyer
[EMAIL PROTECTED] writes:

 hi

 i have a string :
 a =
 this\nis\na\nsentence[startdelim]this\nis\nanother[enddelim]this\nis\n

 inside the string, there are \n. I don't want to substitute the '\n'
 in between
 the [startdelim] and [enddelim] to ''. I only want to get rid of the
 '\n' everywhere else.

Well, I'm not an expert on re's - I've only been using them for three
decades - but I'm not sure this can be done with a single re, as the
pattern you're interested in depends on context, and re's don't handle
that well.

On the

-- 
Mike Meyer [EMAIL PROTECTED]  http://www.mired.org/home/mwm/
Independent WWW/Perforce/FreeBSD/Unix consultant, email for more information.
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: re sub help

2005-11-04 Thread Mike Meyer
[EMAIL PROTECTED] writes:

 hi

 i have a string :
 a =
 this\nis\na\nsentence[startdelim]this\nis\nanother[enddelim]this\nis\n

 inside the string, there are \n. I don't want to substitute the '\n'
 in between
 the [startdelim] and [enddelim] to ''. I only want to get rid of the
 '\n' everywhere else.

Well, I'm not an expert on re's - I've only been using them for three
decades - but I'm not sure this can be done with a single re, as the
pattern you're interested in depends on context, and re's don't handle
that well.

On the other hand, this is fairly straightforward with simple string
operations:

 a = this\nis\na\nsentence[startdelim]this\nis\nanother[enddelim]this\nis\n
 sd = '[startdelim]'
 ed = '[enddelim]'
 s, r = a.split(sd, 1)
 m, e = r.split(ed, 1)
 a = s + sd + m.replace('\n', '') + ed + e
 a
'this\nis\na\nsentence[startdelim]thisisanother[enddelim]this\nis\n'
 

mike

-- 
Mike Meyer [EMAIL PROTECTED]  http://www.mired.org/home/mwm/
Independent WWW/Perforce/FreeBSD/Unix consultant, email for more information.
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Getting a function name from string

2005-11-03 Thread Mike Meyer
[EMAIL PROTECTED] (Bengt Richter) writes:
For a lot of uses, it'd be better to build the dictionary by hand
rather than relying on one of the tools that turns a namespace into a
dictionary.
 IMO it would be nice if name lookup were as cleanly
 controllable and defined as attribute/method lookup.
 Is it a topic for py3k?

I'm not sure what you mean by cleanly controllable and defined. I'd
say name lookup is well defined and clean, as it's the same as it is
in pretty much any language that supports nested scopes, modulo
assignment creating things in the local scope. If you're talking about
letting users control that mechanism, that's definitely py3k material,
if not python-like-language-that's-not-python material. On the other
hand, if you just want to provide tools that let users do name lookups
the way they can do attribute lookups, that's easy, and we could add
that now:

 getname(name) - returns the object name is bound to.
 setname(name, value) - binds the variable name.

And maybe you want an optional third argument of a callable, which
causes the functions to work on names starting in the callables name
space.

The real question is (as always) - what's the use case? In particular,
what's the use case that we really want to encourage? As I said,
you're usually better off separating dynamically built names into
their own dictionary rather than trying to plug them into - or pull
them out of - one of the language namespaces. If you plug them in,
there's no way to get them back out except by mechanisms similar to
how you got them in, so you might as well let those mechanisms include
a dictionary. If you pull it out, you might as well have referenced
the bare name rather than the string. If you constructed the name in
some way, then changing the constructor to do a dictionary lookup
should be straightforward. All of these technics avoid problems that
come from overloading a language namespace.

Of course, I'm not omniscient, so you may have something that doesn't
fit those cases in mind - in which case, let's hear about it.

You can even arrange to run python code with your dictionary as a
namespace. Following are excerpts from a tool I'm currently not
working on.

class P:
def __init__(self):
self.variables = dict(In = self.set_input, Out = self.set_output,
  Labels = self.set_labels, Float = DoubleVar,
  String = StringVar, Int = IntVar)

def calculate(self):
for name, var in self.inputs.items():
self.variables[name] = var.get()
self.variables['calculate']()
for name, var in self.outputs.items():
var.set(self.variables[name])

def run(self):
self.app.mainloop(self.calculate)


def main(args):
app = P()
execfile(argv[1], app.variables)
app.run()

Here I create a dictionnary - P().variables - and populate it with
names and their values. I then run the code of interest with that
dictionary as the global namespace. The code will add things to the
namespace - in particular, calculate. I then run a loop which will,
at unspecified times, set names in the P().variables dictionary, run
the calculate callable created by the code of interest, then copy
values out of the dictionary. This works ilke a charm. If you don't
want to deal with entire files, you can use exec, or even eval if you
just want to get a value back.

  mike
-- 
Mike Meyer [EMAIL PROTECTED]  http://www.mired.org/home/mwm/
Independent WWW/Perforce/FreeBSD/Unix consultant, email for more information.
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Class Variable Access and Assignment

2005-11-03 Thread Mike Meyer
Antoon Pardon [EMAIL PROTECTED] writes:
 What would you expect to get if you wrote b.a = b.a + 2?
 I would expect a result consistent with the fact that both times
 b.a would refer to the same object.

Except they *don't*. This happens in any language that resolves
references at run time. Changing that would be changing a fundamental
- and *important* - feature of Python. Arbitrary restrictions to
prevent a single case of this from doing something people who aren't
used to suvh behavior are kludges, and would constitute a wart on the
language, pure and simple.

If you think this is bad, you should consider Jensen's device. It uses
call-by-name, which Python doesn't have. This defers evauation of an
argument until the actual use of the argument. You can fake it in
python by passing strings that you eval and/or lambdas that you call -
I'm not going to work out the details.

Here's the function:

real procedure SIGMA(x, i, n);
   value n;
   real x; integer i, n;
begin
   real s;
   s := 0;
   for i := 1 step 1 until n do
  s := s + x;
   SIGMA := s;
end

The object referred to by x in the body of the loop is different
*every pass through the loop*, at least in the expected usage. That
usage is SIGMA(a(i), i, 10). Call-by-name evaluates a(i) when x is
mentioned. Since i changes with each pass through the loop, a(i) is a
different element in the array. So SIGMA(a(i), i, 10) is sum(a[:10])
(give or take). But SIGMA(a(i) * b(i), i, 10) is sum([a(i) * b(i) for
i in xrange(10)]) (more or less).

This isn't insane, it's a feature. It's a *very powerful*
feature. Yes, it causes behavior that's unexpected and appears to be
wrong to people who aren't used to resolving names at run time. They
need to get used to it. Then they can start taking advantage of it.

 I think it even less sane, if the same occurce of b.a refers to two
 different objects, like in b.a += 2

That's a wart in +=, nothing less. The fix to that is to remove +=
from the language, but it's a bit late for that.

 mike
-- 
Mike Meyer [EMAIL PROTECTED]  http://www.mired.org/home/mwm/
Independent WWW/Perforce/FreeBSD/Unix consultant, email for more information.
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Can Anyone Help me on this

2005-11-03 Thread Mike Meyer
[EMAIL PROTECTED] writes:
 If what you want is a reversed copy, you could just append list1
 elements to list2, and use the reverse function such as
 ...
 for i in list1:
 ...   list2.append(i)
 ...

Don't do this by ahnd - let python do it for you:

list2 = list(list1)

or

list2 = list1[:]

  mike

 list2.reverse()
 list1
 ['1', '2', '3', '4', '5', '6', '7', '8', '9']
 list2
 ['9', '8', '7', '6', '5', '4', '3', '2', '1']


-- 
Mike Meyer [EMAIL PROTECTED]  http://www.mired.org/home/mwm/
Independent WWW/Perforce/FreeBSD/Unix consultant, email for more information.
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Class Variable Access and Assignment

2005-11-03 Thread Mike Meyer
Graham [EMAIL PROTECTED] writes:
 I just seems to me that instance.var shouldn't defer to the class
 variable if
 an instance variable of the same name does not exists, it should, at
 least how i
 understand it raise an exception.

 Is my thinking way off here?

Yes. This behavior is how you get inheritted access to class
variables. Consider:

class Counter(object):
  A mutable counter.
  # implementation elided

class A(object):
  instance_count = Counter()
  def __init__(self):
  self.instance_count.increment()

class B(A):
  instance_count = Counter()

This is sufficient to count instances of A and B. If
self.instance_count raised an exception, you'd have to do something
like A.instance_count in A.__init__, which would mean the __init__ B
inherited from A would do the wrong thing. Currently, you can either
reference class variables by explicit class, or via inheritance, and
both behaviors are desirable. If you're going to disallow
self.class_variable, you need to come up with a mechanism to replace
the latter behavior.

mike
-- 
Mike Meyer [EMAIL PROTECTED]  http://www.mired.org/home/mwm/
Independent WWW/Perforce/FreeBSD/Unix consultant, email for more information.
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Class Variable Access and Assignment

2005-11-03 Thread Mike Meyer
Graham [EMAIL PROTECTED] writes:
 Many thanks your explaination cleared up many of the questions I had.
 I know think i can understand the purpose, regardless of my opinion, i
 do however think that one should be able to assign the value in the
 same way it is accessed.

That's not true in lots of cases, and would be a serious restriction
on the language. The rules for where a binding takes place are the way
the are for good reason. Restricting lookups to those rules would make
a number of things more difficult, and would make some features
(cough-closures-cough) nearly useless.

 Given your previous example:

 class Counter(object):
  A mutable counter.
  # implementation elided

 class A(object):
  instance_count = Counter()
  def __init__(self):
  self.instance_count.increment()


 if you changed
 class A(object):
  instance_count = Counter()
  def __init__(self):
  self.instance_count.increment()

 to

 class A(object):
  instance_count = 0
  def __init__(self):
  self.instance_count = self.instance_count + 1

 It would not work as planned. I understand all the reasons why this
 occurs, but i dont understand why its implemented this way. Because it
 acts in a different way than you expect.

No, it acts in a different way than *you* expect. It does exactly what
I expect, which is why I didn't write it that way.

 It seems to me that
 self.instance_count should not create a new entry in the __dict__ if a
 class variable of that name is already present anywhere in that objects
 hierarchy.

 Does that make sense?

Yes, but such behavior would make Python worse, not better.  Right
now, binding instance.name *always* works(*) on the value of the name
attribute of instance. Creating a special case for when one of the
classes instance belongs to happens to have an attribute name would
be less consistent than the current behavior. Yes, the current
behavior is surprising to people who aren't used to dynamic
languages. But there are lots of such things - they're part of the
power of dynamic languages. People who want to program in dynamic
languages just have to get used to those things. That's a lesser price
to pay than having a language cluttered with special cases just to
avoid surprising people who aren't used to the language yet.

 Again thank you for explaination.

You're welcome.

   mike

*) I almost said binds, but that's not true. Attempting to bind an
attribute can invoke arbitrary code - but it's got the instance and
attribute name to work with. If you really wanted to, you could create
a class for which trying to set an attribute behaved as you wanted. Be
warned - it's not as easy as it looks.
-- 
Mike Meyer [EMAIL PROTECTED]  http://www.mired.org/home/mwm/
Independent WWW/Perforce/FreeBSD/Unix consultant, email for more information.
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: If Statement Error (Tic Tac Toe)

2005-11-02 Thread Mike Meyer
[EMAIL PROTECTED] writes:

 Nevermind my previous reply: I've been fixing up the code and
 understand it.  I'm at a nifty 80 lines where I am at now rather than
 209 lines in the previous version!  The changes are immense!

 The only problem I have is whenever player two goes, it says the cell
 is filled.  But the code:

 if gameboard[cell] not in 'OX':
 gameboard[cell] = 'X'
 else:
 print This cell is already filled.
 turnnumber = turnnumber - 1

 is not any different than the code I am using for player one.  Anything
 I have to change regarding 'OX' or something else?

Make sure you're setting cell properly - you didn't show that.

Also, the above code is a syntax error; I assume your running code has
the correct indentation.

mike
-- 
Mike Meyer [EMAIL PROTECTED]  http://www.mired.org/home/mwm/
Independent WWW/Perforce/FreeBSD/Unix consultant, email for more information.
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Xah's edu corner: the Journey of Foreign Characters thru Internet

2005-11-02 Thread Mike Meyer
Peter Hansen [EMAIL PROTECTED] writes:

 Mike Meyer wrote:
 Xah Leh is incompetent, but
 apparently well-intentioned.
 In your view is that (well-intentioned) an established fact at this
 point?  I was still waiting for conclusive evidence.

No, it's not an established fact, which is why I said apparently. I
think *he* thinks he is doing the world a favor by ignoring
established netiquette in his postings.

But he's clearly incompetent, and that's sufficient to explain his
behavior. And there's a saying about malice and incompetence.

mike
-- 
Mike Meyer [EMAIL PROTECTED]  http://www.mired.org/home/mwm/
Independent WWW/Perforce/FreeBSD/Unix consultant, email for more information.
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: If Statement Error (Tic Tac Toe)

2005-11-02 Thread Mike Meyer
[EMAIL PROTECTED] writes:

 The code's indentation was fine - I forgot to declare cell in player
 two's section and not just in player one.

 The code (including the win check, for once!) is working.  The last
 obstacle is my tie checker; it doesn't seem to like what I have:

 if ((gameboard[0:9] is 'X' or 'O') and (win == 0)):
  print Tie.
  win = 2

 Will the [0:9] range not work in this?  Should I write out each option?
  Or is there some way to check if all things in the list are NOT ' '?

 I tried

 if ((gameboard[0:9] is not ' '))

Yes, those tests are not doing what you want them to do. On the other
hand, you can still test this in one swell foop with in:

  if ' ' not in gameboard and win == 0:
 print Tie.
 win = 2

That does assume that gameboard and gameboard[0:9] are the same
thing. But they should be. For that matter, the defaults for the slice
should be 0 and 9, so that gameboard[:] is the same thing yet
again. But since you're not changing the board but reading it, there's
no need to make a copy, which is what the sliced versions do.

mike
-- 
Mike Meyer [EMAIL PROTECTED]  http://www.mired.org/home/mwm/
Independent WWW/Perforce/FreeBSD/Unix consultant, email for more information.
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: dictionary that have functions with arguments

2005-11-02 Thread Mike Meyer
[EMAIL PROTECTED] writes:
 hi
 i have a dictionary defined as

 execfunc = { 'key1' : func1 }

 to call func1, i simply have to write execfunc[key1] .
 but if  i have several arguments to func1 , like

 execfunc = { 'key1' : func1(**args) }

 how can i execute func1 with variable args?
 using eval or exec?

Whenever you think should I use eval or exec for this, you should
*immediately* stop and think What am I doing wrong?.

Others have suggested using a tuple to hold the function and
arguments, and pointed out the mistake in your invocation.

Whenever you're thinking about doing an evalu with a fixed string, you
can replace it with a lambda. That looks like:

 execfunc = dict(key1 = lambda: func1('hello'))
 def func1(x): print x
... 
 execfunc['key1']()
hello
 

You can use the tuple format, and then use apply and the extended call
syntax to keep it in one line:

 execfunc = dict(key1 = (func1, ('hello',)))
 apply(*execfunc['key1'])
hello
 

Note that applly is depreciated - you're supposed to use the extended
call syntax instead. This particular use case for apply can't be
handled by the extended call syntax.

Using dictionaries instead of a fixed arg is a trivial change to both
these examples.

  mike
-- 
Mike Meyer [EMAIL PROTECTED]  http://www.mired.org/home/mwm/
Independent WWW/Perforce/FreeBSD/Unix consultant, email for more information.
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: how to check for unix password

2005-11-02 Thread Mike Meyer
[EMAIL PROTECTED] writes:
 i created a login page that authenticate the user and his/her password
 to the unix ssystem. what modules can i used to compare the unix
 password with what the user typed in the cgi form? the password is
 encrypted (shadowed) so i need to
 decrypt it first before comparing to what the user typed. or this
 cannot be done at all?

As has already been pointed out, users authenticate to Unix systems
with a lot more than passwords.

Also, it's not a good idea to make a web page use a system
password. Web page passwords tend to be poorly protected.

Finally, you can't decrypt a Unix password file password. The
algorithm is to encrypt what the user typed (with crypt.crypt) then
compare that with the entry in the password file. You pass crypt.crypt
the user-entered pasword as the first argument, and the password from
the password file as the second, and compare the returned value to the
password from the password file.

 mike
-- 
Mike Meyer [EMAIL PROTECTED]  http://www.mired.org/home/mwm/
Independent WWW/Perforce/FreeBSD/Unix consultant, email for more information.
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Most efficient way of storing 1024*1024 bits

2005-11-02 Thread Mike Meyer
Tor Erik Sønvisen [EMAIL PROTECTED] writes:
 I need a time and space efficient way of storing up to 6 million bits. Time 
 efficency is more important then space efficency as I'm going to do searches 
 through the bit-set.

Six megabytes is pretty much nothing on a modern computer. I'd store
the things as a string of 0 and 1, and then use .find (or maybe
the in keyword) for doing the searches.

This doesn't work very well if you're going to mutate the string,
though.

mike
-- 
Mike Meyer [EMAIL PROTECTED]  http://www.mired.org/home/mwm/
Independent WWW/Perforce/FreeBSD/Unix consultant, email for more information.
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: How to print random strings

2005-11-02 Thread Mike Meyer
[EMAIL PROTECTED] writes:

 Im at the end of chapter 3 of Python Programming For The Absolute
 Beginner, Michael Dawson  and he asks to make a fortune program that
 displays a fortune each time its ran, and to have 5 unique fortunes.

 Whats confusing is that, he never discussed how to do this. The only
 thing he talked about was using random.randrange() and I tried that
 with text but it seems like its only for integers as it complains when
 I put text in the argument.

 So how would I go about have 5 strings, and running a program that will
 randomly pick one of those to print?

 I think he may have forgot to cover something?

Well, randrange can be used to do this, but random.choice is more
pythonic.

mike
-- 
Mike Meyer [EMAIL PROTECTED]  http://www.mired.org/home/mwm/
Independent WWW/Perforce/FreeBSD/Unix consultant, email for more information.
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: another beginner sort of question

2005-11-02 Thread Mike Meyer
John Salerno [EMAIL PROTECTED] writes:
[Wants to learn C# and Python simultaneously.]
 So my question is, is this feasible?

Should be. It might be faster to do them sequentually.

 Or does learning Python require (or entail) learning all the details
 behind it?

Not really. There are some traps you can fall into that are obvious if
you know how the underlying implementation works, but even for those,
you just need the general idea, not the details.

 Also, do I need to know anything about C or C++?

No. In fact, the less you know about them, the less you'll have to
unlearn to use Python effectively.

 Python seems to connected to those languages that I'm afraid
 learning Python by itself might not be practical, but hopefully
 that's unfounded.

CPython (the implementation most people mean when they say Python)
is written in C, and has well-defined APIs for putting an interpreter
into a C program, or making functionality from a C library available
to a CPython program. Other implementations have similar hooks for
different languages. Unless you want to get into the internals of an
implementation, to embed Python in an application, or to write a
Python extension (usually because you're wrapping an existing
library), you won't need to worry about any of these.

One thing. While Python is called a scripting language, it doesn't
have facilities for dealing with shell scripting that other scripting
languages have. As such, it's harder to do shell scripting type
things in Python than in those languages. On the other hand, it's
easier than doing them in C, for the same reason that doing pretty
much anything in Python is easier than doing it in C. On the gripping
hand, if you do things pythonically instead of like you'd do them in a
shell script, you may find that Python is easier than the shell
script.

mike
-- 
Mike Meyer [EMAIL PROTECTED]  http://www.mired.org/home/mwm/
Independent WWW/Perforce/FreeBSD/Unix consultant, email for more information.
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Pychecker Re: Nested List Question

2005-11-02 Thread Mike Meyer
Roman Suzi [EMAIL PROTECTED] writes:
 On Thu, 3 Nov 2005, Chris McCoy wrote:
 gridSystemId = [[None]*columns]*rows
 You've made gridSystemID a list of `rows` references to the SAME inner
 list, so the behavior you observe is the only possible one.
 If you want copies instead, ASK for copies...:
 gridSystemId = [ [None]*columns for x in xrange(rows) ]
 Interesting, could not pychecker recognize such situations in Python
 code and give warnings?

Well, it could always just issue warnings everytime it saw a list
multiplied by something. But that would get annoying in the cases
where that idiom doesn't have problems - which is naturally most such
usages. Your example included one such, which is why the translation
wasn't to:

gridSystemId = [[None for y in xrange(columns)] for x in xrange(rows)]  WRONG

[None] * columns doesn't have problems. Nor does any other immutable
object. So to avoid spurious warnings, pychecker would have to know
whether the objects in the list were immutable or not. It could guess
that if the objects are builtin types, but not for other types.

 mike
-- 
Mike Meyer [EMAIL PROTECTED]  http://www.mired.org/home/mwm/
Independent WWW/Perforce/FreeBSD/Unix consultant, email for more information.
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Xah's edu corner: the Journey of Foreign Characters thru Internet

2005-11-01 Thread Mike Meyer
[EMAIL PROTECTED] writes:

 Interesting opinions this man has, I must say. A Sith of Computing he
 may be?

No. Sith are competent but evil. Xah Leh is incompetent, but
apparently well-intentioned. Some of his opinions are correct, but
that appears to be more by accident than anything else, and there's no
easy way to distinguish those from his usual work.

He has been accurately described as stupider than spam. Please help
Google find him that way by adding an appropriater link to your web
site: a href=http://xahlee.org/;stupider than spam/a

  mike
-- 
Mike Meyer [EMAIL PROTECTED]  http://www.mired.org/home/mwm/
Independent WWW/Perforce/FreeBSD/Unix consultant, email for more information.
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: extracting numbers from a file, excluding words

2005-11-01 Thread Mike Meyer
Kristina Kudriaðova [EMAIL PROTECTED] writes:

 1 Nov 2005 09:19:45 -0800, [EMAIL PROTECTED] [EMAIL PROTECTED]:
 Hi, I have a file with this content:

  z zzz z
 ...
 xxx xx x 34.215
 zzz zz 
 ...


 Hi,

 I'd suggest doing this:

 f = file('...')
 for line in f:
 if 'xxx xx x' in line:
 var = float(line[len('xxx xx x'):].strip())
 f.close()

Alternatively:

start = len('xxx xx x')
for line in f:
if line.startswith('xxx xx x'):
   var = float(line[start:].strip())
   
   mike
-- 
Mike Meyer [EMAIL PROTECTED]  http://www.mired.org/home/mwm/
Independent WWW/Perforce/FreeBSD/Unix consultant, email for more information.
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: If Statement Error (Tic Tac Toe)

2005-11-01 Thread Mike Meyer
 filled.
 turnnumber = turnnumber - 1

 if row == 3 and column == 3:
 if gameboard[8] != ('O' or 'X'):
 gameboard[8] = ('X')

 else:
 print This cell is already filled.
 turnnumber = turnnumber - 1

 turnnumber = turnnumber + 1

 if (((gameboard[0] and gameboard[1] and gameboard[2]) ==
 'X') or ((gameboard[3] and gameboard[4] and gameboard[5]) == 'X') or
 ((gameboard[6] and gameboard[7] and gameboard[8]) == 'X')\
 or ((gameboard[0] and gameboard[3] and gameboard[6]) ==
 'X') or ((gameboard[1] and gameboard[4] and gameboard[7]) == 'X') or
 ((gameboard[2] and gameboard[5] and gameboard[8]) == 'X')\
 or ((gameboard[0] and gameboard[4] and gameboard[8]) ==
 'X') or ((gameboard[2] and gameboard[4] and gameboard[6]) == 'X')):
 print Player 2 wins!
 win = 1


-- 
Mike Meyer [EMAIL PROTECTED]  http://www.mired.org/home/mwm/
Independent WWW/Perforce/FreeBSD/Unix consultant, email for more information.
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Python's website does a great disservice to the language

2005-11-01 Thread Mike Meyer
CppNewB [EMAIL PROTECTED] writes:
 My thoughts weren't to add a flash demo or develop some 500K logo or fixing 
 the font size. But the logos look like they were done in Paint and maybe a 
 readable default font is in order.

So what's wrong with the fonts they use - where they didn't let your
browser pick them, anyway. In the latter case, the fault is yours, not
theirs.

mike
-- 
Mike Meyer [EMAIL PROTECTED]  http://www.mired.org/home/mwm/
Independent WWW/Perforce/FreeBSD/Unix consultant, email for more information.
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: If Statement Error (Tic Tac Toe)

2005-11-01 Thread Mike Meyer
[EMAIL PROTECTED] writes:

 Thank you.  It seems I didn't understand logic statements as much as I
 thought I did!

 The one remaining question I have deals with this:

 if gameboard[cell] not in 'OX':
 gameboard[cell] = 'O'
  else:
 print This cell is already filled.
 turnnumber -= 1

 (gameboard[cell] not in 'OX' is the gameboard[cell] != 'OX' text I sort
 of used in my code, right?)

 I am confused about OX:  what exactly *is* it?  After that, I plan to
 rewrite all the code... :)

OX is a string. Saying shortstring in longstring is one way of
asking if shortstring is contained in longstring. In your case,
shortstring is one character long, but that's a string to
Python. Python doesn't have a distinct character data type.

While I'm at it, don't be confused by single quotes vs. double
quotes. Unlike other languages, in Python they have the same
meaning. The only difference is what you have to do to get a single
(double) quote in the string.

 mike
-- 
Mike Meyer [EMAIL PROTECTED]  http://www.mired.org/home/mwm/
Independent WWW/Perforce/FreeBSD/Unix consultant, email for more information.
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Arguments for button command via Tkinter?

2005-10-31 Thread Mike Meyer
[EMAIL PROTECTED] [EMAIL PROTECTED] writes:
 Recently, I have been needing to do this alot and I can never find a
 way around it, the main reason I want to do this is because for example
 in the application I am making right now, it creates a grid of buttons
 in a loop and they all have the same purpose so they need to call the
 same method, within this method they need to change the background
 color of the button clicked, I've tried stuff like...

 button['command'] = lambda: self.fill(button)

 def fill(self, wid):
 button['bg'] = '#ff'

 But that's no good, everytime it I do click a button the lambda method
 I have setup keeps getting recreated so all of the button commands
 point to the same lambda method, I have also tried appending them to an
 array, and calling them from that.

 Anyone have any clue as of what to do? Thanks.

People have pointed out how to do this with a class, but you can do it
with lambdas as well. I suspect the problem you're having is that
python is binding values at call time instead of at definition time,
but it's hard to tell without more of your source code.

If I'm right, one solution is to make the values you want bound at
definition time optional arguments to the lambda, bound to the correct
value then. So you'd do:

  button['command'] = lambda b = button: self.fill(b)

The version you used will look up the name button when the lambda is
invoked. My version will look it up when the lambda is defined.

 mike
-- 
Mike Meyer [EMAIL PROTECTED]  http://www.mired.org/home/mwm/
Independent WWW/Perforce/FreeBSD/Unix consultant, email for more information.
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Expanding Python as a macro language

2005-10-31 Thread Mike Meyer
Jorgen Grahn [EMAIL PROTECTED] writes:

 On 29 Oct 2005 17:25:58 -0700, [EMAIL PROTECTED] [EMAIL PROTECTED] wrote:
 ...
 Michel wrote:
 ...
  Linux can run perfectly happily without any form of windowing
  environment.

 I know, but nowadays almost any relevant application has a GUI.

 Side note: lots of people would disagree with that. For example, from my
 perspective, almost all relevant applications /lack/ a GUI. (And some that
 has one would be better off without it.)

This brings up an interesting point for scripting unix
applications. One common approach is to provide the application
functionaliity in a library, then wrap that in various ways, of which
a GUI is only one. You script the application by wrapping the
library for your favorite scripting language, and the script will never
open a GUI. In fact, it'll work fine in an environment where the GUI
couldn't open at all.

 mike
-- 
Mike Meyer [EMAIL PROTECTED]  http://www.mired.org/home/mwm/
Independent WWW/Perforce/FreeBSD/Unix consultant, email for more information.
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Microsoft Hatred FAQ

2005-10-31 Thread Mike Meyer
David Schwartz [EMAIL PROTECTED] writes:
 Mike Meyer [EMAIL PROTECTED] wrote in message 
 news:[EMAIL PROTECTED]
 Microsoft's behavior consisted of arguments, that is, did not
 involve force, the threat of force, fraud, or the threat of
 fraud. This is perhaps the most vital distinction that there is.
 Wrong. Either your definition of force is to narrow, or you're wrong
 that it's the distinction is even vaguely vital. If I convince
 everyone who might make food available to you not to do so - for
 example, by paying them more than their interaction with you is worth
 to them, I can starve you to death. I'd say I've used force against
 you - an economic force.
 Right, you would say that, because you don't see the difference between 
 guns and arguments.

Except I'm willing to allow your definition. All it takes is reading
the next paragraph for you to see that.

Of course, you've dropped the real point, which is your own inabillity
to distinguish between, as you put it, guns and arguments. You
always act as if every mention of a crime committed by someone other
than microsoft involved guns, even when most of them don't. You have
as yet to offer any explanation for that other than that you're
following MS's orders.

 This is the kind of force that MS wields
 illegally. I'm willing to admit this isn't a usual definition of
 force, and won't argue if you want to say that it isn't force. But in
 that case, the fact that I didn't use force against you is
 irrelevant to you - you're just as dead.
 No, it is completely relevant. You are trying to say the means don't 
 matter if the same end is achieved. But if you're going to say that, you 
 might as well say there's no difference between shooting someone and letting 
 them die of natural causes.

Another straw man argument. I'm not trying to say anything of the
sort.

mike
-- 
Mike Meyer [EMAIL PROTECTED]  http://www.mired.org/home/mwm/
Independent WWW/Perforce/FreeBSD/Unix consultant, email for more information.
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Microsoft Hatred FAQ

2005-10-31 Thread Mike Meyer
David Schwartz [EMAIL PROTECTED] writes:
 Mike Meyer [EMAIL PROTECTED] wrote in message 
 news:[EMAIL PROTECTED]
 Of course, you've dropped the real point, which is your own inabillity
 to distinguish between, as you put it, guns and arguments. You
 always act as if every mention of a crime committed by someone other
 than microsoft involved guns, even when most of them don't. You have
 as yet to offer any explanation for that other than that you're
 following MS's orders.
 Your sole evidence for this claim is that I once equated theft with 
 force. Yes, you are correct that it's possible to steal something without 
 using force. Even in this case, from context, it was quite clear that 
 forceful theft was intended.

No, my evidence for this claim, as I've repeatedly pointed out, is
that *every* time someone compares MS with any other criminal
activity, you whine about guns and refuse to deal with the
issue. You've gone from claiming that you don't do this, to claiming
you only do this when people refer to guns, to ducking the issue.

 There is a fundamental category difference between the fundamental 
 inter-personal wrongs of force and fraud and every other invented wrong. 
 People are making a concerted attempt in this thread to obliterate that 
 distinction, and I include you in those making that attempt.

Another straw man. I'm not trying to oblitarate that difference, I'm
trying to find out why you regularly ignore that difference for
everyone but MS. You respond by falsely claiming that you aren't doing
so, or by ducking the issue.

mike
-- 
Mike Meyer [EMAIL PROTECTED]  http://www.mired.org/home/mwm/
Independent WWW/Perforce/FreeBSD/Unix consultant, email for more information.
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Microsoft Hatred FAQ

2005-10-31 Thread Mike Meyer
David Schwartz [EMAIL PROTECTED] writes:
 I'm trying to find out why you regularly ignore that difference for
 everyone but MS.
 To substantiate that claim, you'd have to point to some cases where I 
 talk about something other than MS.

You do that *every time* someone compares MS with other criminals -
you immediately refer to criminals with guns and refuse to discuss
the issue further. And yes, you've already claimed that you only do
that when the other reference is to actual use of force, and I've
already disproved that.

 But if you do a little research, you'll find I'm completely
 consistent and have said similar things about numerour other
 entities.

Not in this thread, you haven't. The only consistency here has been
trying to treat MS's crimes as somehow different from other peoples
crimes. I'm still waiting for you to quit trying to lie (or, as you
would say, argue) your way out of it, and come up with a reason for
this behavior other than doing so at MS's orders.

mike

-- 
Mike Meyer [EMAIL PROTECTED]  http://www.mired.org/home/mwm/
Independent WWW/Perforce/FreeBSD/Unix consultant, email for more information.
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Microsoft Hatred FAQ

2005-10-31 Thread Mike Meyer
David Schwartz [EMAIL PROTECTED] writes:

 Mike Meyer [EMAIL PROTECTED] wrote in message 
 news:[EMAIL PROTECTED]
 David Schwartz [EMAIL PROTECTED] writes:
 I'm trying to find out why you regularly ignore that difference for
 everyone but MS.
 To substantiate that claim, you'd have to point to some cases where I
 talk about something other than MS.
 You do that *every time* someone compares MS with other criminals -
 you immediately refer to criminals with guns and refuse to discuss
 the issue further. And yes, you've already claimed that you only do
 that when the other reference is to actual use of force, and I've
 already disproved that.
 You have not disproved that. The closest you've come to a disproof is 
 one case where the word theft was used (while earlier in the thread, 
 actual physical force had been used, but not in that specific spot) where 
 the context strongly suggested that it meant theft by force.

Now you're simply lieing. I never discussed force earlier in the
thread.

 You are correct that it is possible to steal something without actually 
 using physical force. But that's not an important difference. The hugely 
 important difference, and the one that you and others *are* seeking to 
 obliterate, is the difference between inherently unjust actions such as 
 force and fraud and actions that are neither forceful nor fraudulent.

And now you change your story again. You've gone from referring to all
other criminal acts as criminals with guns to actual use of force
to using force or fraud.

 The only consistency here has been
 trying to treat MS's crimes as somehow different from other peoples
 crimes.
 That's because the only crimes that have come up in this thread are 
 Microsoft's crimes (that don't involve force or fraud) and other crimes 
 (such as theft, threats of force, and the like) which do. Duh.

Actually, they don't necessarily, but that's relevant. You simply
label *all* crime other than MS's as criminals with guns and refuse
to discuss them.

 There were a few narrow cases where Microsoft was actually accused of 
 actions that I do consider force or fraud.

That's true. They committed a fraud - by lieing to federal officials
in court - and *you* responded by calling those federal officials
criminals with guns, and using that to *excuse* MS's criminal acts
in this case.

Which is more of the same old song and dance from you: treating MS's
criminal acts as somehow different from any other criminals acts.

 mike
-- 
Mike Meyer [EMAIL PROTECTED]  http://www.mired.org/home/mwm/
Independent WWW/Perforce/FreeBSD/Unix consultant, email for more information.
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Automatic binding of **kwargs to variables

2005-10-30 Thread Mike Meyer
[EMAIL PROTECTED] [EMAIL PROTECTED] writes:
 Peter Hansen wrote:
 Do you mean this instead?

   elif name in expected_form1_kwargs and name not in kwargs:

 What you wrote doesn't do what you think it does...  it actually tests
 for whether True or False is a key in kwargs, depending on whether name
 in expected_form1_kwargs returns True or False.

 Hi Peter,

 you're right... the code needed also other fixes and I didn't find a
 way to do it other than this:

 def foo(**kwargs):
 expected_form1_kwargs = [arg1, arg2]

 for name in expected_form1_kwargs:
 if name not in kwargs:
 kwargs[name]=None

 for name in kwargs:
 if name in kwargs and name not in expected_form1_kwargs:
 raise ValueError, Unrecognized keyword:  + name

 print kwargs


You're contorting things horribly, and - not unexpected when you do
that kind of thing - running code that has no effect. To wit, the
second loop goes through every key in kwargs. The first thing it does
is test to see if the key you just selected from kwargs is in
kwargs. This test is always true.

IIUC, the reason you're going through these contortions is that the
argument list is long and repeated in a number of different places. So
you're going to declare expected_form1_kwargs in one place and use it
repeatedly. Presumably, you can also bundle the set/check code up in a
function that you pass kwargs to as well.

Have you thought about generating the more readable form from code you
write? One of the most powerful tools available to programmers is code
that writes code. This seems to be the perfect case for such a
tool. You could, for instance, run your code through m4 to generate
the Python that actually gets run. A trivial script that looks for
^def  and then replaced **kwargs with your list could serve as a
custom preprocessor.  To be particulary perverse, you could try using
a Cheetah template to generate your code - I've not tried generating
Python with it, but it creates Makefiles quite nicely.

   mike
-- 
Mike Meyer [EMAIL PROTECTED]  http://www.mired.org/home/mwm/
Independent WWW/Perforce/FreeBSD/Unix consultant, email for more information.
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Controlling output using print with format string

2005-10-30 Thread Mike Meyer
Paul Watson [EMAIL PROTECTED] writes:

 It is clear that just using 'print' with variable names is relatively
 uncontrollable.  However, I thought that using a format string would
 reign the problem in and give the desired output.

 Must I resort to sys.stdout.write() to control output?

No. You can control the output of print to whatever degree you want -
just convert the printed objects to strings by hand before printing
them.

 $ python
 Python 2.4.1 (#1, Jul 19 2005, 14:16:43)
 [GCC 4.0.0 20050519 (Red Hat 4.0.0-8)] on linux2
 Type help, copyright, credits or license for more information.
   s = 'now is the time'
   for c in s:
 ... print c,
 ...
 n o w   i s   t h e   t i m e
   for c in s:
 ... print %c % (c),
 ...
 n o w   i s   t h e   t i m e
   for c in s:
 ... print %1c % (c),
 ...
 n o w   i s   t h e   t i m e

On the other hand, you can't keep print from putting a space between
all the objects it prints. So you have to convert all the objects into
a single string before printing that string.

 mike
-- 
Mike Meyer [EMAIL PROTECTED]  http://www.mired.org/home/mwm/
Independent WWW/Perforce/FreeBSD/Unix consultant, email for more information.
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: need start point for getting html info from web

2005-10-30 Thread Mike Meyer
[EMAIL PROTECTED] writes:
 i have a small app that i am going to need to get information from a
 few tables on different websites. i have looked at urllib and httplib.
 the sites i need to get data from mostly have this data in tables. So
 that, i think would make it easier. Anyone suggest a good starting
 point for me to find out how to do this, or know of a link to a good
 how-to?

Don't have a link to a howto. But you're halfway there. urllib (and
urllib2) will get HTML text from the websites. Pulling data from it
sort of depends on the nature of the HTML. If it's well-structured
XHTML, you can use your favorite xml library. if it's well structured
HTML, you can try htmllib, but it's pretty primitive. If it's not
well-structured, you can use BeautifulSoup. I've used it to pull data
from tables. The problem with any of this is that your code really
depends on the structure - or lack thereof - of the HTML you're
scraping. If they change it, your code breaks.

  mike
-- 
Mike Meyer [EMAIL PROTECTED]  http://www.mired.org/home/mwm/
Independent WWW/Perforce/FreeBSD/Unix consultant, email for more information.
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Expanding Python as a macro language

2005-10-29 Thread Mike Meyer
[EMAIL PROTECTED] (Alex Martelli) writes:

...
 But the problem is that in Linux you can't even send a keystroke to
 a running GUI application!
 Actually, if the app is running under X11 you may try to fake out a
 keystroke event (with low level calls, but ctypes might let you use it
 from Python).  Of course, the app WILL be told that the keystroke is
 fake, through a special flag if it cares to check for it, for security
 reasons; but if the app doesn't specifically defend itself in this way.

I'm pretty sure that python-xlib can do this without having to go to
low level calls.

mike
-- 
Mike Meyer [EMAIL PROTECTED]  http://www.mired.org/home/mwm/
Independent WWW/Perforce/FreeBSD/Unix consultant, email for more information.
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Automatic binding of **kwargs to variables

2005-10-29 Thread Mike Meyer
[EMAIL PROTECTED] [EMAIL PROTECTED] writes:

 Mike Meyer wrote:
 [snip]
for name, value in kwargs.items():
if name in ('a', 'list', 'of', 'valid', 'keywords'):
   exec %s = %s % (name, value)
else:
   raise ValueError, Unrecognized keyword  + name

 Others will probably tell you that you really shouldn't be using exec.

 What about using setattr?

 for name, value in kwargs.items():
 if name in ('a', 'list', 'of', 'valid', 'keywords'):
setattr(self, name, value)
 else:
raise ValueError, Unrecognized keyword  + name

You clipped my description of what's wrong with setattr: it doesn't
bind the names in the local namespace, it binds them to an
object. This may be acceptable, but wasn't what the OP asked for.

 I'd probably turn the list of valid keywords into another dictionary to
 make it easy to specify default values for all the parameters as well.

If you're going to do that, why not just specify them explicitly in
the function definition instead of using **kwargs? That takes care of
everything - binding, checking, and providing a default value - in one
swoop.

mike
-- 
Mike Meyer [EMAIL PROTECTED]  http://www.mired.org/home/mwm/
Independent WWW/Perforce/FreeBSD/Unix consultant, email for more information.
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Scanning a file

2005-10-29 Thread Mike Meyer
Paul Watson [EMAIL PROTECTED] writes:

 Mike Meyer [EMAIL PROTECTED] wrote in message 
 news:[EMAIL PROTECTED]
 Paul Watson [EMAIL PROTECTED] writes:
 ...
 Did you do timings on it vs. mmap? Having to copy the data multiple
 times to deal with the overlap - thanks to strings being immutable -
 would seem to be a lose, and makes me wonder how it could be faster
 than mmap in general.

 The only thing copied is a string one byte less than the search string for 
 each block.

Um - you removed the code, but I could have *sworn* that it did
something like:

  buf = buf[testlen:] + f.read(bufsize - testlen)

which should cause the the creation of three strings: the last few
bytes of the old buffer, a new bufferfull from the read, then the sum
of those two - created by copying the first two into a new string. So
you wind up copying all the data.
  
Which, as you showed, doesn't take nearly as much time as using mmap.

   Thanks,
   mike
-- 
Mike Meyer [EMAIL PROTECTED]  http://www.mired.org/home/mwm/
Independent WWW/Perforce/FreeBSD/Unix consultant, email for more information.
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Recursive generators and backtracking search

2005-10-29 Thread Mike Meyer
Talin [EMAIL PROTECTED] writes:
 As an alternative, I'd like to present the following implementation. If
 you compare this one with the one in lib/test/test_generator.py you
 will agree (I hope) that by using recursive generators to implement
 backtracking, the resulting code is a little more straightforward and
 intuitive:

I'd propose one change to that...
 def qsearch( qarray = () ):
 for q in range( 0, bsize ):# Try each position
 if not threaten( qarray, q ):  # If not threatened
 pos = qarray + ( q, ); # Append to the pos array

 if len( pos ) = bsize:# Are we done?
 yield pos  # Yield the answer
 else:# recursively call new generator
 for pos in qsearch( pos ):
 yield pos

Um - do you really want to reuse the variable pos here? Yeah, it
works, but this strikes me as very confusing. I'm not sure that it
might not be implementation dependent.

  mike
-- 
Mike Meyer [EMAIL PROTECTED]  http://www.mired.org/home/mwm/
Independent WWW/Perforce/FreeBSD/Unix consultant, email for more information.
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Expanding Python as a macro language

2005-10-29 Thread Mike Meyer
[EMAIL PROTECTED] writes:
 My answer is the same as that given to Michael about low level
 programming.
 But I must thank anyway Alex for giving informations to such a level
 (I didn't know that under Linux there was such a level of
 sophistication with the possibility for an application to discover fake
 keystrokes...).

 If this fake keystroke is used to fire an event (like pressing a
 button), the fake flag is propagated to the action taken by the button?

The action is internal to the application, so whether or not it
propogates the fake flag is up to the application. In between the X
event and the action is the GUI library, which may also choose to not
propogate the flag.

 For the other Alex observations (about Mac OsX and my examples of
 automation centered on web automation) I have a PC, and the fact that
 Python is very good at dealing with the web, doesn't help too much
 in this case...
 In any case a macro language like AutoIt is a general purpose
 application.

 At last I must thank  Mike Meyer for his suggestion to use python-xlib
 to avoid low level programming...

Just to clarify - python-xlib will let you avoid the need for C
functions to do things like send X events. In that sense, it helps you
avoid low-level programming. On the other hand, you're still sending X
events, so in *that* sense, you're not avoiding low-level programming.

In general, application scripting facilities are one thing that Unix
hasn't dealt with well. Windows and OS X both do better (but neither
do as well later versions of AmigaDOS). Whereas those systems come
with a (or a selection) mechanism that applications can use to allow
macro programming, Unix doesn't, so each application is left up to
it's own devices. The most popular solution is to embed a custom
language for scripting - which is where something like Python comes
in. Some of them now export APIs that can be hooked up to a variety of
languages.  IIRC, Gnome has embraced CORBA as an interface for such,
but it didn't seem well supported the last time I looked. The bottom
line is that when it comes to scripting applications on Unix, your
solution is going to be tailored to the application.

The Unix variant with the best system-provided scripting facility is
probably Plan 9. Some Linux variants are moving in that direction, but
I don't know of any Linux applications that provide support for it.

 mike
-- 
Mike Meyer [EMAIL PROTECTED]  http://www.mired.org/home/mwm/
Independent WWW/Perforce/FreeBSD/Unix consultant, email for more information.
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Scanning a file

2005-10-28 Thread Mike Meyer
Andrew McCarthy [EMAIL PROTECTED] writes:

 On 2005-10-28, [EMAIL PROTECTED] [EMAIL PROTECTED] wrote:
 I'm now down to:

 f = open(filename, rb)
 s = f.read()
 sub = \x00\x00\x01\x00
 count = s.count(sub)
 print count

 Which is quite fast. The only problems is that the file might be huge.
 I really have no need for reading the entire file into a string as I am
 doing here. All I want is to count occurences this substring. Can I
 somehow count occurences in a file without reading it into a string
 first?

 Yes - use memory mapping (the mmap module). An mmap object is like a
 cross between a file and a string, but the data is only read into RAM
 when, and for as long as, necessary. An mmap object doesn't have a
 count() method, but you can just use find() in a while loop instead.

Except if you can't read the file into memory because it's to large,
there's a pretty good chance you won't be able to mmap it either.  To
deal with huge files, the only option is to read the file in in
chunks, count the occurences in each chunk, and then do some fiddling
to deal with the pattern landing on a boundary.

  mike
-- 
Mike Meyer [EMAIL PROTECTED]  http://www.mired.org/home/mwm/
Independent WWW/Perforce/FreeBSD/Unix consultant, email for more information.
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Typing tutor help script needed, please

2005-10-28 Thread Mike Meyer
Throw [EMAIL PROTECTED] writes:

 G'day everyone!

 I would like to design typing tutor exercises for Afrikaans (and other
 languages possibly).  This is for a GPL project.  For this, I need a
 script that can extract words from a long list of words, based on which
 letters those words contain, and write then write output to a file.
 Does anyone know of an existing script for this, or can anyone write me
 one, please?

This sounds nearly trivial in Python. More specifics would help.

 Preferably I must be able to extract words which contain only certain
 letters (they need not contain all of those letters, but they may not
 contain any other letters).  It would be nice if I can also extract
 words which do not contain certain letters.

Again, this sounds nearly trivial in Python.

For instance. to get a list of all words that don't contain 'a', you'd
do something  like:

f = open(textfile)
words = f.read().split()
f.close()
selected = [word for word in words if 'a' not in word]

and then selected has the list of words that don't have an 'a' in
them. Tweaking the test the other way - to select words with an 'a',
remove the 'not'. You can combine the tests with 'and' and 'or'.

But without a UI, that's pretty much useless - and you haven't said
what you want the UI to be.

 mike
-- 
Mike Meyer [EMAIL PROTECTED]  http://www.mired.org/home/mwm/
Independent WWW/Perforce/FreeBSD/Unix consultant, email for more information.
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Newbie question: string replace

2005-10-28 Thread Mike Meyer
[EMAIL PROTECTED] [EMAIL PROTECTED] writes:

 Now it works:
 rex = re.compile(r'(^.*username *=[^]*)([^]*)(.*$)')
 for line in fileinput.input(FILE, inplace=1):
  m = rex.match(line)
  if m is not None:
  line = %s%s%s\n % (m.group(1), new_name, m.group(3))
  print line

 But there is an extra line break after each line in FILE. Why is that?

Because print prints a newline after all the values you pass it.

 I tried to line = %s%s%s % (m.group(1), new_name, m.group(3)) but it
 turns out there are two extra line breaks after each line...

It's not clear what you mean by this. I would have expected this
change to solve the problem. Where do you get two extra newlines?

   mike
-- 
Mike Meyer [EMAIL PROTECTED]  http://www.mired.org/home/mwm/
Independent WWW/Perforce/FreeBSD/Unix consultant, email for more information.
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Microsoft Hatred FAQ

2005-10-28 Thread Mike Meyer
David Schwartz [EMAIL PROTECTED] writes:
 Mike Meyer [EMAIL PROTECTED] wrote in message 
 news:[EMAIL PROTECTED]
 The quote about the mafia doesn't compare MS's actions to actual use
 of force.
 I'm sorry, that's just absurd. I won't speculate on what motivates you 
 to engage in such crazy distortion. Of course the quote about the Mafia 
 compares MS's actions to actual use of force.

Maybe true, maybe not - but it doesn't matter. The point is that you
respond to *every* comparison of MS with other criminals as a
comparison to criminals with guns, and then refuse to discuss the
issue, with utter disregard as to what the other person said. That you
might be right in one case is irrelevant - we're talking about a
pattern of behavior.

If MS had been found guilty of abuse of monopoly power in one
instance, few people would care. People do make mistakes. That they
have been found guilty of such abuse repeatedly, on multiple
continents, and were recently caught doing it yet again - that's what
makes them criminals. It's the pattern of behavior that matters, not
the single instances.

I'm still waiting for you to come up with an explanation for the
pattern of your behavior other than that you're taking orders from
MS. But I expect yout to deny that it exists.

mike
-- 
Mike Meyer [EMAIL PROTECTED]  http://www.mired.org/home/mwm/
Independent WWW/Perforce/FreeBSD/Unix consultant, email for more information.
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Automatic binding of **kwargs to variables

2005-10-28 Thread Mike Meyer
[EMAIL PROTECTED] [EMAIL PROTECTED] writes:
 I have a very long list of parameters coming from a web form to my
 method foo(self, **kwargs)

 I would like to avoid manually binding the variables to the values
 coming through the **kwargs dictionary, just to keep the code cleaner,
 I'd like to bind them automatically

 I was adviced against doing it by adding stuff to locals such as:

 locals()[x] = 5

 since the Python Docs say:

 Warning: The contents of this dictionary should not be modified;
 changes may not affect the values of local variables used by the
 interpreter.

 Is there a way to do this? Maybe object.__setattr__() or something that
 involves metaclasses?

.__setattr__() will modify the instance variables, not the local
variables.

 As I might go touching something that I shouldn't (like locals()) I'd
 like some advice from you before deciding which way to go.

Reference the keyword arguments through the dictionary. Looking up
values by name is what dictionaries are for. Trying to move the names
from a dictionary into the local (or global) namespace is usually a
bad idea. If you don't know all the names in advance, you risk
collisions between the items in the dictionary and the legitimate
inhabitants of the namespace. If you do know all the names in advance,
you need to make sure the keys in the dictionary are in the list of
values you expect, so you need an explicit list.

That said, the most straightforward way to do this that I can think of
is with exec:

   for name, value in kwargs.items():
   if name in ('a', 'list', 'of', 'valid', 'keywords'):
  exec %s = %s % (name, value)
   else:
  raise ValueError, Unrecognized keyword  + name

Others will probably tell you that you really shouldn't be using exec.

   mike
-- 
Mike Meyer [EMAIL PROTECTED]  http://www.mired.org/home/mwm/
Independent WWW/Perforce/FreeBSD/Unix consultant, email for more information.
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Microsoft Hatred FAQ

2005-10-28 Thread Mike Meyer
David Schwartz [EMAIL PROTECTED] writes:
 Mike Meyer [EMAIL PROTECTED] wrote in message 
 news:[EMAIL PROTECTED]
 David Schwartz [EMAIL PROTECTED] writes:
 Mike Meyer [EMAIL PROTECTED] wrote in message
 news:[EMAIL PROTECTED]
 Maybe true, maybe not - but it doesn't matter. The point is that you
 respond to *every* comparison of MS with other criminals as a
 comparison to criminals with guns, and then refuse to discuss the
 issue, with utter disregard as to what the other person said. That you
 might be right in one case is irrelevant - we're talking about a
 pattern of behavior.
 That is not true. I make a serious distinction between crimes that 
 involve the use of force and crimes that don't. Microsoft was convicted of 
 crimes that do *not* involve use of force. I am trying very hard to make 
 sure that distinction is preserved.

Except you treat *every* mention of any crime other than abuse of
monopoly power as a crime that involves the use of force - whether it
does or not. In the example I quoted, you did it with theft -
without reference to how the theft happened. There are lots of forms
of theft that don't involve use of force - abuse of monopoly power is
one of them, but so is embezzlement. Yet you accused me of equating
arguments with guns.

 I'm still waiting for you to come up with an explanation for the
 pattern of your behavior other than that you're taking orders from
 MS. But I expect yout to deny that it exists.
 The pattern of my behavior is that it is vital to me to preserve the 
 distinction between force and non-force.

If it that vital, then you need to make the distinction yourself, and
quit treating *any* crime not committed by MS as if it involved the
use of force.

 Guns and arguments represent two 
 fundamentally different categories of human behavior. And I reject the moral 
 claim that it is okay to respond to arguments with guns.

Oh, we know you reject it. You never miss an opportunity to say so -
even if it's not at all relevant.

As predicted, you denied the truth of what I said, then provided an
excuse for a behavior that you don't follow. So we're still waiting
for an explanation for your actual behavior other than your being a
shill for MS.

 Microsoft's behavior consisted of arguments, that is, did not
 involve force, the threat of force, fraud, or the threat of
 fraud. This is perhaps the most vital distinction that there is.

Wrong. Either your definition of force is to narrow, or you're wrong
that it's the distinction is even vaguely vital. If I convince
everyone who might make food available to you not to do so - for
example, by paying them more than their interaction with you is worth
to them, I can starve you to death. I'd say I've used force against
you - an economic force. This is the kind of force that MS wields
illegally. I'm willing to admit this isn't a usual definition of
force, and won't argue if you want to say that it isn't force. But in
that case, the fact that I didn't use force against you is
irrelevant to you - you're just as dead.

mike
-- 
Mike Meyer [EMAIL PROTECTED]  http://www.mired.org/home/mwm/
Independent WWW/Perforce/FreeBSD/Unix consultant, email for more information.
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Scanning a file

2005-10-28 Thread Mike Meyer
Paul Watson [EMAIL PROTECTED] writes:
 Here is a better one that counts, and not just detects, the substring.  This 
 is -much- faster than using mmap; especially for a large file that may cause 
 paging to start.  Using mmap can be -very- slow.

 #!/usr/bin/env python
 import sys

 fn = 't2.dat'
 ss = '\x00\x00\x01\x00'

 be = len(ss) - 1# length of overlap to check
 blocksize = 64 * 1024# need to ensure that blocksize  overlap

 fp = open(fn, 'rb')
 b = fp.read(blocksize)
 count = 0
 while len(b)  be:
 count += b.count(ss)
 b = b[-be:] + fp.read(blocksize)
 fp.close()

 print count
 sys.exit(0) 



Did you do timings on it vs. mmap? Having to copy the data multiple
times to deal with the overlap - thanks to strings being immutable -
would seem to be a lose, and makes me wonder how it could be faster
than mmap in general.

 Thanks,
 mike
-- 
Mike Meyer [EMAIL PROTECTED]  http://www.mired.org/home/mwm/
Independent WWW/Perforce/FreeBSD/Unix consultant, email for more information.
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: how do i run another script from my python script

2005-10-27 Thread Mike Meyer
Daniel Schüle [EMAIL PROTECTED] writes:
 Steve already answeared to your question, regaring PHP script
 if this would be python script, you could run it by import'ing it

That's not very pythonic. Better is to provide a function in the
script to run it (say run), then run that in the script iff it's
the script is being executed:

if __name__ == '__main__':
   run()

Then to use it from another script, you'd do something like:

 import myscript
 myscript.run()

 or maybe using threading

Launching new threads as part of the import process is
*dangerous*. There are some nasty bugs lurking there that cause things
like your import never finishing. If you need to start a thread in a
module, the approach outlined above avoids those bugs.

mike
-- 
Mike Meyer [EMAIL PROTECTED]  http://www.mired.org/home/mwm/
Independent WWW/Perforce/FreeBSD/Unix consultant, email for more information.
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: How to replace all None values with the string Null in a dictionary

2005-10-27 Thread Mike Meyer
dcrespo [EMAIL PROTECTED] writes:

 Hi all,

 How can I replace all None values with the string 'Null' in a
 dictionary?

Iterate over everything in the dictionary:

for key, item in mydict.items():
if item is None:
   mydict[key] = 'Null'

   mike

-- 
Mike Meyer [EMAIL PROTECTED]  http://www.mired.org/home/mwm/
Independent WWW/Perforce/FreeBSD/Unix consultant, email for more information.
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Microsoft Hatred FAQ

2005-10-27 Thread Mike Meyer
Paul Rubin http://[EMAIL PROTECTED] writes:

 Sorry to be pedantic, but I think it's an important point that no court 
 ever found that Microsoft illegally acquired a monopoly. So to characterize 
 the monopoly itself as illegal is simply erroneous.

 Who is paying you to tell these ridiculous crap?  The monopoly is illegal
 if maintained by anticompetitive means regardless of how it was acquired.
 From http://cyber.law.harvard.edu/msdoj/conclusions-l.html:

Section 2 of the Sherman Act declares that it is unlawful for a person
or  firm  to monopolize . . . any part of the trade or commerce among
the  several  States,  or with foreign nations . . . . 15 U.S.C. § 2.
This language operates to limit the means by which a firm may lawfully
either  acquire  or  perpetuate  monopoly  power. Specifically, a firm
violates  sec.  2  if  it  attains  or  preserves  monopoly power through
anticompetitive acts.

The threshold element of a  sec 2 monopolization offense being the
possession of monopoly power in the relevant market...

Which means that the successful exercise of monopoly power is
sufficient to demonstrate the existence of a monopoly. A claim that
David Schwartz has never bothered to answer.

 David Schwartz, I have a direct question for you: are you on
 Microsoft's payroll?

I've noticed something strange that makes me wonder the same
thing. Everytime someone compares MS's behavior with that of any other
criminals, he responds about MS's activity being equated to that of
criminals with guns, and refuses to discuss the issue. Ironically,
while no one else has so much as compared MS to criminals with guns -
after all, they're white collar criminals - David Schwartz called the
DOJ official who were investigating MS criminals with guns pointed
out [MS officers] heads.

It seems like he's trying to avoid (further) tarnishing MS's
reputation by avoiding having MS associated with other criminals. You
have to wonder what could caause that kinnd of behavior.

 mike
-- 
Mike Meyer [EMAIL PROTECTED]  http://www.mired.org/home/mwm/
Independent WWW/Perforce/FreeBSD/Unix consultant, email for more information.
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Microsoft Hatred FAQ

2005-10-27 Thread Mike Meyer
David Schwartz [EMAIL PROTECTED] writes:
 Mike Meyer [EMAIL PROTECTED] wrote in message 
 news:[EMAIL PROTECTED]
 I've noticed something strange that makes me wonder the same
 thing. Everytime someone compares MS's behavior with that of any other
 criminals, he responds about MS's activity being equated to that of
 criminals with guns, and refuses to discuss the issue. Ironically,
 while no one else has so much as compared MS to criminals with guns -
 after all, they're white collar criminals - David Schwartz called the
 DOJ official who were investigating MS criminals with guns pointed
 out [MS officers] heads.
 I can't understand why you would post an outright lie like this. 

I, on the other hand, understand why you accuse me of lying. You don't
have an answer to the claim, so you call the claim false. SOP for you.

 Ironically, while no one else has so much as compared MS to criminals with 
 guns. I defy you to find *one* place where I complain that MS behavior is 
 equated to the actual use of force where that is not in fact done in 
 precisely the thread I'm replying to.

The first one is at:

http://groups.google.com/group/comp.lang.python/msg/8422f41c9fe137b0?dmode=sourcehl=en

The original comment was:

 No, they didn't ask for more than Windows were worth. They tilted the
 playing field against MS competitors by causing consumers to pay MS
 money for products they didn't receive. In most countries, taking
 money from unwilling victims without giving them anything in exchange
 is called theft.

Note that no mention is made of guns or force - just a definition of
theft. Unless you're so narrowminded that nothing short of pointing a
gun at someone and demanding money from them is stealing from them,
there is no way that this can be equated to the actual use of
force. And if you do believe that definition of stealing, I'll do your
bookkeeping for free - and I won't steal from you.

Your reply:

It is not theft if you can simply say no to the deal and all that 
happens is that you don't get the product. Your argument is preposterous. If 
you accept arguments that equate guns with arguments, the next step is that 
using a gun is a rational response to an argument one doesn't like. Oh wait, 
you're already there.

Wherein you accuse me of equating MS's actions with using guns, which
is *exactly* what I said you do.

There are lots more examples of you doing this kind of thing. Like I
said, everytime someone compares MS's behavior with some less
controversial criminal behavior, you act like they accused MS of
holding people up at gunpoint.

mike
-- 
Mike Meyer [EMAIL PROTECTED]  http://www.mired.org/home/mwm/
Independent WWW/Perforce/FreeBSD/Unix consultant, email for more information.
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: specify arbitrary library directory directly in code?

2005-10-27 Thread Mike Meyer
[EMAIL PROTECTED] writes:

 ### Initial Findings
 After searching around a bit, it appears that python does not have a
 built-in mechanism for specifying arbitrary directories as the first
 place to look for module code. There are options that allow one to
 specify alternate directories, but all these options require changes
 to environment variables, or changes (or additions) to the files in
 the standard package directories.

 But if you define 'built-in mechanism' meaning a change that only
 requires modifying the code in 'faa_script.py' ... then there is
 no 'built-in mechanism'.

 ### Question
 Are the initial findings correct? If not, can you specify the code
 (or the link to the documentation) that specifies how to do what I
 am trying to do? If the initial findings are correct, are there any
 alternative workarounds to allow faa_script.py to automatically
 discover module code in specific sibling directories?

No, the findings aren't correct. You can manipulate the search path
directly.

import sys
sys.path.insert(0, /directory/to/search)

will cause future imports to search /directory/to/search before all
other directories. Likewise, sys.append(/directory/to/search) will
search it after other directories.

Docs here: 
http://www.python.org/doc/2.2.3/tut/node8.html#SECTION00820

 mike
-- 
Mike Meyer [EMAIL PROTECTED]  http://www.mired.org/home/mwm/
Independent WWW/Perforce/FreeBSD/Unix consultant, email for more information.
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Microsoft Hatred FAQ

2005-10-27 Thread Mike Meyer
David Schwartz [EMAIL PROTECTED] writes:

 Mike Meyer [EMAIL PROTECTED] wrote in message 
 news:[EMAIL PROTECTED]

 Ironically, while no one else has so much as compared MS to criminals 
 with
 guns. I defy you to find *one* place where I complain that MS behavior 
 is
 equated to the actual use of force where that is not in fact done in
 precisely the thread I'm replying to.

 The first one is at:

 http://groups.google.com/group/comp.lang.python/msg/8422f41c9fe137b0?dmode=sourcehl=en

 The original comment was:

 No, they didn't ask for more than Windows were worth. They tilted the
 playing field against MS competitors by causing consumers to pay MS
 money for products they didn't receive. In most countries, taking
 money from unwilling victims without giving them anything in exchange
 is called theft.

 Note that no mention is made of guns or force - just a definition of
 theft. Unless you're so narrowminded that nothing short of pointing a
 gun at someone and demanding money from them is stealing from them,
 there is no way that this can be equated to the actual use of
 force. And if you do believe that definition of stealing, I'll do your
 bookkeeping for free - and I won't steal from you.

 Your reply:

It is not theft if you can simply say no to the deal and all that
happens is that you don't get the product. Your argument is preposterous. 
If
you accept arguments that equate guns with arguments, the next step is 
that
using a gun is a rational response to an argument one doesn't like. Oh 
wait,
you're already there.

 Wherein you accuse me of equating MS's actions with using guns, which
 is *exactly* what I said you do.

 This thread is large and complex, and I can't always know exactly what's 
 a reply to what reply to what. So what's said in what part of a thread may 
 carry over to another part of that same thread.

So follow the link and read it. I quoted the comment and reply
directly to make life easier on the readers. I quoted them exactly in
context. That you try and deny they illustrate you doing exactly what
I said you do is only to be expected.

 There are lots more examples of you doing this kind of thing. Like I
 said, everytime someone compares MS's behavior with some less
 controversial criminal behavior, you act like they accused MS of
 holding people up at gunpoint.

 They are. Read the quotes. Here they are again:

So what? That doesn't change the essential truth of my statement -
that you react to *every* comparison of MS's activities with less
controversial criminal activity with the You're comparing them to
criminals with guns. I won't discuss that. It really does make me
think that you're more interested in protecting MS's reputation than
in any discussinon.

Unless you're so narrowminded that nothing short of pointing a
gun at someone and demanding money from them is stealing from them,
there is no way that this can be equated to the actual use of
force. And if you do believe that definition of stealing, I'll do your
bookkeeping for free - and I won't steal from you.
 You are seriously saying that people in this thread have not 
 consistently described Microsoft's actions as analogous to an actual use of 
 force? Have you read any of the thread? Do I need to dig out more quotes?

Yes, I've read the thread. It's full of you creating straw men,
calling those who disagree with you liars, calling the government
crooks, and the like. I'd be interested in seeing *one* quote that
compare MS's actions to the actual use of force. And I want what I
gave you - the link to the google groups page the quote came from, and
enough context to find it.

The quote about the mafia doesn't compare MS's actions to actual use
of force. It compares MS to people who are willing to use force to
get their ends. But there is no actual use of force.


 These are all from early in the thread, long before the posts you are 
 complaining about:
 The choice was go along with MS arm twisting or go out of business.

No, this wasn't long before the post I quoted; it occured well after
it. And while this really does refer to the actual use of force,
anyone even vaguely familiar with common english usage will recognize
the phrase arm twisting as an idiomatic usage for a being extremely
persuasive, with no actual use of force taking place. If that's the
best you can do, you really haven't got an argument.

 To my way of thinking what MS did was similar to a the only magasine
 wholesaler in town telling retailers it had to sell kiddie porn under
 the table or pay full retail for all magazines.

No actual use of force in this one, either. This is a *very* apt
comparison. The only real difference between this and what MS did is
that it replaces something mildly objectionable - charging people for
something they aren't getting - with something very objectionable -
selling kiddie porn.

 However, you may be right that some of my replies to you may not have 
 been justified as responses to just

Re: a Haskell a Day

2005-10-26 Thread Mike Meyer
Erik Max Francis [EMAIL PROTECTED] writes:
 Xah Lee wrote:
 This is my learning notes on Haskell. I call it a-Haskell-a-day.

Interesting. Xah Lee's orginals don't show up in the Python
newsgroup. No big loss.

In any case, please help google find Xah Lee under the proper search
terms. Add a link to his website with the anchor stupider than spam.
A typical usage might be:

And pointing out that some people are a
href=http://xahlee.org/;stupider than spam/a.

Thanks,
mike
-- 
Mike Meyer [EMAIL PROTECTED]  http://www.mired.org/home/mwm/
Independent WWW/Perforce/FreeBSD/Unix consultant, email for more information.
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Double replace or single re.sub?

2005-10-26 Thread Mike Meyer
Iain King [EMAIL PROTECTED] writes:

 I have some code that converts html into xhtml.  For example, convert
 all i tags into em.  Right now I need to do to string.replace calls
 for every tag:

 html = html.replace('i','em')
 html = html.replace('/i','/em')

 I can change this to a single call to re.sub:

 html = re.sub('([/]*)i', r'\1em', html)

 Would this be a quicker/better way of doing it?

Maybe. You could measure it and see. But neither will work in the face
of attributes or whitespace in the tag.

If you're going to parse [X]HTML, you really should use tools that are
designed for the job. If you have well-formed HTML, you can use the
htmllib parser in the standard library. If you have the usual crap one
finds on the web, I recommend BeautifulSoup.

  mike
-- 
Mike Meyer [EMAIL PROTECTED]  http://www.mired.org/home/mwm/
Independent WWW/Perforce/FreeBSD/Unix consultant, email for more information.
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Set an environment variable

2005-10-26 Thread Mike Meyer
Jorgen Grahn [EMAIL PROTECTED] writes:
 On Wed, 26 Oct 2005 07:42:19 -0700, Alex Martelli [EMAIL PROTECTED] wrote:
 Grant Edwards [EMAIL PROTECTED] wrote:
 On 2005-10-24, Eric Brunel [EMAIL PROTECTED] wrote:
  The only think you can export an environment variable to is a
  child process
  Well, you know that, and I know that too. From my experience,
  many people don't...
 True.  Using Unix for 20+ years probably warps one's perception
 of what's obvious and what isn't.
 This specific issue is identical in Windows, isn't it?  I do not know
 any OS which does have the concept of environment variable yet lets
 such variables be ``exported'' to anything but a child process.
 AmigaDOS, if I recall correctly. Its ENV: drive/namespace is global, and
 that's its closest thing to Unix environment variables.

AmigaDOS had both global environment variables (using the ENV: device)
and local environment variables, that worked like the Unix
version. You manipulated them in a similar way in the shell, and they
had a similar API for programmers: one call with a flag to indicate
which you wanted. The ENV: device was an implementation detail that
let you save/restore the state of the global environment with file
commands. The posix calls checked the local then global variables.

Of course, this is now 10+ year old memory, and I may not RC.

   mike
-- 
Mike Meyer [EMAIL PROTECTED]  http://www.mired.org/home/mwm/
Independent WWW/Perforce/FreeBSD/Unix consultant, email for more information.
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Read/Write from/to a process

2005-10-25 Thread Mike Meyer
Dennis Lee Bieber [EMAIL PROTECTED] writes:
   Followed by the Amiga... The Amiga IPC used message ports (linked
 lists owned by the creating process to which, if public [named], other
 processes could send message packets). The Amiga port of REXX made use
 of message ports as its native IPC...

 address SOME_APPLICATION

 would redirect all non-REXX statements to the message port named
 SOME_APPLICATION; the default being address COMMAND -- a shell
 interpreter. Then, any non-REXX statement would be processed by the
 shell interpreter -- and the easiest way to force a non-REXX statement
 was to put a quote mark around it (or the first word of it)

 delete my_file_var

 would translate my_file_var to whatever had been assigned to it earlier,
 and pass a delete command to the shell.

 address ED

 would now route such statements to the ed editor

These things weren't unique to the Amiga port of Rexx. Rexx on VM/CMS
did all these things. Tying them to the native IPC mechanism as a
brilliant move. Add in the shared library for supporting Rexx so that
adding Rexx support to an application (or to a language!) was trivial,
and Rexx the end result was that Rexx was used far more heavily on the
Amiga than on VM/CMS.

The downside was that variables had a default value of their name in
upper case, and any statement that wasn't recognized evaluated as a
string expresion and then fed to the current command processor. So
that the quote marks weren't needed - if you hadn't assigned a value
to the variable delete, then you could just do

   delete my_file_var

and it would do the right thing. Of course, almost anything you typed
like that was valid, creating interesting bugs. I made it a habit to
keep 0 and 1 commands around that just logged errors, as those
would catch such things.

  mike
-- 
Mike Meyer [EMAIL PROTECTED]  http://www.mired.org/home/mwm/
Independent WWW/Perforce/FreeBSD/Unix consultant, email for more information.
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: security

2005-10-25 Thread Mike Meyer
Mattia Adami [EMAIL PROTECTED] writes:

 Hi to all.
 I'm intristing in write a plugin for browsers that can execute python
 code.
 I know the main problem is security. Many thread were opened about this
 in the ng.
 I would know if fork python rewriting some library could avoid
 problems. I.e. one problem is the possibility to access files. If I
 rewrite the open() function so that raises exception if the program try
 to access a file out of a defined directory.
 I'm sure not a security expert, so please be patient if my question is
 stupid.

People who *are* security experts have looked into modifying Python to
make it secure, and given up on the project as unrealistic. Generally,
taking an existing project that wasn't designed with security in mind
and making it secure is hard. Not impossible, but not easy.

You might consider using Jython and jythonc to produce objects to run
in the JVM. Not only was that designed with security in mind, but most
browsers come with a JVM already installed.

 mike
-- 
Mike Meyer [EMAIL PROTECTED]  http://www.mired.org/home/mwm/
Independent WWW/Perforce/FreeBSD/Unix consultant, email for more information.
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: [OT] Re: output from external commands

2005-10-25 Thread Mike Meyer
Terry Hancock [EMAIL PROTECTED] writes:
 I think Mr. Lundh's point was only that the output from glob.glob is already
 guaranteed to be strings, so using either '%s'%f or str(f) is superfluous.

Just for the record - this was why I asked what the point was in the
first place.

  mike
-- 
Mike Meyer [EMAIL PROTECTED]  http://www.mired.org/home/mwm/
Independent WWW/Perforce/FreeBSD/Unix consultant, email for more information.
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Newbie question: string replace

2005-10-25 Thread Mike Meyer
[EMAIL PROTECTED] [EMAIL PROTECTED] writes:

 So how to overwrite the config file directly in script.py instead of
 running script.py with two params?

Don't overwrite the file directly. Save a copy, then rename it. That
way, you don't replace the new version until you know the old version
is safely written to disk, thus avoid accidently losing the data in
the file.

def modfile(filein, mod):
fileout = filein + ' temp'
fin = open(filein, 'r')
fout = open(fileout, 'w')
fout.write(mod(fin.read()))
fin.close()
fout.close()
os.unlink(filein)
os.rename(fileout, filein)

The truly paranoid will replace os.unlink(filein) with
os.rename(filein, filein + '.back').

mike
-- 
Mike Meyer [EMAIL PROTECTED]  http://www.mired.org/home/mwm/
Independent WWW/Perforce/FreeBSD/Unix consultant, email for more information.
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Microsoft Hatred FAQ

2005-10-25 Thread Mike Meyer
Not Bill Gates [EMAIL PROTECTED] writes:
 [EMAIL PROTECTED] wrote...
 On Tue, 25 Oct 2005 15:35:47 +, Not Bill Gates wrote:
  Heck, I dunno.  Like you, I don't even really care all that much.  
 You don't care that innovation in desktop software has been crippled by
 the actions of the monopoly player Microsoft?
 You need to first prove innovation in desktop software has been 
 crippled, don't you?

MS took desktop software through pretty much the same sequence of
offerings that the mainframe and minicomputer software industry had
been throgh: flat file systems and single-tasking OS's in a command
line environment, adding nested file systems, adding TSRs, adding a
windowing environment, adding true multitasking and finally
multiprocessor systems. This took them what - 20+ years?

While MS was innovating by giving us directories, others who had
learned the lessons from mainframe and minicomputer systems were
offering us desktop systems with all those features - and an office
suite that ran in the windowing systems - for a fraction of the price
of anything that was capable of running MS-DOS.

MS managed to kill off or drive into niche markets the companies who
were actually doing innovative work on desktop systems, and it's taken
the desktop software industry two decades to recover from that. I'll
accept that as crippling until a better definition comes along.

   mike
-- 
Mike Meyer [EMAIL PROTECTED]  http://www.mired.org/home/mwm/
Independent WWW/Perforce/FreeBSD/Unix consultant, email for more information.
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Top-quoting defined [was: namespace dictionaries ok?]

2005-10-25 Thread Mike Meyer
 On Tuesday 25 October 2005 00:31, Duncan Booth wrote:
 P.S. James, *please* could you avoid top-quoting
 James Stroud [EMAIL PROTECTED] writes:
 I'm sorry if I can't find a dumb terminal for a VAX with which to read my 
 email. Perhaps, if i could, I would understand your frustration a little 
 better.

You don't need a VAX. All it takes is trying to dethread a top-posted
conversation. I've given up in frustration on more than one such
venture.

 The only reason I'm making a point of this is that many people come to 
 python from fields other than computer science or hacker-ology. Posting 
 styles will 
 vary considerably among these people, so deal with the deviants carefully. 

I belive Duncan did so. He asked you to please* avoid to
top-posting.

 Their differences in styles, like mine, probably arise from the culture of 
 their respective fields. Most, like me, may not even know what the heck you 
 are talking about. Also, here is a well written synopsis of the arguments in 
 favor of top-posting and they may even be strong enough to legitimize the 
 practice:
http://alpage.ath.cx/toppost/toppost.htm

Well, if I could find a 300 baud modem, I might understand his
objection to not top-posting (he really only has one). His comments
about why people shouldn't complain about top-posting are full of
suggestions that don't generally work on netnews and mail, though they
may be valid elsewhere, and his suggestion about when to top-post
leaves out the *very important* criteria that you don't expect to read
any replies to your comment, and don't care if you make life difficult
for those who have different expectations.

He's right about inadequately-trimmed repklies, though. Not being on a
300 baud modem, I don't think they're as annoying as top-posting, but
they are certainly something that we can do without.

 In light of these arguments, I hereby reserve the right to revert to 
 top-posting if the compulsion overwhelms me.

That's your right. Be aware that people will ignore, correct and/or
complain about you doing so.

 mike
-- 
Mike Meyer [EMAIL PROTECTED]  http://www.mired.org/home/mwm/
Independent WWW/Perforce/FreeBSD/Unix consultant, email for more information.
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Microsoft Hatred FAQ

2005-10-25 Thread Mike Meyer
Not Bill Gates [EMAIL PROTECTED] writes:

 [EMAIL PROTECTED] wrote...
 Not Bill Gates [EMAIL PROTECTED] writes:
  [EMAIL PROTECTED] wrote...
  On Tue, 25 Oct 2005 15:35:47 +, Not Bill Gates wrote:
   Heck, I dunno.  Like you, I don't even really care all that much.  
  You don't care that innovation in desktop software has been crippled by
  the actions of the monopoly player Microsoft?
  You need to first prove innovation in desktop software has been 
  crippled, don't you?
 
 MS took desktop software through pretty much the same sequence of
 offerings that the mainframe and minicomputer software industry had
 been throgh: flat file systems and single-tasking OS's in a command
 line environment, adding nested file systems, adding TSRs, adding a
 windowing environment, adding true multitasking and finally
 multiprocessor systems. This took them what - 20+ years?

 shrug  Being pissed off about how things could have been done 
 better is a losing proposition.

I'm not pissed off about it - I've got better things to do. You asked
for prove that desktop software development was crippled by MS. I
provided it.

   mike
-- 
Mike Meyer [EMAIL PROTECTED]  http://www.mired.org/home/mwm/
Independent WWW/Perforce/FreeBSD/Unix consultant, email for more information.
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: output from external commands

2005-10-24 Thread Mike Meyer
darren kirby [EMAIL PROTECTED] writes:
 If all you want is filenames this will work:
 import glob
 files = [%s % f for f in glob.glob(*)]

What's the point of doing %s % f? How is this different from just
file = [f for f in glob.glob(*)]?

 mike
-- 
Mike Meyer [EMAIL PROTECTED]  http://www.mired.org/home/mwm/
Independent WWW/Perforce/FreeBSD/Unix consultant, email for more information.
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Importing at runtime

2005-10-24 Thread Mike Meyer
David Poundall [EMAIL PROTECTED] writes:
 I have several .py files in a directory that I would like to import at
 run time.  Each file contains a state machine that requires to be run
 in its own thread.

Imports happen at run time. Beware starting threads in the code run at
import time in each module, though - there are some nasty bugs lurking
there. Instead, start the threads in functions invoked from the main
routine.

 The first problem I have is how can I import the code in all of the .py
 files without knowing the file names in advance.

 Can this be done ??

Yes. Use the __import__ builtin.

 mike
-- 
Mike Meyer [EMAIL PROTECTED]  http://www.mired.org/home/mwm/
Independent WWW/Perforce/FreeBSD/Unix consultant, email for more information.
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Microsoft Hatred FAQ

2005-10-23 Thread Mike Meyer
entropy [EMAIL PROTECTED] writes:
 [EMAIL PROTECTED] wrote...
 [EMAIL PROTECTED] wrote...
  In comp.lang.perl.misc David Schwartz [EMAIL PROTECTED] wrote:
   Mike Meyer [EMAIL PROTECTED] wrote in message 
   Sorry, but nobody but the government actually owns property. In most
   places, you can't make non-trivial changes to your property without
   permission from the government. They even charge you rent on your
   property, only they call it property tax.
  I see you are a totalitarianist or perhaps a communist. If you want 
   to 
   live in America and discuss things that are relevent to America, let me 
   know.
  Why would you say that - Mike Meyer made a point to which you have
  obviously no answer. Or do you deny that his comments on this matter
  of property are true?
 Methinks David simply missed that Mike was being facetious.  (Irony 
 and facetiousness don't translate well into print, as Frank Zappa 
 once noted.)
 Uh, you _were_ being facetious there, weren't you Mike?

No, I wasn't. The statements I made are true: the government charges
you taxes on your property, and in most places restricts the changes
you can make to it and the things you can do in it. I used the words
rent and ownership in an unconventional way to emphasize the
point.

The conventional usage of ownwership ignores these kinds of
facts. So you can talk about your house even if you're renting it,
or if the bank still owns most of the house. There are political
groups that are unhappy with these facts, and like to point out the
inconsistencies in the usage of the word ownership. See URL:
http://www.huppi.com/kangaroo/L-propertyrights.htm  and URL:
http://magazine.14850.com/9307/politics.html  for examples of this.

David claimed that everyone had a right to do whatever they wanted
with their property. This is simply false throughout most of the
civilized world - zoning laws control what kinds of business you can
run on your property, various laws designed to control the looks of
the town dictate what you can do to the exterior or lawn, flood and
earthquake laws state what kinds of structural changes you can make,
and so on. I took the view of a political extremist to point out that
he was wrong. David predictably used that to tar me as an extremist
from the other end of the spectrum.

 mike
-- 
Mike Meyer [EMAIL PROTECTED]  http://www.mired.org/home/mwm/
Independent WWW/Perforce/FreeBSD/Unix consultant, email for more information.

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


Re: Microsoft Hatred FAQ

2005-10-23 Thread Mike Meyer
David Schwartz [EMAIL PROTECTED] writes:
 Instead, you outline a class of actions and tag them
 all as illegal. That's why we have laws against assault and battery
 and unsafe driving. And laws against exercising monopoly power in an
 unfair manner.
 Interesting how you, again, equate a gun and an argument. It is very 
 important to you to justify responding to arguments with guns. However, I 
 reject that premise at its roots, not just in your application of it.

Another straw man. I never mentioned the word gun at all, and none
of the crimes I discussed require a gun.

You apparently aren't interested in constructive intercourse on the
question. You're just interesting in knocking down your own
arguments. Personally, I'd rather not watch you masterbate.

   mike
-- 
Mike Meyer [EMAIL PROTECTED]  http://www.mired.org/home/mwm/
Independent WWW/Perforce/FreeBSD/Unix consultant, email for more information.
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: how to count and extract images

2005-10-23 Thread Mike Meyer
Joe [EMAIL PROTECTED] writes:
 start = s.find('a href=somefile') + len('a
 href=somefile') 
 stop = s.find('Save File/a/B',
 start) fileName = s[start:stop]
 and then construct the url with the filename to download the image 
 which works fine as cause every image has the Save File link and I can
 count number of images easy the problem is when there is more than image I
 try using while loop downlaod files, wirks fine for the first one but
 always matches the same, how can count and thell the look to skip the fist
 one if it has been downloaded and go to next one, and if next one is
 downloaded go to next one, and so on.

To answer your question, use the first optional argument to find in both
invocations of find:

stop = 0
while end = 0:
  start = s.find('a href=somefile', stop) + len('a href=somefile')
  stop = s.find('Save File/a/B', start)
  fileName = s[start:stop]

Now, to give you some advice: don't do this by hand, use an HTML
parsing library. The code above is incredibly fragile, and will break
on any number of minor variations in the input text.  Using a real
parser not only avoids all those problems, it makes your code shorter.
I like BeautifulSoup:

soup = BeautifulSoup(s)
for anchor in soup.fetch('a'):
fileName = anchor['href']

to get all the hrefs. If you only want the ones that have Save File
in the link text, you'd do:

soup = BeautifulSoup(s)
for link in soup.fetchText('Save File'):
fileName = link.findParent('a')['href']

mike
-- 
Mike Meyer [EMAIL PROTECTED]  http://www.mired.org/home/mwm/
Independent WWW/Perforce/FreeBSD/Unix consultant, email for more information.
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Syntax across languages

2005-10-23 Thread Mike Meyer
Tom Anderson [EMAIL PROTECTED] writes:
 On Sun, 23 Oct 2005, Fredrik Lundh wrote:
 [EMAIL PROTECTED] wrote:
 - ~== for approximate FP equality
 str(a) == str(b)
 This is taken from the AI 754 standard, i take it? :)

 Seriously, that's horrible. Fredrik, you are a bad man, and run a bad
 railway.

 However, looking at the page the OP cites, the only mention of that
 operator i can find is in Dylan, and in Dylan, it's nothing to do with
 approximate FP equality - it means 'not identical', which we can spell
 is not.

 What would approximate FP equality even mean? How approximate?

Hopefully user defined. Rexx has a global control that lets you set
the number of digits to be considered significant in doing an FP
equality test.

 mike
-- 
Mike Meyer [EMAIL PROTECTED]  http://www.mired.org/home/mwm/
Independent WWW/Perforce/FreeBSD/Unix consultant, email for more information.
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: output from external commands

2005-10-23 Thread Mike Meyer
James Colannino [EMAIL PROTECTED] writes:

 Hey everyone.  First off, I'm new to the list.  I had had a little bit
 of experience with Perl before discovering Python.  The more Python I
 learn, the more I love it :)  I just have a quick question to ask.  I
 know that this is probably a simple question, but I've been googling
 around, and partly because I'm not sure exactly what to search for,
 I've been unsuccessful at finding an answer.  What I'd like to do is
 be able to take the output of an external command and assign it as an
 array of strings.  So, for example, in Perl I could do something like:

 @files = `ls`;

 So I guess I'm looking for something similiar to the backticks in
 Perl.  Forgive me if I've asked something that's a bit basic for this
 list.  Any help would be greatly appreciated :)  Thanks very much in
 advance.

This is a scripting language feature. Python doesn't have direct
support for it, any more than C++ does. To get that functionality, you
want to use either the os.popen function, or - preferable, but only
available in newer Pythons - the subprocess module.

  mike
-- 
Mike Meyer [EMAIL PROTECTED]  http://www.mired.org/home/mwm/
Independent WWW/Perforce/FreeBSD/Unix consultant, email for more information.
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Syntax across languages

2005-10-23 Thread Mike Meyer
Dennis Lee Bieber [EMAIL PROTECTED] writes:
 On Sun, 23 Oct 2005 20:59:46 -0400, Mike Meyer [EMAIL PROTECTED] declaimed
 the following in comp.lang.python:

 Hopefully user defined. Rexx has a global control that lets you set
 the number of digits to be considered significant in doing an FP
 equality test.
 
   That's the good part about the REXX model...

   The bad part? It's based on the number of significant digits...
 Rather than a true epsilon.

Depends on what you mean by a true epsilon. If you mean I want the
two values to differ by at most 10^-5, then number of significant
digits is a better option. If you mean I want the two values to
differ by at most 1 part in 10^5, then number of significant digits
is a slightly less flexible option.

You really, *really* don't want to specify the acceptable difference
as a fixed value. You want it to be relative to the size of objects
being compared. The latter is what number of significant digits gives
you. The downside is that you're restricted to values that are a power
of 10, and can't specify things like I want the two values to differ
by at most one part in 34,000 or some such oddity. On the up side,
number of significant digits is (maybe was - been a long time since I
did any serious number crunching) a common form for expressing the
accuracy of a calculation.

 mike
-- 
Mike Meyer [EMAIL PROTECTED]  http://www.mired.org/home/mwm/
Independent WWW/Perforce/FreeBSD/Unix consultant, email for more information.
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: best way to replace first word in string?

2005-10-22 Thread Mike Meyer
Steven D'Aprano [EMAIL PROTECTED] writes:
 py def tester(n):
 ... s1 = 
 ... s2 = %s * n
 ... bytes = tuple([chr(i % 256) for i in range(n)])
 ... t1 = time.time()
 ... for i in range(n):
 ... s1 = s1 + chr(i % 256)
 ... t1 = time.time() - t1
 ... t2 = time.time()
 ... s2 = s2 % bytes
 ... t2 = time.time() - t2
 ... assert s1 == s2
 ... print t1, t2
 ...
 py 
 py tester(x)
 3.24212408066 0.01252317428
 py tester(x)
 2.58376598358 0.01238489151
 py tester(x)
 2.76262307167 0.01474809646

 The string formatting is two orders of magnitude faster than the
 concatenation. The speed difference becomes even more obvious when you
 increase the number of strings being concatenated:

The test isn't right - the addition test case includes the time to
convert the number into a char, including taking a modulo.

I couldn't resist adding the .join idiom to this test:

 def tester(n):
...  l1 = [chr(i % 256) for i in range(n)]
...  s1 = 
...  t1 = time.time()
...  for c in l1:
...   s1 += c
...  t1 = time.time() - t1
...  s2 = '%s' * n
...  l2 = tuple(chr(i % 256) for i in range(n))
...  t2 = time.time()
...  s2 = s2 % l2
...  t2 = time.time() - t2
...  t3 = time.time()
...  s3 = ''.join(l2)
...  t3 = time.time() - t3
...  assert s1 == s2
...  assert s1 == s3
...  print t1, t2, t3
... 
 tester(x)
0.0551731586456 0.0251281261444 0.0264830589294
 tester(x)
0.0585241317749 0.0239250659943 0.0256059169769
 tester(x)
0.0544500350952 0.0271301269531 0.0232360363007

The order of magnitude now falls to a factor of two. The original
version of the test on my box also showed an order of magnitude
difference, so this isn't an implementation difference.

This version still includes the overhead of the for loop in the test.

The join idiom isn't enough faster to make a difference.

 py tester(x*10)
 2888.56399703 0.13130998611

 tester(x * 10)
1.22272014618 0.252701997757 0.27273607254
 tester(x * 10)
1.21779584885 0.255345106125 0.242965936661
 tester(x * 10)
1.25092792511 0.311630964279 0.241738080978
 

Here we get the addition idiom being closer to a factor of four
instead of two slower. The .joim idiom is still nearly identical.

mike
-- 
Mike Meyer [EMAIL PROTECTED]  http://www.mired.org/home/mwm/
Independent WWW/Perforce/FreeBSD/Unix consultant, email for more information.
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Question about inheritance...

2005-10-22 Thread Mike Meyer
KraftDiner [EMAIL PROTECTED] writes:

 I have a base class called Shape
 And then classes like Circle, Square, Triangle etc, that inherit from
 Shape:

 My quesiton is can a method of the Shape class call a method in Circle,
 or Square etc...?

Yup:

 class Shape(object):
...  def commented_draw(self):
...   print Drawing, self.__class__.__name__
...   self.draw()
... 
 class Circle(Shape):
...  def draw(self):
...   print Drawing a Circle
... 
 c = Circle()
 c.commented_draw()
Drawing Circle
Drawing a Circle
 

Or maybe you meant invoking them directly, which a method in Shape
would do by calling Circle.draw(self). The latter is ugly - you should
use self.draw() to invoke the draw routine that's correct for self.

mike
-- 
Mike Meyer [EMAIL PROTECTED]  http://www.mired.org/home/mwm/
Independent WWW/Perforce/FreeBSD/Unix consultant, email for more information.
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: High Order Messages in Python

2005-10-22 Thread Mike Meyer
[EMAIL PROTECTED] [EMAIL PROTECTED] writes:

 I'm reading about high order messages in Ruby by Nat Pryce, and
 thinking if it could be  util and if so, if it could be done in Python.
 Someone already tried?

Yes, I'm pretty sure it could be done in Python. All it really needs
is the ability to catch references to undefined attributes, which
Python has. You make the HOM of your collection class return an object
with a reference to self, and the __getattr__ method of that classs
then invokes getattr on each object in the referenced collection
instance for the undefined method, manipulating the result as
appropriate for that HOM, and returning a new list.

But this really isn't a very good fit for Python. This is really a
feature for more aggressively OO languages. To make the examples HOMs
described really useful, you want to add them to some base class for
collections. But that base class doesn't exist in Python - Python just
isn't that OO.

You could add various HOMs to lists, tuples, generators and iterators
- but then anyone who wanted to create a new sequence class would have
to add all of the HOMs that their clients might want to use. The
pythonic way would be to add a function that works with all the
various sequence types - which would then automatically work with any
user-defined classes that quacked like a sequence. It's not very OO -
but it is pythonic. In fact, Python already has functions that capture
the functionality of the example HOMs in the links you posted: where
and unless are handled by filter. in_order_of and in_reverse_order_of
are handled by sorted. do is is handled by map. Of course, recent
versions of python provide list comprehensions as preferable to some
of these functions.

That said, HOM's are a *very* powerful mechanism. The examples - and
my discussion of them - cover just one very broad use case. There may
be others where they are a better fit with Python. Having examples of
how to do these kinds of things around is probably worthwhile.

mike
-- 
Mike Meyer [EMAIL PROTECTED]  http://www.mired.org/home/mwm/
Independent WWW/Perforce/FreeBSD/Unix consultant, email for more information.
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Microsoft Hatred FAQ

2005-10-22 Thread Mike Meyer
 that 
 both OSX and Linux competed with it.

I guess it hasn't sunk in yet that the existence of competition
doesn't keep a company from being a monopoly. But it does. Companies
that owned less of their market than MS owns of it's market have been
broken up. A classic monopoly behavior is to want the competition to
survive just barely, because that gives them leverage in
court. Which is why MS has helped out Apple in the past.

 There is no way Microsoft could have expected the
 market to be defined in this way and no way to argue that Microsoft had 
 any
 reason to believe their conduct was illegal.
 Microsoft have lawyers. Microsoft destroyed emails and at least one senior
 manager perjured himself in court. Microsoft created a fake video
 demonstration which they then gave as evidence. Do you really believe that
 Microsoft's executives are so incompetent that they don't get legal advice
 before writing up contracts? Or that nobody in authority at Microsoft
 realised that destroying evidence and lying to a judge are crimes?
 When a criminal willing to use force points a gun at your head, you lie 
 to him.

You sound like an anarchist to me. This wasn't a criminal, this was
the government. Lieing to random individuals isn't a crime. Lieing to
the government is.

 In any case, even if you are right that Microsoft had no ideas... so what?
 Ignorance of the law never has been an excuse for criminal behaviour. It
 has always been every individual's responsibility to make sure that they
 do not act illegally, and that goes for companies as well.
 I am not saying Microsoft did not know the law. I am saying that no 
 rational person could have expected the law to be applied to Microsoft that 
 way it was. The law *must* put a person on notice of precisely what conduct 
 it prohibits. However, in this case, the law's applicability was conditioned 
 on an abritrary and irrational choice of what the relevant market was.

MS has a long history of dancing with the DOJ, and has been repeatedly
warned about the legality - or lack thereof - of their behavior. No
rational person who knew of that history could expect the law to be
applied to MS in any way other than the way it was.

   mike
-- 
Mike Meyer [EMAIL PROTECTED]  http://www.mired.org/home/mwm/
Independent WWW/Perforce/FreeBSD/Unix consultant, email for more information.
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Python vs Ruby

2005-10-22 Thread Mike Meyer
Steven D'Aprano [EMAIL PROTECTED] writes:
 On Fri, 21 Oct 2005 13:03:29 +0100, Alex Stapleton wrote:
 Perl is more like a CISC CPU. There are a million different commands.
 Python is more RISC like.
 Line count comparisons = pointless.

 Not so.

 Every line = more labour for the developer = more cost and time.
 Every line = more places for bugs to exist = more cost and time.

There were studies done in the 70s that showed that programmers
produced the same number of debugged lines of code a day no matter
what language they used. So a language that lets you build the same
program with fewer lines of code will let you build the program in
less time.

 I find it sometimes helps to imagine extreme cases. Suppose somebody comes
 to you and says Hi, I want you to develop a web scrapping application to
 run on my custom hardware. You look at the project specifications and
 realise that the hardware has no OS, no TCP/IP, no file manager, no
 compiler. So you have to quote the potential customer on writing all these
 layers of software, potentially tens of millions of lines of code.
 Even porting an existing OS to the new hardware is not an insignificant
 job. Think how much time and money it would take.

Then factor in the profits to be reaped from selling the ported
OS/compilers :-).

mike
-- 
Mike Meyer [EMAIL PROTECTED]  http://www.mired.org/home/mwm/
Independent WWW/Perforce/FreeBSD/Unix consultant, email for more information.
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Binding a variable?

2005-10-22 Thread Mike Meyer
Steven D'Aprano [EMAIL PROTECTED] writes:

 On Fri, 21 Oct 2005 13:33:18 -0400, Mike Meyer wrote:

 Paul Dale [EMAIL PROTECTED] writes:
 
 Hi everyone,

 Is it possible to bind a list member or variable to a variable such that

 temp = 5

 list = [ temp ]

 Don't use the names of built-in functions as variables.

 temp == 6

 list

 would show

 list = [ 6 ]
 
 No. You need to either put a mutable in the list, or subclass list so
 that indexing gets the value, looks it up in the appropriate
 namespace, and returns that value.

 Or something even conceptually simpler than having to muck about with
 looking up different namespaces:
[elided]

Um, that's the *first* solution I proposed: putting a mutable on the
list.

mike
-- 
Mike Meyer [EMAIL PROTECTED]  http://www.mired.org/home/mwm/
Independent WWW/Perforce/FreeBSD/Unix consultant, email for more information.
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Microsoft Hatred FAQ

2005-10-22 Thread Mike Meyer
David Schwartz [EMAIL PROTECTED] writes:
 Mike Meyer [EMAIL PROTECTED] wrote in message 
 news:[EMAIL PROTECTED]
 Neither I, nor you, nor the government of any nation, should care a
 monkey's toss specifically for Microsoft's success. Microsoft is one
 special interest, out of a potentially unbounded number of possible
 players in the economy of a country and the world.
 No, not at all. It is the gravest act of self-contradiction to 
 maintain
 that one should be allowed to pursue one's own interest while denying 
 that
 same right to others.
 Not at all. No one is denying anyones right to purssue their own
 interest. What's being denied is the right to use illegal means to do
 so. If MS restricted themselves to legal means, no one would have a
 problem with them.
 The conclusion that the means were illegal is predicated on the 
 definition of the relevent market as desktop operating systems for 32-bit 
 x86 computers. Conduct is not illegal unless some law puts people on 
 adequate notice that their conduct is illegal. What law put Microsoft on 
 notice that the relevent market would be defined in the bizarre and almost 
 nonsensical way?

Not at all. The conclusion that the means were illegal was because
*they worked*. If MS didn't have monopoly power, the people they were
dealing with would have laughed at them.

 Unless you or I are specifically shareholders in Microsoft, we should 
 not
 care about their specific success; and the government should be entirely
 agnostic about who are the winners and losers in an economy.

 We should certainly care that Microsoft be allowed to pursue their 
 own
 success. The government should be agnostic about who the winners and 
 losers
 are, but must respect each entity's right to attempt to be that winner.
 Nice thought. Unfortunately, the government doesn't work that
 way. They believe that a practical monopoly is a bad thing, and limit
 the things such a company can do, and have been known to disassemble
 companies they believe are harming the economy in general.
 In other words, they believe the rights of Microsoft to do what they 
 please with what is theirs is subservient to some general obligation to help 
 the economy as a whole. I am saying that Microsoft has no obligaiton to the 
 economy as a whole but instead has an obligation to its stockholders. It 
 would be the gravest dereliction of that obligation for Microsoft to 
 sacrifice itself for some general benefit.

You do like straw men, don't you? Nowhere in the what I said does the
word help appear; you pulled it out of thin air, and what you said
in general has *nothing* to do with what you quoted above. The
statements don't contradict each other in any way, and both happen to
be true.

 The
 government's role should be to ensure a level playing field, and minimum
 levels of health, safety and environmental standards. There is no place
 for government giving special-interests like Microsoft favours.

 The problem is, people complain when the playing field is in fact 
 level.
 For example, Microsoft's exclusionary Windows agreements didn't ask for
 more than Windows was worth (or nobody would have agreed to them). Yet 
 they
 are considered examples of the playing field not being level.
 No, they didn't ask for more than Windows were worth. They tilted the
 playing field against MS competitors by causing consumers to pay MS
 money for products they didn't receive. In most countries, taking
 money from unwilling victims without giving them anything in exchange
 is called theft.
 It is not theft if you can simply say no to the deal and all that 
 happens is that you don't get the product. Your argument is preposterous. If 
 you accept arguments that equate guns with arguments, the next step is that 
 using a gun is a rational response to an argument one doesn't like. Oh wait, 
 you're already there.

Yup, we're there - and you brought us there, by referring to federal
judges as criminals pointing guns.

Of course, there are lots more straw men in this argument. I didn't
mention guns at all - you manufactured that from nothing. Theft
doesn't have to involve guns. Hell, it doesn't even have to involve
the knowledge of the victim, which is the case here. Everyone buying a
system from those that MS bullied paid for an MS OS, whether they got
one or not, and wether they knew it or not - and MS got the
money. They didn't even realize they were being robbed, so saying no
was never an option.

 Microsoft's behaviour over-all has been just as anti-social,
 anti-competitive and harmful to the over-all running of the economy as a
 hypothetical Walmart or Safeway that regularly parked their trucks in 
 the
 middle of the main road for a few hours while they unloaded.

 The problem is, the government does not own the economy. So it does 
 not
 get to manage it the way it gets to manage the roads it in fact owns.

 Sorry, but you're wrong. The government *does* own the econnomy

Re: Python vs Ruby

2005-10-22 Thread Mike Meyer
[EMAIL PROTECTED] (Alex Martelli) writes:
 Mike Meyer [EMAIL PROTECTED] wrote:
  Every line = more labour for the developer = more cost and time.
  Every line = more places for bugs to exist = more cost and time.
 There were studies done in the 70s that showed that programmers
 produced the same number of debugged lines of code a day no matter
 what language they used. So a language that lets you build the same
 program with fewer lines of code will let you build the program in
 less time.
 Of course, these results only apply where the complexity (e.g., number
 of operators, for example) in a single line of code is constant.

I'm not sure what you're trying to say here. The tests ranged over
things from PL/I to assembler. Are you saying that those two languages
have the same complexity in a single line?

 for item in sequence: blaap(item)

 or

 for item in sequence:
 blaap(item)

 are EXACTLY as easy (or hard) to write, maintain, and document -- it's
 totally irrelevant that the number of lines of code has doubled in the
 second (more standard) layout of the code!-)

The studies didn't deal with maintenance. They only dealt with
documentation in so far as code was commented.

On the other hand, studies of reading comprehension have shown that
people can read and comprehend faster if the line lengths fall within
certain ranges. While it's a stretch to assume those studies apply to
code, I'd personally be hesitant to assume they don't apply without
some reseach. If they do apply, then your claims about the difficulty
of maintaining and documenting being independent of the textual line
lengths are wrong. And since writing code inevitable involves
debugging it - and the studies specified debugged lines - then the
line length could affect how hard the code is to write as well.

  mike
-- 
Mike Meyer [EMAIL PROTECTED]  http://www.mired.org/home/mwm/
Independent WWW/Perforce/FreeBSD/Unix consultant, email for more information.
-- 
http://mail.python.org/mailman/listinfo/python-list


<    1   2   3   4   5   6   7   8   9   10   >