Re: can't generate iterator from list

2011-09-10 Thread Peter Otten
Dr. Phillip M. Feldman wrote:

> 
> It is supposed to be possible to generate a list representation of any
> iterator that produces a sequence of finite length, but this doesn't
> always work. Here's a case where it does work:
> 
> Input:
> 
> from itertools import combinations
> list(combinations(range(4),2))
> 
> Output:
> 
> [(0, 1), (0, 2), (0, 3), (1, 2), (1, 3), (2, 3)]
> 
> When I define my own classes that produce iterators, conversion to a list
> works for some of these classes but not for others. Here's a case where it
> doesn't work:
> 
> In:  list(balls_in_numbered_boxes(2, [3,3,3]))
> 
> Out:
> 
> [array([0, 0, 1]),
>  array([0, 0, 1]),
>  array([0, 0, 1]),
>  array([0, 0, 1]),
>  array([0, 0, 1])]

[snip code where excessive commenting does more bad than good]

The problem is that you return the same mutable object on every next() call. 
Here's a simplified example:

>>> def g(items):
... x = [None]
... for item in items:
... x[0] = item
... yield x
...
>>> list(g("abc"))
[['c'], ['c'], ['c']]

When you invoke it using next() you are fooled into thinking that it works 
as desired:

>>> it = g("abc")
>>> a = next(it)
>>> a
['a']
>>> b = next(it)
>>> b
['b']

but only until you look back at the previous item:

>>> a
['b']

Once you understand what is going on the fix is easy -- don't reuse the 
mutable object:

>>> def g(items):
... for item in items:
... yield [item]
...
>>> list(g("abc"))
[['a'], ['b'], ['c']]


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


Applying a function recursively

2011-09-10 Thread hetchkay
Hi,
I want to apply a "convert" function on an object as follows:
If the object is of MyType type, invoke the passed in function.
If the object is a dictionary, apply on the keys and values of the
dictionary recursively.
If the object is a set, list or tuple, apply on each element
recursively.
Else, leave the object as is.

I wrote the following code:
def convert(obj, func):
   if isinstance(obj, MyType):
  return func(obj)
   elif isinstance(obj, dict):
  return dict((convert(key, func), convert(value, func)) for key,
value in obj.iteritems())
   elif isinstance(obj, (list, tuple, set)):
  return obj.__class__(convert(x, func) for x in obj)
   else:
  return obj

Is there a better way to do this?
Is there any way I can make this code faster?

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


Re: Applying a function recursively

2011-09-10 Thread Chris Rebert
On Sat, Sep 10, 2011 at 12:19 AM, hetchkay  wrote:
> Hi,
> I want to apply a "convert" function on an object as follows:
> If the object is of MyType type, invoke the passed in function.
> If the object is a dictionary, apply on the keys and values of the
> dictionary recursively.
> If the object is a set, list or tuple, apply on each element
> recursively.
> Else, leave the object as is.
>
> I wrote the following code:
> def convert(obj, func):
>   if isinstance(obj, MyType):
>      return func(obj)
>   elif isinstance(obj, dict):
>      return dict((convert(key, func), convert(value, func)) for key,
> value in obj.iteritems())
>   elif isinstance(obj, (list, tuple, set)):
>      return obj.__class__(convert(x, func) for x in obj)
>   else:
>      return obj
>
> Is there a better way to do this?

None comes to mind.

> Is there any way I can make this code faster?

Possibly, but it involves ignoring subclasses, and may not actually be
faster in your particular case (it comes down to additional function
calls vs. cost of if-elif-else chain). It would be along the lines of:

def convert_mytype(obj, func):
return func(obj)

def convert_dict(obj, func):
return dict((convert(key, func), convert(value, func)) for key,
value in obj.iteritems())

def dont_convert(obj, func):
return obj

TYPE2FUNC = {MyType : convert_mytype, dict : convert_dict, ... }

def convert(obj, func):
return TYPE2FUNC.get(type(obj), dont_convert)(obj, func)


As they say though, premature optimization is the root of all evil.

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


Re: Applying a function recursively

2011-09-10 Thread Ben Finney
hetchkay  writes:

> Hi,
> I want to apply a "convert" function on an object as follows:
> If the object is of MyType type, invoke the passed in function.
> If the object is a dictionary, apply on the keys and values of the
> dictionary recursively.
> If the object is a set, list or tuple, apply on each element
> recursively.
> Else, leave the object as is.

That smells like a bad design. Why are you using the same function for
al of those different behaviours?

That's not merely rhetorical; the design isn't absolutely wrong. But
it's wrong often enough that you need to have a compelling reason to
make such a complex behaviour in a single function.

I suspect, if you can be explicit about the goal you're aiming for with
this code, a better design can be found that doesn't require all those
polymorphism-breaking type checks.

-- 
 \  “An expert is a man who has made all the mistakes which can be |
  `\ made in a very narrow field.” —Niels Bohr |
_o__)  |
Ben Finney
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: killing a script

2011-09-10 Thread Cameron Simpson
On 10Sep2011 11:25, Steven D'Aprano  
wrote:
| Cameron Simpson wrote:
| > My copy of the 2.7 docs says:
| >   This is implemented by calling the Standard C function system(), and
| >   has the same limitations.
| > and sure enough, "man 3 system" says:
| 
| I don't consider having to look up documentation for a function in a
| completely different language (in this case, C) as "documented behaviour of
| os.system". 

You're kidding, surely? A wrapper function for a system supplied function
should recite everything about the wrapped function's behaviour (including
the system dependent stuff) in the wrapper doco?

| Does the C standard define the behaviour of system(), or is that
| implementation dependent?

The standard specifies the core behaviour - the behaviour guarrenteed to
be present everywhere. Plenty of stuff has platform dependent minor
nuances. As long as portable code relies only on the standard behaviour
everybody wins.

| It sounds to me that the Python developers are
| implicitly refusing responsibility for the detailed behaviour of os.system
| by noting that it depends on the C function.

Of course they are, and they are right to do so. But noting that it
calls the standard function does guarrentee various things about what it
does.

| What do Jython, PyPy and
| IronPython do?
| 
| Perhaps the docs for os.system should explicitly note that the behaviour is
| implementation dependent, rather than just hint at it. Either that or
| explicitly state what os.system does.

I find it hard to understand how anyone can read this text:

  This is implemented by calling the Standard C function system(), and
  has the same limitations

and not imagine it to be dependent on the specification for system().

| > Continuing with the Python docs for os.system: 
| > 
| >   On Unix, the return value is the exit status of the process encoded in
| >   the format specified for wait().
| > 
| > and it is easy to inspect that value for "the subprocess died from a
| > signal". Not inspecting the exit status correctly will always be an
| > opportunity for incorrect app behaviour.
| 
| Except that the subprocess can catch the KeyboardInterrupt before exiting,

This is very true, though such programs usually have a special reason to
do so - the default aborting behaviour is often sufficient.

| and there's no guarantee that it will return an appropriate error code.

Of course. However, the subprocess should still exit with a nonzero exit
status (unless it it written badly). If the caller considers success of
the called program to be important, it should probably be aborting if
the returned value is nonzero anyway.

But yeah, people should probably be reaching for subprocess if they want
to notice SIGINT specially.

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

But pessimism IS realism!   - D.L.Bahr
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: A bit of a boggle about subprocess.poll() and the codes it receives from a process

2011-09-10 Thread Kushal Kumaran
On Fri, Sep 9, 2011 at 11:02 PM, J  wrote:
> Hi,
> I need a bit of help sorting this out...
> I have a memory test script that is a bit of compiled C.  The test itself
> can only ever return a 0 or 1 exit code, this is explicitly coded and there
> are no other options.
> I also have a wrapper test script that calls the C program that should also
> only return 0 or 1 on completion.
> The problem i'm encountering, however, involves the return code when
> subprocess.poll() is called against the running memory test process.  The
> current code in my wrapper program looks like this:
> def run_processes(self, number, command):
>         passed = True
>         pipe = []
>         for i in range(number):
>             pipe.append(self._command(command))
>             print "Started: process %u pid %u: %s" % (i, pipe[i].pid,
> command)
>         sys.stdout.flush()
>         waiting = True
>         while waiting:
>             waiting = False
>             for i in range(number):
>                 if pipe[i]:
>                     line = pipe[i].communicate()[0]
>                     if line and len(line) > 1:
>                         print "process %u pid %u: %s" % (i, pipe[i].pid,
> line)
>                         sys.stdout.flush()
>                     if pipe[i].poll() == -1:
>                         waiting = True
>                     else:
>                         return_value = pipe[i].poll()
>                         if return_value != 0:
>                             print "Error: process  %u pid %u retuned %u" %
> (i, pipe[i].pid, return_value)
>                             passed = False
>                         print "process %u pid %u returned success" % (i,
> pipe[i].pid)
>                         pipe[i] = None
>         sys.stdout.flush()
>         return passed
> So what happens here is that in the waiting loop, if pipe[i].poll returns a
> -1, we keep waiting, and then if it returns anything OTHER than -1, we exit
> and return the return code.

Does self._command return a subprocess.Popen object?  The
documentation at
http://docs.python.org/library/subprocess.html#subprocess.Popen.poll
says that the poll method sets and returns the returncode attribute.
returncode is expected to be None until the process terminates, and a
negative value indicates that the subprocess has been terminated
because of a signal.  If poll() returns -1, it means that the process
has been terminated by signal number 1 (probably SIGHUP).

> BUT, I'm getting, in some cases, a return code of 127, which is impossible
> to get from the memory test program.
> The output from this bit of code looks like this in a failing situation:
> Error: process 0 pid 2187 retuned 127
> process 0 pid 2187 returned success
> Error: process 1 pid 2188 retuned 127
> process 1 pid 2188 returned success
> I'm thinking that I'm hitting some sort of race here where the kernel is
> reporting -1 while the process is running, then returns 127 or some other
> status when the process is being killed and then finally 0 or 1 after the
> process has completely closed out.  I "think" that the poll picks up this
> intermediate exit status and immediately exits the loop, instead of waiting
> for a 0 or 1.
> I've got a modified version that I'm getting someone to test for me now that
> changes
>  if pipe[i].poll() == -1:
>      waiting = True
> to this
> if pipe[i].poll() not in [0,1]:
>     waiting = True
> So my real question is: am I on the right track here, and am I correct in my
> guess that the kernel is reporting different status codes to
> subprocess.poll() during the shutdown of the polled process?
>

I'm unaware of any such race condition.  It is more likely that the
program you are running can return such error codes.  Perhaps you
should examine its stderr output.

It's unclear what you're trying to do here.  If you'd just like to
wait until all the started processes are finished, you should use the
wait() method instead of the poll() method.  You should also note that
the communicate() method already waits for the process to complete, so
further calls to poll() and wait() are superfluous.  Normally, after
communicate() returns, you would simply check pipe[i].returncode and
be on your way.

-- 
regards,
kushal
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: try... except with unknown error types

2011-09-10 Thread Nobody
On Wed, 31 Aug 2011 21:01:34 +, Chris Torek wrote:

> Still, it sure would be nice to have a static analysis
> tool that could answer questions about potential exceptions. :-) )

That's an impossibility in a dynamic language.

If you call f.read() where f was passed in as a parameter, the exceptions
which f.read() may throw depend upon exactly what f is; there's no
guarantee that it will actually be a built-in file object, only that it
will have a .read() method (otherwise you will get an AttributeError).

Even if f was returned from open(), there's no guarantee that
__builtins__.open() won't have been replaced by the time of the call.

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


Re: killing a script

2011-09-10 Thread Nobody
On Sat, 10 Sep 2011 11:25:40 +1000, Steven D'Aprano wrote:

>> and sure enough, "man 3 system" says:
> 
> I don't consider having to look up documentation for a function in a
> completely different language (in this case, C) as "documented behaviour of
> os.system". 

Well, tough luck. os.system() is a wrapper around the platform's system().
The authors of the Python documentation cannot possibly know what that
function will do on all platforms, even less so once you factor in the
user's ability to replace that function via platform-specific means.

> Does the C standard define the behaviour of system(), or is that
> implementation dependent?

The C99 standard says:

   7.20.4.5  The system function

   Synopsis

   [#1]

   #include 
   int system(const char *string);

   Description

   [#2] If string  is  a  null  pointer,  the  system  function
   determines  whether  the  host  environment  has  a  command
   processor. If string is  not  a  null  pointer,  the  system
   function  passes  the  string  pointed  to by string to that
   command processor to be  executed  in  a  manner  which  the
   implementation  shall  document;  this  might then cause the
   program calling system to behave in a non-conforming  manner
   or to terminate.

   Returns

   [#3]  If the argument is a null pointer, the system function
   returns nonzero only if a command  processor  is  available.
   If  the  argument  is  not  a  null  pointer, and the system
   function does return, it returns  an  implementation-defined
   value.

It doesn't require a platform to even have a command interpreter, let
alone specify its behaviour. On Unix, system() is defined to use /bin/sh,
which ought to be some kind of Bourne shell; but even then, it might be a
minimal shell such as dash or something with many extensions such as bash.
On Windows, it uses the interpreter specified by the COMSPEC environment
variable, or cmd.exe (NT-based) or command.com (DOS-based) if COMSPEC
isn't set (in either case, PATH is used).

> It sounds to me that the Python developers are
> implicitly refusing responsibility for the detailed behaviour of os.system
> by noting that it depends on the C function.

Indeed. Which is the correct thing to do. Practically every function in
the os module does likewise.

> What do Jython, PyPy and IronPython do?
 
I don't know, but I would expect them to use libc's system() function,
except for Jython which might use java.lang.Runtime.exec().


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


Re: How to structure packages

2011-09-10 Thread Nobody
On Fri, 09 Sep 2011 11:37:44 +1000, Chris Angelico wrote:

>> The Java compiler also acts as a "make" program. If it doesn't find
>> a .class file for a needed class, it will search for the corresponding
>> .java file and compile that. So to compile a complex program, you only
>> need to compile the top-level file (e.g. HelloWorld.java), and it will
>> compile everything which is required. No Makefile is needed, as the
>> relationship between classes, object files and source files is fixed.
>>
> 
> If that's the entire benefit, then I think this is a rather hefty
> price to pay for the elimination of a makefile.

It also eliminates the need for TAGS files, browser database (PDB) files,
etc. Once you know the class name, all of the filenames follow from that.

I suspect that the one-to-one correspondence between classes and .class
files is mostly technical (e.g. Java's security model). The one-to-one
correspondence between class files and source files could probably be
relaxed, but at the expense of complicating the IDE and toolchain.

I never saw it as a problem, given that Java is fundamentally class-based:
there are no global variables or functions, only classes.

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


Re: try... except with unknown error types

2011-09-10 Thread Peter Otten
Chris Torek wrote:

> >>> import socket
> >>> isinstance(socket.error, IOError)
> False

Here you test if the socket.error *class* is an instance of IOError; this 
would print True if IOError were socket.error's metaclass. However:

>>> isinstance(socket.error(), IOError)
True

or more directly:

>>> issubclass(socket.error, IOError)
True
>>> issubclass(socket.error, EnvironmentError)
True

This is a relatively recent change:

$ python2.5 -c'from socket import error; print issubclass(error, IOError), 
issubclass(error, EnvironmentError)'
False False
$ python2.6 -c'from socket import error; print issubclass(error, IOError), 
issubclass(error, EnvironmentError)'
True True

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


Re: How to structure packages

2011-09-10 Thread Chris Angelico
On Sat, Sep 10, 2011 at 8:11 PM, Nobody  wrote:
> I suspect that the one-to-one correspondence between classes and .class
> files is mostly technical (e.g. Java's security model). The one-to-one
> correspondence between class files and source files could probably be
> relaxed, but at the expense of complicating the IDE and toolchain.

One class per object file isn't a problem - you can always .jar your
classes if the proliferation of small files bothers you, and then it's
just a different way of indexing the mound of code.

One class per source file complicates the human's view in order to
simplify the tools'. Not sure that's really worthwhile.

> I never saw it as a problem, given that Java is fundamentally class-based:
> there are no global variables or functions, only classes.

Yeah... of course you can easily simulate globals with static members
in a dedicated class, but it's slower. THIS, though, is where Java's
security model comes in - you can assign security X to Globals1.class
and security Y to Globals2.class, rather than trying to juggle
security issues in a monolithic "globals" namespace. IMHO it's not
worth the hassle, though. I'd rather just have globals.

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


Re: what's the command for (cd ..) in python

2011-09-10 Thread Waldek M.
On Fri, 09 Sep 2011 23:03:10 +1000, Steven D'Aprano wrote:
> But think carefully before doing this. Some functions may be confused if you
> change directories while they are running. You may be better off staying in
> the same directory, and adjusting the path names to the files as you work
> with them.

Curious.
Do you mean multi-threaded environment or even in single thread?
If the latter is the case, I'd say those functions make 
very nasty assumptions. Who are they, anyways? ;)

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


[ANN] Shed Skin 0.9

2011-09-10 Thread Mark Dufour
Hi all,

I have just released version 0.9 of Shed Skin, a (restricted-)Python to C++
compiler.

Please see my blog for the full announcement:

http://shed-skin.blogspot.com

The Shed Skin homepage is located here:

http://shedskin.googlecode.com


Thanks!
Mark Dufour.
-- 
http://www.youtube.com/watch?v=E6LsfnBmdnk
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: what's the command for (cd ..) in python

2011-09-10 Thread Steven D'Aprano
Waldek M. wrote:

> On Fri, 09 Sep 2011 23:03:10 +1000, Steven D'Aprano wrote:
>> But think carefully before doing this. Some functions may be confused if
>> you change directories while they are running. You may be better off
>> staying in the same directory, and adjusting the path names to the files
>> as you work with them.
> 
> Curious.
> Do you mean multi-threaded environment or even in single thread?
> If the latter is the case, I'd say those functions make
> very nasty assumptions. Who are they, anyways? ;)

The main one that comes to mind is os.walk, which has this to say:

Caution:  if you pass a relative pathname for top, don't change the
current working directory between resumptions of walk.  walk never
changes the current directory, and assumes that the client doesn't
either.

Seems like a reasonable assumption to me.

-- 
Steven

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


Doctest failing

2011-09-10 Thread Tigerstyle
Hi guys.

I'm strugglin with some homework stuff and am hoping you can help me
out here.

This is the code:

small_words = ('into', 'the', 'a', 'of', 'at', 'in', 'for', 'on')

def book_title(title):
""" Takes a string and returns a title-case string.
All words EXCEPT for small words are made title case
unless the string starts with a preposition, in which
case the word is correctly capitalized.
>>> book_title('DIVE Into python')
'Dive into Python'
>>> book_title('the great gatsby')
'The Great Gatsby'
>>> book_title('the WORKS OF AleXANDer dumas')
'The Works of Alexander Dumas'
"""
new_title = []
title_split = title.strip().lower().split()
for word in title_split:
if title_split[0] in small_words:
new_title.append(word.title())
elif word in small_words:
new_title.append(word.lower())
else:
new_title.append(word.title())
return(' '.join(new_title))

def _test():
import doctest, refactory
return doctest.testmod(refactory)
if __name__ == "__main__":
_test()

All tests are failing even though I am getting the correct output on
the first two tests. And the last test still gives me "Of" instead of
"of"

Any help is appreciated.

Rgds

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


Re: Doctest failing

2011-09-10 Thread Mel
Tigerstyle wrote:

> Hi guys.
> 
> I'm strugglin with some homework stuff and am hoping you can help me
> out here.
> 
> This is the code:
> 
> small_words = ('into', 'the', 'a', 'of', 'at', 'in', 'for', 'on')
> 
> def book_title(title):
> """ Takes a string and returns a title-case string.
> All words EXCEPT for small words are made title case
> unless the string starts with a preposition, in which
> case the word is correctly capitalized.
> >>> book_title('DIVE Into python')
> 'Dive into Python'
> >>> book_title('the great gatsby')
> 'The Great Gatsby'
> >>> book_title('the WORKS OF AleXANDer dumas')
> 'The Works of Alexander Dumas'
> """
> new_title = []
> title_split = title.strip().lower().split()
> for word in title_split:
> if title_split[0] in small_words:
> new_title.append(word.title())
> elif word in small_words:
> new_title.append(word.lower())
> else:
> new_title.append(word.title())
> return(' '.join(new_title))
> 
> def _test():
> import doctest, refactory
> return doctest.testmod(refactory)
> if __name__ == "__main__":
> _test()
> 
> All tests are failing even though I am getting the correct output on
> the first two tests. And the last test still gives me "Of" instead of
> "of"
> 
> Any help is appreciated.

I don't know about doctest -- I suspect it wants a structured docstring to 
specify the tests -- but this

if title_split[0] in small_words:
new_title.append(word.title())

can't be what you want.

Mel.

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


Re: Doctest failing

2011-09-10 Thread Peter Otten
Tigerstyle wrote:

> I'm strugglin with some homework stuff and am hoping you can help me
> out here.
> 
> This is the code:
> 
> small_words = ('into', 'the', 'a', 'of', 'at', 'in', 'for', 'on')

> new_title = []
> title_split = title.strip().lower().split()
> for word in title_split:
> if title_split[0] in small_words:
> new_title.append(word.title())
> elif word in small_words:
> new_title.append(word.lower())
> else:
> new_title.append(word.title())

The logic of the for-loop is flawed; the expression 

title_split[0] in small_words

will always evaluate to True if the first word is a "small word", even when 
the loop is already past the first word. You can work around that with a 
flag along these lines

first = True
for word in title_split:
if first:
# special treatment for the first word
first = False
else:
# put checks for all words but the first here
new_title.append(fixed_word) # assuming you have stored the titlecased
 # or lowercased word in the fixed_word
 # variable

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


Re: Doctest failing

2011-09-10 Thread Thomas Jollans
On 10/09/11 13:20, Tigerstyle wrote:
> Hi guys.
> 
> I'm strugglin with some homework stuff and am hoping you can help me
> out here.
>
> All tests are failing even though I am getting the correct output on
> the first two tests. And the last test still gives me "Of" instead of
> "of"

Cannot reproduce. I only get the one, expected, failure.

% python -m doctest books.py
**
File "books.py", line 12, in books.book_title
Failed example:
book_title('the WORKS OF AleXANDer dumas')
Expected:
'The Works of Alexander Dumas'
Got:
'The Works Of Alexander Dumas'
**
1 items had failures:
   1 of   3 in books.book_title
***Test Failed*** 1 failures.

>
> def _test():
> import doctest, refactory
> return doctest.testmod(refactory)
> if __name__ == "__main__":
> _test()
>

What is this "refactory"? Are you testing the right code? What is the
output of your test - does it make sense for the module?

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


test if a subclass inherits a superclass method

2011-09-10 Thread Kayode Odeyemi
Hello,

I'm testing Python's class abstractness and inheritance. Since interface
doesn't exist, I will
like to test how to have access to a superclass method from a subclass
without necessary
invoking or overriding the superclass method in its subclass.

>>> class Equipment(object):
... def fault():
... return "fault"
...
>>> Equipment().__class__

>>> class Vehicle(Equipment):
...  # Find out here if Vehicle has access to fault

I want to know whether Vehicle has access to Equipment's fault() method.
Just want to know if it's there(that a vehicle can also develop a fault).

I know I can override it, but I want to know if I can use it directly
without overriding it.
-- 
Odeyemi 'Kayode O.
http://www.sinati.com. t: @charyorde
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: test if a subclass inherits a superclass method

2011-09-10 Thread Kayode Odeyemi
On Sat, Sep 10, 2011 at 12:58 PM, Kayode Odeyemi  wrote:

> Hello,
>
> I'm testing Python's class abstractness and inheritance. Since interface
> doesn't exist, I will
> like to test how to have access to a superclass method from a subclass
> without necessary
> invoking or overriding the superclass method in its subclass.
>
> >>> class Equipment(object):
> ... def fault():
> ... return "fault"
> ...
> >>> Equipment().__class__
> 
> >>> class Vehicle(Equipment):
> ...  # Find out here if Vehicle has access to fault
>
> I want to know whether Vehicle has access to Equipment's fault() method.
> Just want to know if it's there(that a vehicle can also develop a fault).
>
> I know I can override it, but I want to know if I can use it directly
> without overriding it.
>

OK! I figured it out like this:

>>> v = Vehicle()
>>> dir(v)
['__class__', '__delattr__', '__dict__', '__doc__', '__format__',
'__getattribut
e__', '__hash__', '__init__', '__module__', '__new__', '__reduce__',
'__reduce_e
x__', '__repr__', '__setattr__', '__sizeof__', '__str__',
'__subclasshook__', '_
_weakref__', 'fault']

Cool stuff.

-- 
> Odeyemi 'Kayode O.
> http://www.sinati.com. t: @charyorde
>
>


-- 
Odeyemi 'Kayode O.
http://www.sinati.com. t: @charyorde
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Doctest failing

2011-09-10 Thread Alister Ware
On Sat, 10 Sep 2011 04:20:17 -0700, Tigerstyle wrote:

> Hi guys.
> 
> I'm strugglin with some homework stuff and am hoping you can help me out
> here.
> 
> This is the code:
> 
> small_words = ('into', 'the', 'a', 'of', 'at', 'in', 'for', 'on')
> 
> def book_title(title):
> """ Takes a string and returns a title-case string. All words EXCEPT
> for small words are made title case unless the string starts with a
> preposition, in which case the word is correctly capitalized.
> >>> book_title('DIVE Into python')
> 'Dive into Python'
> >>> book_title('the great gatsby')
> 'The Great Gatsby'
> >>> book_title('the WORKS OF AleXANDer dumas')
> 'The Works of Alexander Dumas'
> """
> new_title = []
> title_split = title.strip().lower().split()
> for word in title_split:
> if title_split[0] in small_words:
> new_title.append(word.title())
> elif word in small_words:
> new_title.append(word.lower())
> else:
> new_title.append(word.title())
> return(' '.join(new_title))
> 
> def _test():
> import doctest, refactory return doctest.testmod(refactory)
> if __name__ == "__main__":
> _test()
> 
> All tests are failing even though I am getting the correct output on the
> first two tests. And the last test still gives me "Of" instead of "of"
> 
> Any help is appreciated.
> 
> Rgds
> 
> T

Ignoring the docttests my process would be to process each word & then 
manually capitalize he 1st word, .I would als0 use a comprehension as 
makes for cleaner code:-

small_words=('into','the','a','of','at','in','for','on')

def capitalize(word):
if word in small_words:
return word
else:
return word.title()

def set_title(phrase):
result=[ capitalize(x.lower()) for x in phrase.split(' ') ]
result[0]=result[0].title()
return " ".join(result)


print set_title('the works of alexander dumas')


-- 
... I don't like FRANK SINATRA or his CHILDREN.
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Applying a function recursively

2011-09-10 Thread hetchkay
>
> I suspect, if you can be explicit about the goal you're aiming for with
> this code, a better design can be found that doesn't require all those
> polymorphism-breaking type checks.
>
It is difficult to explain what I am trying to do, but let me try. I
am mapping data from one hierarchy into another. The mapping rules are
quite complex. I developed a system such that the rules could be
defined as "expressions" of the source hierarchy i.e a particular
entry in the target hierarchy could be represented as an expression of
entries in the source hierarchy. Suppose a point is stored in x, y
coordinates in the source hierarchy, and in polar coordinates in the
target hierarchy, I could write (forget for the moment what sourcePt
is):
pointMapping = {
  sourcePt.key : dict(
 radius = Sqrt(sourcePt.value['x']**2 + sourcePt.value['y']**2),
 angle = Atan(sourcePt.value['y']/sourcePt.value['x']),
   ),
}
The above dictionary is delay-evaluated. sourcePt is an instance of a
class that facilitates the delayed evaluation. Sqrt, Atan etc. are
wrappers to the math functions to facilitate delayed evaluation. When
I encounter a point object, I could 'evaluate' the above mapping for
the point object to get the target dictonary.
The actual requirements are much more involved than this. The
motivation of the design was to enable application developers (who are
not experts in python) to be able to write the mappings. The mappings
also need to be readable.
You could consider this to be some sort of DSL. However, because of
the number of rules involved, I am trying to be as close to Python
expressions as possible. If the target setting is to be a tuple, for
example, I want to be able to write the tuple directly as "( expr1,
expr2 )", rather than, say, "Tuple(expr1, expr2)".
There is also a requirement to validate the mapping on load so that
run-time errors are minimized.
The system we are developing is quite reusable and we have been able
to use it for three different mappings so far. At this point, I am
trying to profile the code and noticed that a non-trivial amount of
time is being spent in the particular function I mentioned in this
thread.

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


Re: what's the command for (cd ..) in python

2011-09-10 Thread Waldek M.
On Sat, 10 Sep 2011 21:11:32 +1000, Steven D'Aprano wrote:
> The main one that comes to mind is os.walk, which has this to say:
> 
> Caution:  if you pass a relative pathname for top, don't change the
> current working directory between resumptions of walk.  walk never
> changes the current directory, and assumes that the client doesn't
> either.
> 
> Seems like a reasonable assumption to me.

Oh, that kind of functions.
Yes, that *is* surely reasonable, just as mixing chdir/mkdir/remove
and whatever else that affects the directory structure is not a good idea,
or at least something to be done carefully.
I just wouldn't think to give it some special credit.

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


Re: How to structure packages

2011-09-10 Thread Littlefield, Tyler

On 9/10/2011 4:11 AM, Nobody wrote:

On Fri, 09 Sep 2011 11:37:44 +1000, Chris Angelico wrote:


The Java compiler also acts as a "make" program. If it doesn't find
a .class file for a needed class, it will search for the corresponding
.java file and compile that. So to compile a complex program, you only
need to compile the top-level file (e.g. HelloWorld.java), and it will
compile everything which is required. No Makefile is needed, as the
relationship between classes, object files and source files is fixed.


If that's the entire benefit, then I think this is a rather hefty
price to pay for the elimination of a makefile.

It also eliminates the need for TAGS files, browser database (PDB) files,
etc. Once you know the class name, all of the filenames follow from that.

I suspect that the one-to-one correspondence between classes and .class
files is mostly technical (e.g. Java's security model). The one-to-one
correspondence between class files and source files could probably be
relaxed, but at the expense of complicating the IDE and toolchain.

I never saw it as a problem, given that Java is fundamentally class-based:
there are no global variables or functions, only classes.

Sure there are no global variables, but having one class per file is one 
of the big things I hate about Java. Sure it keeps things organized, but 
that's a bit to much for me.




--

Take care,
Ty
Web: http://tds-solutions.net
The Aspen project: a light-weight barebones mud engine
http://code.google.com/p/aspenmud

Sent from my toaster.

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


Re: test if a subclass inherits a superclass method

2011-09-10 Thread Littlefield, Tyler

On 9/10/2011 5:58 AM, Kayode Odeyemi wrote:

Hello,

I'm testing Python's class abstractness and inheritance. Since 
interface doesn't exist, I will
like to test how to have access to a superclass method from a subclass 
without necessary

invoking or overriding the superclass method in its subclass.

>>> class Equipment(object):
... def fault():
... return "fault"
...
>>> Equipment().__class__

>>> class Vehicle(Equipment):
...  # Find out here if Vehicle has access to fault

I want to know whether Vehicle has access to Equipment's fault() method.
Just want to know if it's there(that a vehicle can also develop a fault).

I know I can override it, but I want to know if I can use it directly 
without overriding it.


Perhaps this helps:
http://stackoverflow.com/questions/610883/how-to-know-if-an-object-has-an-attribute-in-python


--
Odeyemi 'Kayode O.
http://www.sinati.com. t: @charyorde




--

Take care,
Ty
Web: http://tds-solutions.net
The Aspen project: a light-weight barebones mud engine
http://code.google.com/p/aspenmud

Sent from my toaster.

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


Re: Applying a function recursively

2011-09-10 Thread Roy Smith
In article 
<4ee53496-ebec-4ee5-be0c-de344ac58...@y39g2000prd.googlegroups.com>,
 hetchkay  wrote:

[complicated description elided]
> You could consider this to be some sort of DSL. However, because of
> the number of rules involved, I am trying to be as close to Python
> expressions as possible. If the target setting is to be a tuple, for
> example, I want to be able to write the tuple directly as "( expr1,
> expr2 )", rather than, say, "Tuple(expr1, expr2)".
> There is also a requirement to validate the mapping on load so that
> run-time errors are minimized.

I assume by DSL you mean Domain Specific Language?  Having worked with a 
number of DSLs, my emphatic recommendation is DON'T DO IT!

What you will end up with is a language which is almost, but not quite, 
like some other existing language.  That means the people who use it 
will need to learn something new.  If you make it "as close to Python as 
possible", all that will happen is people will assume it's Python, and 
constantly be surprised when things don't work the same way.

Whatever features of "real Python" you left out, people will invariably 
be clamoring for.  Eventually, you will be forced to duct-tape them into 
the language.  You will end up bogged down forever in language 
maintenance, adding features, fixing bugs, writing documentation, 
providing tech support, etc.

Think of all the ecosystem stuff which grows up around a real language.  
Debuggers.  Profilers.  Language-aware editors (or plugins to emacs, 
eclipse, etc).  Add-in libraries to do a zillion things you never 
thought you would want to do.  Hoards of really smart and enthusiastic 
people on mailing lists, newsgroups, Stack Overflow, etc willing to help 
out with problems.  Books.  Professional training courses.  You will end 
up with replicating all that, or doing without.

It sounds like what you really want to do is just use Python as your 
scripting language.  The lazy evaluation features you desire can be 
handled by writing some library code.
-- 
http://mail.python.org/mailman/listinfo/python-list


Is there anyway to use urllib2 to download a file from http server?

2011-09-10 Thread crow
As the title.

Or is there other module that can handle this task?

Many thanks in advance
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Is there anyway to use urllib2 to download a file from http server?

2011-09-10 Thread Stefan Behnel

crow, 10.09.2011 17:41:

As the title.

Or is there other module that can handle this task?


Did you notice that is has documentation?

http://docs.python.org/library/urllib2.html#examples

Stefan

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


Re: Doctest failing

2011-09-10 Thread Chris Angelico
On Sat, Sep 10, 2011 at 10:24 PM, Alister Ware
 wrote:
> Ignoring the docttests my process would be to process each word & then
> manually capitalize he 1st word, .I would als0 use a comprehension as
> makes for cleaner code:-
>
> def capitalize(word):
>    if word in small_words:
>        return word
>    else:
>        return word.title()

And I'd do this with a lambda, but that's just me. Of course, if your
logic is more complicated, it makes more sense to keep it in a named
function, but a single conditional call can fit nicely into a lambda.

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


Re: [ANN] Shed Skin 0.9

2011-09-10 Thread Terry Reedy

On 9/10/2011 7:08 AM, Mark Dufour wrote:

Hi all,

I have just released version 0.9 of Shed Skin, a (restricted-)Python to
C++ compiler.


That should say "Python2.(4-6) to C++ compiler".

I do not want to pick on Mark, who I think is doing great work, but I 
find it annoying when third-party developers use the generic 'Python' to 
refer to undisclosed older versions of Python.



Please see my blog for the full announcement:

http://shed-skin.blogspot.com 


While Mark never reveals this crucial information anywhere on this page,


The Shed Skin homepage is located here:

http://shedskin.googlecode.com 


he does specify versions in the first sentence of this page. That is 
much better than some other projects.


--
Terry Jan Reedy

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


Re: Is there anyway to use urllib2 to download a file from http server?

2011-09-10 Thread Abhijeet Mahagaonkar
I guess urlretrieve() would do the job

-AB

On Sat, Sep 10, 2011 at 9:11 PM, crow  wrote:

> As the title.
>
> Or is there other module that can handle this task?
>
> Many thanks in advance
> --
> http://mail.python.org/mailman/listinfo/python-list
>
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Doctest failing

2011-09-10 Thread Terry Reedy

On 9/10/2011 7:20 AM, Tigerstyle wrote:

Hi guys.

I'm strugglin with some homework stuff and am hoping you can help me
out here.


We appreciate you saying so instead of hiding that this is homework.


small_words = ('into', 'the', 'a', 'of', 'at', 'in', 'for', 'on')

def book_title(title):
 """ Takes a string and returns a title-case string.
 All words EXCEPT for small words are made title case
 unless the string starts with a preposition, in which
 case the word is correctly capitalized.
 >>>  book_title('DIVE Into python')
 'Dive into Python'
 >>>  book_title('the great gatsby')
 'The Great Gatsby'
 >>>  book_title('the WORKS OF AleXANDer dumas')
 'The Works of Alexander Dumas'
 """
 new_title = []
 title_split = title.strip().lower().split()



 for word in title_split:
 if title_split[0] in small_words:
 new_title.append(word.title())
 elif word in small_words:
 new_title.append(word.lower())
 else:
 new_title.append(word.title())


The key issue is that you want to treat the first word one way (.title 
it) and the rest differently (conditionally .title or not) . So 
immediately separate the first from the rest and then process each. 
There are at least three ways to do the split. Perhaps I should stop 
with this hint, and certainly you should think a bit before reading 
further, but here is what I consider to be the most elegant 3.2 code.

.
,
,
,
.
.
.
first, *rest = title.strip().lower().split()
new_title = [first.title()]
for word in rest:
if word not in small_words:
word = word.title()
new_title.append(word)


 return(' '.join(new_title))


doctest.testmod() now passes (there is no 'refactory' here)


def _test():
 import doctest, refactory
 return doctest.testmod(refactory)
if __name__ == "__main__":
 _test()


--
Terry Jan Reedy

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


Idioms combining 'next(items)' and 'for item in items:'

2011-09-10 Thread Terry Reedy
Python's iterator protocol for an iterator 'items' allows combinations 
of explicit "next(items)" calls with the implicit calls of a "for item 
in items:" loop. There are at least three situations in which this can 
be useful. (While the code posted here is not testable, being incomplete 
or having pseudocode lines, I have tested full examples of each idiom 
with 3.2.)



1. Process first item of an iterable separately.

A traditional solution is a flag variable that is tested for each item.

first = True

for item in iterable:
  if first:

first = False
  else:


(I have seen code like this posted on this list several times, including 
today.)


Better, to me, is to remove the first item *before* the loop.

items = iter(iterable)


Sometimes  and  can be combined in with next(items)>. For instance, "result = []" followed by 
"result.append(process_first(item))" becomes "result = 
[process_first(item)]".


The statement containing the explicit next(items) call can optionally be 
wrapped to explicitly handle the case of an empty iterable in whatever 
manner is desired.


try:

except StopIteration:
raise ValueError("iterable cannot be empty")


For an iterable known to have a small number of items, the first item 
can be removed, with only a small copying penalty, with a simple assignment.


first, *rest = iterable

The older and harder to write version of this requires the iterable to 
be a sequence?


first, rest = seq[0], seq[1:]


2. Process the last item of an iterable differently. As far as I know, 
this cannot be done for a generic iterable with a flag. It requires a 
look ahead.


items = iter(iterable)
current = next(items)
for item in items:
  
  current = item


To treat both first and last differently, pull off the first before 
binding 'current'. Wrap next() calls as desired.



3. Process the items of an iterable in pairs.

items = iter(iterable)
for first in items:
second = next(items)


This time, StopIteration is raised for an odd number of items. Catch and 
process as desired. One possibility is to raise ValueError("Iterable 
must have an even number of items").


A useful example of just sometimes pairing is iterating a (unicode) 
string by code points ('characters') rather than by code units. This 
requires combining the surrogate pairs used for supplementary characters 
when the units are 16 bits.


chars = iter(string)
for char in chars:
if is_first_surrogate(char):
char += next(chars)
yield char

--
Terry Jan Reedy

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


Re: Doctest failing

2011-09-10 Thread Terry Reedy

On 9/10/2011 7:47 AM, Peter Otten wrote:


You can work around that with a
flag along these lines

first = True
for word in title_split:
 if first:
 # special treatment for the first word
 first = False
 else:
 # put checks for all words but the first here
 new_title.append(fixed_word) # assuming you have stored the titlecased
  # or lowercased word in the fixed_word
  # variable


An alternative to a flag and testing every item is to remove and process 
the first item *before* the loop. See my response on this thread or my 
new thread

Idioms combining 'next(items)' and 'for item in items:'

--
Terry Jan Reedy

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


Re: can't generate iterator from list

2011-09-10 Thread Dr. Phillip M. Feldman

Very nice explanation!  I've circumvented the problem by returning a
`deepcopy` of the list.  I've also added an acknowledgment.  The revised
code is attached.

I'd like to see both balls in numbered boxes (this code) and balls in
unnumbered (indistinguishable) boxes in Python's `itertools` module, but
don't know whom to contact about this.  Also, I haven't yet worked out a
good algorithm for balls in unnumbered boxes.  Any suggestions will be
appreciated.

Phillip http://old.nabble.com/file/p32439307/balls_in_numbered_boxes.py
balls_in_numbered_boxes.py 
-- 
View this message in context: 
http://old.nabble.com/can%27t-generate-iterator-from-list-tp32435519p32439307.html
Sent from the Python - python-list mailing list archive at Nabble.com.

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


[ANN] dispy: distribute computations and execute in parallel

2011-09-10 Thread Giridhar Pemmasani
Hello,

I would like to announce dispy (http://dispy.sf.net) that can
distribute and parallelize computations among computing nodes over
network (yes, yet another implementation of parallelization). This is
useful for problems in SIMD paradigm where a computation can be
executed with multiple data simultaneously. Salient features of dispy
are:

 * Computations (Python functions or standalone programs) and its
   dependencies (files, Python functions, classes, modules) are
   distributed automatically as and when needed.

 * Computation nodes can be anywhere on the network. For security,
   either simple hash based authentication or SSL encryption can be
   used.

 * A computation may specify which nodes are allowed to execute it
   (for now, using simple patterns of IP addresses).

 * After each execution is finished, the results of execution, output,
   errors and exception trace are made available for further
   processing.

 * Nodes may become available dynamically: dispy will schedule jobs
   whenever a node is available and computations can use that node. If
   a node fails while executing scheduled jobs, those jobs may be
   resubmitted to other nodes if computations allow it.

 * dispy can be used in a single process to use all the nodes
   exclusively (simpler to use) or in multiple processes sharing the
   nodes.

Currently dispy works with Python 2.7 and has been tested with nodes
running Linux and OS X.

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


Re: can't generate iterator from list

2011-09-10 Thread Dr. Phillip M. Feldman

I just realized that there is a defect in my algorithm, so I will try to code
this using a recursive algorithm instead.

-- 
View this message in context: 
http://old.nabble.com/can%27t-generate-iterator-from-list-tp32435519p32439439.html
Sent from the Python - python-list mailing list archive at Nabble.com.

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


optionparse: how to add a line break to the help text

2011-09-10 Thread Gelonida N
I'm having a small question about optionparse.

Normaly optionparser will format the help text according to the
console's width.

I just wondered if there is any way to insert a line breakk into an
options help text.

Example:

from optparse import OptionParser

parser = OptionParser()
parser.add_option("-f", action="store",
help="This option is really really complicated"
 " and I'd like to write"
 " a few paragrpahs to explain how it works."
 "\nHowever the line breaks are stripped off"
 " and it's thus difficult to structure the help text")

args = ['-h']
parser.parse_args(args)

Is there any trick to force a new paragraph/ line break before the word
'However'?


Thanks in advance for suggestions.


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


Re: Doctest failing

2011-09-10 Thread Peter Otten
Terry Reedy wrote:

> On 9/10/2011 7:47 AM, Peter Otten wrote:
> 
>> You can work around that with a
>> flag along these lines
>>
>> first = True
>> for word in title_split:
>>  if first:
>>  # special treatment for the first word
>>  first = False
>>  else:
>>  # put checks for all words but the first here
>>  new_title.append(fixed_word) # assuming you have stored the
>>  titlecased
>>   # or lowercased word in the fixed_word
>>   # variable
> 
> An alternative to a flag and testing every item is to remove and process
> the first item *before* the loop. See my response on this thread or my
> new thread
> Idioms combining 'next(items)' and 'for item in items:'

I reckoned the approach with the flag the most beginner-friendly because you 
don't have to think too hard about the corner-cases, namely

>>> book_title("")
''

When I use the "process first item before the loop" approach I usually end 
up with a helper generator

def _words(words, small_words={w.title(): w for w in small_words}):
yield next(words)
for word in words:
yield small_words[word] if word in small_words else word

def book_title(s):
return " ".join(_words(iter(s.title().split(

and the nagging thought that I'm making it more complex than need be.


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


Re: using python in web applications

2011-09-10 Thread Littlefield, Tyler

On 9/9/2011 10:19 PM, Ben Finney wrote:

"Littlefield, Tyler"  writes:


I'm curious if there are some good solutions for using Python in web
applications.

Start with:

http://docs.python.org/howto/webservers.html#frameworks>
http://wiki.python.org/moin/WebFrameworks>



Awesome, will do, thanks.


and try your criteria against what you find there.




--

Take care,
Ty
Web: http://tds-solutions.net
The Aspen project: a light-weight barebones mud engine
http://code.google.com/p/aspenmud

Sent from my toaster.

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


Re: IOError 35 when trying to read the result of call to urllib2.urlopen

2011-09-10 Thread matt
On Sep 9, 6:02 pm, Steven D'Aprano  wrote:
> matt wrote:
> > When I try to look at "resp_body" I get this error:
>
> > IOError: [Errno 35] Resource temporarily unavailable
>
> > I posted to the same URI using curl and it worked fine, so I don't
> > think it has to do with the server.
>
> Are your Python code and curl both using the same proxy? It may be that one
> is going direct and the other is using a proxy.
>
> Or perhaps the destination is just flaky, and the resource genuinely is
> temporarily unavailable. Or it doesn't like your useragent string and is
> lying.
>
> --
> Steven

No proxy.  It's all local over the loopback interface (probably should
have mentioned that).  The service I'm POSTing to is a Python web app
with CherryPy as the framework.  Also, because of some dependency
issues I'm using Python 2.6.
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: optionparse: how to add a line break to the help text

2011-09-10 Thread Rafael Durán Castañeda

On 10/09/11 22:43, Gelonida N wrote:

I'm having a small question about optionparse.

Normaly optionparser will format the help text according to the
console's width.

I just wondered if there is any way to insert a line breakk into an
options help text.

Example:

from optparse import OptionParser

parser = OptionParser()
parser.add_option("-f", action="store",
 help="This option is really really complicated"
  " and I'd like to write"
  " a few paragrpahs to explain how it works."
  "\nHowever the line breaks are stripped off"
  " and it's thus difficult to structure the help text")

args = ['-h']
parser.parse_args(args)

Is there any trick to force a new paragraph/ line break before the word
'However'?


Thanks in advance for suggestions.



You can use """ for multiple line texts:
>>> text = \
... """fsdfsfsdfsdf
... sfsdfsfsdf
... sdfsdf  s
...
... """
>>> text
'fsdfsfsdfsdf\nsfsdfsfsdf\nsdfsdf  s\n\n'


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


Re: Python and Outlook-style rules

2011-09-10 Thread Brian
On Sep 9, 5:19 pm, Alec Taylor  wrote:
> Something like this?
>
> http://stackoverflow.com/questions/387606/using-user-input-to-find-in...
>

Actually, I'm looking for a framework or something similar - something
more like this, only for python:
http://www.codeproject.com/KB/macros/RulesWizard.aspx
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: using python in web applications

2011-09-10 Thread Laurent
[troll]
For a serious web based MMO you'd rather stick to low level and forget about 
bloated Object Relational Mapping java-like layered kind of frameworks that are 
made for Rapid Applications Development, not for efficiency.
[/troll]

"Eve Online", a well known MMORPG was developped with stackless python : 
http://highscalability.com/eve-online-architecture

You mentioned nginx so I can tell you I personally use Linux + nginx + mongodb 
(pymongo) + Python 3 version of cherrypy (with Mako templates) and it's working 
fine after some tuning.
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: optionparse: how to add a line break to the help text

2011-09-10 Thread Robert Kern

On 9/10/11 5:16 PM, Rafael Durán Castañeda wrote:

On 10/09/11 22:43, Gelonida N wrote:

I'm having a small question about optionparse.

Normaly optionparser will format the help text according to the
console's width.

I just wondered if there is any way to insert a line breakk into an
options help text.

Example:

from optparse import OptionParser

parser = OptionParser()
parser.add_option("-f", action="store",
help="This option is really really complicated"
" and I'd like to write"
" a few paragrpahs to explain how it works."
"\nHowever the line breaks are stripped off"
" and it's thus difficult to structure the help text")

args = ['-h']
parser.parse_args(args)

Is there any trick to force a new paragraph/ line break before the word
'However'?


Thanks in advance for suggestions.



You can use """ for multiple line texts:
 >>> text = \
... """fsdfsfsdfsdf
... sfsdfsfsdf
... sdfsdf s
...
... """
 >>> text
'fsdfsfsdfsdf\n sfsdfsfsdf\nsdfsdf s\n\n'


That's not the problem. OptionParser removes all of the original newlines that 
are in the given text when it formats the text for display. I don't think there 
is a simple workaround.


--
Robert Kern

"I have come to believe that the whole world is an enigma, a harmless enigma
 that is made terrible by our own mad attempt to interpret it as though it had
 an underlying truth."
  -- Umberto Eco

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


Re: what's the command for (cd ..) in python

2011-09-10 Thread Cameron Simpson
On 10Sep2011 15:57, Waldek M.  wrote:
| On Sat, 10 Sep 2011 21:11:32 +1000, Steven D'Aprano wrote:
| > The main one that comes to mind is os.walk, which has this to say:
| > 
| > Caution:  if you pass a relative pathname for top, don't change the
| > current working directory between resumptions of walk.  walk never
| > changes the current directory, and assumes that the client doesn't
| > either.
| > 
| > Seems like a reasonable assumption to me.
| 
| Oh, that kind of functions.
| Yes, that *is* surely reasonable, just as mixing chdir/mkdir/remove
| and whatever else that affects the directory structure is not a good idea,
| or at least something to be done carefully.
| I just wouldn't think to give it some special credit.

It's like umask and friends - they affect a program-wide implicit state.
Umask affects default new file permissions, chdir affects starting point
for relative pathnames, etc. All implicit.

The usual biggie with chdir is that of course your program may have been
handed relative file pathnames on the command line. They need resolving
_before_ any chdiring happens, if chdiring is going to happen.

For this kind of reason chdir, umask et al are generally best used in
top level logic only rather than inside library functions.

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

It's hard to believe that something as rational as the metric system was
invented by the French. - Henry O. Farad 
-- 
http://mail.python.org/mailman/listinfo/python-list


import packet.module without importing packet.__init__ ?

2011-09-10 Thread Gelonida N
Hi,

I am little shaky with how exactly python imports packages / modules etc.

Is it possible to import a module from a packet without importing its
__init__.py ?


Full example:
==

# application.py -
print "starting application"
import mypacket.module1

# mypacket.__init__.py -
print "importing __init__"


# mypacket.module1.py -
print "importing module1"



The output, that I get with python 2.6 is
$ python application.py
> starting application
> importing __init__
> importing module1

Under certain circumstances I'd like to avoid the implicit import
of __init__.py

Due to other constrains of my project I am obliged to have a
non empty __init__.py




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


recursive algorithm for balls in numbered boxes

2011-09-10 Thread Dr. Phillip M. Feldman

I've written a recursive class that creates an iterator to solve a general
formulation of the combinatorics problem known as "balls in numbered boxes"
(also known as "indistinguishable balls in distinguishable boxes").  The
code has been extensively tested and appears to work, but isn't terribly
elegant.  Any suggestions about how to improve it will be appreciated.

Also, I'd like to get this functionality into the Python's `itertools`
module (the present set of combinatorics functions in `itertools` does not
include "balls in boxes").  Does anyone know whom I should contact about
this?

Phillip

http://old.nabble.com/file/p32440187/balls_in_numbered_boxes.py
balls_in_numbered_boxes.py 
-- 
View this message in context: 
http://old.nabble.com/recursive-algorithm-for-balls-in-numbered-boxes-tp32440187p32440187.html
Sent from the Python - python-list mailing list archive at Nabble.com.

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


Re: using python in web applications

2011-09-10 Thread Littlefield, Tyler

On 9/10/2011 5:35 PM, Laurent wrote:

[troll]
For a serious web based MMO you'd rather stick to low level and forget about 
bloated Object Relational Mapping java-like layered kind of frameworks that are 
made for Rapid Applications Development, not for efficiency.
[/troll]

I replied to that one off list I guess, but I figured Django was way 
more overhead than I wanted, doesn't really fit with solving the speed 
issue.



"Eve Online", a well known MMORPG was developped with stackless python : 
http://highscalability.com/eve-online-architecture

You mentioned nginx so I can tell you I personally use Linux + nginx + mongodb 
(pymongo) + Python 3 version of cherrypy (with Mako templates) and it's working 
fine after some tuning.


Awesome, thanks. I'm new to this, so some of that flew over my head, but 
I'll take a look at all of them. I'm not sure of the relevance of 
stackless in this case; I was looking into PyPy, but I'm not really sure 
whether that can be connected with nginx. I guess I could just write the 
web server in Python and use it from that point.


--

Take care,
Ty
Web: http://tds-solutions.net
The Aspen project: a light-weight barebones mud engine
http://code.google.com/p/aspenmud

Sent from my toaster.

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


Re: import packet.module without importing packet.__init__ ?

2011-09-10 Thread Steven D'Aprano
Gelonida N wrote:


> Is it possible to import a module from a packet without importing its
> __init__.py ?

Untested, but I think so. But you shouldn't. The solution (if it does work,
as I said I haven't tested it) is a hack.

Suppose you have a library like this:

modules/
+-- spam.py
+-- ham.py
+-- package/
+-- __init__.py
+-- cheese.py

The directory "modules" is in your Python path, so you can "import spam"
or "import package". The trick is to add "modules/package" to the Python
path so that "import cheese" will find cheese.py directly.

You can do that by manipulating sys.path, or by setting the environment
variable PYTHONPATH.

Another hack would be to add a hard link to the top level:

modules/
+-- spam.py
+-- ham.py
+-- cheese.py <+
+-- package/   |
+-- __init__.py|
+-- cheese.py <+


This is not a copy, it is a hard link: the same file appears in literally
two places. (But note that not all file systems support hard linking.) Soft
links or shortcuts might also work. Now you can "import cheese".

But generally, you should design your package so that it doesn't matter
whether or not __init__.py is imported first. I see three reasnable
situations:

(1) package.cheese should rely on package, in which case it requires
package.__init__ to be imported first. (Sometimes I put code common to the
entire package in __init__.py.)

(2) Just don't worry about the fact that package.__init__ gets imported
first. Do you have any idea how many modules get imported when Python
starts up? What's one more?

(3) If cheese.py truly is independent of package, then it shouldn't be part
of package. Just make it a stand alone module outside the package
directory, and be done. (Your installer can still treat cheese.py as a
dependency of package, and ensure that they are both installed.)



-- 
Steven

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


Re: optionparse: how to add a line break to the help text

2011-09-10 Thread Rhodri James
On Sat, 10 Sep 2011 23:16:42 +0100, Rafael Durán Castañeda  
 wrote:



On 10/09/11 22:43, Gelonida N wrote:

I'm having a small question about optionparse.

Normaly optionparser will format the help text according to the
console's width.

I just wondered if there is any way to insert a line breakk into an
options help text.

Example:

from optparse import OptionParser

parser = OptionParser()
parser.add_option("-f", action="store",
 help="This option is really really complicated"
  " and I'd like to write"
  " a few paragrpahs to explain how it works."
  "\nHowever the line breaks are stripped off"
  " and it's thus difficult to structure the help text")

args = ['-h']
parser.parse_args(args)

Is there any trick to force a new paragraph/ line break before the word
'However'?


Thanks in advance for suggestions.



You can use """ for multiple line texts:
 >>> text = \
... """fsdfsfsdfsdf
... sfsdfsfsdf
... sdfsdf  s
...
... """
 >>> text
'fsdfsfsdfsdf\nsfsdfsfsdf\nsdfsdf  s\n\n'


Unfortunately the help text is formatted using textwrap, which presumes  
that the entire text is a single paragraph.  To get paragraphs in the help  
text, you'll need to write an IndentedHelpFormatter subclass that splits  
the text on "\n\n", textwraps the split string individually, then re-joins  
them.  _format_text() and format_option() look like the methods that would  
need replacing.


--
Rhodri James *-* Wildebeest Herder to the Masses
--
http://mail.python.org/mailman/listinfo/python-list


Re: optionparse: how to add a line break to the help text

2011-09-10 Thread Gelonida N
Hi James,

On 09/11/2011 03:12 AM, Rhodri James wrote:
> On Sat, 10 Sep 2011 23:16:42 +0100, Rafael Durán Castañeda
>  wrote:
> 
>> On 10/09/11 22:43, Gelonida N wrote:
>>>
>>> from optparse import OptionParser
>>>
>>> parser = OptionParser()
>>> parser.add_option("-f", action="store",
>>>  help="This option is really really complicated"
>>>   " and I'd like to write"
>>>   " a few paragrpahs to explain how it works."
>>>   "\nHowever the line breaks are stripped off"
>>>   " and it's thus difficult to structure the help text")
>>>
>>> args = ['-h']
>>> parser.parse_args(args)
>>>
>>> Is there any trick to force a new paragraph/ line break before the word
>>> 'However'?
>>

> 
> Unfortunately the help text is formatted using textwrap, which presumes
> that the entire text is a single paragraph.  To get paragraphs in the
> help text, you'll need to write an IndentedHelpFormatter subclass that
> splits the text on "\n\n", textwraps the split string individually, then
> re-joins them.  _format_text() and format_option() look like the methods
> that would need replacing.
> 

Thanks a lot. Good to know, that there are options, though a little more
complicated than expected.

I'll live withou tthe line break for the time being and will deep dive
lateron, when I really want to insist on pragraphs within a help section.

I like the idea of '\n\n' as paragraph markes.
Long term this could probably even become an enhancement for  optparse
(with an explicit option to enable it in order to break no existing code)


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


Re: optionparse: how to add a line break to the help text

2011-09-10 Thread Tim Chase

On 09/10/11 20:54, Gelonida N wrote:

Unfortunately the help text is formatted using textwrap, which presumes
that the entire text is a single paragraph.  To get paragraphs in the
help text, you'll need to write an IndentedHelpFormatter subclass that
splits the text on "\n\n", textwraps the split string individually, then
re-joins them.  _format_text() and format_option() look like the methods
that would need replacing.


Thanks a lot. Good to know, that there are options, though a little more
complicated than expected.


Just in case you want it:

http://bytes.com/topic/python/answers/734066-how-output-newline-carriage-return-optparse

it's come up several times and several years ago I hacked 
together exactly the solution Rhodri mentions.


-tkc


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


Re: Doctest failing

2011-09-10 Thread ting
On Sep 10, 7:47 am, Peter Otten <__pete...@web.de> wrote:
> Tigerstyle wrote:
> > I'm strugglin with some homework stuff and am hoping you can help me
> > out here.
>
> > This is the code:
>
> > small_words = ('into', 'the', 'a', 'of', 'at', 'in', 'for', 'on')
> >     new_title = []
> >     title_split = title.strip().lower().split()
> >     for word in title_split:
> >         if title_split[0] in small_words:
> >             new_title.append(word.title())
> >         elif word in small_words:
> >             new_title.append(word.lower())
> >         else:
> >             new_title.append(word.title())
>
> The logic of the for-loop is flawed; the expression
>
> title_split[0] in small_words
>
> will always evaluate to True if the first word is a "small word", even when
> the loop is already past the first word. You can work around that with a
> flag along these lines
>
> first = True
> for word in title_split:
>     if first:
>         # special treatment for the first word
>         first = False
>     else:
>         # put checks for all words but the first here
>     new_title.append(fixed_word) # assuming you have stored the titlecased
>                                  # or lowercased word in the fixed_word
>                                  # variable

Another way to tackle this is to just capitalize the entire sentence
before returning it.

new_title = []
title_split = title.strip().lower().split()
for word in title_split:
  if word in small_words:
new_title.append(word.lower())
  else:
new_title.append(word.title())
return(''.join(new_title).capitalize())

However, I'm only helping with your homework, because I want to point
out that doctest comments should be *readable*. Don't just throw them
in, take the extra 10-15 seconds to make them easier on the eyes. In
the workplace, months will pass before you review this code again, so
you want to make it easier for an older version of yourself to
remember it.

And it takes very little effort to make it readable. Here's your
doctest string, reformatted with just a tiny bit more whitespace. I
even cut out a sentence.

def book_title(title):
  """
  All words EXCEPT for small words are made title case
  unless the string starts with a preposition, in which
  case the word is correctly capitalized.

  >>> book_title('DIVE Into python')
  'Dive into Python'
  >>> book_title('the great gatsby')
  'The Great Gatsby'
  >>> book_title('the WORKS OF AleXANDer dumas')
  'The Works of Alexander Dumas'
  """
--
// T.Hsu
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: recursive algorithm for balls in numbered boxes

2011-09-10 Thread Chris Rebert
On Sat, Sep 10, 2011 at 5:43 PM, Dr. Phillip M. Feldman
 wrote:
> I've written a recursive class that creates an iterator to solve a general
> formulation of the combinatorics problem known as "balls in numbered boxes"
> (also known as "indistinguishable balls in distinguishable boxes").  The
> code has been extensively tested and appears to work, but isn't terribly
> elegant.  Any suggestions about how to improve it will be appreciated.

Significantly refactored (but untested) version:
https://gist.github.com/1209079

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


Re: optionparse: how to add a line break to the help text

2011-09-10 Thread Gelonida N
Hi Tim,


Thanks a lot!!!



On 09/11/2011 04:08 AM, Tim Chase wrote:
> On 09/10/11 20:54, Gelonida N wrote:
>>> Unfortunately the help text is formatted using textwrap, which presumes
>>> that the entire text is a single paragraph.  To get paragraphs in the
>>> help text, you'll need to write an IndentedHelpFormatter subclass that
>>> splits the text on "\n\n", textwraps the split string individually, then
>>> re-joins them.  _format_text() and format_option() look like the methods
>>> that would need replacing.
> 
> Just in case you want it:
> 
> http://bytes.com/topic/python/answers/734066-how-output-newline-carriage-return-optparse
> 

It works (of course ;-) ) like a charm.
Good to know, that I'm not the only one who want's to structure the help
text a little nicer.

> 
> it's come up several times and several years ago I hacked together
> exactly the solution Rhodri mentions.

Considering, that you posted the snippet in 2007 and this is very
probably a reocurring problem for any slighty more complicated help text
it is really a pity, that it did not become of part of the standard
optparse library :-(


Thanks again.


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


Re: using python in web applications

2011-09-10 Thread Laurent
Well PyPy is just an implementation of Python among many others (but limited to 
version 2.7). It is not a web server. If you want to make PyPy interact with a 
web server (such as nginx) you have to use a special protocol such as WSGI or 
Fast-CGI. For best performances you can for instance use uWSGI that integrates 
well with nginx but for this you have to recompile nginx. As you see it's a bit 
complex, you should read the wikis. May the Force be with you.
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: import packet.module without importing packet.__init__ ?

2011-09-10 Thread Gelonida N
Hi Steven,

Thanks for your answer.

On 09/11/2011 02:56 AM, Steven D'Aprano wrote:
> Gelonida N wrote: 
>> Is it possible to import a module from a packet without importing its
>> __init__.py ?
> 
> Untested, but I think so. But you shouldn't. The solution (if it does work,
> as I said I haven't tested it) is a hack.
> 
> Suppose you have a library like this:
. . .
. . .
> The directory "modules" is in your Python path, so you can "import spam"
> or "import package". The trick is to add "modules/package" to the Python
> path so that "import cheese" will find cheese.py directly.
> 

Yes it's a hack, but good to know it exists. Just in case.

> Another hack would be to add a hard link to the top level:
> 

As you said: Hard links would be a little annoying on some file systems.

> 
> But generally, you should design your package so that it doesn't matter
> whether or not __init__.py is imported first. I see three reasnable
> situations:
> 
> (1) package.cheese should rely on package, in which case it requires
> package.__init__ to be imported first. (Sometimes I put code common to the
> entire package in __init__.py.)
> 
> (2) Just don't worry about the fact that package.__init__ gets imported
> first. Do you have any idea how many modules get imported when Python
> starts up? What's one more?
> 
> (3) If cheese.py truly is independent of package, then it shouldn't be part
> of package. Just make it a stand alone module outside the package
> directory, and be done. (Your installer can still treat cheese.py as a
> dependency of package, and ensure that they are both installed.)
> 


There's still something, that I am philosophycally missing.

Wy do I have to import the entire tree if I'm just interested in a leave.

this is not always desired.

let's imagine

kitchen.__init__
kitchen.knives
kitchen.pans
kitchen.pots


or alternatively
os
os.path
os.wheteverelse

Let's assume, that  kitchen/__init__.py is importing the sub modules
knives, pans and pots

then I understand of course, that the next lines would import the entire
kitchen

> import kitchen # will import all activities
> kitchen.pots.boil(egg)


But if I know that I just want to boil some eggs I would really
appreciate if I could just say

> from kitchen.pot import boil
> boil(eggs)
without having the side effect of getting pans and knives also loaded
into memory.

Moving pots out into a separate package doesn't really feel right.
I agree, that a 10 level deep package tree is not really desirable, but
having every package flat on the top level doesn't sound that good either.

So it seems, that developers of huge packages shouldn't really do a lot
in __init__.py.

Otherwise any user would always 'suck in' the entiry package with all
its sub modules.




To give more details about why I asked this question:

The real setup looks a little different and there are some issues, that
I am having:

One package in question is a huge django application.

Within the package is one module defining some constants and tiny
functions, which I would like to reuse from some command line tools,
which should start quickly without loading any of the django specific
modules. (the startup penalty and the memory overhead would be noticable)

I am using rpc4django, which expects, that __init__.py of the django
application exports some all rpc functions
which will basically import 95% of the django application and the entire
django frame work (none of which were required by my command tool,
support utility for this application)


I could of course create a separate package just for this tiny sub
module, but somehow it doesn't feel right to me.


The second issue is a little more complicated. basically I end up having
circular dependencies.

as I have two applications that  send a few customs django signals forth
an back.

If I wouldn't import __init__.py or if I moved the declaration of the
signals into a third package then circular dependencies would disappear.


So it seems, that if I can't avoid loading __init__.py in
a nice way I have to create  one or two tiny packages outside of my
django application,
or change the python path  as you proposed (increases of course name
space clashes)

or to find a way for rpc4django of loading / locating the rpc functions
without putting them  in __init__.py change the rpc functions such, that
they will only import the other sub modules when being called the first
time (proxies / delayed imports, . . .)



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


Re: using python in web applications

2011-09-10 Thread Chris Angelico
On Sun, Sep 11, 2011 at 9:35 AM, Laurent  wrote:
> [troll]
> For a serious web based MMO you'd rather stick to low level and forget about 
> bloated Object Relational Mapping java-like layered kind of frameworks that 
> are made for Rapid Applications Development, not for efficiency.
> [/troll]

I have been trolled. :)

For any sort of web-based real-time game, here's two things to avoid:

1) Snoopability and modifiability of crucial data. Everything should
be stored on the server, so that if anyone fiddles, all they do is
break their own client so it has to reload from the server.
2) Massive waste of resources due to unnecessary frameworks/facilities.

I play a lot of Flash games, and right now I'm playing one that has
coped poorly with a miniature slashdotting. A spike in popularity
resulted in a spike in server lag. I fired up a little packet sniffer,
and discovered that every time you do anything in the game, the client
makes a new POST request to its server. Why?! It's not that hard to
hold a socket connection open!

In another context, a similar issue - not a problem as yet (that I
know of), but would be if the system were to be slashdotted. A server
whose sole purpose is to handle script-instigated requests (using HTTP
POST for its transport) sends back a Set-Cookie header with a session
id. This implies that the server is holding session data for all these
clients that are never going to send that cookie back. The server
appears to be IIS with ASP scripts, so presumably stateful sessions
are enabled by default, so it's costing memory and processing to hold,
then discard, all those useless (and probably empty) session state
tags. And presumably many MANY other servers have the same thing.

What's YOUR framework doing that you don't need, and how much is it costing you?

Yeah, I was trolled bad. :)

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


Deadlock problem using multiprocessing

2011-09-10 Thread 蓝色基因
This is my first touch on the multiprocessing module, and I admit not
having a deep understanding of parallel programming, forgive me if
there's any obvious error. This is my test code:

# deadlock.py

import multiprocessing

class MPTask:
def __init__(self):
self._tseq= range(10)   # task sequence
self._pool= multiprocessing.Pool(2) # process pool
def _exe(self, num):
return num**2
def run(self):
result= self._pool.map_async(self._exe, self._tseq)
return result.get()

result= MPTask().run()
print(result)

And seemingly it creates a deadlock, I have to manually kill the
processes in the system monitor, yet it would be OK if the _exe()
function was defined outside the MPTask class:

# no_deadlock.py
import multiprocessing

def _exe(num):
return num**2

class MPTask:
def __init__(self):
self._tseq= range(10)   # task sequence
self._pool= multiprocessing.Pool(2) # process pool
def run(self):
result= self._pool.map_async(_exe, self._tseq)
return result.get()

result= MPTask().run()
print(result)

This would give the correct answer without any deadlock. My questions:

1. Why is this, where did the deadlock come from?

2. Besides, is there any material I can refer to about how to avoid
deadlocks when using multiple processes in the program?

Thanks!

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


Re: Idioms combining 'next(items)' and 'for item in items:'

2011-09-10 Thread Ian Kelly
On Sat, Sep 10, 2011 at 1:36 PM, Terry Reedy  wrote:
> The statement containing the explicit next(items) call can optionally be
> wrapped to explicitly handle the case of an empty iterable in whatever
> manner is desired.
>
> try:
>    
> except StopIteration:
>    raise ValueError("iterable cannot be empty")

The only time that's optional is when you're writing an iterator and
the try-except would end up looking like this:

try:
# do stuff with next(items)
except StopIteration:
raise StopIteration

And even then, it's probably a good idea to clearly document that
you're allowing a StopIteration from one iterator to propagate up as a
StopIteration for another.

Apart from that case, whenever you call next() you should always be
prepared to catch a StopIteration.  Letting a StopIteration propagate
up the stack to parts unknown is bad juju because it's a flow control
exception, not an error-signaling exception.  If it happens to
propagate up to another for loop, then it will break out of the for
loop, and the exception will simply be swallowed.

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


convert time

2011-09-10 Thread 守株待兔
how can i convert "Dec 11" into  2011-12?-- 
http://mail.python.org/mailman/listinfo/python-list


Re: import packet.module without importing packet.__init__ ?

2011-09-10 Thread Steven D'Aprano
Gelonida N wrote:


> There's still something, that I am philosophycally missing.
> 
> Wy do I have to import the entire tree if I'm just interested in a leave.

You don't. Python just imports the branches leading to the leaf, not the
entire tree.

[...]
> But if I know that I just want to boil some eggs I would really
> appreciate if I could just say
> 
>> from kitchen.pot import boil
>> boil(eggs)
> without having the side effect of getting pans and knives also loaded
> into memory.

In your example, you stated that kitchen explicitly imports kitchen.pans and
kitchen.knives. So in that case, take it up with the designer of the
kitchen package -- it was his decision to import them, just like he
imported re and math and httplib. (For example.) 

If kitchen is your package, then it is your choice: if you want to have
control over when sub-packages get imported, then don't automatically
import them in __init__.py. If you want them to be automatically imported,
like os and os.path, then automatically import them. If you don't, don't.

But note that you cannot expect to import kitchen.pot without importing
kitchen first.


> One package in question is a huge django application.
> 
> Within the package is one module defining some constants and tiny
> functions, which I would like to reuse from some command line tools,
> which should start quickly without loading any of the django specific
> modules. (the startup penalty and the memory overhead would be noticable)
> 
> I am using rpc4django, which expects, that __init__.py of the django
> application exports some all rpc functions
> which will basically import 95% of the django application and the entire
> django frame work (none of which were required by my command tool,
> support utility for this application)
> 
> 
> I could of course create a separate package just for this tiny sub
> module, but somehow it doesn't feel right to me.

Why should it be a package? Just make it a stand-alone module.

Or use path manipulation from your command line tool to import it on its
own. It might be somewhat of a hack, but if need to do it, do so.

Or do something like this:

my_app/
+--  __init__.py  # lightweight, nearly empty
+--  cmd_tools.py
+--  module1.py
+--  module2.py
+--  rpc/
 +-- __init__.py  # huge, imports my_app.module1, my_app.module2, etc.


then point rpc4django at my_app.rpc instead of my_app.



-- 
Steven

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


Re: convert time

2011-09-10 Thread Chris Rebert
2011/9/10 守株待兔 <1248283...@qq.com>:
> how can i convert "Dec 11" into  2011-12?

Read the fine manuals for the `time` or `datetime` modules.
http://docs.python.org/library/datetime.html

>>> from datetime import datetime
>>> datetime.strptime("Dec 11", "%b %y")
datetime.datetime(2011, 12, 1, 0, 0)
>>> datetime.strptime("Dec 11", "%b %y").strftime("%Y-%m")
'2011-12'

Note that this code depends on an appropriate locale setting for
parsing the month name abbreviations.

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


Re: convert time

2011-09-10 Thread Steven D'Aprano
守株待兔 wrote:

> how can i convert "Dec 11" into  2011-12?


if my_str == "Dec 11":
return 1999  # 2011 - 12


Does that help?

But seriously... 2011-12 is not a proper date, so the simplest way is
probably something like this:

def convert(date_str):
month, short_year = date_str.split()
if len(short_year) == 4:
year = short_year
else:
year = '20' + short_year
months = 'Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec'.split()
month = months.index(month) + 1
return year + '-' + str(month)

Otherwise see the time and datetime modules:

http://docs.python.org/library/time.html
http://docs.python.org/library/datetime.html


-- 
Steven

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


Re: killing a script

2011-09-10 Thread Steven D'Aprano
Cameron Simpson wrote:

> On 10Sep2011 11:25, Steven D'Aprano 
> wrote:
> | Cameron Simpson wrote:
> | > My copy of the 2.7 docs says:
> | >   This is implemented by calling the Standard C function system(), and
> | >   has the same limitations.
> | > and sure enough, "man 3 system" says:
> | 
> | I don't consider having to look up documentation for a function in a
> | completely different language (in this case, C) as "documented behaviour
> | of os.system".
> 
> You're kidding, surely? 

No, I meant exactly what I said, but I suspect that you misunderstood what I
said. I blame myself for not making myself more clear, sorry.


> A wrapper function for a system supplied function 
> should recite everything about the wrapped function's behaviour (including
> the system dependent stuff) in the wrapper doco?

Heavens no, I certainly don't mean that. That would be silly. 

What I mean is that in the context of discussing Python library
functions, "documented behaviour" refers to what the Python docs state,
namely the function docstring and the docs at http://docs.python.org/ (or
the 3.x version). Third-party documentation doesn't count: not blogs,
not "some guy sent me an email", and not documentation for other tools
either.

So if you describe a feature of os.system as "documented", I'm going to
understand that as *Python* documentation. Hence my question about where it
is documented. If we're discussing external documentation, we should say so
up front: not all Python users are using CPython, and not all Python coders
know C and have access to the Linux man pages.


-- 
Steven

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


Re: optionparse: how to add a line break to the help text

2011-09-10 Thread Ben Finney
Gelonida N  writes:

> Considering, that you posted the snippet in 2007 and this is very
> probably a reocurring problem for any slighty more complicated help
> text it is really a pity, that it did not become of part of the
> standard optparse library :-(

The ‘optparse’ library is, as the online documentation shows
http://docs.python.org/library/optparse.html>, deprecated for new
code:

The optparse module is deprecated and will not be developed further;
development will continue with the argparse module.

The standard library ‘argparse’ module has a feature you might prefer
http://docs.python.org/library/argparse.html#formatter-class>.

-- 
 \  “An expert is a man who has made all the mistakes which can be |
  `\ made in a very narrow field.” —Niels Bohr |
_o__)  |
Ben Finney
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: convert time

2011-09-10 Thread Ben Finney
Steven D'Aprano  writes:

> But seriously... 2011-12 is not a proper date

It's valid by ISO 8601. The standard allows any number of parts to be
dropped, from least to most significant, in order to have a value with
deliberately reduced precision.

   
https://secure.wikimedia.org/wikipedia/en/wiki/ISO_8601#General_principles>

> Otherwise see the time and datetime modules:

>>> import datetime
>>> text = "2011-12"
>>> datetime.datetime.strptime(text, "%Y-%m")
datetime.datetime(2011, 12, 1, 0, 0)

-- 
 \“It's not what you pay a man, but what he costs you that |
  `\ counts.” —Will Rogers |
_o__)  |
Ben Finney
-- 
http://mail.python.org/mailman/listinfo/python-list