Re: CLI framework using python

2014-10-14 Thread Naoki INADA
Click_ is another CLI framework.

It support multi-level nested command like git and it has some nice utilities.

I love it's design.






.. _click: http://click.pocoo.org/3/




—
Sent from Mailbox

On Tue, Oct 14, 2014 at 10:35 PM, vijnaana bhairava 
wrote:

> Hi Folks,
> The requirement is to develop a CLI framework in python for a linux router.
> The suggestions i got is to use PyCli/Cliff. Not sure which would be the 
> right choice!  Also, a few APIs are mentioned here: 
> https://pythonhosted.org/pyCLI/#module-cli.app
> Since i couldn't find any actual implementation which uses pyCli,
> i can't figure out how to make use of pyCLI.
> Another question i have is whether it uses argparse?
> If so, what value add does PYCLI do?
> Regards,
> vij
> On Thursday, October 9, 2014 5:50:51 PM UTC+5:30, vijnaana bhairava wrote:
>> Hi,
>> 
>> 
>> 
>> I need to develop a python CLI framework.
>> 
>> 
>> 
>> For example if i need to set an ip address in linux:
>> 
>> 
>> 
>> ifconfig eth0 172.16.25.125
>> 
>> 
>> 
>> I should be able to use python to do the above.
>> 
>> 
>> 
>> 1. The user will execute a python script to which i will pass the params 
>> eth0 and ip address (something like ifconf.py  eth0 172.16.25.125)
>> 
>> 
>> 
>> 2. Within the script i grab the params and do something to the effect of 
>> user executing 'ifconfig eth0 172.16.25.125' from the shell.
>> 
>> 
>> 
>> 3. There are other such commands for which i will be using python scripts. I 
>> came across pyCLI, but it doesn't have much documentation, so couldn't 
>> figure out how to move forward.
>> 
>> 
>> 
>> 4. The CLI framework needs to reuse code so i didn't want to use pure python 
>> and develop a framework from scratch. Rather use something like pyCLI/CLIFF.
>> 
>> 
>> 
>> The problem is lack of documentation with examples on how to use the above.
>> 
>> 
>> 
>> Any guidance would be greatly appreciated.
>> 
>> 
>> 
>> Regards & Thanks,
>> 
>> Vij
> -- 
> https://mail.python.org/mailman/listinfo/python-list-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Reading from sys.stdin reads the whole file in

2014-08-26 Thread Naoki INADA
I recommend Python 3.

On Python 2, iterating lines without buffering is slow, tricky and ugly.




for line in iter(sys.stdin.readline(), ''):

    print line
—
Sent from Mailbox

On Wed, Aug 27, 2014 at 3:03 PM, Chris Angelico  wrote:

> On Wed, Aug 27, 2014 at 3:19 PM, Steven D'Aprano  wrote:
>> When I pipe one to the other, I expect each line to be printed as they
>> arrive, but instead they all queue up and happen at once:
> You're seeing two different problems here. One is the flushing of
> stdout in out.py, as Marko mentioned, but it's easily proven that
> that's not the whole issue. Compare "python out.py" and "python
> out.py|cat" - the latter will demonstrate whether or not it's getting
> flushed properly (the former, where stdout is a tty, will always flush
> correctly).
> But even with that sorted, iterating over stdin has issues in Python
> 2. Here's a tweaked version of your files (note that I cut the sleeps
> to 2 seconds, but the effect is the same):
> rosuav@sikorsky:~$ cat out.py
> import time
> print("Hello...",flush=True)
> time.sleep(2)
> print("World!",flush=True)
> time.sleep(2)
> print("Goodbye!",flush=True)
> rosuav@sikorsky:~$ cat slurp.py
> from __future__ import print_function
> import sys
> import time
> for line in sys.stdin:
> print(time.ctime(), line)
> rosuav@sikorsky:~$ python3 out.py|python slurp.py
> Wed Aug 27 16:00:16 2014 Hello...
> Wed Aug 27 16:00:16 2014 World!
> Wed Aug 27 16:00:16 2014 Goodbye!
> rosuav@sikorsky:~$ python3 out.py|python3 slurp.py
> Wed Aug 27 16:00:19 2014 Hello...
> Wed Aug 27 16:00:21 2014 World!
> Wed Aug 27 16:00:23 2014 Goodbye!
> rosuav@sikorsky:~$
> With a Py2 consumer, there's still buffering happening. With a Py3
> consumer, it works correctly. How to control the Py2 buffering,
> though, I don't know.
> ChrisA
> -- 
> https://mail.python.org/mailman/listinfo/python-list-- 
https://mail.python.org/mailman/listinfo/python-list


Re: What is file.encoding convention?

2009-07-23 Thread Naoki INADA
>
> Yes! I confused by it.

s/I confused/I am confused/

> "Writing unicode to a file(-like)" is a simple requirement.
> Does python have any simple resolution for it?

s/resolution/solution/

Sorry about my poor English.
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: What is file.encoding convention?

2009-07-23 Thread Naoki INADA
>  What is the encoding of sys.stderr in your example?
Sorry, I missed. It is cp932

> So the problem is essentially this: if a stream has an encoding
> attribute, sometimes it is a wrapped stream which encodes Unicode
> (e.g. a stream obtained via the codecs module) and sometimes it is not
> (e.g. sys.stdout, sys.stderr).

Yes! I confused by it.

>> The encoding that this file uses. When Unicode strings are written to a file,
>>  they will be converted to byte strings using this encoding. In addition,
>> when the file is connected to a terminal, the attribute gives the encoding
>> that the terminal is likely to use

I feel this doc means "file object with encoding attribute encodes
unicode
regardless it is tty or not" and sys.stdout/stderr defies convention.

If this doc means "file object encodes unicode if it isn't tty.", I
should write
like below::

if not afile.isatty():
if getattr(afile, "encoding") is not None:
afile.write(unicode_str)
elif getattr(afile, "encoding") is not None:
afile.write(unicode_str.encode(afile.encoding))
else:
afile.write(unicode_str.encode(fallback_encoding)) # utf8,
defaultencoding, preferedencoding, ...


"Writing unicode to a file(-like)" is a simple requirement.
Does python have any simple resolution for it?
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: What is file.encoding convention?

2009-07-23 Thread Naoki INADA
> What is file.encoding convention?
> If I want to write a unicode string to a file(-like) that have
> encoding attribute, I should do
> (1) try: file.write(unicode_str),
> (2) except UnicodeEncodeError: file.write(unicode_str.encode
> (file.encoding))
> like logging?
> It seems agly.

s/agly/ugly/
-- 
http://mail.python.org/mailman/listinfo/python-list


What is file.encoding convention?

2009-07-22 Thread Naoki INADA
In document :

>> The encoding that this file uses. When Unicode strings are written to a file,
>>  they will be converted to byte strings using this encoding. In addition,
>> when the file is connected to a terminal, the attribute gives the encoding
>> that the terminal is likely to use

But in logging.StreamHandler.emit() ::

try:
if (isinstance(msg, unicode) and
getattr(stream, 'encoding', None)):
#fs = fs.decode(stream.encoding)
try:
stream.write(fs % msg)
except UnicodeEncodeError:
#Printing to terminals sometimes fails.
For example,
#with an encoding of 'cp1251', the above
write will
#work if written to a stream opened or
wrapped by
#the codecs module, but fail when writing
to a
#terminal even when the codepage is set to
cp1251.
#An extra encoding step seems to be
needed.
stream.write((fs % msg).encode
(stream.encoding))
else:
stream.write(fs % msg)
except UnicodeError:
stream.write(fs % msg.encode("UTF-8"))

And behavior of sys.stdout in Windows::
>>> import sys
>>> sys.stdout.encoding
'cp932'
>>> u = u"あいう"
>>> u
u'\u3042\u3044\u3046'
>>> print >>sys.stdout, u
あいう
>>> sys.stderr.write(u)
Traceback (most recent call last):
  File "", line 1, in 
UnicodeEncodeError: 'ascii' codec can't encode characters in position
0-2: ordinal not in range(128)


What is file.encoding convention?
If I want to write a unicode string to a file(-like) that have
encoding attribute, I should do
(1) try: file.write(unicode_str),
(2) except UnicodeEncodeError: file.write(unicode_str.encode
(file.encoding))
like logging?
It seems agly.
-- 
http://mail.python.org/mailman/listinfo/python-list