How to print a number as if in the python interpreter?

2012-07-06 Thread Peng Yu
Hi,

In [2]: sum([.1, .1, .1, .1, .1, .1, .1, .1, .1, .1])
Out[2]: 0.

In ipython, I got the above output. But I got a different output from
"print". Is there a way to print exact what I saw in ipython?

~/linux/test/python/man/library/math/fsum$ cat main.py
#!/usr/bin/env python
print sum([.1, .1, .1, .1, .1, .1, .1, .1, .1, .1])
~/linux/test/python/man/library/math/fsum$ ./main.py
1.0

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


pip fails to install packages on moutain loin (Mac OS 10.8.2)

2012-10-18 Thread Peng Yu
Hi,

I installed Python using python-2.7.3-macosx10.6.dmg on my Mac OS
10.8.2.

When try to use pip to install packages, I get the following message.
Then the installation fails.

gcc-4.2 not found, using clang instead


I then create a link from /usr/bin/gcc to gcc-4.2. Then I run pip
again, I get the following error message.

Does anybody have a solution to install python on Mac OS 10.8.2 so
that packages can be installed with pip?

/Developer/SDKs/MacOSX10.6.sdk/usr/include/c++/4.2.1/exception:
42:28: error: bits/c++config.h: No such file or directory
In file included from /Developer/SDKs/MacOSX10.6.sdk/usr/include/c+
+/4.2.1/bits/stl_algobase.h:70,
 from /Developer/SDKs/MacOSX10.6.sdk/usr/include/c+
+/4.2.1/bits/char_traits.h:46,
 from /Developer/SDKs/MacOSX10.6.sdk/usr/include/c+
+/4.2.1/string:47,
 from /Developer/SDKs/MacOSX10.6.sdk/usr/include/c+
+/4.2.1/stdexcept:44,

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


How to access the document for __call__ from command line?

2012-10-18 Thread Peng Yu
Hi,

reference.pdf from python document has the following description. It
is not accessible from help() in the command line. Is there an
alternative so that I can quickly access these class attributes or
method names from the command line?

object.__call__(self [, args... ])
Called when the instance is “called” as a function; if this method is
defined, x(arg1, arg2, ...) is a
shorthand for x.__call__(arg1, arg2, ...).

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


How to print python commands automatically?

2012-11-08 Thread Peng Yu
Hi,

In bash, set -v will print the command executed. For example, the
following screen output shows that the "echo" command is printed
automatically. Is there a similar thing in python?

~/linux/test/bash/man/builtin/set/-v$ cat main.sh
#!/usr/bin/env bash

set -v
echo "Hello World!"
~/linux/test/bash/man/builtin/set/-v$ ./main.sh
echo "Hello World!"
Hello World!



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


Re: How to print python commands automatically?

2012-11-09 Thread Peng Yu
> Is this what you want?
> http://docs.python.org/2/library/trace.html

I'm not able to get the mixing of the python command screen output on
stdout. Is there a combination of options for this purpose?

~/linux/test/python/man/library/trace$ cat main1.py
#!/usr/bin/env python

def f():
  print "Hello World!"

f()
~/linux/test/python/man/library/trace$ cat main.sh
#!/usr/bin/env bash

python -m trace --count -C . main1.py -t

~/linux/test/python/man/library/trace$ ./main.sh
Hello World!
~/linux/test/python/man/library/trace$ cat main1.cover
   #!/usr/bin/env python

1: def f():
1:   print "Hello World!"

1: f()



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


Re: How to print python commands automatically?

2012-11-09 Thread Peng Yu
> Try with just --trace?
>
>
> C:\ramit>python.exe -m trace test.py
> C:\ramit\Python27\lib\trace.py: must specify one of --trace, --count, 
> --report, --listfuncs, or --trackcalls
>
> C:\ramit>python -m trace --trace test.py
>  --- modulename: test, funcname: 
> test.py(2): def f():
> test.py(5): f()
>  --- modulename: test, funcname: f
> test.py(3): print "Hello World!"
> Hello World!
>  --- modulename: trace, funcname: _unsettrace
> trace.py(80): sys.settrace(None)

I have to explicitly specify the modules I want to ignore. Is there a
way to ignore all the modules by default?

~/linux/test/python/man/library/trace/import$ cat.sh main.py main.sh test.py
==> main.py <==
#!/usr/bin/env python

import test

test.test()


==> main.sh <==
#!/usr/bin/env bash

python -m trace --trace main.py


==> test.py <==
def test1():
  print "Hello World!"

def test():
  test1()

~/linux/test/python/man/library/trace/import$ python -m trace --trace
--ignore-module=test main.py
 --- modulename: main, funcname: 
main.py(3): import test
main.py(5): test.test()
Hello World!
 --- modulename: trace, funcname: _unsettrace
trace.py(80): sys.settrace(None)


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


Why queue.empty() returns False even after put() is called?

2012-11-23 Thread Peng Yu
Hi,

The empty() returns True even after put() has been called. Why it is
empty when there some items in it? Could anybody help me understand
it? Thanks!

~/linux/test/python/man/library/multiprocessing/Queue/empty$ cat
main.py
#!/usr/bin/env python

import multiprocessing

queue = multiprocessing.Queue()
print queue.empty()
queue.put(['a', 'b'])
queue.put(['c', 'd'])
print queue.empty()


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


The default locale of sorted()

2012-12-03 Thread Peng Yu
Hi,

I'm not able to find the documentation on what locale is used for
sorted() when the 'cmp' argument is not specified. Could anybody let
me what the default is? If I always want LC_ALL=C, do I need to
explicitly set the locale? Or it is the default?

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


python module development workflow

2012-04-11 Thread Peng Yu
Hi,

It is confusing to me what the best workflow is for python module
development. There is setup.py, egg. Also, pip, easy_install.

Could any expert suggest an authoritative and complete guide for
developing python modules? Thanks!

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


Re: python module development workflow

2012-04-11 Thread Peng Yu
On Apr 11, 10:25 am, John Gordon  wrote:
> In <2900f481-fbe9-4da3-a7ca-5485d1ceb...@m13g2000yqc.googlegroups.com> Peng 
> Yu  writes:
>
> > It is confusing to me what the best workflow is for python module
> > development. There is setup.py, egg. Also, pip, easy_install.
>
> It's unclear what you are asking.
>
> How to develop your own modules?
>
> How to package and distribute your own modules once they're finished?

I'm asking these two questions.
-- 
http://mail.python.org/mailman/listinfo/python-list


Why variable used in list comprehension available outside?

2012-05-02 Thread Peng Yu
Hi,

The following example demonstrates the variable 'v' used in the list
comprehension is accessible out site the list comprehension.

I think that 'v' should be strictly local. Does anybody know where
this behavior is documented and why it is designed this way?

~/linux/test/python/man/library/__buildin__/class/{/iteritems$ cat main1.py
#!/usr/bin/env python

d = {'one': 10, 'two': 20}

for k, v in d.iteritems():
  print k, v
  x=[2*v for v in [1, 2, 3]]
  print x
  print k, v
~/linux/test/python/man/library/__buildin__/class/{/iteritems$ ./main1.py
two 20
[2, 4, 6]
two 3
one 10
[2, 4, 6]
one 3


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


When convert two sets with the same elements to lists, are the lists always going to be the same?

2012-05-03 Thread Peng Yu
Hi,

list(a_set)

When convert two sets with the same elements to two lists, are the
lists always going to be the same (i.e., the elements in each list are
ordered the same)? Is it documented anywhere?

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


Re: When convert two sets with the same elements to lists, are the lists always going to be the same?

2012-05-04 Thread Peng Yu
On Thu, May 3, 2012 at 11:16 PM, Terry Reedy  wrote:
> On 5/3/2012 8:36 PM, Peng Yu wrote:
>>
>> Hi,
>>
>> list(a_set)
>>
>> When convert two sets with the same elements to two lists, are the
>> lists always going to be the same (i.e., the elements in each list are
>> ordered the same)? Is it documented anywhere?
>
>
> "A set object is an unordered collection of distinct hashable objects".
> If you create a set from unequal objects with equal hashes, the iteration
> order may (should, will) depend on the insertion order as the first object
> added with a colliding hash will be at its 'natural position in the hash
> table while succeeding objects will be elsewhere.
>
> Python 3.3.0a3 (default, May  1 2012, 16:46:00)
>>>> hash('a')
> -292766495615408879
>>>> hash(-292766495615408879)
> -292766495615408879
>>>> a = {'a', -292766495615408879}
>>>> b = {-292766495615408879, 'a'}
>>>> list(a)
> [-292766495615408879, 'a']
>>>> list(b)
> ['a', -292766495615408879]

Thanks. This is what I'm looking for. I think that this should be
added to the python document as a manifestation (but nonnormalized) of
what "A set object is an unordered collection of distinct hashable
objects" means.

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


Re: When convert two sets with the same elements to lists, are the lists always going to be the same?

2012-05-04 Thread Peng Yu
On Fri, May 4, 2012 at 6:21 AM, Chris Angelico  wrote:
> On Fri, May 4, 2012 at 8:14 PM, Peng Yu  wrote:
>> Thanks. This is what I'm looking for. I think that this should be
>> added to the python document as a manifestation (but nonnormalized) of
>> what "A set object is an unordered collection of distinct hashable
>> objects" means.
>
> There are other things that can prove it to be unordered, too; the
> exact pattern and order of additions and deletions can affect the
> iteration order. The only thing you can be sure of is that you can't
> be sure of it.

I agree. My point was just to suggest adding more explanations on the
details in the manual.

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


Re: When convert two sets with the same elements to lists, are the lists always going to be the same?

2012-05-04 Thread Peng Yu
On Fri, May 4, 2012 at 12:43 PM, Terry Reedy  wrote:
> On 5/4/2012 8:00 AM, Peng Yu wrote:
>>
>> On Fri, May 4, 2012 at 6:21 AM, Chris Angelico  wrote:
>>>
>>> On Fri, May 4, 2012 at 8:14 PM, Peng Yu  wrote:
>>>>
>>>> Thanks. This is what I'm looking for. I think that this should be
>>>> added to the python document as a manifestation (but nonnormalized) of
>>>> what "A set object is an unordered collection of distinct hashable
>>>> objects" means.
>>>
>>>
>>> There are other things that can prove it to be unordered, too; the
>>> exact pattern and order of additions and deletions can affect the
>>> iteration order. The only thing you can be sure of is that you can't
>>> be sure of it.
>>
>>
>> I agree. My point was just to suggest adding more explanations on the
>> details in the manual.
>
>
> I am not sure how much clearer we can be in the language manual. The word
> 'unordered' means just that. If one imposes an arbitrary linear order on an
> unordered collection, it is arbitrary. It is frustrating that people do not
> want to believe that, and even write tests depending on today's arbitrary
> serialization order being deterministic indefinitely. There is a section
> about this in the doctest doc, but people do it anyway. I will think about a
> sentence to add.

You can just add the example that you posted to demonstrate what the
unordered means. A curious user might want to know under what
condition the "unorderness" can affect the results, because for
trivial examples (like the following), it does seem that there is some
orderness in a set.

set(['a', 'b', 'c'])
set(['c', 'b', 'a'])

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


Re: When convert two sets with the same elements to lists, are the lists always going to be the same?

2012-05-04 Thread Peng Yu
On Fri, May 4, 2012 at 6:12 PM, Cameron Simpson  wrote:
> On 04May2012 15:08, Peng Yu  wrote:
> | On Fri, May 4, 2012 at 12:43 PM, Terry Reedy  wrote:
> | > On 5/4/2012 8:00 AM, Peng Yu wrote:
> | >> On Fri, May 4, 2012 at 6:21 AM, Chris Angelico  wrote:
> | >>> On Fri, May 4, 2012 at 8:14 PM, Peng Yu  wrote:
> | >>>> Thanks. This is what I'm looking for. I think that this should be
> | >>>> added to the python document as a manifestation (but nonnormalized) of
> | >>>> what "A set object is an unordered collection of distinct hashable
> | >>>> objects" means.
> | >>>
> | >>> There are other things that can prove it to be unordered, too; the
> | >>> exact pattern and order of additions and deletions can affect the
> | >>> iteration order. The only thing you can be sure of is that you can't
> | >>> be sure of it.
> | >>
> | >> I agree. My point was just to suggest adding more explanations on the
> | >> details in the manual.
> | >
> | > I am not sure how much clearer we can be in the language manual. The word
> | > 'unordered' means just that. [...]
> |
> | You can just add the example that you posted to demonstrate what the
> | unordered means. A curious user might want to know under what
> | condition the "unorderness" can affect the results, because for
> | trivial examples (like the following), it does seem that there is some
> | orderness in a set.
>
> I'm with Terry here: anything else in the line you suggest would
> complicate things for the reader, and potentially mislead.
>
> Future implementation changes (and, indeed, _other_ implementations like
> Jython) can change any of this. So there _are_ no ``condition the
> "unorderness" can affect the results'': a set is unordered, and you
> could even _legitimately_ get different orders from the same set
> if you iterate over it twice. It is unlikely, but permissable.
>
> Any attempt to describe such conditions beyond "it might happen at any
> time" would be misleading.
>
> | set(['a', 'b', 'c'])
> | set(['c', 'b', 'a'])
>
> The language does not say these will get the same iteration order. It
> happens that the Python you're using, today, does that.
>
> You can't learn the language specification from watching behaviour;
> you learn the guarrenteed behaviour -- what you may rely on happening --
> from the specification, and you can test that an implementation obeys (or
> at any rate, does not disobey) the specification by watching behaviour.
>
> You seem to be trying to learn the spec from behaviour.

My point is if something is said in the document, it is better to be
substantiated by an example. I don't think that this has anything with
"learn the spec from behaviour."

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


Re: When convert two sets with the same elements to lists, are the lists always going to be the same?

2012-05-05 Thread Peng Yu
Hi Terry,

Thank you for you detailed email.

> If two collections are equal, should the iteration order be the same? It has
> always been true that if hash values collide, insertion order matters.
> However, a good hash function avoids hash collisions as much as possible in
> practical use cases. Without doing something artificial, as I did with the
> example, collisions should be especially rare on 64-bit builds. If one
> collection has a series of additions and deletions so that the underlying
> hash table has a different size than an equal collection build just from
> insertions, then order will also be different.

The reason that I asked to add the artificial example in the doc is
because I never completely understand the unorderness until I see your
artificial example. You will surely use more words for explaining what
"unorderness" means than just showing your example. And the example
(since formatted as code) is more eye catching than just plain text.

For my case, since I didn't understand the unorderness, I made some
subtle bug in my program, which works fine in my testing code.
However, it produce a small amount of corrupted results for the real
production use, which is harder to debug. It did wasted quite some of
my time.

> For the doc, the problem is what to say and where without being repetitous
> (and to get multiple people to agree ;-).

I agree that people have different opinions on issues like this. But I
think that "The Customer Is God". Readers of the doc is the customers,
the writers of the doc is the producers. The opinion of customers
should carry more weight than producers.

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


Re: When convert two sets with the same elements to lists, are the lists always going to be the same?

2012-05-05 Thread Peng Yu
> Documentation that takes ten pages to say something is just as bad as
> documentation that leaves stuff out, because it's almost guaranteed
> that it won't be read.

That's the point. If a simple example (6 lines) can demonstrate the
concept, why spending "ten pages" to explain it. My experience is that
for certain things, it is better describe by a spec once you know it,
but it is certainly not true for people to learn it. A reasonable
strategy is to interleave spec with demonstrating examples. There is
no excuse to not to make the manual easier to read.

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


Is there something like head() and str() of R in python?

2017-11-19 Thread Peng Yu
Hi, R has the functions head() and str() to show the brief content of
an object. Is there something similar in python for this purpose?

For example, I want to inspect the content of the variable "train".
What is the best way to do so? Thanks.

$ cat demo.py
from __future__ import division, print_function, absolute_import

import tflearn
from tflearn.data_utils import to_categorical, pad_sequences
from tflearn.datasets import imdb

# IMDB Dataset loading
train, test, _ = imdb.load_data(path='imdb.pkl', n_words=1,
valid_portion=0.1)

# 
https://raw.githubusercontent.com/llSourcell/How_to_do_Sentiment_Analysis/master/demo.py

-- 
Regards,
Peng
-- 
https://mail.python.org/mailman/listinfo/python-list


How to get the redirected URL only but not the actual content?

2017-12-01 Thread Peng Yu
Hi,

Does anybody know how only get the redirected URL but not the actual content?

I guess the request module probably should be used. But I am not sure
how to do it exactly.

Can somebody show me the best way to request
(https://doi.org/10.1109/5.771073) and get the URL
(http://ieeexplore.ieee.org/document/771073/)? Thanks.

-- 
Regards,
Peng
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: How to get the redirected URL only but not the actual content?

2017-12-01 Thread Peng Yu
Where is `?reload=true` from? How to just get the redict URL that one
would get from the browser? Thanks.

> 'http://ieeexplore.ieee.org:80/document/771073/?reload=true'

-- 
Regards,
Peng
-- 
https://mail.python.org/mailman/listinfo/python-list


Anything similar to __END__ in perl

2017-12-07 Thread Peng Yu
Hi, perl has __END__ which ignore all the lines below it.

Is there anything similar to __END__ in python? Thanks.

-- 
Regards,
Peng
-- 
https://mail.python.org/mailman/listinfo/python-list


What is wrong with this regex for matching emails?

2017-12-17 Thread Peng Yu
Hi,

I would like to extract "[email protected]". But it only shows ".hij".
Does anybody see what is wrong with it? Thanks.

$ cat main.py
#!/usr/bin/env python
# vim: set noexpandtab tabstop=2 shiftwidth=2 softtabstop=-1 fileencoding=utf-8:

import re
email_regex = re.compile('[a-zA-Z0-9_.+-]+@[a-zA-Z0-9-]+(\.[a-zA-Z0-9-]+)')
s = '[email protected].'
for email in re.findall(email_regex, s):
print email

$ ./main.py
.hij

-- 
Regards,
Peng
-- 
https://mail.python.org/mailman/listinfo/python-list


How to edit a function in an interactive python session?

2017-12-20 Thread Peng Yu
Hi,

R has the function edit() which allows the editing of the definition
of a function. Does python have something similar so that users can
edit python functions on the fly? Thanks.

https://www.rdocumentation.org/packages/utils/versions/3.4.3/topics/edit

-- 
Regards,
Peng
-- 
https://mail.python.org/mailman/listinfo/python-list


Where is ^ (symmetric_difference) documented in help()?

2017-12-22 Thread Peng Yu
Hi, I see the following two lines are the same. But I'd like to find
where ^ is documented via the help() function (I am not looking for
the document in html)? Does anybody know? Thanks.

s.symmetric_difference(t)
s ^ t

-- 
Regards,
Peng
-- 
https://mail.python.org/mailman/listinfo/python-list


Why are both locals() and globals() set?

2017-12-22 Thread Peng Yu
Hi, The following example shows that both locals() and globals() are
updated when x and f are defined. Shouldn't they be considered and
global variable and functions only? Why does it make sense to set
locals() as well? Thanks.

$ cat  ./main.py
#!/usr/bin/env python
# vim: set noexpandtab tabstop=2 shiftwidth=2 softtabstop=-1 fileencoding=utf-8:

d = dict()

d['l1'] = set(locals().keys())
d['g1'] = set(globals().keys())

x = 10
def f():
  pass

d['l2'] = set(locals().keys())
d['g2'] = set(globals().keys())

print d['l2'] - d['l1']
print d['g2'] - d['g1']

import os.path
d['l3'] = set(locals().keys())
d['g3'] = set(globals().keys())

print d['l3'] - d['l2']
print d['g3'] - d['g2']

from os import path
d['l4'] = set(locals().keys())
d['g4'] = set(globals().keys())

print d['l4'] - d['l3']
print d['g4'] - d['g3']

$  ./main.py
set(['x', 'f'])
set(['x', 'f'])
set(['os'])
set(['os'])
set(['path'])
set(['path'])


-- 
Regards,
Peng
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Where is ^ (symmetric_difference) documented in help()?

2017-12-22 Thread Peng Yu
Where is it documented that __xor__ and ^ is the same as
symmetric_difference? Thanks.

BTW, I am using to Python 2, your help message is different from mine.
Do you use Python 3?

On Fri, Dec 22, 2017 at 9:41 PM, Dan Sommers  wrote:
> On Fri, 22 Dec 2017 21:35:53 -0600, Peng Yu wrote:
>
>> Hi, I see the following two lines are the same. But I'd like to find
>> where ^ is documented via the help() function (I am not looking for
>> the document in html)? Does anybody know? Thanks.
>>
>> s.symmetric_difference(t)
>> s ^ t
>
> It's sort of documented in help(set):
>
>  |  __xor__(self, value, /)
>  |  Return self^value.
>
> HTH,
> Dan
>
> --
> https://mail.python.org/mailman/listinfo/python-list



-- 
Regards,
Peng
-- 
https://mail.python.org/mailman/listinfo/python-list


What is the meaning of @@?

2017-12-22 Thread Peng Yu
Hi, I only can find the doc for @. What does @@ mean in python?

-- 
Regards,
Peng
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: What is the meaning of @@?

2017-12-24 Thread Peng Yu
See for example this file.

https://github.com/tensorflow/tensorflow/blob/master/tensorflow/python/ops/rnn_cell.py

On Sat, Dec 23, 2017 at 12:03 AM, Steve D'Aprano
 wrote:
> On Sat, 23 Dec 2017 04:38 pm, Peng Yu wrote:
>
>> Hi, I only can find the doc for @. What does @@ mean in python?
>
> I don't think that @@ means anything yet.
>
> There was a proposal to use @@ for matrix exponentiation in Numpy, as @ is
> used for matrix multiplication, but that was left on hold to see whether it
> is really needed or not.
>
> Where did you find @@ in Python?
>
>
> (By the way, @ for matrix multiplication only works in Python 3.5 or better.)
>
> https://www.python.org/dev/peps/pep-0465/
>
>
>
> --
> Steve
> “Cheer up,” they said, “things could be worse.” So I cheered up, and sure
> enough, things got worse.
>
> --
> https://mail.python.org/mailman/listinfo/python-list



-- 
Regards,
Peng
-- 
https://mail.python.org/mailman/listinfo/python-list


Where is the usage of (list comprehension) documented?

2018-01-14 Thread Peng Yu
Hi,

I see the following usage of list comprehension can generate a
generator. Does anybody know where this is documented? Thanks.

$ cat main.py
#!/usr/bin/env python

import sys
lines = (line.rstrip('\n') for line in sys.stdin)
print lines

lines = [line.rstrip('\n') for line in sys.stdin]
print lines
$ seq 10 | ./main.py
 at 0x1101ecd70>
['1', '2', '3', '4', '5', '6', '7', '8', '9', '10']

-- 
Regards,
Peng
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Where is the usage of (list comprehension) documented?

2018-01-14 Thread Peng Yu
> When you use square brackets, you're creating a generator, as in your
> second example. Your first example is a slightly different beast
> called a "generator expression". If you search for that in the docs or
> on the web, you'll find what you want.

Thanks. Can the documentation be found by `help()` in python?

-- 
Regards,
Peng
-- 
https://mail.python.org/mailman/listinfo/python-list


Can utf-8 encoded character contain a byte of TAB?

2018-01-15 Thread Peng Yu
Hi,

I use the following code to process TSV input.

$ printf '%s\t%s\n' {1..10} | ./main.py
['1', '2']
['3', '4']
['5', '6']
['7', '8']
['9', '10']
$ cat main.py
#!/usr/bin/env python
# vim: set noexpandtab tabstop=2 shiftwidth=2 softtabstop=-1 fileencoding=utf-8:

import sys
for line in sys.stdin:
fields=line.rstrip('\n').split('\t')
print fields

But I am not sure it will process utf-8 input correctly. Thus, I come
up with this code. However, I am not sure if this is really necessary
as my impression is that utf-8 character should not contain the ascii
code for TAB. Is it so? Thanks.

$ cat main1.py
#!/usr/bin/env python
# vim: set noexpandtab tabstop=2 shiftwidth=2 softtabstop=-1 fileencoding=utf-8:

import sys
for line in sys.stdin:
#fields=line.rstrip('\n').split('\t')
fields=line.rstrip('\n').decode('utf-8').split('\t')
print [x.encode('utf-8') for x in fields]

$ printf '%s\t%s\n' {1..10} | ./main1.py
['1', '2']
['3', '4']
['5', '6']
['7', '8']
['9', '10']


-- 
Regards,
Peng
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Can utf-8 encoded character contain a byte of TAB?

2018-01-15 Thread Peng Yu
> Just to be clear, TAB *only* appears in utf-8 as the encoding for the actual 
> TAB character, not as a part of any other character's encoding. The only 
> bytes that can appear in the utf-8 encoding of non-ascii characters are 
> starting with 0xC2 through 0xF4, followed by one or more of 0x80 through 0xBF.

So for utf-8 encoded input, I only need to use this code to split each
line into fields?

import sys
for line in sys.stdin:
fields=line.rstrip('\n').split('\t')
print fields

Is there a need to use this code to split each line into fields?

import sys
for line in sys.stdin:
fields=line.rstrip('\n').decode('utf-8').split('\t')
print [x.encode('utf-8') for x in fields]

-- 
Regards,
Peng
-- 
https://mail.python.org/mailman/listinfo/python-list


Where is _sre.SRE_Match?

2018-02-07 Thread Peng Yu
Hi,

I see _sre.SRE_Match is returned by re.match. But I don't find where
it is defined. Does anybody know how to get its help page within
python command line? Thanks.

>>> import re
>>> m = re.match('a', 'abc')
>>> print type(m)

>>> _sre.SRE_Match
Traceback (most recent call last):
  File "", line 1, in 
NameError: name '_sre' is not defined

-- 
Regards,
Peng
-- 
https://mail.python.org/mailman/listinfo/python-list


read Unicode characters one by one in python2

2018-02-24 Thread Peng Yu
Here shows some code for reading Unicode characters one by one in
python2. Is it the best code for reading Unicode characters one by one
in python2?

https://rosettacode.org/wiki/Read_a_file_character_by_character/UTF8#Python

-- 
Regards,
Peng
-- 
https://mail.python.org/mailman/listinfo/python-list


How to extract the raw bytes of the decoded unicode?

2018-02-24 Thread Peng Yu
Hi,

I can extracted the encoded value as bytes. But is there a way to
extracted the decoded value (for á, it is C1)? Thanks.

$ cat ./dumpunicode.py
#!/usr/bin/env python3

while True:
c = sys.stdin.read(1)
if c:
print(c)
print('0x' + ''.join(['%x' % x for x in reversed(bytes(c,
encoding='utf-8'))]))
else:
break
$ ./dumpunicode.py <<< á
á
0xa1c3


0xa


-- 
Regards,
Peng
-- 
https://mail.python.org/mailman/listinfo/python-list


How to only get \n for newline without the single quotes?

2018-02-24 Thread Peng Yu
I would like to just get the escaped string without the single quotes.
Is there a way to do so? Thanks.

>>> x='\n'
>>> print repr(x)
'\n'

-- 
Regards,
Peng
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: How to only get \n for newline without the single quotes?

2018-02-24 Thread Peng Yu
On Sat, Feb 24, 2018 at 12:45 PM, Wildman via Python-list
 wrote:
> On Sat, 24 Feb 2018 11:41:32 -0600, Peng Yu wrote:
>
>> I would like to just get the escaped string without the single quotes.
>> Is there a way to do so? Thanks.
>>
>>>>> x='\n'
>>>>> print repr(x)
>> '\n'
>
> Python 3.5.3 (default, Jan 19 2017, 14:11:04)
> [GCC 6.3.0 20170118] on linux
> Type "help", "copyright", "credits" or "license" for more information.
>>>> x='/n'
>>>> print(repr(x))
> '/n'
>>>> print(repr(x).strip("'"))
> /n
>>>>
>
> Python 2.7.13 (default, Nov 24 2017, 17:33:09)
> [GCC 6.3.0 20170516] on linux2
> Type "help", "copyright", "credits" or "license" for more information.
>>>> x='/n'
>>>> print repr(x)
> '/n'
>>>> print repr(x).strip("'")
> /n
>>>>

I was looking for something builtin python. There is not such a builtin way?

-- 
Regards,
Peng
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: How to only get \n for newline without the single quotes?

2018-02-24 Thread Peng Yu
On Sat, Feb 24, 2018 at 1:08 PM, Peng Yu  wrote:
> On Sat, Feb 24, 2018 at 12:45 PM, Wildman via Python-list
>  wrote:
>> On Sat, 24 Feb 2018 11:41:32 -0600, Peng Yu wrote:
>>
>>> I would like to just get the escaped string without the single quotes.
>>> Is there a way to do so? Thanks.
>>>
>>>>>> x='\n'
>>>>>> print repr(x)
>>> '\n'
>>
>> Python 3.5.3 (default, Jan 19 2017, 14:11:04)
>> [GCC 6.3.0 20170118] on linux
>> Type "help", "copyright", "credits" or "license" for more information.
>>>>> x='/n'
>>>>> print(repr(x))
>> '/n'
>>>>> print(repr(x).strip("'"))
>> /n
>>>>>
>>
>> Python 2.7.13 (default, Nov 24 2017, 17:33:09)
>> [GCC 6.3.0 20170516] on linux2
>> Type "help", "copyright", "credits" or "license" for more information.
>>>>> x='/n'
>>>>> print repr(x)
>> '/n'
>>>>> print repr(x).strip("'")
>> /n
>>>>>
>
> I was looking for something builtin python. There is not such a builtin way?

Also, this is not printed as \f or \e. I'd like things like \a, \b,
... \v be printed as such. Is there a way to do so in python?

>>> print repr('\f')
'\x0c'
>>> print repr('\e')
'\\e'

-- 
Regards,
Peng
-- 
https://mail.python.org/mailman/listinfo/python-list


How to access the help page of SRE_Pattern?

2018-03-05 Thread Peng Yu
Hi,

>>> import re
>>> prog=re.compile('[a-f]+')
>>> help(prog)

I can use the above command to access SRE_Pattern. But this involves
the creation of an object of the class.

I tried to directly access the class. But it does not work. Does
anybody know if there is a way to directly access the class help page?
Thanks.

>>> help(_sre.SRE_Pattern)
Traceback (most recent call last):
  File "", line 1, in 
AttributeError: 'module' object has no attribute 'SRE_Pattern'
>>> help(SRE_Pattern)
Traceback (most recent call last):
  File "", line 1, in 
NameError: name 'SRE_Pattern' is not defined


-- 
Regards,
Peng
-- 
https://mail.python.org/mailman/listinfo/python-list


How to get get_body() to work? (about email)

2023-03-19 Thread Peng Yu
Hi,

https://docs.python.org/3/library/email.parser.html

It says "For MIME messages, the root object will return True from its
is_multipart() method, and the subparts can be accessed via the
payload manipulation methods, such as get_body(), iter_parts(), and
walk()."

But when I try the following code, get_body() is not found. How to get
get_body() to work?

$ python3 -c 'import email, sys; msg =
email.message_from_string(sys.stdin.read()); print(msg.get_body())'
<<< some_text
Traceback (most recent call last):
  File "", line 1, in 
AttributeError: 'Message' object has no attribute 'get_body'

-- 
Regards,
Peng
-- 
https://mail.python.org/mailman/listinfo/python-list


ipython display figure

2021-04-13 Thread Peng Yu
Hi,

https://www.fuzzingbook.org/html/Grammars.html

I am trying to follow an example on the above page. But it does not
show a figure. Could anybody let me know how to display the figure?

$  ipython3
Python 3.8.0 (v3.8.0:fa919fdf25, Oct 14 2019, 10:23:27)
Type 'copyright', 'credits' or 'license' for more information
IPython 7.22.0 -- An enhanced Interactive Python. Type '?' for help.

[ins] In [1]: from fuzzingbook.Grammars import *

[ins] In [2]: from IPython.display import SVG, display

[ins] In [3]: SVG(show_diagram(syntax_diagram_expr(EXPR_GRAMMAR[''][0])))
Out[3]: 

-- 
Regards,
Peng
-- 
https://mail.python.org/mailman/listinfo/python-list


How to get the source code of python function being decorated?

2016-09-16 Thread Peng Yu
Hi, See the following example, I am not able to get the source code of
the actual function that does the calculation of partial_ratio. Does
anybody know what is the correct way of getting the source?

/tmp$ ./main.py
@functools.wraps(func)
def decorator(*args, **kwargs):
if args[0] is None or args[1] is None:
return 0
return func(*args, **kwargs)

/tmp$ cat ./main.py
#!/usr/bin/env python
# vim: set noexpandtab tabstop=2 shiftwidth=2 softtabstop=-1 fileencoding=utf-8:

import fuzzywuzzy.fuzz
import inspect
print inspect.getsource(fuzzywuzzy.fuzz.partial_ratio)

-- 
Regards,
Peng
-- 
https://mail.python.org/mailman/listinfo/python-list


Where is the documentation for ','?

2016-09-16 Thread Peng Yu
Hi,

I'm wondering where is the documentation for ',' as in the following usage.

x = 1
y = 2
x, y = y, x

I tried help(','). But there are too many ',' in it and I don't see in
which section ',' is documented. Could anybody let me know? Thanks.

-- 
Regards,
Peng
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Where is the documentation for ','?

2016-09-16 Thread Peng Yu
help(tuple) gives me this, which does not mention ',' either.

Help on class tuple in module __builtin__:

class tuple(object)
 |  tuple() -> empty tuple
 |  tuple(iterable) -> tuple initialized from iterable's items
 |
 |  If the argument is a tuple, the return value is the same object.
 |
 |  Methods defined here:
 |
 |  __add__(...)
 |  x.__add__(y) <==> x+y
 |
 |  __contains__(...)
 |  x.__contains__(y) <==> y in x
 |
 |  __eq__(...)
 |  x.__eq__(y) <==> x==y
 |
 |  __ge__(...)
 |  x.__ge__(y) <==> x>=y
 |
 |  __getattribute__(...)
 |  x.__getattribute__('name') <==> x.name
 |
 |  __getitem__(...)
 |  x.__getitem__(y) <==> x[y]
 |
 |  __getnewargs__(...)
 |
 |  __getslice__(...)
 |  x.__getslice__(i, j) <==> x[i:j]
 |
 |  Use of negative indices is not supported.
 |
 |  __gt__(...)
 |  x.__gt__(y) <==> x>y
 |
 |  __hash__(...)
 |  x.__hash__() <==> hash(x)
 |
 |  __iter__(...)
 |  x.__iter__() <==> iter(x)
 |
 |  __le__(...)
 |  x.__le__(y) <==> x<=y
 |
 |  __len__(...)
 |  x.__len__() <==> len(x)
 |
 |  __lt__(...)
 |  x.__lt__(y) <==> x x*n
 |
 |  __ne__(...)
 |  x.__ne__(y) <==> x!=y
 |
 |  __repr__(...)
 |  x.__repr__() <==> repr(x)
 |
 |  __rmul__(...)
 |  x.__rmul__(n) <==> n*x
 |
 |  count(...)
 |  T.count(value) -> integer -- return number of occurrences of value
 |
 |  index(...)
 |  T.index(value, [start, [stop]]) -> integer -- return first
index of value.
 |  Raises ValueError if the value is not present.
 |
 |  --
 |  Data and other attributes defined here:
 |
 |  __new__ = 
 |      T.__new__(S, ...) -> a new object with type S, a subtype of T



On Fri, Sep 16, 2016 at 9:13 PM, MRAB  wrote:
> On 2016-09-17 03:05, Peng Yu wrote:
>>
>> Hi,
>>
>> I'm wondering where is the documentation for ',' as in the following
>> usage.
>>
>> x = 1
>> y = 2
>> x, y = y, x
>>
>> I tried help(','). But there are too many ',' in it and I don't see in
>> which section ',' is documented. Could anybody let me know? Thanks.
>>
> Search for 'tuple' instead.
> --
> https://mail.python.org/mailman/listinfo/python-list



-- 
Regards,
Peng
-- 
https://mail.python.org/mailman/listinfo/python-list


Is there something similar to `set -v` of bash in python

2016-09-16 Thread Peng Yu
Hi, `set -v` in bash allows the print of the command before print the
output of the command.

I want to do the similar thing --- print a python command and then
print the output of the command. Is it possible with python?

-- 
Regards,
Peng
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Where is the documentation for ','?

2016-09-16 Thread Peng Yu
OK. But it is documented somewhere in python doc?

On Fri, Sep 16, 2016 at 9:48 PM, Lawrence D’Oliveiro
 wrote:
> On Saturday, September 17, 2016 at 2:05:49 PM UTC+12, Peng Yu wrote:
>> x, y = y, x
>
> It’s just syntactic sugar for
>
> (x, y) = (y, x)
> --
> https://mail.python.org/mailman/listinfo/python-list



-- 
Regards,
Peng
-- 
https://mail.python.org/mailman/listinfo/python-list


How to convert 'ö' to 'oe' or 'o' (or other similar things) in a string?

2016-09-17 Thread Peng Yu
Hi, I want to convert strings in which the characters with accents
should be converted to the ones without accents. Here is my current
code.



$ cat main.sh
#!/usr/bin/env bash
# vim: set noexpandtab tabstop=2:

set -v
./main.py Förstemann
./main.py Frédér8ic@

$ cat main.py
#!/usr/bin/env python
# vim: set noexpandtab tabstop=2 shiftwidth=2 softtabstop=-1 fileencoding=utf-8:

import sys
import unicodedata
print unicodedata.normalize('NFKD',
sys.argv[1].decode('utf-8')).encode('ascii', 'ignore')



The complication is that some characters have more than one way of
conversions. E.g., 'ö' can be converted to either 'oe' or 'o'. I want
to get all the possible conversions of a string. Does anybody know a
good way to do so? Thanks.

-- 
Regards,
Peng
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Is there something similar to `set -v` of bash in python

2016-09-17 Thread Peng Yu
> python -m trace -t yourprogram.py

If I want to add some command in yourprogram.py to show the commands
used it instead of calling trace from the command line, can it be
done?

-- 
Regards,
Peng
-- 
https://mail.python.org/mailman/listinfo/python-list


Is the content available in the html doc available in help()?

2016-09-17 Thread Peng Yu
Hi, I want to get the same content as the html doc from help(). I am
not sure if this is possible (as I tried help(inspect) which does not
give the same content). Could anybody confirm if there is a way to get
the same content from help()? Thanks.

https://docs.python.org/2/library/inspect.html

-- 
Regards,
Peng
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Is the content available in the html doc available in help()?

2016-09-17 Thread Peng Yu
Sorry. I am still referring to python2.

On Sat, Sep 17, 2016 at 8:45 PM, Lawrence D’Oliveiro
 wrote:
> On Sunday, September 18, 2016 at 12:51:11 PM UTC+12, Peng Yu wrote:
>> I want to get the same content as the html doc from help().
>
> ldo@theon:~> pydoc3 inspect
> Help on module inspect:
>
> NAME
> inspect - Get useful information from live Python objects.
>
> MODULE REFERENCE
> https://docs.python.org/3.5/library/inspect
>
> (etc etc etc)
>
> worked for me, and it’s for a more up-to-date version of Python as well...
> --
> https://mail.python.org/mailman/listinfo/python-list



-- 
Regards,
Peng
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Is there something similar to `set -v` of bash in python

2016-09-17 Thread Peng Yu
The manual says the following.

"The trace function is invoked (with event set to 'call') whenever a
new local scope is entered; it should return a reference to a local
trace function to be used that scope, or None if the scope shouldn’t
be traced."

It means that one can not somehow settrace in one line and expect to
get the trace function being called in the next line.

So something like `set -v` in bash sounds not possible. Is it so?

On Sat, Sep 17, 2016 at 5:28 PM, Ned Batchelder  wrote:
> On Saturday, September 17, 2016 at 4:41:32 PM UTC-4, Peng Yu wrote:
>> > python -m trace -t yourprogram.py
>>
>> If I want to add some command in yourprogram.py to show the commands
>> used it instead of calling trace from the command line, can it be
>> done?
>
> I don't know of a way to do that, but it might be possible.
>
> --Ned.
> --
> https://mail.python.org/mailman/listinfo/python-list



-- 
Regards,
Peng
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: How to convert 'ö' to 'oe' or 'o' (or other similar things) in a string?

2016-09-17 Thread Peng Yu
On Sat, Sep 17, 2016 at 3:20 PM, Martin Schöön  wrote:
> Den 2016-09-17 skrev Kouli :
>> Hello, try the Unidecode module - https://pypi.python.org/pypi/Unidecode.

I don't find a way to make it print oe for ö. Could anybody please
advise what is the correct way to do it?

==> main.py <==
#!/usr/bin/env python
# vim: set noexpandtab tabstop=2 shiftwidth=2 softtabstop=-1 fileencoding=utf-8:

import sys
from unidecode import unidecode
print unidecode(sys.argv[1].decode('utf-8'))


==> main.sh <==
#!/usr/bin/env bash
# vim: set noexpandtab tabstop=2:

./main.py Schöön

$ ./main.sh
Schoon

>> Kouli
>>
>> On Sat, Sep 17, 2016 at 6:12 PM, Peng Yu  wrote:
>>> Hi, I want to convert strings in which the characters with accents
>>> should be converted to the ones without accents. Here is my current
>>> code.
>
> Side note from Sweden. Å, ä and ö are not accented characters in our
> language. They are characters of their own. If you want to look up
> someone called Öhman in the phone directory you go to the Ö section
> not the O section.
>
> Related anecdote from Phoenix AZ. By now you have noticed my family
> name: Schöön. On airline tickets and boarding passes in the U.S. it
> gets spelled Schoeoen. This landed me in a special security check
> once. After a while the youngish lady performing the search started
> to look like 1+1 did not sum up to 2. She looked at my passport
> and boarding pass again and asked why I was there with her. I pointed
> at another young lady, the one that 'scrutinized' passports and
> boarding passes while chatting with a friend and said "She told me
> to go over here." "Wait here, I have to talk to my supervisor."
>
> A few minutes passed (I was alone in the enhances security check
> throughout...) and then "I am sorry but you should not have had
> to go through here. There was a mistake."
>
> So there you are: If you are a middle aged, caucasian guy with
> a passport from northern Europe and no 'funny' letters in your name
> your are not a threat in the eyes of TSA in Arizona.
>
> Sorry for wasting the bandwidth.
>
> /Martin
> --
> https://mail.python.org/mailman/listinfo/python-list



-- 
Regards,
Peng
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Is there something similar to `set -v` of bash in python

2016-09-18 Thread Peng Yu
On Sunday, September 18, 2016, Ned Batchelder  wrote:

> On Saturday, September 17, 2016 at 11:09:04 PM UTC-4, Peng Yu wrote:
> > The manual says the following.
> >
> > "The trace function is invoked (with event set to 'call') whenever a
> > new local scope is entered; it should return a reference to a local
> > trace function to be used that scope, or None if the scope shouldn’t
> > be traced."
> >
> > It means that one can not somehow settrace in one line and expect to
> > get the trace function being called in the next line.
> >
> > So something like `set -v` in bash sounds not possible. Is it so?
>
> You've found a good reason why "set -v" would be very difficult if
> not impossible in Python.
>
> I'm curious though, why you would want to trace every line in a
> program every time you (or anyone else) ran it?


This is for debugging not for normal run. But I need the ability to control
the range of the code in which the debug message is printed.


> I view tracing lines
> as a debugging technique. Once the program is behaving properly, why
> do you want all the noise of the traced lines?
>
> In Bash scripts we do it because some scripts are light automation
> where the person running the script should have a clear idea of what
> actions it is taking.  The actions in the script are commands that the
> person might run themselves at other times, and so the difference
> between running the script and running the commands is not great.
>
> But in a Python program, presumably the difference is greater. I cannot
> type out single Python lines at my shell prompt to perform the actions
> the Python program does.
>
> Perhaps you want to define an alias for "python -m trace -t $*" ?
>
> --Ned.
> --
> https://mail.python.org/mailman/listinfo/python-list
>


-- 
Regards,
Peng
-- 
https://mail.python.org/mailman/listinfo/python-list


How to import all things defined the files in a module directory in __init__.py?

2016-09-21 Thread Peng Yu
Hi,

Suppose that I have file1.py, ..., filen.py in a module directory.

I want to import all the thing (or the ones available in the
respective __all__) defined in each of the file by putting the
following lines in __init__.py

from file1 import *

from filen import *

However, I don't want to hardcode the file names in __init__.py. Is
there an automatic way of doing it?

-- 
Regards,
Peng
-- 
https://mail.python.org/mailman/listinfo/python-list


Where is import defined in the source code? (python 2)

2016-09-21 Thread Peng Yu
Hi, I want know where import is defined in the source code. Is it
implemented using __import__?

-- 
Regards,
Peng
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: How to import all things defined the files in a module directory in __init__.py?

2016-09-22 Thread Peng Yu
On Wed, Sep 21, 2016 at 11:14 PM, Ben Finney  wrote:
> Peng Yu  writes:
>
>> I want to import all the thing (or the ones available in the
>> respective __all__) defined in each of the file by putting the
>> following lines in __init__.py
>>
>> from file1 import *
>> 
>> from filen import *
>>
>> However, I don't want to hardcode the file names in __init__.py. Is
>> there an automatic way of doing it?
>
> Why do you want to do that? It will defeat static analysis of the code,
> which is one of the more important tools to make your code reliable.
>
> It will also make the names in the code impossible to automatically
> match against where they came from. Explicit is better than implicit;
> you are proposing to make an unknown horde of names in the code implicit
> and untraceable.
>
> Why? What problem are you trying to solve that you believe needs this
> approach?

This will make refactoring easy. If everything is explicit, when one
do refactoring, at two places need to be changed which can be a
burden.

-- 
Regards,
Peng
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: How to import all things defined the files in a module directory in __init__.py?

2016-09-22 Thread Peng Yu
On Thu, Sep 22, 2016 at 8:35 PM, Ben Finney  wrote:
> Peng Yu  writes:
>
>> On Wed, Sep 21, 2016 at 11:14 PM, Ben Finney  
>> wrote:
>> > [Importing ‘*’ from a module] will also make the names in the code
>> > impossible to automatically match against where they came from.
>> > Explicit is better than implicit; you are proposing to make an
>> > unknown horde of names in the code implicit and untraceable.
>>
>> This will make refactoring easy. If everything is explicit, when one
>> do refactoring, at two places need to be changed which can be a
>> burden.
>
> That's completely backward: Importing ‘*’ from the module makes
> refactoring significantly *more* difficult.
>
> With explicit ‘import foo; foo.lorem()’, an automated tool can know that
> when ‘lorem’ changes to a different name, this module's use of
> ‘foo.lorem’ should also change.

Is there such a good automated tool for python refactoring?

> With non-explicit ‘from foo import *; lorem()’, then an automated too
> has *no way* of knowing that ‘lorem’ should change when you alter that
> name in the ‘foo’ module.
>
> So no, what you say above is the opposite of correct. Instead, using
> star import makes a rename *more* difficult to do correctly.
>
> --
>  \  “Faith is generally nothing more than the permission religious |
>   `\ people give to one another to believe things strongly without |
> _o__)  evidence.” —Sam Harris, _Letter to a Christian Nation_ 2006 |
> Ben Finney
>
> --
> https://mail.python.org/mailman/listinfo/python-list



-- 
Regards,
Peng
-- 
https://mail.python.org/mailman/listinfo/python-list


Is there a way to change the closure of a python function?

2016-09-27 Thread Peng Yu
Hi, In many other functional language, one can change the closure of a
function. Is it possible in python?

http://ynniv.com/blog/2007/08/closures-in-python.html

-- 
Regards,
Peng
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Is there a way to change the closure of a python function?

2016-09-27 Thread Peng Yu
On Tue, Sep 27, 2016 at 10:01 AM, Chris Angelico  wrote:
> On Wed, Sep 28, 2016 at 12:01 AM, Peng Yu  wrote:
>> Hi, In many other functional language, one can change the closure of a
>> function. Is it possible in python?
>>
>> http://ynniv.com/blog/2007/08/closures-in-python.html
>>
>
> From the blog post:
>
> """In some languages, the variable bindings contained in a closure
> behave just like any other variables. Alas, in python they are
> read-only."""
>
> This is not true, at least as of Python 3.

So in Python 2, this is true?

> def makeInc(x):
>   def inc(y, moreinc=0):
>  # x is "closed" in the definition of inc
>  nonlocal x
>  x += moreinc
>  return y + x
>   return inc
>
> The 'nonlocal' keyword is like 'global', applying only to assignments
> (the blog post already mentions the possibility of mutating an object
> rather than reassigning it), and permitting assignment into a broader
> scope than the function's locals. You can also have multiple closures
> in the same context, and changes made by one of them will affect the
> others.
>
> ChrisA
> --
> https://mail.python.org/mailman/listinfo/python-list



-- 
Regards,
Peng
-- 
https://mail.python.org/mailman/listinfo/python-list


Is there an peekable similar to peekable but in additional allowing one to put some data to it?

2017-01-11 Thread Peng Yu
Hi, peekable from more-itertools only allow peeking an iterator. But
sometimes, one may want to take a look at an element, manipulate it,
then put it back to the iterator. Is there a class in python that can
help do this?

https://pypi.python.org/pypi/more-itertools/

-- 
Regards,
Peng
-- 
https://mail.python.org/mailman/listinfo/python-list


OpenSSL error

2018-09-06 Thread Peng Yu
Hi,

I got the following error. Does anybody know how to fix it? Thanks.

$ pip
Traceback (most recent call last):
  File "/Library/Frameworks/Python.framework/Versions/2.7/bin/pip",
line 7, in 
from pip._internal import main
  File 
"/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/pip/_internal/__init__.py",
line 42, in 
from pip._internal import cmdoptions
  File 
"/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/pip/_internal/cmdoptions.py",
line 16, in 
from pip._internal.index import (
  File 
"/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/pip/_internal/index.py",
line 14, in 
from pip._vendor import html5lib, requests, six
  File 
"/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/pip/_vendor/requests/__init__.py",
line 97, in 
from pip._vendor.urllib3.contrib import pyopenssl
  File 
"/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/pip/_vendor/urllib3/contrib/pyopenssl.py",
line 46, in 
import OpenSSL.SSL
  File 
"/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/OpenSSL/__init__.py",
line 8, in 
from OpenSSL import rand, crypto, SSL
  File 
"/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/OpenSSL/SSL.py",
line 118, in 
SSL_ST_INIT = _lib.SSL_ST_INIT
AttributeError: 'module' object has no attribute 'SSL_ST_INIT'


-- 
Regards,
Peng
-- 
https://mail.python.org/mailman/listinfo/python-list


How to print unicode characters with yaml.safe_dump()?

2016-05-31 Thread Peng Yu
Hi, The following code shows that "Michał" is printed differently for
print(yaml.safe_dump(...)) and the direct print. Does anybody know how
to use yaml.safe_dump() so that "Michał" will be printed as is.

~$ cat main.py
#!/usr/bin/env python
# vim: set noexpandtab tabstop=2 shiftwidth=2 softtabstop=-1 fileencoding=utf-8:

import yaml

foo = {
u'first': u"Michał",
u'last': u"Seweryn",
}

print foo['first']

print(yaml.safe_dump(foo, default_flow_style=True).encode('utf-8'))
print(yaml.safe_dump(foo, default_flow_style=False).encode('utf-8'))
~$ ./main.py
Michał
{first: "Micha\u0142", last: Seweryn}

first: "Micha\u0142"
last: Seweryn

-- 
Regards,
Peng
-- 
https://mail.python.org/mailman/listinfo/python-list


Conversion between basic regular expression and extended regular expression

2018-11-17 Thread Peng Yu
Hi,

I'd like to use a program to convert between basic regular expression
(BRE) and extended regular expression (ERE). (see man grep for the
definition of BRE and ERE). Does python has a module for this purpose?
Thanks.

-- 
Regards,
Peng
-- 
https://mail.python.org/mailman/listinfo/python-list


How to load cookies from a json input in python-requests?

2019-08-12 Thread Peng Yu
```
import requests
s = requests.Session()
import json
s.cookies.set_cookie(requests.utils.cookiejar_from_dict(json.load(sys.stdin)))
```

I used the above command to load cookies from a json file. But I got
the following error. Does anybody know how to fix the error? Thanks.

```
Traceback (most recent call last):
  File "/xxx/xxx.py", line 15, in 

s.cookies.set_cookie(requests.utils.cookiejar_from_dict(json.load(sys.stdin)))
  File 
"/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/requests/cookies.py",
line 345, in set_cookie
if hasattr(cookie.value, 'startswith') and
cookie.value.startswith('"') and cookie.value.endswith('"'):
AttributeError: 'RequestsCookieJar' object has no attribute 'value'
```

-- 
Regards,
Peng
-- 
https://mail.python.org/mailman/listinfo/python-list


Is there a character that never appears in the output of zlib.compress?

2020-01-28 Thread Peng Yu
Hi,

I'd like to tell what part is zlib.compress data in an input stream.
One way is to use some characters that never appear in zlib.compress
output to denote the boundary. Are there such characters? Thanks.

-- 
Regards,
Peng
-- 
https://mail.python.org/mailman/listinfo/python-list


How to read the original data line by line from stdin in python 3 just like python 2?

2020-01-28 Thread Peng Yu
Suppose that I use this to read from stdin. But `line` contains
decoded data in python 3. In python 2, it contains the original data.
What is the best way to get the original data in python 3? Thanks.

```
for line in sys.stdin:
   ...
```

-- 
Regards,
Peng
-- 
https://mail.python.org/mailman/listinfo/python-list


UnicodeEncodeError: 'ascii' codec can't encode character u'\ua000' in position 0: ordinal not in range(128)

2015-01-13 Thread Peng Yu
Hi,

I am trying to understand what does encode() do. What are the hex
representations of "u" in main.py? Why there is UnicodeEncodeError
when main.py is piped to xxd? Why there is no such error when it is
not piped? Thanks.

~$ cat main.py
#!/usr/bin/env python

u = unichr(40960) + u'abcd' + unichr(1972)
print u
~$ cat main_encode.py
#!/usr/bin/env python

u = unichr(40960) + u'abcd' + unichr(1972)
print u.encode('utf-8')
$ ./main.py
ꀀabcd޴
~$ cat main.sh
#!/usr/bin/env bash

set -v
./main.py | xxd
./main_encode.py | xxd

~$ ./main.sh
./main.py | xxd
Traceback (most recent call last):
  File "./main.py", line 4, in 
print u
UnicodeEncodeError: 'ascii' codec can't encode character u'\ua000' in
position 0: ordinal not in range(128)
./main_encode.py | xxd
000: ea80 8061 6263 64de b40a ...abcd...

-- 
Regards,
Peng
-- 
https://mail.python.org/mailman/listinfo/python-list


How to install dtrx with pip?

2015-04-25 Thread Peng Yu
Hi,

I get the following error when I try to install pip. Does anybody know
what it is wrong and how to fix it? Thanks.

~/Downloads$ pip install dtrx
Downloading/unpacking dtrx
  Could not find any downloads that satisfy the requirement dtrx
  Some externally hosted files were ignored (use --allow-external dtrx
to allow).
Cleaning up...
No distributions at all found for dtrx
Storing debug log for failure in /Users/py/.pip/pip.log
~/Downloads$ pip install --allow-external dtrx
You must give at least one requirement to install (see "pip help install")

-- 
Regards,
Peng
-- 
https://mail.python.org/mailman/listinfo/python-list


How add class name to format of the logging module?

2011-10-12 Thread Peng Yu
Hi,

The following attributes does not include the class name. Is there a
way to add class name to the format string? Thanks!

http://docs.python.org/library/logging.html#logrecord-attributes

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


How to generate error when argument are not supplied and there is no explicit defults (in optparse)?

2011-10-14 Thread Peng Yu
Hi,

The following code doesn't give me error, even I don't specify the
value of filename from the command line arguments. filename gets
'None'. I checked the manual, but I don't see a way to let
OptionParser fail if an argument's value (which has no default
explicitly specified) is not specified. I may miss some thing in the
manual. Could any expert let me know if there is a way to do so?
Thanks!

#!/usr/bin/env python

from optparse import OptionParser

usage = 'usage: %prog [options] arg1 arg2'
parser = OptionParser(usage=usage)
parser.set_defaults(verbose=True)
parser.add_option('-f', '--filename')

#(options, args) = parser.parse_args(['-f', 'file.txt'])
(options, args) = parser.parse_args()

print options.filename



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


How to automatically get the indent level from code?

2013-03-16 Thread Peng Yu
Hi,

I want to get the indent level within the code. For example, I want to
print 1 within the while loop as the line is indented 1 level. Is it
possible to get it within python?

while 1:
   #print the level of indent, which is 1 here.

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


pprint defaultdict one record per line

2013-03-17 Thread Peng Yu
Hi,

pprint can not print defaultdict one record per line. Is there some
other convenient way in python to print one record per line?

~/linux/test/python/man/library/pprint/function/pprint$ ./main.py
{'two': [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14], 'one': [0,
1, 2, 3, 4, 5, 6, 7, 8, 9]}
{'one': [0, 1, 2, 3, 4, 5, 6, 7, 8, 9],
 'two': [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14]}
defaultdict(, {'two': [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10,
11, 12, 13, 14], 'one': [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]})
~/linux/test/python/man/library/pprint/function/pprint$ cat main.py
#!/usr/bin/env python

import pprint

d=dict(one=range(10), two=range(15))
print d
pprint.pprint(d)

from collections import defaultdict
d=defaultdict(list)
d['one']=range(10)
d['two']=range(15)
pprint.pprint(d)

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


How to add the current dir to sys.path when calling a python file?

2013-03-17 Thread Peng Yu
Hi,

man python says "If a script  argument  is  given,  the directory
containing the script is inserted in the path in front of $PYTHONPATH.
The search path can be manipulated from  within a Python program as
the variable sys.path." Instead I want to have the current directory
inserted to the front of $PYTHONPATH without changing anything the
script. Is there a way to do so?

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


Re: How to add the current dir to sys.path when calling a python file?

2013-03-18 Thread Peng Yu
On Mon, Mar 18, 2013 at 1:54 AM, Steven D'Aprano
 wrote:
> On Sun, 17 Mar 2013 22:56:07 -0500, Peng Yu wrote:
>
>> Hi,
>>
>> man python says "If a script  argument  is  given,  the directory
>> containing the script is inserted in the path in front of $PYTHONPATH.
>> The search path can be manipulated from  within a Python program as the
>> variable sys.path." Instead I want to have the current directory
>> inserted to the front of $PYTHONPATH without changing anything the
>> script. Is there a way to do so?
>
> No. If you want to manipulate the path, you have to write code to do so,
> and put it in your script. That's very simple:
>
> import os, sys
> sys.path.insert(0, os.getcwd())

Actually, it is quite simple. Just use stdin to take the python file.

~/linux/test/python/man/library/sys/path$ cat.sh main.py subdir/main.py
==> main.py <==
#!/usr/bin/env python

import sys

print sys.path

==> subdir/main.py <==
#!/usr/bin/env python

import sys

print sys.path
~/linux/test/python/man/library/sys/path$ diff <(python - < main.py)
<(python - < subdir/main.py)



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


Re: How to automatically get the indent level from code?

2013-03-18 Thread Peng Yu
On Sun, Mar 17, 2013 at 1:23 AM, Mark Shroyer  wrote:
> I realize this isn't yet precisely what you're asking for, but look at the 
> inspect and ast modules:
>
> import ast, inspect
>
> def indent_level():
> lineno = inspect.currentframe().f_back.f_lineno
>
> with open(__file__) as source_file:
> tree = ast.parse(source_file.read(), filename=__file__)
>
> for node in ast.walk(tree):
> if hasattr(node, 'lineno') and node.lineno == lineno:
> return node.col_offset
>
> def example_usage():
> print("first indent_level() = {0}".format(indent_level()))
> if True:
> print("second indent_level() = {0}".format(indent_level()))
>
> if __name__ == '__main__':
> example_usage()
>
> The indent_level function above returns the textual column offset rather than 
> the logical block level you're asking for, e.g.:
>
> first indent_level() = 4
> second indent_level() = 8
>
> But hopefully it's a start.

Thanks. I try to run it from stdin. Obviously, it does not work. The
problem is the stdin can not be read again. Is there a way to extend
the code that indentation can be computed even the code is from stdin?

~/linux/test/python/tricks/indent_level$ python - < main.py
Traceback (most recent call last):
  File "", line 23, in 
  File "", line 16, in example_usage
  File "", line 8, in indent_level
IOError: [Errno 2] No such file or directory: ''
~/linux/test/python/tricks/indent_level$ cat main.py
#!/usr/bin/env python

import ast, inspect

def indent_level():
  lineno = inspect.currentframe().f_back.f_lineno

  with open(__file__) as source_file:
tree = ast.parse(source_file.read(), filename=__file__)

  for node in ast.walk(tree):
if hasattr(node, 'lineno') and node.lineno == lineno:
  return node.col_offset

def example_usage():
  print indent_level()
  #print("first indent_level() = {0}".format(indent_level()))
  if True:
print indent_level()
#print("second indent_level() = {0}".format(indent_level()))

if __name__ == '__main__':
  example_usage()

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


The usage of -m option of python

2013-03-18 Thread Peng Yu
Hi,

I don't quite understand how -m option is used. And it is difficult to
search for -m in google. Could anybody provide me with an example on
how to use this option? Thanks!

   -m module-name
  Searches sys.path for the named module and runs the
corresponding .py file as a script.

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


How avoid installing directories match some pattern when using distutils.core setup?

2013-03-18 Thread Peng Yu
Hi,

By default, setup.py will install everything in the source directory.
I want mask some directories so that they will not be installed. Is
there a way to do so in setup.py?

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


using pandoc instead of rst to document python

2013-04-23 Thread Peng Yu
Hi,

I'm wondering if it possible to use pandoc instead of rst to document
python. Is there a documentation system support this format of python
document?

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


Re: using pandoc instead of rst to document python

2013-04-23 Thread Peng Yu
On Tue, Apr 23, 2013 at 5:40 PM, R. Michael Weylandt
 wrote:
> On Tue, Apr 23, 2013 at 6:36 PM, Peng Yu  wrote:
>> Hi,
>>
>> I'm wondering if it possible to use pandoc instead of rst to document
>> python. Is there a documentation system support this format of python
>> document?

Sorry for the confusion. When I said pandoc, I meant pandoc's markdown.

http://johnmacfarlane.net/pandoc/README.html#pandocs-markdown

> Pandoc is a converter while rst is a format so they're not directly
> comparable; pandoc can convert _to_ and _from_ rst to a wide variety
> of other formats, but you still have to write documentation in one
> format or another. If you want to use an rst-centric documentation
> tool, you can write in, e.g., Markdown, convert to rst and then run
> your other tool on it.

I currently use sphinx to generate the doc (in rst). How to figure it
to support pandoc's markdown?

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


look-behind fixed width issue (package re)

2008-10-23 Thread Peng Yu
Hi,

It seem that the current python requires fixed-width pattern for look-
behind. I'm wondering if there is any newly development which make
variable-width pattern available for look-behind.

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


Re: look-behind fixed width issue (package re)

2008-10-24 Thread Peng Yu
> Most probably a backport to Python 2.6 or even 2.5 under a different
> module name like re_ng wouldn't be too difficult to do for anybody that
> needs the new functionality and knows a bit about building extension
> modules.

I did a google a search. But I don't find any document that describe
it. Does it have almost the same functionality as the re package that
will be in Python 2.7? Where is the decumentation of it?

If I just need variable width look-behind, I just replace re with
re_ng on python 2.5 or 2.6, right? Is re_ng available on python 2.4?

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


how to match whole word

2008-07-15 Thread Peng Yu
Hi,

The following code snippet is from /usr/bin/rpl. I would like the it
to match a word, for example, "abc" in ":abc:". But the current one
would not match "abc" in ":abc:". I tried to modify it myself. Would
you please let me know what is the corrected way to do it?

Thanks,
Peng

if opts.whole_words:
regex = re.compile(r"(?:(?<=\s)|^)" + re.escape(old_str) + 
r"(?=\s|
$)",
   opts.ignore_case and re.I or 
0)
--
http://mail.python.org/mailman/listinfo/python-list


Re: how to match whole word

2008-07-16 Thread Peng Yu
On Jul 15, 10:29 pm, Gary Herron <[EMAIL PROTECTED]> wrote:
> Peng Yu wrote:
> > Hi,
>
> > The following code snippet is from /usr/bin/rpl. I would like the it
> > to match a word, for example, "abc" in ":abc:". But the current one
> > would not match "abc" in ":abc:". I tried to modify it myself. Would
> > you please let me know what is the corrected way to do it?
>
> > Thanks,
> > Peng
>
> >if opts.whole_words:
> >regex = re.compile(r"(?:(?<=\s)|^)" + re.escape(old_str) + 
> > r"(?=\s|
> > $)",
> >   opts.ignore_case and re.I or 
> > 0)
> > --
> >http://mail.python.org/mailman/listinfo/python-list
>
> The regular expression "\w+" will match (what might be your definition
> of) a word, and in particular will match abc in :abc:.   Regular
> expressions have lots of other special \-sequences that might be worth
> your while to read about:  http://docs.python.org/lib/re-syntax.html
>
> Gary Herron

I didn't read the docs and tried the following code.

regex = re.compile(r"\A" + re.escape(old_str) + r"\Z",
opts.ignore_case and re.I or 0)

But I'm not sure why it is not working.

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


python doc in command line

2008-07-16 Thread Peng Yu
Hi,

Perl has a command line help perldoc. I'm wondering if python has a
similar help command.

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


How to match word boundary?

2008-07-22 Thread Peng Yu
Hi,

I use r"\ba\b" to match "a". However, I can not use "\ba::\b" to match
"a::b".

I would like to match "a::" in "a::b", but not in "a:: b". That is,
the character after "::" should be a alphanumeric character. Could you
let me know how to do it and why "\b" would not work?

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


Is there a metacharacter to match transitions between any two of a set of non intersected char classes?

2008-07-24 Thread Peng Yu
Hi,

'\b' only match the boundary between alphanumerical char and
nonalphanumerical char. I'm wonder if there is a generic metacharacter
to match the boundary between any non intersected char set defined in
regex and its complement.

For example, I have a few non intersected char sets [a-zA-Z_0-9], [:]
and [\s], and lets call the new metacharactor \m.

r"abc\m" would be the same as r"abc\b"
r"abc:\m' would match "abc:" in r"abc:xy" but not "abc::xy"
r"a \m" (with one space) would match "a " (with one space) in "a abc"
but not "a  abc" (with two spaces).

I would like that I have the flexibility to define more non
intersected char sets, while I don't want change the regex that I have
written.

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


How to delete a line with re?

2008-08-17 Thread Peng Yu
Hi,

I want to delete the line with abc in the following program. But the
following program does not do what I want. Can somebody let me know
how to do it?

Thanks,
Peng

#!/usr/bin/python

import re

file="""abcd
efg
hijk
lmn
"""

regex = re.compile("^abcd$", re.MULTILINE)

print file,
print "$"
print regex.sub('', file),
--
http://mail.python.org/mailman/listinfo/python-list


Exception as the primary error handling mechanism?

2009-12-31 Thread Peng Yu
I observe that python library primarily use exception for error
handling rather than use error code.

In the article API Design Matters by Michi Henning

Communications of the ACM
Vol. 52 No. 5, Pages 46-56
10.1145/1506409.1506424
http://cacm.acm.org/magazines/2009/5/24646-api-design-matters/fulltext

It says "Another popular design flaw—namely, throwing exceptions for
expected outcomes—also causes inefficiencies because catching and
handling exceptions is almost always slower than testing a return
value."

My observation is contradicted to the above statement by Henning. If
my observation is wrong, please just ignore my question below.

Otherwise, could some python expert explain to me why exception is
widely used for error handling in python? Is it because the efficiency
is not the primary goal of python?
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Exception as the primary error handling mechanism?

2010-01-01 Thread Peng Yu
On Thu, Dec 31, 2009 at 11:24 PM, Chris Rebert  wrote:
> On Thu, Dec 31, 2009 at 8:47 PM, Peng Yu  wrote:
>> I observe that python library primarily use exception for error
>> handling rather than use error code.
>>
>> In the article API Design Matters by Michi Henning
>>
>> Communications of the ACM
>> Vol. 52 No. 5, Pages 46-56
>> 10.1145/1506409.1506424
>> http://cacm.acm.org/magazines/2009/5/24646-api-design-matters/fulltext
>>
>> It says "Another popular design flaw—namely, throwing exceptions for
>> expected outcomes—also causes inefficiencies because catching and
>> handling exceptions is almost always slower than testing a return
>> value."
>>
>> My observation is contradicted to the above statement by Henning. If
>> my observation is wrong, please just ignore my question below.
>>
>> Otherwise, could some python expert explain to me why exception is
>> widely used for error handling in python? Is it because the efficiency
>> is not the primary goal of python?
>
> Correct; programmer efficiency is a more important goal for Python instead.
> Python is ~60-100x slower than C;[1] if someone is worried by the
> inefficiency caused by exceptions, then they're using completely the
> wrong language.

Could somebody let me know how the python calls and exceptions are
dispatched? Is there a reference for it?
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Exception as the primary error handling mechanism?

2010-01-02 Thread Peng Yu
On Sat, Jan 2, 2010 at 6:05 AM, Diez B. Roggisch  wrote:
> Peng Yu schrieb:
>>
>> On Thu, Dec 31, 2009 at 11:24 PM, Chris Rebert  wrote:
>>>
>>> On Thu, Dec 31, 2009 at 8:47 PM, Peng Yu  wrote:
>>>>
>>>> I observe that python library primarily use exception for error
>>>> handling rather than use error code.
>>>>
>>>> In the article API Design Matters by Michi Henning
>>>>
>>>> Communications of the ACM
>>>> Vol. 52 No. 5, Pages 46-56
>>>> 10.1145/1506409.1506424
>>>> http://cacm.acm.org/magazines/2009/5/24646-api-design-matters/fulltext
>>>>
>>>> It says "Another popular design flaw—namely, throwing exceptions for
>>>> expected outcomes—also causes inefficiencies because catching and
>>>> handling exceptions is almost always slower than testing a return
>>>> value."
>>>>
>>>> My observation is contradicted to the above statement by Henning. If
>>>> my observation is wrong, please just ignore my question below.
>>>>
>>>> Otherwise, could some python expert explain to me why exception is
>>>> widely used for error handling in python? Is it because the efficiency
>>>> is not the primary goal of python?
>>>
>>> Correct; programmer efficiency is a more important goal for Python
>>> instead.
>>> Python is ~60-100x slower than C;[1] if someone is worried by the
>>> inefficiency caused by exceptions, then they're using completely the
>>> wrong language.
>>
>> Could somebody let me know how the python calls and exceptions are
>> dispatched? Is there a reference for it?
>
> The source?
>
> http://python.org/ftp/python/2.6.4/Python-2.6.4.tgz
>
> These are really deep internals that - if they really concern you - need
> intensive studies, not casual reading of introductionary documents. IMHO you
> shouldn't worry, but then, there's a lot things you seem to care I
> wouldn't... :)

For my own interest, I want understand the run time behavior of python
and what details causes it much slower. Although people choose python
for its programming efficiency, but sometimes the runtime still
matters. This is an important aspect of the language. I'm wondering
this is not even documented.  Why everybody has to go to the source
code to understand it?

Are you sure that there is no document that describes how python is
working internally (including exceptions)?
-- 
http://mail.python.org/mailman/listinfo/python-list


Is there a method (similar to str() method in R) that can print the data structure in python?

2009-09-26 Thread Peng Yu
Hi,

I am looking for a method in python that is similar to the function
str() in R, if you are familiar with R,

If you have no idea of R, what I want is to print the class
information of an object and the values of its members. Overloading
'__expr__' and '__repr__' then using 'print' can sort of do what I
want. But not quite. For example, if I have a list of many elements, I
don't want to print all the elements. R's str() function can
automatically take care of this issue. It also has other advantages, I
am wondering if there is something similar available in python?

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


Why the file mode of .pyc files has x?

2009-09-26 Thread Peng Yu
Hi,

It is strange that the file mode of .pyc files are changed to
executable (x mode) after the corresponding .py files have been run. I
have the follow python, which is compiled from source code by me.

$ python --version
Python 2.6.2

Should .pyc files have the x mode?

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


Re: Why the file mode of .pyc files has x?

2009-09-26 Thread Peng Yu
On Sat, Sep 26, 2009 at 8:10 AM, Steven D'Aprano
 wrote:
> On Sat, 26 Sep 2009 06:57:52 -0500, Peng Yu wrote:
>
>> Hi,
>>
>> It is strange that the file mode of .pyc files are changed to executable
>> (x mode) after the corresponding .py files have been run.
>
> Are you sure? They don't on my system.
>
>
> [st...@sylar test]$ ls -l
> total 8
> -rw-rw-r-- 1 steve steve 6 2009-09-26 23:06 test.py
> [st...@sylar test]$ python2.6 test.py
> [st...@sylar test]$ ls -l
> total 8
> -rw-rw-r-- 1 steve steve 6 2009-09-26 23:06 test.py
>
>
> Running a .py file does not generally create a .pyc file. Normally you
> have to import it:
>
> [st...@sylar test]$ python2.6
> Python 2.6.1 (r261:67515, Dec 24 2008, 00:33:13)
> [GCC 4.1.2 20070502 (Red Hat 4.1.2-12)] on linux2
> Type "help", "copyright", "credits" or "license" for more information.
>>>> import test
>>>> exit()
> [st...@sylar test]$ ls -l
> total 16
> -rw-rw-r-- 1 steve steve  6 2009-09-26 23:06 test.py
> -rw-rw-r-- 1 steve steve 94 2009-09-26 23:08 test.pyc
>
>
> Have you checked the umask of your system?

Here are my test case. If the .py file has the 'x' mode, the
corresponding .pyc file also has the 'x' mode after the .py file is
imported.

pe...@selenium:~/test/python/pyc_mode$ umask
0077
pe...@selenium:~/test/python/pyc_mode$ ll
total 8
-rw--- 1 pengy lilab  29 2009-09-26 10:10:45 main.py
-rwx-- 1 pengy lilab 106 2009-09-26 10:19:17 test.py
pe...@selenium:~/test/python/pyc_mode$ for f in *.py; do echo "###$f";
cat $f;done
###main.py
import test
test.test_func()
###test.py
#!/usr/bin/env python

def test_func() :
  print "in_test_func"

if __name__ == '__main__':
  test_func()
pe...@selenium:~/test/python/pyc_mode$ python main.py
in_test_func
pe...@selenium:~/test/python/pyc_mode$ ll
total 12
-rw--- 1 pengy lilab  29 2009-09-26 10:10:45 main.py
-rwx-- 1 pengy lilab 106 2009-09-26 10:19:17 test.py
-rwx-- 1 pengy lilab 339 2009-09-26 10:20:39 test.pyc
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Is there a method (similar to str() method in R) that can print the data structure in python?

2009-09-27 Thread Peng Yu
On Sat, Sep 26, 2009 at 2:05 PM, Robert Kern  wrote:
> On 2009-09-26 09:32 AM, Peng Yu wrote:
>>
>> Hi,
>>
>> I am looking for a method in python that is similar to the function
>> str() in R, if you are familiar with R,
>>
>> If you have no idea of R, what I want is to print the class
>> information of an object and the values of its members. Overloading
>> '__expr__' and '__repr__' then using 'print' can sort of do what I
>> want. But not quite. For example, if I have a list of many elements, I
>> don't want to print all the elements. R's str() function can
>> automatically take care of this issue. It also has other advantages, I
>> am wondering if there is something similar available in python?
>
> I use Armin Ronacher's pretty.py as a pluggable pretty-printer. You can plug
> into its logic to implement these kinds of tools.
>
>  http://dev.pocoo.org/hg/sandbox/file/tip/pretty

But I want an even simpler solution. I don't want the user to define
__pretty__. Is there a tool that can automatically print the content
of an object without defining such a member function like __pretty__.

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


Re: Is there a method (similar to str() method in R) that can print the data structure in python?

2009-09-27 Thread Peng Yu
On Sun, Sep 27, 2009 at 1:20 PM, Simon Forman  wrote:
> On Sun, Sep 27, 2009 at 12:14 PM, Peng Yu  wrote:
>> On Sat, Sep 26, 2009 at 2:05 PM, Robert Kern  wrote:
>>> On 2009-09-26 09:32 AM, Peng Yu wrote:
>>>>
>>>> Hi,
>>>>
>>>> I am looking for a method in python that is similar to the function
>>>> str() in R, if you are familiar with R,
>>>>
>>>> If you have no idea of R, what I want is to print the class
>>>> information of an object and the values of its members. Overloading
>>>> '__expr__' and '__repr__' then using 'print' can sort of do what I
>>>> want. But not quite. For example, if I have a list of many elements, I
>>>> don't want to print all the elements. R's str() function can
>>>> automatically take care of this issue. It also has other advantages, I
>>>> am wondering if there is something similar available in python?
>>>
>>> I use Armin Ronacher's pretty.py as a pluggable pretty-printer. You can plug
>>> into its logic to implement these kinds of tools.
>>>
>>>  http://dev.pocoo.org/hg/sandbox/file/tip/pretty
>>
>> But I want an even simpler solution. I don't want the user to define
>> __pretty__. Is there a tool that can automatically print the content
>> of an object without defining such a member function like __pretty__.
>>
>> Regards,
>> Peng
>
> Have you examined the pprint module?
>
> http://docs.python.org/library/pprint.html
>
> The repr module might also be of interest:
>
> http://docs.python.org/library/repr.html

Here is the example that I tried. It seems that I have to define
__expr__ to show A's member '_x'.  But this is tedious if A has many
members. I'm looking for a solution that I don't have to write
anything explicitly in a class in order to print it pretty.

$ cat main.py
import pprint

class A:
  def __init__(self):
self._x = 10

  def show(self):
print self._x

a = A()
a.show()

pp = pprint.PrettyPrinter()
pp.pprint(a)
$ python main.py
10
<__main__.A instance at 0x2aba2bd8>
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Why the file mode of .pyc files has x?

2009-09-28 Thread Peng Yu
On Mon, Sep 28, 2009 at 12:43 AM, greg  wrote:
> Peng Yu wrote:
>>
>> -rw--- 1 pengy lilab  29 2009-09-26 10:10:45 main.py
>> -rwx-- 1 pengy lilab 106 2009-09-26 10:19:17 test.py
>> -rwx-- 1 pengy lilab 339 2009-09-26 10:20:39 test.pyc
>
> Doesn't happen for me with 2.5 on Darwin.
>
> What python/OS are you using?

python 2.6.2 and CentOS
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Why the file mode of .pyc files has x?

2009-09-28 Thread Peng Yu
On Mon, Sep 28, 2009 at 8:22 AM, Steven D'Aprano
 wrote:
> On Mon, 28 Sep 2009 17:01:40 +1000, Cameron Simpson wrote:
>
>> On 26Sep2009 10:24, Peng Yu  wrote: | >
>> [st...@sylar test]$ ls -l
>> | > total 16
>> | > -rw-rw-r-- 1 steve steve  6 2009-09-26 23:06 test.py | > -rw-rw-r--
>> 1 steve steve 94 2009-09-26 23:08 test.pyc | >
>> | > Have you checked the umask of your system? |
>> | Here are my test case. If the .py file has the 'x' mode, the |
>> corresponding .pyc file also has the 'x' mode after the .py file is |
>> imported.
>>
>> Yes, I see this too.
>
> Is this a problem?
>
> Taking a wild guess, I'd imagine that the .pyc file gets its permissions
> copied from the .py file. I don't see that as a major issue. A buglet,
> rather than a bug, at worst.

Although this is a small issue, I still think that it is annoying. If
it can be fixed in the future version of python, that will be great.

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


Re: How to refer to class name and function name in a python program?

2009-09-28 Thread Peng Yu
On Sun, Sep 20, 2009 at 12:43 PM, Benjamin Kaplan
 wrote:
> On Sun, Sep 20, 2009 at 12:43 PM, Peng Yu  wrote:
>> On Sun, Sep 20, 2009 at 11:32 AM, Vijayendra Bapte
>>  wrote:
>>> On Sep 20, 8:38 pm, Peng Yu  wrote:
>>>> Hi,
>>>>
>>>> I have the following code. I want to change the function body of
>>>> __repr__ to something like
>>>>
>>>>     return 'In %s::%s' % ($class_name, $function_name)
>>>>
>>>> I'm wondering what I should write for $class_name and $function_name in 
>>>> python.
>>>>
>>>> Regards,
>>>> Peng
>>>>
>>>> class A:
>>>>   def __init__(self):
>>>>     pass
>>>>
>>>>   def __repr__(self):
>>>>     return 'In A::__repr__'
>>>>
>>>> a = A()
>>>> print a
>>>
>>> Using decorator:
>>> 
>>>
>>> def echo(func):
>>>    def _echo(self, *args, **kw):
>>>        return "In %s.%s" % (self.__class__.__name__, func.func_name)
>>>
>>>    return _echo
>>>
>>> class A:
>>>
>>>   �...@echo
>>>    def __repr__(self):
>>>        pass
>>>
>>> a = A()
>>> print a
>>
>> What does @echo mean?
>>
>> Regards,
>> Peng
>
> It's a decorator, which wraps the function with another function it's
> the equivalent of calling
>
> def __repr__(self) :
>    pass
>
> __repr__ = echo(__repr__)

I looked at the table of content of python tutorial at
http://docs.python.org/tutorial/

But I don't see which section discuss this concept. If it is there,
would you please let me know which section I should read. Or this
concept is discussed somewhere else?

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


How to jump out of nested 'for'-loops?

2009-09-28 Thread Peng Yu
Hi,

I want some command to jump out of nested loop. I'm wondering what is
the most convenient way to do so in python.

for i in range(10):
  print "i = ", i
  for j in range(10):
if i*10 + j == 50:
  print i*10 + j
  break # I want to jump out of the loops.

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


  1   2   3   >