Re: Installing WebDAV server

2011-08-31 Thread Paul Kölle

Hi,

Am 30.08.2011 22:00, schrieb Fokke Nauta:

Hi all,

I am completely new to Python, but I'm confronted with a problem I can't
solve.

Welcome to python.


This is my question:

[snip]


I installed Python 3.2.1 and extracted the packages PyWebDAV and PyXML. Now
I have a working Python app and 2 directories called PyWebDAV-0.9.4.1 and
PyXML-0.8.4. In the PyWebDAV README it says:

Installation and setup of server can be as easy as follows:

$ easy_install PyWebDAV
$ davserver -D /tmp -n -J

But of course it doesn't work like that. When I start up Python GUI I see
the ">>>" prompt instead of the "$" prompt. But where do I place the two
directories? And there is no easy_install script in the PyXML-0.8.4
directory, only a setup.py and ez_setup.py script. I guess the latter is the
one to use. But how?
You dont install from "Python GUI", use normal cmd, navigate to the 
folder you downloaded PyXML and PyWebDAV and run "python setup.py 
install" (python.exe has to be in your PATH). Then you have to find the 
startup-script "davserver". Find your python installation directory and 
look into /Tools/Scripts, in my computer this is 
E:\python27\Tools\Scripts. PyXML and PyWebDAV get installed in the 
site-packages folder i.e. E:\python27\Lib/site-packages. You might have 
to look for "davserver" there...


hth
 Paul

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


Re: Installing WebDAV server

2011-08-31 Thread Fokke Nauta
"Thomas 'PointedEars' Lahn"  wrote in message 
news:[email protected]...
> Fokke Nauta wrote:
>
>> "Thomas 'PointedEars' Lahn"  wrote in message
>> news:[email protected]...
>
> It's attribution _line_, not attribution novel.  Your quotes are hardly
> legible, too ? 
>
>>> Fokke Nauta wrote:
 I'm running a PC with XP Pro32, [.]
 [.] In the PyWebDAV README it says:

 Installation and setup of server can be as easy as follows:

 $ easy_install PyWebDAV
 $ davserver -D /tmp -n -J

 But of course it doesn't work like that. When I start up Python GUI
>>> That is really not a *G*raphical User Interface, but the (text-based)
>>> Python shell.
>>
>> Yes, I noticed. But the application has the name of Python GUI.
>
> ACK.  Admittedly I cannot remember having used Python on Windows (XP) 
> except
> via Cygwin.
>
 I see the ">>>" prompt instead of the "$" prompt.
>>>  "Doctor, my arm hurts when I move it." - "Don't move it, then."
>>
>> I don't see the point here ...
>
> Do not run `python' or the "Python GUI", then.
>
>>> The Python shell executes Python code.  The above obviously is not 
>>> Python
>>> code, but *system* shell commands.  So let the *system* command shell
>>> execute them (as indicated by the `$' prompt, which is customary for a
>>> sh-based UNIX/Linux shell prompt).
>>
>> I know. I worked with SCO Unix and various sorts of Linux.
>> But never with Python, so I hadn't got a clue about the prompt.
>
> Come on, with that experience you see a `$' and those commands and don't
> realize it is (ba)sh?

Ofcourse I realized it was Unix/Linux. I already could tell that as the 
packages I downloaded were tar.gz files.
So I unpacked them and expected to run a Python installer script from the 
Python command line.
Hence my question "How do I do that", but perhaps I did not make myself 
clear enough.

Tried to run the Python installer script from the DOS command line but that 
resulted in an error.

As I have Cygwin running as well, I could try to install it there instead of 
in Windows.

>>> Since you use Windows XP, type `cmd' to get the command shell (if you
>>> knew MS-DOS, which I doubt, you are at home now).
>>
>> I know MSDOS. I even worked with CP/M
>
> Good for you.
>
>>> However, you appear to have found the *UNIX/Linux* README (and the
>>> corresponding version?) of that server: the second command is usually 
>>> how
>>> you would run a program as daemon on Unices (run through an init 
>>> script),
>>> while on Windows NT (like XP) you would have a setup program install a
>>> service for you (maybe to execute that command when the service is
>>> started).  Look for the Windows version.
>>
>> There is no other Windows version except the packages I mentioned,
>> PyWebDAV and PyXML. The only Windows thing I got was the Python
>> interpreter itself.
>
> Has it not occurred to you to STFW for "easy_install" first?

What do you mean by STFW?

I wasn't aware that easy_install was a utility. Downloaded and installed the 
Windows version and run easy_install pywebdav.
It downloaded something, installed something and finished something.
But, once again, don't know how to proceed.
Otherwise I'll give it a try under Cygwin.

 And there is no easy_install script in the PyXML-0.8.4
 directory, only a setup.py and ez_setup.py script. I guess the latter 
 is
 the one to use. But how?
>>> RTFM.
>>
>> Which fucking manual?
>
> That of the server, on Windows-related information.  Or that of
> easy_install.  Or Python.  Whichever comes first.

It's my own server and I didn't write a manual for it.
In the manual of Easy_install it says how to install packaged etc and I did 
sucessfully.
There is no furter information as how to proceed. That's why I posted my 
question here.

 How do I proceed next?
>>> Look for the Windows version.  If there is none, get easy_install and 
>>> use
>>> it as described.

I did and it worked. What's next?

Fokke 


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


Subclassing str object

2011-08-31 Thread Yaşar Arabacı
Hİ,

I originally posted my question to here:
http://stackoverflow.com/q/7255655/886669 Could you people please look at it
and enlighten me a little bit? I would appreciate an answer either from here
or at stackoverflow. Thanks in advance.

-- 
http://yasar.serveblog.net/
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Installing WebDAV server

2011-08-31 Thread Laszlo Nagy



What do you mean by STFW?

Search The Fucking Web ?

I wasn't aware that easy_install was a utility. Downloaded and installed the
Windows version and run easy_install pywebdav.
It downloaded something, installed something and finished something.

Then it's installed!

But, once again, don't know how to proceed.
Is that so hard? I have never used pywebdav but the first page I hit 
through Google search is:


http://code.google.com/p/pywebdav/

Where it says:


Installation and setup of server can be as easy as follows:

$ easy_installPyWebDAV
$ davserver-D/tmp-n-J
Starting  upPyWebDAV  server(version0.9.2-dev)
>>  ATTENTION:  Authentication  disabled!
>>  Serving  datafrom  /tmp
>>  Listening  on localhost(8008)
So you successfully ran easy_install. Then I guess you will have to look 
for a program named "davserver" and start it up. I suspect that 
searching for  "davserver.*" under your site-packages dir or Python 
installation dir will do.


  L

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


argparse: showing full help for subparsers

2011-08-31 Thread Chris Withers

Hi All,

If I have subparsers set up and do:

myscript.py --help

I get the summary for each of the top-level commands, but to get the 
help for each sub-command I have to do:


myscript.py subcommand --help

Is there any way I can get "myscript.py --help" to show the help 
hierarchically, including all options for all subcommands?


cheers,

Chris

--
Simplistix - Content Management, Batch Processing & Python Consulting
- http://www.simplistix.co.uk
--
http://mail.python.org/mailman/listinfo/python-list


Re: Installing WebDAV server

2011-08-31 Thread Fokke Nauta
"Paul Kölle"  wrote in message 
news:[email protected]...
> Hi,
>
> Am 30.08.2011 22:00, schrieb Fokke Nauta:
>> Hi all,
>>
>> I am completely new to Python, but I'm confronted with a problem I can't
>> solve.
> Welcome to python.
>
>> This is my question:
> [snip]
>
>> I installed Python 3.2.1 and extracted the packages PyWebDAV and PyXML. 
>> Now
>> I have a working Python app and 2 directories called PyWebDAV-0.9.4.1 and
>> PyXML-0.8.4. In the PyWebDAV README it says:
>>
>> Installation and setup of server can be as easy as follows:
>>
>> $ easy_install PyWebDAV
>> $ davserver -D /tmp -n -J
>>
>> But of course it doesn't work like that. When I start up Python GUI I see
>> the ">>>" prompt instead of the "$" prompt. But where do I place the two
>> directories? And there is no easy_install script in the PyXML-0.8.4
>> directory, only a setup.py and ez_setup.py script. I guess the latter is 
>> the
>> one to use. But how?
> You dont install from "Python GUI", use normal cmd, navigate to the folder 
> you downloaded PyXML and PyWebDAV and run "python setup.py install" 
> (python.exe has to be in your PATH). Then you have to find the 
> startup-script "davserver". Find your python installation directory and 
> look into /Tools/Scripts, in my computer this is 
> E:\python27\Tools\Scripts. PyXML and PyWebDAV get installed in the 
> site-packages folder i.e. E:\python27\Lib/site-packages. You might have to 
> look for "davserver" there...
>

Thanks, Paul.

I ran "python setup.py install" in both the PyXML and PyWebDAV directories. 
A lot of things happened and are added into those directories and I guess it 
will be OK.
Next step, the startup-script "davserver". There is no script as such, also 
not in \python27\tools\scripts.
I found 2 similar scripts:
1. server.py in D:\Python27\WebDAV\PyWebDAV\DAVServer
2. WebDAVServer.py in D:\Python27\WebDAV\PyWebDAV\DAV

Which one is the one to use?

I also configured config.ini in D:\Python27\WebDAV\PyWebDAV\DAVServer

In this file it says:
"# Auth Database Table, Must exists in database prior to firstrun
dbtable=webDav

# Create User Database Table and Insert system user"

I created in MySQL a database called webDav.
I can create a table called User, but how many fields?

With regards,
Fokke


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


PC locks up with list operations

2011-08-31 Thread Steven D'Aprano
Twice in a couple of weeks, I have locked up my PC by running a Python 2.5
script that tries to create a list that is insanely too big.

In the first case, I (stupidly) did something like:

mylist = [0]*12345678901234

After leaving the machine for THREE DAYS (!!!) I eventually was able to get
to a console and kill the Python process. Amazingly, it never raised
MemoryError in that time.

The second time was a little less stupid, but not much:

mylist = []
for x in itertools.combinations_with_replacement(some_big_list, 20):
mylist.append(func(x))

After three hours, the desktop is still locked up. I'm waiting to see what
happens in the morning before rebooting.

Apart from "Then don't do that!", is there anything I can do to prevent this
sort of thing in the future? Like instruct Python not to request more
memory than my PC has?

I am using Linux desktops; both incidents were with Python 2.5. Do newer
versions of Python respond to this sort of situation more gracefully?



-- 
Steven

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


Re: Installing WebDAV server

2011-08-31 Thread Fokke Nauta
"Laszlo Nagy"  wrote in message 
news:[email protected]...
>
>> What do you mean by STFW?
> Search The Fucking Web ?

OK, the modern version of RTFM.

>> I wasn't aware that easy_install was a utility. Downloaded and installed 
>> the
>> Windows version and run easy_install pywebdav.
>> It downloaded something, installed something and finished something.
> Then it's installed!
>> But, once again, don't know how to proceed.
> Is that so hard? I have never used pywebdav but the first page I hit 
> through Google search is:
>
> http://code.google.com/p/pywebdav/

I've been there.

> Where it says:
>
>> Installation and setup of server can be as easy as follows:
>>
>> $ easy_installPyWebDAV
>> $ davserver-D/tmp-n-J
>> Starting  upPyWebDAV  server(version0.9.2-dev)
>> >>  ATTENTION:  Authentication  disabled!
>> >>  Serving  datafrom  /tmp
>> >>  Listening  on localhost(8008)

Yes, but that's Unix/Linux again.
But I'm in Windows, without experience with Python.

> So you successfully ran easy_install. Then I guess you will have to look 
> for a program named "davserver" and start it up. I suspect that searching 
> for  "davserver.*" under your site-packages dir or Python installation dir 
> will do.
>

Fokke 


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


Re: PC locks up with list operations

2011-08-31 Thread Chris Withers

On 31/08/2011 13:33, Steven D'Aprano wrote:

I am using Linux desktops; both incidents were with Python 2.5. Do newer
versions of Python respond to this sort of situation more gracefully?


Ironically, Windows does better here and dumps you out with a 
MemoryError before slowly recovering.


Linux seems to fair badly when programs use more memory than physically 
available. Perhaps there's some per-process thing that can be used to 
limit things on Linux?


cheers,

Chris

--
Simplistix - Content Management, Batch Processing & Python Consulting
- http://www.simplistix.co.uk
--
http://mail.python.org/mailman/listinfo/python-list


Re: PC locks up with list operations

2011-08-31 Thread Steven D'Aprano
Chris Withers wrote:

> On 31/08/2011 13:33, Steven D'Aprano wrote:
>> I am using Linux desktops; both incidents were with Python 2.5. Do newer
>> versions of Python respond to this sort of situation more gracefully?
> 
> Ironically, Windows does better here and dumps you out with a
> MemoryError before slowly recovering.
> 
> Linux seems to fair badly when programs use more memory than physically
> available. Perhaps there's some per-process thing that can be used to
> limit things on Linux?

As far as I know, ulimit ("user limit") won't help. It can limit the amount
of RAM available to a process, but that just makes the process start using
virtual memory more quickly. It can also limit the amount of virtual memory
used by the shell, but not of other processes. In other words, Linux will
try really, really, really hard to give you the 84 gigabytes you've asked
for on a 2 GB system, even if it means DOSing your system for a month.

Of course, I would be very happy to learn I'm wrong.



-- 
Steven

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


Re: PC locks up with list operations

2011-08-31 Thread Benjamin Kaplan
On Wed, Aug 31, 2011 at 8:40 AM, Chris Withers  wrote:
>
> On 31/08/2011 13:33, Steven D'Aprano wrote:
>>
>> I am using Linux desktops; both incidents were with Python 2.5. Do newer
>> versions of Python respond to this sort of situation more gracefully?
>
> Ironically, Windows does better here and dumps you out with a MemoryError 
> before slowly recovering.
>
> Linux seems to fair badly when programs use more memory than physically 
> available. Perhaps there's some per-process thing that can be used to limit 
> things on Linux?
>
> cheers,
>
> Chris
>
> --

32-bit or 64-bit Python? A 32-bit program will crash once memory hits
2GB. A 64-bit program will just keep consuming RAM until your computer
starts thrashing. The problem isn't your program using more RAM than
you have, just more RAM than you have free. Last time I faced a
situation like this, I just decided it was better to stick to the
32-bit program and let it crash if it got too big.


> Simplistix - Content Management, Batch Processing & Python Consulting
>            - http://www.simplistix.co.uk
> --
> http://mail.python.org/mailman/listinfo/python-list
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: PC locks up with list operations

2011-08-31 Thread Peter Otten
Steven D'Aprano wrote:

> Twice in a couple of weeks, I have locked up my PC by running a Python 2.5
> script that tries to create a list that is insanely too big.
> 
> In the first case, I (stupidly) did something like:
> 
> mylist = [0]*12345678901234
> 
> After leaving the machine for THREE DAYS (!!!) I eventually was able to
> get to a console and kill the Python process. Amazingly, it never raised
> MemoryError in that time.
> 
> The second time was a little less stupid, but not much:
> 
> mylist = []
> for x in itertools.combinations_with_replacement(some_big_list, 20):
> mylist.append(func(x))
> 
> After three hours, the desktop is still locked up. I'm waiting to see what
> happens in the morning before rebooting.
> 
> Apart from "Then don't do that!", is there anything I can do to prevent
> this sort of thing in the future? Like instruct Python not to request more
> memory than my PC has?
> 
> I am using Linux desktops; both incidents were with Python 2.5. Do newer
> versions of Python respond to this sort of situation more gracefully?

If you are starting these scripts from the shell, how about ulimit?

$ ulimit -v 4
$ python -c'print range(10**5)[-1]'
9
$ python -c'print range(10**6)[-1]'
Traceback (most recent call last):
  File "", line 1, in 
MemoryError
$


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


How to save the packages received by a network interface or some port in a file and resend the packages received when needed?

2011-08-31 Thread [email protected]
hi,
  This is a question not specific to Python,but its related somehow,and I
believe I can get some help from your fellow:)
  I am doing my work on a server service program on Linux that processes the
packages sent to the socket it listens.Their is already a old such service
listening on the port doing its job,and
I can't stop the old server service, and I need to get the packages sent to
the old server and send them to my new server service to make sure it works
well .How can I get the package and resent them to my new service? Is there
such a tool or is there some functionality that tools such as tcpdump
already provides?
Thanks:)
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Handling 2.7 and 3.0 Versions of Dict

2011-08-31 Thread Martin v. Loewis
Am 31.08.2011 03:43, schrieb Travis Parks:
> I am writing a simple algorithms library that I want to work for both
> Python 2.7 and 3.x. I am writing some functions like distinct, which
> work with dictionaries under the hood. The problem I ran into is that
> I am calling itervalues or values depending on which version of the
> language I am working in. Here is the code I wrote to overcome it:
> 
> import sys
> def getDictValuesFoo():
> if sys.version_info < (3,):
> return dict.itervalues
> else:
> return dict.values
> 
> getValues = getDictValuesFoo()
> 
> def distinct(iterable, keySelector = (lambda x: x)):
> lookup = {}
> for item in iterable:
> key = keySelector(item)
> if key not in lookup:
> lookup[key] = item
> return getValues(lookup)
> 
> I was surprised to learn that getValues CANNOT be called as if it were
> a member of dict. I figured it was more efficient to determine what
> getValues was once rather than every time it was needed.
> 
> First, how can I make the method getValues "private" _and_ so it only
> gets evaluated once?

Not sure what "private" means here. Having the logic selected only once
goes like this

if sys.version_info < (3,):
  def getDictValues(dict):
  return dict.itervalues()
else:
  def getDictValues(dict):
  return dict.values()

> Secondly, will the body f the distinct method be
> evaluated immediately?

Yes.

> How can I delay building the dict until the first value is requested?

Make it a generator:

def distinct(iterable, keySelector = (lambda x: x)):
lookup = {}
for item in iterable:
key = keySelector(item)
if key not in lookup:
lookup[key] = item
for v in  getValues(lookup):
yield v

This delays *building* the dictionary until the *first* value is
requested. I.e. it completes building the dictionary before the first
value is returned.

If you also want to interleave iteration over iterable with fetching
distinct values, write it like that:

def distinct(iterable, keySelector = (lambda x: x)):
seen = {}
for item in iterable:
key = keySelector(item)
if key not in seen:
yield item
seen[key] = item

> I noticed that hashing is a lot different in Python than it is in .NET
> languages. .NET supports custom "equality comparers" that can override
> a type's Equals and GetHashCode functions. This is nice when you can't
> change the class you are hashing. That is why I am using a key
> selector in my code, here. Is there a better way of overriding the
> default hashing of a type without actually modifying its definition? I
> figured a requesting a key was the easiest way.

You could provide a Key class that takes a hash function and a value
function:

class Key:
  def __init__(self, value, hash, eq):
self.value, self.hash, self.eq = value, hash, eq
  def __hash__(self):
return self.hash(self.value)
  def __eq__(self, other_key):
return self.eq(self.value, other_key.value)

This class would then be used instead of your keySelector.

With that, you could change the dictionary to a set. Actually, you
could already do so in the second generator version:

def distinct(iterable, keySelector = (lambda x: x)):
seen = set()
for item in iterable:
key = keySelector(item)
if key not in seen:
yield item
seen.add(key) # item is not needed anymore

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


Re: Installing WebDAV server

2011-08-31 Thread Laszlo Nagy



Where it says:


Installation and setup of server can be as easy as follows:

$ easy_installPyWebDAV
$ davserver-D/tmp-n-J
Starting  upPyWebDAV  server(version0.9.2-dev)

  ATTENTION:  Authentication  disabled!
  Serving  datafrom  /tmp
  Listening  on localhost(8008)

Yes, but that's Unix/Linux again.
But I'm in Windows, without experience with Python.
Not really... The "easy_install" command is the same on windows. Maybe 
the command prompt is different, but the command itself is the same. 
Same is true with the "davserver" command. If you can find a 
"davserver.exe" or "davserver.py" or "davserver.pyw" file under 
site-packages or tools/scripts, then that will be the program that you 
need to start up. No magic.


   L

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


Re: PC locks up with list operations

2011-08-31 Thread Robert Spanjaard

On 08/31/2011 02:40 PM, Chris Withers wrote:

On 31/08/2011 13:33, Steven D'Aprano wrote:

I am using Linux desktops; both incidents were with Python 2.5. Do newer
versions of Python respond to this sort of situation more gracefully?


Ironically, Windows does better here and dumps you out with a
MemoryError before slowly recovering.


I think it's a little premature to make such a statement based on a single 
user experience. I've used Linux for six years now, and it NEVER locked up, 
even when a program leaks memory like hell.
I can't duplicate the OP's behaviour because my Python (2.6.5, 64 bit) does 
generate an instant MemoryError (which answers the question, ofcourse), but 
I've used a VLC version that had a nasty leak. My swap space is on a SSD, 
and VLC filled it completely in about 10 seconds. But then, VLC got killed 
automatically, and the system recovered gracefully.
On a system with the swapspace on a regular HD, the same will happen, but 
it'll take more time. And while VLC is filling the swap space, the system 
does not lock up at all. You can still keep working, and (for example) fire 
up the process manager and kill VLC yourself.


>>> mylist = [0]*12345678901234
Traceback (most recent call last):
  File "", line 1, in 
MemoryError

--
Regards, Robert  http://www.arumes.com
--
http://mail.python.org/mailman/listinfo/python-list


Re: PC locks up with list operations

2011-08-31 Thread Rodrick Brown
$ man limits.conf 

Sent from my iPhone

On Aug 31, 2011, at 8:33 AM, Steven D'Aprano 
 wrote:

> Twice in a couple of weeks, I have locked up my PC by running a Python 2.5
> script that tries to create a list that is insanely too big.
> 
> In the first case, I (stupidly) did something like:
> 
> mylist = [0]*12345678901234
> 
> After leaving the machine for THREE DAYS (!!!) I eventually was able to get
> to a console and kill the Python process. Amazingly, it never raised
> MemoryError in that time.
> 
> The second time was a little less stupid, but not much:
> 
> mylist = []
> for x in itertools.combinations_with_replacement(some_big_list, 20):
>mylist.append(func(x))
> 
> After three hours, the desktop is still locked up. I'm waiting to see what
> happens in the morning before rebooting.
> 
> Apart from "Then don't do that!", is there anything I can do to prevent this
> sort of thing in the future? Like instruct Python not to request more
> memory than my PC has?
> 
> I am using Linux desktops; both incidents were with Python 2.5. Do newer
> versions of Python respond to this sort of situation more gracefully?
> 
> 
> 
> -- 
> Steven
> 
> -- 
> http://mail.python.org/mailman/listinfo/python-list
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: How to save the packages received by a network interface or some port in a file and resend the packages received when needed?

2011-08-31 Thread Matty Sarro
Its possible using TCPDUMP and wireshark. however its a bit of a
manual process (open the pcap in wireshark, select the correct tcp
stream, and extract the file). I did this to show a vulnerability in
how medical images were transmitted in a university hospital once :)

Here are some guides, maybe it can serve as a jumping off point?
http://packetlife.net/blog/2009/jul/13/quick-packet-capture-data-extraction/
http://wiki.wireshark.org/TCP_Reassembly

There are some C# libraries specifically for this:
http://www.codeproject.com/KB/IP/TcpRecon.aspx

Not sure if anything exists explicitly for python though.
-Matty

On Wed, Aug 31, 2011 at 9:35 AM, [email protected]
 wrote:
> hi,
>   This is a question not specific to Python,but its related somehow,and I
> believe I can get some help from your fellow:)
>   I am doing my work on a server service program on Linux that processes the
> packages sent to the socket it listens.Their is already a old such service
> listening on the port doing its job,and
> I can't stop the old server service, and I need to get the packages sent to
> the old server and send them to my new server service to make sure it works
> well .How can I get the package and resent them to my new service? Is there
> such a tool or is there some functionality that tools such as tcpdump
> already provides?
> Thanks:)
>
> --
> http://mail.python.org/mailman/listinfo/python-list
>
>
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Returning a value from exec or a better solution

2011-08-31 Thread Ian Kelly
On Wed, Aug 31, 2011 at 12:35 AM, Arnaud Delobelle  wrote:
>> You don't know that, an implementation may for example set __bultins__
>> to None, prior to returning, its not an unreasonable thing to do and
>> the docs don't say they can't.
>
> I haven't studied the docs but I'm certain that such an implementation
> would break a lot of code.

For example:

a = 42
exec "a += 1729"
print(a)

...since print would no longer be available in the global namespace.

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


Re: How to save the packages received by a network interface or some port in a file and resend the packages received when needed?

2011-08-31 Thread Grant Edwards
On 2011-08-31, Matty Sarro  wrote:

> Its possible using TCPDUMP and wireshark. however its a bit of a
> manual process (open the pcap in wireshark, select the correct tcp
> stream, and extract the file).

Presumably the OP knows the port IP address and port number on which
the server is listening, so wouldn't it be simpler to just capture TCP
traffic to/from that IP/port?

Then you can play it back using tcprewrite, tcpreplay, et al.

But, I don't see how that's going to work.  The OP seems to want to
capture a TCP session and then "replay it" so that the client from the
session ends up talking to a different server during the replay. The
chances of the new server starting up a connection with the same ACK
sequence number is practially nil isn't it?

>> ?? This is a question not specific to Python,but its related
>> somehow,and I believe I can get some help from your fellow:) ?? I am
>> doing my work on a server service program on Linux that processes the
>> packages sent to the socket it listens.Their is already a old such
>> service listening on the port doing its job,and I can't stop the old
>> server service, and I need to get the packages sent to the old server
>> and send them to my new server service to make sure it works well
>> .How can I get the package and resent them to my new service? Is
>> there such a tool or is there some functionality that tools such as
>> tcpdump already provides? Thanks:)

-- 
Grant Edwards   grant.b.edwardsYow! ... this must be what
  at   it's like to be a COLLEGE
  gmail.comGRADUATE!!
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Installing WebDAV server

2011-08-31 Thread Fokke Nauta
"Laszlo Nagy"  wrote in message 
news:[email protected]...
>
>>> Where it says:
>>>
 Installation and setup of server can be as easy as follows:

 $ easy_installPyWebDAV
 $ davserver-D/tmp-n-J
 Starting  upPyWebDAV  server(version0.9.2-dev)
>>   ATTENTION:  Authentication  disabled!
>>   Serving  datafrom  /tmp
>>   Listening  on localhost(8008)
>> Yes, but that's Unix/Linux again.
>> But I'm in Windows, without experience with Python.
> Not really... The "easy_install" command is the same on windows. Maybe the 
> command prompt is different, but the command itself is the same. Same is 
> true with the "davserver" command. If you can find a "davserver.exe" or 
> "davserver.py" or "davserver.pyw" file under site-packages or 
> tools/scripts, then that will be the program that you need to start up. No 
> magic.
>

Actually, I installed easy_install setuptools for Windows 
(setuptools-0.6c11.win32-py2.7.exe). Running easy_install generated an error 
message:
"Setuptools version 0.6c9 or greater has been installed.
(Run "ez_setup.py -U setuptools" to reinstall or upgrade.)"

I did, quite something happened but the same error message came back when 
retrying.

So, I installed it another way, indicated by Paul Kölle:
"navigate to the folder you downloaded PyXML and PyWebDAV and run "python 
setup.py
install" (python.exe has to be in your PATH). "

That worked fine, but:

There is no davserver script or executable. Please read my response to Paul 
Kölle.

Fokke



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


Creating python egg including pre-built libraries

2011-08-31 Thread merk
I have a set of complex libraries that I have wrapped with swig.   I
would like to create a python egg of the swig generated python files
and the pre-built libraries.   The libraries have a lot of
dependencies and I don't want to force the user to get all the
dependencies to try to build the libraries from source.  I just want
to distribute some python files and the libraries for a given
platform.

Does anyone have a recipe for doing something like this?

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


Why do class methods always need 'self' as the first parameter?

2011-08-31 Thread T. Goodchild
I’m new to Python, and I love it.  The philosophy of the language (and
of the community as a whole) is beautiful to me.

But one of the things that bugs me is the requirement that all class
methods have 'self' as their first parameter.  On a gut level, to me
this seems to be at odds with Python’s dedication to simplicity.

For example, consider Python’s indent-sensitive syntax.  Although
other languages didn’t use indentation to specify scope, programmers
always used indentation anyways.  Making indentation took a common
practice, made it a rule, and the result was a significantly improved
signal-to-noise ratio in the readability of Python code.

So why is 'self' necessary on class methods?  It seems to me that the
most common practice is that class methods *almost always* operate on
the instance that called them.  It would make more sense to me if this
was assumed by default, and for "static" methods (methods that are
part of a class, but never associated with a specific instance) to be
labelled instead.

Just curious about the rationale behind this part of the language.
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: How to save the packages received by a network interface or some port in a file and resend the packages received when needed?

2011-08-31 Thread Emile van Sebille

On 8/31/2011 6:35 AM [email protected] said...

hi,
   This is a question not specific to Python,but its related somehow,and
I believe I can get some help from your fellow:)
   I am doing my work on a server service program on Linux that
processes the packages sent to the socket it listens.Their is already a
old such service listening on the port doing its job,and
I can't stop the old server service, and I need to get the packages sent
to the old server and send them to my new server service to make sure it
works well .How can I get the package and resent them to my new service?
Is there such a tool or is there some functionality that tools such as
tcpdump already provides?


I recently set up a standby spare fax server on a network that I also 
needed to test, and was able to tee the source transmissions to both 
systems.  That may be an option, particularly as it sounds like you've 
written a consumer of info and are not replying and interacting with the 
source.


Emile



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


Re: Why do class methods always need 'self' as the first parameter?

2011-08-31 Thread John Gordon
In <[email protected]> "T. 
Goodchild"  writes:

> So why is 'self' necessary on class methods?  It seems to me that the
> most common practice is that class methods *almost always* operate on
> the instance that called them.  It would make more sense to me if this
> was assumed by default, and for "static" methods (methods that are
> part of a class, but never associated with a specific instance) to be
> labelled instead.

> Just curious about the rationale behind this part of the language.

How would a method access instance variables without 'self'?

They probably could have made 'self' a magical attribute that just
appears out of thin air instead of being passed as an argument, like
'this' in C++.  But would that really provide any benefit?

-- 
John Gordon   A is for Amy, who fell down the stairs
[email protected]  B is for Basil, assaulted by bears
-- Edward Gorey, "The Gashlycrumb Tinies"

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


Re: Why do class methods always need 'self' as the first parameter?

2011-08-31 Thread Emile van Sebille

On 8/31/2011 7:35 AM T. Goodchild said...


Just curious about the rationale behind this part of the language.


http://docs.python.org/faq/design.html

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


Re: Why do class methods always need 'self' as the first parameter?

2011-08-31 Thread Neil Cerutti
On 2011-08-31, T. Goodchild  wrote:
> I?m new to Python, and I love it.  The philosophy of the
> language (and of the community as a whole) is beautiful to me.
>
> But one of the things that bugs me is the requirement that all
> class methods have 'self' as their first parameter.  On a gut
> level, to me this seems to be at odds with Python?s dedication
> to simplicity.

Think it through carefully, and you'll probably agree with
Python's design. But not necessarily.

In any case, this is a very common complaint, so check out the
Python FAQ.

http://docs.python.org/faq/design.html#why-self

-- 
Neil Cerutti
"A politician is an arse upon which everyone has sat except a man."
  e. e. cummings 
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Why do class methods always need 'self' as the first parameter?

2011-08-31 Thread Javier Collado
Hello,

2011/8/31 T. Goodchild :
> But one of the things that bugs me is the requirement that all class
> methods have 'self' as their first parameter.  On a gut level, to me
> this seems to be at odds with Python’s dedication to simplicity.

I think the answer to this question is part of the zen of python:
<>

http://www.python.org/dev/peps/pep-0020/

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


Re: How to save the packages received by a network interface or some port in a file and resend the packages received when needed?

2011-08-31 Thread [email protected]
In fact,UDP is enough for me,I heared that tcpdump and netcat can store and
resend the udp packages to get the replay effect,but I don't know how, or is
there some better way? I am working on a Linux server and only some basic
terminal tools are available :)

2011/8/31 Emile van Sebille 

> On 8/31/2011 6:35 AM [email protected] said...
>
>  hi,
>>   This is a question not specific to Python,but its related somehow,and
>> I believe I can get some help from your fellow:)
>>   I am doing my work on a server service program on Linux that
>> processes the packages sent to the socket it listens.Their is already a
>> old such service listening on the port doing its job,and
>> I can't stop the old server service, and I need to get the packages sent
>> to the old server and send them to my new server service to make sure it
>> works well .How can I get the package and resent them to my new service?
>> Is there such a tool or is there some functionality that tools such as
>> tcpdump already provides?
>>
>
> I recently set up a standby spare fax server on a network that I also
> needed to test, and was able to tee the source transmissions to both
> systems.  That may be an option, particularly as it sounds like you've
> written a consumer of info and are not replying and interacting with the
> source.
>
> Emile
>
>
>
>
> --
> http://mail.python.org/**mailman/listinfo/python-list
>
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Why do class methods always need 'self' as the first parameter?

2011-08-31 Thread Steven D'Aprano
T. Goodchild wrote:

> So why is 'self' necessary on class methods?  

I assume you are talking about the declaration in the method signature:

def method(self, args): ...

rather than why methods have to be called using self.method. If not, there's
already a FAQ for that second question:

http://docs.python.org/faq/design.html#why-self


> It seems to me that the 
> most common practice is that class methods *almost always* operate on
> the instance that called them.

By the way, what you're calling "class methods" are actually *instance*
methods, because they receive the instance "self" as the first parameter.

Python does have class methods, which receive the class, not the instance,
as the first parameter. These are usually written something like this:

class K(object):
@classmethod
def spam(cls, args):
print cls  # always prints "class K", never the instance

Just like self, the name cls is a convention only. Class methods are usually
used for alternate constructors.

There are also static methods, which don't receive any special first
argument, plus any other sort of method you can invent, by creating
descriptors... but that's getting into fairly advanced territory. They're
generally specialised, and don't see much use.

As you can see, the terminology is not exactly the same as Java.


> It would make more sense to me if this 
> was assumed by default, ...

Well here's the thing. Python methods are wrappers around function objects.
The method wrapper knows which instance is involved (because of the
descriptor magic which I alluded to above), but the function doesn't and
can't. Or at least not without horrible run-time hacks.

By treating "self" as an ordinary parameter which needs to be declared, you
can do cool stuff like bound and unbound methods:

f = "instance".upper  # this is a bound method
g = str.upper  # this is an unbound method

The bound method f already has the instance "self" filled in, so to speak.
So you can now just call it, and it will work:

f()
=> returns "INSTANCE"

The unbound method still needs the instance supplied. This makes it perfect
for code like this:

for instance in ("hello", "world"):
print g(instance)

especially if you don't know what g will be until run-time. (E.g. will it be
str.upper, str.lower, str.title?)

Because methods require that first argument to be given explicitly, unbound
methods are practically ordinary functions. They're so like functions that
in Python 3, they're done away with altogether, and the unwrapped function
object will be returned instead.

You can also do nifty stuff like dynamic method injections:

>>> def func(a, b):
... print(a, b)
...
>>> class K(object):
... pass
...
>>> K.func = func  # dynamically inject a method
>>> instance = K()
>>> instance.func(23)
(<__main__.K object at 0xb7f0a4cc>, 23)

and it all just works. You can even inject a method onto the instance,
although it takes a bit more effort to make that work.

All this is possible without nasty hacks because self is treated as just an
ordinary parameter of functions. Otherwise, the compiler would need to know
whether the function was being called from inside a method wrapper or not,
and change the function signature appropriately, and that just gets too
ugly and messy for words.

So for the cost of having to declare self as an argument, we get:

* instant visual recognition of what's intended as a method ("the 
  first argument is called self") and what isn't
* a nicely consistent treatment of function signatures at all times
* clean semantics for the local variable namespace
* the same mechanism (with minor adjustments) can be used for class 
  and static methods
* bound and unbound methods semantics

plus as a bonus, plenty of ongoing arguments about whether or not having to
explicitly list "self" as a parameter is a good thing or not, thus keeping
people busy arguing on mailing lists instead of coding





-- 
Steven

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


Re: Handling 2.7 and 3.0 Versions of Dict

2011-08-31 Thread Ian Kelly
On Wed, Aug 31, 2011 at 3:55 AM, Martin v. Loewis  wrote:
> if sys.version_info < (3,):
>  def getDictValues(dict):
>  return dict.itervalues()
> else:
>  def getDictValues(dict):
>  return dict.values()

The extra level of function call indirection is unnecessary here.
Better to write it as:

if sys.version_info < (3,):
getDictValues = dict.itervalues
else:
getDictValues = dict.values

(which is basically what the OP was doing in the first place).

>> I noticed that hashing is a lot different in Python than it is in .NET
>> languages. .NET supports custom "equality comparers" that can override
>> a type's Equals and GetHashCode functions. This is nice when you can't
>> change the class you are hashing. That is why I am using a key
>> selector in my code, here. Is there a better way of overriding the
>> default hashing of a type without actually modifying its definition? I
>> figured a requesting a key was the easiest way.
>
> You could provide a Key class that takes a hash function and a value
> function:
>
> class Key:
>  def __init__(self, value, hash, eq):
>    self.value, self.hash, self.eq = value, hash, eq
>  def __hash__(self):
>    return self.hash(self.value)
>  def __eq__(self, other_key):
>    return self.eq(self.value, other_key.value)
>
> This class would then be used instead of your keySelector.

For added value, you can make it a class factory so you don't have to
specify hash and eq over and over:

def Key(keyfunc):
class Key:
def __init__(self, value):
self.value = value
def __hash__(self):
return hash(keyfunc(self.value))
def __eq__(self, other):
return keyfunc(self) == keyfunc(other)
return Key

KeyTypeAlpha = Key(lambda x: x % 7)

items = set(KeyTypeAlpha(value) for value in sourceIterable)

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


fun with nested loops

2011-08-31 Thread Daniel
Dear All,

I have some complicated loops of the following form

for c in configurations: # loop 1
while nothing_bad_happened: # loop 2
while step1_did_not_work: # loop 3
for substeps in step1 # loop 4a
# at this point, we may have to
-leave loop 1
-restart loop 4
-skip a step in loop 4
-continue on to loop 4b

while step2_did_not_work: # loop 4b
for substeps in step2:
# at this point, we may have to
-leave loop 1
-restart loop 2
-restart loop 4b
...
...many more loops...


I don't see any way to reduce these nested loops logically, they
describe pretty well what the software has to do.
This is a data acquisition application, so on ever line there is
a lot of IO that might fail or make subsequent steps useless or
require a
retry.

Now every step could need to break out of any of the enclosing loops.
So basically I have to transform every loop to be of the following
horror:

# general transformation of
# "for c in configurations..."
# to provide restart, break and continue
# callable from any nesting level inside of the loop

class loop1_restart(Exception): pass
class loop1_break(Exception): pass
class loop1_continue(Exception): pass

while True:
try:
for c in configurations:
while True:
try:
# inner loops go here, of course, they would have
to get
# all the boilerplate added, too
while nothing_bad_happened:
while step1_did_not_work:
   if cond1:
   raise loop1_restart()
   elif cond3:
   raise loop1_break()
   elif cond3:
   raise loop1_continue()

break

except loop1_continue:
pass
break
except loop1_restart:
pass
except loop1_break:
break

Of course this is extremely tedious and error prone.
If Python had named loops (PEP 3136, unfortunately rejected), this
would be trivial:
In Fortran I can continue (cycle), break (exit) and redo (goto label)
arbitrary
loops, which results in neat code:

10 loop1: do I=1,3
loop2: do J=1,4
print *,I,J
goto 10
cycle loop1
exit loop1
enddo loop2
enddo loop1


My question is, how do I obtain something that implements the
following logic:

@named_loop(fred) # I wish this would be possible
for i in range(10):
for j in range(10):
break fred # breaks out of outer loop
continue fred # continues outer loop
redo fred # resets outer loop and restarts with i=0


The best solution would be something along the Proposal D - Explicit
iterators
in PEP 3136, in this case it would even be possible to peek at the
next i or
advance/reverse the iterator a few steps.

Has anyone an idea on a nice way to write breaks/continues/redos for
deeply
nested loops?


Dan


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


Re: Subclassing str object

2011-08-31 Thread Yaşar Arabacı
I made a class like this (I shortened it just to show the point), what do
you think about it, do you think it is the python way of subclassing str (or
unicode in this case)

# -*- coding:utf-8 -*-class kelime(unicode):

def __init__(self,sozcuk):
self._sozcuk = sozcuk

def __getattribute__(self, isim):
att = super(kelime, self).__getattribute__(isim)

if not callable(att):
return att

def sonra_cagir(*args, **kwargs):
sonuc = att(*args, **kwargs)
if isinstance(sonuc, basestring):
return kelime(sonuc)
return sonuc
return sonra_cagir

def cogul(self):

for harf in reversed(self._sozcuk):
if harf in kalin:
return kelime(self._sozcuk + u"lar")
elif harf in ince:
return kelime(self._sozcuk + u"ler")

return kelime(self._sozcuk + u"lar")


31 Ağustos 2011 14:43 tarihinde Yaşar Arabacı  yazdı:

> Hİ,
>
> I originally posted my question to here:
> http://stackoverflow.com/q/7255655/886669 Could you people please look at
> it and enlighten me a little bit? I would appreciate an answer either from
> here or at stackoverflow. Thanks in advance.
>
> --
> http://yasar.serveblog.net/
>
>


-- 
http://yasar.serveblog.net/
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Why do class methods always need 'self' as the first parameter?

2011-08-31 Thread Steven D'Aprano
John Gordon wrote:

> In <[email protected]> "T.
> Goodchild"  writes:
> 
>> So why is 'self' necessary on class methods?  It seems to me that the
>> most common practice is that class methods *almost always* operate on
>> the instance that called them.  It would make more sense to me if this
>> was assumed by default, and for "static" methods (methods that are
>> part of a class, but never associated with a specific instance) to be
>> labelled instead.
> 
>> Just curious about the rationale behind this part of the language.
> 
> How would a method access instance variables without 'self'?

If Python had compile time declarations, the compiler could know whether x=1
was referring to a local variable x or an attribute x.

The reader might not, but the compiler would :)


By the way, although the Python docs are a little inconsistent, the usual
term here is "attribute" rather than "instance variable". 

Attributes need not live on the instance: they can also live on the class, a
superclass, or be computed at run-time via at least three different
mechanisms I can think of (__getattribute__, __getattr__, properties).
Local variables are treated a bit differently from attributes, but broadly
speaking, if you need a dot to access something, it's an attribute, if you
don't, it's a name binding (or variable).

Python even has two different sorts of errors for "variable" lookup
failures: NameError (or UnboundLocalError) for un-dotted names, and
AttributeError for dotted names.


> They probably could have made 'self' a magical attribute that just
> appears out of thin air instead of being passed as an argument, like
> 'this' in C++.  But would that really provide any benefit?

Well obviously the C++ people thought so :)

The effort to type "self, " in method signatures is pretty low. I don't
think it is a problem. But other languages are free to choose differently.
Cobra, for example, is explicitly derived from Python in many ways, but it
drops the "self" (as well as other changes).

http://cobra-language.com/docs/python/



-- 
Steven

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


Re: How to save the packages received by a network interface or some port in a file and resend the packages received when needed?

2011-08-31 Thread Emile van Sebille

On 8/31/2011 8:37 AM [email protected] said...

In fact,UDP is enough for me,I heared that tcpdump and netcat can store
and resend the udp packages to get the replay effect,but I don't know
how,


That may be, but I've never tried that.



or is there some better way? I am working on a Linux server and
only some basic terminal tools are available :)


If appropriate, I'd try the iptables --tee option.

Emile

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


Re: Why do class methods always need 'self' as the first parameter?

2011-08-31 Thread Grant Edwards
On 2011-08-31, Steven D'Aprano  wrote:

> Well obviously the C++ people thought so :)

Well _that's_ certainly a ringing endorsement in the context of 
designing a language that's easy to understand and use. 


;)

-- 
Grant Edwards   grant.b.edwardsYow! Where's SANDY DUNCAN?
  at   
  gmail.com
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Why do class methods always need 'self' as the first parameter?

2011-08-31 Thread Terry Reedy

On 8/31/2011 10:35 AM, T. Goodchild wrote:


But one of the things that bugs me is the requirement that all class
methods have 'self' as their first parameter.  On a gut level, to me
this seems to be at odds with Python’s dedication to simplicity.


Actually, it is a consequence of Python's dedication to simplicity. A 
method is simply a function that is an attribute of a class. (This is 
even clearer in Py 3.) Hence, there is no special syntax for methods.


Consider

def double(obj): return 2*obj.value

class C:
def __init__(self, val):
self.value = val

c = C(3)
C.double = double
c.doub = double
# not c.double as that would mask access to C.double in c.double() below
print(double(c), C.double(c), c.double(), c.doub(c))
# 6 6 6 6

--
Terry Jan Reedy


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


Re: fun with nested loops

2011-08-31 Thread aspineux
On Aug 31, 5:51 pm, Daniel  wrote:
> Dear All,
>
> I have some complicated loops of the following form
>
> for c in configurations: # loop 1
>     while nothing_bad_happened: # loop 2
>         while step1_did_not_work: # loop 3
>             for substeps in step1 # loop 4a
>                 # at this point, we may have to
>                 -leave loop 1
>                 -restart loop 4
>                 -skip a step in loop 4
>                 -continue on to loop 4b
>
>         while step2_did_not_work: # loop 4b
>             for substeps in step2:
>                 # at this point, we may have to
>                 -leave loop 1
>                 -restart loop 2
>                 -restart loop 4b
>                 ...
>         ...many more loops...
>
> I don't see any way to reduce these nested loops logically, they
> describe pretty well what the software has to do.
> This is a data acquisition application, so on ever line there is
> a lot of IO that might fail or make subsequent steps useless or
> require a
> retry.
>
> Now every step could need to break out of any of the enclosing loops.
> So basically I have to transform every loop to be of the following
> horror:
>
> # general transformation of
> # "for c in configurations..."
> # to provide restart, break and continue
> # callable from any nesting level inside of the loop
>
> class loop1_restart(Exception): pass
> class loop1_break(Exception): pass
> class loop1_continue(Exception): pass
>
> while True:
>     try:
>         for c in configurations:
>             while True:
>                 try:
>                     # inner loops go here, of course, they would have
> to get
>                     # all the boilerplate added, too
>                     while nothing_bad_happened:
>                         while step1_did_not_work:
>                            if cond1:
>                                raise loop1_restart()
>                            elif cond3:
>                                raise loop1_break()
>                            elif cond3:
>                                raise loop1_continue()
>
>                     break
>
>                 except loop1_continue:
>                     pass
>         break
>     except loop1_restart:
>         pass
>     except loop1_break:
>         break
>
> Of course this is extremely tedious and error prone.
> If Python had named loops (PEP 3136, unfortunately rejected), this
> would be trivial:
> In Fortran I can continue (cycle), break (exit) and redo (goto label)
> arbitrary
> loops, which results in neat code:
>
> 10 loop1: do I=1,3
>     loop2: do J=1,4
>         print *,I,J
>         goto 10
>         cycle loop1
>         exit loop1
>     enddo loop2
> enddo loop1
>
> My question is, how do I obtain something that implements the
> following logic:
>
> @named_loop(fred) # I wish this would be possible
> for i in range(10):
>     for j in range(10):
>         break fred # breaks out of outer loop
>         continue fred # continues outer loop
>         redo fred # resets outer loop and restarts with i=0
>
> The best solution would be something along the Proposal D - Explicit
> iterators
> in PEP 3136, in this case it would even be possible to peek at the
> next i or
> advance/reverse the iterator a few steps.
>
> Has anyone an idea on a nice way to write breaks/continues/redos for
> deeply
> nested loops?

Hi Dan, it looks like you have already answered all your questions.

one more idea, a kind of named loop:

ic=0
op='what to do'
while ichttp://en.wikipedia.org/wiki/Finite-state_machine


Regards


>
> Dan

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


Re: Usage of PyDateTime_FromTimestamp

2011-08-31 Thread MRAB

On 31/08/2011 04:39, Andreas wrote:

Am 30.08.2011 23:49, schrieb MRAB:


The key phrase is "argument tuple". The arguments passed to a Python
call are always a tuple, not PyFloat_Object.

You can build a tuple from the PyFloat_Object using:

 Py_BuildValue("(O)", float_object)

The "(O)" says to build a tuple ("(...)") containing a single object
("O").



Some other points:

Py_BuildValue defaults to building a tuple if the format string
contains multiple items, eg "OO" means the same as "(OO)". In your
case, there is only one, so the (...) is required.

The object returned will have a reference count of 1, and any Python
object passed to the function will have its reference count incremented.


Thank you very much! That solved my problem.
Here the full working example:

static double doubleValue = 1314761451;
PyObject *floatObj = NULL;
PyObject *timeTuple = NULL;
PyObject *dateTime = NULL;

floatObj = PyFloat_FromDouble(doubleValue);
timeTuple = Py_BuildValue("(O)", floatObj);
dateTime = PyDateTime_FromTimestamp(timeTuple);


PyFloat_FromDouble returns an object with a reference count of 1 and
Py_BuildValue increments that reference count, so you should probably
be decrementing it afterwards, otherwise you'll have a memory leak. The
usual rule is that you create it, use it, then 'decref' it.

Also, you should be doing some error-checking, seeing if Py_BuildValue,
etc, is returning NULL, indicating an error.
--
http://mail.python.org/mailman/listinfo/python-list


Closures and Partial Function Application

2011-08-31 Thread Travis Parks
I was a little disappointed the other day when I realized that
closures were read-only. I like to use closures quite a bit.

Can someone explain why this limitation exists? Secondly, since I can
cheat by wrapping the thing being closure-ified, how can I write a
simple wrapper that has all the same members as the thing (decorator),
that then applies them to the underlying thing?

I also like partial function application. What is the easiest way of
achieving this in Python? Would it look something like this:

def foo(x, y):
return x + y

xFoo = lambda y: foo(10, y)
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Closures and Partial Function Application

2011-08-31 Thread Arnaud Delobelle
On 31 August 2011 17:45, Travis Parks  wrote:
> I was a little disappointed the other day when I realized that
> closures were read-only. I like to use closures quite a bit.
>
> Can someone explain why this limitation exists? Secondly, since I can
> cheat by wrapping the thing being closure-ified, how can I write a
> simple wrapper that has all the same members as the thing (decorator),
> that then applies them to the underlying thing?

I don't understand.  Can you give an example?

> I also like partial function application. What is the easiest way of
> achieving this in Python? Would it look something like this:
>
> def foo(x, y):
>    return x + y
>
> xFoo = lambda y: foo(10, y)

from functools import partial

foo10 = partial(foo, 10)

HTH

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


Re: Subclassing str object

2011-08-31 Thread Terry Reedy

On 8/31/2011 7:43 AM, Yaşar Arabacı wrote:

Hİ,

I originally posted my question to here:
http://stackoverflow.com/q/7255655/886669 Could you people please look
at it and enlighten me a little bit? I would appreciate an answer either
from here or at stackoverflow.


I believe two people already gave my answer. If 'a' is bound to a str() 
object, "a.capitalize() will return a standard, unmodified str, not your 
custom class, so a.capitalize().mycustommethod() will fail.". If you 
reject that enlightenment, there is not much more to say. (And if you 
accept it, I am not sure what you still need.)


To put is a different way, if you want to chain together existing string 
methods and your new methods, you must start with objects of your new 
subclass and wrap every string method that you want to chain.


class mystr(str):
...
def capitalize(s): return mystr(str.capitalize(s))

You ended up doing something like this in your edit #3, except you 
perhaps should not have the __init__ method (depending on what 'sozcuk' 
is) and you do the wrapping on every call instead of just once and use 
the generalized signature *arg,**kwds for every method, which will make 
tracebacks much less informative.


--
Terry Jan Reedy


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


Re: Subclassing str object

2011-08-31 Thread Ian Kelly
2011/8/31 Yaşar Arabacı :
> I made a class like this (I shortened it just to show the point), what do
> you think about it, do you think it is the python way of subclassing str (or
> unicode in this case)

You don't need the _sozcuk attribute at all here.  It's just the same
as the value of the unicode object itself.  The code could be
simplified to:

class kelime(unicode):

def __getattribute__(self, isim):
att = super(kelime, self).__getattribute__(isim)
if not callable(att):
return att
def sonra_cagir(*args, **kwargs):
sonuc = att(*args, **kwargs)
if isinstance(sonuc, basestring):
return kelime(sonuc)
return sonuc
return sonra_cagir

def cogul(self):
for harf in reversed(self):
if harf in kalin:
return kelime(self + u"lar")
elif harf in ince:
return kelime(self + u"ler")
return kelime(self + u"lar")

Also, "isinstance(sonuc, basestring)" should probably be
"isinstance(sonuc, unicode)".  Otherwise you'll break the encode
method.

If you want "kelime(u'one') + kelime(u'two')" to return a kelime
instance, you'll need to override the __add__ special method as well.
Likewise for "kelime(u'repeat') * 20" and the __mul__ method.  You'll
also want to override __getitem__ so that slicing returns a kelime
instance as expected.

Finally, I gather that the goal of this is not to modify the behavior
of the unicode class at all, but just to add custom methods?  I would
strongly recommend that you not use a subclass for this, and instead
just write some functions that take a string to operate on as an
argument.  Subclassing built-in types tends to be tricky as you can
see, and this doesn't seem like a good reason to attempt it.

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


Re: Installing WebDAV server

2011-08-31 Thread Paul Kölle

Hi, answers below...

Am 31.08.2011 14:18, schrieb Fokke Nauta:

"Paul Kölle"  wrote in message
news:[email protected]...

Hi,

Am 30.08.2011 22:00, schrieb Fokke Nauta:

Hi all,

I am completely new to Python, but I'm confronted with a problem I can't
solve.

Welcome to python.


This is my question:

[snip]


I installed Python 3.2.1 and extracted the packages PyWebDAV and PyXML.
Now
I have a working Python app and 2 directories called PyWebDAV-0.9.4.1 and
PyXML-0.8.4. In the PyWebDAV README it says:

Installation and setup of server can be as easy as follows:

$ easy_install PyWebDAV
$ davserver -D /tmp -n -J

But of course it doesn't work like that. When I start up Python GUI I see
the ">>>" prompt instead of the "$" prompt. But where do I place the two
directories? And there is no easy_install script in the PyXML-0.8.4
directory, only a setup.py and ez_setup.py script. I guess the latter is
the
one to use. But how?

You dont install from "Python GUI", use normal cmd, navigate to the folder
you downloaded PyXML and PyWebDAV and run "python setup.py install"
(python.exe has to be in your PATH). Then you have to find the
startup-script "davserver". Find your python installation directory and
look into/Tools/Scripts, in my computer this is
E:\python27\Tools\Scripts. PyXML and PyWebDAV get installed in the
site-packages folder i.e. E:\python27\Lib/site-packages. You might have to
look for "davserver" there...



Thanks, Paul.

I ran "python setup.py install" in both the PyXML and PyWebDAV directories.
A lot of things happened and are added into those directories and I guess it
will be OK.
Next step, the startup-script "davserver". There is no script as such, also
not in \python27\tools\scripts.
I found 2 similar scripts:
1. server.py in D:\Python27\WebDAV\PyWebDAV\DAVServer
2. WebDAVServer.py in D:\Python27\WebDAV\PyWebDAV\DAV

Which one is the one to use?
Your install locations look odd, but it might work nevertheless. The 
server is in DAVServer\server.py, you can look at the file and you will see:


if __name__ == '__main__':
run()

at the bottom. This is the "entry point" of a python script if called 
from the command line.


My install looks a bit different but I can start the server as follows:
python.exe 
E:\Python27\Lib\site-packages\pywebdav-0.9.4.1-py2.7.egg\DAVServer\server.py 
-D c:\home -n

WARNING:pywebdav:Authentication disabled!
Listening on localhost (8008)



I also configured config.ini in D:\Python27\WebDAV\PyWebDAV\DAVServer
I would use a config file outside the program directory and use the -c 
or --config switch, run server.py without arguments to see possible 
startup options.




In this file it says:
"# Auth Database Table, Must exists in database prior to firstrun
dbtable=webDav

# Create User Database Table and Insert system user"

I created in MySQL a database called webDav.
I can create a table called User, but how many fields?
Don't know if that's documented somewhere but you can just look at the 
code in mysqlauth.py in the same directory as server.py. Seems it needs 
three columns, (User,Pass,can_write<0|1>) but I haven't 
tried.


cheers
 Paul

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


RE: Why do class methods always need 'self' as the first parameter?

2011-08-31 Thread Prasad, Ramit
>def double(obj): return 2*obj.value
>
>class C:
> def __init__(self, val):
> self.value = val
>
>c = C(3)
>C.double = double
>c.doub = double
># not c.double as that would mask access to C.double in c.double() below
>print(double(c), C.double(c), c.double(), c.doub(c))

Sorry if I get some of the following terminology wrong, I get a bit confused on 
Python terms. I hope the following is still coherent. (Is there a dictionary of 
Python terminology?)

Given the above example I get this
>>> print c.double(c)
TypeError: double() takes exactly 1 argument (2 given)

>>> print c.doub(c)
6

It seems to me that if I add a function to the list of class attributes it will 
automatically wrap with "self" but adding it to the object directly will not 
wrap the function as a method. Can somebody explain why? I would have thought 
that any function added to an object would be a method (unless decorated as a 
class method). 

Hmm, or does the decoration just tell Python not to turn an object's function 
into a method? I.e. Is the decorator basically just the syntactic sugar for 
doing the above?



Ramit


Ramit Prasad | JPMorgan Chase Investment Bank | Currencies Technology
712 Main Street | Houston, TX 77002
work phone: 713 - 216 - 5423




This email is confidential and subject to important disclaimers and
conditions including on offers for the purchase or sale of
securities, accuracy and completeness of information, viruses,
confidentiality, legal privilege, and legal entity disclaimers,
available at http://www.jpmorgan.com/pages/disclosures/email.  
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Closures and Partial Function Application

2011-08-31 Thread Chris Rebert
On Wed, Aug 31, 2011 at 9:45 AM, Travis Parks  wrote:
> I was a little disappointed the other day when I realized that
> closures were read-only. I like to use closures quite a bit.

Assuming I'm intuiting your question correctly, then you're incorrect;
they are "read/write". You just need a `nonlocal` declaration for the
variables in question. See http://www.python.org/dev/peps/pep-3104/
and http://docs.python.org/release/3.1.3/reference/simple_stmts.html#nonlocal
for details.

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


Re: Python Tools for Visual Studio - anyone using it?

2011-08-31 Thread mfperzel
On Aug 30, 1:34 pm, Philip Semanchuk  wrote:
> Hi all,
> I was reminded today (via Slashdot) of Python Tools for Visual Studio which 
> was discussed on this list back in March 
> (http://mail.python.org/pipermail/python-list/2011-March/1267662.html) and 
> has reached version 1.0. Is anyone here using it? Care to share pros & cons?
>
> Here's the URL for those who haven't heard of it 
> before:http://pytools.codeplex.com/
>
> Thanks
> Philip

I have been using it for a few days now.  I really like it.  It
doesn't have all of the features of other python specific IDEs but the
core features are done very well. It feels very complete. It seems to
have all the features I need and they feel rock solid.

Pros:
   Seemed very snappy compared to other Python IDEs I have used
   Debugger is very well done.
   Price( Free when using the VS 2010 Shell )
   **Subjective:  The editor just feels really nice.
Cons:
   "Basic" compared to other IDES. By this I mean it doesn't have some
of the nice-to-have features like explicit Django support.
   Have to download and install a 180mb "Shell" version of VS to use
it if you don't already have VS installed.


Personally I was very surprised at how good this plugin is.  It has
quickly become my python IDE of choice on Windows( or anywhere else )
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Subclassing str object

2011-08-31 Thread Yaşar Arabacı
@Ian: Thanks for you comments. I indeed didn't need the _sozcuk attribute at
all, so I deleted it. My class's addition and multiplication works without
overwriting __add__ and __mul__ because, this class uses unicode's __add__
and __mul__ than creates a new kelime instance with return value of those
methods in __getattribute__.

I didn't get a good grasp on how using basestring there might broke
encoding, could you explain a little bit more, or provide a reading
material?

And, as with the purpose, yes, it is intended to add some methods on
unicode. But the bigger purpose is to learn how to work with builtin
objects, or more spesifically, immutable ones. Therefore I value much to
make this simple example work, and make it work in a python way.

@Terry: yes, my *args **kwargs method made tracebacks much less informative.
I agree on that. I am still trying to implement a better way.

@all:

So the thing I wonder, when creating new instance in for example
capitalize() method, does str use something like self.__new__() or
unicode.__new__()? Because, in latter case, I could override the __new__
method on my class, so that every method would create my class's instance,
instead of unicode's



31 Ağustos 2011 20:11 tarihinde Ian Kelly  yazdı:

> 2011/8/31 Yaşar Arabacı :
> > I made a class like this (I shortened it just to show the point), what do
> > you think about it, do you think it is the python way of subclassing str
> (or
> > unicode in this case)
>
> You don't need the _sozcuk attribute at all here.  It's just the same
> as the value of the unicode object itself.  The code could be
> simplified to:
>
> class kelime(unicode):
>
>def __getattribute__(self, isim):
>att = super(kelime, self).__getattribute__(isim)
>if not callable(att):
>return att
>def sonra_cagir(*args, **kwargs):
>sonuc = att(*args, **kwargs)
>if isinstance(sonuc, basestring):
>return kelime(sonuc)
>return sonuc
>return sonra_cagir
>
>def cogul(self):
> for harf in reversed(self):
>if harf in kalin:
>return kelime(self + u"lar")
>elif harf in ince:
>return kelime(self + u"ler")
>return kelime(self + u"lar")
>
> Also, "isinstance(sonuc, basestring)" should probably be
> "isinstance(sonuc, unicode)".  Otherwise you'll break the encode
> method.
>
> If you want "kelime(u'one') + kelime(u'two')" to return a kelime
> instance, you'll need to override the __add__ special method as well.
> Likewise for "kelime(u'repeat') * 20" and the __mul__ method.  You'll
> also want to override __getitem__ so that slicing returns a kelime
> instance as expected.
>
> Finally, I gather that the goal of this is not to modify the behavior
> of the unicode class at all, but just to add custom methods?  I would
> strongly recommend that you not use a subclass for this, and instead
> just write some functions that take a string to operate on as an
> argument.  Subclassing built-in types tends to be tricky as you can
> see, and this doesn't seem like a good reason to attempt it.
>
> Cheers,
> Ian
>



-- 
http://yasar.serveblog.net/
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Closures and Partial Function Application

2011-08-31 Thread Travis Parks
On Aug 31, 1:18 pm, Chris Rebert  wrote:
> On Wed, Aug 31, 2011 at 9:45 AM, Travis Parks  wrote:
> > I was a little disappointed the other day when I realized that
> > closures were read-only. I like to use closures quite a bit.
>
> Assuming I'm intuiting your question correctly, then you're incorrect;
> they are "read/write". You just need a `nonlocal` declaration for the
> variables in question. Seehttp://www.python.org/dev/peps/pep-3104/
> andhttp://docs.python.org/release/3.1.3/reference/simple_stmts.html#nonl...
> for details.
>
> Cheers,
> Chris
>
>

Cool. So I just need to put "nonlocal" in front of the variable name.
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Closures and Partial Function Application

2011-08-31 Thread Travis Parks
On Aug 31, 1:51 pm, Travis Parks  wrote:
> On Aug 31, 1:18 pm, Chris Rebert  wrote:
>
> > On Wed, Aug 31, 2011 at 9:45 AM, Travis Parks  
> > wrote:
> > > I was a little disappointed the other day when I realized that
> > > closures were read-only. I like to use closures quite a bit.
>
> > Assuming I'm intuiting your question correctly, then you're incorrect;
> > they are "read/write". You just need a `nonlocal` declaration for the
> > variables in question. Seehttp://www.python.org/dev/peps/pep-3104/
> > andhttp://docs.python.org/release/3.1.3/reference/simple_stmts.html#nonl...
> > for details.
>
> > Cheers,
> > Chris
>
> Cool. So I just need to put "nonlocal" in front of the variable name.

Am I doing something wrong, here? nonlocal isn't registering. Which
version did this get incorporated?
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: fun with nested loops

2011-08-31 Thread Chris Angelico
On Thu, Sep 1, 2011 at 1:51 AM, Daniel  wrote:
>
> Has anyone an idea on a nice way to write breaks/continues/redos for
> deeply
> nested loops?
>

Do you only ever have one top-level loop that you would be naming? If
so, put that loop into a function and use return instead of break.
Unfortunately that doesn't work for continue.

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


Re: Closures and Partial Function Application

2011-08-31 Thread Ian Kelly
On Wed, Aug 31, 2011 at 12:02 PM, Travis Parks  wrote:
> Am I doing something wrong, here? nonlocal isn't registering. Which
> version did this get incorporated?

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


Re: Closures and Partial Function Application

2011-08-31 Thread Travis Parks
On Aug 31, 2:18 pm, Ian Kelly  wrote:
> On Wed, Aug 31, 2011 at 12:02 PM, Travis Parks  wrote:
> > Am I doing something wrong, here? nonlocal isn't registering. Which
> > version did this get incorporated?
>
> 3.0

Ah, okay. It would be really useful for unit testing. Unfortunately, I
want to make the code I am writing compatible with 2.x and 3.x. I will
just deal with it until 3.x takes over. Glad to know Guido sees the
importance.
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Why do class methods always need 'self' as the first parameter?

2011-08-31 Thread Chris Rebert
On Wed, Aug 31, 2011 at 10:12 AM, Prasad, Ramit
 wrote:
>>def double(obj): return 2*obj.value
>>
>>class C:
>>     def __init__(self, val):
>>         self.value = val
>>
>>c = C(3)
>>C.double = double
>>c.doub = double
>># not c.double as that would mask access to C.double in c.double() below
>>print(double(c), C.double(c), c.double(), c.doub(c))
>
> Sorry if I get some of the following terminology wrong, I get a bit confused 
> on Python terms. I hope the following is still coherent. (Is there a 
> dictionary of Python terminology?)

The documentation has a glossary:
http://docs.python.org/glossary.html
It's not entirely comprehensive though.

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


Re: Closures and Partial Function Application

2011-08-31 Thread [email protected]
On 31 août, 18:45, Travis Parks  wrote:
> I was a little disappointed the other day when I realized that
> closures were read-only. I like to use closures quite a bit.

They are not _strictly_ read only, but Python being first and foremost
an OO language, it's usually way simpler to use OO instead of closures
when you start needing such features.

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


Re: fun with nested loops

2011-08-31 Thread Daniel

> one more idea, a kind of named loop:
interesting idea, thanks.


>
> When it become too complicate, I use state 
> machine:http://en.wikipedia.org/wiki/Finite-state_machine
I unsuccessfully played a bit with a FSM, but there is a lot of data
that is passed around between the states and a lot of counting (like
trying a certain step n times), so the FSM turned out to be even more
complex. And I have to keep the code simple for non CS people to run
the actual experiment. The loops are kind of self-explanatory, this is
exactly how you would specify the experiment, even though I am really
hitting a wall at the moment.

Maybe I am really missing an obvious solution, because breaking out of
nested loops really doesn't seem like anything fancy. Fortran/c/c++/
Ruby/Perl all have that facility, even Java has named loops.
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: fun with nested loops

2011-08-31 Thread Daniel

> Do you only ever have one top-level loop that you would be naming? If
no, unfortunately not. The rough structure is several loops deep, and
I need to break/continue/restart many of them.
Continue is used more than break, because most of the time that I find
some strange value, I'd just _continue_ a few levels up
to restart the current measurements.


for some configurations
while not enough data collected
while not in the right state
for steps  in steps to bring the system to the right state
if the system is bad, break out of all loops
if it just need to be reset, just redo the steps
if it is ok, go to the next while loop
while in the right state
steps to do some measurements
...



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


Re: fun with nested loops

2011-08-31 Thread Chris Angelico
On Thu, Sep 1, 2011 at 5:07 AM, Daniel  wrote:
>> Do you only ever have one top-level loop that you would be naming? If
> no, unfortunately not. The rough structure is several loops deep, and
> I need to break/continue/restart many of them.
> Continue is used more than break, because most of the time that I find
> some strange value, I'd just _continue_ a few levels up
> to restart the current measurements.
>

Ah well, was worth a try. Raising exceptions smells wrong for this,
but peppering your code with sentinel checks isn't much better. I
don't really know what would be a good solution to this... except
maybe this, which was proposed a few years ago and which I'd never
heard of until Google showed it to me just now:
http://entrian.com/goto/

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


Re: Python Tools for Visual Studio - anyone using it?

2011-08-31 Thread Andrew McLean
I understand that Python Tools for Visual Studio doesn't work with VS 
Express, but does work with the (free) VS 2010 Shell. Does anyone know 
if you can install VS Express and VS Shell on the same machine?

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


Re: Python Tools for Visual Studio - anyone using it?

2011-08-31 Thread Brian Curtin
On Wed, Aug 31, 2011 at 14:29, Andrew McLean  wrote:

> I understand that Python Tools for Visual Studio doesn't work with VS
> Express, but does work with the (free) VS 2010 Shell. Does anyone know if
> you can install VS Express and VS Shell on the same machine?


Yes, because the shell and Express work together.
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Closures and Partial Function Application

2011-08-31 Thread Travis Parks
On Aug 31, 2:03 pm, "[email protected]"
 wrote:
> On 31 août, 18:45, Travis Parks  wrote:
>
> > I was a little disappointed the other day when I realized that
> > closures were read-only. I like to use closures quite a bit.
>
> They are not _strictly_ read only, but Python being first and foremost
> an OO language, it's usually way simpler to use OO instead of closures
> when you start needing such features.

I like to leave OO to large-scale architectures and leave functional
paradigms for implementation details.

Writing an entire class for wrapping an int seems excessive.
Especially if that code is limited to a small scope. I agree, though,
that there is a time and a place for everything.
-- 
http://mail.python.org/mailman/listinfo/python-list


Text file with mixed end-of-line terminations

2011-08-31 Thread Alex van der Spek

I have a text file that uses both '\r' and '\r\n' end-of-line terminations.

The '\r' terminates the first 25 lines or so, the remainder is termiated 
with '\r\n'


Reading this file like this:


for line in open(filename,'r'):
   line= #Do whatever needs doing...


The first line read is actually a string consiting of the first 25 lines.
The readline() method does the same thing.

Is there a way to make it read one line at a time, regardless of the line 
termination?


By the way, the newlines attribute reports None after reading a few lines. I 
tried on Linux and Windows. I use the standard binaries as distributed.


Thanks in advance,
Alex van der Spek


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


Re: Text file with mixed end-of-line terminations

2011-08-31 Thread Chris Rebert
On Wed, Aug 31, 2011 at 12:37 PM, Alex van der Spek  wrote:
> I have a text file that uses both '\r' and '\r\n' end-of-line terminations.
>
> The '\r' terminates the first 25 lines or so, the remainder is termiated
> with '\r\n'

> Is there a way to make it read one line at a time, regardless of the line
> termination?

Universal Newline Support
http://www.python.org/dev/peps/pep-0278/

http://docs.python.org/library/functions.html#open
(Modes involving "U")

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


Re: Why do class methods always need 'self' as the first parameter?

2011-08-31 Thread Chris Torek
>In article <[email protected]>
T. Goodchild asked, in part:
>>... One of the things that bugs me is the requirement that all class
>>methods have 'self' as their first parameter.

In article <[email protected]>
Steven D'Aprano   wrote:
[Comprehensive reply, noting that these are actually instance
methods, and that there are class and static methods as well]:

>Python does have class methods, which receive the class, not the instance,
>as the first parameter. These are usually written something like this:
>
>class K(object):
>@classmethod
>def spam(cls, args):
>print cls  # always prints "class K", never the instance
>
>Just like self, the name cls is a convention only. Class methods are usually
>used for alternate constructors.
>
>There are also static methods, which don't receive any special first
>argument, plus any other sort of method you can invent, by creating
>descriptors... but that's getting into fairly advanced territory. ...
[rest snipped]

I am not sure whether T. Goodchild was asking any of the above or
perhaps also one other possible question: if an instance method
is going to receive an automatic first "self" parameter, why require
the programmer to write that parameter in the "def"?  For instance
we *could* have:

class K(object):
def meth1(arg1, arg2):
self.arg1 = arg1 # self is "magically available"
self.arg2 = arg2

@classmethod
def meth2(arg):
use(cls) # cls is "magically available"

and so on.  This would work fine.  It just requires a bit of implicit
sneakiness in the compiler: an instance method magically creates
a local variable named "self" that binds to the invisible first
parameter, and a class method magically creates a local variable
named "cls" that binds to the invisible first parameter, and so
on.

Instead, we have a syntax where you, the programmer, write out the
name of the local variable that binds to the first parameter.  This
means the first parameter is visible.  Except, it is only visible
at the function definition -- when you have the instance and call
the instance or class method:

black_knight = K()
black_knight.meth1('a', 1)
black_knight.meth2(2)

the first parameters (black_knight, and black_knight.__class__,
respectively) are magic, and invisible.

Thus, Python is using the "explicit is better than implicit" rule
in the definition, but not at the call site.  I have no problem with
this.  Sometimes I think implicit is better than explicit.  In this
case, there is no need to distinguish, at the calls to meth1() and
meth2(), as to whether they are "class" or "instance" methods.  At
the *calls* they would just be distractions.

At the *definitions*, they are not as "distraction-y" since it is
important to know, during the definition, whether you are operating
on an instance (meth1) or the class itself (meth2), or for that
matter on neither (static methods).  One could determine this from
the absence or presence of "@classmethod" or "@staticmethod", but
the minor redundancy in the "def" statement seems, well, minor.

Also, as a bonus, it lets you obfuscate the code by using a name
other than "self" or "cls". :-)
-- 
In-Real-Life: Chris Torek, Wind River Systems
Intel require I note that my opinions are not those of WRS or Intel
Salt Lake City, UT, USA (40°39.22'N, 111°50.29'W)  +1 801 277 2603
email: gmail (figure it out)  http://web.torek.net/torek/index.html
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: try... except with unknown error types

2011-08-31 Thread Chris Torek
In article ,
Terry Reedy   wrote:
>I would expect that catching socket.error (or even IOError) should catch 
>all of those.
>
>"exception socket.error
>A subclass of IOError ...

Except that, as Steven D'Aprano almost noted elsethread, it isn't
(a subclass of IOError -- the note was that it is not a subclass
of EnvironmentError).  In 2.x anyway:

>>> import socket
>>> isinstance(socket.error, IOError)
False
>>> isinstance(socket.error, EnvironmentError)
False
>>> 

(I just catch socket.error directly for this case.)

(I have also never been sure whether something is going to raise
an IOError or an OSError for various OS-related read or write
operation failures -- such as exceeding a resource limit, for
instance -- so most places that do I/O operations on OS files, I
catch both.  Still, it sure would be nice to have a static analysis
tool that could answer questions about potential exceptions. :-) )
-- 
In-Real-Life: Chris Torek, Wind River Systems
Intel require I note that my opinions are not those of WRS or Intel
Salt Lake City, UT, USA (40°39.22'N, 111°50.29'W)  +1 801 277 2603
email: gmail (figure it out)  http://web.torek.net/torek/index.html
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Why no warnings when re-assigning builtin names?

2011-08-31 Thread Chris Torek
(I realize this thread is old.  I have been away for a few weeks.
I read through the whole thread, though, and did not see anyone
bring up this one particular point: there is already a linting
script that handles this.)

>On Mon, Aug 15, 2011 at 10:52 PM, Gerrat Rickert
> wrote:
>> With surprising regularity, I see program postings (eg. on StackOverflow)
>> from inexperienced Python users accidentally re-assigning built-in names.
>>
>> For example, they'll innocently call some variable, `list', and assign a
>> list of items to it.

In article 
Chris Angelico   wrote:
>It's actually masking, not reassigning. That may make it easier or
>harder to resolve the issue.
>
>If you want a future directive that deals with it, I'd do it the other
>way - from __future__ import mask_builtin_warning or something - so
>the default remains as it currently is. But this may be a better job
>for a linting script.

The pylint program already does this:

$ cat shado.py
"module doc"
def func(list):
"func doc"
return list
$ pylint shado.py
* Module shado
W0622:  2:func: Redefining built-in 'list'
...
Your code has been rated at 6.67/10

If your shadowing is done on purpose, you can put in a pylint
comment directive to suppress the warning.

Pylint is the American Express Card of Python coding: "don't leave
$HOME without it!" :-)
-- 
In-Real-Life: Chris Torek, Wind River Systems
Intel require I note that my opinions are not those of WRS or Intel
Salt Lake City, UT, USA (40°39.22'N, 111°50.29'W)  +1 801 277 2603
email: gmail (figure it out)  http://web.torek.net/torek/index.html
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Why do class methods always need 'self' as the first parameter?

2011-08-31 Thread Ian Kelly
On Wed, Aug 31, 2011 at 11:12 AM, Prasad, Ramit
 wrote:
> It seems to me that if I add a function to the list of class attributes it 
> will automatically wrap with "self" but adding it to the object directly will 
> not wrap the function as a method. Can somebody explain why? I would have 
> thought that any function added to an object would be a method (unless 
> decorated as a class method).

Because things stored on the class are generally viewed as part of the
class definition, whereas things stored on an instance are generally
viewed as data -- a function stored on an object instance is usually
just meant to be a function.  Consider the following code:

class Sorter(object):
def __init__(self, keyfunc):
self.keyfunc = keyfunc
def sort(self, item_list):
item_list.sort(key=self.keyfunc)

sorter = Sorter(lambda x: x.id)
sorter.sort(some_list_of_items)

If adding keyfunc as an attribute to the object wrapped it up as a
method, it would break, since the function is not expecting a "self"
argument.

More technically, because descriptors are only invoked when they're
stored on the class.

> Hmm, or does the decoration just tell Python not to turn an object's function 
> into a method? I.e. Is the decorator basically just the syntactic sugar for 
> doing the above?

If you mean the staticmethod decorator, yes, it pretty much just wraps
the function as a "staticmethod" instance to prevent it from being
wrapped into an ordinary method when it's accessed.

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


Re: Why no warnings when re-assigning builtin names?

2011-08-31 Thread Seebs
On 2011-08-31, Chris Torek  wrote:
> (I realize this thread is old.  I have been away for a few weeks.
> I read through the whole thread, though, and did not see anyone
> bring up this one particular point: there is already a linting
> script that handles this.)

Yes.  I've found pylint... A weird mix of "very helpful, thanks" and
"oh, come off it".  A thread about pylint is where I got my example of
the natural Python way to express a parabola:
theValueRepresentingTheYAxisLocationOfThePoint = 
theValueRepresentingTheXAxisLocationOfThe Point *
theValueRepresentingTheXAxisLocationOfThe Point

I still say that there are times when short names are natural and
idiomatic, and much clearer than long names.  :P

But I do think that, given the basic assumption that pylint is a core
tool for vetting code, it is probably adequate for it to provide the
warnings.

-s
-- 
Copyright 2011, all wrongs reversed.  Peter Seebach / [email protected]
http://www.seebs.net/log/ <-- lawsuits, religion, and funny pictures
http://en.wikipedia.org/wiki/Fair_Game_(Scientology) <-- get educated!
I am not speaking for my employer, although they do rent some of my opinions.
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Subclassing str object

2011-08-31 Thread Ian Kelly
2011/8/31 Yaşar Arabacı :
> @Ian: Thanks for you comments. I indeed didn't need the _sozcuk attribute at
> all, so I deleted it. My class's addition and multiplication works without
> overwriting __add__ and __mul__ because, this class uses unicode's __add__
> and __mul__ than creates a new kelime instance with return value of those
> methods in __getattribute__.

I think if you try it, you'll find that the result is an ordinary
unicode object, not a kelime instance, because __getattribute__ is
*not* invoked when Python looks up special method names on the class
object.

> I didn't get a good grasp on how using basestring there might broke
> encoding, could you explain a little bit more, or provide a reading
> material?

The unicode.encode method takes a unicode object and encodes it into a
byte string (a str object).  If you then wrap that up in a kelime
object, which is a unicode subclass, it has to decode the string back
to unicode (using the default ascii codec, since it isn't specified).
Thus the result of the call is no longer an encoded byte string as
would be expected.  If you're lucky, you'll get a UnicodeDecodeError
since it's just using the ascii codec.  If you're unlucky, it will
silently return a result of the wrong type.

> So the thing I wonder, when creating new instance in for example
> capitalize() method, does str use something like self.__new__() or
> unicode.__new__()? Because, in latter case, I could override the __new__
> method on my class, so that every method would create my class's instance,
> instead of unicode's

No, that doesn't work.
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Closures and Partial Function Application

2011-08-31 Thread Terry Reedy

On 8/31/2011 12:45 PM, Travis Parks wrote:

I was a little disappointed the other day when I realized that
closures were read-only.


'Were', in 2.x. The standard 2.x workaround for a single nonlocal is to 
wrap it in a list.


def f():
i = [0]
def g(): i[0] += 1
for j in range(5): g()
print(i)

f()
# 5

--
Terry Jan Reedy

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


Re: Why do class methods always need 'self' as the first parameter?

2011-08-31 Thread Terry Reedy

On 8/31/2011 1:12 PM, Prasad, Ramit wrote:

def double(obj): return 2*obj.value

class C:
  def __init__(self, val):

>>   self.value = val


c = C(3)

>> C.double = double
>> c.doub = double
>> # not c.double as that would mask access to C.double in c.double()
>> print(double(c),

C.double(c), c.double(), c.doub(c))


Above is 3.2 code. To be exactly equivalent with 2.x, you need
class C(object):


Sorry if I get some of the following terminology wrong, I get a bit
confused on Python terms. I hope the following is still coherent. (Is
there a dictionary of Python terminology?)



Given the above example I get this

print c.double(c)

TypeError: double() takes exactly 1 argument (2 given)


Right, because c.double() translates to C.double(c), and c.double(x)
translates to C.double(c,x), which is not valid.


print c.doub(c)

6

It seems to me that if I add a function to the list of class
attributes it will automatically wrap with "self"


When accessed via an instance of the class, the instance is 
automagically added as the first argument to be bound to the first 
parameter. The name 'self' is a convention, not a requirement.



but adding it to
the object directly will not wrap the function as a method. Can
somebody explain why?


Someone else did. Not wrapping is normal, wrapping is a special case.

--
Terry Jan Reedy

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


Re: PC locks up with list operations

2011-08-31 Thread Gregory Ewing

Steven D'Aprano wrote:


As far as I know, ulimit ("user limit") won't help. It can limit the amount
of RAM available to a process, but that just makes the process start using
virtual memory more quickly.


ulimit -v is supposed to set the maximum amount of virtual
memory the process can use.


It can also limit the amount of virtual memory
used by the shell, but not of other processes.


That doesn't sound right. Not sure about Linux, but the
man page for sh on Darwin says:

Provides  control  over the resources available to the shell and
to processes started by it, on systems that allow such  control.

The Python process should also be able to set its own
limits using resource.setrlimit().

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


Re: Handling 2.7 and 3.0 Versions of Dict

2011-08-31 Thread Gregory Ewing

Ian Kelly wrote:


if sys.version_info < (3,):
getDictValues = dict.itervalues
else:
getDictValues = dict.values

(which is basically what the OP was doing in the first place).


And which he seemed to think didn't work for some
reason, but it seems fine as far as I can tell:

Python 2.7 (r27:82500, Oct 15 2010, 21:14:33)
[GCC 4.2.1 (Apple Inc. build 5664)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> gv = dict.itervalues
>>> d = {1:'a', 2:'b'}
>>> gv(d)


% python3.1
Python 3.1.2 (r312:79147, Mar  2 2011, 17:43:12)
[GCC 4.2.1 (Apple Inc. build 5664)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> gv = dict.values
>>> d = {1:'a', 2:'b'}
>>> gv(d)
dict_values(['a', 'b'])

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


Re: fun with nested loops

2011-08-31 Thread Steven D'Aprano
Daniel wrote:

> And I have to keep the code simple for non CS people to run
> the actual experiment.

Do you think the software in the Apple iPod is "simple"? Or Microsoft
Windows? No. You need to keep the *interface* simple. The internal details
can be as complicated as they are needed to be.

Same applies to your data acquisition application. Unless you expect these
non-CS people to be hacking the source code, they only interact with the
interface, not the internals.

Earlier, back in your initial post, you said:

"I don't see any way to reduce these nested loops logically, they
describe pretty well what the software has to do.
This is a data acquisition application, so on ever line there is
a lot of IO that might fail or make subsequent steps useless or
require a retry."

Do you think you're the first person to have written a data acquisition
application in Python? Almost certainly you can simplify the structure of
the code by splitting it into functions appropriately, instead of the
spaghetti code you have (apparently) written with jumps all over the place.
To take the most obvious, simple example: any time you have a loop that you
might want to redo, the right solution is to put the loop inside a
function, and then "redo the loop" becomes "call the function again".

I suppose that, just possibly, your application really would benefit from
named labels to jump to. But if so, you've stumbled across something rarer
than iridium.



-- 
Steven

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


Re: fun with nested loops

2011-08-31 Thread Steven D'Aprano
Chris Angelico wrote:

> Ah well, was worth a try. Raising exceptions smells wrong for this,
> but peppering your code with sentinel checks isn't much better. I
> don't really know what would be a good solution to this... except
> maybe this, which was proposed a few years ago and which I'd never
> heard of until Google showed it to me just now:
> http://entrian.com/goto/

You're a wicked, wicked man.

:)


-- 
Steven

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


Re: Why do class methods always need 'self' as the first parameter?

2011-08-31 Thread Steven D'Aprano
Chris Torek wrote:

>>There are also static methods, which don't receive any special first
>>argument, plus any other sort of method you can invent, by creating
>>descriptors... but that's getting into fairly advanced territory. ...
> [rest snipped]
> 
> I am not sure whether T. Goodchild was asking any of the above or
> perhaps also one other possible question: if an instance method
> is going to receive an automatic first "self" parameter, why require
> the programmer to write that parameter in the "def"?

Er, yes, just like I suggested in my opening paragraph, and as I answered
following the bit you marked as snipped :)


> For instance 
> we *could* have:
> 
> class K(object):
> def meth1(arg1, arg2):
> self.arg1 = arg1 # self is "magically available"
> self.arg2 = arg2
> 
> @classmethod
> def meth2(arg):
> use(cls) # cls is "magically available"
> 
> and so on.  This would work fine.  It just requires a bit of implicit
> sneakiness in the compiler: an instance method magically creates
> a local variable named "self" that binds to the invisible first
> parameter, and a class method magically creates a local variable
> named "cls" that binds to the invisible first parameter, and so
> on.

It would need more than "a bit", because methods are just wrappers around
functions. One way would be for Python to give that up, and require methods
to be special built-in types like functions. That adds complexity to the
compiler, and (very likely) would decrease the level of dynamism possible.

Another way would be for the compiler to perform darkest black magic to
determine whether the function was being called from inside a method or
not. That would be complicated and fragile.

[...] 
> At the *definitions*, they are not as "distraction-y" since it is
> important to know, during the definition, whether you are operating
> on an instance (meth1) or the class itself (meth2), or for that
> matter on neither (static methods).  One could determine this from
> the absence or presence of "@classmethod" or "@staticmethod"

classmethod and staticmethod are functions, not declarations. You can't
assume that @classmethod is the only way to get a class method: the
metaclass could do it, or you could inject one in from the outside. You can
dynamically change the state of a method from instance method to class
method and back again at run-time.

Python classes have a lot of dynamism made possible by the fact that methods
are just wrappers around functions with an explicitly declared "self". That
dynamism is rarely used, but not *that* rarely, and is very useful when
used. Implicit self would likely negate all that.



-- 
Steven

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


Re: PC locks up with list operations

2011-08-31 Thread Tim Chase

On 08/31/11 18:31, Gregory Ewing wrote:

The Python process should also be able to set its own
limits using resource.setrlimit().


A new corner of stdlib that I've never poked at.  Thanks for the 
suggestion.  Disappointed though that it doesn't seem to have 
docstrings on the functions, so I had to wade back out to the 
online docs to probe at it.  Granted, after the fact, they were 
pretty obvious, but it would be nice if 
"help(resource.getrlimit)" gave me a hint as to what that one 
expected parameter should have been.


-tim

  import resource as r
  token = "RLIMIT_"
  for item in dir(r):
if item.startswith(token):
  print "%s:" % item[len(token):],
  print "%s hard/%s soft" % r.getrlimit(getattr(r, item))



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


Re: Handling 2.7 and 3.0 Versions of Dict

2011-08-31 Thread Travis Parks
On Aug 31, 7:37 pm, Gregory Ewing  wrote:
> Ian Kelly wrote:
> > if sys.version_info < (3,):
> >     getDictValues = dict.itervalues
> > else:
> >     getDictValues = dict.values
>
> > (which is basically what the OP was doing in the first place).
>
> And which he seemed to think didn't work for some
> reason, but it seems fine as far as I can tell:
>
> Python 2.7 (r27:82500, Oct 15 2010, 21:14:33)
> [GCC 4.2.1 (Apple Inc. build 5664)] on darwin
> Type "help", "copyright", "credits" or "license" for more information.
>  >>> gv = dict.itervalues
>  >>> d = {1:'a', 2:'b'}
>  >>> gv(d)
> 
>
> % python3.1
> Python 3.1.2 (r312:79147, Mar  2 2011, 17:43:12)
> [GCC 4.2.1 (Apple Inc. build 5664)] on darwin
> Type "help", "copyright", "credits" or "license" for more information.
>  >>> gv = dict.values
>  >>> d = {1:'a', 2:'b'}
>  >>> gv(d)
> dict_values(['a', 'b'])
>
> --
> Greg

My problem was that I didn't understand the scoping rules. It is still
strange to me that the getValues variable is still in scope outside
the if/else branches.
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Why do class methods always need 'self' as the first parameter?

2011-08-31 Thread Chris Angelico
On Thu, Sep 1, 2011 at 10:48 AM, Steven D'Aprano
 wrote:
> Python classes have a lot of dynamism made possible by the fact that methods
> are just wrappers around functions with an explicitly declared "self". That
> dynamism is rarely used, but not *that* rarely, and is very useful when
> used. Implicit self would likely negate all that.
>

Hmm. Got any examples sitting around? I'm curious as to what you can
do with this. I'm like a kid with a new chemistry set - "what happens
if I mix a little of everything together?"...

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


Re: Why do class methods always need 'self' as the first parameter?

2011-08-31 Thread Eric Snow
On Wed, Aug 31, 2011 at 7:47 PM, Chris Angelico  wrote:
> On Thu, Sep 1, 2011 at 10:48 AM, Steven D'Aprano
>  wrote:
>> Python classes have a lot of dynamism made possible by the fact that methods
>> are just wrappers around functions with an explicitly declared "self". That
>> dynamism is rarely used, but not *that* rarely, and is very useful when
>> used. Implicit self would likely negate all that.
>>
>
> Hmm. Got any examples sitting around? I'm curious as to what you can
> do with this. I'm like a kid with a new chemistry set - "what happens
> if I mix a little of everything together?"...

First thing that comes to mind is calling a base class's
implementation of a method:

class X(Y):
def __init__(self, value):
Y.__init__(self)
self.value = value

-eric

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


Help required accessing dictionary

2011-08-31 Thread mrinalini

Hi

I need to access the dictionary of the script that I am running through
my vc++ application by embedding python.
I am linking to python dynamically. I want to obtain the dictionary of
the script and access the variables declared in the script.
However, with the PyObject * that I get from the dictionary, I am not
able to find the type of the object. The reason being that
GetProcAddress to PyInt_Check returns a NULL. The same thing with
PyFloat_Check and so on. I think this is because they are macros and 
not

exported functions.

What can be done to be able to perform these checks without statically
linking to the pyhon lib ?

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


Re: Why do class methods always need 'self' as the first parameter?

2011-08-31 Thread Chris Torek
In article <[email protected]>
Steven D'Aprano   wrote:
>Er, yes, just like I suggested in my opening paragraph, and as I answered
>following the bit you marked as snipped :)

Oops, so you did (went back and re-read it).  Must have gotten
interrupted and lost track. :-)

>> [A different hack would] requires a bit of implicit
>> sneakiness in the compiler: an instance method magically creates
>> a local variable named "self" that binds to the invisible first
>> parameter, and a class method magically creates a local variable
>> named "cls" that binds to the invisible first parameter, and so
>> on.

>It would need more than "a bit", because methods are just wrappers
>around functions.

Well, depends on how the hack would be done. :-)  For instance,
the @decorator might turn on something that "undoes" or "replaces"
the "self" parameter.  That is, with ordinary class functions and
methods:

class HackyNotQuitePythonVersion:
def ordinary(arg):
self.arg = arg

would compile to (approximately):

class PythonVersion:
def __mrap(self, *args, **kwargs):
def ordinary(arg):
self.arg = arg
ordinary(*args, **kwargs)
ordinary = __mrap

(add the usual other manipulations to suit here, i.e., all the
stuff for making introspection work right, i.e., @functools.wraps).
@staticmethod would suppress the wrapper entirely, while @classmethod
would change it to one that binds the "cls" argument.  (Any function
without some appropriate @whatever gets the Method Wrapper __mrap.
@staticmethod tells the class builder not to add any wrapper, and
@classmethod tells it to add the Class Wrapper __crap.  [The name
tells you what I think of the above code. :-) ])

(Note subtle ground for bugs here: if you then actually define a
"self" parameter, it shadows the outer-scope one from the wrapper.
So while I am not even proposing that anyone should do this in the
first place, it has more downsides than mere implementation
complexity.)

>Another way would be for the compiler to perform darkest black magic to
>determine whether the function was being called from inside a method or
>not. That would be complicated and fragile.

Yes, even worse than my outlined implementation above, I think.

>classmethod and staticmethod are functions, not declarations.

They are decorator functions, but to someone *reading the code*
they are also "declarations" of sort.  This is all I meant: they
tell the (human) reader/programmer which "secret arguments" to
expect.

>You can't assume that @classmethod is the only way to get a
>class method: the metaclass could do it, or you could inject
>one in from the outside.

Yes, but that would all still work, as in this not-quite-Python
(worsened-Python) language, whoever writes those metaclasses and
other decorators would continue to do whatever icky stuff was
required (e.g., __mrap and __crap above).  It would mean yet more
things for people to know about, but then, metaclasses and decorators
*always* mean that:

@hmm
def spam():
return magic

Is "magic" something supplied by the decorator?  You have to look
at the decorator to find out, as the rather horrid example I have
attached shows.

(Note: I am doing all this is python 2.x on the laptop.  Using
global, in @hmm, is evil, but it works.  I did not bother trying
to write a metaclass that inserts __mrap, etc., but I believe it
can be done.)

>Python classes have a lot of dynamism made possible by the fact that methods
>are just wrappers around functions with an explicitly declared "self". That
>dynamism is rarely used, but not *that* rarely, and is very useful when
>used. Implicit self would likely negate all that.

I do not believe it would *negate* it, just *complicate* it.  But
that is not a good thing either. :-)

- horrible example / test code below
import functools
def hmm(func):
@functools.wraps(func)
def wrapper(*args, **kwargs):
global magic, rlevel
try:
save = magic, rlevel
restore = True
rlevel += 1
except NameError:
restore = False
rlevel = 1
magic = func.__name__ + " and eggs"
ret = func(*args, **kwargs)
if restore:
magic, rlevel = save
else:
del magic, rlevel
return ret
return wrapper

@hmm
def ham():
if rlevel < 2:
print spam()
return magic
@hmm
def spam():
return magic

print ham()
try:
print magic
except NameError:
print 'name "magic" is not available here, as desired'
try:
print rlevel
except NameError:
print 'name "rlevel" is not available here, as desired'

class X(object):
def __mrap(self, *args, **kwargs):
def xset(arg):
self.arg = arg
xset(*args, **kwargs)
xset = __mrap
def __mrap(self, *args, **kwargs):
def show():
print self.arg
show(*args, **kwargs)

How to daemonize a HTTPServer

2011-08-31 Thread babbu Pehlwan
I have written a http server using BaseHTTPServer module. Now I want
to instantiate it through another python script. The issue here is
after instantiate the control doesn't come back till the server is
running. Please suggest.
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: try... except with unknown error types

2011-08-31 Thread John Nagle

On 8/21/2011 5:30 PM, Steven D'Aprano wrote:

Chris Angelico wrote:



A new and surprising mode of network failure would be indicated by a
new subclass of IOError or EnvironmentError.


/s/would/should/

I don't see why you expect this, when *existing* network-related failures
aren't:


import socket
issubclass(socket.error, EnvironmentError)

False

(Fortunately that specific example is fixed in Python 3.)


   I think I reported that some years ago.

   There were some other errors in the URL and SSL area that
weren't subclasses of EnvironmentError.  It's also possible
to get UnicodeError from URL operations.

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


idiomatic analogue of Perl's: while (<>) { ... }

2011-08-31 Thread Sahil Tandon
I've been tasked with converting some programs from Perl -> Python, and
am (as will soon be obvious) new to the language.  A few archive/google
searches were inconclusive on a consensus approach, which is OK, but I
just wonder if there is a more Python-esque way to do the following in
Python 2.7.1:

%%
# unbuffer STDOUT
sys.stdout = os.fdopen(sys.stdout.fileno(), 'w', 0)

# process input, line-by-line, and print responses after parsing input 
while 1:
  rval = parse(raw_input())
  if rval == None:
print('foo')
  else:
print('bar')
%%

This works, but while reading the documentation, I thought of using 'for
line in fileinput.input()' in lieu of 'while 1:' construct.  This does
not work when debugging the program on the command line -- the script
appears to just hang no matter what is typed into STDIN.  I believe this
is because of some internal buffering when using fileinput.  Is there a
recommended way to disable such buffering?  Am I taking a totally wrong
approach?

Feel free to just link me to previous discussions on the topic(s) if I
have missed them.  Please be gentle with your cluebats. :-)

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


Re: How to daemonize a HTTPServer

2011-08-31 Thread Kushal Kumaran
On 1 Sep 2011 08:54, "babbu Pehlwan"  wrote:
>
> I have written a http server using BaseHTTPServer module. Now I want
> to instantiate it through another python script. The issue here is
> after instantiate the control doesn't come back till the server is
> running. Please suggest.

What did a web search for "python daemon" lead to? I believe there's a
python-daemon package available on pypi.

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


Re: idiomatic analogue of Perl's: while (<>) { ... }

2011-08-31 Thread Steven D'Aprano
On Thu, 1 Sep 2011 02:56 pm Sahil Tandon wrote:

> I've been tasked with converting some programs from Perl -> Python, and
> am (as will soon be obvious) new to the language.  A few archive/google
> searches were inconclusive on a consensus approach, which is OK, but I
> just wonder if there is a more Python-esque way to do the following in
> Python 2.7.1:
> 
> %%
> # unbuffer STDOUT
> sys.stdout = os.fdopen(sys.stdout.fileno(), 'w', 0)

I've never bothered with unbuffered stdout, but that looks fine to me.

I'm not sure if it is necessary though, because print seems to automatically
flush the buffer after each line in my testing. Unless you're printing
repeatedly to the same line, I'm not sure unbuffered stdout is helpful.


> # process input, line-by-line, and print responses after parsing input
> while 1:
>   rval = parse(raw_input())
>   if rval == None:
> print('foo')
>   else:
> print('bar')
> %%

"while True" is considered slightly more idiomatic (readable), but
otherwise, that seems fine.

 
> This works, but while reading the documentation, I thought of using 'for
> line in fileinput.input()' in lieu of 'while 1:' construct.  This does
> not work when debugging the program on the command line -- the script
> appears to just hang no matter what is typed into STDIN.  I believe this
> is because of some internal buffering when using fileinput.  Is there a
> recommended way to disable such buffering?  Am I taking a totally wrong
> approach?

I'm not sure anything about fileinput is exactly *recommended*, it's kinda
discouraged on account of being a bit slow. See help(fileinput) at the
interactive prompt.

For what it's worth, the default buffersize for fileinput.input is 0, so if
that doesn't do what you want, I don't think fileinput is the right
solution.



-- 
Steven

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


Re: idiomatic analogue of Perl's: while (<>) { ... }

2011-08-31 Thread Peter Otten
Sahil Tandon wrote:

> I've been tasked with converting some programs from Perl -> Python, and
> am (as will soon be obvious) new to the language.  A few archive/google
> searches were inconclusive on a consensus approach, which is OK, but I
> just wonder if there is a more Python-esque way to do the following in
> Python 2.7.1:
> 
> %%
> # unbuffer STDOUT
> sys.stdout = os.fdopen(sys.stdout.fileno(), 'w', 0)
> 
> # process input, line-by-line, and print responses after parsing input
> while 1:
>   rval = parse(raw_input())
>   if rval == None:
> print('foo')
>   else:
> print('bar')
> %%
> 
> This works, but while reading the documentation, I thought of using 'for
> line in fileinput.input()' in lieu of 'while 1:' construct.  This does
> not work when debugging the program on the command line -- the script
> appears to just hang no matter what is typed into STDIN.  I believe this
> is because of some internal buffering when using fileinput.  Is there a
> recommended way to disable such buffering?  Am I taking a totally wrong
> approach?
> 
> Feel free to just link me to previous discussions on the topic(s) if I
> have missed them.  Please be gentle with your cluebats. :-)

A quick look into fileinput.py reveals that it uses readlines() and slurps 
in the complete "file". I'm not sure that was a clever design decision...

Here's a partial reimplementation that should work for your use-case:

import sys
from itertools import chain

def _open_many(files):
for file in files:
if file == "-":
yield sys.stdin
else:
with open(file) as f:
yield f

def input(files=None, buffered=False):
if files is None:
files = sys.argv[1:]
if not files:
files = ["-"]
files = _open_many(files)
if not buffered:
files = (iter(file.readline, "") for file in files)
return chain.from_iterable(files)

for line in input():
print line.strip().upper()


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