Tuesday, March 16, 2010

comp.lang.python - 24 new messages in 9 topics - digest

comp.lang.python
http://groups.google.com/group/comp.lang.python?hl=en

comp.lang.python@googlegroups.com

Today's topics:

* How to add a library path to pythonpath ? - 3 messages, 3 authors
http://groups.google.com/group/comp.lang.python/t/2307dae70d0e5aaf?hl=en
* C-API PyObject_Call - 6 messages, 3 authors
http://groups.google.com/group/comp.lang.python/t/03c70fd75579881f?hl=en
* equivalent of Ruby's Pathname? - 2 messages, 2 authors
http://groups.google.com/group/comp.lang.python/t/f580fb3763208425?hl=en
* Python bindings tutorial - 3 messages, 3 authors
http://groups.google.com/group/comp.lang.python/t/73dcf6c0b97465aa?hl=en
* Searching for most pythonic/least stupid way to do something simple - 3
messages, 2 authors
http://groups.google.com/group/comp.lang.python/t/428d06aafb5d0184?hl=en
* Dynamic Class Creation - 2 messages, 2 authors
http://groups.google.com/group/comp.lang.python/t/2bd3e29c06ee0899?hl=en
* passing a socket to a subprocess in windows - 1 messages, 1 author
http://groups.google.com/group/comp.lang.python/t/54d432395cc76fa1?hl=en
* datetime string conversion error - 3 messages, 2 authors
http://groups.google.com/group/comp.lang.python/t/2944ba75fca9d570?hl=en
* to pass self or not to pass self - 1 messages, 1 author
http://groups.google.com/group/comp.lang.python/t/02e5000548314c86?hl=en

==============================================================================
TOPIC: How to add a library path to pythonpath ?
http://groups.google.com/group/comp.lang.python/t/2307dae70d0e5aaf?hl=en
==============================================================================

== 1 of 3 ==
Date: Tues, Mar 16 2010 8:56 am
From: Christian Heimes


Steve Holden schrieb:
> Barak, Ron wrote:
>> Hi,
>>
>> I'm trying to add a library path to my pythonpath, but seems it is not
>> accepted -
>>
>> On Windows DOS window:
>>
>> C:\>echo %PYTHONPATH%
>> c:\views\cc_view\TS_svm_ts_tool\SVMInspector\lib\
>>
> That looks like it should work. The only thing I notice is that I don't
> have a trailing backslash on my Windows PYTHONPATH. Could that be the
> problem?

Yes, that's definitely a major issue on Windows. Windows' stat() method
returns an error for paths with a trailing (back)slash. For example
_stat("C:\\Windows") works but _stat("C:\\Windows\\") sets errno to
ENOENT or ENOTDIR. I got bitten by the issue a couple of years ago as I
worked on pars of Python's import system.

Quoting Tim Peters: [1]

The Microsoft stat() function is extremely picky about
trailing (back)slashes. For example, if you have a
directory c:/python, and pass "c:/python/" to the MS stat
(), it claims no such thing exists. This isn't documented
by MS, but that's how it works: a trailing (back)slash is
required if and only if the path passed in "is a root". So
MS stat() doesn't understand "/python/", and doesn't
understand "d:" either. The former doesn't tolerate a
(back)slash, while the latter requires one.


Christian

[1] http://mail.python.org/pipermail/python-bugs-list/2002-April/011099.html


== 2 of 3 ==
Date: Tues, Mar 16 2010 9:23 am
From: Dave Angel


Barak, Ron wrote:
>
>> -----Original Message-----
>> From: Dave Angel [mailto:davea@ieee.org]
>> Sent: Tuesday, March 16, 2010 5:04 PM
>> To: Barak, Ron
>> Cc: Pablo Recio Quijano; python-list@python.org
>> Subject: RE: How to add a library path to pythonpath ?
>>
>>
>>
>> Barak, Ron wrote:
>>
>>> Thanks for the suggestion Pable.
>>>
>>> However, I really need the $PYTHONPATH to include this
>>>
>> additional library, so all Python scripts could use it.
>>
>>> In Windows I have defined PYTHONPATH as
>>> c:\views\cc_view\TS_svm_ts_tool\SVMInspector\lib\, and also in the
>>> Windows registry I have
>>>
>>>
>>>
>> HKEY_LOCAL_MACHINE\SOFTWARE\Python\PythonCore\version\PythonPath\ as
>>
>> C:\Python26\Lib;C:\Python26\DLLs;C:\views\cc_view\TS_svm_ts_tool\SVMIn
>>
>>> spector\lib\;C:\Python26\Lib\lib-tk;
>>>
>>> However, even with all the above, the SVMInspecor modules
>>>
>> are not found.
>>
>>>
>> See my response at the end. Top-posting makes for a very
>> confusing thread.
>>
>>> <snip>
>>>
>>> 2010/3/16 Barak, Ron <Ron.Barak@lsi.com<mailto:Ron.Barak@lsi.com>>
>>>
>>> Hi,
>>>
>>> I'm trying to add a library path to my pythonpath, but
>>>
>> seems it is not
>>
>>> accepted -
>>>
>>> On Windows DOS window:
>>>
>>> C:\>echo %PYTHONPATH%
>>> c:\views\cc_view\TS_svm_ts_tool\SVMInspector\lib\
>>>
>>> C:\>python -c "import sys ; print sys.path"
>>> ['', 'c:\\views\\cc_view\\TS_svm_ts_tool\\SVMInspector\\lib',
>>> 'C:\\WINDOWS\\syst em32\\python26.zip', 'C:\\Python26\\DLLs',
>>> 'C:\\Python26\\lib', 'C:\\Python26\\l ib\\plat-win',
>>> 'C:\\Python26\\lib\\lib-tk', 'C:\\Python26', 'C:\\Python26\\lib\\
>>> site-packages',
>>> 'C:\\Python26\\lib\\site-packages\\wx-2.8-msw-unicode']
>>>
>>> C:\>
>>>
>>> On Windows cygwin:
>>>
>>> $ echo $PYTHONPATH
>>> .:/cygdrive/c/views/cc_view/TS_svm_ts_tool/SVMInspector/lib/
>>>
>>> $ python -c "import sys ; print sys.path"
>>> ['',
>>>
>>>
>> '.:\\cygdrive\\c\\views\\cc_view\\TS_svm_ts_tool\\SVMInspector\\lib',
>>
>>> 'C:\\WINDOWS\\system32\\python26.zip', 'c:\\Python26\\DLLs',
>>> 'c:\\Python26\\lib', 'c:\\Python26\\lib\\plat-win',
>>> 'c:\\Python26\\lib\\lib-tk', 'c:\\Python26',
>>> 'c:\\Python26\\lib\\site-packages',
>>> 'c:\\Python26\\lib\\site-packages\\wx-2.8-msw-unicode']
>>>
>>> $
>>>
>>> What am I doing wrong ?
>>>
>>> Thanks,
>>> Ron.
>>>
>>>
>>>
>>>
>> In your original message, what you displayed worked exactly
>> as expected. As you didn't say what made you think something
>> was wrong, I ignored the message.
>>
>> Now, you say that some modules are not found. So how about
>> giving some more specifics so someone can actually debug the
>> problem. Pick a particular module, tell us the complete path
>> to that module, and show us both the sys.path strings (which
>> look correct for the Windows case, and of course bogus for
>> the cygwin case), as well as the offending import statement
>> and its traceback.
>>
>> DaveA
>>
>
> Hi Dave,
>
> My Python is the Windows Python (I need this for wxPython):
>
> $ ls -ls `which python`
> 1 lrwxrwxrwx 1 rbarak mkgroup-l-d 31 Mar 4 14:02 /usr/bin/python -> /cygdrive/c/Python26/python.exe
>
> Here's a demo script:
>
> #!/usr/bin/env python
>
> import sys
> print "\n",sys.path,"\n"
>
> from ErrorManager.ErrorManager import ErrorManager
>
> And it's run produces:
>
> $ python -u tmp.py
>
> ['c:\\views\\cc_view\\TS_svm_ts_tool\\svm_ts_tool', '.:\\cygdrive\\c\\views\\cc_view\\TS_svm_ts_tool\\SVMInspector\\lib', 'C:\\WINDOWS\\system32\\python26.zip', 'c:\\Python26\\DLLs', 'c:\\Python26\\lib', 'c:\\Python26\\lib\\plat-win', 'c:\\Python26\\lib\\lib-tk', 'c:\\Python26', 'c:\\Python26\\lib\\site-packages', 'c:\\Python26\\lib\\site-packages\\wx-2.8-msw-unicode']
>
> Traceback (most recent call last):
> File "tmp.py", line 6, in <module>
> from ErrorManager.ErrorManager import ErrorManager
> ImportError: No module named ErrorManager.ErrorManager
>
> $ ls -ls /cygdrive/c/views/cc_view/TS_svm_ts_tool/SVMInspector/lib/ErrorManager/ErrorManager.py
> 4 -rwxr-xr-x 1 rbarak ???????? 1843 Sep 21 19:16 /cygdrive/c/views/cc_view/TS_svm_ts_tool/SVMInspector/lib/ErrorManager/ErrorManager.py
>
> $ grep ErrorManager /cygdrive/c/views/cc_view/TS_svm_ts_tool/SVMInspector/lib/
> ErrorManager/ErrorManager.py
> class ErrorManager:
>
> $ echo $PYTHONPATH
> .:/cygdrive/c/views/cc_view/TS_svm_ts_tool/SVMInspector/lib/
>
> Please let me know if the above is enough, or you'd like to see the results from a Windows' DOS window.
>
> Thanks,
> Ron.
>
>
Well, I pointed out that your cygwin path is bogus, so of course it
won't work. One of those entries is

'.:\\cygdrive\\c\\views\\cc_view\\TS_svm_ts_tool\\SVMInspector\\lib'

I use Linux, and I use Windows. But cygwin's glomming together has
never made sense; some things are Unix-like, and others are
Windows-like. So anything you do in that environment is beyond me.

If I had to guess, though I'd say you wanted to drop those first two
characters from your PYTHONPATH. If you really wanted two directories
there, you should be separating them with semicolon (Windows rules),
rather than colon (Linux rules). First thing I'd do to test my theories
is to print something like:
print sys.path[1]
print os.listdir(sys.path[1])
Does Python even see the cygwin view of the world ?

You also don't show the location of the module you're trying to import.
I'm guessing that it's at ...SVMInspector/lib/ErrorManager/ErrorManager.py
Do you have a file ...SVMInspector/lib/ErrorManager/__init__.py ?

I would seriously recommend against naming your package, your module,
and your class all the same thing. Makes debugging very hard. At least
make them have different case combinations.

DaveA

== 3 of 3 ==
Date: Tues, Mar 16 2010 9:56 am
From: Steve Holden


Dave Angel wrote:
> Barak, Ron wrote:
>>
>>> -----Original Message-----
>>> From: Dave Angel [mailto:davea@ieee.org]
>>> Sent: Tuesday, March 16, 2010 5:04 PM
>>> To: Barak, Ron
>>> Cc: Pablo Recio Quijano; python-list@python.org
>>> Subject: RE: How to add a library path to pythonpath ?
>>>
>>>
>>>
>>> Barak, Ron wrote:
>>>
>>>> Thanks for the suggestion Pable.
>>>>
>>>> However, I really need the $PYTHONPATH to include this
>>>>
>>> additional library, so all Python scripts could use it.
>>>
>>>> In Windows I have defined PYTHONPATH as
>>>> c:\views\cc_view\TS_svm_ts_tool\SVMInspector\lib\, and also in the
>>>> Windows registry I have
>>>>
>>>>
>>>>
>>> HKEY_LOCAL_MACHINE\SOFTWARE\Python\PythonCore\version\PythonPath\ as
>>>
>>> C:\Python26\Lib;C:\Python26\DLLs;C:\views\cc_view\TS_svm_ts_tool\SVMIn
>>>
>>>> spector\lib\;C:\Python26\Lib\lib-tk;
>>>>
>>>> However, even with all the above, the SVMInspecor modules
>>>>
>>> are not found.
>>>
>>>>
>>> See my response at the end. Top-posting makes for a very
>>> confusing thread.
>>>
>>>> <snip>
>>>>
>>>> 2010/3/16 Barak, Ron <Ron.Barak@lsi.com<mailto:Ron.Barak@lsi.com>>
>>>>
>>>> Hi,
>>>>
>>>> I'm trying to add a library path to my pythonpath, but
>>>>
>>> seems it is not
>>>
>>>> accepted -
>>>>
>>>> On Windows DOS window:
>>>>
>>>> C:\>echo %PYTHONPATH%
>>>> c:\views\cc_view\TS_svm_ts_tool\SVMInspector\lib\
>>>>
>>>> C:\>python -c "import sys ; print sys.path"
>>>> ['', 'c:\\views\\cc_view\\TS_svm_ts_tool\\SVMInspector\\lib',
>>>> 'C:\\WINDOWS\\syst em32\\python26.zip', 'C:\\Python26\\DLLs',
>>>> 'C:\\Python26\\lib', 'C:\\Python26\\l ib\\plat-win',
>>>> 'C:\\Python26\\lib\\lib-tk', 'C:\\Python26', 'C:\\Python26\\lib\\
>>>> site-packages',
>>>> 'C:\\Python26\\lib\\site-packages\\wx-2.8-msw-unicode']
>>>>
>>>> C:\>
>>>>
>>>> On Windows cygwin:
>>>>
>>>> $ echo $PYTHONPATH
>>>> .:/cygdrive/c/views/cc_view/TS_svm_ts_tool/SVMInspector/lib/
>>>>
>>>> $ python -c "import sys ; print sys.path"
>>>> ['',
>>>>
>>>>
>>> '.:\\cygdrive\\c\\views\\cc_view\\TS_svm_ts_tool\\SVMInspector\\lib',
>>>
>>>> 'C:\\WINDOWS\\system32\\python26.zip', 'c:\\Python26\\DLLs',
>>>> 'c:\\Python26\\lib', 'c:\\Python26\\lib\\plat-win',
>>>> 'c:\\Python26\\lib\\lib-tk', 'c:\\Python26',
>>>> 'c:\\Python26\\lib\\site-packages',
>>>> 'c:\\Python26\\lib\\site-packages\\wx-2.8-msw-unicode']
>>>>
>>>> $
>>>>
>>>> What am I doing wrong ?
>>>>
>>>> Thanks,
>>>> Ron.
>>>>
>>>>
>>>>
>>>>
>>> In your original message, what you displayed worked exactly
>>> as expected. As you didn't say what made you think something
>>> was wrong, I ignored the message.
>>>
>>> Now, you say that some modules are not found. So how about
>>> giving some more specifics so someone can actually debug the
>>> problem. Pick a particular module, tell us the complete path
>>> to that module, and show us both the sys.path strings (which
>>> look correct for the Windows case, and of course bogus for
>>> the cygwin case), as well as the offending import statement
>>> and its traceback.
>>>
>>> DaveA
>>>
>>
>> Hi Dave,
>>
>> My Python is the Windows Python (I need this for wxPython):
>>
>> $ ls -ls `which python`
>> 1 lrwxrwxrwx 1 rbarak mkgroup-l-d 31 Mar 4 14:02 /usr/bin/python ->
>> /cygdrive/c/Python26/python.exe
>>
>> Here's a demo script:
>>
>> #!/usr/bin/env python
>>
>> import sys
>> print "\n",sys.path,"\n"
>>
>> from ErrorManager.ErrorManager import ErrorManager
>>
>> And it's run produces:
>>
>> $ python -u tmp.py
>>
>> ['c:\\views\\cc_view\\TS_svm_ts_tool\\svm_ts_tool',
>> '.:\\cygdrive\\c\\views\\cc_view\\TS_svm_ts_tool\\SVMInspector\\lib',
>> 'C:\\WINDOWS\\system32\\python26.zip', 'c:\\Python26\\DLLs',
>> 'c:\\Python26\\lib', 'c:\\Python26\\lib\\plat-win',
>> 'c:\\Python26\\lib\\lib-tk', 'c:\\Python26',
>> 'c:\\Python26\\lib\\site-packages',
>> 'c:\\Python26\\lib\\site-packages\\wx-2.8-msw-unicode']
>>
>> Traceback (most recent call last):
>> File "tmp.py", line 6, in <module>
>> from ErrorManager.ErrorManager import ErrorManager
>> ImportError: No module named ErrorManager.ErrorManager
>>
>> $ ls -ls
>> /cygdrive/c/views/cc_view/TS_svm_ts_tool/SVMInspector/lib/ErrorManager/ErrorManager.py
>>
>> 4 -rwxr-xr-x 1 rbarak ???????? 1843 Sep 21 19:16
>> /cygdrive/c/views/cc_view/TS_svm_ts_tool/SVMInspector/lib/ErrorManager/ErrorManager.py
>>
>>
>> $ grep ErrorManager
>> /cygdrive/c/views/cc_view/TS_svm_ts_tool/SVMInspector/lib/
>> ErrorManager/ErrorManager.py
>> class ErrorManager:
>>
>> $ echo $PYTHONPATH
>> .:/cygdrive/c/views/cc_view/TS_svm_ts_tool/SVMInspector/lib/
>>
>> Please let me know if the above is enough, or you'd like to see the
>> results from a Windows' DOS window.
>>
>> Thanks,
>> Ron.
>>
>>
> Well, I pointed out that your cygwin path is bogus, so of course it
> won't work. One of those entries is
>
> '.:\\cygdrive\\c\\views\\cc_view\\TS_svm_ts_tool\\SVMInspector\\lib'
>
> I use Linux, and I use Windows. But cygwin's glomming together has
> never made sense; some things are Unix-like, and others are
> Windows-like. So anything you do in that environment is beyond me.
>
> If I had to guess, though I'd say you wanted to drop those first two
> characters from your PYTHONPATH. If you really wanted two directories
> there, you should be separating them with semicolon (Windows rules),
> rather than colon (Linux rules). First thing I'd do to test my theories
> is to print something like:
> print sys.path[1]
> print os.listdir(sys.path[1])
> Does Python even see the cygwin view of the world ?
>
> You also don't show the location of the module you're trying to import.
> I'm guessing that it's at ...SVMInspector/lib/ErrorManager/ErrorManager.py
> Do you have a file ...SVMInspector/lib/ErrorManager/__init__.py ?
>
> I would seriously recommend against naming your package, your module,
> and your class all the same thing. Makes debugging very hard. At least
> make them have different case combinations.
>
In point of fact the mistake that's happening here is running the
Windows Python under Cygwin and expecting it to make sense.

Just because you run the Windows Python interpreter under Windows
doesn't make it magically capable of understanding a Cygwin path
specification.

We have yet to hear why the Cygwin Python isn't acceptable (though one
reason might be the unavailability of 2.6).

regards
Steve
--
Steve Holden +1 571 484 6266 +1 800 494 3119
See PyCon Talks from Atlanta 2010 http://pycon.blip.tv/
Holden Web LLC http://www.holdenweb.com/
UPCOMING EVENTS: http://holdenweb.eventbrite.com/


==============================================================================
TOPIC: C-API PyObject_Call
http://groups.google.com/group/comp.lang.python/t/03c70fd75579881f?hl=en
==============================================================================

== 1 of 6 ==
Date: Tues, Mar 16 2010 9:08 am
From: moerchendiser2k3


But the stack is empty after PyObject_Call, isnt it?


== 2 of 6 ==
Date: Tues, Mar 16 2010 9:17 am
From: Stefan Behnel


moerchendiser2k3, 16.03.2010 17:08:
> But the stack is empty after PyObject_Call, isnt it?

I think Steve was expecting that you wanted to debug into your program,
step into the call, and find the line yourself.

Stefan

== 3 of 6 ==
Date: Tues, Mar 16 2010 9:18 am
From: Stefan Behnel


moerchendiser2k3, 16.03.2010 12:52:
> i have a serious problem and I am looking for a solution. I pass an
> instance of a class from a file to PyObject_Call. When something
> fails, I get a full traceback. If it succeeds, I get the return value.
>
> Is it possible to get information from which line/file the return
> value of PyObject_Call is?

Could you explain what you want to do with this information and in what
cases you need it? Do you want to extract the information programmatically
at runtime?

Stefan

== 4 of 6 ==
Date: Tues, Mar 16 2010 9:41 am
From: moerchendiser2k3


In one case I have to check the return value of PyObject_Call, and if
its not of the correct return value,
I throw an exception, but I just get a simple output:

TypeError: Expected an instance of XYZ, no int.

instead of

Traceback (most called...)
TypeError: in line 3, file test.py: expected an instance of XYZ, no
int...


== 5 of 6 ==
Date: Tues, Mar 16 2010 10:14 am
From: Steve Holden


moerchendiser2k3 wrote:
> In one case I have to check the return value of PyObject_Call, and if
> its not of the correct return value,
> I throw an exception, but I just get a simple output:
>
> TypeError: Expected an instance of XYZ, no int.
>
> instead of
>
> Traceback (most called...)
> TypeError: in line 3, file test.py: expected an instance of XYZ, no
> int...

Could we perhaps see a little bit more of the code? Are you throwing the
exception from within your C code or from the Python calling environment
with a raise statement?

regards
Steve
--
Steve Holden +1 571 484 6266 +1 800 494 3119
See PyCon Talks from Atlanta 2010 http://pycon.blip.tv/
Holden Web LLC http://www.holdenweb.com/
UPCOMING EVENTS: http://holdenweb.eventbrite.com/

== 6 of 6 ==
Date: Tues, Mar 16 2010 11:25 am
From: moerchendiser2k3


Hi, currently I am not at home, I will post some stuff when I am back.
Just the note: I throw an exception with the C API.

Looks like that


PyObject *result = PyObject_Call(my_isntance, "", NULL);
if(result==NULL)
{
PyErr_Print(); //when this happens, the traceback is correct with
information about the file/line
return;
}

if(!PyXYZ_Check(result))
{
PyErr_SetString(PyExc_TypeError, "Wrong type, ....");
PyErr_Print(); //missing information of the file/line.
return;
}

Well, I expect, that there are no information about the line/file, so
I know something is missing, but what is missing?

Bye, moerchendiser2k3

==============================================================================
TOPIC: equivalent of Ruby's Pathname?
http://groups.google.com/group/comp.lang.python/t/f580fb3763208425?hl=en
==============================================================================

== 1 of 2 ==
Date: Tues, Mar 16 2010 9:12 am
From: Phlip


Chris Rebert wrote:

> The next closest thing would probably be the Python Cookbook:http://code.activestate.com/recipes/langs/python/

One thing I really like about ... my hacked version of path.py ... is
path.cd( lambda: ... ). It works great inside fabfile.py to
temporarily switch to a different folder:

sample_project = path('sample_project').abspath()

def run():
sample_project.cd( lambda:
_sh('python manage.py runserver 0.0.0.0:8000 --
settings=test_settings') )

After the lambda runs, we exception-safely return to the home folder.

(BTW I'm aware that a fabfile.py command with only one statement will
return to its shell and remain in the correct folder. It's just ...
the thought!)

This be .cd():

class path:

def cd(self, block=None):
previous = path(os.path.curdir).abspath()
self.chdir()

if block:
try: block()
finally: previous.chdir()

That's based on Jason Orendoff's work at http://www.jorendorff.com/articles/python/path

--
Phlip
http://c2.com/cgi/wiki?ZeekLand


== 2 of 2 ==
Date: Tues, Mar 16 2010 10:00 am
From: Steve Holden


Phlip wrote:
> Chris Rebert wrote:
>
>> The next closest thing would probably be the Python Cookbook:http://code.activestate.com/recipes/langs/python/
>
> One thing I really like about ... my hacked version of path.py ... is
> path.cd( lambda: ... ). It works great inside fabfile.py to
> temporarily switch to a different folder:
>
> sample_project = path('sample_project').abspath()
>
> def run():
> sample_project.cd( lambda:
> _sh('python manage.py runserver 0.0.0.0:8000 --
> settings=test_settings') )
>
> After the lambda runs, we exception-safely return to the home folder.
>
> (BTW I'm aware that a fabfile.py command with only one statement will
> return to its shell and remain in the correct folder. It's just ...
> the thought!)
>
> This be .cd():
>
> class path:
>
> def cd(self, block=None):
> previous = path(os.path.curdir).abspath()
> self.chdir()
>
> if block:
> try: block()
> finally: previous.chdir()
>
> That's based on Jason Orendoff's work at http://www.jorendorff.com/articles/python/path
>
Wouldn't this be better written as a context manager?

Be aware also that this won't work well in a multi-threaded environment
(assuming os.path.chdir is ultimately used to change directories)
because it effects the process's (globaL current directory.

regards
Steve
--
Steve Holden +1 571 484 6266 +1 800 494 3119
See PyCon Talks from Atlanta 2010 http://pycon.blip.tv/
Holden Web LLC http://www.holdenweb.com/
UPCOMING EVENTS: http://holdenweb.eventbrite.com/


==============================================================================
TOPIC: Python bindings tutorial
http://groups.google.com/group/comp.lang.python/t/73dcf6c0b97465aa?hl=en
==============================================================================

== 1 of 3 ==
Date: Tues, Mar 16 2010 9:20 am
From: Johny


Is there any tutorial how to write a bindings for a exe ( dos)
program?
I would like to run it from a Python directly
( using import command and a particular function from the binding)
not using os.system command.
Thanks
L.


== 2 of 3 ==
Date: Tues, Mar 16 2010 11:13 am
From: Joaquin Abian


On Mar 16, 5:20 pm, Johny <pyt...@hope.cz> wrote:
> Is there any tutorial how to write a bindings for a exe ( dos)
> program?
> I would like to run it from a Python directly
> ( using import command and a particular function from the binding)
>  not using os.system command.
> Thanks
> L.

subprocess ?


== 3 of 3 ==
Date: Tues, Mar 16 2010 12:12 pm
From: "Gabriel Genellina"


En Tue, 16 Mar 2010 13:20:40 -0300, Johny <python@hope.cz> escribi�:

> Is there any tutorial how to write a bindings for a exe ( dos)
> program?
> I would like to run it from a Python directly
> ( using import command and a particular function from the binding)
> not using os.system command.

Do you mean that you want to execute a particular function in the .exe
program?
That's not possible (ok, you *could* do that if you work hard enough, but
that's not how things are usually done)

--
Gabriel Genellina


==============================================================================
TOPIC: Searching for most pythonic/least stupid way to do something simple
http://groups.google.com/group/comp.lang.python/t/428d06aafb5d0184?hl=en
==============================================================================

== 1 of 3 ==
Date: Tues, Mar 16 2010 9:31 am
From: david jensen


Thanks Paul, but i don't immediately see how that helps (maybe I'm
just being dense)... nn's solution, though i initially thought it
worked, actually has a similar problem:

intended:
>>> print getOutcomes([3,4,5,5])
[[6, 1, 5, 5], [4.5, 2.5, 5, 5], [3, 4, 5, 5], [1.5, 5.5, 5, 5], [0,
7, 5, 5]]
>>> print getOutcomes([4,3,5,5])
[[7, 0, 5, 5], [5.5, 1.5, 5, 5], [4, 3, 5, 5], [2.5, 4.5, 5, 5], [1,
6, 5, 5]]

nn's solution:
>>> print getOutcomesNN([4,3,5,5])
[[6.0, 1.0, 5, 5], [4.5, 2.5, 5, 5], [3.0, 4.0, 5, 5], [1.5, 5.5, 5,
5], [0.0, 7.0, 5, 5]]


it's obvious why this is happening (the list comprehension assumes
that index 0 is, after all, index 0), but not immediately obvious how
to fix it, except by going with gerard's solution or doing what i did
in the beginning: flip them if they're in the wrong order, remember,
and flip them again at the end.

dmj


== 2 of 3 ==
Date: Tues, Mar 16 2010 9:54 am
From: david jensen


of course, changing nn's to:
def getOutcomes(myList=[2,5,8,3,5]):
low_id = int(myList[0]>myList[1])
amountToShare = 2*myList[low_id]
remainder = myList[not low_id]-myList[low_id]
tail=list(myList[2:])
outcomes = [[amountToShare*perc, remainder+amountToShare*(1-perc)]+
tail for perc in (1.0, 0.75, 0.5, 0.25, 0.0)] if not low_id else
[[remainder+amountToShare*perc, amountToShare*(1-perc)]+ tail for perc
in (1.0, 0.75, 0.5, 0.25, 0.0)]
return outcomes


works, just hides the ugliness in a more compact form


== 3 of 3 ==
Date: Tues, Mar 16 2010 12:07 pm
From: Michael Torrie


david jensen wrote:
> of course, changing nn's to:
> def getOutcomes(myList=[2,5,8,3,5]):
> low_id = int(myList[0]>myList[1])
> amountToShare = 2*myList[low_id]
> remainder = myList[not low_id]-myList[low_id]
> tail=list(myList[2:])
> outcomes = [[amountToShare*perc, remainder+amountToShare*(1-perc)]+
> tail for perc in (1.0, 0.75, 0.5, 0.25, 0.0)] if not low_id else
> [[remainder+amountToShare*perc, amountToShare*(1-perc)]+ tail for perc
> in (1.0, 0.75, 0.5, 0.25, 0.0)]
> return outcomes
>
>
> works, just hides the ugliness in a more compact form

If Gerard's code works, I would consider it far superior to your code
here. Pythonic does not necessarily mean short and ugly, nor does it
mean that you have to always use list comprehensions. Having a
readable algorithm that's easy to follow in the future is a far better
way than trying to use python's cool features to compact the code to as
small and unreadable section as possible.

I used to use list comprehension all the time, but I've found that often
an explicit for loop is a much better solution in terms of
maintainability. Especially when you start seeing nested comprehensions
such as you have here.


==============================================================================
TOPIC: Dynamic Class Creation
http://groups.google.com/group/comp.lang.python/t/2bd3e29c06ee0899?hl=en
==============================================================================

== 1 of 2 ==
Date: Tues, Mar 16 2010 9:49 am
From: Jack Diederich


On Tue, Mar 16, 2010 at 2:18 AM, Chris Rebert <clp2@rebertia.com> wrote:
> On Mon, Mar 15, 2010 at 11:01 PM, Josh English
> <joshua.r.english@gmail.com> wrote:
>> I have a large program with lots of data stored in XML. I'm upgrading
>> my GUI to use ObjectListView, but with my data in XML, I don't have
>> regular objects to interact with the OLV. I do have an XML validator
>> that defines the structure of the XML elements, and I'm trying to
>> dynamically create a class to wrap the XML element.
>>
>> So, an element like:
>>
>> <market code="WotF">
>> <title>Writers of the Future</title>
>> </market>
>>
>> I want to create a class like:
>>
>> class Market(object):
>>    def __init__(self, elem):
>>        self._elem = elem
>>
>>    def get_code(self):
>>        return self._elem.get('code')
>>
>>    def set_code(self, value):
>>        self._elem.set('code', value)
>>
>>    def get_title(self):
>>        return self._elem.find('title').text
>>
>>    def set_title(self, value):
>>        node = self._elem.find('title')
>>        node.text = value
>>
>> Naturally, I don't want to hand code this for every interface but
>> would like to create them dynamically. (The laziness of programming, I
>> guess.)
>>
>> What's the best way to create these helper methods?

You can either define a catch-all __getattr__ method to look them up
dynamically, or as Chris kinda-suggested write descriptors for the
individual elements.

class Market():
def __init__(self, elem):
self._elem = elem
def __getattr__(self, name):
try:
# I'm assuming this raises a KeyError when not found
return self._elem.get(name)
except KeyError:
return self._elem.find(name)
def __setitem__(self, name, value):
# like __getitem__ but for setting

Chris' property maker function is almost like a descriptor class (how
properties are implemented under the hood), here's a refactoring
[untested]

class ElemGetProperty():
def __init__(self, name):
self.name = name
def __get__(self, ob, cls):
return ob._elem.get(self.name)
def __set__(self, ob, val):
ob._elem.set(self.name, val)

You could write one property class for each kind of element (get/find)
and then put them in your class like this

class Market():
code = ElemGetProperty('code')
title = ElemFindProeprty('title')

The getattr/setattr method is easier to understand and will handle
arbitrary elements; for the descriptors version you'll have to define
one for each tag that might be used on the class.

-Jack


== 2 of 2 ==
Date: Tues, Mar 16 2010 9:53 am
From: Chris Rebert


On Tue, Mar 16, 2010 at 9:49 AM, Jack Diederich <jackdied@gmail.com> wrote:
> On Tue, Mar 16, 2010 at 2:18 AM, Chris Rebert <clp2@rebertia.com> wrote:
>> On Mon, Mar 15, 2010 at 11:01 PM, Josh English
>> <joshua.r.english@gmail.com> wrote:
<snip>
>>> What's the best way to create these helper methods?
>
> You can either define a catch-all __getattr__ method to look them up
> dynamically, or as Chris kinda-suggested write descriptors for the
> individual elements.
>
> class Market():
>  def __init__(self, elem):
>    self._elem = elem
>  def __getattr__(self, name):
>    try:
>      # I'm assuming this raises a KeyError when not found
>      return self._elem.get(name)
>    except KeyError:
>      return self._elem.find(name)
>  def __setitem__(self, name, value):

did you mean __setattr__ here?

<snip>
> The getattr/setattr method is easier to understand and will handle
> arbitrary elements;  for the descriptors version you'll have to define
> one for each tag that might be used on the class.

Cheers,
Chris
--
http://blog.rebertia.com

==============================================================================
TOPIC: passing a socket to a subprocess in windows
http://groups.google.com/group/comp.lang.python/t/54d432395cc76fa1?hl=en
==============================================================================

== 1 of 1 ==
Date: Tues, Mar 16 2010 10:10 am
From: Daniel Platz


Hello!

I have a problem with passing a socket to a subprocess in windows. It
works in Linux and for windows there is a workaround in the Python
doc. However, this workaround does not work. It was already noted by
other people and they Python issue tracker

http://bugs.python.org/issue5879
***************************************************************************************************************
the example from

http://docs.python.org/library/multiprocessing.html?highlight=multiprocessing#module-multiprocessing

named "
# Example where a pool of http servers share a single listening socket
#
"
does not work on windows.

Reason:

s = socket.fromfd(fd, family, type_, proto)

in line 156 of reduction.py

fails, because fromfd is not available on windows. Sad thing:
reduction.py was put into processing.py exactly to solve that problem
(i.e. reduction.py is provided as workaround for socket.fromfd not
available on windows, from the documentation:
if sys.platform == 'win32':
import multiprocessing.reduction
# make sockets pickable/inheritable


the solution within processing was:

try:
fromfd = socket.fromfd
except AttributeError:
def fromfd(fd, family, type, proto=0):
s = socket._socket.socket()
_processing.changeFd(s, fd, family, type, proto)
return s

but: _multiprocessing has no longer a method changeFd.

Harald
***********************************************************************************************************************

Has someone information about this or can help me to solve the
problem.

Thanks in advance

Daniel

==============================================================================
TOPIC: datetime string conversion error
http://groups.google.com/group/comp.lang.python/t/2944ba75fca9d570?hl=en
==============================================================================

== 1 of 3 ==
Date: Tues, Mar 16 2010 11:56 am
From: Jordan Apgar


Hey all,
I'm trying to convert a string to a date time object and all my fields
convert except for month which seems to default to january.

here's what I'm doing:
date = "2010-03-16 14:46:38.409137"
olddate = datetime.strptime(date,"%Y-%m-%j %H:%M:%S.%f")

print date
print olddate

I get:
2010-03-16 14:46:38.409137
2010-01-16 14:46:38.409137

notice the 01 in the second date from what I could tell everything is
formatted correctly.

thanks for the help.
~Jordan


== 2 of 3 ==
Date: Tues, Mar 16 2010 12:07 pm
From: Christian Heimes


Jordan Apgar wrote:
> Hey all,
> I'm trying to convert a string to a date time object and all my fields
> convert except for month which seems to default to january.
>
> here's what I'm doing:
> date = "2010-03-16 14:46:38.409137"
> olddate = datetime.strptime(date,"%Y-%m-%j %H:%M:%S.%f")
>
> print date
> print olddate
>
> I get:
> 2010-03-16 14:46:38.409137
> 2010-01-16 14:46:38.409137
>
> notice the 01 in the second date from what I could tell everything is
> formatted correctly.


%j is documtend as "Day of the year as a decimal number [001,366].". Did
you mean %d instead?

Christian

== 3 of 3 ==
Date: Tues, Mar 16 2010 12:11 pm
From: Jordan Apgar


On Mar 16, 3:07 pm, Christian Heimes <li...@cheimes.de> wrote:
> Jordan Apgar wrote:
> > Hey all,
> > I'm trying to convert a string to a date time object and all my fields
> > convert except for month which seems to default to january.
>
> > here's what I'm doing:
> > date = "2010-03-16 14:46:38.409137"
> >  olddate = datetime.strptime(date,"%Y-%m-%j %H:%M:%S.%f")
>
> > print date
> > print olddate
>
> > I get:
> > 2010-03-16 14:46:38.409137
> > 2010-01-16 14:46:38.409137
>
> > notice the 01 in the second date from what I could tell everything is
> > formatted correctly.
>
> %j is documtend as "Day of the year as a decimal number [001,366].". Did
> you mean %d instead?
>
> Christian

That fixed it, thank you


==============================================================================
TOPIC: to pass self or not to pass self
http://groups.google.com/group/comp.lang.python/t/02e5000548314c86?hl=en
==============================================================================

== 1 of 1 ==
Date: Tues, Mar 16 2010 11:59 am
From: Jason Tackaberry


On Tue, 2010-03-16 at 10:04 +0100, Bruno Desthuilliers wrote:
> Answer here:
>
> http://wiki.python.org/moin/FromFunctionToMethod

I have a sense I used to know this once upon a time, but the question
came to my mind (possibly again) and I couldn't think of an answer:

Why not create the bound methods at instantiation time, rather than
using the descriptor protocol which has the overhead of creating a new
bound method each time the method attribute is accessed?

Cheers,
Jason.


==============================================================================

You received this message because you are subscribed to the Google Groups "comp.lang.python"
group.

To post to this group, visit http://groups.google.com/group/comp.lang.python?hl=en

To unsubscribe from this group, send email to comp.lang.python+unsubscribe@googlegroups.com

To change the way you get mail from this group, visit:
http://groups.google.com/group/comp.lang.python/subscribe?hl=en

To report abuse, send email explaining the problem to abuse@googlegroups.com

==============================================================================
Google Groups: http://groups.google.com/?hl=en

0 Comments:

Post a Comment

Subscribe to Post Comments [Atom]

<< Home


Real Estate