Re: hash() yields different results for different platforms

2006-07-12 Thread Tim Peters
[Grant Edwards]
>> ...
>> The low 32 bits match, so perhaps you should just use that
>> portion of the returned hash?
>>
>> >>> hex(12416037344)
>> '0x2E40DB1E0L'
>> >>> hex(-468864544 & 0x)
>> '0xE40DB1E0L'
>>
>> >>> hex(12416037344 & 0x)
>> '0xE40DB1E0L'
>> >>> hex(-468864544 & 0x)
>> '0xE40DB1E0L'

[Qiangning Hong]
> Is this relationship (same low 32 bits) guaranteed?

No.  Nothing about hashes is guaranteed, except that when x and y are
of a hashable type, and x == y, then hash(x) == hash(y) too.

> Will it change in the future version?

That's possible, but not planned.  Note that the guts of string
hashing in CPython today is implemented via

while (--len >= 0)
x = (103*x) ^ *p++;

where x is C type "long", and the C language doesn't even define what
that does (behavior when signed multiplication overflows isn't defined
in C).
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Is there a limit to os.popen()?

2006-07-12 Thread Lawrence D'Oliveiro
In article <[EMAIL PROTECTED]>,
 "Carl J. Van Arsdall" <[EMAIL PROTECTED]> wrote:

>Lawrence D'Oliveiro wrote:
>> In article <[EMAIL PROTECTED]>,
>>  "Carl J. Van Arsdall" <[EMAIL PROTECTED]> wrote:
>>
>>   
>>> Well, running the make on the command line seems to work just fine, no 
>>> errors at all.
>>> 
>>
>> What about running it as
>>
>> make kernelrelease | cat
>>
>> This way the output goes to a pipe, which is what happens when it's 
>> called from your script. Do you see those "broken pipe" messages after a 
>> few minutes in this case?
>>   
>Alright, so I tried that line in the interpreter and got the same 
>problem.

No, I wanted you to try it from a bash command prompt, to try to 
simulate the environment when the make command on its own is invoked via 
popen.
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: file.readlines() and IOError exceptions

2006-07-12 Thread Fredrik Lundh
Astan Chee wrote:

> now the file Im trying to read has recently had alot of read/writes from 
> other users/threads/etc, so every now and again I get a
> IOError: [Errno 9] Bad file descriptor

> I know this has something to do with not being able to read while some 
> filehandles are open (or is it?)..

EBADF usually means that you're trying to read from a file that's no 
longer open.  other users shouldn't be able to cause that error; I'd 
suspect sloppy thread programming...



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


Re: Restricted Access

2006-07-12 Thread iapain
> You'll need to make your own AccessControl/ZopeGuards.py-like module, and
> probably subclass the RestrictionMutator  to enable/disable certain
> functionnality (interdiction of names beginning by '_' for example is hard
> coded).

Your reply is pretty hopeful, I saw that one, its the only fractional
part. I'm agree with others that I need to setup a safe runtime
enviornment rather than detecting harmful code.

> This should be possible by providing a wrapper function for file and open (see
> the Guards.py module).

Thats a nice idea, I guess it should work. I should try it really
quick! Thanks!

Best!
iapain

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


Re: Sets and Membership Tests

2006-07-12 Thread Nick Vatamaniuc
JK,

You are correct to implement __hash__ and __eq__. The problem is how
you implemented them. Usually your __eq__ method should compare the
necessary attributes of the objects for equality. The __hash__ should
return a 32-bit integer. Your best bet is probably to return a hash of
hashes of your attributes that are used in equality comparison. What
this means is that your attributes used to produce the __hash__ should
also be hashable. This is important yet not immediatly obvious. So you
could for example return hash( (attribute1, attribute2, attribute3) ),
where attribute1, attribute2, attribute3 are all hashable.
 Of course, you provided no code and no error messages (the
'SoFarNoLuck' exception is not descriptive enough ; )

Hope this helps



JKPeck wrote:
> I would like to be able use sets where the set members are objects of a
> class I wrote.
> I want the members to be distinguished by some of the object content,
> but I have not figured out how a set determines whether two (potential)
> elements are identical.  I tried implementing __eq__ and __ne__ and
> __hash__ to make objects with identical content behave as identical for
> set membership, but so far no luck.
>
> I could subclass set if necessary, but I still don't know what I would
> need to override.
> 
> TIA for any advice you can offer.

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


Re: Is there a limit to os.popen()?

2006-07-12 Thread Steve Holden
Carl J. Van Arsdall wrote:
> I'm not sure the proper way to phrase the question, but let me try.
> 
> Basically, I'm working with a script where someone wrote:
> 
> kr = string.strip(os.popen('make kernelrelease').read())
> 
> 
> And then searches kr to match a regular expression.
> 
> This seems to have been working, however lately when this line executes 
> I get a number of messages to stderr after several minutes of execution:
> 
> cat: write error: Broken pipe
> cat: write error: Broken pipe
> cat: write error: Broken pipe
> 
> 
> I know the output from this make has been growing (make applies some 
> patches and the patch list is growing).  Does os.popen() have some kind 
> of read buffer limit that i'm hitting which is causing things to break?
> 
> 
> 
> 
While I can't tell you where the error is, I *can* tell you that the 
message is caused when some proces that's writing to a pipe sees the 
pipeline's reader close the pipe while the writer is still sending data.

If you are seeing alot of error messages you shoudl really redirect the 
standard error to a file to avoid them cluttering up your console.

regards
  Steve
-- 
Steve Holden   +44 150 684 7255  +1 800 494 3119
Holden Web LLC/Ltd  http://www.holdenweb.com
Skype: holdenweb   http://holdenweb.blogspot.com
Recent Ramblings http://del.icio.us/steve.holden

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


Re: Object Persistence Using a File System

2006-07-12 Thread Bruno Desthuilliers
Chris Spencer wrote:
> Before I get too carried away with something that's probably
> unnecessary, please allow me to throw around some ideas. I've been
> looking for a method of transparent, scalable, and human-readable object
> persistence, and I've tried the standard lib's Shelve, Zope's ZODB,
> Divmod's Axiom, and others. However, while they're all useful, none
> satisfies all my criteria. So I started writing some toy code of my own:
> http://paste.plone.org/5227
> 
> All my code currently does is transparently keep track of object changes
> without requiring any special coding on part of the user, and a function
> to convert an object to a file system hierarchy of folders and files.
> Please, let me know what you think.

As you say, using filesystem for fine-grained persistance may not be the
most efficient solution. I also wonder how (if...) you intend to address
concurrent R/W access and transactions...

A few observations and questions :
- you should avoid tests on concrete types as much as possible - at
least use isinstance
- tuples are immutable containers. What about them ?
- what about multiple references to a same object ?

-- 
bruno desthuilliers
python -c "print '@'.join(['.'.join([w[::-1] for w in p.split('.')]) for
p in '[EMAIL PROTECTED]'.split('@')])"
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: hash() yields different results for different platforms

2006-07-12 Thread Fredrik Lundh
Qiangning Hong wrote:

> /.../ add a "hash" column in the table, make it a unique key

at this point, you should have slapped yourself on the forehead, and gone
back to the drawing board.

 



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


Re: import and global namespace

2006-07-12 Thread Bruno Desthuilliers
Dennis Lee Bieber wrote:
> On Wed, 12 Jul 2006 02:48:44 +0200, Bruno Desthuilliers
> <[EMAIL PROTECTED]> declaimed the following in
> comp.lang.python:
> 
> 
>>nate a écrit :
>>
>>>I'd like a module than I'm importing to be able to use objects in the
>>>global namespace into which it's been imported.  is there a way to do
>>>that?
> 
>
> 
>>NB : FWIW, the clean solution probably involve a class...
> 
> 
>   Or a common module that both (all) import...

I'm afraid this would be as messy. Having one module depending on
implicits external names is a good way to spaghetti code.

-- 
bruno desthuilliers
python -c "print '@'.join(['.'.join([w[::-1] for w in p.split('.')]) for
p in '[EMAIL PROTECTED]'.split('@')])"
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Relying on the behaviour of empty container in conditional statements

2006-07-12 Thread Roel Schroeven
Steven D'Aprano schreef:
> If seq can be None as well as a sequence, doing a test "if len(seq) > 0"
> won't save you because len(None) will fail. You need an explicit test
> for seq being None:
> 
> if seq is not None and len(seq) > 0
> 
> Or even better:
> 
> if seq
> 
> which Just Works regardless of the type of seq.

Yes, true.

I agree that testing in a Boolean context works best in those cases. 
After a good night's sleep I remember why I felt uneasy doing it like 
that: I feel that 'if seq' should be synonym with 'if seq is not None', 
but I can't explain why. No rational reasons I think; it's probably just 
from being used to C and C++ where 'if (p)' in pointer contexts is used 
as synonym for 'if (p != NULL)'.

In general I don't have too many problems using Python idioms instead of 
C or C++ idioms, but apparently sometimes my years of experience in 
these languages sometimes show trough in Python. Luckily my BASIC habits 
have died out long ago.

-- 
If I have been able to see further, it was only because I stood
on the shoulders of giants.  -- Isaac Newton

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


Re: How to use images at the bachground?

2006-07-12 Thread sreekant
pipehappy wrote:
>> hi all,
>> how to get out of the python shell which is executing a command?
>> how to use images in the background of a page in Tkinter?
> 
> on Unix, ctrl-c or ctrl-d may do the job.
> on Windows, ctrl-z will do
> 

Hi I presume you meant putting a image as a background for a Tkinter 
app. If so , try Tkinter.Canvas.

Best of luck
sree
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: help a newbie with a IDE/book combination

2006-07-12 Thread Ron Rogers Jr.
On 9 Jul 2006 16:42:27 -0700
[EMAIL PROTECTED] wrote:

> Hi,
> 
> I already have a couple of newbie books on Python itself, but
> would rather get started with a nice to use IDE and I am
> therefore looking for a good IDE to learn Python. 
> 
> Is there a good IDE which would be well documented out there?


Python comes with IDLE.  It should be in /usr/local/bin

If not try:

python2.4 /usr/local/lib/python2.4/idlelib/idle.py

I like idle as a beginner because it's easy for me to test out
my simple little learning scripts while in it. (though I figure
there's probably a way to do that from within vim or emacs)

As some suggested, as a beginner you don't need much of a super
powered IDE.  If I wasn't using IDLE, I'd be using vim or gvim.


CronoCloud (Ron Rogers Jr.)


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


Re: first book about python

2006-07-12 Thread Ron Rogers Jr.
On Sun, 09 Jul 2006 03:41:52 +0300
IOANNIS MANOLOUDIS <[EMAIL PROTECTED]> wrote:

> I want to learn python.
I am looking for a book which will help
> me get started and should contain the foundations. I am not
> looking for the Python bible. Any recommendations?
> Ioannis
> 

Hmm, no one has mentioned Python Programming for the Absolute
Beginner (second edition) by Michael Dawson.
http://www.amazon.com/gp/product/1598631128/102-5443736-1342538?v=glance&n=283155

Yes, it's very game oriented, but it's also very newbie oriented.

I've slso downloaded some Python tutorials:

I've got the Python version of How to Think Like a Computer
Scientist:

http://www.ibiblio.org/obp/thinkCSpy/

And Dive into Python (available online as well as paper):

http://diveintopython.org/

Hope this helps.

CronoCloud (Ron Rogers Jr.)




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


Re: Augument assignment versus regular assignment

2006-07-12 Thread Antoon Pardon
On 2006-07-11, Piet van Oostrum <[EMAIL PROTECTED]> wrote:
>> Antoon Pardon <[EMAIL PROTECTED]> (AP) wrote:
>
>>AP> As I read the language reference the x stands for a target expression.
>>AP> Now what does it mean to evaluate a target expression like col[key].
>>AP> IMO it means finding the location of the item in the collection: the
>>AP> bucket in the directory, the node in the tree ... grosso mode it
>>AP> boils down to the call to __setitem__ or __getitem__ depending
>>AP> on where the col[key] was located in the line (or if you prefer
>>AP> the view from the interpreter it boils down to the BINARY_SUBSCR
>>AP> and STORE_SUBSCR opcodes).
>
>>AP> So if the language reference seems to implies that col[key] will
>>AP> only be evaluated once in a line like: "col[key] += 1" I expect
>>AP> only one call from __setitem__ or __getitem__ (or only one
>>AP> from BINARY_SUBSCR or STORE_SUBSCR)
>
> You need both the __setitem__ and the __getitem__ otherwise it won't work.
> I think the "evaluated once" clause is for cases like:
>
> f(a)[g(b)] += 1
>
> where f and/or g have side effects. These side effects should then take
> place only once. Side effects in __setitem__ and __getitem__ is not what it
> is talking about.

Well I'll start on an possitive note and accept this. Now I'd like you
to answer some questions.

1) Do you think the langauge reference makes it clear that this is how
   the reader has to understand things.

2a) In case you answer yes to question (1). Can you explain me how
I have to read the language reference in order to deduce this
is indeed the way things should be understood.

2b) In case you anser no to question (1). Do you find it unreasonable
that I ask that the language reference would be rewritten here so
that your explanation can be (more easily) deduced from it.

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


Re: Object Persistence Using a File System

2006-07-12 Thread Nick Vatamaniuc
Chris,

Interesting concept. But why is there a need for a human readable
object persistence that is x10 slower than pickle? In other words
present a good use case with a rationale (i.e. your "criteria" that you
mentioned). The only one I can think of so far is debugging.

Also some objects are inherently not human readable (they are large, or
just binary/array data for example), or you could simply end up having
so many of them (10GB worth of disk space) that just due to the volume
they will become not very readable and you would need some kind of a
custom query mechanism (or find+grep) to search through your objects if
you wanted to read/edit values in them.

In your code comments I saw that another reason is resistance to
corruption. I think that a database that is backed up regularly is
probably a better solution. Besides, sometimes you want your failure to
be dramatic (go down with a bang!) so it gets your attention and you
can restore everything with a backup. Otherwise, with a tens of
thousands of files, some of them might end up being corrupted before
your next filesystem check gets to them, by then, the corruption could
spread (your program would load it, perform  computations, persist the
wrong results and so on),  and you wouldn't even notice it.

Hope these comments help,
Nick V.



Chris Spencer wrote:
> Before I get too carried away with something that's probably
> unnecessary, please allow me to throw around some ideas. I've been
> looking for a method of transparent, scalable, and human-readable object
> persistence, and I've tried the standard lib's Shelve, Zope's ZODB,
> Divmod's Axiom, and others. However, while they're all useful, none
> satisfies all my criteria. So I started writing some toy code of my own:
> http://paste.plone.org/5227
>
> All my code currently does is transparently keep track of object changes
> without requiring any special coding on part of the user, and a function
> to convert an object to a file system hierarchy of folders and files.
> Please, let me know what you think.
> 
> Thanks,
> Chris

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


Re: Accessors in Python (getters and setters)

2006-07-12 Thread mystilleef
Lousy Attribute Name:
self.tmp

Accessors:
set_temporary_buffer
get_temporary_buffer

The attribute name I chose, "tmp" sucks. I have used that name in
dozens of places spanning over 27000 LOC. There's a chance that other
develops might misinterpret exactly what "tmp" does. Plus I don't want
emails from other developers querying me about what "tmp" is/does.
"tmp" is obvious to me, but not necessarily to others. Now compare that
to the accessors. Not only do they improve readability at the expense
of more code, they actually allow me to change the lousily named
attribute "tmp" to "temporary_buffer" without grepping, seding,
searching, replacing and praying. Sure, if you are dealing with less
than a 1000LOC you can get away with using "tmp" or renaming it easily.
But if you are dealing with a much larger code base and more
developers, issues like this rapidly become a nuisance.

Yes, it is possible to name crappy accessors too (e.g set_tmp/get_tmp).
But developers tend to pay more attention to given methods/functions
less crappy names, at least when compared to data attributes. This
stems from the fact that in many languages data attributes aren't
usually part of the API, as well as the whole OO(Encapsulation) blah
blah. I know I would not name the accessors set_tmp/get_tmp, because my
philosophy is that methods/functions need to have meaningful names and
state their intended purpose. I don't hold data attributes to such
standards and I imagine many developers don't either and least based on
other people's code I've read. Plus there are many occassions when
attributes are not intended to be APIs, but eventually become one.
After all most data attributes are created with the purpose of serving
methods.

Bruno Desthuilliers wrote:
> mystilleef wrote:
> > Hello,
> >
> > Thanks for the responses. The reason I want to change the name of the
> > attribute is because it doesn't reflect the purpose of the attribute,
> > anymore. The attribute was originally a string object, but not anymore.
> > It is primarily a readability issue. There are also a few key
> > attributes I don't want developers, including myself, fiddling with.
> > Properties /accessors are good because they allow you to encapsulate
> > attributes so you can change implementations at will. Some of you have
> > argued I would have needed to change accessor names too if I had
> > misnamed them earlier. It's hard to say. I find myself changing the
> > names of attributes a lot more frequently than I do functions or
> > methods. Choosing a crappy attribute name is effortless for me,
> > especially during intense coding sessions. I usually realize I've
> > choosing a crappy attribute name the next day, sometimes even later.
> > However, I put a lot more effort into choosing method and function
> > names, especially when I know it may likely be a public API.
>
> What you need to understand here is that in Python,
> 1/ methods *are* attributes
> 2/ every attribute whose name is not prefixed by a leading underscore is
> considered part of the api ('__magic__' names being a special case).
>
> So it has nothing to do with "data vs method" dichotomy (which makes no
> sens in a languages where functions and methods are objects), only with
> "API vs implementation". You choosed a crappy name for an attribute
> that's part of the API, so it's *exactly* the same case as if you had
> chosen a crappy name for a public method in Java. Think of public "data"
> attributes as magical getter/setters with the most straightforward
> behaviour, and of properties as the way to override this default behaviour.
>
> > Plus it's
> > really hard to choose crappy accessor name.
>
> What about getMyCrappyAttributeName/setMyCrappyAttributeName ?-)
>
>
>
> --
> bruno desthuilliers
> python -c "print '@'.join(['.'.join([w[::-1] for w in p.split('.')]) for
> p in '[EMAIL PROTECTED]'.split('@')])"

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


Re: hash() yields different results for different platforms

2006-07-12 Thread Nick Vatamaniuc
Using Python's hash as column in the table might not be a good idea.
You just found out why. So you could instead just use the base url and
create an index based on that so next time just quickly get all urls
from same base address then do a linear search for a specific one, or
even easier, implement your own hashes without using any of the
Python's built-in hash() functions. For example, transform each
character to an int and multply them all mod 2^32-1 or something like
that. Even better I think someone already posted the Python's way of
generating hashes for string, well, just re-implement it in Python such
that your version will yield the same hash on  any platform.

Hope  this helps,
Nick V.

Qiangning Hong wrote:
> I'm writing a spider. I have millions of urls in a table (mysql) to
> check if a url has already been fetched. To check fast, I am
> considering to add a "hash" column in the table, make it a unique key,
> and use the following sql statement:
>   insert ignore into urls (url, hash) values (newurl, hash_of_newurl)
> to add new url.
>
> I believe this will be faster than making the "url" column unique key
> and doing string comparation.  Right?
>
> However, when I come to Python's builtin hash() function, I found it
> produces different values in my two computers!  In a pentium4,
> hash('a') -> -468864544; in a amd64, hash('a') -> 12416037344.  Does
> hash function depend on machine's word length?
>
> If it does, I must consider another hash algorithm because the spider
> will run concurrently in several computers, some are 32-bit, some are
> 64-bit.  Is md5 a good choice? Will it be too slow that I have no
> performance gain than using the "url" column directly as the unique
> key?
>
> I will do some benchmarking to find it out. But while making my hands
> dirty, I would like to hear some advice from experts here. :)

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


Re: Object Persistence Using a File System

2006-07-12 Thread Nick Vatamaniuc
Good point about isinstance. Here is a good explanation why:
http://www.canonical.org/~kragen/isinstance/
Also the frozenset should be added the list of immutable types.

Nick Vatamaniuc


Bruno Desthuilliers wrote:
> Chris Spencer wrote:
> > Before I get too carried away with something that's probably
> > unnecessary, please allow me to throw around some ideas. I've been
> > looking for a method of transparent, scalable, and human-readable object
> > persistence, and I've tried the standard lib's Shelve, Zope's ZODB,
> > Divmod's Axiom, and others. However, while they're all useful, none
> > satisfies all my criteria. So I started writing some toy code of my own:
> > http://paste.plone.org/5227
> >
> > All my code currently does is transparently keep track of object changes
> > without requiring any special coding on part of the user, and a function
> > to convert an object to a file system hierarchy of folders and files.
> > Please, let me know what you think.
>
> As you say, using filesystem for fine-grained persistance may not be the
> most efficient solution. I also wonder how (if...) you intend to address
> concurrent R/W access and transactions...
>
> A few observations and questions :
> - you should avoid tests on concrete types as much as possible - at
> least use isinstance
> - tuples are immutable containers. What about them ?
> - what about multiple references to a same object ?
>
> --
> bruno desthuilliers
> python -c "print '@'.join(['.'.join([w[::-1] for w in p.split('.')]) for
> p in '[EMAIL PROTECTED]'.split('@')])"

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


Re: What is a type error?

2006-07-12 Thread Andreas Rossberg
David Hopwood wrote:
> George Neuner wrote:
>>
>>All of this presupposes that you have a high level of confidence in
>>the compiler.  I've been in software development for going in 20 years
>>now and worked 10 years on high performance, high availability
>>systems.  In all that time I have yet to meet a compiler ... or
>>significant program of any kind ... that is without bugs, noticeable
>>or not.
> 
> [...]
> 
> One of the main reasons for this, IMHO, is that many compilers place too
> much emphasis on low-level optimizations of dubious merit. For C and
> Java, I've taken to compiling all non-performance-critical code without
> optimizations, and that has significantly reduced the number of compiler
> bugs that I see. It has very little effect on overall execution performance
> (and compile times are quicker).
> 
> I don't think that over-complex type systems are the cause of more than a
> small part of the compiler bug problem. In my estimation, the frequency of
> bugs in different compilers *for the same language* can vary by an order of
> magnitude.

Also, the use of typed intermediate languages within the compiler might 
actually help drastically cutting down on the more severe problem of 
code transformation bugs, notwithstanding the relative complexity of 
suitable internal type systems.

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


Re: Accessors in Python (getters and setters)

2006-07-12 Thread Maric Michaud
Le mercredi 12 juillet 2006 11:17, mystilleef a écrit :
> Yes, it is possible to name crappy accessors too (e.g set_tmp/get_tmp).
> But developers tend to pay more attention to given methods/functions
> less crappy names, at least when compared to data attributes.
Not python developers.

> This 
> stems from the fact that in many languages data attributes aren't
> usually part of the API, as well as the whole OO(Encapsulation) blah
> blah. I know I would not name the accessors set_tmp/get_tmp, because my
> philosophy is that methods/functions need to have meaningful names and
> state their intended purpose.
But that's not python philosophy.

> I don't hold data attributes to such 
> standards and I imagine many developers don't either and least based on
> other people's code I've read. Plus there are many occassions when 
> attributes are not intended to be APIs, but eventually become one.
But they are in Python and that is the python's philosophy. All attribute or 
method not beginning with an '_' *is* API.

> After all most data attributes are created with the purpose of serving
> methods.
And in python the reverse can be true :

class a(object) :
def __init__(self, ro_attr) : self.__attr = ro_attr
def _getAttr(self) :
"""A method which serves an attribute"""
return self.__attr
attr = property(_getAttr)




-- 
_

Maric Michaud
_

Aristote - www.aristote.info
3 place des tapis
69004 Lyon
Tel: +33 426 880 097
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: array of array of float

2006-07-12 Thread Fredrik Lundh
"Schüle Daniel" wrote:

> > a = [[] for in range(200)]
>
> correction :)
>
> a = [[] for i in range(200)]

the "*500" part still seems to be missing...

 



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

Re: hash() yields different results for different platforms

2006-07-12 Thread Piet van Oostrum
> Grant Edwards <[EMAIL PROTECTED]> (GE) wrote:

>GE> The low 32 bits match, so perhaps you should just use that
>GE> portion of the returned hash?

If the hashed should be unique, 32 bits is much too low if you have
millions of entries.
-- 
Piet van Oostrum <[EMAIL PROTECTED]>
URL: http://www.cs.uu.nl/~piet [PGP 8DAE142BE17999C4]
Private email: [EMAIL PROTECTED]
-- 
http://mail.python.org/mailman/listinfo/python-list


RE: hash() yields different results for different platforms

2006-07-12 Thread Kerry, Richard
 
The hash is not expected to be unique, it just provides a starting point
for another search (usually linear ?).  

See http://en.wikipedia.org/wiki/Hash_function


Helpfully,
Maybe,
Richard.




-Original Message-
From: [EMAIL PROTECTED]
[mailto:[EMAIL PROTECTED] On
Behalf Of Piet van Oostrum
Sent: 12 July 2006 10:56
To: [email protected]
Subject: Re: hash() yields different results for different platforms

> Grant Edwards <[EMAIL PROTECTED]> (GE) wrote:

>GE> The low 32 bits match, so perhaps you should just use that
>GE> portion of the returned hash?

If the hashed should be unique, 32 bits is much too low if you have
millions of entries.
-- 
Piet van Oostrum <[EMAIL PROTECTED]>
URL: http://www.cs.uu.nl/~piet [PGP 8DAE142BE17999C4]
Private email: [EMAIL PROTECTED]
-- 
http://mail.python.org/mailman/listinfo/python-list
-- 
http://mail.python.org/mailman/listinfo/python-list


Progress Bars in python

2006-07-12 Thread Hari Sekhon
Hi,
   I've written a script which backs up a huge bunch of files, but I 
don't want the script to output the file names as it does this as it 
clutters the screen, I only output errors.

So in order to see that the script is working and not stuck, I'd like to 
implement some kind of progress bar or something, perhaps like the 
spinning thing that you often see in linux or freebsd consisting of 
switching / - \ | on the spot to get the appearance of a spinning 
bar I can figure out that the spinning bar is done by switching 
these four chars but I don't know how they get each char to replace the 
last one instead of printing them in succession.

Does anybody have any good suggestions about what the best way of doing 
this or any other suggestions for the best ways to show that the script 
is processing...?


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


Re: hash() yields different results for different platforms

2006-07-12 Thread Paul Rubin
"Kerry, Richard" <[EMAIL PROTECTED]> writes:
> The hash is not expected to be unique, it just provides a starting point
> for another search (usually linear ?).  

The database is good at organizing indexes and searching in them.  Why
not let the database do what it's good at.
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Progress Bars in python

2006-07-12 Thread Fredrik Lundh
Hari Sekhon wrote:

>   I've written a script which backs up a huge bunch of files, but I
> don't want the script to output the file names as it does this as it
> clutters the screen, I only output errors.
>
> So in order to see that the script is working and not stuck, I'd like to
> implement some kind of progress bar or something, perhaps like the
> spinning thing that you often see in linux or freebsd consisting of
> switching / - \ | on the spot to get the appearance of a spinning
> bar I can figure out that the spinning bar is done by switching
> these four chars but I don't know how they get each char to replace the
> last one instead of printing them in succession.

use backspace ("\b") or carriage return ("\r") to move the cursor backwards.

minimal example:

import time, sys

for i in range(100+1):
sys.stdout.write("\r%c %d%% done..." % ("|/-\\"[i%4], i))
sys.stdout.flush()
time.sleep(0.2)

maximal example (also see the comments):

http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/473899

 



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


Re: timeit module for comparing the performance of two scripts

2006-07-12 Thread Fredrik Lundh
"3c273" wrote:

> Doh! Me thinks Windows at work "python /?" (No good!)

that was supposed to be fixed in 2.5, but it doesn't seem to have made it into
beta 2.  hmm.

 



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


Re: I need some help

2006-07-12 Thread Jonathan Harris
Tim Heaney wrote:
> Several different people have written modules to help you read (and
> write) ID3 tags.

On a related topic, I have a Perl module that reads MP4/AAC tags -
http://search.cpan.org/~jhar/MP4-Info/ - that I'm considering porting to 
Python.

But before I start, is anyone aware of an existing Python module to read 
MP4/AAC tags?

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


Re: How to terminate a main script?

2006-07-12 Thread Tal Einat
Helmut Jarausch wrote:
> Hi,
>
> I'm still looking for an elegant and clear means to
> terminate the main script in Python.
>
> Unfortunately, Python doesn't allow a 'return'
> instruction in the main script.
>
It is quite a common practice for Python scripts to define a main()
function which contains the actual code, and have "main()" on the last
line of the file. This allows you to just 'return' from wherever you
like in your code.

It is even more common to use this in the end instead of "main()":
if __name__ == "__main__":
main()

This way, if the file is imported as a module, the main() function
won't execute, but if it is run directly from a command line or using
execfile() it will execute.

Enjoy!

- Tal

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


Strange behaviour of Numeric Float32 array?

2006-07-12 Thread Rolf Wester
Hi,

the code:

from Numeric import *

def my_minimum(a):
n=shape(a)[0]
x = 1.0e20
for i in range(n):
if a[i] < x:
x = a[i]
return x



def strange(a):
a[3] = -6303.0
h = my_minimum(a)
for i in range(10):
print i,a[i],
a[i] = a[i] - h
print a[i],h

a = zeros(10,Float32)
strange(a)
b = zeros(10,Float64)
strange(b)

produces the output:

0 0.0 6303.0 -6303.0
1 0.0 6303.0 -6303.0
2 0.0 6303.0 -6303.0
3 -6303.0 0.0 0.0
4 0.0 0.0 0.0
5 0.0 0.0 0.0
6 0.0 0.0 0.0
7 0.0 0.0 0.0
8 0.0 0.0 0.0
9 0.0 0.0 0.0
0 0.0 6303.0 -6303.0
1 0.0 6303.0 -6303.0
2 0.0 6303.0 -6303.0
3 -6303.0 0.0 -6303.0
4 0.0 6303.0 -6303.0
5 0.0 6303.0 -6303.0
6 0.0 6303.0 -6303.0
7 0.0 6303.0 -6303.0
8 0.0 6303.0 -6303.0
9 0.0 6303.0 -6303.0

Can anybody tell me why in the Float32 version h is changed?
Thank you in advance.

Regards

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


Re: Accessors in Python (getters and setters)

2006-07-12 Thread Bruno Desthuilliers
mystilleef wrote:
> Lousy Attribute Name:
>   self.tmp
> 
> Accessors:
>   set_temporary_buffer
>   get_temporary_buffer
> 
> The attribute name I chose, "tmp" sucks.

Well, it's surely not as descriptive as 'temporary_buffer'

> I have used that name in
> dozens of places spanning over 27000 LOC. 

Too bad for you.

> There's a chance that other
> develops might misinterpret exactly what "tmp" does. Plus I don't want
> emails from other developers querying me about what "tmp" is/does.
> "tmp" is obvious to me, but not necessarily to others.

So why did you name it that way at first ?

> Now compare that
> to the accessors. 

But 'tmp' actually *is* an accessor.

> Not only do they improve readability

Err... do you find:

obj.set_temporary_buffer(val)
val = obj.get_temporary_buffer()

really more readable than:

obj.temporary_buffer = val
val = obj.temporary_buffer


> at the expense
> of more code,

Indeed. In both the class and client code.

> they actually allow me to change the lousily named
> attribute "tmp" to "temporary_buffer" without grepping, seding,
> searching, replacing and praying.

You still fail to get the point. You actually choose a crappy name for a
*public* property. It's *exactly* as if, in Java, you had named your
getter/setter 'get_tmp' and 'set_tmp'.

> Sure, if you are dealing with less
> than a 1000LOC you can get away with using "tmp" or renaming it easily.
> But if you are dealing with a much larger code base and more
> developers, issues like this rapidly become a nuisance.

Indeed. But it's *your* responsability to choose good names for the API.

> Yes, it is possible to name crappy accessors too (e.g set_tmp/get_tmp).
or 'tmp'.

> But developers tend to pay more attention to given methods/functions
> less crappy names, at least when compared to data attributes.

s/developpers/you/

> This
> stems from the fact that in many languages data attributes aren't
> usually part of the API, 

Once again, in Python, there is *no* such thing as 'data attributes'.
*All* attributes are *objects* - some of them callable.

> as well as the whole OO(Encapsulation) blah
> blah.

Don't confuse encapsulation with data-hiding.

> I know I would not name the accessors set_tmp/get_tmp, because my
> philosophy is that methods/functions need to have meaningful names and
> state their intended purpose.

That's true for each and every name in a program.

> I don't hold data attributes to such
> standards 

Too bad for you.

> and I imagine many developers don't either and least based on
> other people's code I've read. Plus there are many occassions when
> attributes are not intended to be APIs,

Then mark them as being implementation (ie : prefix them with a single
underscore).

> but eventually become one.
> After all most data attributes are created with the purpose of serving
> methods.

Nope. You have the class API, and the class implementation. Both made of
both callable and non-callable attributes.

Mystilleef, I do share your pain (really - been here, done that,
etc...), and I understand that grasping Python requires some mental
adjustments when coming from Java and friends (been here, done that,
etc...). But you seriously can't blame the language for your own mistakes.

If you intented 'tmp' to be part of the API, then you're responsible for
the bad naming. If you didn't, then you're responsible for breaking the
encapsulation - FWIW, following the convention (single leading
underscore) could have make it clearer to you. In both cases, you
happily used a bad name in 27 KLOC - so you really had a lot of time and
occasions to notice something wrong with this.

-- 
bruno desthuilliers
python -c "print '@'.join(['.'.join([w[::-1] for w in p.split('.')]) for
p in '[EMAIL PROTECTED]'.split('@')])"
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Generating all ordered substrings of a string

2006-07-12 Thread Tal Einat

[EMAIL PROTECTED] wrote:
> Hi,
>  I want to generate all non-empty substrings of a string of length >=2.
> Also,
> each substring is to be paired with 'string - substring' part and vice
> versa.
>  Thus, ['abc'] gives me [['a', 'bc'], ['bc', 'a'], ['ab', 'c'], ['c',
> 'ab'], ['b', 'ac'], ['ac', 'b']] etc.
>  Similarly, 'abcd' should give me [['a', 'bcd'], ['bcd', 'a'], ['abc',
> 'd'], ['d', 'abc'], ['b', 'acd'], ['acd', 'b'],['c', 'abd'], ['abd', 'c'],
> ['ab', 'cd'], ['cd', 'ab'], ['bc', 'ad'], ['ad', 'bc'], ['ac',
> 'bd'],['bd','ac']]
>

In your last example you have ['ac','bd'], but neither 'ac' nor 'bd' is
a _substring_ of 'abcd'.

If you want to compute all possible (non-empty) sub-groups of a group
(a group of characters, in your case), that's really quite a common
algorthmic problem and you should be able to Google for a solution.

Once you have all possible subgroups, just make your (weird) pairs,
remove doubles (either by using a set or by sorting and removing
identical neighboring objects), and you're done.

If you're looking for a more efficient solution, specialized for your
specific problem, you'll have to explain more precisely what you're
trying to do, as well as why existing solutions aren't good enough.

- Tal

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


Last used directory?

2006-07-12 Thread Michael Yanowitz
Hello:

   Is there a global or some trick I can use to have
Python remember the last directory visited?
   What I mean is suppose I have this function:

def get_filename():
""" Returns a filename selected from a Tkinter File Selection Dialog """
strFilename = tkFileDialog.askopenfilename(initialdir='.',
   filetypes=[('Python
files','*.py'),
  ('All
Files','*.*')])
return strFilename

but instead of having initialdir='.' (current directory), I
would like it set to the last visited directory, which can be from a
previous run or even a previous day. Is that possible? If so how?


Thanks in advance:
Michael Yanowitz

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


Re: threading troubles

2006-07-12 Thread Antoon Pardon
On 2006-07-10, sreekant <[EMAIL PROTECTED]> wrote:
> Hi folks
>
> What am I doing wrong in the following? I just want to run fluidsynth in 
> the background.
> #
> class MyThread(threading.Thread):
>  def __init__(self, cmd, callback):
>  self.__cmd = cmd
>  self.__callback = callback
>  threading.Thread.__init__(self)
>
>  def run(self):
>  os.system(self.__cmd)
>  self.__callback('abcd')
>  return
>
>
> cmd=midiplay+' '+fmidi
> xc=MyThread(cmd,addlog)
> xc.start()
>
>
> ##
> midiplay is 'fluidsynth -ni /home/mysndfont.sf2 mymidi.mid'
> addlog is a function which prints the log.
>
> If I run it only with xc.start() it does not run the program as in 
> os.system. However if I  put
> xc.start()
> xc.run()
>
> then it starts and runs it in foreground with my pygtk ui non responsive.
>
> What am I missing!

You may be missing nothing. If I recall correctly a similar problem was
once reported on the pygtk-list. Some investigation showed that some
programs couldn't be reliably run from a thread, using os.system.

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


Re: Last used directory?

2006-07-12 Thread Steven D'Aprano
On Wed, 12 Jul 2006 07:29:18 -0400, Michael Yanowitz wrote:

> but instead of having initialdir='.' (current directory), I
> would like it set to the last visited directory, which can be from a
> previous run or even a previous day. Is that possible? If so how?

Every time you open a file, save the directory in a global variable. Then
instead of passing '.' as initialdir, pass that saved directory.

That will work while you're in the same session. To remember the last
visited directory from one session to the next, you'll need to write it to
some sort of permanent storage like a configuration file.

You'll also need to check what happens if the saved directory no longer
exists.


-- 
Steven.

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


Delivery failure notification

2006-07-12 Thread SMTP_Mail_Security
Your message with Subject: Important 
could not be delivered to the following recipients:

[EMAIL PROTECTED]

Please do not resend your original message.

Delivery attempts will continue to be made for 4  day(s).

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


Problems with Unicode Plane 1 characters on Windows

2006-07-12 Thread Edin Salković
Hi all,

Why doesn't the following code work on Windows XP, although it works
on Linux (Ubuntu 6.06). Both versions are of Python are 2.4, and both
OSs are on the same PC.

>>> import unicodedata
>>> unicodedata.name(U'\U0001d400')
Traceback (most recent call last):
 File "", line 1, in ?
TypeError: need a single Unicode character as parameter

The output should say:
MATHEMATICAL BOLD CAPITAL A

Thanks,
Edin
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Progress Bars in python

2006-07-12 Thread skip

Hari> So in order to see that the script is working and not stuck, I'd
Hari> like to implement some kind of progress bar or something, ...

Here's mine:

http://orca.mojam.com/~skip/python/progress.py

There are both Progress and Counter classes.  Same idea, different output.

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


wxHtmlHelpController

2006-07-12 Thread Bryan
is it possible to get the list of search results from the search box from 
wxPython's wxHtmlHelpControl without it displaying GUI?  i don't see an obvious 
way to do this.

thanks,

bryan

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


Re: wxHtmlHelpController

2006-07-12 Thread Bryan
Bryan wrote:
> is it possible to get the list of search results from the search box from 
> wxPython's wxHtmlHelpControl without it displaying GUI?  i don't see an 
> obvious 
> way to do this.
> 
> thanks,
> 
> bryan
> 

i meant wxPython's wxHtmlHelpController

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


Compiling Python using the Portland Group compiler

2006-07-12 Thread Konrad Hinsen
I am trying to install Python 2.4.3 on an AMD Opteron system using  
the Portland Group's compiler (pgcc). Using

CC="pgcc -DNCURSES_ENABLE_STDBOOL_H=0" OPT="-O0" LINKFORSHARED="-Wl,- 
export-dynamic" ./configure --without-cxx

I finally managed to obtain an executable that would start and work,  
but it fails a couple of test cases:

1) test_coercion reports wrong results for operations with complex  
numbers. For example,
2**(2.+0j) yields (1+0j).

2) test_compare reports some wrong results, such as

(2+0j) != (2+0j)

However, typing

(2+0j) == (2+0j)

into the interpreter yields "True". Perhaps the bug is in the  
execution of the test suite.

3) test_compile reports wrong results as well:

test test_compile failed -- Traceback (most recent call last):
   File "/work/experiences/biophys/hinsen/install/Python-2.4.3/Lib/ 
test/test_compile.py", line 164, in test_literals_with_leading_zeroes
 self.assertEqual(eval("0777j"), 777j)
AssertionError: 777j != 777j

However,
eval("0777j") == 777j
yields "True".

4) test_cpickle crashes with a segmentation fault.


Has anyone encountered such failures before? Does anyone have useful  
suggestions for analyzing them?

Konrad.
--
-
Konrad Hinsen
Centre de Biophysique Moléculaire, CNRS Orléans
Synchrotron Soleil - Division Expériences
Saint Aubin - BP 48
91192 Gif sur Yvette Cedex, France
Tel. +33-1 69 35 97 15
E-Mail: hinsen ät cnrs-orleans.fr
-


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


How to display name of elements in list?

2006-07-12 Thread cz
Hi there,

I'm sure there is a very simple solution for my question, I just didn't
find it up to now.
I'm using a badly documented module and therefore need to find out
about how to access the elements in a list.
(I need to do this in  Python 1.5.3)
Any help appreciated very much. Thanks!

cz

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


Re: How to display name of elements in list?

2006-07-12 Thread Ant
> I'm using a badly documented module and therefore need to find out
> about how to access the elements in a list.
> (I need to do this in  Python 1.5.3)

I presume this is the same in 1.5 use dir():

>>> import os
>>> dir(os)
['F_OK', 'O_APPEND', 'O_BINARY', 'O_CREAT', 'O_EXCL', 'O_NOINHERIT',
'O_RANDOM',
 'O_RDONLY', 'O_RDWR', 'O_SEQUENTIAL', 'O_SHORT_LIVED', 'O_TEMPORARY',
'O_TEXT',

(etc)

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


Re: Accessors in Python (getters and setters)

2006-07-12 Thread Ant

> Yes, it is possible to name crappy accessors too (e.g set_tmp/get_tmp).
> But developers tend to pay more attention to given methods/functions
> less crappy names, at least when compared to data attributes. This

In my experience of getters and setters in Java, most developers choose
attribute names first, and then use the IDE (Java without an IDE
*really* sucks) to auto-generate the getters and setters. So most Java
developers I have worked with pay more attention to attributes than
accessor names as these are derived anyway. So I guess it depends on
who the developers are ;-)

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


Re: How to display name of elements in list?

2006-07-12 Thread Steven D'Aprano
On Wed, 12 Jul 2006 05:17:30 -0700, cz wrote:

> Hi there,
> 
> I'm sure there is a very simple solution for my question, I just didn't
> find it up to now.
> I'm using a badly documented module and therefore need to find out
> about how to access the elements in a list.

Er, the same way you would access the elements in any other list?

mylist[0]
mylist[1:5]

etc.


Perhaps you need to rephrase your question.


-- 
Steven.

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


Re: first book about python

2006-07-12 Thread Steve
I recommend "The Quick Python Book" by Harms and McDonald.  Its
strength is its brevity and *readability* -- you can actually just sit
down and read it and enjoy it.  It doesn't cover the newest features of
Python or the most advanced, but that is not necessary in a beginner's
book.

Once you're up to speed on Python, as a sysadmin you'll probably find
"Python Network Programming" useful.  It is not a beginner's book, but
it is addressed to the kind of issues that you'll probably want to be
using Python for.

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


pygtk crashing when using gtkmozembed

2006-07-12 Thread moumita
hi all,
I am trying to execute one simple program using pygtk-2.8.6 and
gnome-python-extra-2.12.It's crashing at the time of executing the
follwing instruction.
-
 self.moz = gtkmozembed.MozEmbed()
---
python is able to import the gtkmozembed module.
plz help.
Thanks,
Moumita

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


Re: How to display name of elements in list?

2006-07-12 Thread cz

> Perhaps you need to rephrase your question.
> --
> Steven.

Thanks for your reply.
OK, I'll try to make this more clear:
My list called "elten" looks like that:

[Tensor: id = 1,  intensity = 2976.52
 xx = -1447.32, xy = 52.458, xz = -594.186
 yy = -1090.54, yz = -0.0158068, zz = -4043.
, Tensor: id = 26,  intensity = 2896.9
 ...
, Tensor: id = 5,  intensity = 2920.5
 xx = -1534.53, xy = 23.4858, xz = -623.967
 yy = -1070.47, yz = 99.6301, zz = -3979.87
]

Now with
>>print elten[1].id
I will get "1" as an answer.
Or with
>>print elten[1].intensity
it will print "2976.52".
But this doesn't work for >>print elten[1].xx, why? So that's how I
came to the question how to access these values. Any idea?
Thanks a lot!
cz

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


Re: How to display name of elements in list?

2006-07-12 Thread Stefan Behnel
Hi Claudio,

cz wrote:
>> Perhaps you need to rephrase your question.
>> --
>> Steven.
> 
> Thanks for your reply.
> OK, I'll try to make this more clear:
> My list called "elten" looks like that:
> 
> [Tensor: id = 1,  intensity = 2976.52
>  xx = -1447.32, xy = 52.458, xz = -594.186
>  yy = -1090.54, yz = -0.0158068, zz = -4043.
> , Tensor: id = 26,  intensity = 2896.9
>  ...
> , Tensor: id = 5,  intensity = 2920.5
>  xx = -1534.53, xy = 23.4858, xz = -623.967
>  yy = -1070.47, yz = 99.6301, zz = -3979.87
> ]
> 
> Now with
>>> print elten[1].id
> I will get "1" as an answer.
> Or with
>>> print elten[1].intensity
> it will print "2976.52".
> But this doesn't work for >>print elten[1].xx, why? So that's how I
> came to the question how to access these values. Any idea?

The list above is not a valid Python list. What is it that you store in that 
list?

Or is it maybe a dictionary?

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


Re: Multi-threaded FTP Question

2006-07-12 Thread Jeremy Jones

Dennis Lee Bieber wrote:
> On 11 Jul 2006 06:45:42 -0700, [EMAIL PROTECTED] declaimed the
> following in comp.lang.python:
>
>   Could it be that the SERVER is limiting things to 5
> concurrent/parallel connections from any single IP?
>
>   I know I've encountered sites that only allowed two FTP downloads at
> a time...

This is what I was starting to think as well.  The only thing that
looked funky with the OP's code was that it looked like he was writing
everything to a filename of "" (unless he's intentionally modified his
code to not show where he's setting that).

- Jeremy M. Jones

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


Re: How to display name of elements in list?

2006-07-12 Thread cz
> The list above is not a valid Python list. What is it that you store in that 
> list?
>
> Or is it maybe a dictionary?
>
> Stefan

Thanks for your help. How can I find out about what this is? As I said
it's generated by a insufficiently documented module. So if this is a
user defined datatype, is there still a possibility to find the name of
the data fields storing the xx, xy, ... ?
Thanks.

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


Re: Multi-threaded FTP Question

2006-07-12 Thread olsongt

[EMAIL PROTECTED] wrote:
> I'm trying to use ftp in python in a multi-threaded way on a windows
> box - python version 2.4.3.  Problem is that it appears that it's only
> possible to have five instances/threads at one point in time.  Errors
> look like:
>
>File "C:\Python24\lib\ftplib.py", line 107, in __init__
> self.connect(host)
>   File "C:\Python24\lib\ftplib.py", line 132, in connect
> self.welcome = self.getresp()
>   File "C:\Python24\lib\ftplib.py", line 208, in getresp
> resp = self.getmultiline()
>   File "C:\Python24\lib\ftplib.py", line 194, in getmultiline
> line = self.getline()
>   File "C:\Python24\lib\ftplib.py", line 184, in getline
> if not line: raise EOFError
> EOFError
>
> Is it possible to have more than five simultaneous ftp connections?
>
> Thanks.
>
> Derek

It might be XP SP2's worm protection as well:

http://www.speedguide.net/read_articles.php?id=1497

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


Editing File

2006-07-12 Thread D
Hi, I currently have a Python app with a Tkinter GUI frontend that I
use for system administration.  Everytime it launches, it reads a text
file which contains info about each host I wish to monitor - each field
(such as IP, hostname, etc.) is delimited by !!.  Now, I want to be
able to edit host information from within the GUI - what would  be the
best way to go about this?  Basically I just need to either edit the
original host line, or write a new host line and delete the
original..thanks!

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


Re: What is a type error?

2006-07-12 Thread Joachim Durchholz
Marshall schrieb:
> Joachim Durchholz wrote:
>> Marshall schrieb:
>>> Now, I'm not fully up to speed on DBC. The contract specifications,
>>> these are specified statically, but checked dynamically, is that
>>> right?
>> That's how it's done in Eiffel, yes.
>>
>>  > In other words, we can consider contracts in light of
>>> inheritance, but the actual verification and checking happens
>>> at runtime, yes?
>> Sure. Though, while DbC gives rules for inheritance (actually subtypes),
>> these are irrelevant to the current discussion; DbC-minus-subtyping can
>> still be usefully applied.
> 
> Yes, subtyping. Of course I meant to say subtyping.

No need to blush for that, it's perfectly OK in the Eiffel context, 
where a subclass ist always assumed to be a subtype. (This assumption 
isn't always true, which is why Eiffel has some serious holes in the 
type system. The DbC ideas are still useful though, saying "subtype" 
instead of "subclass" just makes the concept applicable outside of OO 
languages.)

> I can certainly see how DbC would be useful without subtyping.
> But would there still be a reason to separate preconditions
> from postconditions? I've never been clear on the point
> of differentiating them (beyond the fact that one's covariant
> and the other is contravariant.)

There is indeed.
The rules about covariance and contravariance are just consequences of 
the notion of having a subtype (albeit important ones when it comes to 
designing concrete interfaces).

For example, if a precondition fails, something is wrong about the 
things that the subroutine assumes about its environment, so it 
shouldn't have been called. This means the error is in the caller, not 
in the subroutine that carries the precondition.

The less important consequence is that this should be reflected in the 
error messages.

The more important consequences apply when integrating software.
If you have a well-tested library, it makes sense to switch off 
postcondition checking for the library routines, but not their 
precondition checking.
This applies not just for run-time checking: Ideally, with compile-time 
inference, all postconditions can be inferred from the function's 
preconditions and their code. The results of that inference can easily 
be stored in the precompiled libraries.
Preconditions, on the other hand, can only be verified by checking all 
places where the functions are called.

>>> Wouldn't it be possible to do them at compile time? (Although
>>> this raises decidability issues.)
>> Exactly, and that's why you'd either uses a restricted assertion
>> language (and essentially get something that's somewhere between a type
>> system and traditional assertion); or you'd use some inference system
>> and try to help it along (not a simple thing either - the components of
>> such a system exist, but I'm not aware of any system that was designed
>> for the average programmer).
> 
> As to the average programmer, I heard this recently on
> comp.databases.theory:
> 
> "Don't blame me for the fact that competent programming, as I view it
> as an intellectual possibility, will be too difficult for "the
> average programmer"  -you must not fall into the trap of rejecting
> a surgical technique because it is beyond the capabilities of the
> barber in his shop around the corner."   -- EWD512

Given the fact that we have far more need for competently-written 
programs than for competent surgery, I don't think that we'll be able to 
follow that idea.

>>> Mightn't it also be possible to
>>> leave it up to the programmer whether a given contract
>>> was compile-time or runtime?
>> I'd agree with that, but I'm not sure how well that would hold up in
>> practice.
> 
> I want to try it and see what it's like.

So do I :-)

Regards,
Jo
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Editing File

2006-07-12 Thread Jeremy Jones

D wrote:
> Hi, I currently have a Python app with a Tkinter GUI frontend that I
> use for system administration.  Everytime it launches, it reads a text
> file which contains info about each host I wish to monitor - each field
> (such as IP, hostname, etc.) is delimited by !!.  Now, I want to be
> able to edit host information from within the GUI - what would  be the
> best way to go about this?  Basically I just need to either edit the
> original host line, or write a new host line and delete the
> original..thanks!

I would create a data structure of the contents of the file and let the
application reference that data structure.  Sounds like it's going to
be a list of lists or a list of dicts.  Each line of the file is going
to be an element of the "main" list.  Each element of the list is going
to be a dict or a list of the details of that particular host.  Make it
so that if your app changes the datastructure, you re-serialize it back
to the file.  This should work the same with adding a new host to
monitor.

It might be easier to use something like Yaml.  I'm doing something
similar with a little podcast grabber I'm working on.  Here's some old
code where I first incorporate using Yaml (down at the bottom of the
page): http://jeremymjones.com/articles/simple-podcast-grabber-python/
The version I have in SVN right now creates a configgish object off of
the Yaml and on re-assignment of either of the two main attributes, it
automatically reserializes it.

Anyway, hope this helps.

- Jeremy M. Jones

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


Re: How to display name of elements in list?

2006-07-12 Thread Diez B. Roggisch
cz wrote:

>> The list above is not a valid Python list. What is it that you store in
>> that list?
>>
>> Or is it maybe a dictionary?
>>
>> Stefan
> 
> Thanks for your help. How can I find out about what this is? As I said
> it's generated by a insufficiently documented module. So if this is a
> user defined datatype, is there still a possibility to find the name of
> the data fields storing the xx, xy, ... ?

I guess the objects stored in the list have a __repr__-method overloaded
that produces the text you see. 

What you should do is to install rlcompleter2, fire up a python prompt and
write code that produces a list of your objects. Then you can play around
with the objects and utilize the reflection capabilities of python, which
are conveniently exposed using rlcompleter2.

Another option is to look into the source of that module and identify the
objects created. Documentation is overrated - use the source, Luke!

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


Re: How to display name of elements in list?

2006-07-12 Thread Magnus Lycka
>> My list called "elten" looks like that:
>>
>> [Tensor: id = 1,  intensity = 2976.52
>>  xx = -1447.32, xy = 52.458, xz = -594.186
>>  yy = -1090.54, yz = -0.0158068, zz = -4043.
>> , Tensor: id = 26,  intensity = 2896.9
>>  ...
>> , Tensor: id = 5,  intensity = 2920.5
>>  xx = -1534.53, xy = 23.4858, xz = -623.967
>>  yy = -1070.47, yz = 99.6301, zz = -3979.87
>> ]

> The list above is not a valid Python list. What is it that you store in that 
> list?

It might well be a normal Python list.

The question is what type the items in the list are...
The result of printing a list L is basically a string you
could make like this:

'[' + ','.join(map(repr,L)) + ']'

It seems the elements in this list appear as something
like this when you apply the repr() function on them:

Tensor: id = 1,  intensity = 2976.52
  xx = -1447.32, xy = 52.458, xz = -594.186
  yy = -1090.54, yz = -0.0158068, zz = -4043.

So, the issue is not how you work with a list,
but how you work with the elements of this type.

To reduce the problem to that, you can assign the
first element in the list to a variable.

elem0 = elten[0]

Then you can inspect that in isolation, without
the confusion of the list.

type(elem0)
dir(elem0)
etc...
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: How to display name of elements in list?

2006-07-12 Thread Stefan Behnel
cz schrieb:
>> The list above is not a valid Python list. What is it that you store in that 
>> list?
>>
>> Or is it maybe a dictionary?
>>
>> Stefan
> 
> Thanks for your help. How can I find out about what this is? As I said
> it's generated by a insufficiently documented module. So if this is a
> user defined datatype, is there still a possibility to find the name of
> the data fields storing the xx, xy, ... ?

Maybe you should read a bit about Python classes and built-in functions like
"dir()", "type()", "vars()", ...

http://docs.python.org/tut/node8.html#SECTION00830
http://docs.python.org/tut/node11.html
http://docs.python.org/lib/built-in-funcs.html

Just start an interactive Python session and play with the object you are
trying to explore. That should get you going.

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


Re: hash() yields different results for different platforms

2006-07-12 Thread Grant Edwards
On 2006-07-12, Carl Banks <[EMAIL PROTECTED]> wrote:
> Grant Edwards wrote:
>> On 2006-07-11, Qiangning Hong <[EMAIL PROTECTED]> wrote:
>>
>> > I'm writing a spider. I have millions of urls in a table (mysql) to
>> > check if a url has already been fetched. To check fast, I am
>> > considering to add a "hash" column in the table, make it a unique key,
>> > and use the following sql statement:
>> >   insert ignore into urls (url, hash) values (newurl, hash_of_newurl)
>> > to add new url.
>> >
>> > I believe this will be faster than making the "url" column unique key
>> > and doing string comparation.  Right?
>>
>> I doubt it will be significantly faster.  Comparing two strings
>> and hashing a string are both O(N).
>
> Playing Devil's Advocate: The hash would be a one-time operation during
> database insertion, whereas string comparison would happen every
> search.

Good point.

> Conceivably, comparing hash strings (which is O(1)) could
> result in a big savings compared to comparing regular strings;

Still, I doubt that the URLs are long enough so that there's a
significant difference.

> but I expect most decent sql implementations already hash data
> internally, so rolling your own hash would be useless at best.

Precisely.  DB designers and implementers have been working on
this problem for 30 years.  I doubt the OP is going to be able
to best them with a few minutes work.

> If the OP's database is lacking, md5 is probably fine. Perhaps
> using a subset of the md5 (the low 32 bits, say) could speed
> up comparisons at risk of more collisions.  Probably a good
> trade off unless the DB is humungous.

My advice: do it the simple way first (let the DB handle it).
Don't try to fix a problem until you know it exists.

Premature optimization

-- 
Grant Edwards   grante Yow!  It's strange, but I'm
  at   only TRULY ALIVE when I'm
   visi.comcovered in POLKA DOTS and
   TACO SAUCE...
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: hash() yields different results for different platforms

2006-07-12 Thread Grant Edwards
On 2006-07-12, Qiangning Hong <[EMAIL PROTECTED]> wrote:
> Grant Edwards wrote:
>> On 2006-07-11, Qiangning Hong <[EMAIL PROTECTED]> wrote:
>> > However, when I come to Python's builtin hash() function, I
>> > found it produces different values in my two computers!  In a
>> > pentium4, hash('a') -> -468864544; in a amd64, hash('a') ->
>> > 12416037344.  Does hash function depend on machine's word
>> > length?
>>
>> Apparently. :)
>>
>> The low 32 bits match, so perhaps you should just use that
>> portion of the returned hash?
>>
>> >>> hex(12416037344)
>> '0x2E40DB1E0L'
>> >>> hex(-468864544 & 0x)
>> '0xE40DB1E0L'
>>
>> >>> hex(12416037344 & 0x)
>> '0xE40DB1E0L'
>> >>> hex(-468864544 & 0x)
>> '0xE40DB1E0L'
>
> Is this relationship (same low 32 bits) guaranteed?

No, I don't believe so.

> Will it change in the future version?

It may.

-- 
Grant Edwards   grante Yow!  Is this an out-take
  at   from the "BRADY BUNCH"?
   visi.com
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: What is a type error?

2006-07-12 Thread Joachim Durchholz
Darren New schrieb:
> As far as I understand it, Eiffel compilers don't even make use of 
> postconditions to optimize code or eliminate run-time checks (like null 
> pointer testing).

That's correct.

I think a large part of the reasons why this isn't done is that Eiffel's 
semantics is (a) too complicated (it's an imperative language after 
all), and (b) not formalized, which makes it difficult to assess what 
optimizations are safe and what aren't.

(Reason (c) is that Eiffel compiler vendors don't have the manpower for 
this kind of work, mostly in quantity but also, to some degree, in 
quality: people with a solid language semantics background tend to be 
repelled by the language inventor's know-it-all deny-the-problems 
don't-bother-me-with-formalisms attitude. He still has moved the state 
of the art ahead - mostly by pointing out a certain class of problems in 
OO designs and explaining them lucidly, and proposing solutions that 
hold up better than average even if still fundamentally flawed.)

Regards,
Jo
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Sets and Membership Tests

2006-07-12 Thread JKPeck
Thanks for the advice.  Once assured that __hash__ etc was the right
route, I found that using hash() instead of object.__hash__() gave me
stable hash valules.  (I am hashing strings that I know to be unique.)

The "no luck" situation was that a set would accept the same object
multiple times, not recognizing that it was truly the same object.


Nick Vatamaniuc wrote:
> JK,
>
> You are correct to implement __hash__ and __eq__. The problem is how
> you implemented them. Usually your __eq__ method should compare the
> necessary attributes of the objects for equality. The __hash__ should
> return a 32-bit integer. Your best bet is probably to return a hash of
> hashes of your attributes that are used in equality comparison. What
> this means is that your attributes used to produce the __hash__ should
> also be hashable. This is important yet not immediatly obvious. So you
> could for example return hash( (attribute1, attribute2, attribute3) ),
> where attribute1, attribute2, attribute3 are all hashable.
>  Of course, you provided no code and no error messages (the
> 'SoFarNoLuck' exception is not descriptive enough ; )
>
> Hope this helps
>
>
>
> JKPeck wrote:
> > I would like to be able use sets where the set members are objects of a
> > class I wrote.
> > I want the members to be distinguished by some of the object content,
> > but I have not figured out how a set determines whether two (potential)
> > elements are identical.  I tried implementing __eq__ and __ne__ and
> > __hash__ to make objects with identical content behave as identical for
> > set membership, but so far no luck.
> >
> > I could subclass set if necessary, but I still don't know what I would
> > need to override.
> > 
> > TIA for any advice you can offer.

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


Data access from multiple code modules

2006-07-12 Thread simon . hibbs
Lets say that I have an application consisting of 3 files. A main.py
file, gui.py and a data.py which handles persistent data storage.
Suppose data.py defines a class 'MyDB' which reads in data from a
database, and main.py creates an instance of this object. How does code
in gui.py access this object? Here's simplified pseudocode:

MAIN.PY
import gui, data
DataObject = data.MyDB(blah)

How do I write code in gui.py that can access DataObject? Is this
entirely the wrong way to approach this sort of problem?

Actualy the problem is more complex because the GUI consists of a main
GUI form, and panels defined as seperate objects in seperate files.
Various panels will contain controlls for manipulating data in the
DataObject, or wherever data storage end up.


Best regards,

Simon Hibbs
(who strugles to get his head round this OOP stuff sometimes).

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


Re: Data access from multiple code modules

2006-07-12 Thread Jeremy Jones

[EMAIL PROTECTED] wrote:
> Lets say that I have an application consisting of 3 files. A main.py
> file, gui.py and a data.py which handles persistent data storage.
> Suppose data.py defines a class 'MyDB' which reads in data from a
> database, and main.py creates an instance of this object. How does code
> in gui.py access this object? Here's simplified pseudocode:
>
> MAIN.PY
> import gui, data
> DataObject = data.MyDB(blah)
>
> How do I write code in gui.py that can access DataObject? Is this
> entirely the wrong way to approach this sort of problem?
>
> Actualy the problem is more complex because the GUI consists of a main
> GUI form, and panels defined as seperate objects in seperate files.
> Various panels will contain controlls for manipulating data in the
> DataObject, or wherever data storage end up.

What does main.py do?  Are you creating an instance of the gui thingy?
If so, you could just pass DataObject into your gui thingy either into
the constructor or to a setter once you create an instance of it.  If
the gui needs any database stuff at instantiation time, you probably
need to pass it into the constructor.  However, a main.py, gui.py, and
db.py smells a little like your standard MVC, in which case, you would
get your controller to pass in the data pieces as the GUI needs them.

- Jeremy M. Jones

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


Re: Sets and Membership Tests

2006-07-12 Thread Alex Martelli
JKPeck <[EMAIL PROTECTED]> wrote:

> Thanks for the advice.  Once assured that __hash__ etc was the right
> route, I found that using hash() instead of object.__hash__() gave me
> stable hash valules.  (I am hashing strings that I know to be unique.)
> 
> The "no luck" situation was that a set would accept the same object
> multiple times, not recognizing that it was truly the same object.

Your problem may have been exactly that you were misunderstanding the
nature and functionality of object.__hash__:

>>> x='guess what'
>>> id(x)
360720
>>> object.__hash__(x)
360720

As you see, what it does is return the id() of the object (that's how
objects are hashed -- if they don't implement __eq__ or __cmp__,
equality comparisons also go to the id()'s, so things work;-) -- unless
their type/class overrides __hash__).  Of course, by going directly to
object.__hash__ you're explicitly *avoiding* the override, so...


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


Re: Progress Bars in python

2006-07-12 Thread Hari Sekhon
On 12/07/06, Fredrik Lundh <[EMAIL PROTECTED]> wrote:
> If the output of the script is sent to a logfile, this tends to puke all> over the logfile... creating one additional entry per iteration, but it's a> good start and I'll look at that link which looks very promising.
there's no way to do this without sending more stuff to stdout than yousee; however, it's easy to *disable* the spinner if you're redirecting theoutput.  hint:$ python -c "import sys; print sys.stdout.isatty
()"True$ python -c "import sys; print sys.stdout.isatty()" >out$ more outFalseThanks, that will do nicely. It will be sufficient to do this testing using isatty() and have the progress bar or spinner.
-h
-- 
http://mail.python.org/mailman/listinfo/python-list

Re: Editing File

2006-07-12 Thread [EMAIL PROTECTED]

D wrote:
> Hi, I currently have a Python app with a Tkinter GUI frontend that I
> use for system administration.  Everytime it launches, it reads a text
> file which contains info about each host I wish to monitor - each field
> (such as IP, hostname, etc.) is delimited by !!.  Now, I want to be
> able to edit host information from within the GUI - what would  be the
> best way to go about this?  Basically I just need to either edit the
> original host line, or write a new host line and delete the
> original..thanks!

Might be overkill - but pickle the  data memeber that contains the
information.  If you use text instead of binary pickling it should
still be editable by hand.  for a single line of text it may be a bit
much - but it's still probably quicker than opening a file, parsing
etc.

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


Re: Data access from multiple code modules

2006-07-12 Thread Bruno Desthuilliers
[EMAIL PROTECTED] wrote:
> Lets say that I have an application consisting of 3 files. A main.py
> file, gui.py and a data.py which handles persistent data storage.
> Suppose data.py defines a class 'MyDB' which reads in data from a
> database, and main.py creates an instance of this object. How does code
> in gui.py access this object? 

Obviously, you need to pass whatever relevant to this code...

> Here's simplified pseudocode:
> 
> MAIN.PY
> import gui, data
> DataObject = data.MyDB(blah)
> 
> How do I write code in gui.py that can access DataObject? 

Passing DataObject to code in guy.py seems like a very straightforward
solution...

> Is this
> entirely the wrong way to approach this sort of problem?

> Actualy the problem is more complex because the GUI consists of a main
> GUI form, and panels defined as seperate objects in seperate files.
> Various panels will contain controlls for manipulating data in the
> DataObject, or wherever data storage end up.

Hmmm... Hard to tell without seeing the code (and I've not done GUI
programmer for a long time), but putting application logic in the GUI is
usually a bad idea IME.  Better to put the application logic in a
separate controler (that has a reference to the model -> here your
DataObject), and have the GUI part call on the controler.

> 
> Best regards,
> 
> Simon Hibbs
> (who strugles to get his head round this OOP stuff sometimes).

Well, actually one can do MVC without OO. It's more a matter of
separating concerns. Now I agree that it's not always obvious to know
for sure which part should be responsible for what...

-- 
bruno desthuilliers
python -c "print '@'.join(['.'.join([w[::-1] for w in p.split('.')]) for
p in '[EMAIL PROTECTED]'.split('@')])"
-- 
http://mail.python.org/mailman/listinfo/python-list


Persistant dictionary with lockable elements

2006-07-12 Thread Will McGugan
Hi,

I'd like to have a persistant dictionary in a server so that incoming
requests acquire a specific Python object, do something with it then
return. There wont be that many objects but it is the persistance that
is important here, I want the information to survive server re-starts /
crashes. The Shelve module seems ideal for this, but because the server
will be multithreaded I would like to be able to lock individual
elements of the shelve while they are being processed (not just the
entire Shelve object).

Is there some way of using Shelve like this, or should I just move to a
more typical database solution?

Thanks,

Will McGugan
http://www.willmcgugan.com

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


Re: Progress Bars in python

2006-07-12 Thread Larry Bates
Hari Sekhon wrote:
> Hi,
>   I've written a script which backs up a huge bunch of files, but I
> don't want the script to output the file names as it does this as it
> clutters the screen, I only output errors.
> 
> So in order to see that the script is working and not stuck, I'd like to
> implement some kind of progress bar or something, perhaps like the
> spinning thing that you often see in linux or freebsd consisting of
> switching / - \ | on the spot to get the appearance of a spinning
> bar I can figure out that the spinning bar is done by switching
> these four chars but I don't know how they get each char to replace the
> last one instead of printing them in succession.
> 
> Does anybody have any good suggestions about what the best way of doing
> this or any other suggestions for the best ways to show that the script
> is processing...?
> 
> 
> Hari

This also is shown in Python Cookbook:

http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/299207

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


Re: How to display name of elements in list?

2006-07-12 Thread Fredrik Lundh
Stefan Behnel wrote:

> The list above is not a valid Python list.

there's no Python 1.5.3 either.  maybe he's posting from a parallel, 
slightly different universe ?



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


Re: Persistant dictionary with lockable elements

2006-07-12 Thread Bruno Desthuilliers
Will McGugan wrote:
> Hi,
> 
> I'd like to have a persistant dictionary in a server so that incoming
> requests acquire a specific Python object, do something with it then
> return. There wont be that many objects but it is the persistance that
> is important here, I want the information to survive server re-starts /
> crashes. The Shelve module seems ideal for this, but because the server
> will be multithreaded I would like to be able to lock individual
> elements of the shelve while they are being processed (not just the
> entire Shelve object).
> 
> Is there some way of using Shelve like this, or should I just move to a
> more typical database solution?

You may want to try with the ZODB. Or with SQLite.



-- 
bruno desthuilliers
python -c "print '@'.join(['.'.join([w[::-1] for w in p.split('.')]) for
p in '[EMAIL PROTECTED]'.split('@')])"
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Data access from multiple code modules

2006-07-12 Thread simon . hibbs

Jeremy Jones wrote:

> What does main.py do?  Are you creating an instance of the gui thingy?
> If so, you could just pass DataObject into your gui thingy either into
> the constructor or to a setter once you create an instance of it.

It's a wxPython app. I created the GUI initialy using wxGlade which
gave me a small myapp.py script, a file containing the main application
frame (containing a listbook controll) and several files containing
panel classes. Each panel class contains controlls for performing
various operations on the data set (adding records, deleting them,
running various transformations). I can't say I understand how it all
works at a deep level, although I've been hacking it about quite
successfuly so far.

Presumably if I pass DataObject through to the Frame object, and from
there through to the Panel objects then presumably this will solve the
probelm? I guess it would be passed by reference so all the panels
would be working on the same data object?

Doh! How simple. Why didn't I think of that? I'm too used to procedural
scripts where you'd just put everything in a global data structure. I
know this is bad, but it's hard to get out of that mentality.

Many thanks,

Simon Hibbs

P.S. Regular reader of your blog on Oreillynet.

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


Re: Problems with Unicode Plane 1 characters on Windows

2006-07-12 Thread Fredrik Lundh
Edin Salković wrote:

> Why doesn't the following code work on Windows XP, although it works
> on Linux (Ubuntu 6.06). Both versions are of Python are 2.4, and both
> OSs are on the same PC.
> 
 import unicodedata
 unicodedata.name(U'\U0001d400')
> Traceback (most recent call last):
>  File "", line 1, in ?
> TypeError: need a single Unicode character as parameter
> 
> The output should say:
> MATHEMATICAL BOLD CAPITAL A

hint:

 >>> len(u'\U0001d400')
2

Python's Unicode system uses 16-bit values internally on some platforms, 
and 32-bit values on some platforms.  on 16-bit platforms, code points 
outside the BMP are stored as surrogate pairs.

also see:

 http://pyref.infogami.com/type-unicode



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

Re: Editing File

2006-07-12 Thread D
Thanks, guys.  So overall, would it just be easier (and not too rigged)
if any changes were made by just editing the text file?  I want to do
this project the right way, but if it's going to be a big pain to
implement the edit function, just modifying the text file directly
isn't that big of a deal..

[EMAIL PROTECTED] wrote:
> D wrote:
> > Hi, I currently have a Python app with a Tkinter GUI frontend that I
> > use for system administration.  Everytime it launches, it reads a text
> > file which contains info about each host I wish to monitor - each field
> > (such as IP, hostname, etc.) is delimited by !!.  Now, I want to be
> > able to edit host information from within the GUI - what would  be the
> > best way to go about this?  Basically I just need to either edit the
> > original host line, or write a new host line and delete the
> > original..thanks!
>
> Might be overkill - but pickle the  data memeber that contains the
> information.  If you use text instead of binary pickling it should
> still be editable by hand.  for a single line of text it may be a bit
> much - but it's still probably quicker than opening a file, parsing
> etc.

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


Re: Editing File

2006-07-12 Thread [EMAIL PROTECTED]

D wrote:
> Thanks, guys.  So overall, would it just be easier (and not too rigged)
> if any changes were made by just editing the text file?  I want to do
> this project the right way, but if it's going to be a big pain to
> implement the edit function, just modifying the text file directly
> isn't that big of a deal..
have you used pickle?  if the data is as simple as you say it is, you
will be able to read the pickle file.  2nd, it will be a lot less code
really.  You just load and unpickle the file into a variable.  After
any edit in the gui, repickle it to the same file.  You would have to
do the same if you edited the text file, except you would need a couple
of lines code to parse the string, etc.

check here for a simple example :
http://wiki.python.org/moin/UsingPickle

>
> [EMAIL PROTECTED] wrote:
> > D wrote:
> > > Hi, I currently have a Python app with a Tkinter GUI frontend that I
> > > use for system administration.  Everytime it launches, it reads a text
> > > file which contains info about each host I wish to monitor - each field
> > > (such as IP, hostname, etc.) is delimited by !!.  Now, I want to be
> > > able to edit host information from within the GUI - what would  be the
> > > best way to go about this?  Basically I just need to either edit the
> > > original host line, or write a new host line and delete the
> > > original..thanks!
> >
> > Might be overkill - but pickle the  data memeber that contains the
> > information.  If you use text instead of binary pickling it should
> > still be editable by hand.  for a single line of text it may be a bit
> > much - but it's still probably quicker than opening a file, parsing
> > etc.

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


check type when assignment

2006-07-12 Thread pipehappy
Hello everyone:

Is there a way to check the type when do assignment?

if I write:
ab = bc
and want to make sure the return value of isinstance(bc, klass) is True
or I will raise
a exception.

Any suggestion?

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


Re: How to display name of elements in list?

2006-07-12 Thread Tal Einat
Diez B. Roggisch  nospam.web.de> writes:
> 
> What you should do is to install rlcompleter2...
[snip]
> 
> Another option is to look into the source of that module and identify the
> objects created. Documentation is overrated - use the source, Luke!

rlcompleter is overrated, and only works on Unix/Linux/etc.

IDLE's interpreter has an auto-completion extension, which is bundled in
Python2.5.

You can also get a much better version of IDLE which includes a more robust
version of the completion extension from Idle-Spoon:
http://idlespoon.python-hosting.com/


stefan writes:
>
> Just start an interactive Python session and play with the object you are
> trying to explore. That should get you going.

+1
Python is fun - just open an interpreter and play around!


- Tal

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


Re: Editing File

2006-07-12 Thread Jeremy Jones

D wrote:
> Thanks, guys.  So overall, would it just be easier (and not too rigged)
> if any changes were made by just editing the text file?  I want to do



> [EMAIL PROTECTED] wrote:



> > Might be overkill - but pickle the  data memeber that contains the
> > information.  If you use text instead of binary pickling it should
> > still be editable by hand.  for a single line of text it may be a bit
> > much - but it's still probably quicker than opening a file, parsing
> > etc.

Look at pickle, but I'd recommend against it if you're anticipating
needing to edit the file by hand.  It's just a little on the ugly side.
 Glance at Yaml (I think it's the pyyaml project in the cheeseshop) as
well.  Here's the code needed to "parse" in a .yaml file:

config = yaml.load(open(self.config_file, "r"))

Here's the code needed to serialize it back in a pretty format:

yaml.dump(config, config_file_obj, default_flow_style=False)

And here's a piece of a .yaml file itself:

feeds:
  http://leo.am/podcasts/floss:
name: FLOSS Weekly
mode: dl
  http://revision3.com/diggnation/feed/high.mp3.xml:
name: Revision3 - Diggnation w/Kevin Rose & Alex Albrecht
mode: dl
  http://geekmuse.net/podcast/:
name: Geek Muse
mode: dl

http://www.itconversations.com/rss/category-rss.php?k=achange2005&e=1:
name: Accelerating Change 2005
mode: dl

Nice and clean.

- Jeremy M. Jones

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


Re: Data access from multiple code modules

2006-07-12 Thread Jeremy Jones

[EMAIL PROTECTED] wrote:



> Doh! How simple. Why didn't I think of that? I'm too used to procedural
> scripts where you'd just put everything in a global data structure. I
> know this is bad, but it's hard to get out of that mentality.

Sounds like you got it.  Just pass it on down as needed.

- jmj

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


Re: timeit module for comparing the performance of two scripts

2006-07-12 Thread Georg Brandl
3c273 wrote:
> "John Machin" <[EMAIL PROTECTED]> wrote in message
> news:[EMAIL PROTECTED]
>> You appear to know what a switch is. I'm therefore surprised that you
>> appear not to
>> know that the convention is that any program that uses
>> command-line switches should do something informative when run with a -h
>> switch.
>>
> Doh! Me thinks Windows at work "python /?" (No good!), Linux at home
> "python -h" (Ah ha!). I still think it should be in the docs somewhere.

python /? now works in 2.5 SVN.

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


Re: check type when assignment

2006-07-12 Thread Tal Einat
pipehappy  gmail.com> writes:

> 
> Hello everyone:
> 
> Is there a way to check the type when do assignment?
> 
> if I write:
> ab = bc
> and want to make sure the return value of isinstance(bc, klass) is True
> or I will raise
> a exception.
> 
> Any suggestion?
> 

1. Check your condition before the assignment: (IMO one-liners are over-rated)

if not isinstance(bc, klass):
raise TypeError # or do whatever else is appropriate
ab = bc

2. If you really insist on doing this in a single statement (in the assignment
itself), write a function for it:

def assert_type(obj, klass):
if not isinstance(bc, klass):
raise TypeError
return obj
ab = assert_type(bc, klass)

Or to be more generic:

def assert_return(obj, func):
assert func(obj)
return obj

ab = assert_return(bc, lambda obj:isinstance(obj, klass))

- Tal

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


don't need dictionary's keys - hash table?

2006-07-12 Thread kdotsky
Hello,
I am using some very large dictionaries with keys that are long strings
(urls).  For a large dictionary these keys start to take up a
significant amount of memory.  I do not need access to these keys -- I
only need to be able to retrieve the value associated with a certain
key, so I do not want to have the keys stored in memory.  Could I just
hash() the url strings first and use the resulting integer as the key?
I think what I'm after here is more like a tradition hash table.  If I
do it this way am I going to get the memory savings I am after?  Will
the hash function always generate unique keys?  Also, would the same
technique work for a set?

Any other thoughts or considerations are appreciated.

Thank You.

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


Re: check type when assignment

2006-07-12 Thread Diez B. Roggisch
pipehappy wrote:

> Hello everyone:
> 
> Is there a way to check the type when do assignment?
> 
> if I write:
> ab = bc
> and want to make sure the return value of isinstance(bc, klass) is True
> or I will raise
> a exception.

In general, not doable. The assignment operator is not overloadable.

Only if you use assignments of the form

a.foo = bar

you could overwrite the __setattribute__-method to achieve what you want. 


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


Re: How to display name of elements in list?

2006-07-12 Thread Diez B. Roggisch
> rlcompleter is overrated, and only works on Unix/Linux/etc.
> 
> IDLE's interpreter has an auto-completion extension, which is bundled in
> Python2.5.

I don't use idle, and don't want to. So for me rlcomlpeter2 is a good thing.
And under windows, it at least works under cygwin.

Diez

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


Re: Editing File

2006-07-12 Thread Tal Einat
akameswaran  gmail.com  gmail.com> writes:

> 
> 
> D wrote:
> > Thanks, guys.  So overall, would it just be easier (and not too rigged)
> > if any changes were made by just editing the text file?
[snip]
> have you used pickle?  if the data is as simple as you say it is, you
> will be able to read the pickle file.  2nd, it will be a lot less code
> really.  You just load and unpickle the file into a variable.  After
> any edit in the gui, repickle it to the same file.  You would have to
> do the same if you edited the text file, except you would need a couple
> of lines code to parse the string, etc.
> 

If you don't plan to edit your data by hand, Pickle is a nice and simple choice.
To save you from coding the persistency bits, you can use Python's built-in
shelve module.

Shelve uses Pickle to serialize Python objects but it does most of the
persistency stuff for you. It is very simple and clean to use. One nice feature
is that can open a shelve in auto-writeback mode, so upon assignment to one of
the shelve's keys (it's like a dict) the shelve is automatically re-serialized
to the file on disk. This saves headaches like remembering to serialize the data
upon exit cleanup, crashes resulting in data loss, etc.

Shelve is the simplest tool I know of. YAML sounds nice and readable, I haven't
tryed it out yet. You could use XML easily enough with ElementTree.

Good luck!

- Tal



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


Re: Editing File

2006-07-12 Thread Maric Michaud
Le mercredi 12 juillet 2006 17:00, D a écrit :
> Thanks, guys.  So overall, would it just be easier (and not too rigged)
> if any changes were made by just editing the text file?  I want to do
> this project the right way, but if it's going to be a big pain to
> implement the edit function, just modifying the text file directly
> isn't that big of a deal..

If you don't want to rely on third party libraries, and want a human-readable 
format I'll suggest using csv file format :

and this way ?

In [47]: class Z(object) :
   : def __init__(self, val) : self.val = val
   :
   :

In [48]: lst = [Z(i) for i in range(2) + [ str(e) for e in range(2) ] ]

In [49]: [ (e, e.val) for e in lst ]
Out[49]:
[(<__main__.Z object at 0xa76c252c>, 0),
 (<__main__.Z object at 0xa76c27ac>, 1),
 (<__main__.Z object at 0xa76c23ac>, '0'),
 (<__main__.Z object at 0xa76c23ec>, '1')]

In [51]: csv.writer(file('config.csv', 'w')).writerows([ 
(type(i.val).__name__, i.val) for i in lst])

In [52]: print file('config.csv').read()
int,0
int,1
str,0
str,1


In [53]: l = [ Z(eval(class_)(val)) for class_, val in 
csv.reader(file('config.csv')) ]

In [54]: [ (e, e.val) for e in l ]
Out[54]:
[(<__main__.Z object at 0xa76c218c>, 0),
 (<__main__.Z object at 0xa76c260c>, 1),
 (<__main__.Z object at 0xa76c256c>, '0'),
 (<__main__.Z object at 0xa76c25cc>, '1')]




-- 
_

Maric Michaud
_

Aristote - www.aristote.info
3 place des tapis
69004 Lyon
Tel: +33 426 880 097
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Multi-threaded FTP Question

2006-07-12 Thread dbandler
Thanks so much for your help on this.  The server that I'm connecting
to is the culprit.  They only allow five connections at a time.

I assumed that it was a code issue.  I think that we're conditioned to
expect that the problem is on the software side of things.

-Derek


[EMAIL PROTECTED] wrote:
> [EMAIL PROTECTED] wrote:
> > I'm trying to use ftp in python in a multi-threaded way on a windows
> > box - python version 2.4.3.  Problem is that it appears that it's only
> > possible to have five instances/threads at one point in time.  Errors
> > look like:
> >
> >File "C:\Python24\lib\ftplib.py", line 107, in __init__
> > self.connect(host)
> >   File "C:\Python24\lib\ftplib.py", line 132, in connect
> > self.welcome = self.getresp()
> >   File "C:\Python24\lib\ftplib.py", line 208, in getresp
> > resp = self.getmultiline()
> >   File "C:\Python24\lib\ftplib.py", line 194, in getmultiline
> > line = self.getline()
> >   File "C:\Python24\lib\ftplib.py", line 184, in getline
> > if not line: raise EOFError
> > EOFError
> >
> > Is it possible to have more than five simultaneous ftp connections?
> >
> > Thanks.
> >
> > Derek
>
> It might be XP SP2's worm protection as well:
> 
> http://www.speedguide.net/read_articles.php?id=1497

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


Re: don't need dictionary's keys - hash table?

2006-07-12 Thread kdotsky
[EMAIL PROTECTED] wrote:
> Hello,
> I am using some very large dictionaries with keys that are long strings
> (urls).  For a large dictionary these keys start to take up a
> significant amount of memory.  I do not need access to these keys -- I
> only need to be able to retrieve the value associated with a certain
> key, so I do not want to have the keys stored in memory.  Could I just
> hash() the url strings first and use the resulting integer as the key?
> I think what I'm after here is more like a tradition hash table.  If I
> do it this way am I going to get the memory savings I am after?  Will
> the hash function always generate unique keys?  Also, would the same
> technique work for a set?
>

I just realized that of course the hash is not always going to be
unique, so this wouldn't really work.  And it seems a hash table would
still need to store the keys (as strings) so that string comparisons
can be done when a collision occurs.  I guess there's no avoiding
storing they keys?

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


Re: Data access from multiple code modules

2006-07-12 Thread Bruno Desthuilliers
[EMAIL PROTECTED] wrote:
> Jeremy Jones wrote:
> 
> 
>>What does main.py do?  Are you creating an instance of the gui thingy?
>>If so, you could just pass DataObject into your gui thingy either into
>>the constructor or to a setter once you create an instance of it.
> 
> 
> It's a wxPython app. I created the GUI initialy using wxGlade which
> gave me a small myapp.py script, a file containing the main application
> frame (containing a listbook controll) and several files containing
> panel classes. Each panel class contains controlls for performing
> various operations on the data set (adding records, deleting them,
> running various transformations).

Do you mean the code effectively doing these operations is in the gui ?
If yes, it would be better to factor it out IMHO.

> I can't say I understand how it all
> works at a deep level,
> although I've been hacking it about quite
> successfuly so far.
> 
> Presumably if I pass DataObject through to the Frame object, and from
> there through to the Panel objects then presumably this will solve the
> probelm?

Presumably !-)

> I guess it would be passed by reference so all the panels
> would be working on the same data object?

Python doesn't really have a "pass by value" semantic, since "variables"
are really just names bound to (ie : refering to) objects. So when it
comes to arguments passing, the argument *name* is local to the function
(rebinding the name won't affect the binding in the caller's namespace),
but what's bound to the name is really a reference to the object (so
mutating the object will effectively affect it).

> Doh! How simple. Why didn't I think of that? I'm too used to procedural
> scripts where you'd just put everything in a global data structure. I
> know this is bad,

Well, depends on the size of the script. But it sure doesn't scale !-)

And FWIW, even with procedural, it's possible (and usually better) to
pass arguments around instead of having a big global datastructure.
Classes are handy when many functions needs to share state (work on a
same dataset), but there's no way to have one call the other and pass it
the dataset.

> but it's hard to get out of that mentality.

Seems you're on the right way !-)

-- 
bruno desthuilliers
python -c "print '@'.join(['.'.join([w[::-1] for w in p.split('.')]) for
p in '[EMAIL PROTECTED]'.split('@')])"
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: check type when assignment

2006-07-12 Thread Bruno Desthuilliers
Diez B. Roggisch wrote:
> pipehappy wrote:
> 
> 
>>Hello everyone:
>>
>>Is there a way to check the type when do assignment?
>>
>>if I write:
>>ab = bc
>>and want to make sure the return value of isinstance(bc, klass) is True
>>or I will raise
>>a exception.
> 
> 
> In general, not doable. The assignment operator is not overloadable.
> 
> Only if you use assignments of the form
> 
> a.foo = bar
> 
> you could overwrite the __setattribute__-method 

(Or use a Descriptor)

> to achieve what you want. 


> 
> Diez


-- 
bruno desthuilliers
python -c "print '@'.join(['.'.join([w[::-1] for w in p.split('.')]) for
p in '[EMAIL PROTECTED]'.split('@')])"
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Sets and Membership Tests

2006-07-12 Thread Nick Vatamaniuc
JK,
As a general rule, let Python call the "magic" __method__ methods
behind the scenes. So don't call obj.__hash()__ or obj.__len__ or
obj.__le__ just use hash(obj), len(obj) or <=. Of course there are
exceptions, for example when calling the __init__() method of a
supercalass inside the __init__ method of your class and perhaps a few
others...
Nick V.


JKPeck wrote:
> Thanks for the advice.  Once assured that __hash__ etc was the right
> route, I found that using hash() instead of object.__hash__() gave me
> stable hash valules.  (I am hashing strings that I know to be unique.)
>
> The "no luck" situation was that a set would accept the same object
> multiple times, not recognizing that it was truly the same object.
>
>
> Nick Vatamaniuc wrote:
> > JK,
> >
> > You are correct to implement __hash__ and __eq__. The problem is how
> > you implemented them. Usually your __eq__ method should compare the
> > necessary attributes of the objects for equality. The __hash__ should
> > return a 32-bit integer. Your best bet is probably to return a hash of
> > hashes of your attributes that are used in equality comparison. What
> > this means is that your attributes used to produce the __hash__ should
> > also be hashable. This is important yet not immediatly obvious. So you
> > could for example return hash( (attribute1, attribute2, attribute3) ),
> > where attribute1, attribute2, attribute3 are all hashable.
> >  Of course, you provided no code and no error messages (the
> > 'SoFarNoLuck' exception is not descriptive enough ; )
> >
> > Hope this helps
> >
> >
> >
> > JKPeck wrote:
> > > I would like to be able use sets where the set members are objects of a
> > > class I wrote.
> > > I want the members to be distinguished by some of the object content,
> > > but I have not figured out how a set determines whether two (potential)
> > > elements are identical.  I tried implementing __eq__ and __ne__ and
> > > __hash__ to make objects with identical content behave as identical for
> > > set membership, but so far no luck.
> > >
> > > I could subclass set if necessary, but I still don't know what I would
> > > need to override.
> > > 
> > > TIA for any advice you can offer.

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


How to delete a Python package

2006-07-12 Thread Jack
Installing a Python package is easy, most of time just
"Setup.py install" However, setup.py doesn't seem to support
an uninstall command. If I want to delete a package that I
do not use any more, should I just manually delete the
corresponding sub directory under Lib\site-packages? 


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


Re: What is a type error?

2006-07-12 Thread Marshall
Joachim Durchholz wrote:
> Darren New schrieb:
> > As far as I understand it, Eiffel compilers don't even make use of
> > postconditions to optimize code or eliminate run-time checks (like null
> > pointer testing).
>
> That's correct.
>
> I think a large part of the reasons why this isn't done is that Eiffel's
> semantics is (a) too complicated (it's an imperative language after
> all), and (b) not formalized, which makes it difficult to assess what
> optimizations are safe and what aren't.

I can see the lack of a formal model being an issue, but is the
imperative bit really all that much of an obstacle? How hard
is it really to deal with assignment? Or does the issue have
more to do with pointers, aliasing, etc.?


Marshall

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


Re: don't need dictionary's keys - hash table?

2006-07-12 Thread Fredrik Lundh
[EMAIL PROTECTED] wrote:

> Will the hash function always generate unique keys?

no.  hash() is designed for dictionaries (hash tables), not for use as a 
cryptographic hash.

depending on your application, a bloom filter might be a good enough:

 http://en.wikipedia.org/wiki/Bloom_filter

(see the links section for a Python implementation)



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


Re: don't need dictionary's keys - hash table?

2006-07-12 Thread Fredrik Lundh
[EMAIL PROTECTED] wrote:

> I just realized that of course the hash is not always going to be
> unique, so this wouldn't really work.  And it seems a hash table would
> still need to store the keys (as strings) so that string comparisons
> can be done when a collision occurs.

btw, Python's dictionary type *is* a highly-optimized implementation of 
a "traditional hash table".



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


PDF with nonLatin-1 characters

2006-07-12 Thread victor
I want to generate a report and the PDF fits perfectly. Though there is 
an issue of using different encoding in the doc. I tried PyPS with no 
success. I need a lib that can make PDFs with an arbitrary set of fonts 
(possibly embed them into the document). What would you suggest?

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


Re: don't need dictionary's keys - hash table?

2006-07-12 Thread Diez B. Roggisch
[EMAIL PROTECTED] wrote:

> Hello,
> I am using some very large dictionaries with keys that are long strings
> (urls).  For a large dictionary these keys start to take up a
> significant amount of memory.  I do not need access to these keys -- I
> only need to be able to retrieve the value associated with a certain
> key, so I do not want to have the keys stored in memory.  Could I just
> hash() the url strings first and use the resulting integer as the key?
> I think what I'm after here is more like a tradition hash table. 

python dictionaries are "traditional" hash-tables.

> If I 
> do it this way am I going to get the memory savings I am after?  Will
> the hash function always generate unique keys?  Also, would the same
> technique work for a set?
> 
> Any other thoughts or considerations are appreciated.

You could try and create a md5 sum of your strings and use that as key. It
_should_ be good enough, but I'm no crypto expert so take that with a grain
of salt.

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


Re: What is a type error?

2006-07-12 Thread Marshall
Joachim Durchholz wrote:
> Marshall schrieb:
>
> > I can certainly see how DbC would be useful without subtyping.
> > But would there still be a reason to separate preconditions
> > from postconditions? I've never been clear on the point
> > of differentiating them (beyond the fact that one's covariant
> > and the other is contravariant.)
>
> There is indeed.
> The rules about covariance and contravariance are just consequences of
> the notion of having a subtype (albeit important ones when it comes to
> designing concrete interfaces).
>
> For example, if a precondition fails, something is wrong about the
> things that the subroutine assumes about its environment, so it
> shouldn't have been called. This means the error is in the caller, not
> in the subroutine that carries the precondition.
>
> The less important consequence is that this should be reflected in the
> error messages.
>
> The more important consequences apply when integrating software.
> If you have a well-tested library, it makes sense to switch off
> postcondition checking for the library routines, but not their
> precondition checking.
> This applies not just for run-time checking: Ideally, with compile-time
> inference, all postconditions can be inferred from the function's
> preconditions and their code. The results of that inference can easily
> be stored in the precompiled libraries.
> Preconditions, on the other hand, can only be verified by checking all
> places where the functions are called.

Interesting!

So, what exactly separates a precondition from a postcondition
from an invariant? I have always imagined that one writes
assertions on parameters and return values, and those
assertions that only reference parameters were preconditions
and those which also referenced return values were postconditions.
Wouldn't that be easy enough to determine automatically?

And what's an invariant anyway?


Marshall

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


Re: check type when assignment

2006-07-12 Thread Nick Vatamaniuc
pipe,

In general it is not possible in one line. You have to do it before
hand or after with an if statement.

As a sidenote though you probably shouldn't use isinstance(), you might
need it less than you think you do, especially if you are using it to
check for some interface. For example, do you really care if bc is of
_type_ klass or just that bc just implements method x, y, z and has
arguments a, b,c? The two things seem to be the same, because they are
the same in C++ or Java, but they are not the same in Python. In Python
an interface and type inheritance can be orthogonal to each other. One
could add methods to objects or change them at will after
instantication such that the type hierarchy would be preserved but the
interface would be broken, or alternatively you could have an object of
a different type that fully implements the interface and  could easily
be used in the code but just because it doesn't pass the isinstance()
test it is flagged as un-usable.
So in your code you could just test for the interface (methods and/or
attributes) with hasattr(bc, 'method') or even better just call the
method and see what happens, if it doesn't exist it will raise an
exception.

A much better explanation about the use and abuse of isinstance() is
here:
http://www.canonical.org/~kragen/isinstance/

Hope this helps,
Nick V.

pipehappy wrote:
> Hello everyone:
>
> Is there a way to check the type when do assignment?
>
> if I write:
> ab = bc
> and want to make sure the return value of isinstance(bc, klass) is True
> or I will raise
> a exception.
> 
> Any suggestion?

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


  1   2   >