Re: Proposal to extend PEP 257 (New Documentation String Spec)

2011-07-15 Thread Michael Hrivnak
Was tried at least once before:
http://www.python.org/dev/peps/pep-0287/

Check in here with your ideas:
http://www.python.org/community/sigs/current/doc-sig/

Have any other languages mandated the use of a specific documentation markup?

Michael

On Thu, Jul 14, 2011 at 7:02 PM, rantingrick rantingr...@gmail.com wrote:

 Hello Folks,

 Lately i have been musing over the ideas of method tagging.
 Specifically i am referring to method identifiers. As most of you know
 i had proposed to add syntactical markers to the language to deal
 with the ambiguities that arise whist eyeball parsing sub classed
 methods that clobber virtual methods. HOWEVER that idea caused some
 fierce controversy within the community, and i can partly understand
 why.

 Although i strongly believe in proper documentation (even to the point
 of forcing syntax on folks) i understand that we cannot implement such
 a thing without major growing pains. So with that being said, i have
 formulated a new battle plan to defeat this problem of ambiguity.

 Unlike most languages out there we have doc-strings; and do we realize
 how great this gift is? Sometimes i wonder because you folks should
 really be using them like they are going out of style!

 As we all know PEP 257 lays out some ground rules for documentation
 strings HOWEVER i feel this PEP did no go far enough. Too many folks
 are refusing to document properly and so i will take this time to
 hammer out a spec. I would like to comments for or against.

 ---
  New Syntax Specification For Documentation Strings
 ---

  {DOC TAG HERE}: {MODULE_NAME|SHORT_SUMMARY_HERE}.
 {NEWLINE}
 {LONG_DESCRIPTION_HERE}
 {NEWLINE}
 Arguments: (if applicable)
    {ARGUMNET_1} {TYPE}:
        ARGUMENT_1_DESCRIPTION}
    {ARGUMNET_2} {TYPE}:
        ARGUMENT_2 DESCRIPTION}
    {ARGUMNET_N} {TYPE}:
        ARGUMENT_N_DESCRIPTION}
 {NEWLINE}
 

 As you can see my spec introduces some new ideas to writing doc-
 strings. Specifically the DOC TAG and {ARG TYPES} are new. Also i've
 found it much more useful to separate args and their respective
 descriptions with a newline and indention.

 ---
  Example: Module Documentation String.
 ---

 Module simpledialog.py:

 This module handles Tkinter dialog boxes.
 It contains the following public symbols:

    Dialog class:
        A base class for dialogs.

    askinteger function:
        Get an integer from the user.

    askfloat function:
        Get a float from the user.

    askstring function:
        Get a string from the user.

 

 I don't know how i feel about marking classes and functions since IF
 we follow the python style guide we don;t need to; but that's IF we
 FOLLOW it people, IF.

 ---
  Example: Func/Meth Documentation String.
 ---

 def askinteger(parent, title, prompt, **kw):
     Interface: Get an integer from the user.

    Return value is an integer.

    Arguments:
        title string:
            the dialog title
        prompt string|integer|float:
            the label text
        **kw:
            see SimpleDialog class


 ---
  Example: Class Inheritance Documentation Strings.
 ---

 class Base():
    def __init__(self):
         Internal:
        self.m1()

    def m1(self, *args):
        Overide: 
        pass

 class Derived(Base):
    def __init__(self):
        Base.__init__(self)

    def _m1(self):
         Internal: blah

    def m1(self):
         Clobbered: see Base for detail

    def m3(self):
         Interface: Blah

 ---
  Tags For Documentation Strings
 ---

  Module:
    The module tag is to be used for module doc strings.

  Virtual:
    The virtual tag is for methods residing in a base class
    that are created specifically to be overridden. Of course as we
    all know every Python methods/function is virtual by default
    however the point of this is to make the code more readable!

  Override:
    This tag should be placed in a derived class's method which has
    clobbered a base method. Typically you can just defer the reader
    to look up the base class for more info.

  Internal:
    This tag should be used on all internal methods (psst: the ones
 that
    start with a single underscore *ahem* or SHOULD start with a
 single
    underscore!).

  Interface:
    This tag is be used for interface method/function doc strings.
 This
    is probably the most important tag. If you don't do any tagging AT
    LEAST tag the interface methods and functions. However i must
    remind you that all these tags are very important.

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

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


Re: None versus MISSING sentinel -- request for design feedback

2011-07-15 Thread Chris Angelico
On Fri, Jul 15, 2011 at 3:28 PM, Steven D'Aprano
steve+comp.lang.pyt...@pearwood.info wrote:
 My question is, should I accept None as the missing value, or a dedicated
 singleton?

 In favour of None: it's already there, no extra code required. People may
 expect it to work.

 Against None: it's too easy to mistakenly add None to a data set by mistake,
 because functions return None by default.

I guess the question is: Why are the missing values there? If they're
there because some function returned None because it didn't have a
value to return, and therefore it's a missing value, then using None
as missing would make a lot of sense. But if it's a more explicit
concept of here's a table of values, and the user said that this one
doesn't exist, it'd be better to have an explicit MISSING. (Which I
assume would be exposed as yourmodule.MISSING or something.)

Agreed that float('nan') and  and spam are all bad values for
Missings. Possibly  should come out as 0, but spam should
definitely fail.

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


Re: Multiplicity and Asininity in Tkinter Event API

2011-07-15 Thread Chris Angelico
On Fri, Jul 15, 2011 at 8:34 AM, rantingrick rantingr...@gmail.com wrote:
  KeyPress
  KeyRelease
  MouseClick
  MouseMotion
  MouseRelease
  MouseWheel

 That's it. Go ahead, try to prove me wrong!


Does MouseClick mean Mouse Button Down, or does it mean Mouse Button
Pressed Then Released Within A Short Time Without The Mouse Moving In
Between? Both events are needed. Since you have MouseRelease, I am
guessing that it's MouseButtonDown (or MousePress, to parallel your
KeyPress/KeyRelease).

If you only have MouseDown and MouseUp events (or MousePress and
MouseRelease, whatever you wish to call them), then users have a lot
of trouble implementing CUA-compliant mouse-click handlers.

In the spirit of Ook and its predecessor, I propose one single event:
InputEvent. It will have a parameter that specifies the key ID or
mouse button that triggered the event; 0 means no key/button, and
indicates a mouse movement event. It will contain the location of the
mouse, and it's up to the program to keep track of the last known
mouse location, to detect movement. The beauty of this system is that
programmers can build interfaces that involve pointing at an object,
holding the four keys D-R-A-G, and moving the mouse to another
location and releasing those keys. This is far more intuitive than the
classic mouse-button way of dragging objects around, and avoids the
age-old debate of whether to drag with the left or right mouse button.
Tkinter should provide this facility so that all new programs will be
written with this in mind.

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


Re: list(), tuple() should not place at Built-in functions in documentation

2011-07-15 Thread Chris Angelico
On Fri, Jul 15, 2011 at 11:32 AM, Steven D'Aprano
steve+comp.lang.pyt...@pearwood.info wrote:
 Inside wrote:

 As telling in the subject,because list and tuple aren't functions,they
 are types.Is that right?

 Yes they are types. But they can still be used as functions. Does it matter?

Python is duck-typed, even in its documentation. If Python describes
something as a function, it means it can be used in place of func in
here:

result = func(arg1, arg2, arg3)

It might be something created with def (a classic function). It
might be something created with lambda. It might be an object with a
__call__ method. It might be a type.

 class foo:
def __call__(self):
return lambda: print(asdf)

 bar=foo()
 quux=bar()
 quux()
asdf

How many functions are defined here?

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


Re: Please critique my script

2011-07-15 Thread Chris Angelico
On Fri, Jul 15, 2011 at 4:03 AM, Ellerbee, Edward eeller...@bbandt.com wrote:
 for makenewlist in range(0,count):
     sortlist.append(npalist.pop(0) + nxxlist.pop(0))

This can alternatively be done with map():

sortlist = map(lambda x,y: x+y, npalist, nxxlist)

However, I'm not sure what your code is meant to do if the two lists
have differing numbers of elements (if the two regexps turn up
differing numbers of results). If you can assume that this won't
happen, the simple map call will do the job.

(It would have been a lot cleaner if Python exposed its operators as
functions. In Pike, that lambda would simply be `+ (backtick-plus).)

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


Re: Proposal to extend PEP 257 (New Documentation String Spec)

2011-07-15 Thread Chris Angelico
On Fri, Jul 15, 2011 at 9:02 AM, rantingrick rantingr...@gmail.com wrote:
 Too many folks
 are refusing to document properly and so i will take this time to
 hammer out a spec.

The tighter you squeeze your fist, Lord Rick, the more star
programmers will slip through your fingers.

Make it so docstrings HAVE to be in a particular format, and people
will stop writing docstrings. Make it so Python functions HAVE to have
docstrings, and people will stop writing Python functions.

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


Re: Please critique my script

2011-07-15 Thread bruno.desthuilli...@gmail.com
On Jul 15, 8:36 am, Chris Angelico ros...@gmail.com wrote:
 This can alternatively be done with map():

 sortlist = map(lambda x,y: x+y, npalist, nxxlist)


 (It would have been a lot cleaner if Python exposed its operators as
 functions.

from operator import add




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


Re: list(), tuple() should not place at Built-in functions in documentation

2011-07-15 Thread bruno.desthuilli...@gmail.com
On Jul 15, 4:58 am, Inside fancheyuj...@gmail.com wrote:
 Hey guy,thx for you feedback first.

 But I can't follow your opinion.Why?because of the list  tuple are placed at 
 built-in function,so before I type 'list' unintentionally on the pyshell and 
 it show me type 'list', I never know that the name 'list' is a type,I 
 used to consider it's a function to produce 'list' type.

 so,after I figure out this matter,I have to change all my code assert 
 isinstance(someobj, (type([]), type((0,  to assert isinstance(someobj, 
 (list, tuple)),that's not a funny job.

Are you sure you need such assertions in your code ?
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: list(), tuple() should not place at Built-in functions in documentation

2011-07-15 Thread bruno.desthuilli...@gmail.com
On Jul 15, 9:27 am, bruno.desthuilli...@gmail.com
bruno.desthuilli...@gmail.com wrote:
 On Jul 15, 4:58 am, Inside fancheyuj...@gmail.com wrote:

  Hey guy,thx for you feedback first.

  But I can't follow your opinion.Why?because of the list  tuple are placed 
  at built-in function,so before I type 'list' unintentionally on the pyshell 
  and it show me type 'list', I never know that the name 'list' is a 
  type,I used to consider it's a function to produce 'list' type.

  so,after I figure out this matter,I have to change all my code assert 
  isinstance(someobj, (type([]), type((0,  to assert 
  isinstance(someobj, (list, tuple)),that's not a funny job.

 Are you sure you need such assertions in your code ?

Sorry, Ben already mentionned this. Need more coffee obviously :-/
-- 
http://mail.python.org/mailman/listinfo/python-list


Python ++ Operator?

2011-07-15 Thread Rafael Durán Castañeda
Hello all,

I seem something like this on python code:

for  :
for  :
 spam[i][eggs] = ham
   ++i
.
.
.

What's the meaning of using i++? Even, does exist ++ operator in python?

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


Re: list(), tuple() should not place at Built-in functions in documentation

2011-07-15 Thread Carl Banks
On Thursday, July 14, 2011 8:00:16 PM UTC-7, Terry Reedy wrote:
 I once proposed, I believe on the tracker, that 'built-in functions' be 
 expanded to 'built-in function and classes'. That was rejected on the 
 basis that people would then expect the full class documentation that is 
 in the 'built-in types' section (which could now be called the 
 built-isssn classes section.

Built in functions and contructors?


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


Re: None versus MISSING sentinel -- request for design feedback

2011-07-15 Thread Cameron Simpson
On 15Jul2011 15:28, Steven D'Aprano steve+comp.lang.pyt...@pearwood.info 
wrote:
| In favour of None: it's already there, no extra code required. People may
| expect it to work.

Broadly, I like this one for the reasons you cite.

| Against None: it's too easy to mistakenly add None to a data set by mistake,
| because functions return None by default.

This is a hazard everywhere, but won't such a circumstance normally
break lots of stuff anyway? What's an example scenario for getting None
by accident but still a bunch of non-None values? The main one I can
imagine is a function with a return path that accidentally misses the
value something, eg:

  def f(x):
if blah:
  return 7
...
if foo:
  return 0
# whoops!


I suppose there's no scope for having the append-to-the-list step sanity
check for the sentinel (be it None or otherwise)?

| In favour of a dedicated MISSING singleton: it's obvious from context. It's
| not a lot of work to implement compared to using None. Hard to accidentally
| include it by mistake. If None does creep into the data by accident, you
| get a nice explicit exception.

I confess to being about to discard None as a sentinel in a bit of my
own code, but only to allow None to be used as a valid value, using the
usual idiom:

  class IQ(Queue):
def __init__(self, ...):
  self._sentinel = object()
  ...

| Against MISSING: users may expect to be able to choose their own sentinel by
| assigning to MISSING. I don't want to support that.

Well, we don't have readonly values to play with :-(
Personally I'd do what I did above: give it a private name like
_MISSING so that people should expect to have inside (and unsupported,
unguarenteed) knowledge if they fiddle with it. Or are you publishing
the sentinal's name to your callers i.e. may they really return _MISSING
legitimately from their functions?

Cheers,
-- 
Cameron Simpson c...@zip.com.au DoD#743
http://www.cskk.ezoshosting.com/cs/

What's fair got to do with it? It's going to happen.- Lawrence of Arabia
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: None versus MISSING sentinel -- request for design feedback

2011-07-15 Thread Rob Williscroft
Steven D'Aprano wrote in news:4e1fd009$0$29986$c3e8da3
$54964...@news.astraweb.com in gmane.comp.python.general:

 I'm designing an API for some lightweight calculator-like statistics
 functions, such as mean, standard deviation, etc., and I want to support
 missing values. Missing values should be just ignored. E.g.:
 
 mean([1, 2, MISSING, 3]) = 6/3 = 2 rather than 6/4 or raising an error.

If you can't make your mind up then maybe you shouldn't:

MISSING = MissingObject()
def mean( sequence, missing = MISSING ):
  ...

Rob.

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


Re: Python ++ Operator?

2011-07-15 Thread Björn Lindqvist
2011/7/15 Rafael Durán Castañeda rafadurancastan...@gmail.com:
 What's the meaning of using i++? Even, does exist ++ operator in python?

No it doesn't, which is a good thing. Python differentiates between
expressions and variable assignments. i++ in C both assigns the
value i + 1 to i AND evaluates to i. Pre and post-increments are
almost always confusing unless they are used as the counter-variable
inside for-loops.


-- 
mvh/best regards Björn Lindqvist
http://www.footballexperts.net/
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: None versus MISSING sentinel -- request for design feedback

2011-07-15 Thread bruno.desthuilli...@gmail.com
On Jul 15, 8:08 am, Chris Angelico ros...@gmail.com wrote:

 Agreed that float('nan') and  and spam are all bad values for
 Missings. Possibly  should come out as 0

In the face of ambiguity, refuse the temptation to guess.

As far as I'm concerned, I'd expect this to raise a TypeError...


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


Re: Python ++ Operator?

2011-07-15 Thread Chris Angelico
2011/7/15 Rafael Durán Castañeda rafadurancastan...@gmail.com:
 Hello all,
 What's the meaning of using i++? Even, does exist ++ operator in python?

++i is legal Python but fairly useless. It's the unary + operator,
applied twice. It doesn't increment the variable.

Now, i+=1 IS valid Python, and WILL do what a C programmer expects it to.

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


Re: Please critique my script

2011-07-15 Thread Chris Angelico
On Fri, Jul 15, 2011 at 5:19 PM, bruno.desthuilli...@gmail.com
bruno.desthuilli...@gmail.com wrote:
 (It would have been a lot cleaner if Python exposed its operators as
 functions.

 from operator import add

Ah yes, I forgot that module. Yep, that cleans up the code a bit.

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


Re: Possible File iteration bug

2011-07-15 Thread bruno.desthuilli...@gmail.com
On Jul 14, 9:46 pm, Billy Mays no...@nohow.com wrote:
 I noticed that if a file is being continuously written to, the file
 generator does not notice it:

 def getLines(f):
      lines = []
      for line in f:
          lines.append(line)
      return lines

what's wrong with file.readlines() ?
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: None versus MISSING sentinel -- request for design feedback

2011-07-15 Thread bruno.desthuilli...@gmail.com
On Jul 15, 7:28 am, Steven D'Aprano steve
+comp.lang.pyt...@pearwood.info wrote:

 I'm designing an API for some lightweight calculator-like statistics
 functions, such as mean, standard deviation, etc., and I want to support
 missing values. Missing values should be just ignored. E.g.:


(snip)

 Against None: it's too easy to mistakenly add None to a data set by mistake,
 because functions return None by default.

Yeps.

 In favour of a dedicated MISSING singleton: it's obvious from context. It's
 not a lot of work to implement compared to using None. Hard to accidentally
 include it by mistake. If None does creep into the data by accident, you
 get a nice explicit exception.

 Against MISSING: users may expect to be able to choose their own sentinel by
 assigning to MISSING. I don't want to support that.

What about allowing users to specificy their own sentinel in the
simplest pythonic way:

# stevencalc.py
MISSING = object()

def mean(values, missing=MISSING):
your code here


Or, if you want to make it easier to specify the sentinel once for the
whole API:

# stevencalc.py
MISSING = object()

class Calc(object):
def __init__(self, missing=MISSING):
self._missing = missing
def mean(self, values):
# your code here


# default:
_calc = Calc()
mean = _calc.mean
# etc...

My 2 cents...
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Python ++ Operator?

2011-07-15 Thread Chris Angelico
2011/7/15 Björn Lindqvist bjou...@gmail.com:
 Pre and post-increments are
 almost always confusing unless they are used as the counter-variable
 inside for-loops.

I agree that they're often confusing (i+j) but there are several
places where they're handy.

array[count++]=value;

or the more direct pointer management:

*ptr++=value;

However, Python doesn't work as close to the bare metal, so it doesn't
have such constructs.

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


Re: Freeze statically

2011-07-15 Thread Sebastien Dudek
Actually, there is a solution but not very clean.

I was surprise by two différent tools:
- Statifier (but it needs to be compiled without ASLR)
- And ErminePro which is very powerful to transform dynamic
executables to static.

But if someone is aware about compiling python code to static more
properly, I'm in!


On Jul 11, 2:24 pm, Sebastien Dudek flux...@gmail.com wrote:
 Hi everyone!

 Let me explain you my big adventure. So I trying to make a static
 python executable using the native Python freeze. I've modified the
 file Modules/Setup.dist using this perl cli : perl -pi -e 's!(^#
 \*shared\*)!*static*\n$1!' Modules/Setup.dist

 Then ./configure, make  make install

 If I'm using my python interpreter to build with freeze a simple
 project that we can call hello world. It works perfectly but it is
 still : dynamically linked (uses shared libs). So modifying the
 Makefile using '-static' for linking, I hopped I could make it static.

 But... it fail :
 +--
 +
 ...
 /home/fluxius/Python-2.7.2/./Modules/pwdmodule.c:156: warning: Using
 'setpwent' in statically linked applications requires at runtime the
 shared libraries from the glibc version used for linking
 /home/fluxius/Python-2.7.2/./Modules/pwdmodule.c:168: warning: Using
 'endpwent' in statically linked applications requires at runtime the
 shared libraries from the glibc version used for linking
 /usr/bin/ld: dynamic STT_GNU_IFUNC symbol `strcmp' with pointer
 equality in `/usr/lib/x86_64-linux-gnu/gcc/x86_64-linux-gnu/
 4.5.2/../../../libc.a(strcmp.o)' can not be used when making an
 executable; recompile with -fPIE and relink with -pie
 collect2: ld returned 1 exit status
 make: *** [hello] Erreur 1
 +--
 +

 Help me please!! =)

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


Re: None versus MISSING sentinel -- request for design feedback

2011-07-15 Thread Teemu Likonen
* 2011-07-15T15:28:41+10:00 * Steven D'Aprano wrote:

 I'm designing an API for some lightweight calculator-like statistics
 functions, such as mean, standard deviation, etc., and I want to
 support missing values. Missing values should be just ignored. E.g.:

 mean([1, 2, MISSING, 3]) = 6/3 = 2 rather than 6/4 or raising an
 error.

 My question is, should I accept None as the missing value, or a
 dedicated singleton?

How about accepting anything but ignoring all non-numbers?
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Installing PyPy alongside Python 2.7 on Windows?

2011-07-15 Thread cjrh
Fair enough. FWIW I have struggled to install binary packages on PyPy on 
Windows.
-- 
http://mail.python.org/mailman/listinfo/python-list


subprocess isql

2011-07-15 Thread peterff66
Hello Python community, 

I am working on a project that retrieves data from remote Sybase. I have to use 
isql and subprocess to do this. Is the following correct?

1. call subprocess.popn to run isql, connect to Sybase
2. run sql (select ...from ...)
3. write retrieved data to subprocess.pipe
4. retrieve data from pipe

Did anybody do similar work before? Any ideas (or code piece) to share?

Thanks so much. 

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


Re: None versus MISSING sentinel -- request for design feedback

2011-07-15 Thread bruno.desthuilli...@gmail.com
On Jul 15, 9:44 am, Cameron Simpson c...@zip.com.au wrote:
 On 15Jul2011 15:28, Steven D'Aprano steve+comp.lang.pyt...@pearwood.info 
 wrote:
 | Against MISSING: users may expect to be able to choose their own sentinel by
 | assigning to MISSING. I don't want to support that.

 Well, we don't have readonly values to play with :-(
 Personally I'd do what I did above: give it a private name like
 _MISSING so that people should expect to have inside (and unsupported,
 unguarenteed) knowledge if they fiddle with it.

I think the point is to allow users to explicitely use MISSING in
their data sets, so it does have to be public. But anyway: ALL_UPPER
names are supposed to be treated as constants, so the warranty void
if messed with still apply.
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: None versus MISSING sentinel -- request for design feedback

2011-07-15 Thread bruno.desthuilli...@gmail.com
On Jul 15, 10:28 am, Teemu Likonen tliko...@iki.fi wrote:

 How about accepting anything but ignoring all non-numbers?

Totally unpythonic. Better to be explicit about what you expect and
crash as loudly as possible when you get anything unexpected.
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: None versus MISSING sentinel -- request for design feedback

2011-07-15 Thread Steven D'Aprano
Cameron Simpson wrote:

 On 15Jul2011 15:28, Steven D'Aprano steve+comp.lang.pyt...@pearwood.info
 wrote:
 | In favour of None: it's already there, no extra code required. People
 | may expect it to work.
 
 Broadly, I like this one for the reasons you cite.
 
 | Against None: it's too easy to mistakenly add None to a data set by
 | mistake, because functions return None by default.
 
 This is a hazard everywhere, but won't such a circumstance normally
 break lots of stuff anyway? 

Maybe, maybe not. Either way, it has nothing to do with me -- I only care
about what my library does if presented with None in a list of numbers.
Should I treat it as a missing value, and ignore it, or treat it as an
error?


 What's an example scenario for getting None 
 by accident but still a bunch of non-None values? The main one I can
 imagine is a function with a return path that accidentally misses the
 value something, eg:
[code snipped]

Yes, that's the main example I can think of. It doesn't really matter how it
happens though, only that it is more likely for None to accidentally get
inserted into a list than it is for a module-specific MISSING value.

My thoughts are, if my library gets presented with two lists:

[1, 2, 3, None, 5, 6]

[1, 2, 3, mylibrary.MISSING, 5, 6]

which is less likely to be an accident rather than deliberate? That's the
one I should accept as the missing value. Does anyone think that's the
wrong choice?


 I suppose there's no scope for having the append-to-the-list step sanity
 check for the sentinel (be it None or otherwise)?

It is not my responsibility to validate data during construction, only to do
the right thing when given that data. The right thing being, raise an
exception if values are not numeric, unless an explicit missing value
(whatever that ends up being).


 | Against MISSING: users may expect to be able to choose their own
 | sentinel by assigning to MISSING. I don't want to support that.
 
 Well, we don't have readonly values to play with :-(
 Personally I'd do what I did above: give it a private name like
 _MISSING so that people should expect to have inside (and unsupported,
 unguarenteed) knowledge if they fiddle with it. Or are you publishing
 the sentinal's name to your callers i.e. may they really return _MISSING
 legitimately from their functions?

Assuming I choose against None, and go with MISSING, it will be a public
part of the library API. The idea being that callers will be responsible
for ensuring that if they have data with missing values, they insert the
correct sentinel, rather than whatever random non-numeric value they
started off with.



-- 
Steven

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


Re: None versus MISSING sentinel -- request for design feedback

2011-07-15 Thread Steven D'Aprano
Rob Williscroft wrote:

 Steven D'Aprano wrote in news:4e1fd009$0$29986$c3e8da3
 $54964...@news.astraweb.com in gmane.comp.python.general:
 
 I'm designing an API for some lightweight calculator-like statistics
 functions, such as mean, standard deviation, etc., and I want to support
 missing values. Missing values should be just ignored. E.g.:
 
 mean([1, 2, MISSING, 3]) = 6/3 = 2 rather than 6/4 or raising an error.
 
 If you can't make your mind up then maybe you shouldn't:

Heh, good point.

It's not so much that I can't make up my mind -- I have a preferred solution
in mind, but I want to hear what sort of interface for dealing with missing
values others expect, and I don't want to prejudice others too greatly.


 MISSING = MissingObject()
 def mean( sequence, missing = MISSING ):

So you think the right API is to allow the caller to specify what counts as
a missing value at runtime? Are you aware of any other statistics packages
that do that?


-- 
Steven

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


Re: None versus MISSING sentinel -- request for design feedback

2011-07-15 Thread Cameron Simpson
On 15Jul2011 20:17, Steven D'Aprano steve+comp.lang.pyt...@pearwood.info 
wrote:
| Cameron Simpson wrote:
|  I suppose there's no scope for having the append-to-the-list step sanity
|  check for the sentinel (be it None or otherwise)?
| 
| It is not my responsibility to validate data during construction, only to do
| the right thing when given that data. The right thing being, raise an
| exception if values are not numeric, unless an explicit missing value
| (whatever that ends up being).

Well there you go. You need to use MISSING, not None. As you say, None
can easily be a mistake and you want to be sure. If what you describe as
right is right, then I too would be using a special sentinal instead
of None.
-- 
Cameron Simpson c...@zip.com.au DoD#743
http://www.cskk.ezoshosting.com/cs/

The English language has a word to describe a group of anarcho-collectivists
without resorting to spiffy hyphenated coined phrases: a mob.
- Tim Mefford, t...@physics.orst.edu
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: None versus MISSING sentinel -- request for design feedback

2011-07-15 Thread Steven D'Aprano
Chris Angelico wrote:

 On Fri, Jul 15, 2011 at 3:28 PM, Steven D'Aprano
 steve+comp.lang.pyt...@pearwood.info wrote:
 My question is, should I accept None as the missing value, or a dedicated
 singleton?

 In favour of None: it's already there, no extra code required. People may
 expect it to work.

 Against None: it's too easy to mistakenly add None to a data set by
 mistake, because functions return None by default.
 
 I guess the question is: Why are the missing values there? If they're
 there because some function returned None because it didn't have a
 value to return, and therefore it's a missing value, then using None
 as missing would make a lot of sense. But if it's a more explicit
 concept of here's a table of values, and the user said that this one
 doesn't exist, it'd be better to have an explicit MISSING. (Which I
 assume would be exposed as yourmodule.MISSING or something.)

In general, you have missing values in statistics because somebody wouldn't
answer a question, and the Ethics Committee frowns on researchers torturing
their subjects to get information. They make you fill out forms.

Seriously, missing data is just missing. Unknown. Lost. Not available. Like:

NameAge Income Years of schooling
==
Bill42  150,00016
Susan   23  39,000 14
Karen   unknown 89,000 15
Bob 31  0  7
George  79  12,000 unknown
Sally   17  19,000 5
Fred66  unknown11

One might still like to calculate the average age as 43.



-- 
Steven

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


Re: None versus MISSING sentinel -- request for design feedback

2011-07-15 Thread Teemu Likonen
* 2011-07-15T03:02:11-07:00 * bruno wrote:

 On Jul 15, 10:28 am, Teemu Likonen tliko...@iki.fi wrote:
 How about accepting anything but ignoring all non-numbers?

 Totally unpythonic. Better to be explicit about what you expect and
 crash as loudly as possible when you get anything unexpected.

Sure, but sometimes an API can be accept anything if any kind of trash
is expected. But it seems that not in this case, so you're right.
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: None versus MISSING sentinel -- request for design feedback

2011-07-15 Thread Chris Angelico
On Fri, Jul 15, 2011 at 8:46 PM, Steven D'Aprano
steve+comp.lang.pyt...@pearwood.info wrote:
 In general, you have missing values in statistics because somebody wouldn't
 answer a question, and the Ethics Committee frowns on researchers torturing
 their subjects to get information. They make you fill out forms.


Which, then, is in support of an explicit User chose not to answer
this question MISSING value.

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


how to get Python to insert special characters in an xml file?

2011-07-15 Thread hackingKK

Hello all.
I am currently developing a python application which reads and writes 
some data to an xml file.

I use the elementTree library for doing this.
My simple question is that if I have some thing like  as in kk  
company  as organisation name, how can I have Python take this as a 
litteral string including the  sign and put in the orgname /orgname 
tag?
Even same applies while reading the file.  I would like to have the  
come as a part of the literal string.

Happy hacking.
Krishnakant.

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


Re: Possible File iteration bug

2011-07-15 Thread Billy Mays

On 07/15/2011 04:01 AM, bruno.desthuilli...@gmail.com wrote:

On Jul 14, 9:46 pm, Billy Maysno...@nohow.com  wrote:

I noticed that if a file is being continuously written to, the file
generator does not notice it:

def getLines(f):
  lines = []
  for line in f:
  lines.append(line)
  return lines


what's wrong with file.readlines() ?


Using that will read the entire file into memory which may not be 
possible.  In the library reference, it mentions that using the 
generator (which calls file.next()) uses a read ahead buffer to 
efficiently loop over the file.  If I call .readline() myself, I forfeit 
that performance gain.


I was thinking that a convenient solution to this problem would be to 
introduce a new Exception call PauseIteration, which would signal to the 
caller that there is no more data for now, but not to close down the 
generator entirely.


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


Re: Possible File iteration bug

2011-07-15 Thread Thomas Rachel

Am 14.07.2011 21:46 schrieb Billy Mays:

I noticed that if a file is being continuously written to, the file
generator does not notice it:


Yes. That's why there were alternative suggestions in your last thread 
How to write a file generator.


To repeat mine: an object which is not an iterator, but an iterable.

class Follower(object):
def __init__(self, file):
self.file = file
def __iter__(self):
while True:
l = self.file.readline()
if not l: return
yield l

if __name__ == '__main__':
import time
f = Follower(open(/var/log/messages))
while True:
for i in f: print i,
print all read, waiting...
time.sleep(4)

Here, you iterate over the object until it is exhausted, but you can 
iterate again to get the next entries.


The difference to the file as iterator is, as you have noticed, that 
once an iterator is exhausted, it will be so forever.


But if you have an iterable, like the Follower above, you can reuse it 
as you want.

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


Re: None versus MISSING sentinel -- request for design feedback

2011-07-15 Thread Steven D'Aprano
Chris Angelico wrote:

 On Fri, Jul 15, 2011 at 8:46 PM, Steven D'Aprano
 steve+comp.lang.pyt...@pearwood.info wrote:
 In general, you have missing values in statistics because somebody
 wouldn't answer a question, and the Ethics Committee frowns on
 researchers torturing their subjects to get information. They make you
 fill out forms.

 
 Which, then, is in support of an explicit User chose not to answer
 this question MISSING value.

Well yes, but None is an explicit missing value too. The question I have is
if I should support None as that value, or something else. Or if anyone can
put a good case for it, both, or neither and so something completely
different.



-- 
Steven

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


Re: Possible File iteration bug

2011-07-15 Thread Billy Mays

On 07/15/2011 08:39 AM, Thomas Rachel wrote:

Am 14.07.2011 21:46 schrieb Billy Mays:

I noticed that if a file is being continuously written to, the file
generator does not notice it:


Yes. That's why there were alternative suggestions in your last thread
How to write a file generator.

To repeat mine: an object which is not an iterator, but an iterable.

class Follower(object):
def __init__(self, file):
self.file = file
def __iter__(self):
while True:
l = self.file.readline()
if not l: return
yield l

if __name__ == '__main__':
import time
f = Follower(open(/var/log/messages))
while True:
for i in f: print i,
print all read, waiting...
time.sleep(4)

Here, you iterate over the object until it is exhausted, but you can
iterate again to get the next entries.

The difference to the file as iterator is, as you have noticed, that
once an iterator is exhausted, it will be so forever.

But if you have an iterable, like the Follower above, you can reuse it
as you want.



I did see it, but it feels less pythonic than using a generator.  I did 
end up using an extra class to get more data from the file, but it seems 
like overhead.  Also, in the python docs, file.next() mentions there 
being a performance gain for using the file generator (iterator?) over 
the readline function.


Really what would be useful is some sort of PauseIteration Exception 
which doesn't close the generator when raised, but indicates to the 
looping header that there is no more data for now.


--
Bill


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


Re: Possible File iteration bug

2011-07-15 Thread Chris Angelico
On Fri, Jul 15, 2011 at 10:52 PM, Billy Mays no...@nohow.com wrote:
 Really what would be useful is some sort of PauseIteration Exception which
 doesn't close the generator when raised, but indicates to the looping header
 that there is no more data for now.


All you need is a sentinel yielded value (eg None).

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


Re: None versus MISSING sentinel -- request for design feedback

2011-07-15 Thread Mel
Steven D'Aprano wrote:

 Well yes, but None is an explicit missing value too. The question I have
 is if I should support None as that value, or something else. Or if anyone
 can put a good case for it, both, or neither and so something completely
 different.

If it's any help, I think (some of?) the database interface packages already 
do just that, returning None when they find NULL fields.


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


Re: subprocess isql

2011-07-15 Thread Frank Millman


peterff66 peterf...@yahoo.com wrote in message 
news:ivo9or+j...@egroups.com...

Hello Python community,

I am working on a project that retrieves data from remote Sybase. I have 
to use isql and subprocess to do this. Is the following correct?


1. call subprocess.popn to run isql, connect to Sybase
2. run sql (select ...from ...)
3. write retrieved data to subprocess.pipe
4. retrieve data from pipe

Did anybody do similar work before? Any ideas (or code piece) to share?


I did something vaguely similar a while ago. I don't know how helpful this 
will be, but it may give you some ideas.


Firstly, these are the main differences between your scenario and mine -

1. I was using 'osql' to connect to MS SQL Server.
2. I used os.popen4, not subprocess. Hopefully you can follow the guidelines 
in the manual to translate to subprocess.
3. In my case, I built up a long command to create and populate various 
database tables using StringIO, passed the string to popen4.stdout, and then 
read popen4.stdin and displayed it on the screen to read any output.
4. On looking at my code, I see that I started a sub-thread to read 
popen4.stdin. I can't remember why I did that, but it may have been 
something to do with getting the output in realtime instead of waiting for 
the command to finish executing.


Here is the code that I used -

   import os
   import threading
   from cStringIO import StringIO

   def read_stdout(stdout):
   while True:
   line = stdout.readline()
   if not line:
   break
   print line

   os.environ['OSQLPASSWORD'] = pwd
   sql_stdin, sql_stdout = os.popen4('osql -U %s -d %s -n' % (user, 
database))


   s = StringIO()
   [call function to build up commands]

   threading.Thread(target=read_stdout, args=(sql_stdout,)).start()

   s.seek(0)
   sql_stdin.writelines(s.readlines())
   s.close()
   sql_stdin.close()

HTH

Frank Millman


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


Re: Multiplicity and Asininity in Tkinter Event API

2011-07-15 Thread rantingrick
On Jul 15, 1:17 am, Chris Angelico ros...@gmail.com wrote:
 On Fri, Jul 15, 2011 at 8:34 AM, rantingrick rantingr...@gmail.com wrote:
   KeyPress
   KeyRelease
   MouseClick
   MouseMotion
   MouseRelease
   MouseWheel

  That's it. Go ahead, try to prove me wrong!

 Does MouseClick mean Mouse Button Down, or does it mean Mouse Button
 Pressed Then Released Within A Short Time Without The Mouse Moving In
 Between? Both events are needed.

NO these six sequences are ALL you need!

I suppose you are suggesting that you will be unable to detect when
the mouse is repeat-firing or when you want to detect groups of
rapid-succession clicks (double, triple, etc)? This info is
contained in the 'event' object passed to the handler by Tkinter.

if event.time [] lasttime:
blah
if event.repeat:
blah

By removing all the unnessary sequence combinations you create
readable code and ease the leaning curve.

  Since you have MouseRelease, I am
 guessing that it's MouseButtonDown (or MousePress, to parallel your
 KeyPress/KeyRelease).

Ok Mr. Pedantic. I suppose for unity sake we should use the sequence
name MousePress instead of MouseClick. I'll give you that one.
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Multiplicity and Asininity in Tkinter Event API

2011-07-15 Thread Chris Angelico
On Fri, Jul 15, 2011 at 11:54 PM, rantingrick rantingr...@gmail.com wrote:
 Ok Mr. Pedantic. I suppose for unity sake we should use the sequence
 name MousePress instead of MouseClick. I'll give you that one.


It is very significant; if you call it Click, people will assume it
means Press, Release within a certain maximum time and without more
than a maximum amount of movement (in some cases 0 movement, others
permit a mickey or two).

Yes, that's all that's actually needed. But if you force programmers
to process the low-level events, you put load on them that they don't
need. It's possible to define every program in terms of a handful of
operations - do you want to ditch all of Python's rich syntax and
library in favour of that?

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


Re: how to get Python to insert special characters in an xml file?

2011-07-15 Thread Philip Semanchuk

On Jul 15, 2011, at 7:53 AM, hackingKK wrote:

 Hello all.
 I am currently developing a python application which reads and writes some 
 data to an xml file.
 I use the elementTree library for doing this.
 My simple question is that if I have some thing like  as in kk  company  
 as organisation name, how can I have Python take this as a litteral string 
 including the  sign and put in the orgname /orgname tag?
 Even same applies while reading the file.  I would like to have the  come as 
 a part of the literal string.

Hi Krishnakant,
You don't need to do anything special to insert metacharacters like  and  and 
 into XML using ElementTree. Just treat them as normal text and ElementTree 
will change them to entity references (amp;, etc.) when it writes your file to 
disk. 

If you're having a specific problem with this, post some code.

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


Re: Possible File iteration bug

2011-07-15 Thread Thomas Rachel

Am 15.07.2011 14:26 schrieb Billy Mays:


I was thinking that a convenient solution to this problem would be to
introduce a new Exception call PauseIteration, which would signal to the
caller that there is no more data for now, but not to close down the
generator entirely.


Alas, an exception thrown causes the generator to stop.


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


Re: Possible File iteration bug

2011-07-15 Thread Thomas Rachel

Am 15.07.2011 14:52 schrieb Billy Mays:


Also, in the python docs, file.next() mentions there
being a performance gain for using the file generator (iterator?) over
the readline function.


Here, the question is if this performance gain is really relevant AKA 
feelable. The file object seems to have another internal buffer 
distinct from the one used for iterating used for the readline() 
function. Why this is not the same buffer is unclear to me.




Really what would be useful is some sort of PauseIteration Exception
which doesn't close the generator when raised, but indicates to the
looping header that there is no more data for now.


a None or other sentinel value would do this as well (as ChrisA already 
said).



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


Re: subprocess isql

2011-07-15 Thread sturlamolden
On 15 Jul, 04:47, peterff66 peterf...@yahoo.com wrote:

 I am working on a project that retrieves data from remote Sybase. I have to 
 use isql and subprocess to do this. Is the following correct?


I once had to hammer a nail, but I had to use a screw driver. Didn't
work.


 Did anybody do similar work before? Any ideas (or code piece) to share?

Nobody has ever used Python to connect with an SQL database before.
There is no Python database API, and no Sybase module.





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


Re: Possible File iteration bug

2011-07-15 Thread Billy Mays

On 07/15/2011 10:28 AM, Thomas Rachel wrote:

Am 15.07.2011 14:52 schrieb Billy Mays:


Also, in the python docs, file.next() mentions there
being a performance gain for using the file generator (iterator?) over
the readline function.


Here, the question is if this performance gain is really relevant AKA
feelable. The file object seems to have another internal buffer
distinct from the one used for iterating used for the readline()
function. Why this is not the same buffer is unclear to me.



Really what would be useful is some sort of PauseIteration Exception
which doesn't close the generator when raised, but indicates to the
looping header that there is no more data for now.


a None or other sentinel value would do this as well (as ChrisA already
said).


Thomas


A sentinel does provide a work around, but it also passes the problem 
onto the caller rather than the callee:


def getLines(f):
lines = []

while True:
yield f.readline()

def bar(f):
for line in getLines(f):
if not line: # I now have to check here instead of in getLines
break
foo(line)


def baz(f):
for line in getLines(f) if line: # this would be nice for generators
foo(line)


bar() is the correct way to do things, but I think baz looks cleaner.  I 
found my self writing baz() first, finding it wasn't syntactically 
correct, and then converting it to bar().  The if portion of the loop 
would be nice for generators, since it seems like the proper place for 
the sentinel to be matched.  Also, with potentially infinite (but 
pauseable) data, there needs to be a nice way to catch stuff like this.


--
Bill






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


Re: Proposal to extend PEP 257 (New Documentation String Spec)

2011-07-15 Thread George Rodrigues da Cunha Silva

Em sexta-feira, 15 de julho de 2011 04:13:43, Chris Angelico escreveu:

On Fri, Jul 15, 2011 at 9:02 AM, rantingrickrantingr...@gmail.com  wrote:

Too many folks
are refusing to document properly and so i will take this time to
hammer out a spec.


The tighter you squeeze your fist, Lord Rick, the more star
programmers will slip through your fingers.

Make it so docstrings HAVE to be in a particular format, and people
will stop writing docstrings. Make it so Python functions HAVE to have
docstrings, and people will stop writing Python functions.

ChrisA


I'm with Chris. If you mandate this sort of things on the programmer 
this will just go downhill.


George

--

George Rodrigues da Cunha Silva
Desenvolvedor GIS
http://geoprocessamento.net
http://blog.geoprocessamento.net
--
http://mail.python.org/mailman/listinfo/python-list


Python 2.6.6 on Windows 7 problem - ImportError: No module named site

2011-07-15 Thread Mark Ohrboot
Hello!

If I start python -v, I get

# installing zipimport hook
import zipimport # builtin
# installed zipimport hook
'import site' failed; traceback
ImportError: No module named site

Entering any command yields
LookupError: no codec search functions registered: can't find
encoding

I reinstalled it, but this makes no difference. Any suggestions?

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


Re: None versus MISSING sentinel -- request for design feedback

2011-07-15 Thread Eric Snow
On Thu, Jul 14, 2011 at 11:28 PM, Steven D'Aprano
steve+comp.lang.pyt...@pearwood.info wrote:
 Hello folks,

 I'm designing an API for some lightweight calculator-like statistics
 functions, such as mean, standard deviation, etc., and I want to support
 missing values. Missing values should be just ignored. E.g.:

 mean([1, 2, MISSING, 3]) = 6/3 = 2 rather than 6/4 or raising an error.

 My question is, should I accept None as the missing value, or a dedicated
 singleton?

 In favour of None: it's already there, no extra code required. People may
 expect it to work.

 Against None: it's too easy to mistakenly add None to a data set by mistake,
 because functions return None by default.

Good point.


 In favour of a dedicated MISSING singleton: it's obvious from context. It's
 not a lot of work to implement compared to using None. Hard to accidentally
 include it by mistake. If None does creep into the data by accident, you
 get a nice explicit exception.

Also good points.


 Against MISSING: users may expect to be able to choose their own sentinel by
 assigning to MISSING. I don't want to support that.


 I've considered what other packages do:-

 R uses a special value, NA, to stand in for missing values. This is more or
 less the model I wish to follow.

 I believe that MATLAB treats float NANs as missing values. I consider this
 an abuse of NANs and I won't be supporting that :-P

I was just thinking of this.  :)


 Spreadsheets such as Excel, OpenOffice and Gnumeric generally ignore blank
 cells, and give you a choice between ignoring text and treating it as zero.
 E.g. with cells set to [1, 2, spam, 3] the AVERAGE function returns 2 and
 the AVERAGEA function returns 1.5.

 numpy uses masked arrays, which is probably over-kill for my purposes; I am
 gratified to see it doesn't abuse NANs:

 import numpy as np
 a = np.array([1, 2, float('nan'), 3])
 np.mean(a)
 nan

 numpy also treats None as an error:

 a = np.array([1, 2, None, 3])
 np.mean(a)
 Traceback (most recent call last):
  File stdin, line 1, in module
  File /usr/lib/python2.5/site-packages/numpy/core/fromnumeric.py, line
 860, in mean
    return mean(axis, dtype, out)
 TypeError: unsupported operand type(s) for +: 'int' and 'NoneType'


 I would appreciate any comments, advice or suggestions.


Too bad there isn't a good way to freeze a name, i.e. indicate that
any attempt to rebind it is an exception.  Trying to rebind None is a
SyntaxError, but a NameError or something would be fine.  Then the
downside of using your own sentinel here goes away.

In reality, using Missing may be your best bet anyway.  If there were
a convention for indicating a name should not be re-bound (like a
single leading underscore indicates private), you could use that
(all caps?).  Since we're all consenting adults it would probably be
good enough to make sure others know that Missing should not be
re-bound...

I might have said to use NotImplemented instead of None, but it can be
re-bound and the name isn't as helpful for your use case.

Another solution, perhaps ugly or confusing, is to use something like
two underscores as the name for your sentinel:

mean([1, 2, __, 3])

Still it seems like using Missing (or whatever) would be better than None.

-eric


 --
 Steven

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

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


Re: None versus MISSING sentinel -- request for design feedback

2011-07-15 Thread Ethan Furman

Mel wrote:

Steven D'Aprano wrote:


Well yes, but None is an explicit missing value too. The question I have
is if I should support None as that value, or something else. Or if anyone
can put a good case for it, both, or neither and so something completely
different.


If it's any help, I think (some of?) the database interface packages already 
do just that, returning None when they find NULL fields.


Indeed.  I'm adding Null support to my dbf package now, and while some 
of the return values (Logical, Date, DateTime, and probably Character) 
will have their own dedicated singletons (Null, NullDate, NullDateTime, 
NullChar -- which will all compare equal to None) the numeric values 
will be None... although, now that I've seen this thread, I'll add the 
ability to choose what the numeric Null is returned as.


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


Re: None versus MISSING sentinel -- request for design feedback

2011-07-15 Thread OKB (not okblacke)
Steven D'Aprano wrote:

 Rob Williscroft wrote:
 MISSING = MissingObject()
 def mean( sequence, missing = MISSING ):
 
 So you think the right API is to allow the caller to specify what
 counts as a missing value at runtime? Are you aware of any other
 statistics packages that do that?

R does it, not in the stats functions itself but in, for instance 
read.table.  When reading data from an external file, you can specify a 
set of values that will be converted to NA in the resulting data frame.

I think it's worth considering this approach, namely separating the 
input of the data into your system from the calculations on that 
data.  You haven't said exactly how people are going to be using your 
API, but your example of where mising data comes from showed something 
like a table of data from a survey.  If this is the case, and users are 
going to be importing sets of data from external files, it makes a lot 
of sense to let them specify convert these particular values to MISSING 
when importing.

Either way, my answer to your original question would be: if you 
want to err on the side of caution, use your own MISSING value and just 
provide a simple function that will MISSING-ize specified values:

def ckeanUp(data, missing=None):
if missing is None:
missing = []
return [d for d in data if d not in missing else MISSING]

(Yet another use of None here! :-)

Then if people find their functions are returning None (or any 
other value, such as an empty string) to mean a genuine missing value, 
they can just wrap the call in this cleanUp function.  The reverse is 
harder to do: if you use None as your missing-value sentinel, you 
irrevocably lose the ability to tell it apart from other uses of None.

-- 
--OKB (not okblacke)
Brendan Barnwell
Do not follow where the path may lead.  Go, instead, where there is
no path, and leave a trail.
--author unknown
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Proposal to extend PEP 257 (New Documentation String Spec)

2011-07-15 Thread rantingrick
On Jul 15, 2:13 am, Chris Angelico ros...@gmail.com wrote:
 On Fri, Jul 15, 2011 at 9:02 AM, rantingrick rantingr...@gmail.com wrote:
  Too many folks
  are refusing to document properly and so i will take this time to
  hammer out a spec.

 The tighter you squeeze your fist, Lord Rick, the more star
 programmers will slip through your fingers.

 Make it so docstrings HAVE to be in a particular format, and people
 will stop writing docstrings. Make it so Python functions HAVE to have
 docstrings, and people will stop writing Python functions.

Hmm, that's strange considering that code MUST be formatted in certain
ways or you get a syntax error (indention, colons, parenthesis, etc,
etc). I don't hear the masses claiming that they are going over to
Ruby simply because of indention.

In my mind doc-strings should ALWAYS be optional HOWEVER if the
programmer decides to create a doc-string THEN he must observe some
syntax rules or his code will throw an SyntaxError. Remember, freedom
is good, unbridled freedom is the root of all evil.

So what's so terrible about structure Chris? Nobody's freedom are
being taken away. You don't HAVE to create doc-strings, just like you
don't HAVE to code with Python (you do free form formatting Ruby).
Python is a language that is meant to be clean. Forced indention makes
that possible. Forced doc-string syntax will complete the circle.


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


Looking for general advice on complex program

2011-07-15 Thread Josh English
Maybe not to the gurus here, but for me, this is a complex problem and I want 
to make sure I understand the real problem.

All of this is in Python 2.7 and wxPython

I have several XML files on a shared drive.
I have applications on other machines that need to access this XML file.
These applications need to read and write to this file.
These applications need to a) be alerted to a file change, or b) monitor the 
file for changes and regular intervals.

In my code, I have XManager classes (using a Singleton pattern) that reads each 
XML file into a tree (using ElementTree). The XManager class can read the file, 
make changes to the tree, and write the file as needed.

Now I'm expanding to the multiple application level, and I think I understand 
what I need to do, and I'm unsure about the exact processes.

I've been trying to have the XManagers check periodically if the XML file they 
monitor has changed. Since I don't want to mess up the GUI with constant 
hanging, I think I can use the thread or threading modules to create a 
recurring timed check, and then I need a separate check to see if the file is 
in use before reading or writing. 

I also need, I think, to have a way to check if the GUI is editing a node 
before the XManager reads the file, so the thread needs to be interrupted, or 
paused, because I don't know if threads would stop if a wxDialog is being show 
modally or not.

So, am I on the right track here? 

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


Re: Looking for general advice on complex program

2011-07-15 Thread Billy Mays

On 07/15/2011 03:47 PM, Josh English wrote:




I remember reading that file locking doesn't work on network mounted 
drives (specifically nfs mounts), but you might be able to simply create 
a 'lock' (mydoc.xml.lock or the like) file for the XML doc in question. 
 If that file exists you could either hang or silently give up.  Not 
sure if that helps.


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


Re: list(), tuple() should not place at Built-in functions in documentation

2011-07-15 Thread Inside
Why I use assertion,please check this code:

class User(object):pass

class Student(User):pass

class Professional(User):pass

def add(user):
assert(user, User)

def add(users):
assert(users, (tuple, list))
#If necessary I'll also check every obj in the sequence to see whether it's 
a User.


I just follow some coding rules of me:
1. Controlling input strictly.
2. In a function keep doubting on its parameters until they're checked.
3. Let potential errors raise as early as possible.
-- 
http://mail.python.org/mailman/listinfo/python-list


OpenCV Object Detection

2011-07-15 Thread T
Hi all.. what is the best way (using OpenCV) to go about detecting
objects by color?  Specifically, I need to detect multiple objects -
first, a main object (a coin), then multiple objects within that main
(green particles on the coin).  I'm finding lots of info about facial
recognition, but not much in the way of just detecting by color.  In
this case, the coin will always be silver and the particles contained
on it will always be some shade of green.

Accuracy is very important - which is why I figured detecting via
color would be the way to go.  My goal is to detect as close to the
exact size of the coin/particles as possible.  That said, if there are
more accurate ways out there I'm open to suggestions!  Thanks in
advance.
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Python ++ Operator?

2011-07-15 Thread Dan Stromberg
On Fri, Jul 15, 2011 at 1:06 AM, Chris Angelico ros...@gmail.com wrote:

 2011/7/15 Björn Lindqvist bjou...@gmail.com:
  Pre and post-increments are
  almost always confusing unless they are used as the counter-variable
  inside for-loops.

 I agree that they're often confusing (i+j) but there are several
 places where they're handy.

 array[count++]=value;

 or the more direct pointer management:

 *ptr++=value;

 However, Python doesn't work as close to the bare metal, so it doesn't
 have such constructs.


I don't regard this as a low level versus VHLL issue - I regard it as a
matter of operators with side effects being too error prone.  Adding such
operators to Python has been discussed (it'd almost certainly be easy to
add), and rejected.

BTW, array operations optimize to the same thing as pointer arithmetic in
most C compilers, but the latter tends to be less clear.
-- 
http://mail.python.org/mailman/listinfo/python-list


Python threading/multiprocessing issue.

2011-07-15 Thread Brandon Harris

I'm working on a tool that runs a number of process is separate thread.
I've, up to this point, been using threading.Thread, but from what I
read multiprocess will allow multiple processors to be used
From the python docs on multiprocessing.
Due to this, the multiprocessing module allows the programmer to fully
   leverage multiple processors on a given machine.

I have run into an issue when modifying the thread object from the run
method. Threading.thread allows me to change an attribute in the run 
method and it hold while multiprocessing.Process loses it.


Here is an example illustrating the inconsistency that I've seen.

--
|
import time
import multiprocessing
import threading

def simple_process_call():
my_process = SimpleProcess()
my_process.start()
while not my_process.done.is_set():
pass

print my_process.my_attribute

class SimpleProcess(multiprocessing.Process):
def __init__(self):
super(SimpleProcess, self).__init__()
self.my_attribute = 'Fail'
self.done = multiprocessing.Event()

def run(self):
self.my_attribute = 'Success'
time.sleep(5)
self.done.set()

def simple_thread_call():
my_thread = SimpleThread()
my_thread.start()
while not my_thread.done.is_set():
pass

print my_thread.my_attribute

class SimpleThread(threading.Thread):
def __init__(self):
super(SimpleThread, self).__init__()
self.my_attribute = 'Fail'
self.done = threading.Event()

def run(self):
self.my_attribute = 'Success'
time.sleep(5)
self.done.set()

if __name__ == '__main__':
# simple_process_call()
simple_thread_call()|


--

The odd thing is that I can modify the multiprocessing.Event and it 
holds, but modifying any attribute on the class goes away.


If I am super ignorant of something, please cure me of it.

Thanks in advance!



Brandon L. Harris

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


Re: Python ++ Operator?

2011-07-15 Thread Chris Torek
In article mailman.1055.1310716536.1164.python-l...@python.org
Chris Angelico  ros...@gmail.com wrote:
2011/7/15 Rafael Durán Castañeda rafadurancastan...@gmail.com:
 Hello all,
 What's the meaning of using i++? Even, does exist ++ operator in python?

++i is legal Python but fairly useless. It's the unary + operator,
applied twice. It doesn't increment the variable.

Well...

class Silly:
def __init__(self, value):
self.value = value
self._pluscount = 0
def __str__(self):
return str(self.value)
def __pos__(self):
self._pluscount += 1
if self._pluscount == 2:
self.value += 1
self._pluscount = 0
return self

def main():
i = Silly(0)
print('initially, i = %s' % i)
print('plus-plus i = %s' % ++i)
print('finally, i = %s' % i)

main()

:-)

(Of course, +i followed by +i *also* increments i...)
-- 
In-Real-Life: Chris Torek, Wind River Systems
Intel require I note that my opinions are not those of WRS or Intel
Salt Lake City, UT, USA (40°39.22'N, 111°50.29'W)  +1 801 277 2603
email: gmail (figure it out)  http://web.torek.net/torek/index.html
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Possible File iteration bug

2011-07-15 Thread Thomas Rachel

Am 15.07.2011 16:42 schrieb Billy Mays:


A sentinel does provide a work around, but it also passes the problem
onto the caller rather than the callee:


That is right.


BTW, there is another, maybe easier way to do this:

for line in iter(f.readline, ''):
do_stuff(line)

This provides an iterator which yields return values from the given 
callable until '' is returned, in which case the iterator stops.


As caller, you need to have knowledge about the fact that you can always 
continue.


The functionality which you ask for COULD be accomplished in two ways:

Firstly, one could simply break the contract of an iterator (which 
would be a bad thing): just have your next() raise a StopIteration and 
then continue nevertheless.


Secondly, one could do a similiar thing and have the next() method raise 
a different exception. Then the caller has as well to know about, but I 
cannot find a passage in the docs which prohibit this.


I just have tested this:
def r(x): return x
def y(x): raise x

def l(f, x): return lambda: f(x)
class I(object):
def __init__(self):
self.l = [l(r, 1), l(r, 2), l(y, Exception), l(r, 3)]
def __iter__(self):
return self
def next(self):
if not self.l: raise StopIteration
c = self.l.pop(0)
return c()

i = I()
try:
for j in i: print j
except Exception, e: print E:, e
print tuple(i)

and it works.


So I think it COULD be ok to do this:

class NotNow(Exception): pass

class F(object):
def __init__(self, f):
self.file = f
def __iter__(self):
return self
def next(self):
l = self.file.readline()
if not l: raise NotNow
return l

f = F(file(/var/log/messages))
import time
while True:
try:
for i in f: print , i,
except NotNow, e:
print pause
time.sleep(1)


HTH,

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


Re: Python threading/multiprocessing issue.

2011-07-15 Thread Lee Harr

 I'm working on a tool that runs a number of process is separate thread.
 I've, up to this point, been using threading.Thread, but from what I
 read multiprocess will allow multiple processors to be used
  From the python docs on multiprocessing.
Due to this, the multiprocessing module allows the programmer to fully
 leverage multiple processors on a given machine.

 I have run into an issue when modifying the thread object from the run
 method. Threading.thread allows me to change an attribute in the run
 method and it hold while multiprocessing.Process loses it.


I am not a multiprocessing expert, but I think the problem you
are having is that Process is running your code in a separate
process, so there is no way you could see those object changes
in your main line code.

In other words, Process is not an exact replacement for Thread.
If you need to communicate between the different parts, you
would want to use the abstractions provided by Queue or Pipe.


Keep reading down the multiprocessing page in the docs until
you get to Exchanging objects between processes:
http://docs.python.org/library/multiprocessing.html#exchanging-objects-between-processes


Sharing state between processes seems like it will be especially
relevant to what you are doing:
http://docs.python.org/library/multiprocessing.html#sharing-state-between-processes

Basically, it says don't do that  :o)


 Here is an example illustrating the inconsistency that I've seen.

One thing that would help here is a sample of what output
you get from your code, and what you were hoping to get.

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


Re: list(), tuple() should not place at Built-in functions in documentation

2011-07-15 Thread Stefan Behnel

Terry Reedy, 15.07.2011 05:00:

On 7/14/2011 9:51 PM, Ben Finney wrote:

Steven D'Aprano writes:


Inside wrote:


As telling in the subject,because list and tuple aren't functions,they
are types.Is that right?


At one time (before 2.2), they were functions and not classes.


They are still functions in the sense that you can call them (with or 
without arguments) and get a result back. The exact distinction can be 
considered an implementation detail in most contexts.


There are even extreme cases that render the distinction completely 
useless. Think of type(), for example. In its exceedingly most common use 
case, it does *not* create a type, even if it's a call to a type 
constructor. Something similar applies to a no-args call to tuple(), which 
does not create a new object in CPython, but only returns a new reference 
to a singleton.


Types in Python are allowed to do these things, so it's not always 
meaningful to distinguish between typeX() being a call to a type or a function.




Yes they are types. But they can still be used as functions. Does it
matter?


As a newcomer to the documentation I looked fruitlessly in the table of
contents for a section that would contain the built-in types. “Built-in
functions” was eliminated for the reason the OP states.

I think it matters. (But I haven't proposed a documentation patch for it.)


I once proposed, I believe on the tracker, that 'built-in functions' be
expanded to 'built-in function and classes'. That was rejected on the basis
that people would then expect the full class documentation that is in the
'built-in types' section (which could now be called the built-isssn classes
section.

A more exact title would be 'built-in callables', but that would be even
less helpful to newcomers. Callables are functions in the generic sense.


I think function is about the best expected and newcomer-friendly 
name one can give to a callable, especially in the context of a 
duck-typed, protocol-oriented language like Python. The section title in 
question describes perfectly its contents.


It's a different question if a separate section like here's a list of 
reference to the descriptions of types that Python provides in its 
builtins is required. But I think we have that already.




In any case, the new index makes it easy to see what is in that chapter.


Agreed.

Stefan

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


Re: Possible File iteration bug

2011-07-15 Thread Ethan Furman

Billy Mays wrote:
A sentinel does provide a work around, but it also passes the problem 
onto the caller rather than the callee


The callee can easily take care of it -- just block until more is ready. 
 If blocking is not an option, then the caller has to deal with it no 
matter how callee is implemented -- an exception, a sentinel, or some 
signal that says nope, nothing for ya! try back later!


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


Re: Python ++ Operator?

2011-07-15 Thread Stefan Behnel

Chris Angelico, 15.07.2011 10:06:

2011/7/15 Björn Lindqvist:

Pre and post-increments are
almost always confusing unless they are used as the counter-variable
inside for-loops.


I agree that they're often confusing (i+j) but there are several
places where they're handy.

array[count++]=value;

or the more direct pointer management:

*ptr++=value;


More direct, sure. But readable? Well, only when you know what this 
specific pattern does. If you have to think about it, it may end up hurting 
your eyes before you figure it out.


Stefan

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


Re: Python threading/multiprocessing issue.

2011-07-15 Thread Brandon Harris
I see. Well I was hoping to see the same result in the multiprocessing 
example as using the threading example. What you pointed out makes sense 
though, but what I don't understand is how modifying the queue in the 
example works fine in both. Possibly it was designed for that kind of use?


Brandon L. Harris


On 07/15/2011 03:55 PM, Lee Harr wrote:

I'm working on a tool that runs a number of process is separate thread.
I've, up to this point, been using threading.Thread, but from what I
read multiprocess will allow multiple processors to be used
   From the python docs on multiprocessing.
Due to this, the multiprocessing module allows the programmer to fully
  leverage multiple processors on a given machine.

I have run into an issue when modifying the thread object from the run
method. Threading.thread allows me to change an attribute in the run
method and it hold while multiprocessing.Process loses it.


I am not a multiprocessing expert, but I think the problem you
are having is that Process is running your code in a separate
process, so there is no way you could see those object changes
in your main line code.

In other words, Process is not an exact replacement for Thread.
If you need to communicate between the different parts, you
would want to use the abstractions provided by Queue or Pipe.


Keep reading down the multiprocessing page in the docs until
you get to Exchanging objects between processes:
http://docs.python.org/library/multiprocessing.html#exchanging-objects-between-processes


Sharing state between processes seems like it will be especially
relevant to what you are doing:
http://docs.python.org/library/multiprocessing.html#sharing-state-between-processes

Basically, it says don't do that  :o)



Here is an example illustrating the inconsistency that I've seen.

One thing that would help here is a sample of what output
you get from your code, and what you were hoping to get.




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


Re: None versus MISSING sentinel -- request for design feedback

2011-07-15 Thread Terry Reedy

On 7/15/2011 6:19 AM, Steven D'Aprano wrote:

Use None as default. Requiring users to use your special value would be 
a nuisance. They may have data prepared separately from your module.



Rob Williscroft wrote:



MISSING = MissingObject()
def mean( sequence, missing = MISSING ):


This is also a good idea.


So you think the right API is to allow the caller to specify what counts as
a missing value at runtime? Are you aware of any other statistics packages
that do that?


AFAIK, standard feature on major packages. BMDP, SAS and SPSS as I 
remember. Missing values could be specified on a per column basis.


--
Terry Jan Reedy

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


Re: Possible File iteration bug

2011-07-15 Thread Terry Reedy

On 7/15/2011 8:26 AM, Billy Mays wrote:

On 07/15/2011 04:01 AM, bruno.desthuilli...@gmail.com wrote:

On Jul 14, 9:46 pm, Billy Maysno...@nohow.com wrote:

I noticed that if a file is being continuously written to, the file
generator does not notice it:

def getLines(f):
lines = []
for line in f:
lines.append(line)
return lines


what's wrong with file.readlines() ?


Using that will read the entire file into memory which may not be


So will getLines.


possible. In the library reference, it mentions that using the generator
(which calls file.next()) uses a read ahead buffer to efficiently loop
over the file. If I call .readline() myself, I forfeit that performance
gain.


Are you sure? Have you measured the difference?

--
Terry Jan Reedy

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


Re: difflib-like library supporting moved blocks detection?

2011-07-15 Thread Vlastimil Brom
2011/7/14 Chris Torek nos...@torek.net:
 In article mailman.1002.1310591600.1164.python-l...@python.org
 Vlastimil Brom  vlastimil.b...@gmail.com wrote:
I'd like to ask about the availability of a text diff library, like
difflib, which would support the detection of moved text blocks.

 If you allow arbitrary moves, the minimal edit distance problem
 (string-to-string edit) becomes substantially harder.  If you only
 allow insert, delete, or in-place-substitute, you have what is
 called the Levenshtein distance case.  If you allow transpositions
 you get Damerau-Levenshtein.  These are both solveable with a
 dynamic programming algorithm.  Once you allow move operations,
 though, the problem becomes NP-complete.

 See http://pages.cs.brandeis.edu/~shapird/publications/JDAmoves.pdf
 for instance.  (They give an algorithm that produces usually
 acceptable results in polynomial time.)
 --
 In-Real-Life: Chris Torek, Wind River Systems


Thanks for the references and explanation!
I do realise the added complexity with taking the moves into account;
given that, my current needs and the usually satisfying results
obtained easily with difflib, I am not going to try to implement some
more complex diffing algorithm.
However, it turns out that the mentioned naive approach with just
recomparing the text additions and removals may be partially viable -
with some luck, i.e. given, the relevant segments are identified as
deletions and inserts and isolated by difflib in the first place (and
not subsumed under larger changes or split).

For illustration, the rough simplified code is attached (sorry for the
style and possible quirks...)
Just now the more similar text segments are just collected, it would
be also possible to sort them on their similarity ratio; the current
approach also allows to highlight potentially multiple moved segments.

Comments and suggestions are, of course, welcome,
regards,
 vbr

# # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #
# # # # # # # # # # # # # # # # # # # # #

#! Python
# -*- coding: utf-8 -*-

import difflib
import itertools

def compare_moves(a, b, similarity_threshold=0.6):

Poor man's text comparison with simple moves check. Compares two
strings using difflib
and additionally tries to detect moved blocks
by comparing similar deleted and inserted segments with each other
- given the similarity_threshold.


seq_matcher = difflib.SequenceMatcher(isjunk=None, a=a, b=b, autojunk=False)
diff_raw = [[tag, i1, i2, j1, j2, a[i1:i2], b[j1:j2]] for tag, i1,
i2, j1, j2 in seq_matcher.get_opcodes()]

deleted, inserted = {}, {}
for tag, i1, i2, j1, j2 in seq_matcher.get_opcodes():
if tag == 'delete':
deleted[(i1, i2)] = [tag, i1, i2, j1, j2, a[i1:i2]]
elif tag == 'insert':
inserted[(i1, i2)] = [tag, i1, i2, j1, j2, b[j1:j2]]

possibly_moved_blocks = []
for deleted_item, inserted_item in
itertools.product(deleted.values(), inserted.values()):
similarity_ratio = difflib.SequenceMatcher(isjunk=None,
a=deleted_item[5], b=inserted_item[5], autojunk=False).ratio()
if similarity_ratio = similarity_threshold:
possibly_moved_blocks.append([deleted_item, inserted_item,
similarity_ratio])

print diff_raw
print possibly_moved_blocks


if __name__ == __main__:
compare_moves(abcXYZdeABfghijklmnopABBCq,
ABCDabcdeACfgXYXZhijklmnopq, similarity_threshold = 0.6)

# # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #
# # # # # #
# output: #
[['insert', 0, 0, 0, 4, '', 'ABCD'], ['equal', 0, 3, 4, 7, 'abc',
'abc'], ['delete', 3, 6, 7, 7, 'XYZ', ''], ['equal', 6, 9, 7, 10,
'deA', 'deA'], ['replace', 9, 10, 10, 11, 'B', 'C'], ['equal', 10, 12,
11, 13, 'fg', 'fg'], ['insert', 12, 12, 13, 17, '', 'XYXZ'], ['equal',
12, 21, 17, 26, 'hijklmnop', 'hijklmnop'], ['delete', 21, 25, 26, 26,
'ABBC', ''], ['equal', 25, 26, 26, 27, 'q', 'q']]

[[['delete', 21, 25, 26, 26, 'ABBC'], ['insert', 0, 0, 0, 4, 'ABCD'],
0.75], [['delete', 3, 6, 7, 7, 'XYZ'], ['insert', 12, 12, 13, 17,
'XYXZ'], 0.8571428571428571]]


compare_moves.py
Description: Binary data
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Possible File iteration bug

2011-07-15 Thread Terry Reedy

On 7/15/2011 10:42 AM, Billy Mays wrote:

On 07/15/2011 10:28 AM, Thomas Rachel wrote:

Am 15.07.2011 14:52 schrieb Billy Mays:



Really what would be useful is some sort of PauseIteration Exception
which doesn't close the generator when raised, but indicates to the
looping header that there is no more data for now.


a None or other sentinel value would do this as well (as ChrisA already
said).



A sentinel does provide a work around, but it also passes the problem
onto the caller rather than the callee:


No more so than a new exception that the caller has to recognize.

--
Terry Jan Reedy

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


abort python script from trace function

2011-07-15 Thread Dave Stark
Hello,
I have a multithreaded application that uses embedded python extensively.  The 
main thread creates python objects that interface test equipment, and users 
execute their own python scripts that run in a separate thread.  The users need 
to be able to pause/resume/abort a script that is currently running.  I 
registered a Py_tracefunc to check the state of the pause/abort buttons from 
the GUI, and pause/resume are working great.  The trouble is that I cannot 
figure out how to implement an abort in my trace function.  I can set 
exceptions use PyErr_SetString or PyThreadState_SetAsyncExc, but these just 
raise exceptions.  If the script is in a block that defines a try/finally the 
exception will be caught, not what I want.  In searching similar topics I saw 
references to PyErr_SetInterrupt, but this had no visible effect at all on my 
application.  I don't want to call Py_Finalize and shut down the whole 
interpreter, because I would lose the Python objects created in the main 
thread.  An uncatchable exception would be ideal.

My embedded python version is:
'2.6.5 (r265:79063, Mar 20 2010, 14:22:52) [MSC v.1500 32 bit (Intel)]'

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


Re: Looking for general advice on complex program

2011-07-15 Thread Cameron Simpson
On 15Jul2011 16:03, Billy Mays 
81282ed9a88799d21e77957df2d84bd6514d9...@myhashismyemail.com wrote:
| I remember reading that file locking doesn't work on network mounted
| drives (specifically nfs mounts), but you might be able to simply
| create a 'lock' (mydoc.xml.lock or the like) file for the XML doc in
| question.  If that file exists you could either hang or silently
| give up.  Not sure if that helps.

There are two approaches to this. Plain old make-a-file won't work - it
is racy (and as mentioned, you can't rely on the various lock
facilities).

You can create a file while your umask is 0777; it will be non-writable
immediately (no chmod required), preventing another attempt to make it.

My personal habit is to make a directory for the lock; mkdir
also can't happen twice to the same name, you don't need to fiddle you
umask (racy and annoying, and problematic if you're using multiple
threads), _and_ you can put meta info inside it, like pid files etc.

Cheers,
-- 
Cameron Simpson c...@zip.com.au DoD#743
http://www.cskk.ezoshosting.com/cs/

I had a wierd dream with Ken Thompson in it once.
- George Politis geo...@research.canon.com.au
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Python ++ Operator?

2011-07-15 Thread Chris Angelico
On Sat, Jul 16, 2011 at 6:26 AM, Dan Stromberg drsali...@gmail.com wrote:

 I don't regard this as a low level versus VHLL issue - I regard it as a
 matter of operators with side effects being too error prone.  Adding such
 operators to Python has been discussed (it'd almost certainly be easy to
 add), and rejected.

It's not that it has or has not, due to its highness of level, but
more a needs or needs not. In Python, iterating over an array is done
with a for loop and the array's own iterator (or enumerate() if you
need the indices), but C doesn't have iterators, so it needs a
convenient notation for incrementing through the array.

 BTW, array operations optimize to the same thing as pointer arithmetic in
 most C compilers, but the latter tends to be less clear.

I'm not fully convinced; there are many times when incrementing
pointers allows for much cleaner code. However, we are talking about
the readability of C among Python programmers. Personally, I find
pointer-dereference-and-post-increment to be perfectly readable, but
it's a construct that I use practically on a daily basis. To someone
who's not familiar with Python, list comps could suffer from the same
issues - what does THIS do? oh.

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


Re: Proposal to extend PEP 257 (New Documentation String Spec)

2011-07-15 Thread Chris Angelico
On Sat, Jul 16, 2011 at 4:56 AM, rantingrick rantingr...@gmail.com wrote:
 Hmm, that's strange considering that code MUST be formatted in certain
 ways or you get a syntax error (indention, colons, parenthesis, etc,
 etc). I don't hear the masses claiming that they are going over to
 Ruby simply because of indention.

Not Ruby, but to other languages. There's this guy in my house named
Chris who tries his best to avoid Python if the code is going to be
shared over any dodgy medium where indentation might be damaged.
There are plenty of other languages that he can use... oh wait, that's
me. Yes, I frequently avoid Python specifically because of its
indentation issues. Does that mean I never use Python? No. Does it
mean I don't post here? Obviously not. Does it mean that further
restrictions can automatically be grandfathered in because there are
already restrictions like this? No.

 In my mind doc-strings should ALWAYS be optional HOWEVER if the
 programmer decides to create a doc-string THEN he must observe some
 syntax rules or his code will throw an SyntaxError. Remember, freedom
 is good, unbridled freedom is the root of all evil.

1) Every syntax element MUST add indentation.
2) Strong encouragement to stick to an 80-character line
Conclusion: Every big function will become two smaller functions, one
of which calls the other.
3) Every function that has a docstring MUST have it fully formatted.
Secondary conclusion: The only functions with docstrings are the ones
that are meant to be called externally.

In other words, docstrings will define the module's interface, and
there'll be a whole lot of utterly undocumented functions because they
didn't merit this massive structured docstring, and it became way way
too much work to factor things out. Either that, or people will just
start ignoring the 80 character limit, but I'm sure you could make
that mandatory - and that one would actually improve some things,
because any program that wants to read Python code needs only allocate
an 81-character buffer.

 So what's so terrible about structure Chris? Nobody's freedom are
 being taken away. You don't HAVE to create doc-strings, just like you
 don't HAVE to code with Python (you do free form formatting Ruby).
 Python is a language that is meant to be clean. Forced indention makes
 that possible. Forced doc-string syntax will complete the circle.

Python was also meant to be a programming language. Programming
languages offer freedom to their users. Without that freedom, it's not
a programming language but a script for another program... such things
can be useful, but are not the same.

This is not a true programming language:
http://www.kongregate.com/games/Coolio_Niato/light-bot

It's a reasonably fun game, but specifically _because_ it's so
restrictive. That is NOT what I want from a programming language or
even a scripting language.

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


Is there a Programming FAQ for Python 3.x?

2011-07-15 Thread Richard D. Moores
Is there a Programming FAQ for Python 3.x? There is
http://docs.python.org/faq/programming.html, but it's for 2.7

Thanks,

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


Type checking versus polymorphism (was: list(), tuple() should not place at Built-in functions in documentation)

2011-07-15 Thread Ben Finney
Inside fancheyuj...@gmail.com writes:

 I just follow some coding rules of me:
 1. Controlling input strictly.

Asserting that the input is of a specific type is too strict. Does your
code work if the input is not a list or tuple?

I suspect strongly that the answer is yes, it works fine with any
sequence, even ones that don't inherit from ‘list’ nor ‘tuple’. It will
probably work with any sequence; it amy even work with any iterable.

Instead of insisting on specific types, you should support polymorphism:
expect *behaviour* and allow any input that exhibits that behaviour.

This is known as “duck typing”: you don't need to care whether it's a
duck, you just need to know whether it walks like a duck and quacks like
a duck. If it turns out to be a goose, but that won't affect your code,
you shouldn't care.

 2. In a function keep doubting on its parameters until they're
 checked.

This is called “Look Before You Leap” (LBYL) programming, and is
generally considered not Pythonic.

Rather, “it is Easier to Ask Forgiveness than Permission” (EAFP) is the
Python programming style, and fits with its widespread reliance on
polymorphism (including “duck typing”).

Accept the input, and use it as though it has the correct behaviour –
without regard to what type is providing that behaviour.

If it doesn't have the expected behaviour, either the type will complain
(raising an exception that you can handle at an appropriate level in
your code), or your comprehensive unit tests will detect the
misbehaviour.

If you don't have comprehensive unit tests, that's where you should put
your effort of strict interface testing. Not type assertions in the
application code.

 3. Let potential errors raise as early as possible.

That is good practice. But you should not compromise polymorphism to
that.

-- 
 \“The whole area of [treating source code as intellectual |
  `\property] is almost assuring a customer that you are not going |
_o__)   to do any innovation in the future.” —Gary Barnett |
Ben Finney
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: list(), tuple() should not place at Built-in functions in documentation

2011-07-15 Thread Chris Angelico
On Sat, Jul 16, 2011 at 5:53 AM, Inside fancheyuj...@gmail.com wrote:
 def add(users):
    assert(users, (tuple, list))
    #If necessary I'll also check every obj in the sequence to see whether 
 it's a User.

 I just follow some coding rules of me:
 1. Controlling input strictly.
 2. In a function keep doubting on its parameters until they're checked.
 3. Let potential errors raise as early as possible.

What you're doing there is writing code in Python, not writing Python code.

To be more Pythonic, your code should actually stop caring about
whether something is-a User, and instead simply care about whether or
not it can be treated as a User. (And for Travaglia fans, yes, a User
object WILL have an abuse() method.)

Instead of asserting that the parameter is a User, just add it
cheerfully to your list, and then when you iterate over the list and
call some method on each one, you'll get an exception if one of them
doesn't have that method.

This allows a huge enhancement to polymorphism, in that you no longer
need to worry about what your pointers are; in C++, you can run over a
list of users and ask if they're all Students, but then you need to
cast all those pointers if you're going to then ask them all what
subjects they're studying. In Python, all you do is ask your list of
objects what subjects they're studying - all the students will
respond, and anything that doesn't know what studying is will throw
an exception.

The analogy with reality breaks down a bit here. I've seen plenty of
students with no idea of what it means to study. But Python can handle
that too - just 'del' the method in the subclass.

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


Re: Python threading/multiprocessing issue.

2011-07-15 Thread Chris Angelico
2011/7/16 Lee Harr miss...@hotmail.com:
 I am not a multiprocessing expert, but I think the problem you
 are having is that Process is running your code in a separate
 process, so there is no way you could see those object changes
 in your main line code.

 In other words, Process is not an exact replacement for Thread.


That's correct; inter-process communication is the realm of sockets
(network or Unix), pipes, signals, etc - but inter-thread
communication is a matter of making sure you don't tread on each
other's toes. In CPython, the latter is guaranteed by the GIL; the
specific advantage of multiprocessing over threading is that each
process has a separate GIL, and that's because all variables are
separate.

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


Re: Is there a Programming FAQ for Python 3.x?

2011-07-15 Thread Thomas Jollans
On 07/16/2011 01:24 AM, Richard D. Moores wrote:
 Is there a Programming FAQ for Python 3.x? There is
 http://docs.python.org/faq/programming.html, but it's for 2.7
 
 Thanks,
 
 Dick Moores

http://docs.python.org/py3k/faq/index.html
http://docs.python.org/py3k/faq/programming.html
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: abort python script from trace function

2011-07-15 Thread Chris Angelico
On Sat, Jul 16, 2011 at 8:00 AM, Dave Stark david.st...@intusurg.com wrote:
 Hello,

 I have a multithreaded application that uses embedded python extensively.
 The main thread creates python objects that interface test equipment, and
 users execute their own python scripts that run in a separate thread.

I did something extremely similar (but without the threading), and was
majorly burnt. Poke around on the archives and you'll find the extent
to which I (and my boss) got egg on our faces; I had thought that it
would be possible to sandbox Python enough for a user to be able to
submit code. It's not. If you're fortunate, someone from this list
will create a file in /tmp, read it back, and then email you showing
how easy it was to do. If you're not, it'll be utter and complete
p0wnage.

PyErr_SetInterrupt() raises KeyboardInterrupt. It works fine, as long
as the script catches that. I had the same issues with my system; I
wanted to administratively guarantee that the script WOULD NOT take
more than X ms of CPU time. Since a Python script can catch
KeyboardInterrupt, it could ignore the watchdog timer. In the end, I
created a second watchdog that, if triggered, would longjmp straight
out past all the Python code and back to a basic cleanup-and-terminate
routine.

There's no way, currently, to make an uncatchable exception. I already
asked. The general response is (and I should have listened, instead of
muffling on and hoping that we could sandbox Python enough to get
by) that Python is not the right language for that sort of job. I was
advised to try Javascript/ECMAScript, and Google's V8 engine is fairly
good; not perfect, but decent. Alternatively, Lua works well, but it's
a lot more effort to embed (especially if you want heavy interfacing
between the script code and your application code - everything's done
with a stack).

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


Re: Is there a Programming FAQ for Python 3.x?

2011-07-15 Thread Richard D. Moores
On Fri, Jul 15, 2011 at 16:39, Thomas Jollans t...@jollybox.de wrote:
 On 07/16/2011 01:24 AM, Richard D. Moores wrote:
 Is there a Programming FAQ for Python 3.x? There is
 http://docs.python.org/faq/programming.html, but it's for 2.7

 Thanks,

 Dick Moores

 http://docs.python.org/py3k/faq/index.html

Excellent! I was going from http://docs.python.org/py3k/index.html
where I chose FAQs from the left column.

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


Re: Looking for general advice on complex program

2011-07-15 Thread Chris Angelico
On Sat, Jul 16, 2011 at 8:37 AM, Cameron Simpson c...@zip.com.au wrote:
 There are two approaches to this.
 You can create a file while your umask is 0777... [or]
 My personal habit is to make a directory for the lock

Both viable options; I'd be inclined toward the second. Or, here's a
third option. Instead of writing to a shared network drive, submit
requests on a TCP socket direct to the monitor program. Spin off a
thread that does this:

* Wait for incoming socket connection
* Set overall cutoff timer; if (say) 2 seconds pass, kill the connection
* Authenticate the client (if applicable)
* Accept the update data, sanitize if necessary
* Write the file to disk
* Notify the XManager
* Loop.

Do all this on *one thread* and then you eliminate all race
conditions. Good use of a TCP listen queue and the cutoff timer will
mean that applications aren't actually kept waiting, but they're still
rigidly locked into a queue - depending on how frequent your updates
are, this could be a problem. If you need simultaneous updates, spin
off a new thread for each socket connection, and then use something
simple like a mapping of file name to semaphore to ensure no two try
to update the same file at once.

By moving the actual file read/writes to a single computer, you
simplify the task of notifying the parent. In fact, if there's only
one process that needs to be made aware of the change, the job's even
easier - all you need to do is change a variable or call a method or
whatever it be, right there in the socket handler.

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


Print encoding problems in console

2011-07-15 Thread Pedro Abranches
Hello everyone.

I'm having a problem when outputing UTF-8 strings to a console.
Let me show a simple example that explains it:

$ python -c 'import sys; print sys.stdout.encoding; print u\xe9'
UTF-8
é

It's everything ok.
Now, if you're using your python script in some shell script you might have
to store the output in some variable, like this:

$ var=`python -c 'import sys; print sys.stdout.encoding; print u\xe9'`

And what you get is:

Traceback (most recent call last):
  File string, line 1, in module
UnicodeEncodeError: 'ascii' codec can't encode character u'\xe9' in position
0: ordinal not in range(128)

So, python is not being able to detect the encoding of the output in a
situation like that, in which the python script is called not directly but
around ``.

Why does happen? Is there a way to solve it either by python or by shell
code?

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


Liskov substitution principle (was: list(), tuple() should not place at Built-in functions in documentation)

2011-07-15 Thread Ben Finney
Chris Angelico ros...@gmail.com writes:

 The analogy with reality breaks down a bit here. I've seen plenty of
 students with no idea of what it means to study. But Python can handle
 that too - just 'del' the method in the subclass.

No, please don't. That would break the Liskov substitution principle
URL:https://secure.wikimedia.org/wikipedia/en/wiki/Liskov_substitution_principle.

By inheriting from a type that provides a method, you're promising that
you will implement at least as much behaviour as the parent. If that
includes a ‘study’ method, then your subclass must also implement (or
inherit) that method.

Code can then be written to expect that, so long as the object inherits
from Student, it will at least have the same minimum level of behaviour
that a Student has.

If you inherit from Student, but delete the ‘study’ method, you would
break any code which assumes any Student has a ‘study’ method –
something that was explicitly promised in the API of Student.

Since you are advocating calling people students when they don't study,
it sounds instead like you want a different set of promises:

class Student(Person):
 An enrolled student at this institution. 

def study(self, subject):
raise NotImplementedError

class LectureAttendee(Student):
 Someone who comes to lectures. 

def attend(self, lecture):
pass

class StudentWhoActuallyStudies(Student):
 A student who actually is capable of studying. 

def study(self, subject):
 Actually apply my knowledge of how to study. 

alice = StudentWhoActuallyStudies(Alice)
bob = Student(Bob)

alice.study(chemistry)  # actual study
bob.study(garden gnome painting)  # not implemented!

Now both Alice and Bob fulfil the technical requirements of a student at
the institution, but the expectations of study capability are clear.

Any code using this implementation of Student knows that, if they want a
student who actually studies, they'd better ask for a more specific
type.

See? We can have overstretched analogies *and* remain within the Liskov
substitution principle.

-- 
 \   Eccles: “I just saw the Earth through the clouds!”  Lew: “Did |
  `\  it look round?”  Eccles: “Yes, but I don't think it saw me.” |
_o__)—The Goon Show, _Wings Over Dagenham_ |
Ben Finney
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Liskov substitution principle (was: list(), tuple() should not place at Built-in functions in documentation)

2011-07-15 Thread Chris Angelico
On Sat, Jul 16, 2011 at 10:04 AM, Ben Finney ben+pyt...@benfinney.id.au wrote:
        def study(self, subject):
            raise NotImplementedError

 See? We can have overstretched analogies *and* remain within the Liskov
 substitution principle.


Hehe! Of course I was speaking utterly in jest, but this raises
(sorry, never could resist a bad pun) another question: What if the
base class implemented study(), and then LazyStudent subclasses
Student but makes study() raise NotImpl? Would that break things? In a
sense, it breaks the whole this is a student so it should act like a
student rule. Suppose it raised UtterApathyError instead - does that
break the LSP?

Chris A
PS. The world's first horseless signature... trapped in the air!
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Print encoding problems in console

2011-07-15 Thread Dan Stromberg
I've used the code below successfully to deal with such a problem when
outputting filenames.  Python2x3 is at
http://stromberg.dnsalias.org/svn/python2x3/ , but here it's just being used
to convert Python 3.x's byte strings to strings (to eliminate the b''
stuff), while on 2.x it's an identity function - if you're targeting 3.x
alone, there's no need to take a dependency on python2x3.

If you really do need to output such characters, rather than replacing them
with ?'s, you could use os.write() to filedescriptor 1 - that works in both
2.x and 3.x.

def ascii_ize(binary):
   '''Replace non-ASCII characters with question marks; otherwise writing to
sys.stdout tracebacks'''
   list_ = []
   question_mark_ordinal = ord('?')
   for ordinal in python2x3.binary_to_intlist(binary):
  if 0 = ordinal = 127:
 list_.append(ordinal)
  else:
 list_.append(question_mark_ordinal)
   return python2x3.intlist_to_binary(list_)


def output_filename(filename, add_eol=True):
   '''Output a filename to the tty (stdout), taking into account that some
tty's do not allow non-ASCII characters'''

   if sys.stdout.encoding == 'US-ASCII':
  converted = python2x3.binary_to_string(ascii_ize(filename))
   else:
  converted = python2x3.binary_to_string(filename)

   replaced = converted.replace('\n', '?').replace('\r', '?').replace('\t',
'?')

   sys.stdout.write(replaced)

   if add_eol:
  sys.stdout.write('\n')


On Fri, Jul 15, 2011 at 5:02 PM, Pedro Abranches pedrof.abranc...@gmail.com
 wrote:

 Hello everyone.

 I'm having a problem when outputing UTF-8 strings to a console.
 Let me show a simple example that explains it:

 $ python -c 'import sys; print sys.stdout.encoding; print u\xe9'
 UTF-8
 é

 It's everything ok.
 Now, if you're using your python script in some shell script you might have
 to store the output in some variable, like this:

 $ var=`python -c 'import sys; print sys.stdout.encoding; print u\xe9'`

 And what you get is:

 Traceback (most recent call last):
   File string, line 1, in module
 UnicodeEncodeError: 'ascii' codec can't encode character u'\xe9' in
 position 0: ordinal not in range(128)

 So, python is not being able to detect the encoding of the output in a
 situation like that, in which the python script is called not directly but
 around ``.

 Why does happen? Is there a way to solve it either by python or by shell
 code?

 Thanks,
 Pedro Abranches

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


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


Re: Print encoding problems in console

2011-07-15 Thread Andrew Berg
-BEGIN PGP SIGNED MESSAGE-
Hash: RIPEMD160

On 2011.07.15 07:02 PM, Pedro Abranches wrote:
 Now, if you're using your python script in some shell script you
 might have to store the output in some variable, like this:
 
 $ var=`python -c 'import sys; print sys.stdout.encoding; print
 u\xe9'`
 
 And what you get is:
 
 Traceback (most recent call last): File string, line 1, in
 module UnicodeEncodeError: 'ascii' codec can't encode character
 u'\xe9' in position 0: ordinal not in range(128)
 
 So, python is not being able to detect the encoding of the output in
 a situation like that, in which the python script is called not
 directly but around ``.
FWIW, it works for me with Python 3:
$ x=$(/c/Python32/python -c print\(\'\\xe9\'\))

$ echo $x
é

I don't know how to get it to work with more than one command to Python;
bash always thinks the next commands are for it:
$ x=$(/c/Python32/python -c import sys; print\(sys.output.encoding\);
print\(\'\\xe9\'\))
  File string, line 1
import
 ^
SyntaxError: invalid syntax
bash: print(sys.output.encoding): command not found
bash: print('\xe9'): No such file or directory

This is using a very old MinGW bash, though.
- -- 
CPython 3.2.1 | Windows NT 6.1.7601.17592 | Thunderbird 5.0
PGP/GPG Public Key ID: 0xF88E034060A78FCB
-BEGIN PGP SIGNATURE-
Version: GnuPG v1.4.11 (MingW32)
Comment: Using GnuPG with Mozilla - http://enigmail.mozdev.org/

iQEcBAEBAwAGBQJOIOEIAAoJEPiOA0Bgp4/LbSIIAJS9hVMTwQtV17pxWU5/IwRa
0X5v3W8mKZAyXTCSL5HmMQ07pPWRAkg5dEmnt+MTmFOVRjWg1yWIzeArmAc/MCmj
LiQcwp9ue6rY7Gt+gUqLFMQgVW9qs4zLLRAcThw9zMVLheOCrVoDc6miyLqcpb8+
RPjVuT9Bd5Vj67lIPOtZNTdB0hZGSwF5maerkot/95NBIuvP8UVBcub3dI6w1bJL
7dIW3NmjkeuWOdRch5s/X+gdPuoBNpfLfsFW3t7sdUscKKWaVjj0tOiNMHne42hD
XFuFauzmizaKpu16Zn9YJGPUhfvCn8QW+mcPFlBzv3g2oxuZMMssFykhU4Yb/7E=
=jVgu
-END PGP SIGNATURE-
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Liskov substitution principle

2011-07-15 Thread Ben Finney
Chris Angelico ros...@gmail.com writes:

 What if the base class implemented study(), and then LazyStudent
 subclasses Student but makes study() raise NotImpl? Would that break
 things? In a sense, it breaks the whole this is a student so it
 should act like a student rule.

That would break the Liskov substitution principle, yes. Anything that
asks for a Student instance should receive an object that can do at
least everything Student can do.

If you want to implement something that can't do some of the things
Student can do, the Liskov substitution principle says you are
implementing some object that does *not* inherit from Student.

This fits nicely with the principle (whose name I'm currently too lazy
to look up) that says you should only use inheritance for “IS-A”
relationships. If LazyStudent can't do everything Student can do, then
it's false to say LazyStudent IS-A Student, so using inheritance for
that would be to state something in code which isn't true.

 Suppose it raised UtterApathyError instead - does that break the LSP?

Probably, yes. Your point is taken though: it's not something that can
be drawn along a bright line.

-- 
 \ “Those are my principles. If you don't like them I have |
  `\others.” —Groucho Marx |
_o__)  |
Ben Finney
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: None versus MISSING sentinel -- request for design feedback

2011-07-15 Thread Gregory Ewing

Ethan Furman wrote:
some 
of the return values (Logical, Date, DateTime, and probably Character) 
will have their own dedicated singletons (Null, NullDate, NullDateTime, 
NullChar -- which will all compare equal to None)


That doesn't seem like a good idea to me. It's common practice
to use 'is' rather than '==' when comparing things to None.

Why do you want to use special null values for these types?

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


Re: Type checking versus polymorphism (was: list(), tuple() should not place at Built-in functions in documentation)

2011-07-15 Thread Steven D'Aprano
Ben Finney wrote:

[...snip explanation of duck-typing...]
 If you don't have comprehensive unit tests, that's where you should put
 your effort of strict interface testing. Not type assertions in the
 application code.

I agree with everything Ben said here, but he has missed something even more
fundamental.

Type *checking* breaks duck-typing. Type *assertions* are even worse,
because assertions aren't guaranteed to run. If you are using assert
isinstance(...) to validate input data, there are situations where your
validation step does not happen at all, and your code may just break in the
least convenient way. So not only are you validating the wrong way, but
sometimes you don't validate at all!

Assertions are for testing internal program logic, not for validation.

(I don't even like using assert for testing. How do you test your code with
assertions turned off if you use assert for testing?)



-- 
Steven

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


Re: Possible File iteration bug

2011-07-15 Thread Steven D'Aprano
Billy Mays wrote:

 I was thinking that a convenient solution to this problem would be to
 introduce a new Exception call PauseIteration, which would signal to the
 caller that there is no more data for now, but not to close down the
 generator entirely.

It never fails to amuse me how often people consider it convenient to add
new built-in functionality to Python to solve every little issue. As
pie-in-the-sky wishful-thinking, it can be fun, but people often mean it to
be taken seriously.

Okay, we've come up with the solution of a new exception, PauseIteration,
that the iterator protocol will recognise. Now we have to:

- write a PEP for it, setting out the case for it;
- convince the majority of CPython developers that the idea is a good one,
  which might mean writing a proof-of-concept version;
- avoid having the Jython, IronPython and PyPy developers come back and say
  that it is impossible under their implementations;
- avoid having Guido veto it;
- write an implementation or patch adding that functionality;
- try to ensure it doesn't cause any regressions in the CPython tests;
- fix the regressions that do occur despite our best efforts;
- ensure that there are no backwards compatibility issues to be dealt with;
- write a test suite for it;
- write documentation for it;
- unless we're some of the most senior Python developers, have the patch
  reviewed before it is accepted;
- fix the bugs that have come to light since the first version;
- make sure copyright is assigned to the Python Software Foundation;
- wait anything up to a couple of years for the latest version of Python,
  including the patch, to be released as production-ready software;
- upgrade our own Python installation to use the latest version, if we can
  and aren't forced to stick with an older version

and now, at long last, we can use this convenient feature in our own code!
Pretty convenient, yes?

(If you think I exaggerate, consider the yield from construct, which has
Guido's support and was pretty uncontroversial. Two and a half years later,
it is now on track to be added to Python 3.3.)

Or you can look at the various recipes on the Internet for writing tail-like
file viewers in Python, and solve the problem the boring old fashioned way.
Here's one that blocks while the file is unchanged:

http://lethain.com/tailing-in-python/

Modifying it to be non-blocking should be pretty straightforward -- just add
a `yield ` after the `if not line`.



-- 
Steven

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


Re: Possible File iteration bug

2011-07-15 Thread Chris Angelico
On Sat, Jul 16, 2011 at 1:42 PM, Steven D'Aprano
steve+comp.lang.pyt...@pearwood.info wrote:
 Okay, we've come up with the solution of a new exception, PauseIteration,
 that the iterator protocol will recognise. Now we have to:

 - write an implementation or patch adding that functionality;


- and add it to our own personal builds of Python, thus bypassing the
entire issue of getting it accepted into Python. Of course, this does
mean that your brilliant code only works on your particular build of
Python, but I'd say that this is the first step - before writing up
the PEP, run it yourself and see whether you even like the way it
feels.

THEN, once you've convinced yourself, start convincing others (ie PEP).

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


Re: list(), tuple() should not place at Built-in functions in documentation

2011-07-15 Thread Steven D'Aprano
Inside wrote:

 Why I use assertion,please check this code:
 
 class User(object):pass
 
 class Student(User):pass
 
 class Professional(User):pass
 
 def add(user):
 assert(user, User)

This does not do what you think it does. All it does is, in some Python
versions, print 

SyntaxWarning: assertion is always true, perhaps remove parentheses?

In other Python versions, it is a no-op: it does nothing.

Perhaps you meant this?

assert isinstance(user, User)

Ben has already posted why isinstance type-checking should usually be
avoided in favour of duck-typing (if it looks like a duck, and sounds like
a duck, and swims like a duck, it might as well be a duck). But let's
suppose you have good reason for sticking to an explicit type-check. The
problem now is with the assert! Assertions are not guaranteed to run. The
caller can turn them off by running Python with the -O (optimize) switch.

Another problem: AssertionError is the wrong sort of exception to raise on
bad arguments. It should normally be TypeError or ValueError, or some other
more specific exception, with a useful error message.

Assertions are for testing your own program logic, not for validating input.
For example, in one of my own libraries, I have this piece of code:

data = _countiter(data)
assert data.count == 0
total = sum(data)
n = data.count
assert n = 0
# much later on...
return math.sqrt(value/n)


_countiter is a wrapper that keeps track of how many items have been
iterated over. I take an arbitrary iterator, wrap it, sum the values, then
check that the number of items is not a negative number. If it is, that's a
bug in my program logic, and I should find out as soon as possible, not
much later on when I try to take the square root of it.

Assertions should be rare, and never used for testing arguments (except,
maybe, for purely internal functions that only get called by your own
functions, never by the caller). If the caller ever sees an AssertionError
generated by your code, that is a bug in your code.



-- 
Steven

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


Re: An interesting beginner question: why we need colon at all in the python language?

2011-07-15 Thread Steven D'Aprano
Wanderer wrote:

 But if you have the colon, why do you need the brackets or backslashes
 in an if statement.
 
 Why not
 
 if condition1 or
condition2 or
condition3:
 do_something()
 
 The statement ain't over til there's a colon.


Because there are virtues in having the parser be nice and simple. Syntax
constraints help identify errors:

mystr = this is a string

Should we say that no closing quote is needed, because the newline
unambiguously ends the string? Well, perhaps... but allowing such a rule
would mask errors:

mystr = this is a %s % type(something)

Good language design requires constraints on what is allowed as well as
freedom from unnecessary syntax. 

The appropriate lines from the Zen are

Errors should never pass silently.
Unless explicitly silenced.

Newlines end parsing of the current token or expression. Including a newline
inside an expression is an error, unless you explicitly silence it by using
a backslash or using brackets. It's a bit too far to say that any if
statement is an explicit way to silent newline errors.



-- 
Steven

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


Re: HOWTO: Parsing email using Python part2

2011-07-15 Thread Steven D'Aprano
aspineux wrote:

 Hello
 
 I have written part 2 about parsing email.
 
 You can find the article here :
 
 http://blog.magiksys.net/parsing-email-using-python-content
 
 This part is a lot longer :

Wow! Well done.

Thank you for sharing this, this is extremely detailed and useful.



-- 
Steven

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


Re: Type checking versus polymorphism (was: list(), tuple() should not place at Built-in functions in documentation)

2011-07-15 Thread Chris Rebert
On Fri, Jul 15, 2011 at 7:47 PM, Steven D'Aprano
steve+comp.lang.pyt...@pearwood.info wrote:
snip
 Assertions are for testing internal program logic, not for validation.

 (I don't even like using assert for testing. How do you test your code with
 assertions turned off if you use assert for testing?)

I would think that would only matter if either the asserted
expressions caused side-effects or there was nontrivial logic in the
AssertionError handler, which would indicate a rather screwy codebase
and point to a possible PEBKAC issue that testing cannot hope to
remedy.

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


Re: Python threading/multiprocessing issue.

2011-07-15 Thread Chris Angelico
On Sat, Jul 16, 2011 at 3:15 PM, Dennis Lee Bieber
wlfr...@ix.netcom.com wrote:
 And (so far as I understand it) each process can claim its own CPU
 core, whereas threads share the active core.

Threads can be put onto separate cores too, and can have their
affinities set. But because of the GIL, actual CPython code can't run
on two cores at once. You might be able to have two Python threads
running at once if they're calling into C modules most of the time,
but it's much safer to either go multiprocessing or to use I/O bound
threads (eg socket handlers).

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


Re: list(), tuple() should not place at Built-in functions in documentation

2011-07-15 Thread Inside
 Perhaps you meant this?
 
 assert isinstance(user, User)

Yes I meant this,sorry,my mistake.



Thx for your and Ben's feedbacks first,it's appreciated.
your points is taken by me,but I want to make my opinion more clearly.

The assertion is JUST show to my function callers during development,warning 
that I want a list/tuple,not some other objects(BTW, your practice of 
wrapping arguments to iter is good,I'll take this to improve my code,thx 
again.).And assertion's off when running Python with the -O (optimize) switch 
is what I expect,it isn't necessary in production code.Argument validation is 
done by constuctor of object which I used in assertion.

I also have a few words about duck-typing.Duck-typing is good,but how about if 
I offer a convenient way to my function user by producing or changing what he 
want to pass me to a *real* duck?Is that more explicit to my user?

Anyway,I'll look at my principles which may need some changes or improvements.
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Questions about os.waitpid(pid, options) on windows

2011-07-15 Thread Christopher Head
On Mon, 11 Jul 2011 20:59:29 -0700 (PDT)
Fan zjuwu...@gmail.com wrote:

 It seems that waitpid take process handle instead of process id as the
 first parameter on Windows.  On Unices platform, the first parameter
 is process id.
 
 This interface is a little bit confusing. What's the purpose for such
 a design?
 
 Thanks a lot,
 Fan

I would assume it's because that's how the underlying OS primitives
work. Under Windows, concepts of process ID and process handle both
exist (and are separate), and the functions that wait for events to
occur (WaitForSingleObject, WaitForMultipleObjects, and so on) take
handles of various sorts, including process handles. Under Linux, there
is no such thing as a process handle, only a process ID, and waitpid
(the system call) takes a process ID.

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


MemoryError vs malloc error

2011-07-15 Thread Amit Dev
Hi,

I've a long running python process (running on freebsd). Sometimes when it
uses too much memory it core dumps. I would've expected it to raise
MemoryError. Normally, when would a python process raise MemoryError and
when would it fail with malloc error and cores? This is happening in pure
python code (Eg. if ' '.join(biglist)) etc.

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


  1   2   3   >