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 vijna...@gmail.com
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-27 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 ros...@gmail.com wrote:

 On Wed, Aug 27, 2014 at 3:19 PM, Steven D'Aprano st...@pearwood.info 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


[issue7768] raw_input should encode unicode prompt with std.stdout.encoding.

2010-01-23 Thread Naoki INADA

New submission from Naoki INADA songofaca...@gmail.com:

raw_input and input should take unicode prompt and encode with 
sys.stdout.encoding like print or input in py3k.

 u = uあいう
 print u
あいう
 x = raw_input(u)
Traceback (most recent call last):
  File stdin, line 1, in module
UnicodeEncodeError: 'ascii' codec can't encode characters in position 0-2: 
ordinal not in range(128)
 import sys
 sys.stdout.encoding
'cp932'

--
components: IO, Unicode
messages: 98208
nosy: naoki
severity: normal
status: open
title: raw_input should encode unicode prompt with std.stdout.encoding.
versions: Python 2.6, Python 2.7

___
Python tracker rep...@bugs.python.org
http://bugs.python.org/issue7768
___
___
Python-bugs-list mailing list
Unsubscribe: 
http://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



[issue1754] WindowsError messages are not properly encoded

2010-01-14 Thread Naoki INADA

Naoki INADA songofaca...@gmail.com added the comment:

I think WindowsError's message should be English like other errors.
FormatMessageW() function can take dwLanguageId parameter.
So I think Python should pass `MAKELANGID(LANG_ENGLISH, SUBLANG_ENGLISH_US)` to 
the parameter.

--
nosy: +naoki

___
Python tracker rep...@bugs.python.org
http://bugs.python.org/issue1754
___
___
Python-bugs-list mailing list
Unsubscribe: 
http://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



[issue7072] isspace(0xa0) is true on Mac OS X

2009-10-06 Thread Naoki INADA

New submission from Naoki INADA songofaca...@gmail.com:

Old FreeBSD's libc has a bug relate to utf-8 locale and Python have 
patch for it: http://svn.python.org/view/python/trunk/Include/pyport.h?
view=diffpathrev=43219r1=36792r2=36793

This bug appears in Mac OS X again. This test fails:
 s = '\xa0'
 assert s.strip() == s
 import locale
 locale.setlocale(locale.LC_ALL, 'en_US.UTF-8')
'en_US.UTF-8'
 assert s.strip() == s

So above patch should be enabled for Mac OS X.

--
messages: 93650
nosy: naoki
severity: normal
status: open
title: isspace(0xa0) is true on Mac OS X
type: behavior
versions: Python 2.6, Python 2.7, Python 3.0, Python 3.1, Python 3.2

___
Python tracker rep...@bugs.python.org
http://bugs.python.org/issue7072
___
___
Python-bugs-list mailing list
Unsubscribe: 
http://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



[issue5911] built-in compile() should take encoding option.

2009-10-03 Thread Naoki INADA

Naoki INADA songofaca...@gmail.com added the comment:

add sample implementation.

--
keywords: +patch
Added file: http://bugs.python.org/file15030/compile_with_encoding.patch

___
Python tracker rep...@bugs.python.org
http://bugs.python.org/issue5911
___
___
Python-bugs-list mailing list
Unsubscribe: 
http://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



[issue6991] logging encoding failes some situation

2009-09-25 Thread Naoki INADA

Changes by Naoki INADA songofaca...@gmail.com:


--
type:  - behavior

___
Python tracker rep...@bugs.python.org
http://bugs.python.org/issue6991
___
___
Python-bugs-list mailing list
Unsubscribe: 
http://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



[issue6991] logging encoding failes some situation

2009-09-25 Thread Naoki INADA

Naoki INADA songofaca...@gmail.com added the comment:

Please see and execute an attached foo.py.

In Python 2.6.2, this cause following error:
python foo.py
Traceback (most recent call last):
  File foo.py, line 3, in module
f.write('\xaa')
  File C:\usr\Python2.6\lib\codecs.py, line 686, in write
return self.writer.write(data)
  File C:\usr\Python2.6\lib\codecs.py, line 351, in write
data, consumed = self.encode(object, self.errors)
UnicodeDecodeError: 'ascii' codec can't decode byte 0xaa in position 0:
ordinal not in ran
ge(128)

--
status: pending - open
Added file: http://bugs.python.org/file14971/foo.py

___
Python tracker rep...@bugs.python.org
http://bugs.python.org/issue6991
___
___
Python-bugs-list mailing list
Unsubscribe: 
http://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



[issue6991] logging encoding failes some situation

2009-09-24 Thread Naoki INADA

New submission from Naoki INADA songofaca...@gmail.com:

When stream is codecs.writer object, stream.write(string) does
string.decode() internally and it may cause UnicodeDecodeError.

Then, fallback to utf-8 is not good.
I think good fallback logic is:
* When message is unicode, message.encode(stream.encoding or 'ascii',
'backslashreplace')
* When message is bytes, message.encode('string_escape')

Attached patch contains this logic, refactoring and test.

--
components: Library (Lib)
files: logging_encode.patch
keywords: patch
messages: 93100
nosy: naoki
severity: normal
status: open
title: logging encoding failes some situation
versions: Python 2.6, Python 2.7
Added file: http://bugs.python.org/file14970/logging_encode.patch

___
Python tracker rep...@bugs.python.org
http://bugs.python.org/issue6991
___
___
Python-bugs-list mailing list
Unsubscribe: 
http://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



[issue6991] logging encoding failes some situation

2009-09-24 Thread Naoki INADA

Changes by Naoki INADA songofaca...@gmail.com:


--
versions: +Python 3.0, Python 3.1, Python 3.2

___
Python tracker rep...@bugs.python.org
http://bugs.python.org/issue6991
___
___
Python-bugs-list mailing list
Unsubscribe: 
http://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



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


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


What is file.encoding convention?

2009-07-22 Thread Naoki INADA
In document http://docs.python.org/library/
stdtypes.html#file.encoding:

 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 stdin, line 1, in module
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


[issue6255] PyInt_FromSize_t is undocumented.

2009-06-10 Thread Naoki INADA

New submission from Naoki INADA songofaca...@gmail.com:

PyInt_FromSize_t() is not in Python/C API document.
People seeing document may be not able to find how to make int from 
unsigned long.

--
assignee: georg.brandl
components: Documentation
messages: 89208
nosy: georg.brandl, naoki
severity: normal
status: open
title: PyInt_FromSize_t is undocumented.
versions: Python 2.6, Python 2.7

___
Python tracker rep...@bugs.python.org
http://bugs.python.org/issue6255
___
___
Python-bugs-list mailing list
Unsubscribe: 
http://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



[issue6255] PyInt_FromSize_t is undocumented.

2009-06-10 Thread Naoki INADA

Naoki INADA songofaca...@gmail.com added the comment:

You're right. PyInt_FromSize_t() isn't safe for unsigned long.

 Maybe a PyInt_FromUnsignedLong method would be useful?  It would be 
trivial to 
 implement.

I hope that all of py3k's PyInt_From** are in Python 2.x.
It makes maintaining extension module for both of Py2.x and Py3k a bit 
easier.

--

___
Python tracker rep...@bugs.python.org
http://bugs.python.org/issue6255
___
___
Python-bugs-list mailing list
Unsubscribe: 
http://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



[issue5911] built-in compile() should take encoding option.

2009-05-02 Thread Naoki INADA

New submission from Naoki INADA songofaca...@gmail.com:

The built-in compile() expects source is encoded in utf-8.
This behavior make it harder to implement alternative shell
like IDLE and IPython. (http://bugs.python.org/issue1542677 and
https://bugs.launchpad.net/ipython/+bug/339642 are related bugs.)

Below is current compile() behavior.

# Python's interactive shell in Windows cp932 console.
 あ
'\x82\xa0'
 uあ
u'\u3042'

# compile() fails to decode str.
 code = compile('uあ', '__interactive__', 'single')
 exec code
u'\x82\xa0'  # u'\u3042' expected.

# compile() encodes unicode to utf-8.
 code = compile(u'あ', '__interactive__', 'single')
 exec code
'\xe3\x81\x82' # '\x82\xa0' (cp932) wanted, but I get utf-8.

Currentry, using PEP0263 like below is needed to get compile
code in expected encoding.

 code = compile('# coding: cp932\n%s' % ('あ',), '__interactive__', 
'single')
 exec code
'\x82\xa0'
 code = compile('# coding: cp932\n%s' % ('uあ',), '__interactive__', 
'single')
 exec code
u'\u3042'

But I feel compile() with PEP0263 is bit dirty hack.
I think adding a 'encoding' argument that have a 'utf-8' as default value to
compile() is cleaner way and it doesn't break backward compatibility.

Following example is describe behavior of compile() with encoding option.

# coding: utf-8 (in utf-8 context)
code = compile('あ', '__foo.py', 'single')
exec code #= '\xe3\x81\x82'

code = compile('あ', '__foo.py', 'single', encoding='cp932') = 
UnicodeDecodeError

code = compile(u'あ', '__foo.py', 'single')
exec code #= '\xe3\x81\x82'

code = compile(u'あ', '__foo.py', 'single', encoding='cp932')
exec code #= '\x82\xa0'

--
components: None
messages: 86994
nosy: naoki
severity: normal
status: open
title: built-in compile() should take encoding option.
type: feature request
versions: Python 2.6, Python 2.7

___
Python tracker rep...@bugs.python.org
http://bugs.python.org/issue5911
___
___
Python-bugs-list mailing list
Unsubscribe: 
http://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



[issue1542677] IDLE shell gives different len() of unicode strings compared to Python shell

2009-04-12 Thread Naoki INADA

Naoki INADA songofaca...@gmail.com added the comment:

utf-8 is not locale encoding.

 f = open('á.txt')

If this line compiled into utf-8 and locale encoding is not utf-8, can't 
open 'á.txt'.

IMHO, in case of Python 2.x, correct approach is fix IOBindings.encoding  
and compile() with pep0263.

--

___
Python tracker rep...@bugs.python.org
http://bugs.python.org/issue1542677
___
___
Python-bugs-list mailing list
Unsubscribe: 
http://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



[issue1542677] IDLE shell gives different len() of unicode strings compared to Python shell

2009-04-12 Thread Naoki INADA

Naoki INADA songofaca...@gmail.com added the comment:

How to use locale.getpreferredencoding() instead of 
locale.nl_langinfo(locale.CODESET).

--- IOBinding.py.back   Sun Apr 12 19:54:52 2009
+++ IOBinding.pySun Apr 12 20:02:58 2009
@@ -35,40 +35,16 @@
 # Encoding for file names
 filesystemencoding = sys.getfilesystemencoding()
 
-encoding = ascii
-if sys.platform == 'win32':
-# On Windows, we could use mbcs. However, to give the user
-# a portable encoding name, we need to find the code page
-try:
-encoding = locale.getdefaultlocale()[1]
-codecs.lookup(encoding)
-except LookupError:
-pass
-else:
-try:
-# Different things can fail here: the locale module may not be
-# loaded, it may not offer nl_langinfo, or CODESET, or the
-# resulting codeset may be unknown to Python. We ignore all
-# these problems, falling back to ASCII
-encoding = locale.nl_langinfo(locale.CODESET)
-if encoding is None or encoding is '':
-# situation occurs on Mac OS X
-encoding = 'ascii'
-codecs.lookup(encoding)
-except (NameError, AttributeError, LookupError):
-# Try getdefaultlocale well: it parses environment variables,
-# which may give a clue. Unfortunately, getdefaultlocale has
-# bugs that can cause ValueError.
-try:
-encoding = locale.getdefaultlocale()[1]
-if encoding is None or encoding is '':
-# situation occurs on Mac OS X
-encoding = 'ascii'
-codecs.lookup(encoding)
-except (ValueError, LookupError):
-pass
+encoding = utf-8
 
-encoding = encoding.lower()
+preferredencoding = None
+try:
+preferredencoding = locale.getpreferredencoding()
+codecs.lookup(preferredencoding)
+encoding = preferredencoding.lower()
+except LookupError:
+pass
+del preferredencoding
 
 coding_re = re.compile(coding[:=]\s*([-\w_.]+))

--

___
Python tracker rep...@bugs.python.org
http://bugs.python.org/issue1542677
___
___
Python-bugs-list mailing list
Unsubscribe: 
http://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



[issue1542677] IDLE shell gives different len() of unicode strings compared to Python shell

2009-04-11 Thread Naoki INADA

Naoki INADA songofaca...@gmail.com added the comment:

This patch is for iplib/PyShell.py#ModifiedInterpreter.runsource.

 if isinstance(source, types.UnicodeType):
 import IOBinding
 try:
 source = source.encode(IOBinding.encoding)
+source = # coding: %s\n%s % (IOBinding.encoding, source)
 except UnicodeError:

--

___
Python tracker rep...@bugs.python.org
http://bugs.python.org/issue1542677
___
___
Python-bugs-list mailing list
Unsubscribe: 
http://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com