Re: Why is the argparse module so inflexible?

2013-06-28 Thread Isaac To
On Sat, Jun 29, 2013 at 9:36 AM, Ethan Furman  wrote:

> On 06/27/2013 03:49 PM, Steven D'Aprano wrote:
>
>>
>> Libraries should not call sys.exit, or raise SystemExit. Whether to quit
>> or not is not the library's decision to make, that decision belongs to
>> the application layer. Yes, the application could always catch
>> SystemExit, but it shouldn't have to.
>>
>
> So a library that is explicitly designed to make command-line scripts
> easier and friendlier should quit with a traceback?
>
> Really?
>

Perhaps put the functionality "handling of the exception of library to
sys.exit with a message" into a method so that the user can override it
(e.g., so that it just throw the same exception to the caller of the
library)?
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: python without OO

2005-01-26 Thread Isaac To
> "beliavsky" == beliavsky  <[EMAIL PROTECTED]> writes:

beliavsky> I think the OO way is slightly more obscure. It's
beliavsky> obvious what x = reverse(x) does, but it is not clear
beliavsky> unless you have the source code whether x.reverse()
beliavsky> reverses x or if it returns a reversed list.

What make it so clear to you that reverse(x) will always return a
reversed list rather than reversing x in place and return nothing?

beliavsky> It is clearer and more concise to write
beliavsky> z = reverse(x) + reverse(y)
beliavsky> than
beliavsky> x.reverse() 
beliavsky> y.reverse()
beliavsky> z = x + y

This isn't anything to do with OO programming.  It is something about
using in interface that your audience expects.  You have exactly the
same problem whether you are using procedural or OO style.  It might
be a case for functional programming, but that's something off-topic.

beliavsky> Furthermore, if in Python the algorithm for the reverse
beliavsky> function applies to many kinds of objects, it just
beliavsky> needs to be coded once, whereas a reverse method would
beliavsky> have to provided for each class that uses it (perhaps
beliavsky> through inheritance).

That the reverse() wants to be a function doesn't mean that the thing
that reverse() operate on doesn't want to be an object.  So this isn't
very clear a problem about OO style vs. procedural style, but instead
a problem about "generic" programming style vs. "concrete" programming
style.  On the other hand, if the thing that reverse() operate on
isn't an object sharing the same interface, it will be more clumsy to
implement a generic reverse() that works for all the different kinds
of object---even if they share similar interfaces.  Try to implement a
generic "reverse" in C when the different type of containers are
encoded as different style struct's accessible from different
function, and you will understand what I mean.  So this is,
marginally, a case *for* OO style.

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


Re: yield_all needed in Python

2005-03-01 Thread Isaac To
> "Douglas" == Douglas Alan <[EMAIL PROTECTED]> writes:

Douglas> If you'll reread what I wrote, you'll see that I'm not
Douglas> concerned with performance, but rather my concern is that
Douglas> I want the syntactic sugar.  I'm tired of writing code
Douglas> that looks like

Douglas>   def foogen(arg1):
Douglas>  def foogen1(arg2):
Douglas> # Some code here
Douglas>  # Some code here
Douglas>  for e in foogen1(arg3): yield e
Douglas>  # Some code here
Douglas>  for e in foogen1(arg4): yield e
Douglas>  # Some code here
Douglas>  for e in foogen1(arg5): yield e  
Douglas>  # Some code here
Douglas>  for e in foogen1(arg6): yield e  

How about writing it like the following?

def gen_all(gen):
for e in gen:
yield e

def foogen(arg1):
def foogen1(arg2):
# Some code here
# Some code here
gen_all(arg3)
# Some code here
gen_all(arg4)
# Some code here
gen_all(arg5)
# Some code here
gen_all(arg6)

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


Re: yield_all needed in Python

2005-03-01 Thread Isaac To
>>>>> "Isaac" == Isaac To <[EMAIL PROTECTED]> writes:

def gen_all(gen):
for e in gen:
yield e

def foogen(arg1):
def foogen1(arg2):
# Some code here
# Some code here
gen_all(arg3)
^ I mean foogen1(arg3), obviously, and similar for below
# Some code here
gen_all(arg4)
# Some code here
gen_all(arg5)
# Some code here
gen_all(arg6)

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


Re: yield_all needed in Python

2005-03-01 Thread Isaac To
> "Douglas" == Douglas Alan <[EMAIL PROTECTED]> writes:

Douglas> If you actually try doing this, you will see why I want
Douglas> "yield_all".

Oh... I see your point.

I was about to suggest that the code in my posts before should be made
to work somehow.  I mean, if in

  def fun1(x):
  if not x:
  raise MyErr()
  ...

  def fun2():
  ...
  fun1(val)

  fun2()

we can expect that main gets the exception thrown by fun1, why in

  def fun1(x):
  if not x:
  yield MyObj()
  ...

  def fun2():
  fun1(val)

  for a in fun2():
  ...

we cannot expect MyObj() to be yielded to main?  But soon I found that
it is not realistic: there is no way to know that fun2 has generator
semantics.  Perhaps that is a short-sightness in not introducing a new
keyword instead of def when defining generators.

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


Re: yield_all needed in Python

2005-03-05 Thread Isaac To
> "Paul" == Paul Moore <[EMAIL PROTECTED]> writes:

Paul> You can work around the need for something like yield_all,
Paul> or explicit loops, by defining an "iflatten" generator,
Paul> which yields every element of its (iterable) argument,
Paul> unless the element is a generator, in which case we recurse
Paul> into it:
Paul> ...

Only if you'd never want to yield a generator.

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


Re: The use of :

2004-11-28 Thread Isaac To
> "Greg" == Greg Ewing <[EMAIL PROTECTED]> writes:

>> The only punctuation you *need* is whitespace. See Forth

Greg> You don't even need that... see FORTRAN. :-)

And you don't need everything else either... see this.

 http://compsoc.dur.ac.uk/whitespace/

:-)

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


Re: creating generators from function

2004-12-08 Thread Isaac To
> "Mike" == Mike Meyer <[EMAIL PROTECTED]> writes:

Mike> I think it's a bit abnormal, because you have to scan the
Mike> loop body for breaks. I tend to write:

Mike> condition = True
Mike> while condition:  # corrected
Mike> #code which iterates my simulation

Then you'd have to scan the loop body to find the location where
condition is set, which is more difficult than locating breaks
normally.  If you get a break, you really breaks.  If you set
condition to False, you still might be modifying it to True later in
your code.  And of course, most editors will highlight the "break" for
you, while no editor will highlight for you the "condition" variable
that you are staring at.

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


Import redirects

2013-02-10 Thread Isaac To
I have a package (say "foo") that I want to rename (say, to "bar"), and for
compatibility reasons I want to be able to use the old package name to
refer to the new package.  Copying files or using filesystem symlinks is
probably not the way to go, since that means any object in the modules of
the package would be duplicated, changing one will not cause the other to
be updated.  Instead, I tried the following as the content of
`foo/__init__.py`:

import sys
import bar
sys.modules['foo'] = bar

To my surprise, it seems to work.  If I `import foo` now, the above will
cause "bar" to be loaded and be used, which is expected.  But even if I
`import foo.baz` now (without first `import foo`), it will now correctly
import "bar.baz" in its place.

Except one thing: it doesn't really work.  If I `import foo.baz.mymod` now,
and if in "bar.baz.mymod" there is a statement `import bar.baz.depmod`,
then it fails.  It correctly load the file "bar/baz/depmod.py", and it
assigns the resulting module to the package object bar.baz as the "depmod"
variable.  But it fails to assign the module object of "mymod" into the
"bar.baz" module.  So after `import foo.baz.mymod`, `foo.baz.mymod` results
in an AttributeError saying 'module' object has no attribute 'mymod'.  The
natural `import bar.baz.mymod` is not affected.

I tested it with both Python 2.7 and Python 3.2, and I see exactly the same
behavior.

Anyone knows why that happen?  My current work-around is to use the above
code only for modules and not for packages, which is suboptimal.  Anyone
knows a better work-around?
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Import redirects

2013-02-11 Thread Isaac To
On Mon, Feb 11, 2013 at 8:27 PM, Oscar Benjamin
wrote:

> On 11 February 2013 06:50, Isaac To  wrote:
> > Except one thing: it doesn't really work.  If I `import foo.baz.mymod`
> now,
> > and if in "bar.baz.mymod" there is a statement `import bar.baz.depmod`,
> then
> > it fails.  It correctly load the file "bar/baz/depmod.py", and it assigns
> > the resulting module to the package object bar.baz as the "depmod"
> variable.
> > But it fails to assign the module object of "mymod" into the "bar.baz"
> > module.  So after `import foo.baz.mymod`, `foo.baz.mymod` results in an
> > AttributeError saying 'module' object has no attribute 'mymod'.  The
> natural
> > `import bar.baz.mymod` is not affected.
>
> My guess is that you have two copies of the module object bar.baz with
> one under the name foo.baz and the other under the name bar.baz. mymod
> is inserted at bar.baz but not at foo.baz. I think a solution in this
> case would be to have your foo/__init__.py also import the subpackage
> 'bar.baz' and give it both names in sys.modules:
>
> import bar.baz
> sys.modules['foo.baz'] = bar.baz
>

Thanks for the suggestion.  It is indeed attractive if I need only to
pre-import all the subpackage and not to redirect individual modules.  On
the other hand, when I actually try this I found that it doesn't really
work as intended.  What I actually wrote is, as foo/__init__.py:

import sys
import bar
import bar.baz
sys.modules['foo.baz'] = bar.baz
sys.modules['foo'] = bar

One funny effect I get is this:

>>> import bar.baz.mymod
>>> bar.baz.mymod

>>> import foo.baz.mymod
>>> bar.baz.mymod


By importing foo.baz.mymod, I change the name of the module from
"bar.baz.mymod" to "foo.baz.mymod".  If that is not bad enough, I also see
this:

>>> import bar.baz.mymod as bbm
>>> import foo.baz.mymod as fbm
>>> bbm is fbm
False

Both effects are there even if bar/baz/mymod.py no longer `import
bar.baz.depmod`.

It looks to me that package imports are so magical that I shouldn't do
anything funny to it, as anything that seems to work might bite me a few
minutes later.

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


Re: Set x to to None and del x doesn't release memory in python 2.7.1 (HPUX 11.23, ia64)

2013-03-09 Thread Isaac To
In general, it is hard for any process to return the memory the OS allocate
to it back to the OS, short of exiting the whole process.  The only case
that this works reliably is when the process allocates a chunk of memory by
mmap (which is chosen by libc if it malloc or calloc a large chunk of
memory), and that whole chunk is not needed any more.  In that case the
process can munmap it.  Evidently you are not see that in your program.
What you allocate might be too small (so libc choose to allocate it using
another system call "sbrk"), or that the allocated memory also hold other
objects not freed.

If you want to reduce the footprint of a long running program that
periodically allocates a large chunk of memory, the "easiest" solution is
to fork a different process to achieve the computations that needs the
memory.  That way, you can exit the process after you complete the
computation, and at that point all memory allocated to it is guaranteed to
be freed to the OS.

Modules like multiprocessing probably make the idea sufficiently easy to
implement.


On Sat, Mar 9, 2013 at 4:07 PM, Wong Wah Meng-R32813
wrote:

>
>
> If the memory usage is continually growing, you have something
> else that is a problem -- something is holding onto objects. Even if Python
> is not returning memory to the OS, it should be reusing the memory it has
> if objects are being freed.
> --
> [] Yes I have verified my python application is reusing the memory (just
> that it doesn't reduce once it has grown) and my python process doesn't
> have any issue to run even though it is seen taking up more than 2G in
> footprint. My problem is capacity planning on the server whereby since my
> python process doesn't release memory back to the OS, the OS wasn't able to
> allocate memory when a new process is spawn off.
>
> --
> http://mail.python.org/mailman/listinfo/python-list
>
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: I hate you all

2013-04-05 Thread Isaac To
You underestimated the arrogance of Python.  Python 3 tab doesn't map to 4
spaces.  It doesn't map to any number of spaces.  Tabs and spaces are
completely unrelated.  If you have a function having the first indentation
level with 4 (or any number of) spaces, the next line starting not with 4
spaces but instead with a tab always lead you to the TabError exception.

If you like to play tricks, you can use "4 spaces plus a tab" as the next
indentation level.  I'd rather not do this kind of things, and forget about
use using tabs at all.  You are out of luck if you want to play the
tab-space tricks, but if you follow the lead, you'll soon find that code
will be more reliable without tabs, especially if you cut-and-paste code of
others.


On Sat, Apr 6, 2013 at 6:04 AM,  wrote:

> On Saturday, April 6, 2013 12:55:29 AM UTC+3, John Gordon wrote:
> > In <64d4fb7c-6a75-4b5f-b5c8-06a4b2b5d...@googlegroups.com>
> terminato...@gmail.com writes:
> >
> > > How can python authors be so arrogant to impose their tabs and spaces
> > > options on me ? It should be my choice if I want to use tabs or not !
> >
> > You are free to use tabs, but you must be consistent.  You can't mix
> > tabs and spaces for lines of code at the same indentation level.
>
> They say so, but python does not work that way. This is a simple script:
>
> from unittest import TestCase
>
> class SvnExternalCmdTests(TestCase) :
> def test_parse_svn_external(self) :
> for sample_external in sample_svn_externals :
> self.assertEqual(parse_svn_externals(sample_external[0][0],
> sample_external[0][1]), [ sample_external[1] ])
>
> And at the `for` statement at line 5 I get:
>
> C:\Documents and Settings\Adrian\Projects>python sample-py3.py
>   File "sample-py3.py", line 5
> for sample_external in sample_svn_externals :
> ^
> TabError: inconsistent use of tabs and spaces in indentation
>
>
> Line 5 is the only line in the file that starts at col 9 (after a tab).
> Being the only line in the file with that indent level, how can it be
> inconsistent ?
>
> You can try the script as it is, and see python 3.3 will not run it
> --
> http://mail.python.org/mailman/listinfo/python-list
>
-- 
http://mail.python.org/mailman/listinfo/python-list