Re: Why tuples use parentheses ()'s instead of something else like 's?

2005-01-01 Thread edin . salkovic
Roy Smith wrote:
 In article [EMAIL PROTECTED],
  Reinhold Birkenfeld [EMAIL PROTECTED] wrote:

  + being an operator

 Looks more like a smiley for guy wearing a bowtie
:)), I had a nice laugh with this one.

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


Re: Complementary language?

2005-01-01 Thread Alan Gauld
On Sat, 25 Dec 2004 18:40:31 -0500, HackingYodel
[EMAIL PROTECTED] wrote:
 Hello all!  I'm learning to program at home.  I can't imagine a better 
 language than Python for this.  The ideal situation, for me, would be to 
 study two languages at the same time.  Probably sounds crazy, but it 
 works out better for me.  

Me too, thats why my web tutorial features Python, VBSCript and
Javascript. (The previous version had BASIC and Tcl with Python)

 fascinating.  C, D, Objective-C, Ocaml, C++, Lisp, how is a non-tech to 
 choose?  Does any single language do a better job in Python's weaker 
 areas? 

C is better at low level stuff, Prolog is better at declaratie
programming and Haskell is better at functional programming.
Lisp/Scheme are good for giving a good theoretical understanding
(try the SICP and HTDP web sites). And Tcl has a really different
approach which is plain fun to grapple with :-)

I chose VBScript and JavaScript because they have similar
structure to Python (traditional imperative programming 
with OOP) but very different syntax. Plus they were free and
easily available (albeit with VBScript limited to Windows 
users, who are 80+% of my visitors). Javascript is especially
useful since its an easy lead in to learning C/C++, Java, 
even Perl to some extent and a lot of sample code sites 
use those languages.

Alan G.
Author of the Learn to Program website
http://www.freenetpages.co.uk/hp/alan.gauld
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: OT: spacing of code in Google Groups

2005-01-01 Thread Terry Reedy

JanC [EMAIL PROTECTED] wrote in message 
news:[EMAIL PROTECTED]
 I don't know if gmane keeps formating of messages intact when posting?
 That could be an alternative too...

Reading posts via gmane with Outlook Express preserves leading spaces just 
fine.  However, OE deletes tabs regardless of what newsgroup does.
The funny thing is that typing a tab gets converted to 4 spaces.

Terry J. Reedy




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


Re: exposing C array to python namespace: NumPy and array module.

2005-01-01 Thread Craig Ringer
On Sat, 2005-01-01 at 08:18, Bo Peng wrote:

 Python's array module is built-in, easy to use, but *without* a 
 FromLenAndData function! Even the buffer interface provides only 'get 
 buffer' but no 'set buffer' functions. Could anyone tell me how I can 
 create an array object from existing data?

Python has no array objects in the core language, only lists. The
distinction is important when discussing numarray etc, because Python
lists and NumPy etc arrays are very different.

While you can build a Python list from a subsection of your C array,
changes made in Python won't be pushed back to the C array it was
created from. If this is OK, you can probably build the list using just
a for loop - I'm not sure if there are any more efficient methods for
variable length lists.

If the Python user needs to be able to change the underlying array, I'd
probably drop the use of the built-in list class entirely and write my
own class that looks like a list (and smells like a list, and tastes
like a list - lucky we didn't step in it!). It can be pretty simple,
providing as few of the list protocol methods as:

__getitem__   (a PyList_GetItem equivalent)
__setitem__   (a PyList_SetItem equivalent)

and preferably:

__len__
__iter__

or as much of the list protocol as documented on the Python/C API page
as you need.

I'd probably implement the class in Python, and have my extension module
provide a couple of simple functions to the underlying C array. These
could be considered private to your list class. That'd make writing
things like the __iter__ method much nicer, while still letting you
implement __len__, __getitem__, __setitem__, etc in C. For example, I
might write:

class CArray(object):
def __init__(self, ...):
...

def __getitem__(self, index):
_carray_getitem(self, index)

def __len__(self):
_carray_len(self, index)

def __iter__(self):
# build and return an interator using Python
...


If you want to write part of your extension module in Python and part in
C, there are two main ways to do it. The usual way is to write a
'wrapper' in Python that imports the C parts, wraps them where necessary
or just pushes them into its own namespace, etc.

The less common way is to import __builtins__ and __main__ into your C
extension module's namespace then PyRun_String() python code in it to
set things up. I find this approach MUCH more useful when embedding
Python in an app and I only want to write small bits of my module in
Python.


The other alternative is to code your class entirely in C, implementing
all the __methods__ as C functions. Unattractive as far as I'm
concerned, but then I find constructing classes using Python's C API
irritating and less clear than it could be.



Here's the code -- hideously reformatted to avoid wrapping in the mail -
in my initmodule() function that I use to set up the module so that
Python code can execute in its namespace. You can ignore the
const_cast stuff, chances are your compiler will ignore the const
problems.


// 'd' is the dictionary of the extension module, as obtained
// with PyModule_GetDict(module)

PyObject* builtinModule = PyImport_ImportModuleEx(
const_castchar*(__builtin__),
d, d, Py_BuildValue(const_castchar*([]))
);
if (builtinModule == NULL)
{
// Error handling will not be shown; it'll depend on your module anyway.
}
PyDict_SetItemString(d, const_castchar*(__builtin__),
 builtinModule);

PyObject* exceptionsModule = PyImport_ImportModuleEx(
 const_castchar*(exceptions), d, d,
 Py_BuildValue(const_castchar*([]))
 );
if (exceptionsModule == NULL) {}
PyDict_SetItemString(d, const_castchar*(exceptions),
 exceptionsModule);

// We can now run Python code in the module's namespace. For
// example (untested), as my real examples wouldn't do you any
// good, they're too bound to the internal API of my module:

QString python_code = ;
python_code += def sample_function():\n;
python_code += print \See, it worked\\n;
// My app sets sysdefaultencoding to utf-8, hence:
char* python_code_cstring = python_code.utf8();

// Note that we pass our module dictionary as both
// locals and globals. This makes the code effectively
// run in the extension module, as if it was being
// run during loading of a Python module after an 
// 'import' statement.
PyObject* result = PyRun_String(python_code_cstring,
Py_file_input,
d,d);
if (result == NULL)
{
qDebug(Python code to declare sample_function failed!);
PyErr_Print(); // also clears the exception
}
// Because 'result' may be NULL, not a PyObject*, we must call PyXDECREF
not Py_DECREF
Py_XDECREF(result);


--

Ugh - I'd forgotten how ugly C code limited to 80 cols and without
syntax highlighting really was. Especially when the reformatting is done
as badly as I've done it. I hope you can make some sense out of that,
anyway. Note that once 

Re: Complementary language?

2005-01-01 Thread Premshree Pillai
On Sat, 1 Jan 2005 09:35:32 + (UTC), Alan Gauld
[EMAIL PROTECTED] wrote:
 On Sat, 25 Dec 2004 18:40:31 -0500, HackingYodel
 [EMAIL PROTECTED] wrote:
  Hello all!  I'm learning to program at home.  I can't imagine a better
  language than Python for this.  The ideal situation, for me, would be to
  study two languages at the same time.  Probably sounds crazy, but it
  works out better for me.

Yes, the best way to learn a new language is probably to compare it
with some other language (of the same paradigm) that you are already
familiar with. The best part about Python is that there really isn't
much learning involved. Python comes closest to what you'd call
pseudocode.

If you know your English, you probably know Python. :D

 
 Me too, thats why my web tutorial features Python, VBSCript and
 Javascript. (The previous version had BASIC and Tcl with Python)
 
  fascinating.  C, D, Objective-C, Ocaml, C++, Lisp, how is a non-tech to
  choose?  Does any single language do a better job in Python's weaker
  areas?
 
 C is better at low level stuff, Prolog is better at declaratie
 programming and Haskell is better at functional programming.
 Lisp/Scheme are good for giving a good theoretical understanding
 (try the SICP and HTDP web sites). And Tcl has a really different
 approach which is plain fun to grapple with :-)
 
 I chose VBScript and JavaScript because they have similar
 structure to Python (traditional imperative programming
 with OOP) but very different syntax. Plus they were free and
 easily available (albeit with VBScript limited to Windows
 users, who are 80+% of my visitors). Javascript is especially
 useful since its an easy lead in to learning C/C++, Java,
 even Perl to some extent and a lot of sample code sites
 use those languages.
 
 Alan G.
 Author of the Learn to Program website
 http://www.freenetpages.co.uk/hp/alan.gauld
 --
 http://mail.python.org/mailman/listinfo/python-list
 


-- 
Premshree Pillai
http://www.livejournal.com/~premshree
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: The Industry choice

2005-01-01 Thread Cameron Laird
In article [EMAIL PROTECTED],
Hans Nowak  [EMAIL PROTECTED] wrote:
Paul Rubin wrote:

 You should write unit tests either way, but in Python you're relying
 on the tests to find stuff that the compiler finds for you with Java.

As I wrote on my weblog a while ago, I suspect that this effect is 
largely psychological.  You jump through hoops, declaring types all over 
the place, checking exceptions, working around the language's 
limitations, etc.  So when your code compiles, it *feels* safer.  Like 
you're at least part of the way towards ensuring correctness.  All that 
work must be good for *something*, right?  Never mind that when writing 
unit tests for a dynamic language, you don't check for these things at 
all.  How often do you explicitly check types in Python unit tests? 
IMHO, when using a dynamic language, you don't need most of the checks 
that Java, C# and their ilk force upon you.
.
.
.
Me, too.

That is, while I have a LOT of respect for Paul's programming
and judgment, and question myself when I'm on the side opposite
him, I ultimately value type declarations in languages such as
Java as more cost than benefit.

It's a funny position to hold, because I simultaneously recognize
that type theory is one of computing's strongest theoretical 
achievements, AND I am a strong advocate of static syntax checkers
such as PyChecker.  Still, I see TDD as the right place to start.
-- 
http://mail.python.org/mailman/listinfo/python-list


Which blog tool

2005-01-01 Thread Mark Carter
I currently use python to automatically summarise a certain newsgroup 
daily, and post the findings that it makes. Someone has suggested that 
they would like a to see a blog of the posts. I wondered if there was a 
python tool/library that could automate the blog postings. Any ideas?

Some details:
* the summaries are basically just text files
* I already have a blog at www.blogger.com 
(http://markcarterturriff.blogspot.com/), so I would like to use that if 
possible; although any alternative free one that I can use to achieve my 
objective would be OK, too.
* I do have my own hosted website, which can use perl but not python; 
but I'd rather use a freebie blog site
* the whole thing must be scriptable, because it will run daily. A GUI 
would therefore likely get in the way.
* generating an RSS feed would be nice
--
http://mail.python.org/mailman/listinfo/python-list


Re: Speed ain't bad

2005-01-01 Thread Anders J. Munch
Bulba! [EMAIL PROTECTED] wrote:

 One of the posters inspired me to do profiling on my newbie script
 (pasted below). After measurements I have found that the speed
 of Python, at least in the area where my script works, is surprisingly
 high.

Pretty good code for someone who calls himself a newbie.

One line that puzzles me:
 sfile=open(sfpath,'rb')

You never use sfile again.
In any case, you should explicitly close all files that you open. Even
if there's an exception:

sfile = open(sfpath, 'rb')
try:
stuff to do with the file open
finally:
sfile.close()


 The only thing I'm missing in this picture is knowledge if my script
 could be further optimised (not that I actually need better
 performance, I'm just curious what possible solutions could be).

 Any takers among the experienced guys?

Basically the way to optimise these things is to cut down on anything
that does I/O: Use as few calls to os.path.is{dir,file}, os.stat, open
and such that you can get away with.

One way to do that is caching; e.g. storing names of known directories
in a set (sets.Set()) and checking that set before calling
os.path.isdir.  I haven't spotted any obvious opportunities for that
in your script, though.

Another way is the strategy of it's easier to ask forgiveness than to
ask permission.
If you replace:
if(not os.path.isdir(zfdir)):
os.makedirs(zfdir)
with:
try:
os.makedirs(zfdir)
except EnvironmentError:
pass

then not only will your script become a micron more robust, but
assuming zfdir typically does not exist, you will have saved the call
to os.path.isdir.

- Anders


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


Re: Which blog tool

2005-01-01 Thread Premshree Pillai
On Sat, 01 Jan 2005 13:14:23 +, Mark Carter [EMAIL PROTECTED] wrote:
 I currently use python to automatically summarise a certain newsgroup
 daily, and post the findings that it makes. Someone has suggested that
 they would like a to see a blog of the posts. I wondered if there was a
 python tool/library that could automate the blog postings. Any ideas?
 
 Some details:
 * the summaries are basically just text files
 * I already have a blog at www.blogger.com
 (http://markcarterturriff.blogspot.com/), so I would like to use that if
 possible; although any alternative free one that I can use to achieve my
 objective would be OK, too.

You can use the Blogger API to post to your Blogger account. There's a
Python interface to the API -- PyBlogger -- available here:
http://beetle.cbtlsl.com/archives/category/pyblogger

You could also use a free service like LiveJournal
(http://www.livejournal.com/). There are several clients available to
post to LJ. I don't know if a Python wrapper to post is available or
not. IAC, it'd be a few lines of code to make the XML-RPC calls. The
protocol docs are available here:
http://www.livejournal.com/doc/server/ljp.csp.protocol.html

HTH
 * I do have my own hosted website, which can use perl but not python;
 but I'd rather use a freebie blog site
 * the whole thing must be scriptable, because it will run daily. A GUI
 would therefore likely get in the way.
 * generating an RSS feed would be nice
 --
 http://mail.python.org/mailman/listinfo/python-list
 


-- 
Premshree Pillai
http://www.livejournal.com/~premshree
-- 
http://mail.python.org/mailman/listinfo/python-list


HELP: Tkinter idiom needed

2005-01-01 Thread Pekka Niiranen
Hi there,
after reading TkInter/thread -recipe:
http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/82965
I wondered if it was possible to avoid using threads
for the following problem:
I have script started from W2K console that normally
prints ascii messages to the screen. However, I have
command line debug -flag that might cause printing
of UTF-8 data to the screen. This fails occassionally
due to Console encoding, of course.
What I need is Tkinter window where some printouts
are directed when script is run in Debug -mode
(Redirection of stdout is out of question).
While testing, I have got this far already:
---script starts
from Tkinter import *
from ScrolledText import ScrolledText
import os, sys, tkFont, codecs
class Pyg_message_box:
  def __init__(self, parent):
self.myParent = parent
self.myContainer1 = Frame(parent)
self.myContainer1.option_add(*font,\
tkFont.Font(family=Arial Unicode MS, size=8))
self.myContainer1.pack()
self.text = ScrolledText()
self.text.pack()
self.button1 = Button(self.myContainer1, text=Quit,\
 command=self.button1Click)
self.button1.pack(side=LEFT)
self.button1.bind(Button-1, self.button1Click)
  def button1Click(self, event):
 self.myContainer1.quit()
  def write(self, s):
self.text.insert(END, s)
root = Tk()
widget = Pyg_message_box(root)
sys.stdout = widget
a = codecs.open(d:\\test.txt, r, utf_16).readlines()
for x in a:
  print x
root.mainloop()
---script ends
My questions are:
- Can I open Tk -window without enclosing the whole script
  between root=Tk() and root.mainloop()?
- What is the idiom of opening Tk -window only when Debug -flag
  is encountered (the script stops running until window is closed)?
Something like:
if not my_debug == ON:
print message # prints to console
else:
# 1) open temporary ScrolledText() Tk -window
# 2) Print stuff to window
# 3) Ask user to close window
I would no like to always open Tkwindows just in case user runs script
with debug -flag on.
-pekka-
--
http://mail.python.org/mailman/listinfo/python-list


Re: what is lambda used for in real code?

2005-01-01 Thread Roy Smith
In article [EMAIL PROTECTED],
 Adam DePrince [EMAIL PROTECTED] wrote:

 We can operate on every other class without having to involve the
 namespace, why should functions be any different?

def is a weird beast.  It does more than just bind a lambda to a name, 
it also alters the function so it knows its own name.  For example, the 
following:

--
def foo ():
print I am foo

bar = foo

def foo ():
print I am foo's evil twin

foo()
print foo

bar()
print bar

baz = lambda : I am lambda
print baz
print baz()
--

will print:

I am foo's evil twin
function foo at 0x363c70
I am foo
function foo at 0x35ae70
function lambda at 0x363770
I am lambda
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: what is lambda used for in real code?

2005-01-01 Thread Diez B. Roggisch
 Why not improve your metaclass wrapping so it knows about properties and
 replaces them with properties containing wrapped functions?

Erg - never thought of that, actually - it was so fast to introduce the
lambda...

But after some tinkering with metaclasses, I think it can be done - I have
to create a mapping between the pure, unwrapped function and the wrapped
one so that I can recreate all properties. 

Thanks for nudging me in that direction :)

-- 
Regards,

Diez B. Roggisch
-- 
http://mail.python.org/mailman/listinfo/python-list


DB-API 2.0 in pysqlite and pgdb

2005-01-01 Thread Roman Suzi

Happy New Year to all Pythoneers!

I am playing with pysqlite and pgdb and their DB-API conformancy.
It was quite interesting to know:

 - sqlite doesn't have mandatory helper-functions Date, Tim, etc.
   (due to an error int it's __init__, but this is quite obvious to correct
   or just to use mx.Date, mx.Time)

more serious mishaps with pgdb (postgresql-python):
it doesn't know how to quote date-time data for the objects
it has constructors itself.

 import pgdb
 c = pgdb.connect(database=template1)
 cu = c.cursor()
 o = pgdb.Time(10, 0, 0)
 cu.execute(select %(o);, vars())
Traceback (most recent call last):
  File stdin, line 1, in ?
  File /usr/local/lib/python2.3/site-packages/pgdb.py, line 189, in execute
self.executemany(operation, (params,))
  File /usr/local/lib/python2.3/site-packages/pgdb.py, line 201, in
executemany
sql = _quoteparams(operation, params)
  File /usr/local/lib/python2.3/site-packages/pgdb.py, line 283, in
_quoteparams
x[k] = _quote(v)
  File /usr/local/lib/python2.3/site-packages/pgdb.py, line 275, in _quote
raise InterfaceError, 'do not know how to handle type %s' % type(x)
pgdb.InterfaceError: do not know how to handle type type 'instance'

This doesn't happen for strings or numbers:

 cu.execute(select %s;, ['s'])
 cu.execute(select %s;, [1])
 cu.execute(select %(k)s;, {'k': 123})
 o
DateTimeDelta object for '10:00:00.00' at 401e8a48

Thus, pgdb doesn't know how to handle DateTimeDelta.

The same with

 cu.execute(select %(o)s;, {'o': pgdb.Date(2005,1,1)})

  . . .
line 201, in executemany
sql = _quoteparams(operation, params)
  File /usr/local/lib/python2.3/site-packages/pgdb.py,
line 283, in _quoteparams
x[k] = _quote(v)
  File /usr/local/lib/python2.3/site-packages/pgdb.py,
line 275, in _quote
raise InterfaceError, 'do not know how to handle type
%s' % type(x)
pgdb.InterfaceError: do not know how to handle type type
'DateTime'

(It was after I commented out exception catch:

#   except:
#   raise OperationalError, internal error in '%s' % sql

in pgdb.py to see where the error occurs)


Am I missing something obvious or is it really a bug/feature of pgdb?

python2.3
postgresql-7.2.1
almost fresh mx.DateTime


Thank you!

Sincerely yours, Roman Suzi
-- 
[EMAIL PROTECTED] =\= My AI powered by GNU/Linux RedHat 7.3
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Which blog tool

2005-01-01 Thread Mark Carter
Premshree Pillai wrote:
You can use the Blogger API to post to your Blogger account. There's a
Python interface to the API -- PyBlogger -- available here:
http://beetle.cbtlsl.com/archives/category/pyblogger
Hey, it Just Works! I got the whole basic thing working in a few 
minutes. It was exactly what I was looking for. Thanks for the link.
--
http://mail.python.org/mailman/listinfo/python-list


want some extra cash, try this

2005-01-01 Thread adamhum
want some extra cash, try this



THIS REALLY CAN MAKE YOU EASY MONEY!!
A little while back, I was on my computer having a grand old time, just
like you are now and came across an article similar to this that said
you could make thousands dollars within weeks with only an initial
investment of $6.00! So I thought, Yeah, right, this must be a scam,
but like most of us, I was curious, so I kept reading. Anyway, it said
that you send $1.00 to each of the 6 names and address stated in the
article. You then place your own name and address in the bottom of the
list at #6, and post the article in at least 200 newsgroups. No catch,
that was it. So after thinking it over, and talking to few people
first, I thought about trying it. I figured what have I got to lose
except 6 stamps and $6.00, right? Like most of us I was a little
skeptical and a little worried about the legal aspects of it all, So I
checked it out with the U.S. Post Office (1-800-725-2161) and they
confirmed that it is indeed legal! Then I invested the measly $6.00.
Well GUESS WHAT!!?. Within 7 days, I started getting money in the mail!
I was shocked! I figured it would end soon, but the money just kept
coming in. In my first week, I made about $25.00. By the end second
week I had made a total over $1,000.00! In the third week I had over
$10,000.00 and it's still growing. Its Certainly worth $6.00, and 6
stamps, I have spent more than that on the lottery!! Let me tell you
how this works and most importantly, why it works?.STEP 1: Get 6
separate pieces of paper and write the following on each piece of paper
PLEASE PUT ME ON YOUR MAILING LIST. Now get 6 US $1.00 bills and
place ONE inside EACH of the 6 pieces of paper so the bill will not be
seen through the envelope to prevent thievery. Next, place one paper in
each stating the above phrase, your name and address, and a $1.00 bill.
What you are doing is creating a service by this. THIS IS ABSOLUTELY
LEGAL! Mail the 6 envelopes to the following addresses:

#1) Jason Barone P.O. Box 1015 Glen Burnie, MD 21060-1015
#2) Jonathon Markwood 3308 Birdsong Way Birmingham, AL 35242
#3) Eben Mcgee 1876 #C Locust, Las Cruces, NM 88001
#4) Ricky DeMaio 471 McCutcheon rd, Hudson WI 54016
#5) P. Ortner 29 Briarlee Dr. Tonawanda, NY 14150
#6) O. Humphrey 8452 E. Louise Dr, Tucson, AZ 85730

STEP 2: Now take the #1 name off the list that you see above, move the
other names up
and add YOUR Name as number 6 on the list. STEP 3: Change anything you
need to, but try to keep this article as close to original as possible.
Now, post your amended article to at least 200 newsgroups, message
board. All you need is 200, but remember, the more you post, the more
money you make!
Just copy this message, paste it in Word or Notepad, find message
boards like this and paste away.

Congratulations? THAT'S IT! All you have to do is jump to different
newsgroups and post away, after you get the hang of it, and it will
take about 30 seconds for each newsgroup! * REMEMBER, THE MORE
NEWSGROUPS OR MESSAGE BOARD YOU POST IN, THE MORE MONEY YOU EILL MAKE!!
BUT YOU HAVE TO POST A MINIMUM OF 200* That's it! You will begin
receiving money from around the world within days!

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


Re: exposing C array to python namespace: NumPy and array module.

2005-01-01 Thread Bo Peng
Craig Ringer wrote:
On Sat, 2005-01-01 at 08:18, Bo Peng wrote:

Python's array module is built-in, easy to use, but *without* a 
FromLenAndData function! Even the buffer interface provides only 'get 
buffer' but no 'set buffer' functions. Could anyone tell me how I can 
create an array object from existing data?

Python has no array objects in the core language, only lists. The
distinction is important when discussing numarray etc, because Python
lists and NumPy etc arrays are very different.
Thank you very much for the detailed reply!
Sorry if I was not clear enough. I was talking about the differece 
between python array module 
(http://docs.python.org/lib/module-array.html, Modules/arraymodule.c in 
the source tree) and NumPy array. They both use C-style memory block 
arrangement for efficient memory access. While NumPy has both, the array 
module is designed to be used purely in Python so there is no header 
file and no function to build an array from a pointer.

One of the methods you suggested (creating a new type) already 
implemented in arraymodule.c. I am not sure if it is appropriate to add 
the file into my project and add a 'CreateFromLenAndBuf' function.

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


Re: pickling a subclass of tuple

2005-01-01 Thread Alex Martelli
fedor [EMAIL PROTECTED] wrote:

 Hi all, happy new year,
 
 I was trying to pickle a instance of a subclass of a tuple when I ran
 into a problem. Pickling doesn't work with HIGHEST_PROTOCOL. How should
 I rewrite my class so I can pickle it?

You're falling afoul of an optimization in pickle's protocol 2, which is
documented in pickle.py as follows:

# A __reduce__ implementation can direct protocol 2 to
# use the more efficient NEWOBJ opcode, while still
# allowing protocol 0 and 1 to work normally.  For this to
# work, the function returned by __reduce__ should be
# called __newobj__, and its first argument should be a
# new-style class.  The implementation for __newobj__
# should be as follows, although pickle has no way to
# verify this:
#
# def __newobj__(cls, *args):
# return cls.__new__(cls, *args)
#
# Protocols 0 and 1 will pickle a reference to __newobj__,
# while protocol 2 (and above) will pickle a reference to
# cls, the remaining args tuple, and the NEWOBJ code,
# which calls cls.__new__(cls, *args) at unpickling time
# (see load_newobj below).  If __reduce__ returns a
# three-tuple, the state from the third tuple item will be
# pickled regardless of the protocol, calling __setstate__
# at unpickling time (see load_build below).

Essentially, and simplifying just a little...: you're inheriting
__reduce_ex__ (because you're not overriding it), but you ARE overriding
__new__ *and changing its signature* -- so, the inherited __reduce__ex__
is used, and, with this protocol 2 optimization, it essentially assumes
that __new__ is similarly used -- or, at least, that a __new__ is used
which does not arbitrarily change the signature!

So, if you want to change __new__'s signature, and yet be picklable by
protocol 2, you have to override __reduce_ex__ to return the right
args... those your class's __new__ expects!


For example, you could consider something like...:

def __newobj__(cls, *args):
return cls.__new__(cls, *args)

class A(tuple):
def __new__(klass, arg1, arg2):
return super(A, klass).__new__(klass, (arg1, arg2))

def __reduce_ex__(self, proto=0):
if proto = 2:
return __newobj__, (A, self[0], self[1])
else:
return super(A, self).__reduce_ex__(proto)

Note the key difference in A's __reduce_ex__ (for proto=2) wrt tuple's
(which is the same as object's) -- that's after an import a where a.py
has this code as well as an 'a = A(1, 2)'...:

 a.a.__reduce_ex__(2)
(function __newobj__ at 0x3827f0, (class 'a.A', 1, 2))
 tuple.__reduce_ex__(a.a, 2)
(function __newobj__ at 0x376770, (class 'a.A', (1, 2)), {}, None,
None)
 

Apart from the additional tuple items (not relevant here), tuple's
reduce returns args as (class 'a.A', (1, 2)) -- two items: the class
and the tuplevalue; so with protocol 2 this ends up calling A.__new__(A,
(1,2))... BOOM, because, differently from tuple.__new__, YOUR override
doesn't accept this signature!  So, I suggest tweaking A's reduce so it
returns args as (class 'a.A', 1, 2)... apparently the only signature
you're willing to accept in your A.__new__ method.

Of course, if A.__new__ can have some flexibility, you COULD have it
accept the same signature as tuple.__new__ and then you wouldn't have to
override __reduce_ex__.  Or, you could override __reduce_ex__ in other
ways, say:

def __reduce_ex__(self, proto=0):
if proto = 2:
proto = 1
return super(A, self).__reduce_ex__(proto)

this would avoid the specific optimization that's tripping you up due to
your signature-change in __new__.

The best solution may be to forget __reduce_ex__ and take advantage of
the underdocumented special method __getnewargs__ ...:

class A(tuple):
def __new__(klass, arg1, arg2):
return super(A, klass).__new__(klass, (arg1, arg2))

def __getnewargs__(self):
return self[0], self[1]

This way, you're essentially choosing to explicitly tell the normal
__reduce_ex__ about the particular arguments you want to be used for the
__new__ call needed to reconstruct your object on unpickling!  This
highlights even better the crucial difference, due strictly to the
change in __new__'s signature...:

 a.a.__getnewargs__()
(1, 2)
 tuple.__getnewargs__(a.a)
((1, 2),)



It IS, I guess, somewhat unfortunate that you have to understand
pickling in some depth to let you change __new__'s signature and yet
fully support pickling... on the other hand, when you're overriding
__new__ you ARE messing with some rather deep infrastructure,
particularly if you alter its signature so that it doesn't accept
normal calls any more, so it's not _absurd_ that compensatory depth of
understanding is required;-).


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


Re: lies about OOP

2005-01-01 Thread Daniel T.
[EMAIL PROTECTED] wrote:

 A paper finding that OOP can lead to more buggy software is at
 http://www.leshatton.org/IEEE_Soft_98a.html

Sure, OOP *can* lead to more buggy software, that doesn't mean it always 
does.


 Les Hatton Does OO sync with the way we think?, IEEE Software, 15(3),
 p.46-54
 This paper argues from real data that OO based systems written in C++
 appear to increase the cost of fixing defects significantly when
 compared with systems written in either C or Pascal. It goes on to
 suggest that at least some aspects of OO, for example inheritance, do
 not fit well with the way we make mistakes.

So, he has data that shows that C++ *appears* to increase the cost of 
fixing defects, then *suggests* that its because C++ is an OO language? 
Sounds like he is ignoring his own data to me...

Mr. Hatton suffers from the same problem that many OO critics suffer. He 
thinks that the language choice decides whether the program written is 
an OO program. I've seen plenty of very non-OO systems written in OO 
languages, I've seen expert OO systems written in non-OO languages. OOP 
isn't a language choice, it is a style of problem solving.

I'm happy to accept that it could take longer to fix bugs in programs 
written in C++ when compared to either C or Pascal, the language itself 
is quite a bit more complicated than either of the latter. 

You know, it tends to take longer to repair a 2004 Mustang than it does 
a 1964 Mustang, does that mean the newer car is not as good?


 If OOP is so beneficial for large projects, why are the Linux kernel,
 the interpreters for Perl and Python, and most compilers I know written
 in C rather than C++?

All three of the systems in question were begun before C++ was 
standardized. Python was also implemented in Java, does that mean OO 
other than C++ is good? Of course not, the fact that the three projects 
in question were implemented in C is not an indictment against OO in any 
way.
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: lies about OOP

2005-01-01 Thread Daniel T.
H. S. Lahman [EMAIL PROTECTED] wrote:

  Les Hatton Does OO sync with the way we think?, IEEE Software, 15(3),
  p.46-54
  This paper argues from real data that OO based systems written in C++
  appear to increase the cost of fixing defects significantly when
  compared with systems written in either C or Pascal. It goes on to
  suggest that at least some aspects of OO, for example inheritance, do
  not fit well with the way we make mistakes.
 
 Try and find and experienced OO developer who would advocate that large, 
 complex generalizations are a good practice.  You can write lousy 
 programs in any paradigm.  The likelihood increases when you use the 
 most technically deficient of all the OOPLs.  (If those developers had 
 used Smalltalk, I'll bet their defect rates would have been 
 substantially lower even if they weren't very good OO developers.)

Careful, the paper never claims that C++ produced more defects than C or 
Pascal. It only claims that the defects found in the C++ program were 
more costly to fix. That is a very big difference.

However, I agree completely with the rest of your comments.
-- 
http://mail.python.org/mailman/listinfo/python-list


Windows process priority setting question...

2005-01-01 Thread Ray S
Is it possible to have an app re-set its own priority?
I see that 2.4+ has the ability for sub-processes, but only on creation.
Apparently
win32process.SetPriorityClass(handle, dwPriorityClass)
and
PyCWinThread.SetThreadPriority(priority)
allows one to change a sub while a the sub is running, but what about the 
current?

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


Re: PyQT installation

2005-01-01 Thread John J. Lee
[EMAIL PROTECTED] (Alex Martelli) writes:
[...]
 Basically: if you want it on Windows for free, forget Qt

Correct.


 (I hear the
 cygwin people are trying to make a GPL Qt available for Win+cyg+XFree,
 but I suspect trolltech ain't happy about that -- anyway, I don't think
 it would be native, X11 being still required).
[...]

Not correct.  It's driven by KDE, and it's more ambitious than that:

http://kde-cygwin.sourceforge.net/qt3-win32/roadmap.php


IIRC, people have already run some KDE apps under Windows (though
still needing X, so far).

I wonder how TrollTech will react as (and if) it progresses.


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


Re: The Industry choice

2005-01-01 Thread Steve Holden
Cameron Laird wrote:
In article [EMAIL PROTECTED],
 [EMAIL PROTECTED] wrote:
Bulba wrote:
OK, so what projects and why would you consider
Python:
1. clearly unsuitable
Large-scale scientific computing projects, such as numerical weather
prediction, where performance is critical. Python could be used as the
glue but not the guts, where Fortran 95 and C++ are more
appropriate. In my tests, some posted here, there has been a
.
.
.
I feel like growling that it's clearly a mistake for large-scale
scientific computing projects not to leverage dynamic languages,
at least in part.  Yes, I've seen projects that would have been
disasters if done entirely in Python.  I've also seen many, many
large-scale scientific projects that soaked up far more resources
than they should have because they limited themselves to C++ or
Fortran.
I argue that it's a false opposition to categorize projects in
terms of use of single languages.  Many projects are MUCH better
off with a mix of Python and Fortran, say (and probably SQL and
JavaScript and ...), and it's pernicious to accomodate the 
managerial conceit that One Language will suffice.
Indeed it's sensible to choose language based on the nature of the task 
to be performed, to avoid language-blindness and to build systems in 
mixed languages.

Unfortunately the hierarchy of power in most modern commercial and 
academic organizations is such that large-scale efforts will be 
nominally run by single individuals, and since those individuals 
typically can dispense favors and determine who advances within the 
organization it's often unwise *not* to accommodate the managerial 
conceit it career advancement is one's primary goal.

which-is-why-i-run-my-own-business-ly y'rs  -  steve
--
Steve Holden   http://www.holdenweb.com/
Python Web Programming  http://pydish.holdenweb.com/
Holden Web LLC  +1 703 861 4237  +1 800 494 3119
--
http://mail.python.org/mailman/listinfo/python-list


Re: PyQT installation

2005-01-01 Thread Ken Godee
John J. Lee wrote:
[EMAIL PROTECTED] (Alex Martelli) writes:
[...]
Basically: if you want it on Windows for free, forget Qt

Correct.
I believe the book C++ GUI programming Qt3 comes
with a windows Qt gpl 3.x version. Just have to buy
the book. No PyQt version to match thou.
Blackadder from the Kompany, while not free, is still
a pretty good deal. Like  $100 for personal and around
$350 for commercial version. Include current windows/linux
versions of (Qt)PyQt along with converted Qt C++ to PyQt docs.
  Not correct.  It's driven by KDE, and it's more ambitious than that:
http://kde-cygwin.sourceforge.net/qt3-win32/roadmap.php
IIRC, people have already run some KDE apps under Windows (though
still needing X, so far).
I wonder how TrollTech will react as (and if) it progresses.
I don't think your giving TrollTech any credit here, yes they have
a business model and need to make money, but not everybody is
Microsoft. They are fully aware and supportive of the project
and I remember reading not to long ago they struck an aggrement
with the project that if anything ever happened to TrollTech they
would release Qt to project under gpl, or something like that.
--
http://mail.python.org/mailman/listinfo/python-list


Re: what is lambda used for in real code?

2005-01-01 Thread Alex Martelli
Steve Holden [EMAIL PROTECTED] wrote:

 Adam DePrince wrote:
 [...]
  
  In sort, we must preserve the ability to create an anonymous function
  simply because we can do so for every other object type, and functions
  are not special enough to permit this special case.
  
 And you'd create an anonymous type how, exactly?

 type('',(),{})
class '__main__.'

maybe...?


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


Re: exposing C array to python namespace: NumPy and array module.

2005-01-01 Thread Craig Ringer
On Sat, 2005-01-01 at 10:27 -0600, Bo Peng wrote:

 Sorry if I was not clear enough. I was talking about the differece 
 between python array module 
 (http://docs.python.org/lib/module-array.html, Modules/arraymodule.c in 
 the source tree) and NumPy array. They both use C-style memory block 
 arrangement for efficient memory access. While NumPy has both, the array 
 module is designed to be used purely in Python so there is no header 
 file and no function to build an array from a pointer.

Thanks for clarifying that - I had misunderstood your reference to
arraymodule.c .

I guess the core language doesn't have an array type, but as there's a
standard lib module that does (I'd forgotten it was there), it hardly
matters.

It would seem sensible to extend that module with a C API for mapping an
existing array. That would be a rather handy thing to have in the
standard library.

 One of the methods you suggested (creating a new type) already 
 implemented in arraymodule.c. I am not sure if it is appropriate to add 
 the file into my project and add a 'CreateFromLenAndBuf' function.

That sounds like a reasonable approach to me, but I'm hardly an expert.
The code's license permits you to do so, and it's hardly worth repeating
the work if you don't have to.

-- 
Craig Ringer

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


Re: exposing C array to python namespace: NumPy and array module.

2005-01-01 Thread Raymond L. Buvel
Bo Peng wrote:
Dear list,
I am writing a Python extension module that needs a way to expose pieces 
of a big C array to python. Currently, I am using NumPy like the following:

 PyObject* res = PyArray_FromDimsAndData(1, int*dim, PyArray_DOUBLE, 
char*buf);

Users will get a Numeric Array object and can change its values (and 
actually change the underlying C array).

This works fine. However, when I deliver my module, I find NumPy is 
unnecessarily large for this simple task. As a matter of fact, I had to 
 build from source NumPy, ATLAS etc on Solaris, Linux, Mac and if a 
user would like to use my module, he has to do the same thing!

Python's array module is built-in, easy to use, but *without* a 
FromLenAndData function! Even the buffer interface provides only 'get 
buffer' but no 'set buffer' functions. Could anyone tell me how I can 
create an array object from existing data? Some vague ideas might be 
used: 1. PyCObject (I do not really understand the manual), 2. copy and 
modify arraymodule.c to my project (doable at all? License issue?) 3. 
Create an array object and hack it. (no api to do this.)

I would strongly suggest an arraymodule.h with Array_FromLenAndData.
Many thanks in advance.
Bo
I don't know how much this will help but when I am faced with a problem 
like this, I use Pyrex and look at the generated code.  All you need to 
do in Pyrex is import the array module and create your array like you 
would in Python.  To get the data into the array you will need to use 
the buffer interface and fill it in from your C code.
--
http://mail.python.org/mailman/listinfo/python-list


Re: PyQT installation

2005-01-01 Thread John J Lee
On Sat, 1 Jan 2005, Ken Godee wrote:
[...]
 I believe the book C++ GUI programming Qt3 comes
 with a windows Qt gpl 3.x version. Just have to buy
 the book. No PyQt version to match thou.

GPL only if you buy the book makes no sense.  Either it's GPL or it 
isn't.  (It isn't, in fact.)

[...]
  IIRC, people have already run some KDE apps under Windows (though
  still needing X, so far).
  
  I wonder how TrollTech will react as (and if) it progresses.
  
 
 I don't think your giving TrollTech any credit here, yes they have
 a business model and need to make money, but not everybody is
 Microsoft. They are fully aware and supportive of the project
 and I remember reading not to long ago they struck an aggrement
 with the project that if anything ever happened to TrollTech they
 would release Qt to project under gpl, or something like that.

I'd forgotten about that agreement (again, driven by KDE).  Reassuring,
assuming the legalese corresponds to what one assumes is the spirit of it,
and that it will hold water if/when tested in the courts.

I am surprised if they support the effort to make a GPL native MS Windows
version of Qt.  They wouldn't have to be evil monopolists to be concerned
about this development, IMHO.


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


Re: The Industry choice

2005-01-01 Thread Hans Nowak
Donn Cave wrote:
Quoth Hans Nowak [EMAIL PROTECTED]:
| Paul Rubin wrote:
|
| You should write unit tests either way, but in Python you're relying
| on the tests to find stuff that the compiler finds for you with Java.
|
| As I wrote on my weblog a while ago, I suspect that this effect is 
| largely psychological.  You jump through hoops, declaring types all over 
| the place, checking exceptions, working around the language's 
| limitations, etc.  So when your code compiles, it *feels* safer.  Like 
| you're at least part of the way towards ensuring correctness.  All that 
| work must be good for *something*, right?  Never mind that when writing 
| unit tests for a dynamic language, you don't check for these things at 
| all.  How often do you explicitly check types in Python unit tests? 
| IMHO, when using a dynamic language, you don't need most of the checks 
| that Java, C# and their ilk force upon you.

I have been fooling around with strongly, statically typed languages
for a couple of years, in my spare time - Objective CAML, Haskell,
O'Haskell.  This is a little different experience than what you two
are talking about - I don't think Java, C# and their ilk are quite as
rigorous, nor do they use type inference - but as much as it would
probably gag an FP enthusiast to say this, the basic idea is the same.
I can only believe that if you think the benefit of static typing is
psychological, either something is very different between the way you
and I write programs, or you're not doing it right.
I do think it makes more sense in functional languages like the ones you 
mention... that's one of the reasons I am trying to learn OCaml.  I 
don't think the type systems in OCaml, Haskell etc can quite be compared 
to what's used in Java, C#, C++ or Delphi.  So far, I am getting the 
impression that the type system in OCaml is actually there to help you, 
rather than something that gets in your way.

I concur that in my OCaml experiments so far, errors pointed out by the 
compiler made a lot more sense, because they pointed to actual problems, 
rather than being a case of you didn't declare this method as public 
static final.

Of course, it helps that, like Python, said languages (OCaml, Haskell) 
are higher-level than Java/C#/etc, so you can express things concisely 
and clearly.  That might be one of the reasons why static, strong typing 
in VHLLs has a much higher return on investment than it has in 
lower-level languages, where you have to write acres of code just to get 
something done (availability of libraries notwithstanding).

--
Hans Nowak
http://zephyrfalcon.org/
--
http://mail.python.org/mailman/listinfo/python-list


Re: The Industry choice

2005-01-01 Thread Rob Emmons
 For managers of companies it's worse: the company makes
 VERY substantial investments into any technology it marries,
 and that means big losses if it goes. Long-term stability
 of this technology in terms of we're not going to be left out
 in cold alone with this technology to feed it means a lot 
 to them. Even a poor technology with external backing 
 of big, stable vendor is better than the excellent technology 
 without ..

There is the stability issue you mention... but also probably the fear
issue.  If you choose a solution from a major company -- then it fails for
some reason or they drop the product -- it's their fault -- you've got an
automatic fall guy.  On the other hand, an open source solution or 
otherwise less accepted solution ... it will probably be consider 
your fault by the organization.  It's a rational decision to avoid 
personal risk when you don't get much reward for choosing something 
different.

Rob


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


Python equivalent of script(1)

2005-01-01 Thread cepl
Is there anything like script(1) for python interactive sessions. From
script(1) manpage:

Script makes a typescript of everything printed on your terminal.
It is useful for students who need a hardcopy record of an
interactive session as proof of an assignment, as the typescript
file can be printed out later with lpr(1).

If the argument file is given, script saves all dialogue in file.
If no file name is given, the typescript is saved in the file
typescript.

In my case I wouldn't like to use it as a proof of anything, but I want
to get a script accessing a library system in my school -- it means
many attempts to play with urllib. I would prefer to do it in an
interactive session, but then I would love to have a record of all what
I've done, so I can edit this record into final script.
Thanks for any hint,

Matej Cepl

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


Re: Python equivalent of script(1)

2005-01-01 Thread Thomas Rast
[EMAIL PROTECTED] writes:

 I would love to have a record of all what I've done, so I can edit
 this record into final script.

You can save the current input history with

 import readline
 readline.write_history_file(python.log)

If you want to log your whole session, including output, try using the
'screen' utility.

HTH
- Thomas

-- 
If you want to reply by mail, substitute my first and last name for
'foo' and 'bar', respectively, and remove '.invalid'.
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Python equivalent of script(1)

2005-01-01 Thread cepl
Thanks a lot.

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


Re: what is lambda used for in real code?

2005-01-01 Thread Steve Holden
Alex Martelli wrote:
Steve Holden [EMAIL PROTECTED] wrote:

Adam DePrince wrote:
[...]
In sort, we must preserve the ability to create an anonymous function
simply because we can do so for every other object type, and functions
are not special enough to permit this special case.
And you'd create an anonymous type how, exactly?

type('',(),{})
class '__main__.'
maybe...?

Indeed. And then you'd insert all the methods as lambdas by 
We both know that the Python language framework has enough introspection 
capabilities to do this, but I'm pretty sure you wouldn't try to pretend 
that this would represent a realistic programming style. Or am I wrong?

regards
 Steve
--
Steve Holden   http://www.holdenweb.com/
Python Web Programming  http://pydish.holdenweb.com/
Holden Web LLC  +1 703 861 4237  +1 800 494 3119
--
http://mail.python.org/mailman/listinfo/python-list


Looping using iterators with fractional values

2005-01-01 Thread drife
Hello,

Making the transition from Perl to Python, and have a
question about constructing a loop that uses an iterator
of type float. How does one do this in Python?

In Perl this construct quite easy:

for (my $i=0.25; $i=2.25; $i+=0.25) {
printf %9.2f\n, $i;
}

Thanks in advance for your help.


Daran Rife
[EMAIL PROTECTED]

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


Re: The Industry choice

2005-01-01 Thread Steve Holden
Rob Emmons wrote:
For managers of companies it's worse: the company makes
VERY substantial investments into any technology it marries,
and that means big losses if it goes. Long-term stability
of this technology in terms of we're not going to be left out
in cold alone with this technology to feed it means a lot 
to them. Even a poor technology with external backing 
of big, stable vendor is better than the excellent technology 
without ..

There is the stability issue you mention... but also probably the fear
issue.  If you choose a solution from a major company -- then it fails for
some reason or they drop the product -- it's their fault -- you've got an
automatic fall guy.  On the other hand, an open source solution or 
otherwise less accepted solution ... it will probably be consider 
your fault by the organization.  It's a rational decision to avoid 
personal risk when you don't get much reward for choosing something 
different.

You are ignoring the fact that with the open source solution you do at 
least have the option of hiring bright programmers to support the 
framework which has now become moribund, whereas when a company goes 
bust there's no guarantee the software IP will ever be extricated from 
the resulting mess.

So I'm not sure I'd agree with rational there, though comprehensible 
might be harder to argue with.

Personally I'd feel in a better position standing before a Board of 
Directors and saying While it's true that our chosen software platform 
isn't being supported by the original team any more, we do have options 
to continue to move it forward, including forming a consortium with 
other users.

Avoidance of blame is way too large a motivator in large organizations, 
and it leads to many forms of sub-optimal decision making.

regards
 Steve
--
Steve Holden   http://www.holdenweb.com/
Python Web Programming  http://pydish.holdenweb.com/
Holden Web LLC  +1 703 861 4237  +1 800 494 3119
--
http://mail.python.org/mailman/listinfo/python-list


Re: Looping using iterators with fractional values

2005-01-01 Thread Mark McEahern
drife wrote:
Hello,
Making the transition from Perl to Python, and have a
question about constructing a loop that uses an iterator
of type float. How does one do this in Python?
 

Use a generator:
 def iterfloat(start, stop, inc):
... f = start
... while f = stop:
... yield f
... f += inc
...
 for x in iterfloat(0.25, 2.25, 0.25):
... print '%9.2f' % x
...
   0.25
   0.50
   0.75
   1.00
   1.25
   1.50
   1.75
   2.00
   2.25

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


I need some advice/help on running my scripts

2005-01-01 Thread Sean
For the last couple of months I have been reading and working throught
the examples in Magnus Lie Hetland's Book Practical Python  This for
all practical purposes is the first computer programming language I
have spent any time at learning, so much of what I have covered in the
book was for the first time.

My problem is that many of the example scripts are run on Linux
machines and I am using Win XP Pro.  Here is a specific example of what
is confusing me.  If I want to open a file from the dos prompt in some
script do I just write the name of the file I want to open (assuming it
is in the same directory) after the script name?
such as

c:\some_script.py some_text_file.txt

Does piping work the same way in dos as it does on a linux machine?
And last but not least, is there a way to do this all from IDLE?

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


Re: Python equivalent of script(1)

2005-01-01 Thread Grant Edwards
On 2005-01-01, [EMAIL PROTECTED] [EMAIL PROTECTED] wrote:

 Is there anything like script(1) for python interactive sessions.

  $ script transcript.txt
  Script started, file is transcript.txt
  $ python
  ...

Not sure if there's a way to shut off readline...

-- 
Grant Edwards   grante Yow!  But was he mature
  at   enough last night at the
   visi.comlesbian masquerade?
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Looping using iterators with fractional values

2005-01-01 Thread Reinhold Birkenfeld
drife wrote:
 Hello,
 
 Making the transition from Perl to Python, and have a
 question about constructing a loop that uses an iterator
 of type float. How does one do this in Python?
 
 In Perl this construct quite easy:
 
 for (my $i=0.25; $i=2.25; $i+=0.25) {
 printf %9.2f\n, $i;
 }

=Py2.3:

for i in [x/4.0 for x in xrange(1, 10)]:
print %9.2f % i

Py2.4:

for i in (x/4.0 for x in xrange(1, 20)):
print %9.2f % i

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


Re: Python equivalent of script(1)

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

 In my case I wouldn't like to use it as a proof of anything, but I want
 to get a script accessing a library system in my school -- it means
 many attempts to play with urllib. I would prefer to do it in an
 interactive session, but then I would love to have a record of all what
 I've done, so I can edit this record into final script.
 Thanks for any hint,

Emacs will do that for you, either in a shell (command shell) or in a
Python shell. Edit a python file, and type C-C ! and it'll start an
interactive python in a buffer that will save all the output.

Of course, you can also run your python after running script. That
will log everything from the python session in the script 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: Looping using iterators with fractional values

2005-01-01 Thread Steven Bethard
Mark McEahern wrote:
drife wrote:
Hello,
Making the transition from Perl to Python, and have a
question about constructing a loop that uses an iterator
of type float. How does one do this in Python?
 

Use a generator:
  def iterfloat(start, stop, inc):
... f = start
... while f = stop:
... yield f
... f += inc
...
  for x in iterfloat(0.25, 2.25, 0.25):
... print '%9.2f' % x
...
   0.25
   0.50
   0.75
   1.00
   1.25
   1.50
   1.75
   2.00
   2.25
 
Or use the numarray module:
py import numarray as na
py for f in na.arange(0.25, 2.25, 0.25):
... print '%9.2f' % f
...
 0.25
 0.50
 0.75
 1.00
 1.25
 1.50
 1.75
 2.00
Steve
--
http://mail.python.org/mailman/listinfo/python-list


Re: what is lambda used for in real code?

2005-01-01 Thread Alex Martelli
Steve Holden [EMAIL PROTECTED] wrote:
   ...
 And you'd create an anonymous type how, exactly?
  
 type('',(),{})
   ...
 Indeed. And then you'd insert all the methods as lambdas by 
 
 We both know that the Python language framework has enough introspection
 capabilities to do this, but I'm pretty sure you wouldn't try to pretend
 that this would represent a realistic programming style. Or am I wrong?

Calling 'type' to make a new type on the fly is occasionally neat -- not
quite as often as (say) using 'lambda', maybe;-).  Neither should be a
'programming _style_' as opposed to an occasional convenience -- of the
two, lambda has more sensible use cases, but is also more prone to
overuse in practice.  Also, the ability to call 'type' to make a type
adds zero complexity or issues to the language: it's basically zero
cost, just like the ability to call 'int' to make an int, and so on.
This can't be said of lambda, alas: it has a non-zero cost in terms of
(slightly) 'fattening' the language.


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


Re: I need some advice/help on running my scripts

2005-01-01 Thread Steven Bethard
Sean wrote:
My problem is that many of the example scripts are run on Linux
machines and I am using Win XP Pro.  Here is a specific example of what
is confusing me.  If I want to open a file from the dos prompt in some
script do I just write the name of the file I want to open (assuming it
is in the same directory) after the script name?
such as
c:\some_script.py some_text_file.txt
It's unclear to me what you want to do here.  If your some_script.py 
looks like:

import sys
f = file(sys.argv[1])
then yes, you can call some_script.py as above, and the file will be 
readable from the 'f' file object.


Does piping work the same way in dos as it does on a linux machine?
Mostly:
[D:\Steve]$ type test.py
import sys
for i, line in enumerate(sys.stdin):
sys.stdout.write(%i:%s % (i, line))
[D:\Steve]$ type input.txt
A
B
C
D
[D:\Steve]$ python test.py  input.txt
0:A
1:B
2:C
3:D
[D:\Steve]$ python test.py  output.txt
Z
Y
X
^Z
^Z
[D:\Steve]$ type output.txt
0:Z
1:Y
2:X
[D:\Steve]$ python test.py  input.txt  output.txt
[D:\Steve]$ type output.txt
0:A
1:B
2:C
3:D
[D:\Steve]$ type input.txt | python test.py
0:A
1:B
2:C
3:D
Note however, that you may run into problems if you don't explicitly 
call python:

[D:\Steve]$ test.py  input.txt
Traceback (most recent call last):
  File D:\Steve\test.py, line 2, in ?
for i, line in enumerate(sys.stdin):
IOError: [Errno 9] Bad file descriptor
And last but not least, is there a way to do this all from IDLE?
What exactly do you want to do?  You can certainly type something like:
f = file('input.txt')
in IDLE to get access to the 'input.txt' file...
Steve
--
http://mail.python.org/mailman/listinfo/python-list


What can I do with Python ??

2005-01-01 Thread BOOGIEMAN
Beginners question, but really what can you do with it ?
How hard is Python to learn compared with other languages 
(let's say C#). Can you make fullscreen game with it (for example) ? 
I've looked at http://www.python.org but nothing concrete there
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Clearing the screen

2005-01-01 Thread Artur M. Piwko
In the darkest hour on Sat, 25 Dec 2004 09:41:54 +1030,
Ishwor [EMAIL PROTECTED] screamed:
 def cls():
   for i in range(1,40):
   print  ;


Slightly ot, but perhaps this'll work for you:

def cls():
print \033[2J


-- 
[ Artur M. Piwko : Pipen : AMP29-RIPE : RLU:100918 : From == Trap! : SIG:217B ]
[ 22:06:17 user up 10478 days, 10:01,  1 user, load average: 0.06, 0.06, 0.06 ]

Even God cannot change the past.
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: exposing C array to python namespace: NumPy and array module.

2005-01-01 Thread Scott David Daniels
Bo Peng wrote:
Dear list,
I am writing a Python extension module that needs a way to expose pieces 
of a big C array to python. Currently, I [use] NumPy Users ... actually
 change the underlying C array.
Python's array module is built-in, easy to use, but *without* a 
FromLenAndData function! 
Python's array module is not built to do this well.  It can re-size the
array, delete elements inside the array, and other things that don't
work very well with C-managed data.  I wrote blocks and views to
overcome this problem.  A View of data can be pointed at data, and
the view behaves much like a Python array (except that you cannot
affect the array's size).  You can even take slices of the view,
which will produce a new view referring to the same base memory.  There
are two kinds of views available, read-only views and writable views.
Have a look at:
http://members.dsl-only.net/~daniels/Block.html
to see if it addresses your problem.  It is MIT-licensed (give credit,
but feel free to use).  Let me know if it works OK, could use a tweak,
or is completely useless.  I'll be more than happy to respond to
questions.
--Scott David Daniels
[EMAIL PROTECTED]
--
http://mail.python.org/mailman/listinfo/python-list


Re: Looping using iterators with fractional values

2005-01-01 Thread Reinhold Birkenfeld
Mike Meyer wrote:

 Or - and much safer when dealing with floating point numbers - iterate
 over integers and generate your float values:
 
 for j in range(1, 9):
 i = j * .25
 print %9.2f % i

There's a glitch there, though - should be range(1, 10).

Reinhold

PS: I'm wondering whether my genexp approach or this one is preferable.
Readability is equal, I would say, but how about speed?

Brought up a few timeits:

Python 2.3
--

for i in [x/4.0 for x in range(1, 10)]:   36,9 sec

for j in range(1, 10): i = j * 0.25:  33,7 sec

Python 2.4
--

for i in (x/4.0 for x in range(1, 10)):   32,5 sec

for j in range(1, 10): i = j * 0.25:  28,4 sec


So what does that tell us?
(a) don't use genexps where there is a simpler approach
(b) Py2.4 rocks!

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


Re: Securing a future for anonymous functions in Python

2005-01-01 Thread Simo Melenius
Doug Holton [EMAIL PROTECTED] writes:

 Steven Bethard wrote:
  Simo Melenius wrote:
  map (def x:

Oops, I found a typo alreay. I meant to write def (x): -- no name
for anonymous functions but just the argument list, please :)

 Right the comma plus other things make this difficult for a parser to
 handle correctly.  Other people have already come up with working

It is difficult since in Python we don't enjoy the ugly luxury of
stuffing statements inside {}s. But as Bengt pointed out, parentheses
don't actually look bad at all in this case.

 Then the multi-line way.  I had to add an overload of map to support
 reversing the order of parameters (list first, then the closure):
 
 newlist = map([1,2,3,4,5,6]) def (x as int):
   return x*x*x

That doesn't seem to scale if you want to pass more than one anonymous
function arguments to the function or call an arbitrary function with
parameters in arbitrary order.


br,
S
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: PyQT installation

2005-01-01 Thread Jarek Zgoda
Ken Godee wrote:
I believe the book C++ GUI programming Qt3 comes
with a windows Qt gpl 3.x version. Just have to buy
the book. No PyQt version to match thou.
No, Sir. It's a non-commercial edition. At the request from Trolltech, 
there's no PyQt-nc available for this version of Qt.

Blackadder from the Kompany, while not free, is still
a pretty good deal. Like  $100 for personal and around
$350 for commercial version. Include current windows/linux
versions of (Qt)PyQt along with converted Qt C++ to PyQt docs.
This is much better way to get PyQt for Windows!
--
Jarek Zgoda
http://jpa.berlios.de/ | http://www.zgodowie.org/
--
http://mail.python.org/mailman/listinfo/python-list


Re: UserDict deprecated

2005-01-01 Thread Hans Nowak
Uwe Mayer wrote:
Why is the UserDict module is deprecated after Python 2.2. The application
of it I have in mind is, i.e. multiple inheritance from file and dic -
which is not possible. 
I am curious, what would you do with a class that derives from both file 
and dict?

--
Hans Nowak
http://zephyrfalcon.org/
--
http://mail.python.org/mailman/listinfo/python-list


Re: UserDict deprecated

2005-01-01 Thread Hans Nowak
Uwe Mayer wrote:
Why is the UserDict module is deprecated after Python 2.2. The
application of it I have in mind is, i.e. multiple inheritance from
file and dic - which is not possible.
[...]
I was writing a class that read /writes some binary file format. I
implemented the functions from the file interface such that they are
refering to records. However, the file format has some header fields and
I'd wanted to grant access to those via the dict-interface.
Another example: working with PyQt I have an instance of a QListView and
wanted to use the list-interface to get and set individual records.
If it's just a matter of attribute access, implementing the relevant 
__getitem__ and __setitem__ methods will probably suffice.  I don't 
think that deriving from dict or list will do you much good here... most 
of the methods will be irrelevant, or won't do what you want, so you 
have to override them anyway.

--
Hans Nowak
http://zephyrfalcon.org/
--
http://mail.python.org/mailman/listinfo/python-list


Approximating scattered data

2005-01-01 Thread Grant Edwards
I've been looking for some way to approximate scattered 3D data
points in Python.  The data doesn't seem to be amenable to
fitting functions like polymials, so I may have to use
something more like a spline surface.

However, I can't find anything usable from Python, and my
Fortram skills are pretty rusty. I tried SciPy, but it's spline
fitting module doesn't work at all for my data.  I've found
mentions of a Python port NURBS toolbox, but all the links I
can find are broken.

I've also found references to Fortran programs from
netlib/toms, but I was hoping there might be something that was
already usable from Python.

Can anybody point me toward a Python module for approximating
scattered data using something like a Renka algorithm?

-- 
Grant Edwards   grante Yow!  How do I get HOME?
  at   
   visi.com
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: The Industry choice

2005-01-01 Thread Paul Rubin
Steve Holden [EMAIL PROTECTED] writes:
  It seems to me
  that IDLE and a lot of the rest of Python are examples of someone
  having a cool idea and writing a demo, then releasing it with a lot of
  missing components and rough edges, without realizing that it can't
  reasonably be called complete without a lot more work.
 
 ^Python^open source^

I wouldn't say so.  I'd say the Linux kernel, GCC, Emacs, Apache,
Mozilla, etc. are all developed with a much more serious attitude than
Python is.  Of course there are lots of other FOSS programs that
someone wrote for their own use and released, that are less polished
than Python, but that are also the subject of less advocacy than Python.
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: The Industry choice

2005-01-01 Thread Paul Rubin
[EMAIL PROTECTED] (Cameron Laird) writes:
 That is, while I have a LOT of respect for Paul's programming
 and judgment, and question myself when I'm on the side opposite
 him, I ultimately value type declarations in languages such as
 Java as more cost than benefit.

I don't find static type declarations to have much cost.  It's just a
few more keystrokes.  I'm open to persuasion about whether they have
benefit.  

I do believe that it's a horrible deficiency in Python that it has no
declarations at all, even optional ones, like perl -w or use
strict.  Python's scoping hacks that result from the lack of
declarations just seem to me like pure insanity.

I was pretty skeptical of Java's checked exceptions when I first used
them but have been coming around about them.  There's just been too
many times when I wrote something in Python that crashed because some
lower-level function raised an exception that the upper level hadn't
been expecting, after the program had been in use for a while.  I'd
sure rather find out about that at compile time.
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: UserDict deprecated

2005-01-01 Thread Steven Bethard
Uwe Mayer wrote:
Saturday 01 January 2005 22:48 pm Hans Nowak wrote:
I am curious, what would you do with a class that derives from both file
and dict?
I was writing a class that read /writes some binary file format. I
implemented the functions from the file interface such that they are
refering to records. However, the file format has some header fields and
I'd wanted to grant access to those via the dict-interface.
If you implemented the file interface functions yourself, why do you 
want to inherit from file?

Another example: working with PyQt I have an instance of a QListView and
wanted to use the list-interface to get and set individual records.
But just inheriting from list won't make this work, will it?  Don't you 
want to do something like:

class C(QListView):
def __getitem__(self, i):
return self.getIndividualRecord(i) # or whatever method gives
   # you the record
Steve
--
http://mail.python.org/mailman/listinfo/python-list


Re: UserDict deprecated

2005-01-01 Thread Alex Martelli
Uwe Mayer [EMAIL PROTECTED] wrote:
   ...
 If I used UserDict I would not need to specify all methods UserDict provides
 alreay, anyway.

The DictMixin class from the UserDict module is *not* deprecated -- only
the UserDict class from the same module.  (If you found info saying
otherwise pls provide a URL: the info is wrong and I'll try to get it
fixed -- thanks!).  DictMixin's purpose is exactly as you state: letting
you make a complete mapping class out of one which only supplies the
very basics, by mix-in inheritance.


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


Re: Looping using iterators with fractional values

2005-01-01 Thread beliavsky
Mike Meyer wrote:

Or - and much safer when dealing with floating point numbers - iterate
over integers and generate your float values:

for j in range(1, 9):
   i = j * .25
   print %9.2f % i

I agree with this suggestion. As an historical aside, Fortran had loops
with floating point variables for decades, but in 1995, the first
standard in a while to REMOVE features, this was one of the few things
deleted. The Fortran standards committee is very conservative about
creating backwards incompatibilities, but they must have thought loops
with floating point variables are so error-prone -- and alternatives
with integer counters are so easy to write -- that they put their foot
down. I know the OP is asking about Python, but the principle is the
same.

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


Re: What can I do with Python ??

2005-01-01 Thread Alex Martelli
BOOGIEMAN [EMAIL PROTECTED] wrote:

 Beginners question, but really what can you do with it ?

You can write application programs, big or small, of just about any kind
you may imagine, on just about any platform you may imagine (from
mainframes and supercomputers down to powerful cellphones such as
Nokia's S-60 series).

 How hard is Python to learn compared with other languages 
 (let's say C#).

Python is among the simplest languages to learn.

 Can you make fullscreen game with it (for example) ? 

Sure!  Have you thought of using google?  The first 2 hits for
python games
are on www.pygame.org, This allows you to create fully featured games
and multimedia programs in the python language, as the google snippet
says.  The third hit is for the book Game Programming With Python on
the amazon site.  The 4th one is about Month Python, but the fifth is
back to our subject -- a Python Game Programming page full of useful
links.  Then you get another link to the same book, a link to a
different book, c.

 I've looked at http://www.python.org but nothing concrete there

You _gotta_ be kidding, right...?  The Beginner's Guide link takes you
right to the BeginnersGuide page which starts with the reassurance that
Python is easy to learn even if you're new to programming and continues
with a zillion useful links.  The Python Books link takes you to a huge
list of books, and the FIRST subheading under specific applications is
for game programming, leading you to the two books I already mentioned.
There's a Search bar, enter Game there, and the FIRST hit is
http://www.python.org/moin/GameProgramming which STARTS with the
reassurance that, yes, you CAN write whole games in Python, and
besides PyGame also points you to PyKira, a fast game development
framework for Python (which) also supports MPEG video, sound (MP3, Ogg
Vorbis, Wav and Multichannel module files), direct images reading and
much more.  Etc, etc, ...!!!

How much more concrete could you expect *ANY* blessed website to BE,
for Pete's sake?!??!


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


Re: exposing C array to python namespace: NumPy and array module.

2005-01-01 Thread Scott David Daniels
Bo Peng wrote:
Scott David Daniels wrote:
I wrote blocks and views to overcome this problem.
I was too impatient to wait for your reply. :-) 
I call 21-hour turnaround over New Year's Eve pretty good. Clearly I
will never be quick enough for you ;-).  Since I presented this at
the Vancouver Python Workshop last August, I'll claim a negative
five months response time (possibly a personal best).
--Scott David Daniels
[EMAIL PROTECTED]
--
http://mail.python.org/mailman/listinfo/python-list


Re: What can I do with Python ??

2005-01-01 Thread Alan Gauld
On Sat, 1 Jan 2005 21:57:32 +0100, BOOGIEMAN 
 (let's say C#). Can you make fullscreen game with it (for example) ? 

RANT
You can but please don't! Make your game run fast in a window.
I hate fascist games programmers who insist on monopolising a 21
inch 1600x1200 display and assuming I have nothing better to do
than play their game. If that's all I wanted to do I'd stick with
DOS, or buy a Nintendo... I have a multi tasking PC please let me
multi task!
/RANT

;-)

Alan G.

Author of the Learn to Program website
http://www.freenetpages.co.uk/hp/alan.gauld
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Approximating scattered data

2005-01-01 Thread beliavsky
Grant Edwards wrote:
I've been looking for some way to approximate scattered 3D data
points in Python. The data doesn't seem to be amenable to
fitting functions like polymials, so I may have to use
something more like a spline surface.

However, I can't find anything usable from Python, and my
Fortram skills are pretty rusty. I tried SciPy, but it's spline
fitting module doesn't work at all for my data. I've found
mentions of a Python port NURBS toolbox, but all the links I
can find are broken.

NURBS is available in Matlab and Scilab at
http://www.aria.uklinux.net/nurbs.php3 , and translating to Python with
Numeric/Numarray should not be too hard.

If you are trying to fit z = f(x,y) without having a particular
functional form in mind, you can apply a nonparametric regression
technique. One of the easiest approaches to code is Nadaraya-Watson
kernel regression -- see for example
http://www.quantlet.com/mdstat/scripts/spm/html/spmhtmlnode24.html ,
equation 4.68, where a Gaussian kernel can be used for K. PyML at
http://pyml.sourceforge.net/doc/tutorial/tutorial.html may implement
this (I have not tried it). LIBSVM at
http://www.csie.ntu.edu.tw/~cjlin/libsvm/ has a Python interface for
Support Vector Machines, a fairly popular and recent flexible
regression method.

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


Re: The Industry choice

2005-01-01 Thread Paul Rubin
[EMAIL PROTECTED] writes:
 Overall I agree with you and would like to have OPTIONAL static type
 declarations in Python, as has often been discussed. But without
 facilities for generic programming, such as templates in C++, static
 type declarations can force one to duplicate a LOT of code, with one
 sorting routine each for integer, floats, strings, etc.

I don't see that big a problem.  The current Python sorting routine
operates on instances of class object and calls the __cmp__ method
to do comparisons.  Every class of sortable objects either defines a
__cmp__ method or inherits one from some superclass, and sort calls
those methods.  Static type declarations would not require writing any
additional sorting routines.
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Red Robin Jython JDK classes

2005-01-01 Thread not [quite] more i squared
Henri Sivonen a écrit :
I am trying to set up the Red Robin Jython Development Tools for 
Eclipse. It finds the Python libraries of Jython and my own jars. It 
does not find the JDK classes. If I try to add classes.jar from the JDK 
to the Jython Class Path of the project, the plug-in no longer finds 
even my own Java classes.

How do I make the plug-in see the JDK classes?
I am using Eclipse 3.0.1, Mac OS X 10.3.7, Java 1.4.2_05, Jython 2.1 and 
Red Robin Jython Development Tools 1.3.9.

if i may ask, did you ask the author of the eclipse plugin ? did anybody 
else answer you ? or could you by any chance, solve it by yourself ?
--
http://mail.python.org/mailman/listinfo/python-list


PEP 288 ponderings

2005-01-01 Thread Steven Bethard
PEP 288 was mentioned in one of the lambda threads and so I ended up 
reading it for the first time recently.  I definitely don't like the 
idea of a magical __self__ variable that isn't declared anywhere.  It 
also seemed to me like generator attributes don't really solve the 
problem very cleanly.  An example from the PEP[1]:

def mygen():
while True:
print __self__.data
yield None
g = mygen()
g.data = 1
g.next()# prints 1
g.data = 2
g.next()# prints 2
I looked in the archives but couldn't find a good discussion of why 
setting an attribute on the generator is preferable to passing the 
argument to next.  Isn't this example basically equivalent to:

class mygen(object):
def next(self, data):
print data
return None
g = mygen()
g.next(1)   # prints 1
g.next(2)   # prints 2
Note that I didn't even define an __iter__ method since it's never used 
in the example.

Another example from the PEP:
def filelike(packagename, appendOrOverwrite):
data = []
if appendOrOverwrite == 'w+':
data.extend(packages[packagename])
try:
while True:
data.append(__self__.dat)
yield None
except FlushStream:
packages[packagename] = data
ostream = filelike('mydest','w')
ostream.dat = firstdat; ostream.next()
ostream.dat = firstdat; ostream.next()
ostream.throw(FlushStream)
This could be rewritten as:
class filelike(object):
def __init__(self, packagename, appendOrOverwrite):
self.data = []
if appendOrOverwrite == 'w+':
self.data.extend(packages[packagename])
def next(self, dat):
self.data.append(dat)
return None
def close(self):
packages[packagename] = self.data
ostream = filelike('mydest','w')
ostream.next(firstdat)
ostream.next(firstdat)
ostream.close()
So, I guess I have two questions:
(1) What's the benefit of the generator versions of these functions over 
the class-based versions?

(2) Since in all the examples there's a one-to-one correlation between 
setting a generator attribute and calling the generator's next function, 
aren't these generator attribute assignments basically just trying to 
define the 'next' parameter list?

If this is true, I would have expected that a more useful idiom would 
look something like:

def mygen():
while True:
data, = nextargs()
print data
yield None
g = mygen()
g.next(1)   # prints 1
g.next(2)   # prints 2
where the nextargs function retrieves the arguments of the most recent 
call to the generator's next function.

With a little sys._getframe hack, you can basically get this behavior now:
py class gen(object):
... def __init__(self, gen):
... self.gen = gen
... def __iter__(self):
... return self
... def next(self, *args):
... return self.gen.next()
... @staticmethod
... def nextargs():
... return sys._getframe(2).f_locals['args']
...
py def mygen():
... while True:
... data, = gen.nextargs()
... print data
... yield None
...
py g = gen(mygen())
py g.next(1)
1
py g.next(2)
2
Of course, it's still a little magical, but I think I like it a little 
better because you can see, looking only at 'mygen', when 'data' is 
likely to change value...

Steve
[1] http://www.python.org/peps/pep-0288.html
--
http://mail.python.org/mailman/listinfo/python-list


Re: Confusion About Classes

2005-01-01 Thread flamesrock
Well, I took a short excursion into xml for a different part of the
program, but the class is now finished!! (source below) I have a few
more questions for you guys.

1)If I set
self.serverConfig = file(os.path.join('configuration','score.conf'),
'w')
and then go self.serverConfig.close(), is there a way to open it in
read mode without calling the filename directly? Like
self.serverConfig.open('r') -which doesn't work BTW- instead of
open(os.path.join('configuration','score.conf'), 'r')? Using
open(self.ServerConfig, 'r') doesn't seem to work either.

2) A problem arose when converting the encoded file back into a string
for decoding; the \n's seemed to have dissapeared so it returned an
error when I tried to decode. Is there a way to stop python from
disposing of those those characters before writing to file?

3) Is there way to retrieve the timestamp of a file via ftp?

There are NAMES, thats why there are NAMESPACES.
A name is just a reference or 'handle' to an object, because.
everything in python is an OBJECT,(except for names, they are just
names).
And
That being said, understanding this part of Python can aid enormously
in
working with the language. If you'd like to think of it in C terms,
basically every 'name' in Python is a pointer to a PyObject, and
'names'
(as pointers) are always passed by value -- that is, you get a copy
of
the pointer, but not a copy of the object.

Ah...now *that* makes sense. So dynamically typed variables aren't
variables at all...but just another term for pointers? cool, I like
that. And thanks for the examples, guys. Its coming together and I'm
starting to actually 'get it' :)

So name mangling basically follows the rules of private and public in
Java? (I have limited experience in java so don't quote me on that.)

(Google groups isn't displaying the spaces correctly, so I added four
*'s = 1 four space indentation..)
import ftplib, ezPyCrypto, ConfigParser
import traceback, sys, os
#---
class create_server:
'''Create a create_server object whenever a new server is to be
added to
the SCORE network.'''
def __init__(self, absCFGpath, domain, servername, master_ftpUSER,
master_ftpPASS, score_ftpUSER='anonymous',
score_ftpPASS='anonymous',
scorecontact='[EMAIL PROTECTED]', maxusers='unlimited',
httpport='80',
ftpport='21', passivemode=True):
self.domain = domain
self.httpport   = httpport
self.ftpport= ftpport
self.servername = servername
self.maxusers   = maxusers
self.scorecontact   = scorecontact
try:
self.serverConfig   =
file(os.path.join('configuration','score.conf'), 'w')
except:
os.mkdir('configuration')
self.serverConfig   =
file(os.path.join('configuration','score.conf'), 'w')
self.absCFGpath= absCFGpath
self.master_ftpUSER= master_ftpUSER
self.master_ftpPASS= master_ftpPASS
self.score_ftpUSER = score_ftpUSER
self.score_ftpPASS = score_ftpPASS
self.passivemode   = passivemode
self.parser= ConfigParser.ConfigParser()

#---
def createUniversalConfig(self):
'''Creates the SCORE servers main configuration file.'''
self.parser.add_section('score')
self.parser.set('score', 'domain', self.domain)
self.parser.set('score', 'servername', self.servername)
self.parser.set('score', 'httpport', self.httpport)
self.parser.set('score', 'ftpport', self.ftpport)
self.parser.set('score', 'maxusers', self.maxusers)
self.parser.set('score', 'scorecontact', self.scorecontact)
self.parser.write(self.serverConfig)
self.serverConfig.close()
return open(os.path.join('configuration','score.conf'), 'r')
#---
def editUniversalConfig(self, section, key, value):
'''edit the SCORE servers main configuration file.'''
self.serverConfig =
open(os.path.join('configuration','score.conf'), 'w')
self.parser.set(section, key, value)
self.serverConfig.close
#---
def createUSERfile(self):
'''Creates an encrypted ascii public ftp password file that
clients use
to decode after logging in'''
crypt = ezPyCrypto.key()
userFile = file(os.path.join('configuration','uFile.enc'), 'w')
userFile.write(crypt.encStringToAscii(self.score_ftpUSER))
userFile.close()
userFile = open(os.path.join('configuration','uFile.enc'), 'r')
return userFile
#---
def createPASSfile(self):

Re: What can I do with Python ??

2005-01-01 Thread BOOGIEMAN
On Sat, 01 Jan 2005 16:03:08 -0500, Mark Nenadov wrote:

 What can you do with Python? Just about anything your heart desires.

Thanks everybody, I downloaded latest windows version and
Python-Docs-2.4 archive. Is that enough for absolute beginner.
Is there any e-book, step by step guide ... etc for download,
or anything else important what I have to know before I start
learning Python ?
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: What can I do with Python ??

2005-01-01 Thread Doug Holton
BOOGIEMAN wrote:
Thanks everybody, I downloaded latest windows version and
Python-Docs-2.4 archive. Is that enough for absolute beginner.
Is there any e-book, step by step guide ... etc for download,
or anything else important what I have to know before I start
learning Python ?
The main thing I would do is subscribe to the python-tutor list.  It is 
the best place by far to ask any questions when you are learning to use 
python: http://mail.python.org/mailman/listinfo/tutor

Second, here are some of the best tutorials specifically designed for 
people with little or no previous programming experience:
http://www.honors.montana.edu/~jjc/easytut/easytut/
http://www.freenetpages.co.uk/hp/alan.gauld/
http://www.dickbaldwin.com/tocpyth.htm
http://www.ibiblio.org/obp/pyBiblio/

And lastly, really a great way to learn is to look at what's already out 
there in python.  Try out some of the many 3rd party libraries and 
programs for python:
For games: http://pygame.org/
For GUI applications: http://www.wxpython.org/
and others: http://www.python.org/pypi
--
http://mail.python.org/mailman/listinfo/python-list


Re: Is it possible to open a dbf

2005-01-01 Thread Miklós P

 Paul Rubin wrote:

  John Fabiani [EMAIL PROTECTED] writes:
  I'm wondering if there is a module available that will open a dbf
 
 So far (more than a minute) I have discovered a reader only.  So if you
have
 a URL or a search string it would be very helpful.

 TIA
 John

Yes, dBase Python yields only some code for reading dBase ... and lots of
enquires about such a thing...

Miklós

---
Jegenye 2001 Bt.
Egyedi szoftverkészítés, tanácsadás | Custom software development,
consulting
Magyarul: http://jegenye2001.parkhosting.com
In English: http://jegenye2001.parkhosting.com/en


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


Re: PEP 288 ponderings

2005-01-01 Thread Jp Calderone
On Sun, 02 Jan 2005 01:04:06 GMT, Steven Bethard [EMAIL PROTECTED] wrote:
PEP 288 was mentioned in one of the lambda threads and so I ended up 
 reading it for the first time recently.  I definitely don't like the 
 idea of a magical __self__ variable that isn't declared anywhere.  It 
 also seemed to me like generator attributes don't really solve the 
 problem very cleanly.  An example from the PEP[1]:
 
 [snip]

  You may be interested in greenlets.  They solve this problem in a way 
which is conceptually much simpler and cleaner.

  They are available as a third party extension, and unfortunately are 
not terribly well documented yet (but luckily they are quite simple).

  http://codespeak.net/svn/user/arigo/greenlet/

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


screen clear question

2005-01-01 Thread jcollins
Is there a command in Python to clear the screen?  That is without writing
multiple blank lines.

Thanks.

Jim C


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


ANN: PyXR 0.9.4 - Cross-Referenced HTML from Python Source

2005-01-01 Thread olsongt
PyXR 0.9.4 - Cross-Referenced HTML from Python Source

PyXR generates pretty-printed HTML pages from python source files to make 
source browsing easier.  It provides extensive cross-referencenced hyperlinks 
that integrate with the Python Library Reference as well as other python source 
files.  Its cross-referencing capabilities are vastly superior to other source 
pretty-printing packages that I've seen.

0.9.4 provides compatibility with the new language semantics in Python 2.4 and 
addresses changes to the format of the Library Reference Manual, in addition to 
a littany of smaller fixes.  Previous versions WILL NOT work with python 2.4.  
This release does provide backward compatibility to python versions as early as 
2.2 and should work on all major OSes.

PyXR is available for download here:
http://sourceforge.net/projects/pyxr/

PyXR'ed markup of Python 2.4's source is available here:
http://pyxr.sourceforge.net/PyXR/

Detailed release notes are available here:
http://pyxr.sourceforge.net/

Enjoy!

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


HTTP GET request with basic authorization?

2005-01-01 Thread Christopher J. Bottaro
How do I do this using httplib.HTTPConnection and
httplib.HTTPConnection.request()?  The library reference only gives a
simple GET example with no header stuff.  I tried this, but it didn't work:
conn.request(GET, /somepage.html, None, {AUTHORIZATION: Basic
username:password})

Thanks for the help.

P.S.  I know nothing about HTTP, I'm just piecing this stuff together from
examples on the internet, please be gentle with your responses.

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


Re: Mailing list hosting?

2005-01-01 Thread weblord

free mailing list offer - limited time
http://nabaza.com/autoresponders.htm

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


Re: OT: spacing of code in Google Groups

2005-01-01 Thread JanC
Terry Reedy schreef:

 JanC [EMAIL PROTECTED] wrote in message 
 news:[EMAIL PROTECTED]
 I don't know if gmane keeps formating of messages intact when
 posting? That could be an alternative too...
 
 Reading posts via gmane with Outlook Express preserves leading spaces
 just fine.  However, OE deletes tabs regardless of what newsgroup
 does. 

OE is useless anyway (at least as a newsreader).

I was talking about the gmane web interface as an alternative to Google's.  
They have an NNTP server but also three http-based interfaces: RSS-feed, 
blog-style  framed (looks more or less like a newsreader).

...

Tested it in gmane.test, posting through their web interface preserves 
whitespace.


-- 
JanC

Be strict when sending and tolerant when receiving.
RFC 1958 - Architectural Principles of the Internet - section 3.9
-- 
http://mail.python.org/mailman/listinfo/python-list


Frameworks for Non-Content Oriented Web Apps

2005-01-01 Thread mirnazim
Hi,

There are great Python Web Application Framework. But most of them are
meant for content oriented web apps.

Is there something that can ease the development of application that
are not content oriented(I call them NON CONTENT-ORIENTED WEB
APPLICATIONS because I don't know what else to call them). I mean the
applications like, accounting,  high volume data entry apps,  where
normally GUI clients have ruled. I know very high quality ERP and
similar packages have been implemented in a web based environment. But
problem is that they have been developed with the tools that were
actually meant for content oriented apps like Zope, PHP, etc.

But is there some sort of framework or something that is actually meant
for such web apps,application that make heavy use of forms, have very
high amount of user interaction etc.

What I am asking here may sound off beat, but I think, in todays world
where web based solutions offers such a flexibility, we really need it.

I also know that I am to ambiguous, but as is the characteristic of
this wonderful community, talks that start as most abigous, transform
in crystal clear.

PS: I am a web developer, using PHP for living. I have been playing
with python for a while. I found python is really a cool language(do I
need to say that ;-)) with a really, really impressive collection of
modules and frameworks. While developing a school information system, I
felt the need of such a framework that makes developing of Non-Content
Oriented Web-Apps easy.

I know I posted a similar message earlier, but this time I a bit more
general.

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


Re: Frameworks for Non-Content Oriented Web Apps

2005-01-01 Thread Tim Churches
[EMAIL PROTECTED] wrote:
Hi,
There are great Python Web Application Framework. But most of them are
meant for content oriented web apps.
Is there something that can ease the development of application that
are not content oriented(I call them NON CONTENT-ORIENTED WEB
APPLICATIONS because I don't know what else to call them). I mean the
applications like, accounting,  high volume data entry apps,  where
normally GUI clients have ruled. I know very high quality ERP and
similar packages have been implemented in a web based environment. But
problem is that they have been developed with the tools that were
actually meant for content oriented apps like Zope, PHP, etc.
Can you give some URL for publicly accessible examples of what you mean 
by a NON CONTENT-ORIENTED WEB APPLICATIONS, so we can get a better 
idea of what you mean?

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


Re: Frameworks for Non-Content Oriented Web Apps

2005-01-01 Thread Paul Rubin
Tim Churches [EMAIL PROTECTED] writes:
 Can you give some URL for publicly accessible examples of what you
 mean by a NON CONTENT-ORIENTED WEB APPLICATIONS, so we can get a
 better idea of what you mean?

I don't think there was anything unclear about it.  A spreadsheet
might be a good example.
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: when wxPython update to python2.4?

2005-01-01 Thread Nick Coghlan
alang_yl wrote:
 i can't wait.
 

wxPython 2.5 already has a version which works with Python 2.4 (grab it from
www.wxpython.org).

For the wxPython 2.4 series, I understand Robin is planning a release which will
both integrate the 2.4 series into the new wxPython versioning scheme, and also
include a Python 2.4 compatible version. The timeframe for that isn't entirely
clear, though.

Cheers,
Nick.

-- 
Nick Coghlan   |   [EMAIL PROTECTED]   |   Brisbane, Australia
---
http://boredomandlaziness.skystorm.net
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Frameworks for Non-Content Oriented Web Apps

2005-01-01 Thread Nick Coghlan
[EMAIL PROTECTED] wrote:
But is there some sort of framework or something that is actually meant
for such web apps,application that make heavy use of forms, have very
high amount of user interaction etc.
Hmm, PJE's PEAK might be worth having a look at: 
http://peak.telecommunity.com/
However, I'm not sure if that will provide enough of the 'web' side of things.
Cheers,
Nick.
--
Nick Coghlan   |   [EMAIL PROTECTED]   |   Brisbane, Australia
---
http://boredomandlaziness.skystorm.net
--
http://mail.python.org/mailman/listinfo/python-list


Re: The Industry choice

2005-01-01 Thread Nick Coghlan
Paul Rubin wrote:
I don't see that big a problem.  The current Python sorting routine
operates on instances of class object and calls the __cmp__ method
to do comparisons.  Every class of sortable objects either defines a
__cmp__ method or inherits one from some superclass, and sort calls
those methods.  Static type declarations would not require writing any
additional sorting routines.
Python's list.sort doesn't check the *type* of the arguments at all. It only 
looks for the relevant comparison methods (__cmp__ or __lt__, as I recall).

Sure, classes written in *Python* will ultimately inherit from either object or 
types.ClassType, but extension classes need not do any such thing.

Yet list.sort works with them all, anyway.
Cheers,
Nick.
--
Nick Coghlan   |   [EMAIL PROTECTED]   |   Brisbane, Australia
---
http://boredomandlaziness.skystorm.net
--
http://mail.python.org/mailman/listinfo/python-list


Re: screen clear question

2005-01-01 Thread Craig Ringer
On Sun, 2005-01-02 at 11:31, jcollins wrote:
 Is there a command in Python to clear the screen?  That is without writing
 multiple blank lines.

Without knowing what 'screen' you're talking about, it's hard to say. If
you mean clearing a terminal, you can call 'tput clear' or
'/usr/bin/clear' on many UNIX systems; no idea about Windows.

--
Craig Ringer

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


Re: PEP 288 ponderings

2005-01-01 Thread Ian Bicking
Steven Bethard wrote:
PEP 288 was mentioned in one of the lambda threads and so I ended up 
reading it for the first time recently.  I definitely don't like the 
idea of a magical __self__ variable that isn't declared anywhere.  It 
also seemed to me like generator attributes don't really solve the 
problem very cleanly.  An example from the PEP[1]:

def mygen():
while True:
print __self__.data
yield None
g = mygen()
g.data = 1
g.next()# prints 1
g.data = 2
g.next()# prints 2
I don't get why this isn't good enough:
def mygen(data):
while True:
print data[0]
yield None
data = [None]
g = mygen(data)
data[0] = 1
g.next()
data[0] = 1
g.next()
Using a one-element list is kind of annoying, because it isn't clear out 
of context that it's just a way of creating shared state.  But it's 
okay, work right now, and provides the exact same functionality.  The 
exception part of PEP 288 still seems interesting.

--
Ian Bicking  /  [EMAIL PROTECTED]  / http://blog.ianbicking.org
--
http://mail.python.org/mailman/listinfo/python-list


Re: The Industry choice

2005-01-01 Thread Donn Cave
Quoth Paul Rubin http://[EMAIL PROTECTED]:
| [EMAIL PROTECTED] writes:
| Overall I agree with you and would like to have OPTIONAL static type
| declarations in Python, as has often been discussed. But without
| facilities for generic programming, such as templates in C++, static
| type declarations can force one to duplicate a LOT of code, with one
| sorting routine each for integer, floats, strings, etc.
|
| I don't see that big a problem.  The current Python sorting routine
| operates on instances of class object and calls the __cmp__ method
| to do comparisons.  Every class of sortable objects either defines a
| __cmp__ method or inherits one from some superclass, and sort calls
| those methods.  Static type declarations would not require writing any
| additional sorting routines.

Yes, it would be really weird if Python went that way, and the
sort of idle speculations we were reading recently from Guido
sure sounded like he knows better.  But it's not like there aren't
some interesting issues farther on downstream there, in the compare
function.  cmp(), and str() and so forth, play a really big role in
Python's dynamically typed polymorphism.  It seems to me they are
kind of at odds with static type analysis, especially if you want
type inference -- kind of a type laundering system, where you can't
tell what was supposed to be there by looking at the code.  Some
alternatives would be needed, I suppose.

Donn Cave, [EMAIL PROTECTED]
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Frameworks for Non-Content Oriented Web Apps

2005-01-01 Thread Ian Bicking
[EMAIL PROTECTED] wrote:
There are great Python Web Application Framework. But most of them are
meant for content oriented web apps.
Is there something that can ease the development of application that
are not content oriented(I call them NON CONTENT-ORIENTED WEB
APPLICATIONS because I don't know what else to call them).
Maybe you mean interactive web applications, as opposed to 
document-centric applications?  But then, that's what most frameworks 
are really designed for.

I mean the
applications like, accounting,  high volume data entry apps,  where
normally GUI clients have ruled. I know very high quality ERP and
similar packages have been implemented in a web based environment. But
problem is that they have been developed with the tools that were
actually meant for content oriented apps like Zope, PHP, etc.
Zope I'd say is content-oriented, though with a bit of imagination you 
can phrase these applications in terms of content.  PHP and many other 
Python frameworks are process-oriented, meaning that each request just 
plain executes some code.  Which is true of Zope too, but the basic 
metaphors in Zope are that a request displays the view of some object, 
which is a little more abstract way of looking at it.

But is there some sort of framework or something that is actually meant
for such web apps,application that make heavy use of forms, have very
high amount of user interaction etc.
Do you mean non-traditional web applications, ala gmail?  Probably not, 
I think you are talking about certain framework concerns that most 
frameworks aspire to in some fashion, but actually achieve to differing 
degrees.  PEAK addresses some of these, but in a UI-neutral way, and 
it's quite experimental (at least in the perspective of a whole 
application; as robust as the individual pieces may be, there's no real 
model for how to use it for a full application).

There's other form processing libraries, but they all are experimental 
in a way.  I developed FormEncode, which relates to some of this.  Zope 
3 has Schemas, which can be used for form generation and validation, and 
Plone has Archetypes.  I don't think there's anything that's a Whole 
Package, but Zope 3 and Plone/Archetypes might be the closest (depending 
on what your vision is).

What I am asking here may sound off beat, but I think, in todays world
where web based solutions offers such a flexibility, we really need it.
I also know that I am to ambiguous, but as is the characteristic of
this wonderful community, talks that start as most abigous, transform
in crystal clear.
PS: I am a web developer, using PHP for living. I have been playing
with python for a while. I found python is really a cool language(do I
need to say that ;-)) with a really, really impressive collection of
modules and frameworks. While developing a school information system, I
felt the need of such a framework that makes developing of Non-Content
Oriented Web-Apps easy.
Eh, it just needs some clear direction for *any* kind of web apps, IMHO.
But with what you are specifically asking for, I think it's just a Hard 
Problem that Is Not Yet Solved, though there is work being done and 
people are attacking it from different directions.

--
Ian Bicking  /  [EMAIL PROTECTED]  / http://blog.ianbicking.org
--
http://mail.python.org/mailman/listinfo/python-list


Re: Any Python XML Data Binding Utilities Avaiable?

2005-01-01 Thread Uche Ogbuji
Sounds like generateDS is closest to what you want:

http://www.rexx.com/~dkuhlman/generateDS.html

If you can bind from instances only and don't need schema, see Amara
Bindery:

http://uche.ogbuji.net/tech/4Suite/amara/

Also consider Gnosis Utilities and ElementTree.

--
Uche OgbujiFourthought, Inc.
http://uche.ogbuji.nethttp://4Suite.orghttp://fourthought.com
Use CSS to display XML -
http://www.ibm.com/developerworks/edu/x-dw-x-xmlcss-i.html
Full XML Indexes with Gnosis -
http://www.xml.com/pub/a/2004/12/08/py-xml.html
Be humble, not imperial (in design) -
http://www.adtmag.com/article.asp?id=10286UBL 1.0 -
http://www-106.ibm.com/developerworks/xml/library/x-think28.html
Use Universal Feed Parser to tame RSS -
http://www.ibm.com/developerworks/xml/library/x-tipufp.html
Default and error handling in XSLT lookup tables -
http://www.ibm.com/developerworks/xml/library/x-tiplook.html
A survey of XML standards -
http://www-106.ibm.com/developerworks/xml/library/x-stand4/
The State of Python-XML in 2004 -
http://www.xml.com/pub/a/2004/10/13/py-xml.html

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


Re: screen clear question

2005-01-01 Thread Daniel Bickett
import os

# windows
os.system(cls)

# bash ( mac, linux )
os.system(clear)

That's all I can account for.

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


Re: Frameworks for Non-Content Oriented Web Apps

2005-01-01 Thread Stephen Thorne
On 1 Jan 2005 20:51:06 -0800, [EMAIL PROTECTED] [EMAIL PROTECTED] wrote:
 But is there some sort of framework or something that is actually meant
 for such web apps,application that make heavy use of forms, have very
 high amount of user interaction etc.
 etc

Yeah, nevow, by those crazy twisted people, is great. The underlying
concept is 'object publishing', and has some very funky technology for
live-updating of a client-side web page via javascript (ala gmail).

Object publishing is along the lines of, instead of having a 'form',
you have an object which represents data. It knows how to handle form
submissions, validate data, etc. And you 'publish' that object. The
user can then interact with that object.

Anyway, I haven't mucked around with nevow for months, you're better
off checking out nevow.com and divmod.org yourself.

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


Re: The Industry choice

2005-01-01 Thread Paul Rubin
Donn Cave [EMAIL PROTECTED] writes:
 Yes, it would be really weird if Python went that way, and the
 sort of idle speculations we were reading recently from Guido
 sure sounded like he knows better.  But it's not like there aren't
 some interesting issues farther on downstream there, in the compare
 function.  cmp(), and str() and so forth, play a really big role in
 Python's dynamically typed polymorphism.  It seems to me they are
 kind of at odds with static type analysis

I don't understand that.  If I see str x = str(3), then I know that
x is a string.
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: PEP 288 ponderings

2005-01-01 Thread Nick Coghlan
Ian Bicking wrote:
Using a one-element list is kind of annoying, because it isn't clear out 
of context that it's just a way of creating shared state.  But it's 
okay, work right now, and provides the exact same functionality.
Uh, isn't shared state what classes were invented for?
Py class mygen(object):
...   def __init__(self, data):
... self.data = data
...   def __iter__(self):
... while 1:
...   print self.data
...   yield None
...
Py g = mygen(0)
Py giter = iter(g)
Py giter.next()
0
Py g.data = 1
Py giter.next()
1
Cheers,
Nick.
--
Nick Coghlan   |   [EMAIL PROTECTED]   |   Brisbane, Australia
---
http://boredomandlaziness.skystorm.net
--
http://mail.python.org/mailman/listinfo/python-list


[ python-Bugs-1092502 ] Memory leak in socket.py on Mac OS X 10.3

2005-01-01 Thread SourceForge.net
Bugs item #1092502, was opened at 2004-12-28 21:09
Message generated for change (Comment added) made by etrepum
You can respond by visiting: 
https://sourceforge.net/tracker/?func=detailatid=105470aid=1092502group_id=5470

Category: Python Library
Group: Platform-specific
Status: Open
Resolution: None
Priority: 5
Submitted By: bacchusrx (bacchusrx)
Assigned to: Nobody/Anonymous (nobody)
Summary: Memory leak in socket.py on Mac OS X 10.3

Initial Comment:
Some part of socket.py leaks memory on Mac OS X 10.3 (both with 
the python 2.3 that ships with the OS and with python 2.4).

I encountered the problem in John Goerzen's offlineimap. 
Transfers of messages over a certain size would cause the program 
to bail with malloc errors, eg

*** malloc: vm_allocate(size=5459968) failed (error code=3)
*** malloc[13730]: error: Can't allocate region

Inspecting the process as it runs shows that python's total memory
size grows wildly during such transfers.

The bug manifests in _fileobject.read() in socket.py. You can 
replicate the problem easily using the attached example with nc -l 
-p 9330  /dev/zero running on some some remote host.

The way _fileobject.read() is written, socket.recv is called with the 
larger of the minimum rbuf size or whatever's left to be read. 
Whatever is received is then appended to a buffer which is joined 
and returned at the end of function.

It looks like each time through the loop, space for recv_size is 
allocated but not freed, so if the loop runs for enough iterations, 
python exhausts the memory available to it.

You can sidestep the condition if recv_size is small (like 
_fileobject.default_bufsize small).

I can't replicate this problem with python 2.3 on FreeBSD 4.9 or  
FreeBSD 5.2, nor on Mac OS X 10.3 if the logic from 
_fileobject.read() is re-written in Perl (for example).

--

Comment By: Bob Ippolito (etrepum)
Date: 2005-01-01 21:25

Message:
Logged In: YES 
user_id=139309

that code paste is missing an int i at the beginning of main..

--

Comment By: Bob Ippolito (etrepum)
Date: 2005-01-01 21:23

Message:
Logged In: YES 
user_id=139309

#include unistd.h

#define NUM_ALLOCATIONS 10
#define ALLOC_SIZE 10485760
#define ALLOC_RESIZE 1492

int main(int argc, char **argv) {
/* exiting will free all this leaked memory */
for (i = 0; i  NUM_ALLOCATIONS; i++) {
void *orig_ptr, *new_ptr;
size_t new_size, orig_size;
orig_ptr = malloc(ALLOC_SIZE);
orig_size = malloc_size(orig_ptr);

if (orig_ptr == NULL) {
printf(failure to malloc %d\n, i);
abort();
}
new_ptr = realloc(orig_ptr, ALLOC_RESIZE);
new_size = malloc_size(new_ptr);
printf(resized %d[%p] - %d[%p]\n,
orig_size, orig_ptr, new_size, new_ptr);
if (new_ptr == NULL) {
printf(failure to realloc %d\n, i);
abort();
}
}
return 0;
}

--

Comment By: Bob Ippolito (etrepum)
Date: 2005-01-01 21:22

Message:
Logged In: YES 
user_id=139309

Ok.  I've tracked it down.  realloc(...) on Darwin doesn't actually resize 
memory unless it *has* to.  For shrinking an allocation, it does not have 
to, therefore realloc(...) with a smaller size is a no-op.

It seems that this may be a misunderstanding by Python.  The man page 
for realloc(...) does not say that it will EVER free memory, EXCEPT in the 
case where it has to allocate a larger region.

I'll attach an example that demonstrates this outside of Python.

--

Comment By: bacchusrx (bacchusrx)
Date: 2005-01-01 18:01

Message:
Logged In: YES 
user_id=646321

I've been able to replicate the problem reliably on both 10.3.5 and 
10.3.7. I've attached two more examples to demonstrate:

Try this: Do, dd if=/dev/zero of=./data bs=1024 count=10240 and save 
server.pl wherever you put data. Have three terminals open. In one, 
run perl server.pl -s0.25. In another, run top -ovsize and in the third 
run python example2.py. 

After about 100 iterations, python's vsize is +1GB (just about the value 
of cumulative_req in example2.py) and if left running will cause a 
malloc error at around 360 iterations with a vsize over 3.6GB (again, just 
about what cumulative_req reports). Mind you, we've only received 
~512kbytes.

server.pl differs from the netcat method in that it (defaults) to sending 
only 1492 bytes at a time (configurable with the -b switch) and sleeps for 
however many seconds specified with the -s switch. This guarantees 
enough iterations to raise the error each time around. When omittting 
the -s switch to server.pl, I don't get the error, but throughput is good 
enough that the loop in readFromSockUntil() only runs a few times.