Re: help: code formatter?

2007-01-08 Thread siggi
Bjoern wrote:

> Why don't you just write one? :)

Very funny! Just learning Python  :(

Regards,

siggi

> siggi wrote:
>
>> as a newbie I have problems with formatting code of downloaded
>> programs, because IDLE's reformatting capabilities are limited .
>> Incorrect indentation, mixing of TAB with BLANKs or eol are often
>> very nasty to correct.
>> Is there a simple code formatter that first removes all
>> indentations and then refomats correctly?
>
> Why don't you just write one? :)
>
> Seriously: Try.
>
> BTW: Guessing to what amount of space TABs must be converted in
> mixed source can be non-trivial.
>
> Regards,
>
>
> Björn
>
> -- 
> BOFH excuse #289:
>
> Interference between the keyboard and the chair.
> 


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

Re: Why less emphasis on private data?

2007-01-08 Thread Andrea Griffini
Steven D'Aprano wrote:

> That is total and utter nonsense and displays the most appalling
> misunderstanding of probability, not to mention a shocking lack of common
> sense.

While I agree that the programming job itself is not
a program and hence the "consider any possibility"
simply doesn't make any sense I can find a bit of
truth in the general idea that *in programs* it is
dangerous to be deceived by probability.

When talking about correctness (that should be the
main concern) for a programmer "almost never" means
"yes" and "almost always" means "not" (probability
of course for example kicks in about efficency).

Like I said however this reasoning doesn't work
well applied to the programming process itself
(that is not a program... as programmers are not
CPUs; no matter what bigots of software engineering
approaches are hoping for).
Private variables are about the programming process,
not the program itself; and in my experience the
added value of C++ private machinery is very low
(and the added cost not invisible).
When working in C++ I like much more using
all-public abstract interfaces and module-level
all-public concrete class definitions (the so
called "compiler firewall" idiom).

Another thing on the same "line of though" of
private members (that should "help programmers")
but for which I never ever saw *anything but costs*
is the broken idea of "const correctness" of C++.
Unfortunately that is not something that can be
avoided completely in C++, as it roots in the core
of the language.

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


Re: private variables

2007-01-08 Thread belinda thom
I knew it was a beehive, but I had hoped someone would know which  
version they were released, so I can put the proper statement into my  
tutorial (e.g. In version , Python provided some support for  
private variables...). I've been avoiding getting stung b/c I see  
both sides and have no preference for one vs. the other.

--b

On Jan 8, 2007, at 10:11 PM, Thomas Ploch wrote:

> belinda thom schrieb:
>> Hello,
>>
>> In what version of python were private variables added?
>>
>> Thanks,
>>
>> --b
>>
>
> With this question you stepped into a bee hive. :-)
>
> Read the 'Why less emphasis on private data?' thread.
>
> But I can't tell you, when this so called 'private variables' were  
> added.
>
> Thomas
> -- 
> http://mail.python.org/mailman/listinfo/python-list

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


Re: Colons, indentation and reformatting. (2)

2007-01-08 Thread Paddy

Paul McGuire wrote:

> "Paddy" <[EMAIL PROTECTED]> wrote in message
> news:[EMAIL PROTECTED]
> >I was just perusing a Wikipedia entry on the "off side rule" at
> > http://en.wikipedia.org/wiki/Off-side_rule .
> > It says that the colon in Python is purely for readability, and cites
> > our FAQ entry
> > http://www.python.org/doc/faq/general.html#why-are-colons-required-fo...
> > .
> > However, near the top of the Alternatives section, it states that for C
> > type, curly braces using languages:
> >  "An advantage of this is that program code can be automatically
> > reformatted and neatly indented without fear of the block structure
> > changing".
> >
> > Thinking about it a little, it seems that a colon followed by
> > non-indented code that has just been pasted in could also be used by a
> > Python-aware editor as a flag to re-indent the pasted code.
> >
> > Tell me it is not so, or I will be editing the Wikipedia page I think.
> >
> > - Paddy.
> >
> No, the ambiguity comes in when you have a nested construct within another
> nested construct. Here is some (fake) code where all the indentation was
> lost after pasting through a badly-behaved newsreader (this is NOT real
> code, I know that it wont really run, I'm just trying to demonstrate the
> indentation issue):
>
> while x:
> a = 100
> if b > 3:
> a += 1
> b += 1
>
> Here are some valid indented versions:
>
> while x:
> a = 100
> if b > 3:
> a += 1
> b += 1
>
> while x:
> a = 100
> if b > 3:
> a += 1
> b += 1
>
> while x:
> a = 100
> if b > 3:
> a += 1
> b += 1
>
> while x:
> a = 100
> if b > 3:
> a += 1
> b += 1
>
> The colons alone are not sufficient to tell us which is correct.
>
> -- Paul
Won't the following rules work when pasting complete Python statements
and complete lines, after other lines in an editor:

lets call the line after which the block is to be pasted the paste
line, and the original indent of the first line of the copied block to
be pasted the copy indent.

If the paste line ends in a colon then the copy indent must be greater
than the paste line indent, or the copy block should be re-indented on
pasting to make it so.
If the paste line does not end in a colon then the copy block indent
should be equal too or less than the paste line indent. If this is not
the case then the user should be asked wether to re-indent the copy
block to be equal to, or de-dented w.r.t. the paste line indent prior
to pasting.

- Paddy.

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


Re: How to write temporary data to file?

2007-01-08 Thread Thomas Ploch
Ravi Teja schrieb:
> Thomas Ploch wrote:
>> Ravi Teja schrieb:
>>> Thomas Ploch wrote:
 Hi folks,

 I have a data structure that looks like this:

 d = {
'url1': {
'emails': ['a', 'b', 'c',...],
'matches': ['d', 'e', 'f',...]
},
'url2': {...
 }

 This dictionary will get _very_ big, so I want to write it somehow to a
 file after it has grown to a certain size.

 How would I achieve that?

 Thanks,
 Thomas
>>> Pickle/cPickle are standard library modules that can persist data.
>>> But in this case, I would recommend ZODB/Durus.
>>>
>>> (Your code example scares me. I hope you have benevolent purposes for
>>> that application.)
>>>
>>> Ravi Teja.
>>>
>> Thanks, but why is this code example scaring you?
>>
>> Thomas
> 
> The code indicates that you are trying to harvest a _very_ (as you put
> it) large set of email addresses from web pages. With my limited
> imagination, I can think of only one group of people who would need to
> do that. But considering that you write good English, you must not be
> one of those mean people that needed me to get a new email account just
> for posting to Usenet :-).
> 
> Ravi Teja.
> 

Oh, well, yes you are right that this application is able to harvest
email addresses. But it can do much more than that. It has a text
matching engine, that according to given meta keywords can scan or not
scan documents in the web and harvest all kinds of information. It can
also be fed with callbacks for each of the Content-Types. I know that
the email matching engine is a kind of a 'grey zone', and I asked
myself, if it needs the email stuff. But I mean you could easily include
the email regex to the text matching engine yourself, so I decided to
add this functionality (it is 'OFF' by default :-) ).

Thomas

P.S.: No, I am a good person.

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


Re: class unbound method and datetime.datetime.today()

2007-01-08 Thread Diez B. Roggisch
[EMAIL PROTECTED] schrieb:
> Hi, I got confused when I learned the function datetime.today().
> 
> So far I learned, unless an instance is created, it is not possible to
> call the class method. For example:
> 
> class Foo:
>   def foo(self):
> pass
> 
> Foo.foo()  # error: unbound method foo().
> 
> What makes me confused is that datetime class? in datetime module
> provides today() function that returns the datetime object.
> 
 import datetime
 datetime.datetime.today()
> datetime.datetime(2007, 1, 9, 15, 34, 35, 23537)
> 
> It looks like that datetime class provides today() method that can be
> callable even if it is unbound method. Do I correct?
> 
> If it is possible to make that kind of function (looks like static
> member function in C++), how can I make that?

It is called a classmethod (in contrast to an instancemethod, which is 
the usual thing), and you can do it - depending on the version of python 
you have - using the built-in funtion/decorator "classmethod". Like this:

class Foo(object):

@classmethod
def bar(cls):
pass


Note that a classmethod gets passed the class as first argument, not an 
instance.

You can also create static methods, using "staticmethod". They won't get 
passed anything.

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


Re: Colons, indentation and reformatting. (2)

2007-01-08 Thread Paddy

[EMAIL PROTECTED] wrote:

> Paddy wrote:
> > I was just perusing a Wikipedia entry on the "off side rule" at
> > http://en.wikipedia.org/wiki/Off-side_rule .
> > It says that the colon in Python is purely for readability, and cites
> > our FAQ entry
> > http://www.python.org/doc/faq/general.html#why-are-colons-required-fo...
> >  .
> > However, near the top of the Alternatives section, it states that for C
> > type, curly braces using languages:
> >   "An advantage of this is that program code can be automatically
> > reformatted and neatly indented without fear of the block structure
> > changing".
> >
> > Thinking about it a little, it seems that a colon followed by
> > non-indented code that has just been pasted in could also be used by a
> > Python-aware editor as a flag to re-indent the pasted code.
>
> How would you re-indent this?
>
> if x>0: print x
If pasted as a line , after a line *ending with* a colon then indent it
w.r.t. previous line.
if pasting full lines after such a line then first pasted line cannot
be indented more than this line; if it is then flag for re-indenting
pasted block either equal too or less than this line.

- Paddy.

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


Re: Recommendations (or best practices) to define functions (or methods)

2007-01-08 Thread Frank Millman

Diez B. Roggisch wrote:
>
> If you don't handle the exceptions, exactly what you seem to want will
> happen - you will see the interpreter stop, and why.
>
> All you did was to take the unpythonic (and un-javaic and un-C#ic) road
> to transform an exception to a returncode. But that has nothing to do
> with actually _dealing_ with the error. Instead, it just makes it more
> likely that you _don't_ deal with it, as a return-code evaluation might
> be more easily forgotten, and turn out with a program that will be
> error-prone, as it continues to run even when the preconditions for
> pieces of code aren't met anymore.
>

I absolutely agree with this. I will describe my particular experience,
in the hope that it will make it clear to vizcayno just how important
this is.

My app does a lot of database/user interaction. I read a row from the
database, display it to the user, allow the user to change the value of
various columns, and write it back again.

I had a routine which validated whether the user input was acceptable
based on various business rules. If the input was good, it would update
the internal value of the column and return True, else it would not
update the value and return False. I would check the return code and,
if False, display an error message and reprompt the user for input.
What could go wrong with that?

Well, during the execution of the program, I also update the values of
various columns programatically. I call the same routine, but I did not
check the return code, as it would add a lot of extra checks, and as it
was program generated it would always pass a valid value.

This was true 99.9% of the time, but occasionally, due to a program
error, I would pass an invalid value. The validation routine correctly
did not update the internal value, and returned False, but as I did not
check this I continued merrily on my way. Later on when I found that
the value had not changed, it took me a long time to trace the error.

I suffered like this for quite a long time before the light bulb went
off, based on a thread on c.l.py. Now I do it like this.

The validation routine performs the same function, but instead of
returning True/False, it raises ValueError if the check fails. Where I
previously tested the return code, I now have a try/except clause,
which does the same as before.

The big difference is, what happens if I programmatically pass an
invalid value? Where before, the error would pass silently, now my
program aborts, with the traceback, which is exactly what I want.

Frank Millman

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


Re: re.sub and re.MULTILINE

2007-01-08 Thread Paddy

nyenyec wrote:

> I feel like a complete idiot but I can't figure out why re.sub won't
> match multiline strings:
>
> This works:
> >>> re.search("^foo", "\nfoo", re.MULTILINE)
> <_sre.SRE_Match object at 0x6c448>
>
> This doesn't. No replacement:
> >>> re.sub("^foo", "bar", "\nfoo", re.MULTILINE)
> '\nfoo'
>
> Why?
>
> Thanks,
> nyenyec

Check the arguments to re.sub.

>>> re.sub('(?m)^foo', 'bar', '\nfoo', count=0)
'\nbar'


- Paddy.

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


Re: Adding functions to classes after definition

2007-01-08 Thread Vasily Sulatskov
A nice guide to descriptors

http://users.rcn.com/python/download/Descriptor.htm

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


Re: How to write temporary data to file?

2007-01-08 Thread Ravi Teja

Thomas Ploch wrote:
> Ravi Teja schrieb:
> > Thomas Ploch wrote:
> >> Hi folks,
> >>
> >> I have a data structure that looks like this:
> >>
> >> d = {
> >>'url1': {
> >>'emails': ['a', 'b', 'c',...],
> >>'matches': ['d', 'e', 'f',...]
> >>},
> >>'url2': {...
> >> }
> >>
> >> This dictionary will get _very_ big, so I want to write it somehow to a
> >> file after it has grown to a certain size.
> >>
> >> How would I achieve that?
> >>
> >> Thanks,
> >> Thomas
> >
> > Pickle/cPickle are standard library modules that can persist data.
> > But in this case, I would recommend ZODB/Durus.
> >
> > (Your code example scares me. I hope you have benevolent purposes for
> > that application.)
> >
> > Ravi Teja.
> >
>
> Thanks, but why is this code example scaring you?
>
> Thomas

The code indicates that you are trying to harvest a _very_ (as you put
it) large set of email addresses from web pages. With my limited
imagination, I can think of only one group of people who would need to
do that. But considering that you write good English, you must not be
one of those mean people that needed me to get a new email account just
for posting to Usenet :-).

Ravi Teja.

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


Re: multi-threaded webcam with SimpleAsyncHTTPServer.py (updated)

2007-01-08 Thread Ray Schumacher
Better asyncore.loop use.
Also fixes a late bug in my first post of code: PILFile.seek(0) 
needed since PIL.save does not reset the pointer.


class ImageServer(RequestHandler):
 def __init__(self, conn, addr, server):
 asynchat.async_chat.__init__(self,conn)
 self.client_address = addr
 self.connection = conn
 self.server = server
 self.set_terminator ('\r\n\r\n')
 self.incoming = deque()
 self.outgoing = deque()
 self.rfile = None
 self.wfile = writewrapper(self.outgoing, -self.use_buffer or 
self.blocksize)
 self.found_terminator = self.handle_request_line
 self.request_version = "HTTP/1.1"
 self.code = None

 def send_head(self):
 buff, width, height = cam.dev.getbuffer()
 imPIL = (Image.frombuffer("RGB", (width, height), buff, 
"raw", "BGR", 0, -1) )
 PILFile.seek(0)
 imPIL.save(PILFile, "JPEG")
 self.send_response(200)
 self.send_header("Content-type", "image/jpeg")
 self.send_header("Content-Length: ", str(PILFile.len))
 self.end_headers()

 self.wfile.write(PILFile.getvalue())
 return
...

 parser.add_option('-3', dest='server',
   help='Run the server for cam images',
   action='store_const', const=3)
 options, args = parser.parse_args()
 if options.server==3:
 from StringIO import StringIO
 from PIL import Image
 if sys.platform == 'win32':
 import VideoCapture
 try:
 del(cam)
 gc.collect()
 print "deleted old cam instance"
 except: pass
 cam = VideoCapture.Device(devnum=options.devNum, 
showVideoWindow=0)
 print cam
 buff, width, height = cam.dev.getbuffer()
 PILFile = StringIO()
 else:
 pass
 # try 
http://laurent.pointal.org/python/projets/pyvideograb/index.pih
 # or fg
 #import fg
 #cam = fg.Grabber()
 #cam.set_source(options.devNum)
 # or video4linux.video_capture / v4lctl
 else:
 if options.root is None:
 parser.error("Need root path to start server")

 if not os.path.isdir(options.root):
 parser.error("Root path does not exist")

 os.chdir(options.root)
 req_handler = which[options.server]
 s=Server('',options.port,req_handler)
 print req_handler.__name__, "running on port", options.port, 
"with root path", options.root
 while True:
 try: asyncore.loop(timeout=1, count=1)
 except KeyboardInter

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


Re: How to write temporary data to file?

2007-01-08 Thread Thomas Ploch
Ravi Teja schrieb:
> Thomas Ploch wrote:
>> Hi folks,
>>
>> I have a data structure that looks like this:
>>
>> d = {
>>  'url1': {
>>  'emails': ['a', 'b', 'c',...],
>>  'matches': ['d', 'e', 'f',...]
>>  },
>>  'url2': {...
>> }
>>
>> This dictionary will get _very_ big, so I want to write it somehow to a
>> file after it has grown to a certain size.
>>
>> How would I achieve that?
>>
>> Thanks,
>> Thomas
> 
> Pickle/cPickle are standard library modules that can persist data.
> But in this case, I would recommend ZODB/Durus.
> 
> (Your code example scares me. I hope you have benevolent purposes for
> that application.)
> 
> Ravi Teja.
> 

Thanks, but why is this code example scaring you?

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


Re: Colons, indentation and reformatting. (2)

2007-01-08 Thread Paul McGuire
"Paddy" <[EMAIL PROTECTED]> wrote in message 
news:[EMAIL PROTECTED]
>I was just perusing a Wikipedia entry on the "off side rule" at
> http://en.wikipedia.org/wiki/Off-side_rule .
> It says that the colon in Python is purely for readability, and cites
> our FAQ entry
> http://www.python.org/doc/faq/general.html#why-are-colons-required-fo...
> .
> However, near the top of the Alternatives section, it states that for C
> type, curly braces using languages:
>  "An advantage of this is that program code can be automatically
> reformatted and neatly indented without fear of the block structure
> changing".
>
> Thinking about it a little, it seems that a colon followed by
> non-indented code that has just been pasted in could also be used by a
> Python-aware editor as a flag to re-indent the pasted code.
>
> Tell me it is not so, or I will be editing the Wikipedia page I think.
>
> - Paddy.
>
No, the ambiguity comes in when you have a nested construct within another 
nested construct. Here is some (fake) code where all the indentation was 
lost after pasting through a badly-behaved newsreader (this is NOT real 
code, I know that it wont really run, I'm just trying to demonstrate the 
indentation issue):

while x:
a = 100
if b > 3:
a += 1
b += 1

Here are some valid indented versions:

while x:
a = 100
if b > 3:
a += 1
b += 1

while x:
a = 100
if b > 3:
a += 1
b += 1

while x:
a = 100
if b > 3:
a += 1
b += 1

while x:
a = 100
if b > 3:
a += 1
b += 1

The colons alone are not sufficient to tell us which is correct.

-- Paul


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


Re: Colons, indentation and reformatting. (2)

2007-01-08 Thread [EMAIL PROTECTED]

Paddy wrote:
> I was just perusing a Wikipedia entry on the "off side rule" at
> http://en.wikipedia.org/wiki/Off-side_rule .
> It says that the colon in Python is purely for readability, and cites
> our FAQ entry
> http://www.python.org/doc/faq/general.html#why-are-colons-required-fo...
>  .
> However, near the top of the Alternatives section, it states that for C
> type, curly braces using languages:
>   "An advantage of this is that program code can be automatically
> reformatted and neatly indented without fear of the block structure
> changing".
>
> Thinking about it a little, it seems that a colon followed by
> non-indented code that has just been pasted in could also be used by a
> Python-aware editor as a flag to re-indent the pasted code.

How would you re-indent this?

if x>0: print x


>
> Tell me it is not so, or I will be editing the Wikipedia page I think.
> 
> - Paddy.

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


Re: How to write temporary data to file?

2007-01-08 Thread Ravi Teja

Thomas Ploch wrote:
> Hi folks,
>
> I have a data structure that looks like this:
>
> d = {
>   'url1': {
>   'emails': ['a', 'b', 'c',...],
>   'matches': ['d', 'e', 'f',...]
>   },
>   'url2': {...
> }
>
> This dictionary will get _very_ big, so I want to write it somehow to a
> file after it has grown to a certain size.
>
> How would I achieve that?
>
> Thanks,
> Thomas

Pickle/cPickle are standard library modules that can persist data.
But in this case, I would recommend ZODB/Durus.

(Your code example scares me. I hope you have benevolent purposes for
that application.)

Ravi Teja.

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


class unbound method and datetime.datetime.today()

2007-01-08 Thread cinsky
Hi, I got confused when I learned the function datetime.today().

So far I learned, unless an instance is created, it is not possible to
call the class method. For example:

class Foo:
  def foo(self):
pass

Foo.foo()  # error: unbound method foo().

What makes me confused is that datetime class? in datetime module
provides today() function that returns the datetime object.

>>> import datetime
>>> datetime.datetime.today()
datetime.datetime(2007, 1, 9, 15, 34, 35, 23537)

It looks like that datetime class provides today() method that can be
callable even if it is unbound method. Do I correct?

If it is possible to make that kind of function (looks like static
member function in C++), how can I make that?

Thanks in advance.

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


Re: Recommendations (or best practices) to define functions (or methods)

2007-01-08 Thread Diez B. Roggisch
vizcayno schrieb:
> Diez B. Roggisch ha escrito:
> 
>> vizcayno schrieb:
>>> Hello:
>>> Need your help in the "correct" definition of the next function. If
>>> necessary, I would like to know about a web site or documentation that
>>> tells me about best practices in defining functions, especially for
>>> those that consider the error exceptions management.
>>> I have the next alternatives but I think there are better:
>> 
>>
>> IMHO none of them is good. Python has exceptions. Use them. There is no
>> need to awkwardly communicate error conditions using return-values. Use
>> return values to return values. Use exceptions in case of errors.
>>
>> Diez
> 
> Diez, in that case I woul prefer not to use exceptions and wait for
> Python to abort itself and wait to see the message it issues.

You are not making sense here - to me at last.

If you don't handle the exceptions, exactly what you seem to want will 
happen - you will see the interpreter stop, and why.

Handling errors in a graceful way can't be done by a standardized way, 
as you seem to want. It depends to much on what the actual code is 
doing. Sometimes an abort is necessary, sometimes you can continue 
working - but it all depends on what your actual usecase is, on a very 
detailed level.

All you did was to take the unpythonic (and un-javaic and un-C#ic) road 
to transform an exception to a returncode. But that has nothing to do 
with actually _dealing_ with the error. Instead, it just makes it more 
likely that you _don't_ deal with it, as a return-code evaluation might 
be more easily forgotten, and turn out with a program that will be 
error-prone, as it continues to run even when the preconditions for 
pieces of code aren't met anymore.


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


Re: private variables

2007-01-08 Thread Ravi Teja

belinda thom wrote:
> Hello,
>
> In what version of python were private variables added?
>
> Thanks,
>
> --b

Short answer - 1.5 (or - so long ago that it doesn't matter anymore)
Long answer - There are no true private variables in Python. Just
private variables names by convention.

See Python docs for a detailed explanation.

Ravi Teja.

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


Colons, indentation and reformatting. (2)

2007-01-08 Thread Paddy
I was just perusing a Wikipedia entry on the "off side rule" at
http://en.wikipedia.org/wiki/Off-side_rule .
It says that the colon in Python is purely for readability, and cites
our FAQ entry
http://www.python.org/doc/faq/general.html#why-are-colons-required-fo...
 .
However, near the top of the Alternatives section, it states that for C
type, curly braces using languages:
  "An advantage of this is that program code can be automatically
reformatted and neatly indented without fear of the block structure
changing".

Thinking about it a little, it seems that a colon followed by
non-indented code that has just been pasted in could also be used by a
Python-aware editor as a flag to re-indent the pasted code.

Tell me it is not so, or I will be editing the Wikipedia page I think.

- Paddy.

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


Colons, indentation and reformatting.

2007-01-08 Thread Paddy
i was just perusing a Wikipedia entry on the "off side rule" at
http://en.wikipedia.org/wiki/Off-side_rule .
It says that the colon in Python is purely for readability, and cites
our FAQ entry
http://www.python.org/doc/faq/general.html#why-are-colons-required-for-the-if-while-def-class-statements
 .
However, near the top of the Alternatives section, it states that for C
type, curly braces using languages:
  "An advantage of this is that program code can be automatically
reformatted and neatly indented without fear of the block structure
changing".

Thinking about it a little, it seems that a colon followed by
non-indented code that has just been pasted in could also be used by a
Python-aware editor as a flag to re-indent the pasted code.

Tell me it is not so, or I will be editing the Wikipedia page I think.
And if it is true then do we need to update the FAQ?

- Paddy.

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


Re: private variables

2007-01-08 Thread Thomas Ploch
belinda thom schrieb:
> Hello,
> 
> In what version of python were private variables added?
> 
> Thanks,
> 
> --b
> 

With this question you stepped into a bee hive. :-)

Read the 'Why less emphasis on private data?' thread.

But I can't tell you, when this so called 'private variables' were added.

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


private variables

2007-01-08 Thread belinda thom
Hello,

In what version of python were private variables added?

Thanks,

--b

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


Re: multi-threaded webcam with SimpleAsyncHTTPServer.py

2007-01-08 Thread Ray Schumacher
 >>Question, though: how can I unblock asyncore.loop(), or at least 
be >>able to interrupt it?
 >Why do you want to do that?

I was then thinking along the lines of a Netmeeting/visual chat 
program, rather than a daemon-type server, where one might want to 
terminate more quickly.
Searching further, I see that a common idiom is like:

while some_condition:
 try: asyncore.loop(timeout=1, count = 1)
 except KeyboardInterrupt: break

or, spawn asyncore.loop() threads. Zope and Medusa seem to use a 
variety of methods.
A regular web-cam program serving up 2 or 3 cams on one machine will 
require more thought on my part, of course. I only run one cam, but a 
friend runs 3 and is pissed at the X10 software he paid for.
Thanks,
Ray

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


Re: Getting started with Crystal Reports...little help in the far court.

2007-01-08 Thread Brian
Mudcat wrote:
> I am not that familiar with Crystal Reports, but having read some other
> posts I know that the way to integrate the API with Python is through
> the COM interface provide by win32all.

> Any pointers in the right direction would be helpful.

Like Armury, I worked on Crystal stuff quite a long time ago.  Here's a
script I was able to dig up.  Edited to protect the innocent.  ;-)

import sys
from win32com.client import Dispatch

app = Dispatch('CrystalRunTime.Application')
rep = app.OpenReport('foo.rpt')
tbl = rep.Database.Tables.Item(1)

prop = tbl.ConnectionProperties('Password')
prop.Value = sys.argv[1]

prop = tbl.ConnectionProperties('Data Source')
prop.Value = 'server'

# tbl.TestConnectivity() should return 1

# clear and set 3 parameters
params = rep.ParameterFields
p1 = params(1)
p2 = params(2)
p3 = params(3)

for param in (p1,p2,p3): param.ClearCurrentValueAndRange()

p1.AddCurrentValue(123)
p2.AddCurrentValue(456)
p3.AddCurrentValue('12/31/99')

rep.PrintOut(promptUser=False)

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


re.sub and re.MULTILINE

2007-01-08 Thread nyenyec
I feel like a complete idiot but I can't figure out why re.sub won't
match multiline strings:

This works:
>>> re.search("^foo", "\nfoo", re.MULTILINE)
<_sre.SRE_Match object at 0x6c448>

This doesn't. No replacement:
>>> re.sub("^foo", "bar", "\nfoo", re.MULTILINE)
'\nfoo'

Why?

Thanks,
nyenyec

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


re.sub and re.MULTILINE

2007-01-08 Thread nyenyec
I feel like a complete idiot but I can't figure out why re.sub won't
match multiline strings:

This works:
>>> re.search("^foo", "\nfoo", re.MULTILINE)
<_sre.SRE_Match object at 0x6c448>

This doesn't. No replacement:
>>> re.sub("^foo", "bar", "\nfoo", re.MULTILINE)
'\nfoo'

Why?

Thanks,
nyenyec

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


How to write temporary data to file?

2007-01-08 Thread Thomas Ploch
Hi folks,

I have a data structure that looks like this:

d = {
'url1': {
'emails': ['a', 'b', 'c',...],
'matches': ['d', 'e', 'f',...]
},
'url2': {...
}

This dictionary will get _very_ big, so I want to write it somehow to a
file after it has grown to a certain size.

How would I achieve that?

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


Evolving doctests for changing output format

2007-01-08 Thread nriley
Hi,

I've got a number of doctests which rely on a certain output format,
and since I wrote the tests I've changed the output format.  Now,
almost all the tests fail.

What I'd like is if I could get doctest to take my tests, and
substitute the obtained output for the provided output.  Then, I could
in this case just replace the doctest file with the generated file, or
in general be able to run it through a file comparison tool so I could
examine the differences.

If it's not clear from the above, here's an example, assuming for the
sake of simplicity that Python somehow changed its default output base
between runs.

INPUT:
>> 10 + 10
20

OUTPUT:
>> 10 + 10
0x14

Is it possible to use doctest in such a "run-only" mode, or script the
above using its API?  I read the documentation several times but just
got further confused (sorry!).

Thanks,

--Nicholas

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


Re: Parallel Python

2007-01-08 Thread Parallel Python The team

On 1/8/07, Laszlo Nagy <[EMAIL PROTECTED]> wrote:


I always thought that if you use multiple processes (e.g. os.fork) then
Python can take advantage of multiple processors. I think the GIL locks
one processor only. The problem is that one interpreted can be run on
one processor only. Am I not right? Is your ppm module runs the same
interpreter on multiple processors? That would be very interesting, and
something new.


Or does it start multiple interpreters? Another way to do this is to
start multiple processes and let them communicate through IPC or a local
network.


  Laszlo

You are right. ppsmp start multiple interpreters in separate processes and

organize communication between them through IPC.

So far ppsmp features load balancing (distributes workload evenly between
worker processes.) and low overhead (example
http://www.parallelpython.com/content/view/17/31/#REVERSE_MD5 submits a 100
jobs to the system with no noticeable overhead).
Of coerce there is always room for growth and I am considering adding new
features/functionality.
Do you have any functionality in mind which you want to see in this system?

Best regards,
Vitalii
-- 
http://mail.python.org/mailman/listinfo/python-list

Re: Fwd: Execute binary code

2007-01-08 Thread Chris Mellon
On 1/8/07, Gabriel Genellina <[EMAIL PROTECTED]> wrote:
> At Monday 8/1/2007 18:01, [EMAIL PROTECTED] wrote:
>
> >Chris Mellon wrote:
> > > Writing to a temp file will be at least 3 times as easy and twice as
> > > reliable as any other method you come up with.
> >
> >I'm not disputing that, but I want to keep a piece of code (a parser
> >for Oracle binary dumps, that I didn't wrote) out of foreign hands, as
> >much as possible. Using a TEMP directory is not "stealth" enough.
>
> This is what I would do  (untested of course!) (Mostly using the
> Win32 API so you'll have to use pywin32 or ctypes).
>
> Call CreateFile with dwShareMode=0, FILE_ATTRIBUTE_TEMPORARY,
> FILE_FLAG_NO_BUFFERING, FILE_FLAG_DELETE_ON_CLOSE.
> That means that no other process could open the file, if it fits in
> available memory probably it won't even be written to disk, and it
> will be deleted as soon as it has no more open handles. File name
> does not have to end in .exe.
> Copy the desired contents into a buffer obtained from VirtualAlloc;
> then call WriteFile; release the buffer (rounding size up to next 4KB 
> multiple)
> Then CreateProcess with CREATE_SUSPENDED, and CloseHandle on the
> file, and CloseHandle on the two handles returned on
> PROCESS_INFORMATION. At this stage, the only open handle to the
> temporary file is held by the section object inside the process.
> Then ResumeThread(hTread) -process begins running- and
> WaitForSingleObject(hProcess) -wait until finishes-.
> As soon as it finishes execution, the last handle to the file is
> closed and it is deleted.
>
> Another approach would be to go below the Windows API and use the
> native API function NtCreateProcess -officially undocumented- which
> receives a section handle (which does not have to be disk based). But
> this interfase is undocumented and known to change between Windows versions...
>
> Or search for a rootkit...
>
>
> --
> Gabriel Genellina
> Softlab SRL
>

Thats a lot of work to execute a binary image that can be trivially
recovered from the python source with 2 minutes of work (up to 15 if
you have to install Python and google for how to write to a file
first).
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Adding functions to classes after definition

2007-01-08 Thread Gabriel Genellina

At Monday 8/1/2007 21:47, Gerard Brunick wrote:


Consider:  A)
 >>> class C(object):
... pass
...
 >>> def f(*args):
... print args
...
 >>> C.f = f
 >>> C.f

 >>> c=C()
 >>> c.f()
(<__main__.C object at 0x04A51170>,)

And B)

 >>> del c
 >>> C.f = types.MethodType(f, None, C)
 >>> C.f

 >>> c = C()
 >>> c.f()
(<__main__.C object at 0x04A51290>,)

I don't understand A).  It is my vague understanding, that methods are
really properties that handle binding on attribute access, so B) should
be the "right" way to add a method to a class after definition.


This is implemented using descriptors. A function object has a __get__ method:

py> f.__get__

py> C.__dict__['f'] is f
True
py> C.f is f
False
py> C.f.im_func is f
True

So, when you assign C.f=f, nothing special happens; but when the 
function is retrieved from the class, the __get__ method is invoked, 
returning an "unbound method".



  Why does
A show up as a method?  Shouldn't it still just be a function?  Certainly
when you define a class, there is some magic in the __new__ method that
turns functions in the initial dictionary into methods, but does this still
happen for all setattr after that?


There's nothing special in __new__ (relating to this, at least). Nor 
even when setting the attribute; the magic happens when you *get* the 
method as an attribute of the class object. Functions don't even have 
a __set__:


py> f.__set__
Traceback (most recent call last):
  File "", line 1, in ?
AttributeError: 'function' object has no attribute '__set__'



Is is possible to set a class attribute
equal to a regular (types.FunctionType) function?


Yes, that's what actually happens. It's not easy to *retrieve* it 
later without getting a MethodType.



Any references that discuss these issues would be greatly appreciated.


Descriptors are documented somewhere... I think they came in Python 
2.2 along with new-style classes.



--
Gabriel Genellina
Softlab SRL 







__ 
Preguntá. Respondé. Descubrí. 
Todo lo que querías saber, y lo que ni imaginabas, 
está en Yahoo! Respuestas (Beta). 
¡Probalo ya! 
http://www.yahoo.com.ar/respuestas 

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

Re: Non-blocking pipes during subprocess handling

2007-01-08 Thread Gabriel Genellina

At Monday 8/1/2007 22:09, Tom Plunket wrote:


I'm using subprocess to launch, well, sub-processes, but now I'm
stumbling due to blocking I/O.

Is there a way for me to know that there's data on a pipe, and possibly
how much data is there so I can get it?  Currently I'm doing this:


Using a thread for each stream is the safest way, specially if you 
can't control the child process.



--
Gabriel Genellina
Softlab SRL 







__ 
Preguntá. Respondé. Descubrí. 
Todo lo que querías saber, y lo que ni imaginabas, 
está en Yahoo! Respuestas (Beta). 
¡Probalo ya! 
http://www.yahoo.com.ar/respuestas 

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

Re: Network failure when using urllib2

2007-01-08 Thread Ravi Teja

[EMAIL PROTECTED] wrote:
> I am fetching different web pages (never the same one) from a web
> server.  Does that make a difference with them trying to block me?
> Also, if it was only that site blocking me, then why does the internet
> not work in other programs when this happens in the script.  It is
> almost like something is seeing a lot of traffic from my computer, and
> cutting it off thinking it is some kind of virus or worm.  I am
> starting to suspect my firewall.  Anyone else have this happen?
>
> I am going to read over that documentation you suggested to see if I
> can get any ideas.  Thanks for the link.
>
> Shuad

No! What I suggested should not effect traffic from other servers. I
would go with Gabriel's suggestion and check for open connections just
in case. Although I can't imagine why that would give you a 404
response since it is a server response (implies successful connection).
I would expect that you would get a client error in such a case.

Of course, you can always rule out your suspicions of local conditions
(turn off security software briefly or try from a different machine)
unless your ISP is implementing safeguards against DOS attacks from
their network with normal users in mind.

Ravi Teja.

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


Non-blocking pipes during subprocess handling

2007-01-08 Thread Tom Plunket
I'm using subprocess to launch, well, sub-processes, but now I'm
stumbling due to blocking I/O.

Is there a way for me to know that there's data on a pipe, and possibly
how much data is there so I can get it?  Currently I'm doing this:

process = subprocess.Popen(
args,
bufsize=1,
universal_newlines=True,
stdout=subprocess.PIPE,
stderr=subprocess.PIPE)

def ProcessOutput(instream, outstream):
text = instream.readline()
if len(text) > 0:
print >>outstream, text,
return True
else:
return False

while process.poll() is None:
ProcessOutput(process.stdout, sys.stdout)
ProcessOutput(process.stderr, sys.stderr)

# clean up everything to EOF once the process ends.
somethingPrinted = True
while somethingPrinted:
somethingPrinted = ProcessOutput(
process.stdout, sys.stdout)
somethingPrinted |= ProcessOutput(
process.stderr, sys.stderr)


Unfortunately, stream.readline will block 'til it gets a line, and
typically there won't be anything on the stderr stream.  The reason for
the redirections in the first place is that I'm launching this script as
a subprocess from a GUI app that catches stdout and stderr and directs
the output to the appropriate windows, but in some cases I don't
actually want the output at all (I've removed that logic though since it
needlessly complicates my example; suffice to say everything below the
process = subprocess.Popen... line is enclosed in a try and then in an
if block.

The documentation on file.read() indicate that there's an option for
"non-blocking" mode, but I'm stumped as to how to even look for how to
enable and use that.

thanks,
-tom!

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


Adding functions to classes after definition

2007-01-08 Thread Gerard Brunick
Consider:  A)
 >>> class C(object):
... pass
...
 >>> def f(*args):
... print args
...
 >>> C.f = f
 >>> C.f

 >>> c=C()
 >>> c.f()
(<__main__.C object at 0x04A51170>,)

And B)

 >>> del c
 >>> C.f = types.MethodType(f, None, C)
 >>> C.f

 >>> c = C()
 >>> c.f()
(<__main__.C object at 0x04A51290>,)

I don't understand A).  It is my vague understanding, that methods are
really properties that handle binding on attribute access, so B) should
be the "right" way to add a method to a class after definition.  Why does
A show up as a method?  Shouldn't it still just be a function?  Certainly
when you define a class, there is some magic in the __new__ method that
turns functions in the initial dictionary into methods, but does this still
happen for all setattr after that?  Is is possible to set a class attribute
equal to a regular (types.FunctionType) function?

Any references that discuss these issues would be greatly appreciated.

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


Re: Network failure when using urllib2

2007-01-08 Thread Gabriel Genellina

At Monday 8/1/2007 21:30, [EMAIL PROTECTED] wrote:


I am fetching different web pages (never the same one) from a web
server.  Does that make a difference with them trying to block me?
Also, if it was only that site blocking me, then why does the internet
not work in other programs when this happens in the script.  It is
almost like something is seeing a lot of traffic from my computer, and
cutting it off thinking it is some kind of virus or worm.  I am
starting to suspect my firewall.  Anyone else have this happen?


Perhaps you're not closing connections once finished?
Try netstat -an from the command line and see how many open 
connections you have.



--
Gabriel Genellina
Softlab SRL 







__ 
Preguntá. Respondé. Descubrí. 
Todo lo que querías saber, y lo que ni imaginabas, 
está en Yahoo! Respuestas (Beta). 
¡Probalo ya! 
http://www.yahoo.com.ar/respuestas 

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

Re: Bitwise expression

2007-01-08 Thread [EMAIL PROTECTED]

Gigs_ wrote:
> Can someone explain me bitwise expression?
> few examples for every expression will be nice
>
> x << y Left shift
> x >> y Right shift
> x & y Bitwise AND
> x | y Bitwise OR
> x ^ y Bitwise XOR (exclusive OR)
> ~x Bitwise negation
>
>
> thanks people


Here's some examples:

##What is BINARY?
##
## n   base 2
##-- 
## 0 
## 1 0001
## 2 0010
## 3 0011
## 4 0100
## 5 0101
## 6 0110
## 7 0111
## 8 1000
## 9 1001
##10 1010
##11 1011
##12 1100
##13 1101
##14 1110
##15 
##
##
##What does << do?
##
##0111
## << 0100
##
##0111
##
##What does >> do?
##
##00100100
## >> 0010
##
##1001
##
##What does & do?
##
##00011011
##  & 1001
##
##1001
##
##00011011
##  & 1110
##
##1010
##
##What does | do?
##
##00010001
##  | 1010
##
##00011011
##
##00011011
##  | 00010001
##
##00011011
##
##What does ^ do?
##
##00011011
##  ^ 0001
##
##0100
##
##0001
##  ^ 1110
##
##00010001
##
##Bitwise demo: the Collatz Conjecture
##
##41 31 47 71 107 161 121 91 137 103 155 233
##175 263 395 593 445 167 251 377 283 425 319
##479 719 1079 1619 2429 911 1367 2051 3077
##577 433 325 61 23 35 53 5 1


bitwise.py

import gmpy  # has lots of neat bitwise operations
 # not found in standard Python

def convert_to_binary(n,bits):
s = gmpy.digits(n,2)   # base 2 conversion
s = '0'*(bits-len(s)) + s  # add leading 0's
return s

def report(n,m,o,p):
print '%s' % (n)
print '%3s %s' % (o,m)
print ''
print '%s' % (p)
print

def Collatz(n):
# if n is even, divide by 2
# if n is odd, multiply by 3 and add 1
# Collat Conjecture: n always reaches 1
while n>1:
# find bit position of LS 1 bit
f = gmpy.scan1(n)
if f == 0:   # then n is odd
n = n*3 + 1
else:# n is even
# remove all factors of 2 in one fell swoop
n = n >> f
print n,
print


print 'What is BINARY?'

print """
 n   base 2
-- """
for n in xrange(16):
print '%2d %s' % (n,convert_to_binary(n,8))


print
print

print 'What does << do?'
print

report(convert_to_binary(7,8), \
   convert_to_binary(4,8), \
   '<<', \
   convert_to_binary(7<<4,8))

print 'What does >> do?'
print

report(convert_to_binary(36,8), \
   convert_to_binary(2,8), \
   '>>', \
   convert_to_binary(36>>2,8))

print 'What does & do?'
print

report(convert_to_binary(27,8), \
   convert_to_binary(9,8), \
   '&', \
   convert_to_binary(27&9,8))

report(convert_to_binary(27,8), \
   convert_to_binary(14,8), \
   '&', \
   convert_to_binary(27&14,8))

print 'What does | do?'
print

report(convert_to_binary(17,8), \
   convert_to_binary(10,8), \
   '|', \
   convert_to_binary(17|10,8))

report(convert_to_binary(27,8), \
   convert_to_binary(17,8), \
   '|', \
   convert_to_binary(27|17,8))

print 'What does ^ do?'
print

report(convert_to_binary(27,8), \
   convert_to_binary(31,8), \
   '^', \
   convert_to_binary(27^31,8))

report(convert_to_binary(31,8), \
   convert_to_binary(14,8), \
   '^', \
   convert_to_binary(31^14,8))

print 'Bitwise demo: the Collatz Conjecture'
print
Collatz(27)

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


Re: Network failure when using urllib2

2007-01-08 Thread jdvolz
I am fetching different web pages (never the same one) from a web
server.  Does that make a difference with them trying to block me?
Also, if it was only that site blocking me, then why does the internet
not work in other programs when this happens in the script.  It is
almost like something is seeing a lot of traffic from my computer, and
cutting it off thinking it is some kind of virus or worm.  I am
starting to suspect my firewall.  Anyone else have this happen?

I am going to read over that documentation you suggested to see if I
can get any ideas.  Thanks for the link.

Shuad

On Jan 8, 4:15 pm, "Ravi Teja" <[EMAIL PROTECTED]> wrote:
> [EMAIL PROTECTED] wrote:
> > I have a script that uses urllib2 to repeatedly lookup web pages (in a
> > spider sort of way).  It appears to function normally, but if it runs
> > too long I start to get 404 responses.  If I try to use the internet
> > through any other programs (Outlook, FireFox, etc.) it will also fail.
> > If I stop the script, the internet returns.
>
> > Has anyone observed this behavior before?  I am relatively new to
> > Python and would appreciate any suggestions.
>
> > ShuadI am assuming that you are fetching the full page every little while.
> You are not supposed to do that. The admin of the web site you are
> constantly hitting probably configured his server to block you
> temporarily when that happens. But don't feel bad :-). This is a common
> Beginners mistake.
>
> Read here on the proper way to do 
> this.http://diveintopython.org/http_web_services/review.html
> especially 11.3.3. Last-Modified/If-Modified-Since in the next page
> 
> Ravi Teja.

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


Re: Maths error

2007-01-08 Thread Dan Bishop
On Jan 8, 3:30 pm, Rory Campbell-Lange <[EMAIL PROTECTED]> wrote:
> >>> (1.0/10.0)  + (2.0/10.0) + (3.0/10.0)
> 0.60009
> >>> 6.0/10.0
> 0.59998
>
> Is using the decimal module the best way around this? (I'm expecting the first
> sum to match the second).

Probably not.  Decimal arithmetic is NOT a cure-all for floating-point
arithmetic errors.

>>> Decimal(1) / Decimal(3) * Decimal(3)
Decimal("0.")
>>> Decimal(2).sqrt() ** 2
Decimal("1.999")

> It seem anachronistic that decimal takes strings as
> input, though.

How else would you distinguish Decimal('0.1') from
Decimal('0.155511151231257827021181583404541015625')?

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


Re: Network failure when using urllib2

2007-01-08 Thread Ravi Teja

[EMAIL PROTECTED] wrote:
> I have a script that uses urllib2 to repeatedly lookup web pages (in a
> spider sort of way).  It appears to function normally, but if it runs
> too long I start to get 404 responses.  If I try to use the internet
> through any other programs (Outlook, FireFox, etc.) it will also fail.
> If I stop the script, the internet returns.
>
> Has anyone observed this behavior before?  I am relatively new to
> Python and would appreciate any suggestions.
>
> Shuad

I am assuming that you are fetching the full page every little while.
You are not supposed to do that. The admin of the web site you are
constantly hitting probably configured his server to block you
temporarily when that happens. But don't feel bad :-). This is a common
Beginners mistake.

Read here on the proper way to do this.
http://diveintopython.org/http_web_services/review.html
especially 11.3.3. Last-Modified/If-Modified-Since in the next page

Ravi Teja.

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


Re: Maths error

2007-01-08 Thread Gabriel Genellina

At Monday 8/1/2007 19:20, Bjoern Schliessmann wrote:


Rory Campbell-Lange wrote:

> Is using the decimal module the best way around this? (I'm
> expecting the first sum to match the second). It seem
> anachronistic that decimal takes strings as input, though.
[...]
Also check the recent thread "bizarre floating point output".


And the last section on the Python Tutorial "Floating Point 
Arithmetic: Issues and Limitations"



--
Gabriel Genellina
Softlab SRL 







__ 
Preguntá. Respondé. Descubrí. 
Todo lo que querías saber, y lo que ni imaginabas, 
está en Yahoo! Respuestas (Beta). 
¡Probalo ya! 
http://www.yahoo.com.ar/respuestas 

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

Re: Fwd: Execute binary code

2007-01-08 Thread Gabriel Genellina

At Monday 8/1/2007 18:01, [EMAIL PROTECTED] wrote:


Chris Mellon wrote:
> Writing to a temp file will be at least 3 times as easy and twice as
> reliable as any other method you come up with.

I'm not disputing that, but I want to keep a piece of code (a parser
for Oracle binary dumps, that I didn't wrote) out of foreign hands, as
much as possible. Using a TEMP directory is not "stealth" enough.


This is what I would do  (untested of course!) (Mostly using the 
Win32 API so you'll have to use pywin32 or ctypes).


Call CreateFile with dwShareMode=0, FILE_ATTRIBUTE_TEMPORARY, 
FILE_FLAG_NO_BUFFERING, FILE_FLAG_DELETE_ON_CLOSE.
That means that no other process could open the file, if it fits in 
available memory probably it won't even be written to disk, and it 
will be deleted as soon as it has no more open handles. File name 
does not have to end in .exe.
Copy the desired contents into a buffer obtained from VirtualAlloc; 
then call WriteFile; release the buffer (rounding size up to next 4KB multiple)
Then CreateProcess with CREATE_SUSPENDED, and CloseHandle on the 
file, and CloseHandle on the two handles returned on 
PROCESS_INFORMATION. At this stage, the only open handle to the 
temporary file is held by the section object inside the process.
Then ResumeThread(hTread) -process begins running- and 
WaitForSingleObject(hProcess) -wait until finishes-.
As soon as it finishes execution, the last handle to the file is 
closed and it is deleted.


Another approach would be to go below the Windows API and use the 
native API function NtCreateProcess -officially undocumented- which 
receives a section handle (which does not have to be disk based). But 
this interfase is undocumented and known to change between Windows versions...


Or search for a rootkit...


--
Gabriel Genellina
Softlab SRL 







__ 
Preguntá. Respondé. Descubrí. 
Todo lo que querías saber, y lo que ni imaginabas, 
está en Yahoo! Respuestas (Beta). 
¡Probalo ya! 
http://www.yahoo.com.ar/respuestas 

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

RE: File Closing Problem in 2.3 and 2.4, Not in 2.5

2007-01-08 Thread Carroll, Barry


John:

<<>
> 
> Hi Barry,
> 
> Please always reply on-list if the communication is non-private -- and
> meaningful :-)

Right. I noticed I hadn't "replied-to-all", so I resent the post to the
mailing list.  Slip of the mouse, no disrespect intended to the list ;^)

> 
> I don't know; here are some things you could check out:
> 
> (1) Windows XP versus *x: The limit on open file handles is 512 in
> Windows XP; write yourself a reference-preserving [hint: append to a
> list] multiple file opener on *x and see how many you get open. I have
> vague recollections of the number 1024 being mentioned.

By rough calculation, we had been through over 500 table invocations
when the error message started showing up.  That fits well enough with
the number of Windows file handles.  I'll check the Linux value.
> 
> (2) Python 2.4 versus 2.5: Read the "what's new" doc, fire up your
> googler, ...

I read through the 2.5 release notes and didn't see anything that looked
related.  I may have been looking for the wrong thing.  I'll check
again.  

> IMHO, the attitude should be to program defensively so that you need
> neither to know nor care about such implementation-specific concerns.
> Operating systems these days give you enough file handles to do any
> reasonable job. It's many moons since MS-DOS 2.n with its max 20 open
> file handles made folks implement LRU caches for file handles.
> 
> Simple rules about unwanted objects include:
> (1) If an object has a free() or close() method, then call it!
> (2) Unbind *all* references to the object.

I tend to agree with you.  I am not the lead programmer on this project,
however, so my preferences do not always get implemented.  This time,
though, as I said, the explicit file close has been retained.  

> Have you checked out my point 2 (holding multiple references to your
> Parser objects)?

Not yet, but I will.  

> 
> Cheers,
> John

Again, thanks.  

Regards,
 
Barry
[EMAIL PROTECTED]
541-302-1107

We who cut mere stones must always be envisioning cathedrals.

-Quarry worker's creed


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


Re: Execute binary code

2007-01-08 Thread Bjoern Schliessmann
[EMAIL PROTECTED] wrote:

> The code I try to execute is Windows specific and it is binary,
> not python. Furthermore, it is stored in a variable within the
> parent python script, not stored on harddisk as a file.

Sure, I just wanted to show that your special application is not
specific for trojan horses oder viruses. One could achieve similar
replication functionality with python by itself.

Regards,


Björn

-- 
BOFH excuse #217:

The MGs ran out of gas.

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


tkinter with wck

2007-01-08 Thread [EMAIL PROTECTED]
I am playing around with this code but I am having trouble getting my x
to be bigger it only seems to redraw when it becomes smaller than the
original size up to full size (in ui_handle_repair).  At other window
sizes it just seems to center it..  I tried playing around with resize
but never got that to do anything...  any ideas (I might be willing to
try other toolkits but the example looks very close to what I was
after)  http://www.dexrow.com

from Tkinter import Tk
from WCK import Widget

#from WCK import Widget

class CrossWidget(Widget):

def ui_handle_repair(self, draw, x0, y0, x1, y1):
black = self.ui_pen("black", 5)

# draw a black cross
draw.line((x0, y0, x1, y1), black)
draw.line((x0, y1, x1, y0), black)

def ui_handle_resize(self, width,height):

black = self,ui_pen("black", 5)
#draw.line((x0, y0, x1, y1), black)
#draw.line((x0, y1, x1, y0), black)
# center the text




root = Tk()

widget = CrossWidget(root)
widget.pack() #(expand=1, fill="both")

root.mainloop()

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


error with "import md5"

2007-01-08 Thread John Pye
Hi all,

I'm doing some convoluted stuff with running a python script from
inside a shared library that's running inside a Tcl/Tk interpreter.

It's all been going surprisingly well, up until the point where my
Python script attempts to import matplotlib, which includes a reference
to import md5:

I get this error bubbling through:

Traceback (most recent call last):
  File "/home/john/ascend/models/johnpye/extpy/extpytest.py", line 5,
in ?
import matplotlib;
  File "/usr/lib/python2.4/site-packages/matplotlib/__init__.py", line
150, in ?
import md5, os, re, shutil, sys, warnings
ImportError: /usr/lib/python2.4/lib-dynload/md5.so: undefined symbol:
_Py_NoneStruct

Is it possible that this could be a bug in the md5 library (Python
2.4.4c1), or does it look like a problem perhaps with RTDL_NOW |
RTDL_LAZY stuff?

Cheers
JP

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


Re: Getting started with Crystal Reports...little help in the far court.

2007-01-08 Thread Amaury Forgeot d'Arc
Good evening,

Mudcat a écrit :
> I am not that familiar with Crystal Reports, but having read some other
> posts I know that the way to integrate the API with Python is through
> the COM interface provide by win32all.
> 
> However, I have been unable to find any other information on how to get
> started. I've used the COM interface before in integrating Excel and a
> couple of other things. So I am familiar with how that works. But there
> are at least 40 options dealing with Crystal and Business Objects. I
> have no idea which makepy file to create or which one provides the
> functionality I need.
> 
> I'm not looking to do much. All I'm really trying to do is provide one
> application where a list of crystal reports can be selected and ran in
> series. Right now we have a lot of reports that all have to be run
> manually (takes a while). So I think all I need api access to is server
> selection, parameter selection, and output formats.
> 
> Any pointers in the right direction would be helpful.


In my previous job we had to to almost the same thing.
If I remember correctly, for batch printing or file export it was enough 
to start with the "CrystalRuntime.Application" class.

It was something along these lines (sorry I don't have any way to test 
it now.):
 app = win32com.client.dynamic.Dispatch("CrystalRuntime.Application")
 report = app.OpenReport("c:/path/to/file.rpt")
 for table in report.Database.Tables:
 table.ConnectionInfo.Password = "passwd"
 ...
The rest is modeled after Visual Basic. There are tons of examples on 
the net.

If you want to show the report on the screen then it is another story.
I only remember the following:
- the application must be a mfc application
- I had to "makepy" a class. I think it was "CrystalReportsViewer".
- create a class derived from both pywin.mfc.activex.Control and 
CrViewer (look at the script generated by makepy).
- create a pywin.mfc.Frame, and put there an instance of the previous class.

Voilà, it's not much.
In the hope that you can do something with it.
But don't give up. At the end, it works...

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


Re: Why less emphasis on private data?

2007-01-08 Thread sturlamolden

Jussi Salmela wrote:

> To surlamolden: I don't know how you define private, but if one defines
> in C an external static variable i.e. a variable outside any functions,
> on the file level, the scope of the variable is that file only.

Sure, in C you can hide instances inside an object image by declaring
them static. But the real virtue of static declarations is to assist
the compiler.

My definition of 'private' for this thread is the private attribute
provided by C++, Java and C#. When I program C I use another idiom,

   /* THIS IS MINE, KEEP YOUR PAWS OFF */

and it works just as well. The same idiom works for Python as well.







> To hg: One does not need in C the static keyword to make a variable
> defined inside a function i.e. a so called 'automatic variable' private
> to that test. Automatic variables are private to their function by
> definition. The static keyword makes the variable permanent i.e. it
> keeps its value between calls but it is of course private also.
>
> To Neil Cerutti: If a programmer in C has got a pointer to some piece of
> memory, that piece is at the mercy of the programmer. There's no data
> hiding at all in this case.
>
> To whom it may concern: please stop comparing C and Python with regard
> to privacy and safety. They are two different worlds altogether. Believe
> me: I've been in this world for 2.5 years now after spending 19 years in
> the C world.
> 
> Cheers,
> Jussi

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


Re: Execute binary code

2007-01-08 Thread citronelu

Bjoern Schliessmann wrote:
> But you could technically achieve this with standard python too
> (just write python source and spawn a python process executing it).

The code I try to execute is Windows specific and it is binary, not
python. Furthermore, it is stored in a variable within the parent
python script, not stored on harddisk as a file.

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


RE: File Closing Problem in 2.3 and 2.4, Not in 2.5

2007-01-08 Thread Carroll, Barry
> -Original Message-
> From: [EMAIL PROTECTED] [mailto:python-
> [EMAIL PROTECTED] On Behalf Of John Machin
> Sent: Saturday, January 06, 2007 11:09 PM
> To: python-list@python.org
> Subject: Re: File Closing Problem in 2.3 and 2.4, Not in 2.5
> 
> Martin v. Löwis wrote:
> > Carroll, Barry schrieb:
> > > What I want to know is:
> > >
> > > * has anyone else encountered a problem like this, * how was the
> > > problem corrected, * can the fix be retro-fitted to 2.5 and 2.4?
> >
> > From your description, I suspect an error in your code. Your description
> > indicates that you don't expect to have more than five files open
> > simultaneously. Yet, the error message "Too many open files" occurs when
> > you open many more files (in the order of hundreds of files).
> >
> > It is very unlikely that there is a bug in Python where it would fail to
> > close a file when .close() is explicitly invoked on it (as your
> > description suggests that you do), so if you get that error message, it
> > can only mean that you fail to close some files.
> 
> I don't understand:  the OP's description suggests nothing of the sort
> to me. What he said was:
> """
> In this way, a tree of Parser instances is created, each with a single
> open file object.  (BTW, recursive and circular references are not
> allowed.)  When each Parser instance comes to the end of its table, the
> instance is explicitly destroyed, presumably destroying any objects it
> holds, AND closing its open file.
> """
> which I interpret as: he is doing del parser_instance, and *presuming*
> (incorrectly) that attributes of parser_instance (including an open
> file object) are magically whisked away instantly, instead of
> later/maybe. He later says he explicitly closed the files, which fixed
> what he alleges (incorrectly) to be a bug.
> 
> To the OP:
> (1) The del statement doesn't "destroy" anything. It unbinds the name
> from the object in the current namespace, and decrements the object's
> reference count. Only if the reference count is then zero will the
> janitor be called in.
> (2) Check the reference count on the parser_instance just before you
> del it. You could be retaining a reference somewhere.
> (3) Explicitly close all non-lightweight objects like files (even
> read-only ones) and sockets rather than hoping they will go away.
> 
> HTH,
> John

John:

Thank you.  I was afraid I had been totally misunderstood.  We have retained 
the explicit file close in the current version of the Parser code.  

I still have a question, however.  Python 2.3 and 2.4 both produce the "Too 
many open files" error on Windows XP Pro, but Python 2.5 does not.  Running on 
the 2.5 interpreter, our program process all the table invocations with no 
errors, a total of over 650 file opens.  Also, Python 2.3 on RedHat Fedora core 
1 runs our program to completion with no errors.  (I know, we are running 
pretty ancient SW here. Constraints unrelated to this question make that 
necessary.)  Do you know why the error would appear on some combinations of OS 
and interpreter and not on others?

Thanks again for your help.

Regards,
 
Barry
[EMAIL PROTECTED]
541-302-1107

We who cut mere stones must always be envisioning cathedrals.

-Quarry worker's creed


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


Re: File Closing Problem in 2.3 and 2.4, Not in 2.5

2007-01-08 Thread John Machin
On 9/01/2007 8:25 AM, Carroll, Barry wrote:
>> -Original Message-
>> From: [EMAIL PROTECTED] [mailto:python-
>> [EMAIL PROTECTED] On Behalf Of John Machin
>> Sent: Saturday, January 06, 2007 11:09 PM
>> To: python-list@python.org
>> Subject: Re: File Closing Problem in 2.3 and 2.4, Not in 2.5
>>
>> Martin v. Löwis wrote:
>>> Carroll, Barry schrieb:
 What I want to know is:

 * has anyone else encountered a problem like this, * how was the
 problem corrected, * can the fix be retro-fitted to 2.5 and 2.4?
>>> From your description, I suspect an error in your code. Your description
>>> indicates that you don't expect to have more than five files open
>>> simultaneously. Yet, the error message "Too many open files" occurs when
>>> you open many more files (in the order of hundreds of files).
>>>
>>> It is very unlikely that there is a bug in Python where it would fail to
>>> close a file when .close() is explicitly invoked on it (as your
>>> description suggests that you do), so if you get that error message, it
>>> can only mean that you fail to close some files.
>> I don't understand:  the OP's description suggests nothing of the sort
>> to me. What he said was:
>> """
>> In this way, a tree of Parser instances is created, each with a single
>> open file object.  (BTW, recursive and circular references are not
>> allowed.)  When each Parser instance comes to the end of its table, the
>> instance is explicitly destroyed, presumably destroying any objects it
>> holds, AND closing its open file.
>> """
>> which I interpret as: he is doing del parser_instance, and *presuming*
>> (incorrectly) that attributes of parser_instance (including an open
>> file object) are magically whisked away instantly, instead of
>> later/maybe. He later says he explicitly closed the files, which fixed
>> what he alleges (incorrectly) to be a bug.
>>
>> To the OP:
>> (1) The del statement doesn't "destroy" anything. It unbinds the name
>> from the object in the current namespace, and decrements the object's
>> reference count. Only if the reference count is then zero will the
>> janitor be called in.
>> (2) Check the reference count on the parser_instance just before you
>> del it. You could be retaining a reference somewhere.
>> (3) Explicitly close all non-lightweight objects like files (even
>> read-only ones) and sockets rather than hoping they will go away.
>>
>> HTH,
>> John
> 
> John:
> 
> Thank you.  I was afraid I had been totally misunderstood.  We have retained 
> the explicit file close in the current version of the Parser code.  
> 
> I still have a question, however.  Python 2.3 and 2.4 both produce the "Too 
> many open files" error on Windows XP Pro, but Python 2.5 does not.  Running 
> on the 2.5 interpreter, our program process all the table invocations with no 
> errors, a total of over 650 file opens.  Also, Python 2.3 on RedHat Fedora 
> core 1 runs our program to completion with no errors.  (I know, we are 
> running pretty ancient SW here. Constraints unrelated to this question make 
> that necessary.)  Do you know why the error would appear on some combinations 
> of OS and interpreter and not on others?
> 
> Thanks again for your help.
> 
> Regards,
>  
> Barry

Hi Barry,

Please always reply on-list if the communication is non-private -- and 
meaningful :-)

I don't know; here are some things you could check out:

(1) Windows XP versus *x: The limit on open file handles is 512 in 
Windows XP; write yourself a reference-preserving [hint: append to a 
list] multiple file opener on *x and see how many you get open. I have 
vague recollections of the number 1024 being mentioned.

(2) Python 2.4 versus 2.5: Read the "what's new" doc, fire up your 
googler, ...

IMHO, the attitude should be to program defensively so that you need 
neither to know nor care about such implementation-specific concerns. 
Operating systems these days give you enough file handles to do any 
reasonable job. It's many moons since MS-DOS 2.n with its max 20 open 
file handles made folks implement LRU caches for file handles.

Simple rules about unwanted objects include:
(1) If an object has a free() or close() method, then call it!
(2) Unbind *all* references to the object.

Have you checked out my point 2 (holding multiple references to your 
Parser objects)?

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


Re: how to find the longst element list of lists

2007-01-08 Thread Steven D'Aprano
On Mon, 08 Jan 2007 13:55:40 +0100, Peter Otten wrote:

>> The precise results depend on the version of Python you're running, the
>> amount of memory you have, other processes running, and the details of
>> what's in the list you are trying to sort. But as my test shows, sort has
>> some overhead that makes it a trivial amount slower for sufficiently small
>> lists, but for everything else you're highly unlikely to beat it.
> 
> Try again with tN.timeit(1) and a second list that is random.shuffle()d and
> copied to L before each measurement. list.sort() treats already sorted
> lists specially.

Or, simply shuffle the list itself. Why copy it?

In my tests, sorting still wins, and by roughly the same factor.

One optimization that might shift the balance would be to remove the
list copying in the non-sort code (list_of_lists[1:]). That's going to be
very time consuming for big lists.


-- 
Steven.

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


Re: Why less emphasis on private data?

2007-01-08 Thread Steven D'Aprano
On Mon, 08 Jan 2007 13:11:14 +0200, Hendrik van Rooyen wrote:

> When you hear a programmer use the word "probability" -
> then its time to fire him, as in programming even the lowest 
> probability is a certainty when you are doing millions of 
> things a second.

That is total and utter nonsense and displays the most appalling
misunderstanding of probability, not to mention a shocking lack of common
sense.


-- 
Steven.

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


Re: Execute binary code

2007-01-08 Thread Bjoern Schliessmann
Larry Bates wrote:

> What you are asking is a virus/trojan "like" program. 

Why? For being a trojan horse it must fake something. For being a
virus it must replicate itself. Writing an executable doesn't imply
the will to replicate itself.

But you could technically achieve this with standard python too
(just write python source and spawn a python process executing it).

Regards,


Björn

-- 
BOFH excuse #28:

CPU radiator broken

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


Re: Maths error

2007-01-08 Thread Bjoern Schliessmann
Rory Campbell-Lange wrote:

> Is using the decimal module the best way around this? (I'm
> expecting the first sum to match the second). It seem
> anachronistic that decimal takes strings as input, though.

What's your problem with the result, or what's your goal? Such
precision errors with floating point numbers are normal because the
precision is limited technically. 

For floats a and b, you'd seldom say "if a == b:" (because it's
often false as in your case) but rather 
"if a - b < threshold:" for a reasonable threshold value which
depends on your application.

Also check the recent thread "bizarre floating point output".

Regards,


Björn

-- 
BOFH excuse #333:

A plumber is needed, the network drain is clogged

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


Getting started with Crystal Reports...little help in the far court.

2007-01-08 Thread Mudcat
I am not that familiar with Crystal Reports, but having read some other
posts I know that the way to integrate the API with Python is through
the COM interface provide by win32all.

However, I have been unable to find any other information on how to get
started. I've used the COM interface before in integrating Excel and a
couple of other things. So I am familiar with how that works. But there
are at least 40 options dealing with Crystal and Business Objects. I
have no idea which makepy file to create or which one provides the
functionality I need.

I'm not looking to do much. All I'm really trying to do is provide one
application where a list of crystal reports can be selected and ran in
series. Right now we have a lot of reports that all have to be run
manually (takes a while). So I think all I need api access to is server
selection, parameter selection, and output formats.

Any pointers in the right direction would be helpful.

Thanks,
Marc

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


Re: Bitwise expression

2007-01-08 Thread Richard Townsend
On Mon, 08 Jan 2007 22:32:12 +0100, Gigs_ wrote:

> Can someone explain me bitwise expression?
> few examples for every expression will be nice
> 

http://wiki.python.org/moin/BitwiseOperators


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


Re: Maybe a little bug of ipython 0.7.3 ?

2007-01-08 Thread Fernando Perez
[EMAIL PROTECTED] wrote:

> I'm new to ipython, and i found it a very cool product.

Glad you like it, though in the future I recommend you post on the ipython
list.  I very rarely scan c.l.py these days, unfortunately.

> $ ipython
> Python 2.5 (r25:51908, Sep 19 2006, 09:52:17) [MSC v.1310 32 bit (Intel)]
> Type "copyright", "credits" or "license" for more information.
> 
> IPython 0.7.3 -- An enhanced Interactive Python.
> 
> 
> In [8]: a = range(1000)
> 
> In [9]: a?
> Type:   list
> Base Class: 
> String Form:[0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15,
> 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26 <...> 0, 981, 98
> 2, 983, 984, 985, 986, 987, 988, 989, 990, 991, 992, 993, 994, 995, 996,
> 997, 998, 999]
> Namespace:  Interactive
> Length: 1000
> Docstring:
> list() -> new list
> list(sequence) -> new list initialized from sequence's items
> 
> *Please note that there is an extra "0" after "**26 <...>", which
> doesn't appear for the followling cases:*

The 'foo?' feature just gives you a summary of information about an object,
and for very long strings, it truncates them in the center, printing only

head<...>tail

where head and tail are each about 100 characters long.

What you are seeing is just an accident of where the truncation happens,
that '0' is the last digit in 980 :)

Cheers,

f

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


Maths error

2007-01-08 Thread Rory Campbell-Lange
>>> (1.0/10.0)  + (2.0/10.0) + (3.0/10.0)
0.60009
>>> 6.0/10.0
0.59998

Is using the decimal module the best way around this? (I'm expecting the first
sum to match the second). It seem anachronistic that decimal takes strings as
input, though.

Help much appreciated;
Rory
-- 
Rory Campbell-Lange 
<[EMAIL PROTECTED]>

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


Network failure when using urllib2

2007-01-08 Thread jdvolz
I have a script that uses urllib2 to repeatedly lookup web pages (in a
spider sort of way).  It appears to function normally, but if it runs
too long I start to get 404 responses.  If I try to use the internet
through any other programs (Outlook, FireFox, etc.) it will also fail.
If I stop the script, the internet returns.

Has anyone observed this behavior before?  I am relatively new to
Python and would appreciate any suggestions. 

Shuad

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


Bitwise expression

2007-01-08 Thread Gigs_
Can someone explain me bitwise expression?
few examples for every expression will be nice

x << y Left shift
x >> y Right shift
x & y Bitwise AND
x | y Bitwise OR
x ^ y Bitwise XOR (exclusive OR)
~x Bitwise negation


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


Re: Fwd: Execute binary code

2007-01-08 Thread citronelu

Chris Mellon wrote:
> Writing to a temp file will be at least 3 times as easy and twice as
> reliable as any other method you come up with.

I'm not disputing that, but I want to keep a piece of code (a parser
for Oracle binary dumps, that I didn't wrote) out of foreign hands, as
much as possible. Using a TEMP directory is not "stealth" enough.

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


Re: Recommendations (or best practices) to define functions (or methods)

2007-01-08 Thread Bruno Desthuilliers
vizcayno a écrit :
> Hello:
> Need your help in the "correct" definition of the next function. If
> necessary, I would like to know about a web site or documentation that
> tells me about best practices in defining functions, especially for
> those that consider the error exceptions management.
> I have the next alternatives but I think there are better:
> 
> Alternative 1:
> =
> def ExecuteSQL(cmdSQL, cursor):

The recommended naming scheme is all_lower for functions, methods and 
variables, CamelCase for classes and ALL_UPPER for pseudo-constants.

> try:
> cursor.execute(cmdSQL)
> except Exception, e:
> return e
> return 1
> 
> Seems a good solution

Err... The whole point of exceptions is to avoid using return values as 
error code. Your code would be *much* better without this braindead 
(sorry) "exception handling":

def execute_sql(sql, cursor):
   cursor.execute(sql)

And now, since it's obvious that it's just a totally useless function 
call. So just ditch this function, and just call cursor.execute directly !-)

(snip other examples of how to not handle exceptions...)
-- 
http://mail.python.org/mailman/listinfo/python-list


Fwd: Execute binary code

2007-01-08 Thread Chris Mellon
On 8 Jan 2007 12:45:45 -0800, [EMAIL PROTECTED] <[EMAIL PROTECTED]> wrote:
> Larry Bates wrote:
>
> > What you are asking is a virus/trojan "like" program.  There's no reason
> > you shouldn't be able to write the code to TEMP directory and execute it.
> >
> > -Larry
>
>
> No, it is not about a trojan, but I guess it's pointless to try to
> convince you otherwise.
>
> It's not about being able to write the code to TEMP directory and
> execute it, it's about not wanting to do so.
>
> -Cornelius
>

Writing to a temp file will be at least 3 times as easy and twice as
reliable as any other method you come up with.


 Repost. Is there any chance at all that ML could set the
reply-to to the list instead of the sender?
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Execute binary code

2007-01-08 Thread citronelu
Larry Bates wrote:

> What you are asking is a virus/trojan "like" program.  There's no reason
> you shouldn't be able to write the code to TEMP directory and execute it.
>
> -Larry


No, it is not about a trojan, but I guess it's pointless to try to
convince you otherwise.

It's not about being able to write the code to TEMP directory and
execute it, it's about not wanting to do so.

-Cornelius

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


Re: Why less emphasis on private data?

2007-01-08 Thread Bruno Desthuilliers
[EMAIL PROTECTED] a écrit :
> Wow, I got a lot more feedback than I expected!
> 
> I can see both sides of the argument, both on technical merits, and
> more philosophical merits.  When I first learned C++ I felt
> setters/getters were a waste of my time and extra code.  When I moved
> to C# I still felt that, and with their 'Property" syntax I perhaps
> felt it more.  What changed my mind is when I started placing logic in
> them to check for values and throw expections or (hopefully) correct
> the data. That's probably reason one why I find it weird in Python

Python does have properties too. The point is that you can as well start 
with a plain attribute, then turn it into a computed one when (and if) 
needed.

> Reason two is, as the user of a class or API I *don't care* what is
> going on inside.

Very true... until you have a legitimate reason to mess with 
implementation because of a use case the author did not expect.

> All I want visible is the data that I can change.  The
> '_' convention is nice.. I do see that.  I guess my old OOP classes are
> hard to forget about.

Access restriction is not a mandatory part of OO. Of course, objects are 
supposed to be treated as "black boxes", but that's also true of a 
binary executable, and nothing (technically) prevents you to open it 
with an hex editor and hack it as you see fit... But then, you would not 
complain about strange bugs, would you ?-)

> I feel that the state of an object should be
> "stable" and "valid" at all times,

That's fine. Just remember that, in Python, methods are attributes too, 
and can be dynamically modified too. So when thinking about "object 
state", don't assume it only implies "data" attributes. Heck, you can 
even dynamically change the *class* of a Python object...

> and if its going into an unstable
> state - error then, not later.  That's why I like being able to protect
> parts of an instances state. If, as a provider of a calculation engine,
> I let the user change the internal state of the engine, I have no
> assurances that my product (the engine) is doing its job...

If you follow the convention, you are not responsible for what happens 
to peoples messing with implementation. period. Just like you're not 
responsible for what happens if someone hack your binary executable with 
an hex editor.

Welcome to Python, anyway.
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Traceback of hanged process

2007-01-08 Thread Mike C. Fletcher
Klaas wrote:
> Hynek Hanke wrote:
>   
>> Hello,
>>
>> please, how do I create a pythonic traceback from a python process that
>> hangs and is not running in an interpreter that I executed manually
>> or it is but doesn't react on CTRL-C etc? I'm trying to debug a server
>> implemented in Python, so I need some analog of 'gdb attach' for C.
>> 
...
> In python2.5, you can run a background thread that listens on a port or
> unix socket, and prints a formatted version of sys._current_frames() to
> stderr.  
>   
You can also use the signal module to similar effect.  Works well in 
Twisted, at least:

http://blog.vrplumber.com/835

HTH,
Mike

-- 

  Mike C. Fletcher
  Designer, VR Plumber, Coder
  http://www.vrplumber.com
  http://blog.vrplumber.com

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


Re: Execute binary code

2007-01-08 Thread olsongt

[EMAIL PROTECTED] wrote:
> Is it possible to execute a binary string stored within a python script
> as executable code ?
>
> The script is run under Windows, and the binary code (a full executable
> file) is stored in a variable in the script.
>
> I know I can use os.system() or os.popen() to run an external file, but
> these functions take as argument a string evaluated as command-line.
>
> I also know I could save the binary code as a temporary file, execute
> it and delete it afterwards, but this is not an alternative.
>
> Thanks.

It's not impossible, that's basically what I did on a smaller scale in
pyasm:

http://mysite.verizon.net/olsongt/

A small C-stub executes arbirary asm that was originally built as a
string.  The tough part for you would be loading all of the referenced
.dlls into memory and patching in all the relocations from the source
COFF file.  It'll be a pain but not entirely impossible.

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


Re: Working with Excel inside Python

2007-01-08 Thread Bruno Desthuilliers
[EMAIL PROTECTED] a écrit :
> Sorry for my little knowledge on Python. Actually my knowledge is
> specific for automating geo-processing tasks within ESRI environment,
> but sometimes I need to automate some other tasks (like this one) which
> require more in-depth knowledge of this language.

I would definitively not qualify function call syntax as "in-depth 
knowledge"

> Lots of documentation are of no use when you don't know exactly what to
> look for or have a wrong idea of what to do.

http://docs.python.org/tut/tut.html
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Working with Excel inside Python

2007-01-08 Thread Bruno Desthuilliers
[EMAIL PROTECTED] a écrit :
> Or, you might want to look at two packages:
> 
> xlrd
> 
> pyExcelerator
> 
> The first can "read" .xls files, and the second can write them.  I've had
> great results with both.

That's fine, but since the OP is mainly using Excel for reformating a 
csv file and saving it as a dbf file, I fail to see how these packages 
would help...

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


Re: Execute binary code

2007-01-08 Thread Larry Bates
[EMAIL PROTECTED] wrote:
> Is it possible to execute a binary string stored within a python script
> as executable code ?
> 
> The script is run under Windows, and the binary code (a full executable
> file) is stored in a variable in the script.
> 
> I know I can use os.system() or os.popen() to run an external file, but
> these functions take as argument a string evaluated as command-line.
> 
> I also know I could save the binary code as a temporary file, execute
> it and delete it afterwards, but this is not an alternative.
> 
> Thanks.
> 

What you are asking is a virus/trojan "like" program.  There's no reason
you shouldn't be able to write the code to TEMP directory and execute it.

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


Re: Traceback of hanged process

2007-01-08 Thread Klaas
Hynek Hanke wrote:
> Hello,
>
> please, how do I create a pythonic traceback from a python process that
> hangs and is not running in an interpreter that I executed manually
> or it is but doesn't react on CTRL-C etc? I'm trying to debug a server
> implemented in Python, so I need some analog of 'gdb attach' for C.
>
> Unfortunatelly, googling and reading documentation revealed nothing, so
> please excuse if this question is dumb.

In python2.5, you can run a background thread that listens on a port or
unix socket, and prints a formatted version of sys._current_frames() to
stderr.  

-Mike

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


Re: Bizarre floating-point output

2007-01-08 Thread Bjoern Schliessmann
Nick Maclaren wrote:

> Not at all.  "Precision" has been used to indicate the number of
> digits after the decimal point for at least 60 years, 

Not only, remember: Computer memories can't think in powers of ten.

> probably 100; in 40 years of IT and using dozens of programming
> languages, I have never seen "display" used for that purpose.

Yes, but since the representation in computers is based on powers of
two, a certain precision in the dual system, i. e. a fixed amount
of dual places, doesn't correspond with a fixed amount of decimal
places. Thus the rounding while displaying -- just to make it look
prettier. The very minimal additional error is silently accepted.

Regards,


Björn

-- 
BOFH excuse #199:

the curls in your keyboard cord are losing electricity.

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


Re: newbieee

2007-01-08 Thread Bjoern Schliessmann
lee wrote:

> I getting familiarised with python...can any one suggest me a good
> editor available for python which runs on windows xp

Look here: http://wiki.python.org/moin/PythonEditors

> one more request guys...can nyone tell me a good reference manual
> for python..

Either the python homepage, or "Python in a Nutshell". The 2nd
edition covers 2.5.

Regards,


Björn

-- 
BOFH excuse #324:

Your packets were eaten by the terminator

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


Re: newbieee

2007-01-08 Thread Daniel Klein
On 8 Jan 2007 10:59:23 -0800, "Thomas Nelson" <[EMAIL PROTECTED]>
wrote:

>O'reilly has a book called Programming Python that covers much of the
>standard library and how to use it for complex tasks.  It may be out of
>date by now, though.  

Programming Python (by Mark Lutz) is now in it's 3rd edition and is
quite up to date.

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


Execute binary code

2007-01-08 Thread citronelu
Is it possible to execute a binary string stored within a python script
as executable code ?

The script is run under Windows, and the binary code (a full executable
file) is stored in a variable in the script.

I know I can use os.system() or os.popen() to run an external file, but
these functions take as argument a string evaluated as command-line.

I also know I could save the binary code as a temporary file, execute
it and delete it afterwards, but this is not an alternative.

Thanks.

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


Re: More Efficient fnmatch.fnmatch for multiple patterns?

2007-01-08 Thread Gabriel Genellina

At Monday 8/1/2007 15:55, Wojciech =?ISO-8859-2?Q?Mu=B3a?= wrote:


pats = re.compile('|'.join(fnmatch.translate(p) for p in patterns))


Hmm, fnmatch.translate does not appear in the docs.
But it does on the module docstring, and in __all__, so certainly 
it's supposed to be public.

I'll file a bug report.


--
Gabriel Genellina
Softlab SRL 







__ 
Preguntá. Respondé. Descubrí. 
Todo lo que querías saber, y lo que ni imaginabas, 
está en Yahoo! Respuestas (Beta). 
¡Probalo ya! 
http://www.yahoo.com.ar/respuestas 

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

Re: Suitability for long-running text processing?

2007-01-08 Thread tsuraan

I remember something about it coming up in some of the discussions of
free lists and better behavior in this regard in 2.5, but I don't
remember the details.



Under Python 2.5, my original code posting no longer exhibits the bug - upon
calling del(a), python's size shrinks back to ~4 MB, which is its starting
size.  I guess I'll see how painful it is to migrate a gentoo system to
2.5... Thanks for the hint :)
-- 
http://mail.python.org/mailman/listinfo/python-list

Re: Q: About a programming problem with Python!

2007-01-08 Thread Tim Peters
[followups set to comp.lang.python]

[Danny]
> I am just getting into OOP using Python and
> because of the floating point of large integer (L)
> square roots all the decimal expansions are truncated.
> This has created a problem because I need these
> decimal expansions in my algorithm other wise
> it adds more sub iterations slowing down
> greatly to the programs performance.
>
> Is there any way of retrieving these truncated decimal
> expansions up to a certain (n) with Python?

You want comp.lang.python for this, not sci.math.  Be sure to give a 
concrete example, giving a specific input and showing exactly as 
possible what you /want/ to achieve.  There are so many possible 
ambiguities in what you wrote here that few people will take time to 
guess at what you might have intended to ask.

> Java and I believe c++ do not have this problem
> but I am not about to learn any of these because 
> I like Python. 

Programming language has little to nothing to do with this, it's the 
data type you're using.  In Java, C++, and Python, if you're using the 
native double-precision floating-point type, you're limited to (on most 
machines) at most 53 bits of precision.

If you need more than that, you need to use a different data type.  For 
example, in recent versions of Python you could use the `decimal` 
module, and set its precision to virtually anything you like; e.g., 
under Python 2.5,

>>> import decimal
>>> t = decimal.Decimal(2)
>>> decimal.getcontext().prec  # show the default precision
28
>>> print t.sqrt()   # sqrt(2) rounded to 28 significant digits
1.414213562373095048801688724
>>> decimal.getcontext().prec = 56  # double the precision
>>> print t.sqrt()   # sqrt(2) to rounded to 56 significant digits
1.4142135623730950488016887242096980785696718753769480732

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


Re: Why less emphasis on private data?

2007-01-08 Thread [EMAIL PROTECTED]
Wow, I got a lot more feedback than I expected!

I can see both sides of the argument, both on technical merits, and
more philosophical merits.  When I first learned C++ I felt
setters/getters were a waste of my time and extra code.  When I moved
to C# I still felt that, and with their 'Property" syntax I perhaps
felt it more.  What changed my mind is when I started placing logic in
them to check for values and throw expections or (hopefully) correct
the data. That's probably reason one why I find it weird in Python

Reason two is, as the user of a class or API I *don't care* what is
going on inside. All I want visible is the data that I can change.  The
'_' convention is nice.. I do see that.  I guess my old OOP classes are
hard to forget about. I feel that the state of an object should be
"stable" and "valid" at all times, and if its going into an unstable
state - error then, not later.  That's why I like being able to protect
parts of an instances state. If, as a provider of a calculation engine,
I let the user change the internal state of the engine, I have no
assurances that my product (the engine) is doing its job...



I appreciate all the feed back and enjoyed reading the discussion. It
helps me understand why Python community has chosen the road they have.
- Thanks.

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


Re: recursive function

2007-01-08 Thread Max Erickson
"cesco" <[EMAIL PROTECTED]> wrote:

> Hi,
> 
> I have a dictionary of lists of tuples like in the following
> example: dict = {1: [(3, 4), (5, 8)],
> 2: [(5, 4), (21, 3), (19, 2)],
> 3: [(16, 1), (0, 2), (1, 2), (3, 4)]]
> 
> In this case I have three lists inside the dict but this number
> is known only at runtime. I have to write a function that
> considers all the possible combinations of tuples belonging to
> the different lists and return a list of tuples of tuples for
> which the sum of the first element of the most inner tuple is
> equal to N. 
> 
> For example, assuming N = 24, in this case it should return:
> [((3, 4), (5, 4), (16, 1)), ((3, 4), (21, 3), (0, 2)), ((5, 8),
> (19, 2), (0, 2))]
> 
> A simple list comprehension would be enough if only I knew the
> number of keys/lists beforehand but this is not the case. I guess
> I need a recursive function. Can anyone help?
> 
> Thanks in advance
> Francesco
> 

This thread is probably of interest:

http://groups.google.com/group/comp.lang.python/browse_frm/thread/f0c0
406fce981a54/59a2a5dcd1507ab9#59a2a5dcd1507ab9


max

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


Re: newbieee

2007-01-08 Thread Thomas Nelson

lee wrote:
> I getting familiarised with python...can any one suggest me a good
> editor available for python which runs on windows xpone more
> request guys...can nyone tell me a good reference manual for python..

I think vim is a very good editor for python, and it's certainly
available for windows xp.

O'reilly has a book called Programming Python that covers much of the
standard library and how to use it for complex tasks.  It may be out of
date by now, though.  Truthfully the only python references I use
regularly are the online docs and this list.

hth,
THN

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


Re: Why less emphasis on private data?

2007-01-08 Thread Neil Cerutti
On 2007-01-08, Jussi Salmela <[EMAIL PROTECTED]> wrote:
> Neil Cerutti kirjoitti:
>> In C one uses the pointer to opaque struct idiom to hide data.
>> For example, the standard FILE pointer.
>
> To Neil Cerutti: If a programmer in C has got a pointer to some
> piece of memory, that piece is at the mercy of the programmer.
> There's no data hiding at all in this case.

That's somewhat disingenuous. You get just as much data hiding
with an opaque data type in C as you get in C++ or Java.

-- 
Neil Cerutti
Potluck supper: prayer and medication to follow. --Church Bulletin Blooper
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Recommendations (or best practices) to define functions (or methods)

2007-01-08 Thread vizcayno
Thank you for your guidelines and for changing my mind. I am trying to
do my best in generating good code and, in that attempt ... I only know
that nothing know.
Regards.

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


Re: More Efficient fnmatch.fnmatch for multiple patterns?

2007-01-08 Thread Wojciech Muła
abcd wrote:
> I am using fnmatch.fnmatch to find some files.  The only problem I have
> is that it only takes one pattern...so if I want to search using
> multiple patterns I have to do something like
>
> patterns = ['abc*.txt', 'foo*']
> 
> for p in patterns:
> if fnmatch.fnmatch(some_file_name, p):
> return True
>
> ...is there a built-in function that will match using multiple patterns?

import re
pats = re.compile('|'.join(fnmatch.translate(p) for p in patterns))

if pats.match(some_file_name):
return True

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


Re: More Efficient fnmatch.fnmatch for multiple patterns?

2007-01-08 Thread Gabriel Genellina

At Monday 8/1/2007 15:10, abcd wrote:


I am using fnmatch.fnmatch to find some files.  The only problem I have
is that it only takes one pattern...so if I want to search using
multiple patterns I have to do something like

patterns = ['abc*.txt', 'foo*']

for p in patterns:
if fnmatch.fnmatch(some_file_name, p):
return True

...is there a built-in function that will match using multiple patterns?


matched = any(fnmatch(filename, p) for p in patterns)


--
Gabriel Genellina
Softlab SRL 







__ 
Preguntá. Respondé. Descubrí. 
Todo lo que querías saber, y lo que ni imaginabas, 
está en Yahoo! Respuestas (Beta). 
¡Probalo ya! 
http://www.yahoo.com.ar/respuestas 

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

Re: Why less emphasis on private data?

2007-01-08 Thread Paul Boddie
Chris Mellon wrote:
> Private data in the C++ and Java OO worlds is so taught so much and
> emphasized so often that people have started thinking of it as being
> desirable for its own sake. But the primary motivation for it grew out
> of the need to maintain compatible interfaces.

This is generally true, yes.

[...]

> Python doesn't have these problems, so the only use for private
> information is to warn your clients away from access to certain names.
> There's no need for compiler enforcement of that, as a convention is
> just as effective.

You'll have to be more clear, here. If you're writing a subclass of
some other class then any usage of private attributes in the superclass
potentially provides the benefit of a free choice in attribute names in
the subclass. If you wanted to warn people away from certain names, it
would be the public attributes that would require the warning, noting
that "your clients" in this context includes people extending classes
as well as those merely instantiating and using them.

> The remaining arguments are generally outgrowths of "but my code is
> SECRET", which just isn't true in general, even less true of Python,
> and not really a healthy attitude anyway.

I don't care about secret attributes, and the namespace privacy aspect
doesn't bother me enough to use private attributes anyway, especially
since I'm the author of most of the superclasses I'm extending. But
either providing namespace privacy or convenient tools to mitigate
namespace sharing seems fairly important to me, at least.

Paul

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


Re: Module to read svg

2007-01-08 Thread Martin v. Löwis
Robert Kern schrieb:
>>> Does anyone know if there's an actual free implementation of this?
>> For the dom module in it, xml.dom.minidom should work. Depending on
>> your processing needs, that might be sufficient.
> 
> I don't think it quite fits what the OP is asking for. SVG defines some 
> non-XML
> structure for some of its contents. For example:
> 
>   
> 
> The OP is asking for a module that would parse the "points" attribute into a 
> list:
> 
>   [(100.0, 200.0), (100.0, 100.0)]

Sure, but he also specifically asked for an implementation of

http://www.w3.org/TR/SVGMobile12/python-binding.html

minidom *is* such an implementation (at least of a subset). If I
was to implement the entire thing, I'd base it on top of minidom:
SVGDocument needs to inherit from Document, and SVGSVGElement
needs to inherit from SVGLocatableElement needs to inherit from
SVGElement needs to inherit from Element.

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


Re: More Efficient fnmatch.fnmatch for multiple patterns?

2007-01-08 Thread Brian Beck
abcd wrote:
> I am using fnmatch.fnmatch to find some files.  The only problem I have
> is that it only takes one pattern...so if I want to search using
> multiple patterns I have to do something like
> 
> patterns = ['abc*.txt', 'foo*']
> 
> for p in patterns:
> if fnmatch.fnmatch(some_file_name, p):
> return True

I don't see anything in the fnmatch and glob modules... but I didn't look
very hard because what the heck is wrong with the four line solution you
have? Looks fine to me.

-- 
Brian Beck
Adventurer of the First Order

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


Re: Bizarre floating-point output

2007-01-08 Thread Nick Maclaren

In article <[EMAIL PROTECTED]>,
"Ziga Seilnacht" <[EMAIL PROTECTED]> writes:
|> 
|> There was a recent bug report identical to your complaints, which
|> was closed as invalid. The rationale for closing it was that things
|> like:
|> 
|> print ("a, bc", "de f,", "gh), i")
|> 
|> would be extremely confusing if the current behaviour was changed. See
|> http://www.python.org/sf/1534769
|> for details.

Well, I wasn't complaining - merely querying.

If this approach is taken, it would be better to document it, so that
authors of derived classes follow the convention.


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


Re: recursive function

2007-01-08 Thread Tim Williams
On 8 Jan 2007 16:03:53 +0100, Neil Cerutti <[EMAIL PROTECTED]> wrote:

>
> len(dict.keys()).
>

Or

len(dict)

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


newbieee

2007-01-08 Thread lee
I getting familiarised with python...can any one suggest me a good
editor available for python which runs on windows xpone more
request guys...can nyone tell me a good reference manual for python..

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


Re: recursive function

2007-01-08 Thread bearophileHUGS
First possible solution:

def rloop(seqin, comb):
# xcross product recipe 302478 by David Klaffenbach
if seqin:
for item in seqin[0]:
newcomb = comb + [item]
for item in rloop(seqin[1:], newcomb):
yield item
else:
yield comb

data = {1: [(3, 4), (5, 8)],
2: [(5, 4), (21, 3), (19, 2)],
3: [(16, 1), (0, 2), (1, 2), (3, 4)]}

print [sol for sol in rloop(data.values(), []) if 24==sum(el[0] for el
in sol)]

Bye,
bearophile

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


Re: xmlrpc and auth-digest

2007-01-08 Thread Laszlo Nagy
Thomas Liesner írta:
> Hi all,
>
> this may have been asked before, but as a newbie with xmlrpc i can't
> find any suitable info on that. Sorry.
> I am trying to write a simple xmlrpc-client in python and the server i
> am trying to receive data from requires http auth digest.
> The info on xmlrpclib covers auth basic thrugh url econding such as
> "user:[EMAIL PROTECTED]", but no auth digest.
>
> Is there any other library i could use for that or can i write some sort
> of wrapper around this using a differnt library like urllib?
>   
What I did before is that I added additional parameters of my published 
functions for the username and the password, and used xmlrpc over https. 
This is secure, but somewhat clumsy.  You can also implement this by 
creating a persistent "session" when logging in, then use the session 
identifier for identification.

If you are interested in XML/RPC over HTTPS:

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

Regarding basic or digest http authentication, I believe it has nothing 
to do with the XML/RPC protocol itself. The basic and digest 
authentication is defined at the HTTP level. It is implemented with HTTP 
headers. Probably you can do this but not with the built-in xmlrpclib. 
If you look at xmlrpclib.py, then you will see that the ServerProxy 
class is an old style class that uses the xmlrpclib.Transport class for 
communication. The Transport class itself uses httplib to post the 
information. In theory, you can copy your own xmlrpclib and send/check 
extra http headers. In my opinion, it would be too much work, too much 
trouble and still not secure.

Regards,

  Laszlo


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


Re: Bizarre floating-point output

2007-01-08 Thread Ziga Seilnacht
Nick Maclaren wrote:

> Well, it's not felt necessary to distinguish those at top level, so
> why should it be when they are in a sequence?

Well, this probably wasn't the best example, see the links below
for a better one.

> But this whole thing is getting ridiculous.  The current implementation
> is a bizarre interpretation of the specification, but clearly not an
> incorrect one.  It isn't important enough to get involved in a religious
> war over - I was merely puzzled as to the odd behaviour, because I have
> to teach it, and it is the sort of thing that can confuse naive users.

There was a recent bug report identical to your complaints, which
was closed as invalid. The rationale for closing it was that things
like:

print ("a, bc", "de f,", "gh), i")

would be extremely confusing if the current behaviour was changed. See
http://www.python.org/sf/1534769
for details.

Ziga

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


More Efficient fnmatch.fnmatch for multiple patterns?

2007-01-08 Thread abcd
I am using fnmatch.fnmatch to find some files.  The only problem I have
is that it only takes one pattern...so if I want to search using
multiple patterns I have to do something like

patterns = ['abc*.txt', 'foo*']

for p in patterns:
if fnmatch.fnmatch(some_file_name, p):
return True

...is there a built-in function that will match using multiple patterns?

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


Re: Why less emphasis on private data?

2007-01-08 Thread Chris Mellon
Private data in the C++ and Java OO worlds is so taught so much and
emphasized so often that people have started thinking of it as being
desirable for its own sake. But the primary motivation for it grew out
of the need to maintain compatible interfaces. These languages rely on
a great deal of shared information between provides and clients of
interfaces in order to work correctly - public/private interfaces are
simply a reflection of that requirement (and the fact that your
clients still need to see the stuff you declare as private is an
example of a leak in that abstraction).

Python doesn't have these problems, so the only use for private
information is to warn your clients away from access to certain names.
There's no need for compiler enforcement of that, as a convention is
just as effective.

The remaining arguments are generally outgrowths of "but my code is
SECRET", which just isn't true in general, even less true of Python,
and not really a healthy attitude anyway.
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Bizarre floating-point output

2007-01-08 Thread Nick Maclaren

In article <[EMAIL PROTECTED]>,
Bjoern Schliessmann <[EMAIL PROTECTED]> writes:
|> 
|> > The use of different precisions for the two cases is not, however,
|> > and it is that I was and am referring to.
|> 
|> You mistake "precision" with "display". 

Not at all.  "Precision" has been used to indicate the number of digits
after the decimal point for at least 60 years, probably 100; in 40 years
of IT and using dozens of programming languages, I have never seen
"display" used for that purpose.


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


Re: Bizarre floating-point output

2007-01-08 Thread Nick Maclaren

In article <[EMAIL PROTECTED]>,
"Ziga Seilnacht" <[EMAIL PROTECTED]> writes:
|> 
|> > I think that you should.  Where does it say that tuple's __str__ is
|> > the same as its __repr__?
|> >
|> > The obvious interpretation of the documentation is that a sequence
|> > type's __str__ would call __str__ on each sub-object, and its __repr__
|> > would call __repr__.
|> 
|> How would you distinguish ['3', '2', '1'] from [3, 2, 1] in that case?

Well, it's not felt necessary to distinguish those at top level, so
why should it be when they are in a sequence?

print "3", 3
3 3

But this whole thing is getting ridiculous.  The current implementation
is a bizarre interpretation of the specification, but clearly not an
incorrect one.  It isn't important enough to get involved in a religious
war over - I was merely puzzled as to the odd behaviour, because I have
to teach it, and it is the sort of thing that can confuse naive users.


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


  1   2   3   >