Re: sql escaping module - Frank Millman Followup

2005-12-09 Thread Steve Holden
David Bear wrote:
>>Steve Holden wrote:
> 
> 
>>Fredrik Lundh wrote:
>>
>>>Frank Millman wrote:
>>>
>>>
>>>
Each of the API's includes the capability of passing commands in the
form of 'string + parameters' directly into the database. This means
that the data values are never embedded into the SQL command at all,
and therefore there is no possibility of injection attacks.
>>>
>>>
> 
> My news server didn't get Franks initial post to the group, so I'm glad that
> Steve included it in his followup.
> 
> The statement above can cause relief or pain. Letting the DBAPI handle
> proper string escapes, formating, etc., is a big relief. However, I am
> still wondering what happens under the covers. If I have a string '1\n'
> that I've read from some source and I really intend on inserting it into
> the data base as a number 1, if the tape column it goes into is of type int
> or num or float, will the DBAPI really know what to do with the newline?
> 
> 
> 
Yes. If you read the DB API documentation 
(http://www.python.org/peps/pep-0249.html) you will see that there's a 
section on "Type Objects and Constructors". It's those that ensure a 
value will be coerced into the required form if possible.

regards
  Steve
-- 
Steve Holden   +44 150 684 7255  +1 800 494 3119
Holden Web LLC www.holdenweb.com
PyCon TX 2006  www.python.org/pycon/

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


Re: ANN: Dao Language v.0.9.6-beta is release!

2005-12-09 Thread Zeljko Vrba
On 2005-12-08, Steven D'Aprano <[EMAIL PROTECTED]> wrote:
>
> Making a mistake in indentation level is precisely analogous to leaving
> out markers in other languages. If your editor is smart enough, and the
>
But look at the following example:

if a:
  some_code1
if b:
  some_code2

If I accidentaly delete if b:, then some_code2 gets under the if a: which is
not intended. With braces (or other delimiters):

if(a) {
  some_code1;
}
if(b) {
  some_code2;
}

if I delete if(b) I get a syntax error about unmatched braces. IMO, the "right"
way to handle braces in C is always to  include them, even when they are not
technically obligatory. I code like that for a long time and it has saved me
a lot of headache. 
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: ANN: Dao Language v.0.9.6-beta is release!

2005-12-09 Thread Fredrik Lundh
Zeljko Vrba wrote:

> But look at the following example:
>
> if a:
>   some_code1
> if b:
>   some_code2
>
> If I accidentaly delete if b:, then some_code2 gets under the if a: which is
> not intended.

not to mention that if you have

if a:
some_code1
some_code2

and accidentally remove some_code2, it won't be executed at all !

do you often remove code by accident?  is this some vi-specific problem ?





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


Re: ANN: Dao Language v.0.9.6-beta is release!

2005-12-09 Thread Antoon Pardon
Op 2005-12-08, Steven D'Aprano schreef <[EMAIL PROTECTED]>:
> On Thu, 08 Dec 2005 08:23:52 +, Antoon Pardon wrote:
>
>> Op 2005-12-07, Steven D'Aprano schreef <[EMAIL PROTECTED]>:
>>> On Wed, 07 Dec 2005 15:26:59 +, Zeljko Vrba wrote:
>>>
 Braces are very convenient to match block start and end. Open a C program
 in the VI editor, and press % in command mode on some brace.. It will take
 you to its matching brace. How do you do something like that with python 
 code
 (or any code that is based purely on indentation..)
>>>
>>> (1) You don't need to, because you can *see* where the indentation
>>> changes, so you don't need to rely on your editor counting for you.
>> 
>> But you can't alway easily discern by how much.
>
> Admittedly, the blind and visually impaired might not. They will need a
> screen reader that understands indentation.
>
> Admittedly, a badly formatted arbitrary piece of text with arbitrary
> indentation from line to line is difficult to judge.
>
> But fortunately we're discussing code, not arbitrary text. Code will not
> jump from one random level of indentation to another: there will often be
> blocks of lines with the same indentation; when the indent level increases
> it should always increase by one only; and when it decreases it will
> usually decrease by one, sometimes two, more rarely three.

Four is not that unusual. The last method in a class and you already
have at least two. If that method ended with an if in a while, not
that uncommon, and you have four.

> If your functions have more than, say, twelve levels of indentation, you
> should be refactoring them.

Well I'm not going to put a hard number on it, but I agree with the gist
of your statement.

> If you are stuck reading other people's code, where they have hard-coded
> indents as (say) two spaces, you may have trouble. But if they are using a
> more standard four or eight spaces, it is easier to judge, and better
> still if they use tabs, you can tell your editor to set the tabs to
> whatever size works for you.

IME it doesn't make that much of a difference whether two spaces are
used, or four. One is too little and more than four is too much, but
between those two it doesn't make much difference for me.

> [snip]
>
>> But sometimes you mess up and your code is no longer indented as it
>> should. If you marked the end of the indentations, you can easily 
>> correct this.
>
> And sometimes you mess up and delete braces you shouldn't have.

Sure and if you have correctly indented your code, that will be
easily corrected too.

> If you
> had Or worse, you delete code you shouldn't have. Whoops!

Lets hope you have a backup.

> Start/end markers (whether braces or keywords) plus indentation carry
> redundant information. Redundancy means you can *sometimes* correct *some*
> errors. But there are costs to redundancy: you have to carry the redundant
> information around (in source code, in your head, in the complexity of the
> parser) for the 99.99% of time that there is no error.

99.99% of the time, I don't need a backup either. Besides what I had in
mind wouldn't force anyone to use end markers. It wouldn't break any
program(with the exception of programs using variable like endif). It
would just allow people to put in the redundancy where they think it would
be usefull.

> And what happens when the indentation and the markers disagree? Languages
> like C decide arbitrarily that the markers are meaningful and indentation
> is not, and it is a potent source of bugs and obfuscation.

Make it an error. That IMO is what redundant information is for, so that
when it doesn't agree, you are alerted to the problem.

> Making a mistake in indentation level is precisely analogous to leaving
> out markers in other languages. If your editor is smart enough, and the
> code is not ambiguous, a smart editor will pick that up straight away. And
> if not, the Python compiler will soon tell you, and you can then fix the
> bug in your code.

A smart editor may just happily continue, putting all further statements
on the wrong indentation level. Besides I hate smart editors. They
always seem to get in my way. This will probably say more about my lack
of knowledge about emacs, but the fact that it always jumps to the
corresponding left parent, bracket or brace when I type the right
character in, just drives me nuts.

Besides no editor has saved me once from the fact that I forget a colon
as last charater of an if, else, try or whatever compound statement,
so why should I rely on the editor for other problems?

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


Re: ANN: Dao Language v.0.9.6-beta is release!

2005-12-09 Thread D H
Fredrik Lundh wrote:
> Zeljko Vrba wrote:
> 
> 
>>But look at the following example:
>>
>>if a:
>>  some_code1
>>if b:
>>  some_code2
>>
>>If I accidentaly delete if b:, then some_code2 gets under the if a: which is
>>not intended.
> 
> 
> not to mention that if you have
> 
> if a:
> some_code1
> some_code2
> 
> and accidentally remove some_code2, it won't be executed at all !
> 
> do you often remove code by accident?  is this some vi-specific problem ?
> 

If you had bothered to read the context he was merely showing an example
to prove that this is not entirely true:
"Making a mistake in indentation level is precisely analogous to leaving 
out markers in other languages."

He was not suggesting that this is some affliction that he suffers, as 
you are suggesting.
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: ElementTree - Why not part of the core?

2005-12-09 Thread Magnus Lycka
[EMAIL PROTECTED] wrote:
> >> ElementTree on the other hand provides incredibly easy access to XML
> >> elements and works in a more Pythonic way.  Why has the API not been
> >> included in the Python core?
> 
> Magnus> I'd really like to see that too. Sure, it's fairly trivial to
> Magnus> install it, but each different package that needs to be
> Magnus> installed from another source, built and tested on multiple
> Magnus> platforms etc, means more work.
> 
> More work than reimplementing the functionality or living with a miserable
> API (I'm thinking normal DOM APIs)?

More work for the CM people. Less work for me.
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Moving a package in cygwin

2005-12-09 Thread Steve Holden
[EMAIL PROTECTED] wrote:
> Hi,
> 
> I'm using python 2.4 and windows XP.
> 
> I have two packages in the windows version of python in site-packages.
> They are PyVisa and ctypes, and both live in
> c:\python24\lib\site-packages
> 
> I'd like to move these to the cygwin version of python on the same
> system.  I tried copying the PyVisa and ctypes directorices (including
> other miscellaneous "junk" like .pyd files associated with ctypes) to
> c:\cygwin\lib\python24\site-packages\, but that didn't do the trick.
> It appears site-packages *is* in my path.  Aside from the pyvisa and
> ctypes directories, site-packages also contains: _ctypes.pyd,
> _ctypes_test.pyd, visa.py, visa.pyc, visa.pyo.  For the windows
> version, this appears to be enough.  For cygwin, however, it's not.
> 
> This is what I get under cygwin:
> 
> $ python
> Python 2.4.1 (#1, May 27 2005, 18:02:40)
> [GCC 3.3.3 (cygwin special)] on cygwin
> Type "help", "copyright", "credits" or "license" for more information.
> 
import visa
> 
> Traceback (most recent call last):
>   File "", line 1, in ?
>   File "/usr/lib/python2.4/site-packages/visa.py", line 1, in ?
> from pyvisa.visa import *
>   File "/usr/lib/python2.4/site-packages/pyvisa/__init__.py", line 27,
> in ?
> import vpp43
>   File "/usr/lib/python2.4/site-packages/pyvisa/vpp43.py", line 41, in
> ?
> from vpp43_types import *
>   File "/usr/lib/python2.4/site-packages/pyvisa/vpp43_types.py", line
> 38, in ?
> import ctypes as _ctypes
>   File "C:\Python24\Lib\site-packages\ctypes\__init__.py", line 13, in
> ?
> from _ctypes import Union, Structure, Array
> ImportError: No module named _ctypes
> 
> 
> The .pyd files are the only files with "_ctypes" in the name.  There
> are no references to pyvisa or ctypes in the c:\python24\lib directory.
> 
> All the documentation points to adding an arbitrary path via modifying
> site.py, sys.path, or other tricks, or installing fresh.  I tried
> installing ctypes with source (python setup.py build...) but that lead
> to compile problems which I won't go into here.
> 
> It seems like moving an already-working package should be easy.  Is
> there anything I'm missing?  Seems like it should be a one-liner
> someplace.
> 
Unfortunately Cygwin and Windows are two quite different platforms. 
While pure Python packages will transfer quite easily in the way you 
have attempted, extension packages (compiled from C or C++) will not. In 
order to make the same packages available under Cygwin you'll have to 
build them from source or find Cygwin installers.

The problem is that the extension packages must be created using the 
same compiler and libraries as (or at least a compiler and libraries 
that are binary compatible with) the Python interpreter.

Since the Cygwin interpreter was built with Cygwin gcc + Cygwin 
libraries and the Windows interpreter was built with Microsoft's Visual 
Studio compiler and libraries there's very little chance of a non-source 
migration of the Windows package to Cygwin.

regards
  Steve
-- 
Steve Holden   +44 150 684 7255  +1 800 494 3119
Holden Web LLC www.holdenweb.com
PyCon TX 2006  www.python.org/pycon/

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


register keyevent in wxpython

2005-12-09 Thread ash
hi,
is there a way to register application wide hotkey in wxpython?
i tried wxWindow::RegisterHotKey(). but the problem is it registers the
hotkey as a system wide hotkey. this causes problems in other
applications running concurrently which use the same hotkey.
i want to do this because i want my app frame (a mdi frame) to accept
the key event and wx.frame accepts only wx.eventCharHook() which raises
no keyevent for normal alphanumeric keys.
any ideas??
thanks in advance for any help.

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


pyparsing and LaTeX?

2005-12-09 Thread Ezequiel, Justin
> Anyone parsing simple LaTeX constructs with pyparsing?

Greetings Tim,

Have always wanted a way to parse LaTeX myself.
Unfortunately, I have been moved to a different project.
However, I am still very much interested.
Did you ever get a reply?
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Using printf in a C Extension

2005-12-09 Thread Frithiof Andreas Jensen

<[EMAIL PROTECTED]> wrote in message news:[EMAIL PROTECTED]
> Hi,
>
>
> I am extending python with C and trying to debug with printf.   The code
> below succssfully returns the string "hello" when compiled and called,
> but the "can print from in here phrase" does not reach python stdout.

It should go to stdout - maybe stdout does not flush. Maybe you need '\n' to
make it flush. Maybe your code broke printf.

> Is there something screwy with my environment or is there some trick to
> this that I don't know.  Any help would be greatly appreciated!

*printf* itself is screwy, much too clever: an interpreter messing with
nakkid pointers on top of varargs!! Bound to blow up if the bug you are
trying to find messes with any of the intricate machinery needed by printf,
such as the stack.

Try to locate the appropriate "debug version"s of printf available for your
platform. Usually there will be several that have the name "print" in them,
like "printmsg", "printerr", "printk" and so forth. They may even be macros
that are #ifdef'ed in/out with "debug" compile options - so you might need
to switch them on.

The point of the debug variants is that they always flush to stdout/stderr -
even when those are redirected - and they are designed to work even in
obscure circumstances such as in the middle of an exception. The price is
usually the loss of the printf formatting that nobody uses anyway (about 80%
of the options ;-) and no varargs.



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


Re: ANN: Dao Language v.0.9.6-beta is release!

2005-12-09 Thread Christophe
David Rasmussen a écrit :
> Antoon Pardon wrote:
> 
>>>
>>> Write shorter functions ;)
>>
>>
>> This has little to do with long functions. A class can contain
>> a large number of methods, whitch are all rather short, and your
>> class will still be spread over several pages.
>>
> 
> Write classes with a smaller interface ;-)
> 
> /David

What about an editor that will reserve the first lines in the edit 
window to show the current class ? Could be a cool feature here :) 
You'll see something like that when you scroll too far :

FileEdit   OptionsAbout
---
class do_something(object):
...
 return 1

def do_it(self):
 print self.what_now
-- 
http://mail.python.org/mailman/listinfo/python-list


idea of building python module using pyrex

2005-12-09 Thread [EMAIL PROTECTED]
Hello,

I have an idea to build python module to speed up python code in some
of field where pyrex shines such as numeric, code which needs a lot of
looping etc.

What do you think?

Sincerely Yours,
pujo

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


Re: How to get the extension of a filename from the path

2005-12-09 Thread Tom Anderson
On Thu, 8 Dec 2005, gene tani wrote:

> Lad wrote:
>
>> what is a way to get the the extension of  a filename from the path?
>
> minor footnote: windows paths can be raw strings for os.path.split(),
> or you can escape "/"
> tho Tom's examp indicates unescaped, non-raw string works with
> splitext()

DOH. Yes, my path's got a tab in it, hasn't it!

tom

-- 
Women are monsters, men are clueless, everyone fights and no-one ever
wins. -- cleanskies
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: efficient 'tail' implementation

2005-12-09 Thread Bengt Richter
On Thu, 08 Dec 2005 02:09:58 -0500, Mike Meyer <[EMAIL PROTECTED]> wrote:

>[EMAIL PROTECTED] writes:
>> I have a file which is very large eg over 200Mb , and i am going to use
>> python to code  a "tail"
>> command to get the last few lines of the file. What is a good algorithm
>> for this type of task in python for very big files?
>> Initially, i thought of reading everything into an array from the file
>> and just get the last few elements (lines) but since it's a very big
>> file, don't think is efficient. 
>
>Well, 200mb isn't all that big these days. But it's easy to code:
>
># untested code
>input = open(filename)
>tail = input.readlines()[:tailcount]
>input.close()
>
>and you're done. However, it will go through a lot of memory. Fastest
>is probably working through it backwards, but that may take multiple
>tries to get everything you want:
>
># untested code
>input = open(filename)
>blocksize = tailcount * expected_line_length
>tail = []
>while len(tail) < tailcount:
>  input.seek(-blocksize, EOF)
>  tail = input.read().split('\n')
>  blocksize *= 2
>input.close()
>tail = tail[:tailcount]
>
>It would probably be more efficient to read blocks backwards and paste
>them together, but I'm not going to get into that.
>
Ok, I'll have a go (only tested slightly ;-)

 >>> def frsplit(fname, nitems=10, splitter='\n', chunk=8192):
 ... f = open(fname, 'rb')
 ... f.seek(0, 2)
 ... bufpos = f.tell() # pos from file beg == file length
 ... buf = ['']
 ... for nl in xrange(nitems):
 ... while len(buf)<2:
 ... chunk = min(chunk, bufpos)
 ... bufpos = bufpos-chunk
 ... f.seek(bufpos)
 ... buf = (f.read(chunk)+buf[0]).split(splitter)
 ... if buf== ['']: break
 ... if bufpos==0: break
 ... if len(buf)>1: yield buf.pop(); continue
 ... if bufpos==0: yield buf.pop(); break
 ...

20 lines from the tail of november's python-dev archive

 >>> print '\n'.join(reversed(list(frsplit(r'v:\temp\clp\2005-November.txt', 
 >>> 20
 lives in the mimelib project's hidden CVS on SF, but that seems pretty
 silly.

 Basically I'm just going to add the test script, setup.py, generated
 html docs and a few additional unit tests, along with svn:external refs
 to pull in Lib/email from the appropriate Python svn tree.  This way,
 I'll be able to create standalone email packages from the sandbox (which
 I need to do because I plan on fixing a few outstanding email bugs).

 -Barry

 -- next part --
 A non-text attachment was scrubbed...
 Name: not available
 Type: application/pgp-signature
 Size: 307 bytes
 Desc: This is a digitally signed message part
 Url : 
http://mail.python.org/pipermail/python-dev/attachments/20051130/e88db51d/attachment.pgp


Might want to throw away the first item returned by frsplit, unless it is !='' 
(indicating a
last line with no \n). Splitting with os.linesep is a problematical default, 
since e.g. it
wouldn't work with the above archive, since it has unix endings, and I didn't 
download it
in a manner that would convert it.

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


Re: Using printf in a C Extension

2005-12-09 Thread Just
In article <[EMAIL PROTECTED]>, [EMAIL PROTECTED] wrote:

> I am extending python with C and trying to debug with printf.   The code 
> below succssfully returns the string "hello" when compiled and called, 
> but the "can print from in here phrase" does not reach python stdout. 
> Is there something screwy with my environment or is there some trick to 
> this that I don't know.  Any help would be greatly appreciated!

Have a look at PySys_WriteStdout().

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


Re: idea of building python module using pyrex

2005-12-09 Thread Diez B. Roggisch
[EMAIL PROTECTED] wrote:

> Hello,
> 
> I have an idea to build python module to speed up python code in some
> of field where pyrex shines such as numeric, code which needs a lot of
> looping etc.

Isn't numeric already written in C?
-- 
Regards,

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


Re: idea of building python module using pyrex

2005-12-09 Thread Fredrik Lundh
[EMAIL PROTECTED] wrote:

> I have an idea to build python module to speed up python code in some
> of field where pyrex shines such as numeric, code which needs a lot of
> looping etc.
>
> What do you think?

I don't think you need anyone's permission to do that, really.  Just grab
the tools and start coding.  If you're happy with the result, feel free to
share it, if you want.





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


Re: Encoding of file names

2005-12-09 Thread Tom Anderson

On Thu, 8 Dec 2005, "Martin v. Löwis" wrote:


utabintarbo wrote:


Fredrik, you are a God! Thank You^3. I am unworthy 


For all those who followed this thread, here is some more explanation:

Apparently, utabintarbo managed to get U+2592 (MEDIUM SHADE, a filled 
50% grayish square) and U+2524 (BOX DRAWINGS LIGHT VERTICAL AND LEFT, a 
vertical line in the middle, plus a line from that going left) into a 
file name. How he managed to do that, I can only guess: most likely, the 
Samba installation assumes that the file system encoding on the Solaris 
box is some IBM code page (say, CP 437 or CP 850). If so, the byte on 
disk would be \xb4. Where this came from, I have to guess further: 
perhaps it is ACUTE ACCENT from ISO-8859-*.


Anyway, when he used listdir() to get the contents of the directory, 
Windows applies the CP_ACP encoding (known as "mbcs" in Python). For 
reasons unknown to me, the US and several European versions of XP map 
this to \xa6, VERTICAL BAR (I can somewhat see that as meaningful for 
U+2524, but not for U+2592).


So when he then applies isfile to that file name, \xa6 is mapped to 
U+00A6, which then isn't found on the Samba side.


So while Unicode here is the solution, the problem is elsewhere; most 
likely in a misconfiguration of the Samba server (which assumes some 
encoding for the files on disk, yet the AIX application uses a different 
encoding).


Isn't the key thing that Windows is applying a non-roundtrippable 
character encoding? If i've understood this right, Samba and Windows are 
talking in unicode, with these (probably quite spurious, but never mind) 
U+25xx characters, and Samba is presenting a quite consistent view of the 
world: there's a file called "double bucky backlash grey box" in the 
directory listing, and if you ask for a file called "double bucky backlash 
grey box", you get it. Windows, however, maps that name to the 8-bit 
string "double bucky blackslash vertical bar", but when you pass *that* 
back to it, it gets encoded as the unicode string "double bucky backslash 
vertical bar", which Sambda then doesn't recognise.


I don't know what Windows *should* do here. I know it shouldn't do this - 
this leads to breaking of some very basic invariants about files and 
directories, and so the kind of confusion utabintarbo suffered. The 
solution is either to apply an information-preserving encoding (UTF-8, 
say), or to refuse to do it at all (ie, raise an error if there are 
unencodable characters), neither of which are particularly beautiful 
solutions. I think Windows is in a bit of a rock/hard place situation 
here, poor thing.


Incidentally, for those who haven't come across CP_ACP before, it's not 
yet another character encoding, it's a pseudovalue which means 'the 
system's current default character set'.


tom

--
Women are monsters, men are clueless, everyone fights and no-one ever
wins. -- cleanskies-- 
http://mail.python.org/mailman/listinfo/python-list

Re: Is there anything that pickle + copy_reg cannot serialize?

2005-12-09 Thread Maurice LING

>>> Since copy_reg lets you specify arbitrary code to serialize arbitrary
>>> objects, you shouldn't run into any single object that you cannot
>>> serialize to a pickle.
>>
>>
>> [snip - example of pickling code objects]
>>
>>
>> I cannot understand 2 things, which I seek assistance for:
>> 1. Is code object the only thing can cannot be pickled (less facing
>> recursion limits)?
> 
> 
> No.  There are lots of objects that cannot be pickled by default.  Any 
> extension type which does not explicitly support it cannot be pickled.  
> Generators cannot be pickled.  Method descriptors can't be pickled.  Et 
> cetera.

Thank Jean-Paul.

Sorry for not specifying clearly enough. Given that copy_reg lets you 
specify arbitrary code to serialize arbitrary objects, of which some are 
taken care of by pickle, in the set of possible Python types,

 >>> import types
 >>> dir(types)
['BooleanType', 'BufferType', 'BuiltinFunctionType', 
'BuiltinMethodType', 'ClassType', 'CodeType', 'ComplexType', 
'DictProxyType', 'DictType', 'DictionaryType', 'EllipsisType', 
'FileType', 'FloatType', 'FrameType', 'FunctionType', 'GeneratorType', 
'InstanceType', 'IntType', 'LambdaType', 'ListType', 'LongType', 
'MethodType', 'ModuleType', 'NoneType', 'NotImplementedType', 
'ObjectType', 'SliceType', 'StringType', 'StringTypes', 'TracebackType', 
'TupleType', 'TypeType', 'UnboundMethodType', 'UnicodeType', 
'XRangeType', '__builtins__', '__doc__', '__file__', '__name__']

What types cannot be serialized by pickle (besides CodeType) and 
requires handling using copy_reg?

Thanks
Maurice


> 
>> 2. In the above example, how copy_reg works with pickle?
> 
> 
> Any time pickle thinks it has found something it cannot pickle, it asks 
> the copy_reg module for some help.  The above example basically teaches 
> the copy_reg module how to give the pickle module the help it needs for 
> code objects.
> 
> Jean-Paul
-- 
http://mail.python.org/mailman/listinfo/python-list


Validating an email address

2005-12-09 Thread Tom Anderson
Hi all,

A hoary old chestnut this - any advice on how to syntactically validate an 
email address? I'd like to support both the display-name-and-angle-bracket 
and bare-address forms, and to allow everything that RFC 2822 allows (and 
nothing more!).

Currently, i've got some regexps which recognise a common subset of 
possible addresses, but it would be nice to do this properly - i don't 
currently support quoted pairs, quoted strings, or whitespace in various 
places where it's allowed. Adding support for those things using regexps 
is really hard. See:

http://www.ex-parrot.com/~pdw/Mail-RFC822-Address.html

For a level to which i am not prepared to stoop.

I hear the email-sig are open to adding a validation function to the email 
package, if a satisfactory one can be written; i would definitely support 
their doing that.

tom

-- 
Women are monsters, men are clueless, everyone fights and no-one ever
wins. -- cleanskies
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Is there anything that pickle + copy_reg cannot serialize?

2005-12-09 Thread Fredrik Lundh
Maurice LING wrote:

> Sorry for not specifying clearly enough. Given that copy_reg lets you
> specify arbitrary code to serialize arbitrary objects, of which some are
> taken care of by pickle, in the set of possible Python types,

the types module contains a selection of type objects; the set of possible
types that you may have in a Python program is unbounded.

> What types cannot be serialized by pickle (besides CodeType) and
> requires handling using copy_reg?

import types, pickle, sys

for t in dir(types):
try:
if t == "BufferType":
o = buffer("hello")
elif t == "EllipsisType":
o = Ellipsis
elif t == "FileType":
o = file("hello.txt", "w")
elif t == "NoneType":
o = None
elif t == "SliceType":
o = slice(0,0,0)
elif t == "XRangeType":
o = xrange(0,0,1)
else:
o = getattr(types, t)()
except:
print t, "FAILED:", sys.exc_value
else:
try:
s = pickle.dumps(o)
except:
print t, "FAILED:", str(sys.exc_value).upper()
else:
print t, "OK"

should give you an idea (feel free to add more object factories)





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


slice notation as values?

2005-12-09 Thread Antoon Pardon
Now slices are objects in python, I was wondering if slice
notation will be usable outside subscribtion in the future.

Will it ever be possible to write things like:

  a = 4:9
  for key, value in tree.items('alfa.': 'beta.'):

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


Re: uuDecode problem

2005-12-09 Thread py
Alex Martelli wrote:
I suggest a redesign...!


What would you suggest?  I have to encode/decode in chunks b/c of the
45 byte limitation.

Thanks.

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


Re: slice notation as values?

2005-12-09 Thread Steve Holden
Antoon Pardon wrote:
> Now slices are objects in python, I was wondering if slice
> notation will be usable outside subscribtion in the future.
> 
> Will it ever be possible to write things like:
> 
>   a = 4:9
>   for key, value in tree.items('alfa.': 'beta.'):
> 
Do you mean

   for key, value in tree.items()['alfa.': 'beta.']:

What would this mean?

regards
  Steve
-- 
Steve Holden   +44 150 684 7255  +1 800 494 3119
Holden Web LLC www.holdenweb.com
PyCon TX 2006  www.python.org/pycon/

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


Re: uuDecode problem

2005-12-09 Thread Fredrik Lundh
"py" wrote:

> What would you suggest?  I have to encode/decode in chunks b/c of the
> 45 byte limitation.

so use 45-byte chunks, instead of one-byte chunks.

but why are you using UU encoding in a nonstandard way ?  why not just
use the "uu" module to do the chunking for you?  the third example on this
page might be helpful:

http://effbot.org/librarybook/uu.htm

(if you don't want the standard begin/end lines, it's probably a better idea
to use base64 encoding instead...)





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


Re: slice notation as values?

2005-12-09 Thread Antoon Pardon
Op 2005-12-09, Steve Holden schreef <[EMAIL PROTECTED]>:
> Antoon Pardon wrote:
>> Now slices are objects in python, I was wondering if slice
>> notation will be usable outside subscribtion in the future.
>> 
>> Will it ever be possible to write things like:
>> 
>>   a = 4:9
>>   for key, value in tree.items('alfa.': 'beta.'):
>> 
> Do you mean
>
>for key, value in tree.items()['alfa.': 'beta.']:

No, the slice is meant to be a parameter to the method.
It would be a more convenient way to write:

  for key, value in tree.items(slice('alfa.', 'beta.'))

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


Re: slice notation as values?

2005-12-09 Thread Duncan Booth
Antoon Pardon wrote:

> Will it ever be possible to write things like:
> 
>   a = 4:9
>   for key, value in tree.items('alfa.': 'beta.'):

The first of these works fine, except you need to use the correct syntax:

>>> a = slice(4,9)
>>> range(10)[a]
[4, 5, 6, 7, 8]
>>> 

The second also works fine, provide tree is a type which supports it and 
you rewrite the call as tree.items(slice('alfa','beta.')) or perhaps 
tree['alfa':'beta.'].items(). To support slicing directly on a dictionary 
you could do:

>>> class sliceable(dict):
def __getitem__(self, item):
if isinstance(item, slice):
return self.__class__((k,v) for (k,v) in self.iteritems()
if item.start <= k < item.stop)
return dict.__getitem__(self, item)

>>> d = sliceable({'alpha': 1, 'aaa': 2, 'beta': 3, 'bee': 4 })
>>> d['alpha':'beta']
{'alpha': 1, 'bee': 4}
>>> d['alpha':'beta.']
{'alpha': 1, 'beta': 3, 'bee': 4}
>>> for key, value in d['alpha':'beta.'].items():
print key, value


alpha 1
beta 3
bee 4

It seems unlikely that this will make it into the builtin dict type, but 
you never know.
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: idea of building python module using pyrex

2005-12-09 Thread [EMAIL PROTECTED]
Hello,

I just wonder if someone has already build it.
Thanks for the response.

Best Regards,
pujo

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


Re: slice notation as values?

2005-12-09 Thread Antoon Pardon
Op 2005-12-09, Duncan Booth schreef <[EMAIL PROTECTED]>:
> Antoon Pardon wrote:
>
>> Will it ever be possible to write things like:
>> 
>>   a = 4:9
>>   for key, value in tree.items('alfa.': 'beta.'):
>
> The first of these works fine, except you need to use the correct syntax:

Sure, I know that. But why a different syntax?

If we have lst = range(10), we can write

   lst[slice(3,7)]

instead of

   lst[3:7]

Now my impression is that should we only have the upper notation, slices
would be less usefull, because it would make using them more cumbersome.

I think that having this easy notation for slices available in more
general circumstances, would make the use of them in other situations
more easy too.

 a = slice(4,9)
 range(10)[a]
> [4, 5, 6, 7, 8]
 
>
> The second also works fine, provide tree is a type which supports it and 
> you rewrite the call as tree.items(slice('alfa','beta.')) or perhaps 
> tree['alfa':'beta.'].items(). To support slicing directly on a dictionary 
> you could do:

 class sliceable(dict):
> def __getitem__(self, item):
> if isinstance(item, slice):
>   return self.__class__((k,v) for (k,v) in self.iteritems()
> if item.start <= k < item.stop)
> return dict.__getitem__(self, item)
>
 d = sliceable({'alpha': 1, 'aaa': 2, 'beta': 3, 'bee': 4 })
 d['alpha':'beta']
> {'alpha': 1, 'bee': 4}
 d['alpha':'beta.']
> {'alpha': 1, 'beta': 3, 'bee': 4}
 for key, value in d['alpha':'beta.'].items():
>   print key, value
>   
> alpha 1
> beta 3
> bee 4
>
> It seems unlikely that this will make it into the builtin dict type, but 
> you never know.

It doesn't need to be in the buildin dict type, I just would think it
could make the interface to my own treedict type more intuitive.

In my treedict the keys are accessible in order. If you have dictionary
with strings as keys doing:

  for key in tree:

will give you the keys in alfabetical order. Doing

  for key in tree['a':'b']:

will give you all the keys that start with an 'a'.

Now there are a number of methods with similar results.
keys, values, items and there iter variants iterkeys,
itervalues and iteritems. These methods are 'sliceable'
too and I think the possibilty of giving such a method
a slice as parameter, with the same notation as in
a subscript, would be the clearest way for the user
to provide slice information.

If the user can write:

  for key in tree['a':'b']:

Why shouldn't he be able to write:

  for key, value in tree.iteritems('a':'b'):

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


Re: idea of building python module using pyrex

2005-12-09 Thread Fredrik Lundh
[EMAIL PROTECTED] wrote:

> I just wonder if someone has already build it.

built what?  it's not like nobody's ever built a Python module using Pyrex 
before, so
I guess you have to be a bit more precise if you want feedback.

 



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


Re: idea of building python module using pyrex

2005-12-09 Thread [EMAIL PROTECTED]

For example stastical module like commulative probability  function for
t distribution, or other numerical module which incorporate looping to
get the result.

I found that pyrex is very helpfull when dealing with looping things.

pujo

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


Re: ANN: Dao Language v.0.9.6-beta is release!

2005-12-09 Thread Benji York
Christophe wrote:
> David Rasmussen a écrit :
> 
>>Antoon Pardon wrote:
>>
>>
Write shorter functions ;)
>>>
>>>
>>>This has little to do with long functions. A class can contain
>>>a large number of methods, whitch are all rather short, and your
>>>class will still be spread over several pages.
>>>
>>
>>Write classes with a smaller interface ;-)
>>
>>/David
> 
> 
> What about an editor that will reserve the first lines in the edit 
> window to show the current class ? Could be a cool feature here :) 

I've been using a Vim script for a while that adds the name of current 
class, function, or Class.Method to the status line and it helps quite a 
bit.
--
Benji York
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: slice notation as values?

2005-12-09 Thread Duncan Booth
Antoon Pardon asked:


> If we have lst = range(10), we can write
> 
>lst[slice(3,7)]
> 
> instead of
> 
>lst[3:7]
> 
> Now my impression is that should we only have the upper notation, slices
> would be less usefull, because it would make using them more cumbersome.

Quite right, but the syntax for the slice only applies inside the square 
brackets and there would be big problems making it work outside a 
subscript. If you allowed a:b you get syntactic ambiguities: e.g. is this a 
slice or a complete if statement:

   if a:b

If you allow a slice on its own but require the square brackets still to be 
there then you don't (I think) get any syntax ambiguities, but the result 
looks like it should be some weird list comprehension so its just too 
confusing:

   myslice = [a:b]

I think the case for freestanding slices is there, but they aren't common 
enough to justify special syntax.

> If the user can write:
> 
>   for key in tree['a':'b']:
> 
> Why shouldn't he be able to write:
> 
>   for key, value in tree.iteritems('a':'b'):
> 

>>> import this
...
There should be one-- and preferably only one --obvious way to do it.
Although that way may not be obvious at first unless you're Dutch.
...

If the user can write

   for key in tree['a':'b']:

then he can write:

   for key in tree['a':'b'].iteritems():

so why add a second way to do that?


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


Re: efficient 'tail' implementation

2005-12-09 Thread Magnus Lycka
[EMAIL PROTECTED] wrote:
> hi
> 
> I have a file which is very large eg over 200Mb , and i am going to use
> python to code  a "tail"
> command to get the last few lines of the file. What is a good algorithm
> for this type of task in python for very big files?
> Initially, i thought of reading everything into an array from the file
> and just get the last few elements (lines) but since it's a very big
> file, don't think is efficient. 
> thanks

To read the last x bytes of a file, you could do:

 >>> import os
 >>> x = 2000 # or whatever...
 >>> f=open('my_big_file')
 >>> l=os.fstat(f.fileno()).st_size
 >>> f.seek(l-x)
 >>> f.read()

Maybe that's a start. I didn't try it on a anything bigger than 16MB,
but it was more or less instantaneous for 16Megs.

If you want the last X lines and know that lines are no more than N
chars, f.seek(l-X*N); f.readlines()[-X:] should give you what you
need... (I think...I didn't test it.)
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: how to put form and display its result(data from database) on the same window?

2005-12-09 Thread lli
Thank you Peter. I will read it.

LLI

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


Re: Another newbie question

2005-12-09 Thread BartlebyScrivener
>>http://en.wikipedia.org/wiki/Law_of_Demeter <<

That was fun. Thanks, Kent.



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


Freeze doesn't import all modules . . .

2005-12-09 Thread Michael Williams
Is there any way to FORCE freeze to import all necessary modules no  
matter what?  I have noticed that a few 3rd party modules simply  
don't get included in a "compiled" bundle.  More specifically,   
"PEXPECT", and some of the required "AMARA" xml modules aren't  
included when you freeze a python script. I've checked this out on  
both Ubuntu 5.10 systems and Mac OS X systems.  Whenever I run the  
compiled version on one of my "matching" systems, it simply tells me  
that "there is no module pexpect" or that "there is no module dom".   
To my knowledge, I shouldn't have to have *anything* installed on the  
receiving system.  Is that not correct?

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


Re: How to stop a linux process

2005-12-09 Thread Michael Williams
You may check out the  http://pexpect.sourceforge.net/ module.  This gives you pretty good control over running processes.  It even allows you to "interact" with them.Regards,MichaelOn Nov 28, 2005, at 4:00 PM, [EMAIL PROTECTED] wrote:From: Glen <[EMAIL PROTECTED]> Date: November 28, 2005 2:10:05 PM EST To: python-list@python.org Subject: How to stop a linux process   When I used the following line to play a midi file in linux,  return_value = os.system('timidity test.mid')  I have encountered two problems. 1. The python script halts until timidity has finished. 2. If I had control of the script, I can't think how I would stop timidity.  Any advice on the 'area' of python I should be looking at would be greatly appreciated.  Thanks. -- 
http://mail.python.org/mailman/listinfo/python-list

Re: slice notation as values?

2005-12-09 Thread Antoon Pardon
Op 2005-12-09, Duncan Booth schreef <[EMAIL PROTECTED]>:
> Antoon Pardon asked:
>
>
>> If we have lst = range(10), we can write
>> 
>>lst[slice(3,7)]
>> 
>> instead of
>> 
>>lst[3:7]
>> 
>> Now my impression is that should we only have the upper notation, slices
>> would be less usefull, because it would make using them more cumbersome.
>
> Quite right, but the syntax for the slice only applies inside the square 
> brackets and there would be big problems making it work outside a 
> subscript. If you allowed a:b you get syntactic ambiguities: e.g. is this a 
> slice or a complete if statement:
>
>if a:b
>
> If you allow a slice on its own but require the square brackets still to be 
> there then you don't (I think) get any syntax ambiguities, but the result 
> looks like it should be some weird list comprehension so its just too 
> confusing:
>
>myslice = [a:b]

I don't see the problem. The syntax for tuples can create syntactic
ambiguities too. Take the following statement:

  f(a,b)

Does the call have two parameters or one that is a tuple? In practice
one uses parenthesis to disambigue the stament. So in the above case
it would be a complete if statement. And myslice = a:b wouldn't
cause a problem.

> I think the case for freestanding slices is there, but they aren't common 
> enough to justify special syntax.
>
>> If the user can write:
>> 
>>   for key in tree['a':'b']:
>> 
>> Why shouldn't he be able to write:
>> 
>>   for key, value in tree.iteritems('a':'b'):
>> 
>
 import this
> ...
> There should be one-- and preferably only one --obvious way to do it.
> Although that way may not be obvious at first unless you're Dutch.
> ...
>
> If the user can write
>
>for key in tree['a':'b']:
>
> then he can write:
>
>for key in tree['a':'b'].iteritems():

No he can't. tree['a':'b'] would provide a list
of keys that all start with an 'a'. Such a list
doesn't have an iteritems method. It wouldn't even
contain the information to construct items.

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


Re: idea of building python module using pyrex

2005-12-09 Thread David M. Cooke
"[EMAIL PROTECTED]" <[EMAIL PROTECTED]> writes:

> For example stastical module like commulative probability  function for
> t distribution, or other numerical module which incorporate looping to
> get the result.
>
> I found that pyrex is very helpfull when dealing with looping
> things.

Pyrex is indeed quite helpful. If you're interested in statistical
distributions, you'll want to look at the scipy.stats module in scipy
(http://www.scipy.org/), which has lots (including the t distribution).

In SciPy, we use Pyrex for the random-number generator module
scipy.random. It's actually used to wrap some C code, but it does the
job well.

-- 
|>|\/|<
/--\
|David M. Cooke
|cookedm(at)physics(dot)mcmaster(dot)ca
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: idea of building python module using pyrex

2005-12-09 Thread [EMAIL PROTECTED]
Hello David,

Is SciPy works with python 2.4.2, Windows XP?
I thought it is only for python 2.3?

pujo

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


Re: Validating an email address

2005-12-09 Thread Jean-Paul Calderone
On Fri, 9 Dec 2005 11:10:04 +, Tom Anderson <[EMAIL PROTECTED]> wrote:
>Hi all,
>
>A hoary old chestnut this - any advice on how to syntactically validate an
>email address? I'd like to support both the display-name-and-angle-bracket
>and bare-address forms, and to allow everything that RFC 2822 allows (and
>nothing more!).
>
>Currently, i've got some regexps which recognise a common subset of
>possible addresses, but it would be nice to do this properly - i don't
>currently support quoted pairs, quoted strings, or whitespace in various
>places where it's allowed. Adding support for those things using regexps
>is really hard. See:
>
>http://www.ex-parrot.com/~pdw/Mail-RFC822-Address.html
>
>For a level to which i am not prepared to stoop.
>
>I hear the email-sig are open to adding a validation function to the email
>package, if a satisfactory one can be written; i would definitely support
>their doing that.

The top part of 

 contains a parser that, IIRC, is basically complete.  There are unit tests 
nearby, too.  The code is MIT licensed.

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


Re: installer question

2005-12-09 Thread Larry Bates
Take a look at Inno Installer.  You should be able to
do everything you list.  You may also want to consider
using py2exe to package up your python program into
.exe prior to creating installer file.  That way you
eliminate the requirement of having python, pythonwin32
installed and you don't have to do anything to pythonpath
or to site packages.

-Larry

Guy Robinson wrote:
> Target audience is little or no programming experience.
> 
> I have a win32 only library I need to write an installer for. As part of
> the installation it must:
> 1.. find where a program is installed
> 2.. copy a file to the directory
> 3.. add the directory to the pythonpath and change a ini file.
> 4.. add a example directory at a user selected path.As well as add this
> to pythonpath.
> 5.. Add the main library to site packages.
> 
> There is also a package of examples which need to be independently
> upgradable and located in the main directory.
> 
> Suggestions?
> I'm thinking disutils would make for the easiest method but as far as I
> can see you can't run a script on finishing the installation or get the
> user to select from a directory. Nor does it seem contain methods to
> find windows programs without downloading pythonwin32 as well.
> 
> For the examples, NSIS or equivalent looks to be what I need but overly
> complicated. Can you combine NSIS and disutils? Or should I just use
> NSIS without disutils.
> 
> Advice appreciated. The first time I've released software before ;-)
> 
> Guy
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Encoding of file names

2005-12-09 Thread utabintarbo
Part of the reason (I think) is that our CAD/Data Management system
(which produces the aforementioned .MODEL files) substitutes (stupidly,
IMNSHO) non-printable characters for embedded spaces in file names.
This is part of what leads to my consternation here.

And yeah, Windows isn't helping matters much. No surprise there. :-P

Just for s&g's, I ran this on python 2.3 on knoppix:

>>> DIR = os.getcwd()
>>> files = os.listdir(DIR)
>>> file = files[-1]
>>> print file
L07JS41C.04389525AA.QTR±INR.E´C-P.D11.081305.P2.KPF.model
>>> print repr(file)
'L07JS41C.04389525AA.QTR\xb1INR.E\xb4C-P.D11.081305.P2.KPF.model'
>>> fullname = os.path.join(DIR, file)
>>> print os.path.isfile(fullname)
True  <--- It works fine here
>>> print os.path.isdir(fullname)
False
>>> files = os.listdir(unicode(DIR))
>>> file = files[-1]
>>> print file
L07JS41C.04389525AA.QTR±INR.E´C-P.D11.081305.P2.KPF.model
>>> print repr(file)
'L07JS41C.04389525AA.QTR\xb1INR.E\xb4C-P.D11.081305.P2.KPF.model'
>>> fullname = os.path.join(DIR, file)
>>> print os.path.isfile(fullname)
True  <--- It works fine here
too!
>>> print os.path.isdir(fullname)
False
>>>

This is when mounting the same samba share in Linux. This tends to
support Tom's point re:the "non-roundtrippability" thing.

Thanks again to all.

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


Re: slice notation as values?

2005-12-09 Thread Duncan Booth
Antoon Pardon wrote:

>> If the user can write
>>
>>for key in tree['a':'b']:
>>
>> then he can write:
>>
>>for key in tree['a':'b'].iteritems():
> 
> No he can't. tree['a':'b'] would provide a list
> of keys that all start with an 'a'. Such a list
> doesn't have an iteritems method. It wouldn't even
> contain the information to construct items.

Why would it produce a list?

Slicing a *list* produces a list, slicing a tuple produces a tuple, slicing 
a string produces a string. I would expect slicing any other type would 
also return you a new object of the same type. Thats what the code sample I 
posted earlier does.
-- 
http://mail.python.org/mailman/listinfo/python-list


Dectecting dir changes

2005-12-09 Thread chuck
I need to write a daemon for Solaris that monitors a directory for
incoming FTP transfers.  Under certain conditions, when the transfer is
complete I need to send an email notification, and do other stuff.
Win32 provides FindFirstChangeNotification(), but as best I can tell
this isn't supported on Solaris.

I am thinking of using the approach suggested here
http://tgolden.sc.sabren.com/python/win32_how_do_i/watch_directory_for_changes.html
which is:

import os, time
path_to_watch = "."
before = dict ([(f, None) for f in os.listdir (path_to_watch)])
while 1:
  time.sleep (10)
  after = dict ([(f, None) for f in os.listdir (path_to_watch)])
  added = [f for f in after if not f in before]
  removed = [f for f in before if not f in after]
  if added: print "Added: ", ", ".join (added)
  if removed: print "Removed: ", ", ".join (removed)
  before = after

My concern with this is that a change may be detected before the ftp
daemon process is done writing the file to disk.  I don't want to take
any action until the file is written and closed.  I know that I could
pole a new file looping to check to see if it's file size is changing
but the timing of such a loop is subject to I/O buffering and is
otherwise not elegant.

Googling shows other solutions using fcntl
(http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/217829) but it
appears that this only works on Linux.

While I'm at it I'm going to throw in a grump about the Python
documentation of fcntl.  The doco indicates to read the source for
fcntl.py to lookup the constants representing the different types of
events/signals that are avaiable.  However fcntl on some platforms
seems to be implemented as a binary leaving no way to look up the
contants for the platform.

Suggestions?

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


Re: pyparsing and LaTeX?

2005-12-09 Thread Tim Arnold

>"Ezequiel, Justin" <[EMAIL PROTECTED]> wrote in message 
> >news:[EMAIL PROTECTED]
>> Anyone parsing simple LaTeX constructs with pyparsing?
>
> Greetings Tim,
>
> Have always wanted a way to parse LaTeX myself.
> Unfortunately, I have been moved to a different project.
> However, I am still very much interested.
> Did you ever get a reply?

Hi Justin,
Yes, I did, from the pyparsing forum on Sourceforge. Paul's responses are 
excellent and his help for my simple needs really got me started.
http://pyparsing.sourceforge.net/

For now I'm working on a tag translator to convert from one LaTeX tagset to 
another, which is a pretty simple task compared to writing a full parser 
like pyLaTeX
http://pylatex.sourceforge.net/

--Tim


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


Re: efficient 'tail' implementation

2005-12-09 Thread Colin J. Williams
[EMAIL PROTECTED] wrote:
> hi
> 
> I have a file which is very large eg over 200Mb , and i am going to use
> python to code  a "tail"
> command to get the last few lines of the file. What is a good algorithm
> for this type of task in python for very big files?
> Initially, i thought of reading everything into an array from the file
> and just get the last few elements (lines) but since it's a very big
> file, don't think is efficient. 
> thanks
> 
If your file is seekable you could consider using file's seek function.

This would appear to give you a way of just working with the last, say 1mb.

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


heartbeats

2005-12-09 Thread Yves Glodt
Hi,

I need to write a heartbeat solution to monitor some external clients, 
and what is different as in the examples that I have seen so far is that 
I want my central server to poll the clients, and not the clients 
"pinging" the central server.

In detail I need a daemon on my central server which e.g. which in a 
loop pings (not really ping but you know what I mean) each 20 seconds 
one of the clients.

The only thing the client has to do is to accept the connection. 
(optionally sending back some bytes). If it refuses it is assumed to be 
offline.

My central server, and this is important, should have a short timeout. 
If one client does not respond because it's offline, after max. 10 
seconds the central server should continue with the next client.


Which python functions would be the most convenient for this application?

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


Re: uuDecode problem

2005-12-09 Thread Alex Martelli
py <[EMAIL PROTECTED]> wrote:

> Alex Martelli wrote:
> I suggest a redesign...!
> 
> 
> What would you suggest?  I have to encode/decode in chunks b/c of the
> 45 byte limitation.

Not quite:

>>> s=45*'v'
>>> a=binascii.b2a_uu(s)
>>> len(a)
62
>>> b=binascii.a2b_uu(a)
>>> len(b)
45
>>> b==s
True
>>> 

I.e., you can pass to a2b_uu ANY string (and ONLY such a string, not,
e.g., a slice or single char of it, as you're trying to do) that's a
result of a b2a_uu call; the length limitation applies only the other
way.

I join /F in suggesting yo use binascii the standard way, but, at any
rate, you should at least redesign your decoding strategy so it only
calls a2b_uu on strings which are the results of b2a_uu calls.


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


Re: Another newbie question

2005-12-09 Thread Alex Martelli
Mike Meyer <[EMAIL PROTECTED]> wrote:

> [EMAIL PROTECTED] (Alex Martelli) writes:
> > Mike Meyer <[EMAIL PROTECTED]> wrote:
> >> My standard object interface is modeled after Meyer's presentation in
> >> OOSC: an objects state is manipulated with methods and examined with
> >> attributes; manipulating attributes doesn't change the internal state
> >> of the object. This makes it possible to change the internal
> >> representation of a class without having to change all the clients of
> >> the class to match.
   ...
> On the other hand, Eiffel specifically forbids setting attributes
> directly, except for the object they belong to. This is done to
> enforce the design goals I stated above: attributes are the "readouts"

We must have a different meaning in mind for the word "goal"; I take it
to mean, as a web definition says, "the state of affairs that a plan is
intended to achieve".  By this definition, your second quoted sentence
identifies a goal, but the former one doesn't -- it's a MEANS, not an
END, as strongly confirmed by the words "This makes it possible to...".

Enabling internals' changes is a goal, and an important one; doing so by
forbidding the setting of attributes is one way to help achieve it, but
properties are another way, and as a matter of language design it
appears to me to be a better approach.  I haven't used Eiffel "in
anger", just evaluated and rejected it years ago for an employer, but I
have used other languages such as ObjectiveC which follow a similar
approach, so my preference is based on some experience (Ruby, though
strongly influenced by Smalltalk, does let you define attribute-setter
methods, with a net effect similar to Deplhi's or Python's properties).

> for an object, and methods are the knobs/dials/etc. This also ties in
> with the compiler having facilities to check class invariants. If you
> allow assignments to attributes in other classes, the assignments have
> to generate code to check the invariants every time you have such an
> assignment, otherwise a future attribute read may come from an object
> in an invalid state. If you only allow attributes to be set by the
> owning objects methods, then you only have to check the invariants on
> method exit.

What classes' invariants do you have to check in those cases?  E.g.,
consider zim.foo.bar.baz() -- you do have to check the invariants of
bar, foo AND zim, right?  And you must do it with code placed inline
after this specific call, since not all calls to that baz method must
check invariants in foo and zim.  So, what's different about having to
generate just the same inline code for, say, zim.foo.bar=baz ?  Since
your compiler must be ready to generate such checks anyway, forbidding
the second form appears to have no pluses.  Or, in other words, having
to spell zim.foo.bar=baz as zim.foo.set_bar(baz) [[and having to code
boilerplate to implement set_bar]] is a style-choice (common to Eiffel
and Smalltalk, and others) which I consider inferior to the alternative
choice you find in Ruby or Python [[allowing autogeneration of the
implied attribute-setter method if necessary]].

> Different idioms for different languages. Trying to write Python in
> Eiffel doesn't work any better than trying to write C++ in Python.

Absolutely, but here I was discussing language design, not usage of
languages whose design is an external "given".


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


Re: Dectecting dir changes

2005-12-09 Thread Fredrik Lundh
"chuck" <[EMAIL PROTECTED]> wrote:

> I need to write a daemon for Solaris that monitors a directory for
> incoming FTP transfers.  Under certain conditions, when the transfer is
> complete I need to send an email notification, and do other stuff.
> Win32 provides FindFirstChangeNotification(), but as best I can tell
> this isn't supported on Solaris.
>
> I am thinking of using the approach suggested here
> http://tgolden.sc.sabren.com/python/win32_how_do_i/watch_directory_for_changes.html
> which is:
>
> import os, time
> path_to_watch = "."
> before = dict ([(f, None) for f in os.listdir (path_to_watch)])
> while 1:
>  time.sleep (10)
>  after = dict ([(f, None) for f in os.listdir (path_to_watch)])
>  added = [f for f in after if not f in before]
>  removed = [f for f in before if not f in after]
>  if added: print "Added: ", ", ".join (added)
>  if removed: print "Removed: ", ", ".join (removed)
>  before = after
>
> My concern with this is that a change may be detected before the ftp
> daemon process is done writing the file to disk.  I don't want to take
> any action until the file is written and closed.  I know that I could
> pole a new file looping to check to see if it's file size is changing
> but the timing of such a loop is subject to I/O buffering and is
> otherwise not elegant.

there is no reliable way to do this, unless you can modify the sending
application to use a temporary filename during transfer (or find an FTP
server that does this automatically), or you're willing to write your own
FTP responder.

checking the mtime for new files might be good enough for some use
cases (poll the directory at regular intervals; if a file is newer than X
seconds, assume it's still being updated).

 



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


Re: Dectecting dir changes

2005-12-09 Thread Sybren Stuvel
chuck enlightened us with:
> The doco indicates to read the source for fcntl.py to lookup the
> constants representing the different types of events/signals that
> are avaiable.  However fcntl on some platforms seems to be
> implemented as a binary leaving no way to look up the contants for
> the platform.

'pydoc fcntl' works fine on my system, even though the module is a
binary .so file.

Sybren
-- 
The problem with the world is stupidity. Not saying there should be a
capital punishment for stupidity, but why don't we just take the
safety labels off of everything and let the problem solve itself? 
 Frank Zappa
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: heartbeats

2005-12-09 Thread Sybren Stuvel
Yves Glodt enlightened us with:
> In detail I need a daemon on my central server which e.g. which in a
> loop pings (not really ping but you know what I mean) each 20
> seconds one of the clients.

You probably mean "really a ping, just not an ICMP echo request".

> The only thing the client has to do is to accept the connection.
> (optionally sending back some bytes). If it refuses it is assumed to
> be offline.

Ok, fair enough.

> My central server, and this is important, should have a short
> timeout.  If one client does not respond because it's offline, after
> max. 10 seconds the central server should continue with the next
> client.

I'd write a single function that pings a client and waits for a
response/timeout. It then should return True if the client is online,
and False if it is offline. You can then use a list of clients and the
filter() function, to retrieve a list of online clients.

Sybren
-- 
The problem with the world is stupidity. Not saying there should be a
capital punishment for stupidity, but why don't we just take the
safety labels off of everything and let the problem solve itself? 
 Frank Zappa
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Dectecting dir changes

2005-12-09 Thread chuck
Is this on Solaris?

I think you may have missed my point.  I don't have fcntl.py on my
Solaris box so how do I know what signals that I can used to monitor a
directory for modification.  In other words will the following work?

fcntl.fcntl(self.fd, fcntl.F_NOTIFY,
fcntl.DN_DELETE|fcntl.DN_CREATE|fcntl.DN_MULTISHOT)

Without fcntl source, which the python documentation suggests that I
look at, I don't know if any of these constants apply to the Solaris
platform.

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


Re: Dr. Dobb's Python-URL! - weekly Python news and links (Dec 7)

2005-12-09 Thread Cameron Laird
In article <[EMAIL PROTECTED]>,
David Isaac <[EMAIL PROTECTED]> wrote:
>
>"Cameron Laird" <[EMAIL PROTECTED]> wrote in message
>news:[EMAIL PROTECTED]
>> Jibes against the lambda-clingers lead eventually to serious
>> questions of style in regard to variable namespacing,
>> lifespan, cleanup, and so on:
>>
>http://groups.google.com/group/comp.lang.python/browse_thread/thread/ad0e15cb6b8f2c32/
>
>
>#evaluate polynomial (coefs) at x using Horner's ruledef horner(coefs,x):
>return reduce(lambda a1,a2: a1*x+a2,coefs)'Nuf said.Alan Isaac
>
>

No.

That is, this follow-up does *not* say enough for me to have confidence
of its intent.  Leaving aside such formalities as the relation between
"Alan Isaac" and "David Isaac", I *think* you're supporting a claim
about the value of lambda with a specific example.  Do I have that
right?  Are you saying that your definition of horner() would suffer
greatly without lambda?
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Dectecting dir changes

2005-12-09 Thread Fredrik Lundh
"chuck" wrote:

> I think you may have missed my point.  I don't have fcntl.py on my
> Solaris box so how do I know what signals that I can used to monitor a
> directory for modification.  In other words will the following work?
>
> fcntl.fcntl(self.fd, fcntl.F_NOTIFY,
> fcntl.DN_DELETE|fcntl.DN_CREATE|fcntl.DN_MULTISHOT)
>
> Without fcntl source, which the python documentation suggests that I
> look at, I don't know if any of these constants apply to the Solaris
> platform.

$ man fcntl

 



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


Re: pickle - recursion - stack size limit - MS windows

2005-12-09 Thread Christian Tismer
Andy Leszczynski wrote:
> I need to pickle quite complex objects and first limitation was default
> 200 for the recursion. sys.setrecursionlimit helped, but still bigger
> objects fail to be pickled because of XP stack size limitation.
> 
> Any idea how to get around the problem ...

If you can live with Python 2.3 at the moment (2.4.2 support is
expected after PyCon 2006 the latest), you can just use Stackless
Python. It is not limited by stack size and also includes
a version of cPickle that is unlimited.

cheers -- chris

-- 
Christian Tismer :^)   
tismerysoft GmbH : Have a break! Take a ride on Python's
Johannes-Niemeyer-Weg 9A :*Starship* http://starship.python.net/
14109 Berlin : PGP key -> http://wwwkeys.pgp.net/
work +49 30 802 86 56  mobile +49 173 24 18 776  fax +49 30 80 90 57 05
PGP 0x57F3BF04   9064 F4E1 D754 C2FF 1619  305B C09C 5A3B 57F3 BF04
  whom do you want to sponsor today?   http://www.stackless.com/
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Dectecting dir changes

2005-12-09 Thread chuck
ty - more useful than 'works here'

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


How to find the type ...

2005-12-09 Thread Lad
Hello
How can I find out in Python whether the operand is integer or a
character  and change from char to int ?
Regards,
L.

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


Re: what's wrong with "lambda x : print x/60,x%60"

2005-12-09 Thread tutufan
Re using the same variable name over and over for different objects in
the same scope:

Argh--don't do this.  It makes a mess for the guy (or gal) that comes
after you and has to understand the code.  Our forefathers fought and
died so that we could have as many unique variable names as we like.
So use them!

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


Re: heartbeats

2005-12-09 Thread Steve Holden
Yves Glodt wrote:
> Hi,
> 
> I need to write a heartbeat solution to monitor some external clients, 
> and what is different as in the examples that I have seen so far is that 
> I want my central server to poll the clients, and not the clients 
> "pinging" the central server.
> 
> In detail I need a daemon on my central server which e.g. which in a 
> loop pings (not really ping but you know what I mean) each 20 seconds 
> one of the clients.
> 
> The only thing the client has to do is to accept the connection. 
> (optionally sending back some bytes). If it refuses it is assumed to be 
> offline.
> 
> My central server, and this is important, should have a short timeout. 
> If one client does not respond because it's offline, after max. 10 
> seconds the central server should continue with the next client.
> 
> 
> Which python functions would be the most convenient for this application?
> 
> Best regards,
> Yves

http://www.google.com/search?q=python+recipe+heartbeat

=>

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

regards
  Steve
-- 
Steve Holden   +44 150 684 7255  +1 800 494 3119
Holden Web LLC www.holdenweb.com
PyCon TX 2006  www.python.org/pycon/

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


Re: How to find the type ...

2005-12-09 Thread robert . dowell
>>> thisisastring = "1"
>>> thisisanint = 1
>>> type(thisisastring)

>>> type(thisisanint)

>>> thisisastring = int(thisisastring)
>>> thisisanint = str(thisisanint)
>>> type(thisisastring)

>>> type(thisisanint)

>>>

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


Re: Dectecting dir changes

2005-12-09 Thread Steve Holden
chuck wrote:
> I need to write a daemon for Solaris that monitors a directory for
> incoming FTP transfers.  Under certain conditions, when the transfer is
> complete I need to send an email notification, and do other stuff.
> Win32 provides FindFirstChangeNotification(), but as best I can tell
> this isn't supported on Solaris.
> 
[...]
> 
> Suggestions?
> 
Write an FTP server in Python, then it will know exactly when each file 
transfer is complete, and it can do the mailing itself!

regards
  Steve
-- 
Steve Holden   +44 150 684 7255  +1 800 494 3119
Holden Web LLC www.holdenweb.com
PyCon TX 2006  www.python.org/pycon/

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


Re: How to find the type ...

2005-12-09 Thread Magnus Lycka
Lad wrote:
> Hello
> How can I find out in Python whether the operand is integer or a
> character  and change from char to int ?

I'm not sure what you mean by "character" in a Python context.
A string? "i = int(i)" will make sure both 5 and "5" are used
as 5, and "five" will be rejected with a ValueError.

 >>> def f(x):
... i = int(x)
... print i, type(i)
...
 >>> f(5)
5 
 >>> f('42')
42 
 >>> f('infinity')
Traceback (most recent call last):
   File "", line 1, in ?
   File "", line 2, in f
ValueError: invalid literal for int(): infinity
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: uuDecode problem

2005-12-09 Thread py
Thanks...I think base64 will work just fine...and doesnt seem to have
45 byte limitations, etc.

Thanks.

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


Re: How to find the type ...

2005-12-09 Thread Steve Holden
Lad wrote:
> Hello
> How can I find out in Python whether the operand is integer or a
> character  and change from char to int ?
> Regards,
> L.
> 
Easiest would just be to apply the int() type function to whatever you 
have and trap any resulting exception.

  >>> getInt(1)
1
  >>> getInt("32767")
32767
  >>> getInt('have a banana')
Traceback (most recent call last):
   File "", line 1, in ?
   File "", line 5, in getInt
ValueError: getInt called with non-integer value

Unfortunately we should also consider:


  >>> getInt("3.14159")
Traceback (most recent call last):
   File "", line 1, in ?
   File "", line 5, in getInt
ValueError: getInt called with non-integer value
  >>> getInt(3.14159)
3
  >>>

which may or may not be what you want.

regards
  Steve
-- 
Steve Holden   +44 150 684 7255  +1 800 494 3119
Holden Web LLC www.holdenweb.com
PyCon TX 2006  www.python.org/pycon/

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


Re: How to find the type ...

2005-12-09 Thread Rocco Moretti
[EMAIL PROTECTED] wrote:
thisisastring = "1"
thisisanint = 1
type(thisisastring)
> 
> 
> 
type(thisisanint)
> 
> 
> 
thisisastring = int(thisisastring)
thisisanint = str(thisisanint)
type(thisisastring)
> 
> 
> 
type(thisisanint)
> 
> 

 >>> print repr(thisisastring)
1
 >>> print repr(thisisanint)
'1'
 >>> thisisastring = 'a'
 >>> thisisanint = 98
 >>> thisisastring = ord(thisisastring) #Using ASCII rep.
 >>> thisisanint = chr(thisisanint)
 >>> type(thisisastring)

 >>> type(thisisanint)

 >>> print repr(thisisastring)
97
 >>> print repr(thisisanint)
'b'




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


Re: How to find the type ...

2005-12-09 Thread Steven Bethard
Lad wrote:
> How can I find out in Python whether the operand is integer or a
> character and change from char to int ?

Python doesn't have a separate character type, but if you want to 
convert a one-character string to it's ASCII number, you can use ord():

 >>> ord('A'), ord('z')
(65, 122)

The answer to your first question is that you probably don't want to. 
You probably want two separate functions, one that takes an integer and 
one that takes a character.  What's your actual function look like?

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


Re: How to find the type ...

2005-12-09 Thread Xavier Morel
Lad wrote:
> Hello
> How can I find out in Python whether the operand is integer or a
> character  and change from char to int ?
> Regards,
> L.
> 
You may want to try the "type" command.
And there is no character type in cPython (unless you're using ctypes 
that is)

There is not much point though, you can use the "int" construct on your 
expression, Python'll try to convert the expression to an integer by 
itself (and throw an exception if it can't)

 >>> a = 3
 >>> int(a)
3
 >>> a = "3"
 >>> int(a)
3
 >>> a = "e"
 >>> int(a)

Traceback (most recent call last):
   File "", line 1, in -toplevel-
 int(a)
ValueError: invalid literal for int(): e
 >>>

You can even do base conversions with it:

 >>> a="0xe"
 >>> int(a)

Traceback (most recent call last):
   File "", line 1, in -toplevel-
 int(a)
ValueError: invalid literal for int(): 0xe
 >>> int(a,16) # Silly me, 0xe is not a decimal
14
 >>>
-- 
http://mail.python.org/mailman/listinfo/python-list


Overloading

2005-12-09 Thread Johannes Reichel
Hi!

In C++ you can overload functions and constructors. For example if I have a
class that represents a complex number, than it would be nice if I can
write two seperate constructors

class Complex:

def __init__(self):
self.real=0
self.imag=0

def __init__self(self,r,i): 
self.real=r
self.imag=i


How would I do this in python?

And by the way, is it possible to overload operators like +,-,*?

def operator+(self,complex2):
Complex res
res.real=self.real+complex2.real
res.imag=self.imag+complex2.imag

return res


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


Re: Dectecting dir changes

2005-12-09 Thread Jean-Paul Calderone
On Fri, 09 Dec 2005 16:50:05 +, Steve Holden <[EMAIL PROTECTED]> wrote:
>chuck wrote:
>> I need to write a daemon for Solaris that monitors a directory for
>> incoming FTP transfers.  Under certain conditions, when the transfer is
>> complete I need to send an email notification, and do other stuff.
>> Win32 provides FindFirstChangeNotification(), but as best I can tell
>> this isn't supported on Solaris.
>>
>[...]
>>
>> Suggestions?
>>
>Write an FTP server in Python, then it will know exactly when each file
>transfer is complete, and it can do the mailing itself!

Or use an existing Python FTP server! ;)

Untested:

  from zope.interface import implements
  from twisted.protocols import ftp

  class UploadNotifyingAvatar(ftp.FTPShell):
  def uploadCompleted(self, path):
  """
  Override me!
  """
  # But, for example:
  from twisted.mail import smtp
  smtp.sendmail(
  '127.0.0.1',
  '[EMAIL PROTECTED]',
  ['[EMAIL PROTECTED]'],
  'Hey!  Someone uploaded a file: %r' % (path,))

  def openForWriting(self, path):
  writer = ftp.FTPShell.openForWriting(path)
  return CloseNotifyingWriter(
  writer,
  lambda: self.uploadCompleted(path))
  
  class CloseNotifyingWriter(object):
  implements(ftp.IWriteFile)

  def __init__(self, wrappedWriter, onUploadCompletion):
  self.wrappedWriter = wrappedWriter
  self.onUploadCompletion = onUploadCompletion

  def receive(self):
  receiver = self.wrappedWriter.receive()
  receiver.addCallback(
  CloseNotifyingReceiver,
  self.onUploadCompletion)
  return receiver

  class CloseNotifyingReceiver(object):
  def __init__(self, wrappedReceiver, onUploadCompletion):
  self.wrappedReceiver = wrappedReceiver
  self.onUploadCompletion = onUploadCompletion

  def registerProducer(self, producer, streaming):
  return self.wrappedReceiver.registerProducer(producer, streaming)

  def unregisterProducer(self):
  # Upload's done!
  result = self.wrappedReceiver.unregisterProducer()
  self.onUploadCompletion()
  return result

  def write(self, bytes):
  return self.wrappedReceiver.write(bytes)

Hook it up to a Realm and a Portal and override uploadCompleted to taste and 
you've got an FTP server that should do you (granted, it's a little long - some 
of this code could be be factored to take advantage of parameterizable 
factories to remove a bunch of the boilerplate).

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


Re: Overloading

2005-12-09 Thread Jean-Paul Calderone
On Fri, 09 Dec 2005 18:29:12 +0100, Johannes Reichel <[EMAIL PROTECTED]> wrote:
>Hi!
>
>In C++ you can overload functions and constructors. For example if I have a
>class that represents a complex number, than it would be nice if I can
>write two seperate constructors
>
>class Complex:
>
>def __init__(self):
>self.real=0
>self.imag=0
>
>def __init__self(self,r,i):
>self.real=r
>self.imag=i
>

class Complex:
def __init__(self, r=0, i=0):
self.real = r
self.imag = i

>
>How would I do this in python?
>
>And by the way, is it possible to overload operators like +,-,*?
>
>def operator+(self,complex2):
>Complex res
>res.real=self.real+complex2.real
>res.imag=self.imag+complex2.imag
>
>return res

def __add__(self, complex2):
res = Complex(self.real + complex2.real, self.imag + complex2.imag)
return res

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


Re: Overloading

2005-12-09 Thread jepler
On Fri, Dec 09, 2005 at 06:29:12PM +0100, Johannes Reichel wrote:
> Hi!
> 
> In C++ you can overload functions and constructors. For example if I have a
> class that represents a complex number, than it would be nice if I can
> write two seperate constructors

Python doesn't support this, but it does support default arguments:
class Complex:
def __init__(self, real=0, imag=0):
self.real = real
self.imag = imag

> And by the way, is it possible to overload operators like +,-,*?
> 
> def operator+(self,complex2):

The special methods have names like __add__.
http://docs.python.org/ref/numeric-types.html

Jeff


pgpBaCTVXSEn0.pgp
Description: PGP signature
-- 
http://mail.python.org/mailman/listinfo/python-list

Re: How to find the type ...

2005-12-09 Thread Martin Christensen
-BEGIN PGP SIGNED MESSAGE-
Hash: SHA1

> "Lad" == Lad  <[EMAIL PROTECTED]> writes:
Lad> How can I find out in Python whether the operand is integer or a
Lad> character and change from char to int ?

In Python, the canonical way of doing this would be to simply assume
that the argument can be converted to an integer and catch any errors
that occur:

def f(x):
try:
x = int(x)
except ValueError:
# It's a non-number string.
do stuff
except TypeError:
# It's neither a number nor a string.
do some other stuff

Martin
-BEGIN PGP SIGNATURE-
Version: GnuPG v1.4.1 (GNU/Linux)
Comment: Using Mailcrypt+GnuPG 

iEYEARECAAYFAkOZwtkACgkQYu1fMmOQldXEzACgqdDVvx29UBVSIfQWnGRiAAk9
xPsAn0yN5jWrUN+6SKIHdwtILRBVyQwR
=HZQq
-END PGP SIGNATURE-
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: HTML parsing/scraping & python

2005-12-09 Thread alex_f_il
Take a look at SW Explorer Automation
(http://home.comcast.net/~furmana/SWIEAutomation.htm)(SWEA). SWEA
creates an object model (automation interface) for any Web application
running in Internet Explorer. It supports all IE functionality:frames,
java script,  dialogs, downloads.

The runtime can  also work under  non-interactive user accounts
(ASP.NET or service applications) on Window 2000/2003 server or Windows
XP.

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


Re: heartbeats

2005-12-09 Thread Tom Anderson
On Fri, 9 Dec 2005, Sybren Stuvel wrote:

> Yves Glodt enlightened us with:
>
>> In detail I need a daemon on my central server which e.g. which in a 
>> loop pings (not really ping but you know what I mean) each 20 seconds 
>> one of the clients.

Do you mean pings one client every 20 sec, or each client every 20 sec?

> You probably mean "really a ping, just not an ICMP echo request".

What's a real ping, if not an ICMP echo request? That's pretty much the 
definitive packet for internetwork groping as far as i know. I think that 
the more generic sense of ping is a later meaning (BICouldVeryWellBW).

>> My central server, and this is important, should have a short timeout. 
>> If one client does not respond because it's offline, after max. 10 
>> seconds the central server should continue with the next client.
>
> I'd write a single function that pings a client and waits for a 
> response/timeout. It then should return True if the client is online, 
> and False if it is offline. You can then use a list of clients and the 
> filter() function, to retrieve a list of online clients.

That sounds like a good plan.

To do the timeouts, you want the settimeout method on socket:



import socket

def default_validate(sock):
return True

def ping(host, port, timeout=10.0, validate=default_validate):

"""Ping a specified host on the specified port. The timeout (in
seconds) and a validation function can be set; the validation
function should accept a freshly opened socket and return True if
it's okay, and False if not. This functions returns True if the
specified target can be connected to and yields a valid socket, and
False otherwise.

"""
sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
sock.settimeout(timeout)
try:
sock.connect((host, port))
except socket.error:
return False
ok = validate(sock)
sock.close()
return ok



A potential problem with this is that in the worst case, you'll be 
spending a little over ten seconds on each socket; if you have a lot of 
sockets, that might mean you're not getting through them fast enough. 
There are two ways round this: handle several pings in parallel using 
threads, or use non-blocking sockets to handle several at once with a 
single thread.

tom

-- 
everything from live chats and the Web, to the COOLEST DISGUSTING
PORNOGRAPHY AND RADICAL MADNESS!!
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Dectecting dir changes

2005-12-09 Thread chuck
Hmmm, that is an interesting idea.  I've noticed the new book on
Twisted, thinking about picking it up.

I assume that this little snippet will handle multiple/concurrent
incoming transfers via threading/sub-process, is scalable, secure, etc?

I could even run it on a non-standard port making it a bit more
(ob)secure.

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


Re: Documentation suggestions

2005-12-09 Thread Trent Mick
[EMAIL PROTECTED] wrote]
> 
> Trent> Nah, the Try Ruby thing is mostly faking it (I believe) rather
> Trent> than running an actually Ruby interactive session ("bastion'ed"
> Trent> or not).
> 
> I don't think so.  I tried typing some stuff at the prompt that it wasn't
> asking for, like "x = [1,2,3]" followed by "x * 5" when it was asking me to
> type "2 + 6".  It evaluated both properly as far as I could tell.  

Yah. My guess at what he is doing (and the way I'd probably do this for
Python) is to compile each statement, only allow certain constructs
(like assigning to a variable, defining literals, basic operator usage,
maybe some control flow statements -- although I didn't get that far)
and then execute those.

> OTOH, it
> hung when I entered "def fib(n)".  Never got to the next prompt.  

Punted on continued/multi-line statements maybe?

> Looking at
> the Javascript, it appears to be some sort of Ajaxian thing...

Yup.


Trent

-- 
Trent Mick
[EMAIL PROTECTED]
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Dr. Dobb's Python-URL! - weekly Python news and links (Dec 7)

2005-12-09 Thread Cameron Laird
In article <[EMAIL PROTECTED]>,
I reported:
.
.
.
>"Python has more web application frameworks than keywords." - Skip
>Montanaro (but probably others going back years)
.
.
.
Incorrect.  Thanks to Fredrik Lundh, who rather miraculously intuited
that this was a mechanical error, and that my real intention was to
highlight what Harald Armin Massa (and others, most likely) had earlier
quipped:
  http://article.gmane.org/gmane.comp.python.general/435579 
  http://online.effbot.org/2004_06_01_archive.htm#europython-1
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: small inconsistency in ElementTree (1.2.6)

2005-12-09 Thread Damjan
>> Do I need to check the output of ElementTree everytime, or there's some
>> hidden switch to change this behaviour?
>
> no.
>
> ascii strings and unicode strings are perfectly interchangable, with some
> minor exceptions.

It's not only translate, it's decode too... probably other methods and
behaviour differ too.
And the bigger picture, string objects are really only byte sequences,
while
text is consisted of characters and that's what unicode strings are
for,
strings-made-of-characters.

It seems to me more logical that an et.text to be a unicode object
always.
It's text, right!

> if you find yourself using translate all the time
> (why?), add an explicit conversion to the translate code.

I'm using translate because I need it :)

I'm currently just wrapping anything from ElementTree in unicode(), but
this
seems like an ugly step.

> (fwiw, I'd say this is a bug in translate rather than in elementtree)

I wonder what the python devels will say? ;)

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


Re: Dr. Dobb's Python-URL! - weekly Python news and links (Dec 7)

2005-12-09 Thread skip

Cameron> In article <[EMAIL PROTECTED]>,
Cameron> I reported:

>> "Python has more web application frameworks than keywords." - Skip
>> Montanaro (but probably others going back years)

Cameron> Incorrect.  Thanks to Fredrik Lundh ...

Yeah, I wondered about that.  I was only parroting what I'd seen in Harald's
post.  I just couldn't remember who'd said it.

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


getting host and path from url

2005-12-09 Thread Steve Young
Hi, this is probably an easy question but is there a way to get the host and path seperatly out of an url? Example:url = "">and i want some way of getting:host = http://news.yahoo.com  and  path = /fc/world/iraqthanks.-Steve     
	
		Yahoo! Shopping 
Find Great Deals on Holiday Gifts at Yahoo! Shopping -- 
http://mail.python.org/mailman/listinfo/python-list

Re: small inconsistency in ElementTree (1.2.6)

2005-12-09 Thread Fredrik Lundh
Damjan wrote:

> > ascii strings and unicode strings are perfectly interchangable, with some
> > minor exceptions.
>
> It's not only translate, it's decode too...

why would you use decode on the strings you get back from ET ?

> probably other methods and behaviour differ too.
>
> And the bigger picture, string objects are really only byte sequences

not if they contain ASCII characters.

> while text is consisted of characters and that's what unicode strings
> are for, strings-made-of-characters.
>
> It seems to me more logical that an et.text to be a unicode object
> always.  It's text, right!
>
> > if you find yourself using translate all the time
> > (why?), add an explicit conversion to the translate code.
>
> I'm using translate because I need it :)
>
> I'm currently just wrapping anything from ElementTree in unicode(), but
> this seems like an ugly step.
>
> > (fwiw, I'd say this is a bug in translate rather than in elementtree)
>
> I wonder what the python devels will say? ;)

well, you're talking to the developer who wrote the original Unicode
implementation...





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


How to detect the presence of a html file

2005-12-09 Thread Phoe6
Operating System: Windows
Python version: 2.4

I have bookmarks.html and wumpus.c under my c:

When I tried to check the presence of the bookmarks.html, I fail.

>>> os.path.isfile('c:\bookmarks.html')
False
>>> os.path.isfile('c:\wumpus.c')
True

>>> os.path.exists('c:\wumpus.c')
True
>>> os.path.exists('c:\bookmarks.html')
False

>>> os.access('c:\bookmarks.html',os.F_OK)
False

I can assure you that c:\bookmarks.html exists! and I opened this and
checked it in the browser as well.

Why is this behavior? And How Should I check for the presence of this
file?

Any help appreciated.

Thanks!
Senthil

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


Re: Overloading

2005-12-09 Thread Christopher Subich
Johannes Reichel wrote:
> Hi!
> 
> In C++ you can overload functions and constructors. For example if I have a
> class that represents a complex number, than it would be nice if I can
> write two seperate constructors
> 
> class Complex:

Please do note, if you want this for the exact use of a Complex class, 
Python does have complex arithmetic built-in:

print (0 + 1j)

The other posted points are still valid (and generally applicable).
-- 
http://mail.python.org/mailman/listinfo/python-list


build_opener()

2005-12-09 Thread Steve Young
  Hello, I had a question about urllib2's  build_opener() statement. I am trying to just get the html from any  webpage as a string but I need everything on the page to be the same as  what it'd be if I would browse to that page (and at the very least, all  the href's). This is my code:  url = ''  req = Request(url)  f = build_opener().open(req)  page = f.read()  f.close()  return page  so looking at the source of the page browsing to the  page, one of the links has an href that looks like this:href = "">  http://news.yahoo.com/s/ap/20051118/ap_on_re_mi_ea/iraq_051118153857;_ylt=AiPsFWWIyLLbGdlCQFLMn8NX6GMA;_ylu=X3oDMTBiMW04NW9mBHNlYwMlJVRPUCUl   
   after running the code and looking at the returned page's same link, it looks like this:href = "">  http://192.168.23.106/s/ap/20051118/ap_on_re_mi_ea/iraq_051118153857it seems that everything after the semi-colon is missing after runningthe build_opener(). Is there a way that I can get the page as a stringwith all the links (href's) to not be missing anything? Thanks.-Steve   
	
		Yahoo! Shopping 
Find Great Deals on Holiday Gifts at Yahoo! Shopping 
	
		Yahoo! Shopping 
Find Great Deals on Holiday Gifts at Yahoo! Shopping -- 
http://mail.python.org/mailman/listinfo/python-list

Re: Using printf in a C Extension

2005-12-09 Thread Lonnie Princehouse
printf will generally work in C extensions (although, as others have
said, it goes to STDOUT which is not necessarily the same as Python
sys.stdout)

Try explicitly flushing the buffer with fflush(stdout)

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


small exe for cross compile for linux-mips tool chain

2005-12-09 Thread niclane
Hi, I'm trying to port the python interpreter to an AP running OpenWRT
with the linux-mips tool chain. I'd like to have the interpreter as
small as possible and was wanted see if others in the community had
done this and if I could learn from them.

I've gotten fairly far into the cross compilation but not the reduction
in size of the interpreter. The scripts I need to run import only the
following modules: serial, sys, time, string, cStringIO, and struct.

If I only need my interpreter to use these modules how do I compile
python for use with only these modules? Would it be possible to get it
down to about 300k? or what would be a resonable size achievable given
the limited set of modules I have need? Currently I've got a size of
1.3 meg!

Thanks for your help.

Nic

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


Catching error text like that shown in console?

2005-12-09 Thread Peter A.Schott
I know there's got to be an easy way to do this - I want a way to catch the
error text that would normally be shown in an interactive session and put that
value into a string I can use later.  I've tried just using a catch statement
and trying to convert the output to string, but this doesn't always work.  I
really don't have programs complex enough to do a lot of specific catching at
this point - I just want to know:
  1. something failed
  2. here's the error output/traceback

Anyone know how I can do this or if it's possible?  Didn't find anything doing a
search so I figured I'd hit up the experts.

Thanks.

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


Re: getting host and path from url

2005-12-09 Thread Michael Spencer
Steve Young wrote:
> Hi, this is probably an easy question but is there a way to get the host and 
> path seperatly out of an url? 
>   
>   Example:
>   
>   url = http://news.yahoo.com/fc/world/iraq
>   
>   and i want some way of getting:
>   
>   host = http://news.yahoo.com
>   and
>   path = /fc/world/iraq
>   
>   thanks.
>   
>   -Steve
>
>   
> 
>   
> -
> Yahoo! Shopping
>  Find Great Deals on Holiday Gifts at Yahoo! Shopping 
> 
check out urlparse in the stdlib

Michael

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


Re: How to detect the presence of a html file

2005-12-09 Thread Kent Johnson
Phoe6 wrote:
> Operating System: Windows
> Python version: 2.4
> 
> I have bookmarks.html and wumpus.c under my c:
> 
> When I tried to check the presence of the bookmarks.html, I fail.
> 
> 
os.path.isfile('c:\bookmarks.html')
> 
> False
> 
os.path.isfile('c:\wumpus.c')
> 
> True

The problem is that \ is special in string literals. \b is a backspace 
character, not the two-character sequence you expect. \w has no special 
meaning so it *is* the two-character sequence you expect.

  >>> len('\b')
1
  >>> len('\w')
2

The simplest fix is to use raw strings for all your Windows path needs:
os.path.isfile(r'c:\bookmarks.html')
os.path.isfile(r'c:\wumpus.c')

In raw strings the only \ escapes are \' and \", everything else is left 
alone.
  >>> len(r'\b')
2
  >>> len(r'\w')
2

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


new in programing

2005-12-09 Thread Efrain Marrero
i want to now how to do this in python
this is java


for(int i=1 ; i<=lim ; i++){

  for(int j=i+1; j<=lim+1; j++){

for(int k =j+1; k<=lim+2;k++){

for(int l=k+1 ; l<=lim+3;l++){

for(int m=l+1 ; m<=lim+4;m++){

for(int o=m+1 ; o<=lim+5;o++){

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


PHP = Perl Improved

2005-12-09 Thread Xah Lee
recently i got a project that involves the use of php. In 2 days, i
read almost the entirety of the php doc. Finding it a breeze because it
is roughly based on Perl, of which i have mastery.

i felt a sensation of neatness, as if php = Perl Improved, for a
dedicated job of server-side scripting. Everything is so-built-in, and
the integrated functions for web application programing such as
CGI/Database is so convenient. What a PRACTICALITY! It has gotten a
long way, even now with a independent interpreter and engine (Zend) for
embedded computation of any other mark-up lang. And, its array/hash is
kinda linguistically cleaner, by combining the two into one. (after
all, array indexes are unique, so they are denotationally and
mathematically list of keyed pairs (hashes) too) As for nested
structure, it does away with Perl's ${x}->{'whatnot'}[$x]->[$y{'z'}]
insanity. And I'm most impressed by its extremely well-written
documentation.

But as i know the lang more, my feeling changed, yet “Perl
Improved” is still apt, with a new interpretation.

see
http://tnx.nl/php

If Unix, Apache, Perl, MySQL etc shit can impact the world with
motherfucking evolutionary outrageous $free$ lies, why should we fault
Pretty Home Page?

 Xah
 [EMAIL PROTECTED]
 ∑ http://xahlee.org/

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

Re: Documentation suggestions

2005-12-09 Thread A.M. Kuchling
On Thu, 8 Dec 2005 18:17:59 +0100, 
Fredrik Lundh <[EMAIL PROTECTED]> wrote:
> cool.  can you post a sample page somewhere?

It's not terribly interesting at the moment.  The generated LaTeX looks like 
this:

\seeurl{http://effbot.org/librarybook/zlib.htm}{The zlib module}

And that gets formatted like other URL links.

BThe associated text currently isn't very helpful, but I'm not sure how
to make it better.  Maybe it could be "'', from ''
by ".  Then the text for your file would be "'The zlib module', 
from '(the eff-bot guide to) The Standard Python Library' by Fredrik Lundh."

Can I safely treat the dc:identifier field as the URL for the work as
a whole?  Then I could turn the book title into a hyperlink.

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


Re: How to detect the presence of a html file

2005-12-09 Thread Phoe6
Kent Johnson wrote:
> The problem is that \ is special in string literals. \b is a backspace
> character, not the two-character sequence you expect. \w has no special
> meaning so it *is* the two-character sequence you expect.

> The simplest fix is to use raw strings for all your Windows path needs:
> os.path.isfile(r'c:\bookmarks.html')
> os.path.isfile(r'c:\wumpus.c')

Thanks a lot, Kent!
I immediately recognized the problem from your reply.
I am just starting with python. :) 

Thanks again!

Regards,
Senthil

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


Re: new in programing

2005-12-09 Thread Brett Hoerner
Efrain Marrero wrote:
> i want to now how to do this in python
> this is java
>
>
> for(int i=1 ; i<=lim ; i++){
>
> for(int j=i+1; j<=lim+1; j++){
>
>   for(int k =j+1; k<=lim+2;k++){
>
>   for(int l=k+1 ; l<=lim+3;l++){
>
>   for(int m=l+1 ; m<=lim+4;m++){
>
>   for(int o=m+1 ; o<=lim+5;o++){

That code wouldn't run on a JVM, do you have the rest?  Or are we to
assume these loops do nothing and just close all the braces?

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


Re: Moving a package in cygwin

2005-12-09 Thread 63q2o4i02
Ok, thanks.  I actually hadn't considered the build system, figuring
that function names, etc., would somehow magically be "exported" to
anyone wanting to use the .pyd library.  

Michael

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


Re: Overloading

2005-12-09 Thread Josef Meile
>>In C++ you can overload functions and constructors. For example if I have a
>>class that represents a complex number, than it would be nice if I can
>>write two seperate constructors
> 
> 
> Python doesn't support this, but it does support default arguments:
Yes, in part you are right since the python core doesn't include them.
However they can be implemented with decorators:

* Subject: decorators and multimethods
   Group:   comp.lang.python
   Link:http://tinyurl.com/d45ym

Anyway, as you, I also use the default arguments.

Regards
Josef

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


Thoughts on object representation in a dictionary

2005-12-09 Thread py
Say I have classes which represent parts of a car such as Engine, Body,
etc.  Now I want to represent a Car in a nested dictionary like...

{string_id:{engine_id:engine_object, body_id:body_object}}ok?

Well the other thing is that I am allowed to store strings in this
dictionary...so I can't just store the Engine and Body object and later
use them.  this is just a requirement (which i dont understand
either)...but its what I have to do.

So my question is there a good way of storing this information in a
nested dictionary such that if I receive this dictionary I could easily
pull it apart and create Engine and Body objects with the information?
Any suggestions at all?  keep in mind I am limited to using a
dictionary of strings...thats it.

Thanks for any input.

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


  1   2   >