Feature Proposal: Sequence .join method

2005-09-29 Thread David Murmann
Hi all!

I could not find out whether this has been proposed before (there are 
too many discussion on join as a sequence method with different 
semantics). So, i propose a generalized .join method on all sequences 
with these semantics:

def join(self, seq):
 T = type(self)
 result = T()
 if len(seq):
 result = T(seq[0])
 for item in seq[1:]:
 result = result + self + T(item)
 return result

This would allow code like the following:

[0].join([[5], [42, 5], [1, 2, 3], [23]])

resulting in:
[5, 0, 42, 5, 0, 1, 2, 3, 0, 23]

You might have noticed that this contains actually two propsals, so if 
you don't like str.join applying str() on each item in the sequence 
replace the line
 result = result + self + T(item)
with
 result = result + self + item
My version has been turned down in the past as far as i read, yet, i 
find the first version more useful in the new context... you can pass a 
sequence of lists or tuples or really any sequence to the method and it 
does what you think (at least what i think :).

Any comments welcome,
David.
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Feature Proposal: Sequence .join method

2005-09-29 Thread David Murmann
David Murmann wrote:
> replace the line
> result = result + self + T(item)
> with
> result = result + self + item
and of course the line
 result = T(seq[0])
with
 result = seq[0]
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Feature Proposal: Sequence .join method

2005-09-29 Thread David Murmann
Steven Bethard wrote:
> David Murmann wrote:
>> Hi all!
>>
>> I could not find out whether this has been proposed before (there are 
>> too many discussion on join as a sequence method with different 
>> semantics). So, i propose a generalized .join method on all sequences 
>> with these semantics:
>>
>> def join(self, seq):
>> T = type(self)
>> result = T()
>> if len(seq):
>> result = T(seq[0])
>> for item in seq[1:]:
>> result = result + self + T(item)
>> return result
>>
>> This would allow code like the following:
>>
>> [0].join([[5], [42, 5], [1, 2, 3], [23]])
> 
> I don't like the idea of having to put this on all sequences.  If you 
> want this, I'd instead propose it as a function (perhaps builtin, 
> perhaps in some other module).
> 
> Also, this particular implementation is a bad idea.  The repeated += to 
> result is likely to result in O(N**2) behavior.
> 
> STeVe

Hi and thanks for the fast reply,

i just figured out that the following implementation is probably much 
faster, and short enough to be used in place for every of my use cases:

def join(sep, seq):
 return reduce(lambda x, y: x + sep + y, seq, type(sep)())

so, i'm withdrawing my proposal, and instead propose to keep reduce and 
lambda in py3k ;).

thanks again,
David.
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Feature Proposal: Sequence .join method

2005-09-29 Thread David Murmann
> def join(sep, seq):
> return reduce(lambda x, y: x + sep + y, seq, type(sep)())

damn, i wanted too much. Proper implementation:

def join(sep, seq):
 if len(seq):
 return reduce(lambda x, y: x + sep + y, seq)
 return type(sep)()

but still short enough

see you,
David.
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Feature Proposal: Sequence .join method

2005-09-30 Thread David Murmann
Michael Spencer wrote:
> Terry Reedy wrote:
>> "David Murmann" <[EMAIL PROTECTED]> wrote in message 
>> news:[EMAIL PROTECTED]
>>
>>>> def join(sep, seq):
>>>>return reduce(lambda x, y: x + sep + y, seq, type(sep)())
>>>
>>> damn, i wanted too much. Proper implementation:
>>>
>>> def join(sep, seq):
>>>if len(seq):
>>>return reduce(lambda x, y: x + sep + y, seq)
>>>return type(sep)()
>>>
>>> but still short enough
>>
>>
>> For general use, this is both too general and not general enough.
>>
>> If len(seq) exists then seq is probably reiterable, in which case it 
>> may be possible to determine the output length and preallocate to make 
>> the process O(n) instead of O(n**2).  I believe str.join does this.  A 
>> user written join for lists could also.  A tuple function could make a 
>> list first and then tuple(it) at the end.
>>
>> If seq is a general (non-empty) iterable, len(seq) may raise an 
>> exception even though the reduce would work fine.
>>
>> Terry J. Reedy
>>
>>
>>
> For the general iterable case, you could have something like this:
> 
>  >>> def interleave(sep, iterable):
>  ... it = iter(iterable)
>  ... next = it.next()
>  ... try:
>  ... while 1:
>  ... item = next
>  ... next = it.next()
>  ... yield item
>  ... yield sep
>  ... except StopIteration:
>  ... yield item
>  ...
>  >>> list(interleave(100,range(10)))
>  [0, 100, 1, 100, 2, 100, 3, 100, 4, 100, 5, 100, 6, 100, 7, 100, 8, 
> 100, 9]

Well, as [EMAIL PROTECTED] pointed out, there is already
itertools.chain which almost does this. In my opinion it could be useful
to add an optional keyword argument to it (like "connector" or "link"),
which is iterated between the other arguments.

> but I can't think of a use for it ;-)

Of course, i have a use case, but i don't know whether this is useful
enough to be added to the standard library. (Yet this would be a much
smaller change than changing all sequences ;)

thanks for all replies,
David.
-- 
http://mail.python.org/mailman/listinfo/python-list


add keyword argument to itertools.chain [was Feature Proposal: Sequence .join method]

2005-09-30 Thread David Murmann

Hi again,

i wrote a small patch that changes itertools.chain to take a "link"
keyword argument. If given, it is iterated between the normal arguments,
otherwise the behavior is unchanged.

I'd like to hear your opinion on both, the functionality and the actual
implementation (as this is one of the first things i ever wrote in C).

till then,
David.
Index: python/dist/src/Modules/itertoolsmodule.c
===
RCS file: /cvsroot/python/python/dist/src/Modules/itertoolsmodule.c,v
retrieving revision 1.41
diff -c -r1.41 itertoolsmodule.c
*** python/dist/src/Modules/itertoolsmodule.c   26 Aug 2005 06:42:30 -  
1.41
--- python/dist/src/Modules/itertoolsmodule.c   30 Sep 2005 22:28:38 -
***
*** 1561,1587 
int tuplesize = PySequence_Length(args);
int i;
PyObject *ittuple;
  
!   if (!_PyArg_NoKeywords("chain()", kwds))
!   return NULL;
  
/* obtain iterators */
assert(PyTuple_Check(args));
ittuple = PyTuple_New(tuplesize);
if(ittuple == NULL)
return NULL;
!   for (i=0; i < tuplesize; ++i) {
!   PyObject *item = PyTuple_GET_ITEM(args, i);
!   PyObject *it = PyObject_GetIter(item);
!   if (it == NULL) {
!   if (PyErr_ExceptionMatches(PyExc_TypeError))
!   PyErr_Format(PyExc_TypeError,
!   "chain argument #%d must support iteration",
!   i+1);
!   Py_DECREF(ittuple);
!   return NULL;
}
-   PyTuple_SET_ITEM(ittuple, i, it);
}
  
/* create chainobject structure */
--- 1561,1621 
int tuplesize = PySequence_Length(args);
int i;
PyObject *ittuple;
+   PyObject *link = NULL;
  
!   if (kwds != NULL && PyDict_Check(kwds)) {
!   link = PyDict_GetItemString(kwds, "link");
!   if (link != NULL)
!   /* create more space for the link iterators */
!   tuplesize = tuplesize*2-1;
!   }
  
/* obtain iterators */
assert(PyTuple_Check(args));
ittuple = PyTuple_New(tuplesize);
if(ittuple == NULL)
return NULL;
!   if (link == NULL) {
!   /* no keyword argument provided */
!   for (i=0; i < tuplesize; ++i) {
!   PyObject *item = PyTuple_GET_ITEM(args, i);
!   PyObject *it = PyObject_GetIter(item);
!   if (it == NULL) {
!   if (PyErr_ExceptionMatches(PyExc_TypeError))
!   PyErr_Format(PyExc_TypeError,
!   "chain argument #%d must 
support iteration",
!   i+1);
!   Py_DECREF(ittuple);
!   return NULL;
!   }
!   PyTuple_SET_ITEM(ittuple, i, it);
!   }
!   }
!   else {
!   for (i=0; i < tuplesize; ++i) {
!   PyObject *it = NULL;
!   if (i%2 == 0) {
!   PyObject *item = PyTuple_GET_ITEM(args, i/2);
!   it = PyObject_GetIter(item);
!   }
!   else {
!   it = PyObject_GetIter(link);
!   }
!   if (it == NULL) {
!   if (PyErr_ExceptionMatches(PyExc_TypeError)) {
!   if (i%2 == 0)
!   PyErr_Format(PyExc_TypeError,
!   "chain argument #%d must 
support iteration",
!   i/2+1);
!   else
!   PyErr_Format(PyExc_TypeError,
!   "chain keyword argument link 
must support iteration");
!   }
!   Py_DECREF(ittuple);
!   return NULL;
!   }
!   PyTuple_SET_ITEM(ittuple, i, it);
}
}
  
/* create chainobject structure */
-- 
http://mail.python.org/mailman/listinfo/python-list

Re: Statement orders

2005-10-02 Thread David Murmann
Fredrik Lundh wrote:
> Monu Agrawal wrote:
> 
>> Hi I am making a gui based tool. When user preses a perticular button I
>> am running a heavy command, before this I want to say user to wait with
>> a image showing infront of her.
>>
>> My code is like:
>>
>> def loadData(self):
>>top=Toplevel(self.parent)
>>top.focus_set()
>>self.parent.wm_title("Loading Data...")
> 
> +top.update() # flush the event queue
> 
>>os.system('a heavy command')
>>os.system('another heavy command)
>>top.destroy()
> 
> 

I had a very similar problem and adding an update call solved it, but
in some tk documentation i read that one should be careful with adding
update calls to callbacks and prefer update_idletasks. in my case the
update method is even called one time before the mainloop is entered,
yet everything seems to work fine, so exactly when is it dangerous to
call update? (or is it dangerous at all?)

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


Re: Statement orders

2005-10-02 Thread David Murmann
[EMAIL PROTECTED] wrote:
> Here's one case where it's bad to call update.
> 
>   def perform_longrunning_calculation():
>   time.sleep(1)
>   app.update()
>   time.sleep(1)
> 

would it be advisable to guard against this with something like this?

def perform_longrunning_calculation():
 if not app.busy:
 app.busy = 1
 time.sleep(1)
 app.update()
 time.sleep(1)
 app.busy = 0

or does this have flaws i'm not seeing?

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


Re: semi-newbie module namespace confusion

2005-10-04 Thread David Murmann
Fredrik Lundh wrote:
> running a piece of python code as a script isn't the same thing as
> importing it as a module:

I ran into the same problem some time ago and even wanted to post here
about it, but found out that it had been reported as a bug three times
at sourceforge (if i remember correctly). The comments there explained
it of course, but I still think that this behavior is somehow "wrong".
I like to think of the import statement as a way to provide the names
defined in a module to the current namespace, so there is no "this gets
evaluated twice".
Now i wonder how difficult it would be to "correct" the behavior? And
would such a change break any code?

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


Re: semi-newbie module namespace confusion

2005-10-04 Thread David Murmann
Fredrik Lundh wrote:
> David Murmann wrote:
> 
>> I ran into the same problem some time ago and even wanted to post here
>> about it, but found out that it had been reported as a bug three times
>> at sourceforge (if i remember correctly). The comments there explained
>> it of course, but I still think that this behavior is somehow "wrong".
>>
>> I like to think of the import statement as a way to provide the names
>> defined in a module to the current namespace, so there is no "this gets
>> evaluated twice".
> 
> are you sure you understand the problem?  import does exactly what you
> say; it creates a module object and populates it by running the code in the
> module.
maybe i don't... but i'm not convinced yet, see below.

> the problem is that when you hand Python a chunk of code (a script), it
> doesn't necessarily know where it came from.  and even if you know the
> filename it came from, there's no way to know if that chunk actually corre-
> sponds to a module somewhere out there (the import system can map a
> module name to a file, but it cannot map a file to a module name).
well, in my opinion python is not trying hard enough. to me it is
immediately obvious that the main module gets evaluated twice and
i am rather sure that one could introduce some magic of the kind
"oh, i reevaluate the main script here, the module does not get filled
the usual way but uses the existing objects instead". this would have
to happen on a very low level (when the file i just read from is known)
but i think it would be possible. whether the effort to do so is worth
it, is a different question...

>> Now i wonder how difficult it would be to "correct" the behavior?
> 
> there's no way to "fix" it without introducing a huge number of 
> inconsistencies.
> 
>> And would such a change break any code?
> 
> any code that uses the "if __name__ == "__main__" pydiom, for a start.
i wouldn't lose that, the main module should still be named "__main__",
it should just get more names (somehow) :)
i see, it's difficult at the moment

hoping i'm not completely wrong here, David.
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Python Challenge ahead [NEW] for riddle lovers

2005-04-29 Thread David Murmann
Shane Hathaway wrote:
That was pretty fun.  Good for a Friday.  Too bad it comes to an abrupt
"temporary end".
Shane
P.S. I hope I didn't hammer your server on step 3.  I was missing the
mark. :-)
Interestingly step 3 is actually wrong... there is an additional 
solution, which looks like cqqmsxk. (I don't think that spoils the fun :))
--
http://mail.python.org/mailman/listinfo/python-list


build curiosities of svn head (on WinXP)

2005-12-25 Thread David Murmann
hi all!

i just built revision 41809 under winxp using a rather uncommon
setup (at least i think so). since i have no visual studio here,
i only used freely available tools: cygwin to get the source, the
microsoft compiler/linker and NAnt (nant.sf.net) as the build tool
to interpret the .vcproj-files (which may be the uncommon part).

as far as i understand this setup is not supported in any way, but
i noticed two problems, that could be interesting for others as well.

first, the ProjectGUID's in "pythoncore.vcproj" and "pcbuild.sln"
mismatch, i fixed that locally by changing "pythoncore.vcproj"
(maybe visual studio is ignoring/shadowing this problem?).

second, the build order in "pcbuild.sln" for elementtree seems to be
wrong, nant tried to build elementtree before pythoncore (which failed).
i fixed this by building elementtree separately.

so my question is: will something like this be changed to support this
setup (or at least to get the project files "cleaner")? should i submit
a patch and see if it gets accepted?

apart from that everything went fine, and i could reproduce the expected
failure (ATM) of the regression test suite:

  http://mail.python.org/pipermail/python-dev/2005-December/059033.html

btw, if anyone is interested in the (rather small) build-script for
nant, just ask,

so long, David.
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: build curiosities of svn head (on WinXP)

2005-12-26 Thread David Murmann
Tim Peters schrieb:
> [David Murmann]
> ...
>>> second, the build order in "pcbuild.sln" for elementtree seems to be
>>> wrong, nant tried to build elementtree before pythoncore (which failed).
>>> i fixed this by building elementtree separately.
> 
> [Steve Holden]
>> Yes, the elementtree module is a new arrival for 3.5, so the build may
>> not yet be perfectly specified. This is useful information.
> 
> I just checked in a fix for that.  Thanks!

this works for me, thanks for the quick patch!

>>> ... and i could reproduce the expected failure (ATM) of the regression
>>> test suite:
>>>
>>>   http://mail.python.org/pipermail/python-dev/2005-December/059033.html
> 
> Note that all tests pass on Windows as of Sunday (in release and debug
> builds, with and without -O).

here i have problems. some tests fail more or less randomly.
after some testing, i found that it seems to be related to
the parsing of float literals in python code (wild guess).
for example, test_pow failed giving me this traceback:

Traceback (most recent call last):
  File "D:\develop\cygwin\usr\local\src\python\python\trunk\lib\test\test_pow.py
", line 109, in test_bug705231
eq(pow(a, 1.23e167), 1.0)
AssertionError: -1.0 != 1.0

this seems very strange to me, as i get this on the interactive
prompt:
>>> pow(-1.0, 1.23e167)
1.0

another example is test_colorsys. this test hangs, because it
uses "frange(0.0, 1.0, 0.2)" (which does, what it looks like),
but the 0.2 is somehow read as 0.0 resulting in an infinite
loop.

i have no clue whether these failures are related to my build
environment or something in the python codebase (or the weather?).
all tests passed when building with gcc from cygwin btw. i will
try building 2.4.2 with nant later and see what that does...

i am lost at the moment, David.

PS: maybe i should note: i built python without _bsddb and without
_tcl but i don't think this should be causing problems.
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: build curiosities of svn head (on WinXP)

2005-12-26 Thread David Murmann
Fredrik Lundh schrieb:
> try setting the locale (via the locale module) from the interactive prompt,
> and see if Python still handles floating point values correctly.
> 

well, it does not:

>>> import locale
>>> locale.setlocale(locale.LC_ALL, '')
'German_Germany.1252'
>>> 3.141592
3.0

so this is where the problem has to be, but i am still not sure what to do
about this. is this a problem with my configuration, with my build or
with python?

i tried changing my windows configuration to use '.' as the decimal point,
after which the above worked as expected, but the test suite still fails
on some tests (audioop, binop, bisect, difflib, doctest, xdrlib fail and
colorsys still hangs). surely my workaround is no general solution and
it doesn't even work, so there has to be a problem somewhere else.

also note that some of these tests (all except difflib and doctest) pass,
if i change them in any way (like adding insignificant whitespace) and run
them alone.

thanks for the help, David.
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: build curiosities of svn head (on WinXP)

2005-12-26 Thread David Murmann
David Murmann schrieb:
> i will try building 2.4.2 with nant later and see what that does...
> 

FYI i did this now and it worked fine, all tests passed.
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: itertools.izip brokeness

2006-01-03 Thread David Murmann
[EMAIL PROTECTED] schrieb:
> [izip() eats one line]

as far as i can see the current implementation cannot be changed
to do the Right Thing in your case. pythons iterators don't allow
to "look ahead", so izip can only get the next element. if this
fails for an iterator, everything up to that point is lost.

maybe the documentation for izip should note that the given
iterators are not necessarily in a sane state afterwards.

for your problem you can do something like:

def izipall(*args):
iters = [iter(it) for it in args]
while iters:
result = []
for it in iters:
try:
x = it.next()
except StopIteration:
iters.remove(it)
else:
result.append(x)
yield tuple(result)

note that this does not yield tuples that are always the same
length, so "for x, y in izipall()" won't work. instead, do something
like "for seq in izipall(): print '\t'.join(seq)".

hope i was clear enough, David.
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Is 'everything' a refrence or isn't it?

2006-01-05 Thread David Murmann
Claudio Grondi schrieb:
> Stuart D. Gathman wrote:
>> for (_idx = 0; _idx < NLST; ++_idx) {
>>   int *i = lst[_idx];
>>   if (*i == *_i2)
> ^-- I have trouble with this line. Is it as is should be? I suppose 
> it is not.
> 

i think he meant

  if (*i == _i2)

but i think python does

  if (i == &_i2)

because there is only one integer object holding the value 2, so
it is sufficient to compare the addresses (i'm not sure about this,
perhaps someone smarter can clarify?).

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


Re: Is 'everything' a refrence or isn't it?

2006-01-05 Thread David Murmann
Ich schrieb:
> but i think python does
> 
>  if (i == &_i2)
> 
> because there is only one integer object holding the value 2, so
> it is sufficient to compare the addresses (i'm not sure about this,
> perhaps someone smarter can clarify?).

well, as far as i can see the relevant function is 

static int
int_compare(PyIntObject *v, PyIntObject *w)
{
register long i = v->ob_ival;
register long j = w->ob_ival;
return (i < j) ? -1 : (i > j) ? 1 : 0;
}

in Objects/intobject.c, which does compare by value. so, this is
either special-cased elsewhere or not optimized (should/can it be?).

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


Re: Is 'everything' a refrence or isn't it?

2006-01-05 Thread David Murmann
Ich schrieb:
> well, as far as i can see the relevant function is
> in Objects/intobject.c, which does compare by value. so, this is
> either special-cased elsewhere or not optimized (should/can it be?).

it is special-cased, but still compares by value. the relevant
parts from "Python/ceval.c":

case COMPARE_OP:
  if (PyInt_CheckExact(w) && PyInt_CheckExact(v)) {
a = PyInt_AS_LONG(v);
b = PyInt_AS_LONG(w);
switch (oparg) {
  case PyCmp_EQ: res = a == b; break;
}
  }

sorry if i am being noisy.

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


Re: Is 'everything' a refrence or isn't it?

2006-01-05 Thread David Murmann
Dan Sommers schrieb:
> int **_idx;
> for( _idx = lst; _idx < lst + NLST; ++_idx ) {
> int *i;
> i = *_idx;
> 
> /* compare "the item to which i is bound" to "a constant" */
> if( *i == *(&_i2) )
> /* rebind i to _i4 */
> i = &_i4;
> }
> 
> for( _idx = lst; _idx < lst + NLST; ++_idx ) {
> int *i = *_idx;
> printf( "%d\n", *i );
> }

i don't see what is so fundamentally different in this version compared
to the one from Stuart Gathman (except it works, but i think it was a typo).
do you want to stress that python uses an iterator (if so, i would have
renamed _idx to _iter, because it ain't not index anymore)? whats the point
of not starting the for loop at 0? and is there a difference between
(*i == _idx) and (*i == *(&_idx))?

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


Re: build curiosities of svn head (on WinXP)

2006-01-09 Thread David Murmann
Paul Moore schrieb:
>> btw, if anyone is interested in the (rather small) build-script for
>> nant, just ask,
> 
> I haven't seen anyone ask, so can I? I'd love to see the build script.

sorry it took me so long, i have been busy the last couple of days,
but here it is:

"PCBuild/nant-sln.build":
---


  

  

  




  

  

  

---

note that bsddb is explicitly excluded because i have not figured
out how to build it without visual studio. the rest of the extensions
are more or less straightforward if you follow the instructions in
"PCBuild/readme.txt". 

> Actually, beyond the script, what tool setup is needed? I have various
> bits of stuff (MS Visual C++ toolkit 2003, platform SDK, Visual Studio
> 8, VS .NET 2003). I found a few oddities, such as problems finding a
> usable msvcrt.lib (should it not have been using msvcr71.lib?) and
> issues generating pythonnt_rc.h.

as you can see in my .build-file i had problems with pythonnt_rc.h too.
AFAICT it should be generated by "make_versioninfo.vcproj" but somehow
it doesn't (i did not investigate further cause the above workaround
works quite well). i had no problems with any libs while building the
core python, but when building c-extensions (especially ssl was tricky
if i remember correctly).

> I sort-of got it working, and I was using a fairly old version of the
> sources (it was just a quick hack, to see if I could work out what I
> needed as a build script) but it would be nice to see the actual
> toolset listed out.

the tools i installed were:
- cygwin (with appropriate tools like svn)

- visual c++ toolkit

- nant

- the platform sdk (you dont need it complete but i dont know exactly what
  parts you need...)

- platform sdk for sp2 (i dont know whether you need this at all)

- MS .NET framework redistributable

that should be it, but i might be forgetting something.
you then need to set some environment variables like PATH, LIB and INCLUDE
to point to the directories in the different packages.


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


The effbot does not exist!

2006-01-11 Thread David Murmann
http://effbot.org/F

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


Re: Reading from input file.

2006-01-11 Thread David Murmann
hi!

i find it rather hard to understand your problem, but i'll try anyway:

[EMAIL PROTECTED] schrieb:
> So.. for my future Python script, the input data are in form of:
> 1
> 1233.2E-3 2123.2323 2E+2   3453.3E+1
> 1233.2E-3 2123.2323 2E+2   3453.3E+1
> 1233.2E-3 2123.2323 2E+2   3453.3E+1
> 2
> 1233.2E-3 2123.2323 2E+2   3453.3E+1
> 1233.2E-3 2123.2323 2E+2   3453.3E+1
> 1233.2E-3 2123.2323 2E+2   3453.3E+1
> and so on...

are these 1's and 2's there to indicate different files, or do
they really appear in the input files?

> The number 1 and 2 and so on, are the numbers of measurments, which are
> different in number depending on analyzed case. So sometimes it could
> be 3 and sometimes 1000 or more.
> 
> What I want to achieve, I want to compare or rather get difference
> between two similar input files. I tried to work with "readline"
> command, however it is hard to use it (for me) since input files are
> different in its size (3,1000 or more input points). Therefore, I
> decided to ask you for help how to do it. Esspecially how to make that
> python script which  will be reading data, each line, and each column
> of input file and therefore will compare it with different input file
> giving finally difference size between each measures for example:
> First data at point 1 minus first data from point 1 from different
> input file:
> (1233.2E-3) - (1233.3E-3) = -0.1E-3

well, from what i understood you might try something like this:

---
from itertools import izip

# iterate over two files at once (stopping at the shorter ones end)
for lines in izip(file('inputfile1'), file('inputfile2')):

# split the lines into columns
columns = [line.split() for line in lines]

# calculate and print the difference of the individual entries
for x, y in izip(*columns):
print float(x)-float(y),
print
---

if this (or something similar) does not work for you, you could take
a look at the difflib module:

  http://docs.python.org/lib/module-difflib.html

hope that helps, David.
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: How can I determine an HTTPMessage ?

2006-01-11 Thread David Murmann
Steve Holden schrieb:
> Kevin wrote:
>> Can you tell me what to look for in an HTTPMessage that is an error?  I
>> have looked at the header objects and I cannot determine an error
>> message.
>>
> I was thinking of a number between one and a thousand, and I forgot it. 
> Could someone please remind me what it was?

import smtplib

server = smtplib.SMTP('your.favorite.smtp.relay')

msg = """From: Spambot
To: [email protected]

Hi!
is it %d?
"""

for i in range(1, 1001):
server.sendmail('Spambot', '[email protected]', msg % i)

server.quit()

# could it be considered dangerous to post such code? ;)

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


Re: how to improve this simple block of code (str.partition?)

2006-01-11 Thread David Murmann
Peter Hansen schrieb:
> Matt's answer is still the only one that passes the tests.

well, here's another one:

-
def mysplit(s, sep):
x = s.rsplit(sep, 1)
return x + ['']*(2-len(x))

def stripZeros(x):
intpart, frac = mysplit(x, '.')
frac = frac.rstrip('0')
if frac:
return intpart+'.'+frac
else:
return intpart
-

i only mention this one, because i just remembered the discussion
about a str.partition method, which could save this version the
extra function. what happened to that method?

for anyone interested, it was proposed here:

  http://mail.python.org/pipermail/python-dev/2005-August/055764.html

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


Re: flatten a level one list

2006-01-11 Thread David Murmann
Robin Becker schrieb:
> Is there some smart/fast way to flatten a level one list using the 
> latest iterator/generator idioms.
> 
> The problem arises in coneverting lists of (x,y) coordinates into a 
> single list of coordinates eg
> 
> f([(x0,y0),(x1,y1),]) --> [x0,y0,x1,y1,] or
> 
> g([x0,x1,x2,..],[y0,y1,y2,]) -->  [x0,y0,x1,y1,]
> 
> clearly if f is doable then g can be done using zip. I suppose this is a 
> special case flatten, but can flatten be done fast? The python  recipes 
> seem rather slow compared to the builtin functions.

well then:

first of all, i need to say, if speed really matters, do it in C.
that being said, python can be fast, too. for this task psyco is your
friend. i got this output from the script given below:

without psyco:

flatten1: 2.78046748059
flatten2: 2.90226239686
flatten3: 4.91070862996
goopy_flatten1: 8.22951110963
goopy_flatten2: 8.56373180172

with psyco:

flatten1: 1.17390339924
flatten2: 1.7209583052
flatten3: 1.18490295558
goopy_flatten1: 1.34892236194
goopy_flatten2: 1.68568386584

the goopy function is taken from the google-functional package (but is
treated a bit unfair, i must admit, being wrapped in a lambda)

so, what does that show us? izip seems a bit faster than zip with these
input data. you want to do your own timings with more realistic data. 
and all these functions are what just came to my mind, i'm sure they
can be improved.

hope this helps,

--
David.

used script:


from itertools import izip

xdata = range(1000)
ydata = range(1000)[::-1]

def flatten1():
return [x for pair in izip(xdata, ydata) for x in pair]

def flatten2():
return [x for pair in zip(xdata, ydata) for x in pair]

def flatten3():
res = []
for pair in izip(xdata, ydata):
for x in pair:
res.append(x)
return res

def goopy_flatten(seq):
  lst = []
  for x in seq:
if type(x) is list or type(x) is tuple:
  for val in x:
lst.append(val)
else:
  lst.append(x)
  return lst

goopy_flatten1 = lambda: goopy_flatten(izip(xdata, ydata))
goopy_flatten2 = lambda: goopy_flatten(zip(xdata, ydata))

if __name__=='__main__':
from timeit import Timer

functions = ['flatten1', 'flatten2', 'flatten3', 'goopy_flatten1', 
'goopy_flatten2']

print 'without psyco:'
print

for fn in functions:
t = Timer(fn+'()', 'from __main__ import '+fn)
print fn+':', t.timeit(5000)

try: import psyco; psyco.full()
except ImportError: pass

print
print 'with psyco:'
print

for fn in functions:
t = Timer(fn+'()', 'from __main__ import '+fn)
print fn+':', t.timeit(5000)
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Real-world use cases for map's None fill-in feature?

2006-01-11 Thread David Murmann
[EMAIL PROTECTED] schrieb:
> I am still left with a difficult to express feeling of
> dissatifaction at this process.
> 
> Plese try to see it from the point of view of
> someone who it not a expert at Python:
> 
> ... [explains his POV]

i more or less completely agree with you, IOW i'd like izip
to change, too. but there are two problems that you haven't
mentioned. first is that, in the case of izip, it is not clear
how it should be fixed and if such a change does not naturally
fit an API it is difficult to incorporate. personally i think 
i like the keyword version ("izip(*args, sentinel=None)") best,
but the .rest-method version is appealing too...

second (and i think this is the reason for the use-case search)
is that someone has to do it. that means implement it and fix
the docs, add a test-case and such stuff. if there are not many
use-cases the effort to do so might not be worthwhile.

that means if someone (you?) steps forward with a patch that does
this, it would dramatically increase the chance of a change ;).

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


Re: flatten a level one list

2006-01-12 Thread David Murmann
Robin Becker schrieb:
> # New attempts:
> from itertools import imap
> def flatten4(x, y):
> '''D Murman'''
> l = []
> list(imap(l.extend, izip(x, y)))
> return l
> 
> 
> from Tkinter import _flatten
> def flatten5(x, y):
> '''D Murman'''
> return list(_flatten(zip(x, y)))

well, i would really like to take credit for these, but they're
not mine ;) (credit goes to Michael Spencer). i especially like
flatten4, even if its not as fast as the phenomenally faster
flatten7.


--
David Murmann (NN!) ;)
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Extending & Embedding Python

2006-01-12 Thread David Murmann
Marco Meoni schrieb:
> Hi all! I've a problem with a C++ class that has to be included in a
> python application. One way to do it is Extending and Embedding the
> Python Interpreter
> Now i have 2 questions
> 1) Is there a one-file version of this tutorial?

i'm not sure what tutorial you mean. are you talking about the
Extending and Embedding section of the python manual?

  http://docs.python.org/ext/ext.html

> 2) Is there anyone that can help me with this problem? The class is
> attached.

i haven't read your code, but you might want to take a look at SWIG:

  http://www.swig.org/

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


Re: Is 'everything' a refrence or isn't it?

2006-01-12 Thread David Murmann
[EMAIL PROTECTED] schrieb:
> "Fredrik Lundh" <[EMAIL PROTECTED]> wrote in message
>> *what* the value is is defined by the operations that the object supports 
>> (via its
>> type).
> 
> Well, that is already better than what is in the Lang Ref.
> But there must be more to it than that.  int(1) and int(2)
> have exactly the same operations, yes?  Yet their values
> are different.  (By "operations" you mean the set of methods
> an object has?)

well, yes, but the behavior of the operations of an object are
based on its value. and in python you have no other possibility
to get closer to an objects value than to look what its methods
do (in the case of an int, print it, compare it, etc.). that
means the whole concept of a value is not really necessary to be
able to write a python program.

> 1. Do all objects have values?
> 2. What is the value of object()?

i'd say these two are closely related, and i'd say yes, all objects
have values and you can't really tell what the value of an object() is.
what you can say is, that two object()s have different values, because
their behavior differs (id(object)!).

but, one could define that id() is a special case and does not
contribute to the behavior of an object, so that different int
objects that print the same really have the same value.

> 3. If two objects are equal with "==", does that
>   mean their values are the same?

no, absolutely not. think this class:

class C(object):
def __eq__(self, other):
return True

> 4. Are object attributes part of an object's type
>   or it's value, or something else?  (I think the first.)

they are part of an objects operations, just a shorthand for
__getattr__ and __setattr__.

> 5. The (only?) way to get an object's value is to
>   evaluate something (a name or a "reference"(*)
>   that refers to the object.

there is no such way (at least not to my knowledge).
at least not in general. of course you can print an int
and "see" its value, but you could define a C extension type
which holds a whole bunch of things as its value that are
completely inaccessible from python (are they then even part
of such an objects value? i don't care, its no use to define
"value" in either way).

i hope i did not confuse more than clarify, but this is the way
i see it.

also note that i understood Fredrik's quote from above in exactly
this way, this is just meant to clarify. (so please correct me if
i'm wrong)

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


Re: Collecting IP range

2006-01-30 Thread David Murmann
yawgmoth7 schrieb:
> Hello, I'm sure that this has been discussed before, but I have a
> question. I have written a few port scanners, banner grabbers in the
> past and have never found a good way to get a range of IP's.
> Obviously, in my opinion the best and simplest way to do somethign
> like and simple port scanner is to do something like:
> 
> 
> for ips in range(startip,endip):
>  
> 

you might want to take a look at IPy:

  http://c0re.23.nu/c0de/IPy/

iterating over ip-address ranges is very easy there:

  >>> ip = IP('127.0.0.0/30')
  >>> for x in ip:
  ...  print x
  ...
  127.0.0.0
  127.0.0.1
  127.0.0.2
  127.0.0.3

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


Re: Collecting IP range

2006-01-30 Thread David Murmann
yawgmoth7 schrieb:
> Well, I seem to have a bit of a problem:
> >>>import IPy
> >>>ip =IP('127.0.0.1/30')
> Traceback (Most recent call last):
> File "". line `, in ?
> NameError: name 'IP' is not defined
> >>>

to make this work with "import IPy" you need
to use "ip = IPy.IP('127.0.0.1/30')".

> I've tried doing it like:
> from IPy import *
> And then doing that, but it gives mea  different error. Thanks once again
> 

what error are you getting? it works for me...

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


Re: IPy module

2006-02-03 Thread David Murmann
yawgmoth7 schrieb:
> I was discussing this in another email, sadly I have misplaced the
> email. I got the module IPy, and I am taking a variable from the
> user(An IP) and doing something like this:
> 
> startip = raw_input("Enter start IP: ")
> ip = IPy.IP(startip\255)
> for x in ip():
>
> 
> Well, this doesn't work(I knew it wouldn't but i had to give it a try.
> Since, this has to be a value from the user, and It has to have a
> range(Hence the \255 part). How could I do this using IPy? Thank you
> for your time.

it is absolutely not clear what you want. what should \255 do? replace
the last part of the ip with 255? if so, why do you want to do that?
what do you expect the user to input? if he enters, for example,
192.168.1.2, which ip-addresses do you expect to get in the loop?

i have no clue what you want to do, sorry.

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


Anaglyph 3D Stereo Imaging with PIL and numpy

2006-05-02 Thread David Murmann
Hi all!

i just found some colored glass and experimented a bit with
these red/green 3d images, so i thought i'd share this simple
script i wrote to generate such images with anyone whos
interested. i also wouldn't mind some comments on the code.

see ya, David.




*** anaglyph.py ***

# uses PIL from http://www.pythonware.com/
# and numpy from http://www.scipy.org/

from PIL import Image
import numpy

_magic = [0.299, 0.587, 0.114]
_zero = [0, 0, 0]
_ident = [[1, 0, 0],
  [0, 1, 0],
  [0, 0, 1]]

# anaglyph methods from here:
# http://mitglied.lycos.de/stereo3d/anaglyphcomparison.htm

true_anaglyph = ([_magic, _zero, _zero], [_zero, _zero, _magic])
gray_anaglyph = ([_magic, _zero, _zero], [_zero, _magic, _magic])
color_anaglyph = ([_ident[0], _zero, _zero], [_zero, _ident[1], _ident[2]])
half_color_anaglyph = ([_magic, _zero, _zero], [_zero, _ident[1], _ident[2]])
optimized_anaglyph = ([[0, 0.7, 0.3], _zero, _zero], [_zero, _ident[1], 
_ident[2]])
methods = [true_anaglyph, gray_anaglyph, color_anaglyph, half_color_anaglyph, 
optimized_anaglyph]

def anaglyph(image1, image2, method=true_anaglyph):
m1, m2 = [numpy.array(m).transpose() for m in method]
im1, im2 = image_to_array(image1), image_to_array(image2)
composite = numpy.matrixmultiply(im1, m1) + numpy.matrixmultiply(im2, m2)
result = array_to_image(image1.mode, image1.size, composite)
return result

def image_to_array(im):
s = im.tostring()
dim = len(im.getbands())
return numpy.fromstring(s, numpy.UnsignedInt8).reshape(len(s)/dim, dim)

def array_to_image(mode, size, a):
return Image.fromstring(mode, size, a.reshape(len(a)*len(mode), 
1).astype(numpy.UnsignedInt8).tostring())

if __name__=='__main__':
im1, im2 = Image.open("left-eye.jpg"), Image.open("right-eye.jpg")

anaglyph(im1, im2, half_color_anaglyph).save('output.jpg', quality=98)
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Wake on LAN and Shutdown for Windows and Linux

2006-05-02 Thread David Murmann
[EMAIL PROTECTED] schrieb:
> How can I shutdown Windows box from my Main (Linux) ?

Have you seen this?

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

it probably won't work on linux (maybe with wine or similar?), but you
can try to invoke this script on a windows machine somehow...

have fun, David.

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


getting rid of pass

2006-05-12 Thread David Murmann
Hi all!

i just had this crazy idea:

instead of

while cond():
pass

write

while cond().

or

try:
import xyz
except ImportError:
pass

compared to

try:
import xyz
except ImportError.

i don't know whether this is syntactically unambiguous or replaces all
uses of pass, but i find it quite nice to read.

also if something like this has been proposed before and rejected, i
apologize for this late-in-the-night idea.

what do you people think?


have a nice day, David.
-- 
http://mail.python.org/mailman/listinfo/python-list