Re: passing context into BaseHTTPRequestHandler

2012-03-23 Thread Bernhard Herzog
Steve Howell  writes:

> I have a use case where I'm running BaseHTTPServer.HTTPServer, and I
> want to configure the request handler with some context.  I've gotten
> the code to work, but it feels overly heavy.  I am wondering if
> anybody could suggest an easier idiom for this.
>
> This is a brief sketch of the code:
>
> class MyHandler(BaseHTTPRequestHandler):
> def __init__(self, context, *args):
> self.context = context
> BaseHTTPRequestHandler.__init__(self, *args)
>
> def do_GET(self):
> // self.context will be available here
>
> context = {  }
> def handler(*args):
> MyHandler(context, *args)
>
> server = HTTPServer(('', port), handler)
> server.serve_forever()

You could store the context in the server object and access it in the
handler methods via self.server.context. It basically works like this:

class MyHTTPServer(HTTPServer):

def __init__(self, *args, **kw):
HTTPServer.__init__(self, *args, **kw)
self.context = {  }


class MyHandler(BaseHTTPRequestHandler):

def do_GET(self):
context = self.server.context
...


server = MyHTTPServer(('', port), MyHandler)
server.serve_forever()


   Bernhard


-- 
Bernhard Herzog  |  ++49-541-335 08 30  |  http://www.intevation.de/
Intevation GmbH, Neuer Graben 17, 49074 Osnabrück | AG Osnabrück, HR B 18998
Geschäftsführer: Frank Koormann, Bernhard Reiter, Dr. Jan-Oliver Wagner
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: C Extension - return an array of longs or pointer?

2005-10-12 Thread Bernhard Herzog
Brandon K <[EMAIL PROTECTED]> writes:

>   long* result = 0;
[...]
>   result = doNumberStuff(in,x);
>   len = sizeof(result)/sizeof(long);

I don't think this will do what you appear to expect it to do.

  Bernhard

-- 
Intevation GmbH http://intevation.de/
Skencil   http://skencil.org/
Thuban  http://thuban.intevation.org/
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: C Wrapper Function, crashing Python?

2005-10-12 Thread Bernhard Herzog
"Java and Swing" <[EMAIL PROTECTED]> writes:

> static PyObject *wrap_doStuff(PyObject *self, PyObject *args) {
[...]
>   char *aString = 0;
>   char *bString = 0;
[...]
>   int ok = PyArg_ParseTuple(args, "sss", &in, &aString, &bString);
[...]
>   free(aString);
>   free(bString);

aString and bString are pointers to memory managed by the strings in
your args tuple.  You must not free them!  The memory is automatically
managed by Python.

   Bernhard

-- 
Intevation GmbH http://intevation.de/
Skencil   http://skencil.org/
Thuban  http://thuban.intevation.org/
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: C Wrapper Function, crashing Python?

2005-10-12 Thread Bernhard Herzog
"Java and Swing" <[EMAIL PROTECTED]> writes:

> thanks for the tip, however even when I do not free aString or bString,
> i'm still crashing at the malloc in the c function, not the wrapper.

Do you have any more places where you use free incorrectly?  In my
experience, calling free with invalid values can corrupt the data
structures used by the memory allocator in such a way that subsequent
malloc calls crash.

   Bernhard

-- 
Intevation GmbH http://intevation.de/
Skencil   http://skencil.org/
Thuban  http://thuban.intevation.org/
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: C Wrapper Function, crashing Python?

2005-10-12 Thread Bernhard Herzog
"Java and Swing" <[EMAIL PROTECTED]> writes:

> char *foo(const char *in) {
> char *tmp;
> tmp = (char *) malloc((strlen(in) * sizeof(char)) + 1);
> strcpy(tmp, in);
> ...
> ...
> free(tmp);
> return someValue;
> }
>
> Is that appropriate?  I was under the impression that when you malloc
> memory, you free it when done.

Looks fine.  I hope someValue does not point somewhere into the tmp
buffer, though.

> I also have things like...
>
> char *bar(char *in) {
> char *results, *finalResults;
> results = (char *) malloc(...);
> finalResults = results;
>
> while (...) { *results++ = some_val; }
>
> return finalResults;
> }

Seems OK, too, assuming the results buffer is big enough.

   Bernhard

-- 
Intevation GmbH http://intevation.de/
Skencil   http://skencil.org/
Thuban  http://thuban.intevation.org/
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Windows vs Linux

2005-10-26 Thread Bernhard Herzog
"Tim Golden" <[EMAIL PROTECTED]> writes:

> But as far as I can tell
> from my experience and from the docs -- and I'm not near a 
> Linux box at the mo -- having used ctrl-r to recall line x 
> in the history, you can't just down-arrow to recall x+1, x+2 etc. 
> Or can you?

You can.  It works fine on this box, at least.
GNU bash, version 2.05a.0(1)-release (i386-pc-linux-gnu)
libreadline 4.2a

   Bernhard

-- 
Intevation GmbH http://intevation.de/
Skencil   http://skencil.org/
Thuban  http://thuban.intevation.org/
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Scanning a file

2005-10-28 Thread Bernhard Herzog
Jorge Godoy <[EMAIL PROTECTED]> writes:

> How about iterating through the file?  You can read it line by line, two lines
> at a time.  Pseudocode follows:
>
> line1 = read_line
> while line2 = read_line:
>   line_to_check = ''.join([line1, line2])
>   check_for_desired_string
>   line1 = line2
>
> With that you always have two lines in the buffer and you can check all of
> them for your desired string, no matter what the size of the file is.

This will fail if the string to search for is e.g. "\n\n\n\n" and it
actually occcurs in the file.

   Bernhard

-- 
Intevation GmbH http://intevation.de/
Skencil   http://skencil.org/
Thuban  http://thuban.intevation.org/
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: sax.make_parser() segfaults

2005-12-01 Thread Bernhard Herzog
"Frank Millman" <[EMAIL PROTECTED]> writes:

>> > If I call sax.make_parser() from the interpreter or from a stand-alone
>> > program, it works fine on all machines, but in the following setup it
>> > works correctly on MSW, but segfaults on both FC4 and RH9.
[...]
>> Progress report - I have narrowed it down to wxPython. I wrote small
>> stand-alone programs, one using Twisted, one using wxPython. Twisted
>> works fine, wxPython segfaults.

Could this be the following python bug:
https://sourceforge.net/tracker/index.php?func=detail&aid=1075984&group_id=5470&atid=105470

   Bernhard

-- 
Intevation GmbH http://intevation.de/
Skencil   http://skencil.org/
Thuban  http://thuban.intevation.org/
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: counting items

2005-01-12 Thread Bernhard Herzog
"It's me" <[EMAIL PROTECTED]> writes:

> May be flatten should be build into the language somehow

That shouldn't be necessary as it can easily be written in a single list
comprehension:

a = [[1,2,4],4,5,[2,3]]
flat_a = [x for cur, rest in [[a[:1], a[1:]]] for x in cur
if (not isinstance(x, (list, tuple))
and (not rest or not cur.append(rest.pop(0)))
or (x and (cur.append(x[0]) or rest.__setslice__(0, 0, x[1:])))
or (not x and rest and cur.append(rest.pop(0]

;-)

   Bernhard

-- 
Intevation GmbH http://intevation.de/
Skencil   http://skencil.org/
Thuban  http://thuban.intevation.org/
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: limited python virtual machine

2005-01-29 Thread Bernhard Herzog
[EMAIL PROTECTED] (Alex Martelli) writes:

> OK then -- vars(type(object)) is a dict which has [[the unbound-method
> equivalent of]] object.__subclasses__ at its entry for key
> '__subclasses__'.  Scratch 'vars' in addition to 'getattr'.  And 'eval'
> of course, or else building up the string 'object.__subclasses__' (in a
> way the regex won't catch) then eval'ing it is easy.  I dunno, maybe I'm
> just being pessimistic, I guess...

You can defeat the regexp without any builtin besides object:

>>> eval("# coding: utf7\n"
 "+AG8AYgBqAGUAYwB0AC4AXwBfAHMAdQBiAGMAbABhAHMAcwBlAHMAXwBf-")

>>> 

   Bernhard


-- 
Intevation GmbH http://intevation.de/
Skencil   http://skencil.org/
Thuban  http://thuban.intevation.org/
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Hey, get this!

2005-02-02 Thread Bernhard Herzog
Steve Holden <[EMAIL PROTECTED]> writes:

> What *I* would like to know is: who is allowing the import of bsddb.os,
> thereby somehow causing the code of the os library module to be run a
> second time.

I would guess (without actually running the code) that this part is
responsible:

>  if package:
>  module.__path__ = sys.path

You usually should initialize a package's __path__ to an empty list.
The __init__ module will take care of modifying it if necessary.

   Bernhard

-- 
Intevation GmbH http://intevation.de/
Skencil   http://skencil.org/
Thuban  http://thuban.intevation.org/
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Finding user's home dir

2005-02-03 Thread Bernhard Herzog
Peter Hansen <[EMAIL PROTECTED]> writes:

> Miki Tebeka wrote:
>>>Hi all, I'm trying to write a multiplatform function that tries to
>>>return the actual user home directory.
>>>...
>> What's wrong with:
>> from user import home
>> which does about what your code does.
>
> :-)
>
> I suspect he simply didn't know about it.  I didn't either...

The purpose of the user module is executing ~/.pythonrc.py, which may
not desirable.  It definitely shouldn't be done by a library, for
instance.  Also, that the user's home directory is available as
user.home is not documented, and I for one wouldn't want to rely on
that.

   Bernhard

-- 
Intevation GmbH http://intevation.de/
Skencil   http://skencil.org/
Thuban  http://thuban.intevation.org/
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Hey, get this!

2005-02-03 Thread Bernhard Herzog
Bernhard Herzog <[EMAIL PROTECTED]> writes:

> Steve Holden <[EMAIL PROTECTED]> writes:
>>  if package:
>>  module.__path__ = sys.path
>
> You usually should initialize a package's __path__ to an empty list.

Actually, normally it's a list that contains the name of the package
directory as its only item.  I'm not sure what you should do when you do
not import from a file system.

   Bernhard

-- 
Intevation GmbH http://intevation.de/
Skencil   http://skencil.org/
Thuban  http://thuban.intevation.org/
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Favorite non-python language trick?

2005-07-02 Thread Bernhard Herzog
Scott David Daniels <[EMAIL PROTECTED]> writes:

> Rocco Moretti wrote:
>> Joseph Garvin wrote:
>>
>> I'm not aware of a language that allows it, but recently I've found
>> myself wanting the ability to transparently replace objects
>> I mainly look for it in the "object replaces self" form, but I guess
>> you could also have it for arbitrary objects, e.g. to wrap a logging
>> object around a function, even if you don't have access to all
>> references of that function.
>> Why isn't it in Python? It's completely counter to the conventional
>> object semantics.
>
> Actually this is the old (and terrifying) Smalltalk message 'becomes:'.
> There is a concrete reason it is not in python: objects are represented
> as pointers to their data structures, do not have identical sizes, and
> therefore cannot be copied into each others data space. 

That limitation applies only some of the time, e.g. when you inherit
from built-in types.  But for ordinary classes it can be done:

>>> class A(object):
... def __init__(self, x):
... self.x = x
... 
>>> class B(object):
... def __init__(self, y):
... self.y = y
... 
>>> a = A(1)
>>> b = B(2)
>>> vars(a)
{'x': 1}
>>> vars(b)
{'y': 2}
>>> a.__class__, b.__class__ = b.__class__, a.__class__
>>> a.__dict__, b.__dict__ = b.__dict__, a.__dict__
>>> vars(a)
{'y': 2}
>>> isinstance(a, B)
True


   Bernhard

-- 
Intevation GmbH http://intevation.de/
Skencil   http://skencil.org/
Thuban  http://thuban.intevation.org/
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Can I make the Python build use an already-installed version of Expat?

2005-07-19 Thread Bernhard Herzog
Steve Juranich <[EMAIL PROTECTED]> writes:

> I'm running into problems where Python and VTK both ship with their
> own distribution of the Expat parser.  As long as you never use the
> Python XML package, everything is fine.  But if you try using the
> Python XML parser after doing an `import vtk', a nice little message
> saying "Segmentation Fault" is your reward.

This sounds like this bugreport on sourceforge:
http://python.org/sf/1075984

   Bernhard

-- 
Intevation GmbH http://intevation.de/
Skencil   http://skencil.org/
Thuban  http://thuban.intevation.org/
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Rich Graphics?

2005-07-28 Thread Bernhard Herzog
Chris Spencer <[EMAIL PROTECTED]> writes:

> I'm trying to write a Gui in Python for manipulating rich graphical
> representations, similar to something like Inkscape. I've tried tkinter,
> wxPython, pyGtk, and while they all do traditional widgets well enough,
> none of them really handle anti-aliased, transparent, transformed shapes
> typical of vector based displays.

You may want to have a look at Skencil (see my .sig for a URL).  The CVS
version does have anti-aliasing and some simple translucency.  It
doesn't support all of SVG yet, though.

   Bernhard

-- 
Intevation GmbH http://intevation.de/
Skencil   http://skencil.org/
Thuban  http://thuban.intevation.org/
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: How to determine that if a folder is empty?

2005-08-08 Thread Bernhard Herzog
[EMAIL PROTECTED] writes:

> On standard Unix fileystems, one way to check for this is to check that the
> st_nlink of the directory is 2. 

In that case you only know that the directory doesn't have any
subdirectories.  It may still contain ordinary files and other
non-directories.

   Bernhard

-- 
Intevation GmbH http://intevation.de/
Skencil   http://skencil.org/
Thuban  http://thuban.intevation.org/
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: python code with indention

2005-02-08 Thread Bernhard Herzog
Nick Vargish <[EMAIL PROTECTED]> writes:

> "Xah Lee" <[EMAIL PROTECTED]> writes:
>
>> is it possible to write python code without any indentation?
>
> Not if Turing-completeness is something you desire.

It's possible to implement a turing machine with a single list
comprehension.  No indentation needed.

   Bernhard

-- 
Intevation GmbH http://intevation.de/
Skencil   http://skencil.org/
Thuban  http://thuban.intevation.org/
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: turing machine in an LC

2005-02-08 Thread Bernhard Herzog
Jeremy Bowers <[EMAIL PROTECTED]> writes:

> On Tue, 08 Feb 2005 17:36:19 +0100, Bernhard Herzog wrote:
>> Nick Vargish <[EMAIL PROTECTED]> writes:
>>> "Xah Lee" <[EMAIL PROTECTED]> writes:
>>>> is it possible to write python code without any indentation?
>>> Not if Turing-completeness is something you desire.
>> 
>> It's possible to implement a turing machine with a single list
>> comprehension.  No indentation needed.
>
> I had to think about it, it's an interesting, and I'm going to tentatively
> disagree, because of the statement/expression dichotomy. "Tentatively"
> because if somebody can answer these objections than I will happily change
> my mind :-)


Here's an implementation of a turing machine simulator I hacked up a
while ago.  For readability's sake, it's a function definition with a
doc-string, but the heart of it is a single list comprehension which
could be written without indentation:


def listcomp_turing(M, T, initial_state = 0):
"""Run the turing machine M on the tape T.

The tape T should be a dictionary mapping head positions to the
value on the tape at that position.  Valid values on the tape are 0
and 1.  Empty positions have the value 0.  The initial head position
is 0.

The turing machine M should be a sequence of state pairs.  The state
of the machine is used as an index into that sequence and thus
should be in range(len(M)).  Each state pair is a pair of
(write_symbol, head_direction, next_state) triples, one for each of
the possible values on the tape at the current head position.  The
initial state is given by the optional parameter initial_state.  If
the next state is None, the machine stops.  The direction may be
either -1 or 1 meaning left and right respectively.  The function
does not enforce this limitation but with other values the machine
is not a turing machine anymore.

The return value is T.
"""
[x for L in [[[initial_state, 0]]]
   for state, pos in L
   if state is not None
  and (L.append([M[state][T.get(pos, 0)][2],
 pos + M[state][T.get(pos, 0)][1]])
   or T.__setitem__(pos, M[state][T.get(pos, 0)][0]))]
return T



For an example, lets take the one from wikipedia's article on turing
machines:


The following Turing machine has an alphabet {'0', '1'}, with 0
being the blank symbol. It expects a series of 1's on the tape, with
the head initially on the leftmost 1, and doubles the 1's with a 0
in between, i.e., "111" becomes "1110111". The set of states is {s1,
s2, s3, s4, s5} and the start state is s1. The action table is as
follows.

   Old Read   Wr.  New Old Read   Wr.  New
   St. Sym.   Sym. Mv. St. St. Sym.   Sym. Mv. St.
   - - - - - - - - - - - - - - - - - - - - - - - -
s1  1  ->  0R   s2  s4  1  ->  1L   s4
s2  1  ->  1R   s2  s4  0  ->  0L   s5
s2  0  ->  0R   s3  s5  1  ->  1L   s5
s3  0  ->  1L   s4  s5  0  ->  1R   s1
s3  1  ->  1R   s3


The listcomp_turing call with a tape containing "11" would be

listcomp_turing([((0, +1, None), (0, +1, 1)),
 ((0, +1, 2), (1, +1, 1)),
 ((1, -1, 3), (1, +1, 2)),
 ((0, -1, 4), (1, -1, 3)),
 ((1, +1, 0), (1, -1, 4))],
{0:1, 1:1})

which produces a result tape of

   {0: 1, 1: 1, 2: 0, 3: 1, 4: 1}


  Bernhard

-- 
Intevation GmbH http://intevation.de/
Skencil   http://skencil.org/
Thuban  http://thuban.intevation.org/
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: A ListComp that maintains its own state

2005-02-09 Thread Bernhard Herzog
Michael Spencer <[EMAIL PROTECTED]> writes:

> So, here's factorial in one line:
> # state refers to list of state history - it is initialized to [1]
> # on any iteration, the previous state is in state[-1]
> # the expression also uses the trick of list.append() => None
> # to both update the state, and return the last state
>
>  >>> [state.append(state[-1] * symbol) or state[-1]
> ... for symbol, state in it.izip(range(1,10),it.repeat([1]))
> ... ]
> [1, 2, 6, 24, 120, 720, 5040, 40320, 362880]
>  >>>

There's no need for repeat:

>>> [state.append(state[-1] * symbol) or state[-1] 
for state in [[1]]
for symbol in range(1, 10)]
[1, 2, 6, 24, 120, 720, 5040, 40320, 362880]


While we're at it, a while back I posted a list comprehension that
implements a 'recursive' flatten:

http://groups.google.de/groups?selm=s9zy8eyzcnl.fsf%40salmakis.intevation.de


   Bernhard

-- 
Intevation GmbH http://intevation.de/
Skencil   http://skencil.org/
Thuban  http://thuban.intevation.org/
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: comment out more than 1 line at once?

2004-11-30 Thread Bernhard Herzog
Riko Wichmann <[EMAIL PROTECTED]> writes:

> I'm using emacs (with python-mode) to do most of my editing.
[...]
> Maybe I just don't know to comment out whole blocks using editor
> commands.

comment-dwim (usually bound to M-; ) comments out the region if it's
active, or, if the region is already commented, uncomments it.

   Bernhard

-- 
Intevation GmbH http://intevation.de/
Skencil   http://skencil.org/
Thuban  http://thuban.intevation.org/
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: make uninstall?

2004-12-13 Thread Bernhard Herzog
"Fredrik Lundh" <[EMAIL PROTECTED]> writes:

> I forgot one file, btw:
>
>>> $ rm /usr/somewhere/bin/python
>
> $ rm /usr/somewhere/bin/python2.3

There are also pydoc and idle.

   Bernhard

-- 
Intevation GmbH http://intevation.de/
Skencil   http://skencil.org/
Thuban  http://thuban.intevation.org/
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: exec src in {}, {} strangeness

2005-03-21 Thread Bernhard Herzog
Stefan Seefeld <[EMAIL PROTECTED]> writes:

> Is there anything wrong with 'exec source in a, b' where
> a and b are distinc originally empty dictionaries ? Again,
> my test code was
>
> class Foo: pass
> class Bar:
>foo = Foo
>
> and it appears as if 'Foo' was added to 'a', but when evaluating
> 'foo = Foo' the interpreter only looked in 'b', not 'a'.

No, it's the other way round.  Foo is added in b since bindings are done
in the local scope.  Your case is a bit more complicated, though.
Here's what I think happens:

class Foo is bound in b, the locals dictionary, so there is no reference
to Foo in the globals dictionary.  The body of class B is executed with
it's own new locals dictionary.  That locals dictionary will effectively
be turned into Bar.__dict__ when the class object is created.

When "foo = Foo" is executed, Foo is first looked up in that new locals
dictionary.  That fails, so it's also looked up in the globals
dictionary a.  That fails as well because Foo was bound in b.  The final
lookup in the builtins also fails, and thus you get an exception.


   Bernhard

-- 
Intevation GmbH http://intevation.de/
Skencil   http://skencil.org/
Thuban  http://thuban.intevation.org/
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Why Python does *SLICING* the way it does??

2005-04-20 Thread Bernhard Herzog
Torsten Bronger <[EMAIL PROTECTED]> writes:

> It's interesting to muse about a language that starts at "1" for all
> arrays and strings, as some more or less obsolete languages do.  I
> think this is more intuitive, since most people (including
> mathematicians) start counting at "1".  The reason for starting at
> "0" is easier memory address calculation, so nothing for really high
> level languages.

There are very good reasons for half-open intervals and starting at 0
apart from memory organization.  Dijkstra explained this quite well in
http://www.cs.utexas.edu/users/EWD/ewd08xx/EWD831.PDF

   Bernhard

-- 
Intevation GmbH http://intevation.de/
Skencil   http://skencil.org/
Thuban  http://thuban.intevation.org/
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Why Python does *SLICING* the way it does??

2005-04-20 Thread Bernhard Herzog
Torsten Bronger <[EMAIL PROTECTED]> writes:

>> http://www.cs.utexas.edu/users/EWD/ewd08xx/EWD831.PDF
>
> I see only one argument there: "Inclusion of the upper bound would
> then force the latter to be unnatural by the time the sequence has
> shrunk to the empty one."  While this surely is unaesthetical, I
> don't think that one should optimise syntax according to this very
> special case.

The other main argument for startig at 0 is that if you do not include
the upper bound and start at 1 then the indices i of a sequence of N
values are 1 <= i < N + 1 which is not as nice as 0 <= i < N. 
opportunity for an off by one error.

Then there's also that, starting at 0, "an element's ordinal (subscript)
equals the number of elements preceding it in the sequence."


   Bernhard

-- 
Intevation GmbH http://intevation.de/
Skencil   http://skencil.org/
Thuban  http://thuban.intevation.org/
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Another newbie question

2005-12-10 Thread Bernhard Herzog

[EMAIL PROTECTED] (Alex Martelli) writes:
> You could make a case for a "2D coordinate" class being "sufficiently
> primitive" to have immutable instances, of course (by analogy with
> numbers and strings) -- in that design, you would provide no mutators,
> and therefore neither would you provide setters (with any syntax) for x
> and y, obviously.  However, a framework for 2D geometry entirely based
> on immutable-instance classes would probably be unwieldy

Skencil's basic objects for 2d geometry, points and transformations, are
immutable.  It works fine.  Immutable object have the great advantage of
making reasoning about the code much easier as the can't change behind
your back.

More complex objects such as poly bezier curves are mutable in Skencil,
and I'm not sure anymore that that was a good design decision.  In most
cases where bezier curve is modified the best approach is to simply
build a new bezier curve anyway.  Sort of like list-comprehensions make
it easier to "modify" a list by creating a new list based on the old
one.

   Bernhard

-- 
Intevation GmbH http://intevation.de/
Skencil   http://skencil.org/
Thuban  http://thuban.intevation.org/
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: list assignment

2006-02-23 Thread Bernhard Herzog
Norvell Spearman <[EMAIL PROTECTED]> writes:

> Lutz and Ascher have tuple and list assignment as separate entries in
> their assignment statement forms table so I was expecting there to be
> some difference; thanks for setting me straight.

In older Python versions there was a difference between list unpacking
and tuple unpacking.  The former would only work with lists and the
latter with tuples.  With Python 1.5, both were unified into a more
general sequence unpacking, but for backwards compatibility both
syntaxes were kept.

   Bernhard

-- 
Intevation GmbH http://intevation.de/
Skencil   http://skencil.org/
Thuban  http://thuban.intevation.org/
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Finding startup files

2005-05-12 Thread Bernhard Herzog
Grant Edwards <[EMAIL PROTECTED]> writes:

> On 2005-05-11, jeff elkins <[EMAIL PROTECTED]> wrote:
>
>> I'm totally new to Python (obvious,yes?) so how might argv[0] fail?
>
> argv[0] contains whatever is put there by the program that
> exec'ed you, and can therefore contain just about anything (or
> nothing).  It may not contain a full path, and your program's
> install directory may not be in your $PATH (it be executed by a
> shortcut or symlink).

That's true for the C-level, i.e. main's argv.  If you're only concerned
about CPython and the program is a script living in a file, then
sys.argv[0] is the filename the python interpreter itself used to read
the script.  Hence it's a valid filename that refers to the script.  It
may be a relative filename, of course, in which case it won't be correct
anymore if the program changes its working directory.

  Bernhard

-- 
Intevation GmbH http://intevation.de/
Skencil   http://skencil.org/
Thuban  http://thuban.intevation.org/
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Dialog with a process via subprocess.Popen blocks forever

2007-03-02 Thread Bernhard Herzog
[EMAIL PROTECTED] writes:

> So, once I start the C Program from the shell, I immediately get its
> output in my terminal. If I start it from a subprocess in python and
> use python's sys.stdin/sys.stdout as the subprocess' stdout/stdin I
> also get it immediately.

If stdout is connected to a terminal, it's usually line buffered, so the
buffer is flushed whenever a newline is written.

> BUT If I use PIPE for both (so I can .write() on the stdin and .read()
> from the subprocess' stdout stream (better: file descriptor)) reading
> from the subprocess stdout blocks forever. If I write something onto
> the subprocess' stdin that causes it to somehow proceed, I can read
> from its stdout.

When stdout is not connected to a terminal, it's usually fully buffered,
so that nothing is actually written to the file until the buffer
overflows or until it's explictly flushed.

If you can modify the C program, you could force its stdout stream to be
line buffered.  Alternatively, you could call fflush on stdout whenever
you're about to read from stdin.  If you can't modify the C program you
may have to resort to e.g. pseudo ttys to trick it into believing that
its stdout is a terminal.

   Bernhard

-- 
Intevation GmbH http://intevation.de/
Skencil   http://skencil.org/
Thuban  http://thuban.intevation.org/
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: round numbers in an array without importing Numeric or Math? - SOLVED, sort of

2006-05-17 Thread Bernhard Herzog
"Paul McGuire" <[EMAIL PROTECTED]> writes:

> ... or if you prefer the functional approach (using map)...
>
> roundToInt = lambda z : int(z+0.5)
> Topamax = map( roundToInt, map( float, map(str, Topamax) ) )
>
> (Python also has a built-in round() function, but this returns floats, not
> ints - if that is okay, then just delete the lambda definition, and replace
> roundToInt with round.)

Your roundToInt behaves differently from round for negative numbers:

>>> roundToInt(-0.6)
0
>>> int(round(-0.6))
-1

   Bernhard

-- 
Intevation GmbH http://intevation.de/
Skencil   http://skencil.org/
Thuban  http://thuban.intevation.org/
-- 
http://mail.python.org/mailman/listinfo/python-list