Re: sys.stdout.write()'s bug or doc bug?

2008-12-28 Thread Qiangning Hong
On Dec 27, 12:31 am, Martin  wrote:
> Python 2.4.4 (#2, Oct 22 2008, 19:52:44)
> [GCC 4.1.2 20061115 (prerelease) (Debian 4.1.1-21)] on linux2
> Type "help", "copyright", "credits" or "license" for more information.
> >>> u = u"\u554a"
> >>> print u
> 啊
> >>> sys.stdout.write(u + "\n")
>
> Traceback (most recent call last):
>  File "", line 1, in ?
> UnicodeEncodeError: 'ascii' codec can't encode character u'\u554a' in
> position 0: ordinal not in range(128)
>
> >>> # you are trying to write unicode, you need to encode it to something 
> >>> that suits your needs
> >>> sys.stdout.write(u.encode("UTF-8") + "\n")
> 啊
> >>> # now go and write a hundred times "Unicode is not an encoding" :)

Actually, I know relationship between unicode and str objects very
well.  That's why I only quoted the unicode-related part of
file.encoding's documentation in my original post.  Thank you.

> > So, my question is, as sys.stdout IS a file object, why it does not
> > use its encoding attribute to convert the given unicode?  An
> > implementation bug? A documenation bug?
>
> hmm I always thought "sys.stdout" is a "file-like object" not that it IS a 
> file.

In my original post, I have figured out that sys.stdout IS a file, by
using type() function.  And isinstance() function tells the same:

Python 2.5.2 (r252:60911, Dec 18 2008, 12:39:19)
[GCC 4.2.1 (Apple Inc. build 5564)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> import sys
>>> type(sys.stdout) is file
True
>>> isinstance(sys.stdout, file)
True

So, sys.stdout SHOULD do what the doc says, otherwise there is a bug
either in implementation of sys.stdout, or in the documentation of
file.
--
http://mail.python.org/mailman/listinfo/python-list


sys.stdout.write()'s bug or doc bug?

2008-12-25 Thread Qiangning Hong
>>> u = u'\u554a'
>>> print u
啊
>>> sys.stdout.write(u)
Traceback (most recent call last):
  File "", line 1, in 
UnicodeEncodeError: 'ascii' codec can't encode character u'\u554a' in
position 0: ordinal not in range(128)
>>> type(sys.stdout)

>>> sys.stdout.encoding
'UTF-8'

Quote from file object's documentation:

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. .

So, my question is, as sys.stdout IS a file object, why it does not
use its encoding attribute to convert the given unicode?  An
implementation bug? A documenation bug?
--
http://mail.python.org/mailman/listinfo/python-list


Re: Prevent self being passed to a function stored as a member variable?

2006-09-04 Thread Qiangning Hong
On 4 Sep 2006 09:39:32 -0700, Sandra-24 <[EMAIL PROTECTED]> wrote:
> How can you prevent self from being passed to a function stored as a
> member variable?
>
> class Foo(object):
>def __init__(self, callback):
>   self.func = callback
>
> f =Foo(lambda x: x)
> f.func(1) # TypeError, func expects 1 argument, recieved 2

Do you really get that error? I got the following in my python shell:

.>>> class Foo(object):
   def __init__(self, callback):
 self.func = callback

.>>> f = Foo(lambda x: x)
.>>> f.func(1)
1
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Is this a good idea or a waste of time?

2006-08-24 Thread Qiangning Hong
On 24 Aug 2006 20:53:49 -0700, [EMAIL PROTECTED] <[EMAIL PROTECTED]> wrote:
> That's bad form. If you insist on doing something like this, at least
> use "isinstance(a, str)" instead of typeof.  But even that breaks duck
> typing; if a is a unicode string, that'll fail when the function may
> work fine for unicode strings.

To check both str and unicode, you could use "isinstance(a, basestring)".
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: splitting words with brackets

2006-07-26 Thread Qiangning Hong
Tim Chase wrote:
> Ah...the picture is becoming a little more clear:
>
>  >>> r = re.compile(r'(?:\([^\)]*\)|\[[^\]]*\]|\S)+')
>  >>> r.findall(s)
> ['(a c)b(c d)', 'e']
>
> It also works on my original test data, and is a cleaner regexp
> than the original.
>
> The clearer the problem, the clearer the answer. :)

Ah, it's exactly what I want!  I thought the left and right sides of
"|" are equal, but it is not true.  I think I must sleep right now,
lacking of sleep makes me a dull :-p.  Thank you and Simon for your
kindly help!

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


Re: splitting words with brackets

2006-07-26 Thread Qiangning Hong
Simon Forman wrote:
> What are the desired results in cases like this:
> 
> "(a b)[c d]" or "(a b)(c d)" ?

["(a b)[c d]"], ["(a b)(c d)"]

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


Re: splitting words with brackets

2006-07-26 Thread Qiangning Hong
Simon Forman wrote:
> def splitup(s):
> return re.findall('''
> \S*\( [^\)]* \)\S*  |
> \S*\[ [^\]]* \]\S*  |
> \S+
> ''', s, re.VERBOSE)

Yours is the same as Tim's, it can't handle a word with two or more
brackets pairs, too.

I tried to change the "\S*\([^\)]*\)\S*" part to "(\S|\([^\)]*\))*",
but it turns out to a mess.

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


Re: splitting words with brackets

2006-07-26 Thread Qiangning Hong
Tim Chase wrote:
>  >>> import re
>  >>> s ='a (b c) d [e f g] h ia abcd(b c)xyz d [e f g] h i'
>  >>> r = re.compile(r'(?:\S*(?:\([^\)]*\)|\[[^\]]*\])\S*)|\S+')
>  >>> r.findall(s)
> ['a', '(b c)', 'd', '[e f g]', 'h', 'ia', 'abcd(b c)xyz', 'd',
> '[e f g]', 'h', 'i']
>
[...]
> However, the above monstrosity passes the tests I threw at
> it.

but it can't pass this one: "(a c)b(c d) e"
the above regex gives out ['(a c)b(c', 'd)', 'e'], but the correct one
should be ['(a c)b(c d)', 'e']

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


Re: splitting words with brackets

2006-07-26 Thread Qiangning Hong
faulkner wrote:
> re.findall('\([^\)]*\)|\[[^\]]*|\S+', s)

sorry i forgot to give a limitation: if a letter is next to a bracket,
they should be considered as one word. i.e.:
"a(b c) d" becomes ["a(b c)", "d"]
because there is no blank between "a" and "(".

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


splitting words with brackets

2006-07-26 Thread Qiangning Hong
I've got some strings to split.  They are main words, but some words
are inside a pair of brackets and should be considered as one unit.  I
prefer to use re.split, but haven't written a working one after hours
of work.

Example:

"a (b c) d [e f g] h i"
should be splitted to
["a", "(b c)", "d", "[e f g]", "h", "i"]

As speed is a factor to consider, it's best if there is a single line
regular expression can handle this.  I tried this but failed:
re.split(r"(?![\(\[].*?)\s+(?!.*?[\)\]])", s).  It work for "(a b) c"
but not work "a (b c)" :(

Any hint?

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


Re: hash() yields different results for different platforms

2006-07-11 Thread Qiangning Hong
Grant Edwards wrote:
> On 2006-07-11, Qiangning Hong <[EMAIL PROTECTED]> wrote:
> > However, when I come to Python's builtin hash() function, I
> > found it produces different values in my two computers!  In a
> > pentium4, hash('a') -> -468864544; in a amd64, hash('a') ->
> > 12416037344.  Does hash function depend on machine's word
> > length?
>
> Apparently. :)
>
> The low 32 bits match, so perhaps you should just use that
> portion of the returned hash?
>
> >>> hex(12416037344)
> '0x2E40DB1E0L'
> >>> hex(-468864544 & 0x)
> '0xE40DB1E0L'
>
> >>> hex(12416037344 & 0x)
> '0xE40DB1E0L'
> >>> hex(-468864544 & 0x)
> '0xE40DB1E0L'

Is this relationship (same low 32 bits) guaranteed?  Will it change in
the future version?

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


hash() yields different results for different platforms

2006-07-11 Thread Qiangning Hong
I'm writing a spider. I have millions of urls in a table (mysql) to
check if a url has already been fetched. To check fast, I am
considering to add a "hash" column in the table, make it a unique key,
and use the following sql statement:
  insert ignore into urls (url, hash) values (newurl, hash_of_newurl)
to add new url.

I believe this will be faster than making the "url" column unique key
and doing string comparation.  Right?

However, when I come to Python's builtin hash() function, I found it
produces different values in my two computers!  In a pentium4,
hash('a') -> -468864544; in a amd64, hash('a') -> 12416037344.  Does
hash function depend on machine's word length?

If it does, I must consider another hash algorithm because the spider
will run concurrently in several computers, some are 32-bit, some are
64-bit.  Is md5 a good choice? Will it be too slow that I have no
performance gain than using the "url" column directly as the unique
key?

I will do some benchmarking to find it out. But while making my hands
dirty, I would like to hear some advice from experts here. :)

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


Re: How to build Python modules on windows?

2005-08-12 Thread Qiangning Hong
". <"@bag.python.org wrote:
> Hi,
> 
> how can I build python modules on windows? I tried to build numarray[0] 
> using Microsoft Visual C++ 2003 Toolkit, but got the following error:
> 
> ---
> error: Python was built with version 7.1 of Visual Studio, and 
> extensions need to be built with the same version of the compiler, but 
> it isn't installed.
> ---

Are you sure you have setup the environment variables before you build?

Here is a reference: "Building Python Extensions with the MS Toolkit
Compiler" (http://www.vrplumber.com/programming/mstoolkit/)

[snip]

-- 
Qiangning Hong

I'm usually annoyed by IDEs because, for instance, they don't use VIM
as an editor. Since I'm hooked to that, all IDEs I've used so far have
failed to impress me.
-- Sybren Stuvel @ c.l.python

Get Firefox!
<http://www.spreadfirefox.com/?q=affiliates&id=67907&t=1>
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: need help with my append syntax

2005-08-12 Thread Qiangning Hong
Do not discuss off-list, maybe others will have better solutions to your
question.  And also please do not top-posting, it makes me difficult to
trim the irrelevant text.

yaffa wrote:
> sorry addr is a variable.  how to i append to that?

I know addr is a variable (or better a name).  But what object do you
assign to it?  I mean, does incident.findNextSibling('td') return a
string or an object of another type?

If your code "addr.append('%s;')" doesn't raise an exception, it is
pretty sure what assigned to addr is not a string (maybe a list, which
has an "append" method).  You can use "print addr" or "print repr(addr)"
to determine that.


> - Original Message - From: "Qiangning Hong" <[EMAIL PROTECTED]>
> To: "yaffa" <[EMAIL PROTECTED]>
> Cc: 
> Sent: Friday, August 12, 2005 12:47 PM
> Subject: Re: need help with my append syntax
> 
> 
>> yaffa wrote:
>>
>>> dear folks,
>>>
>>> i'm trying to append a semicolon to my addr string and am using the
>>> syntax below.  for some reason the added on of the ; doesn't work.
>>> when i print it out later on it only shows the original value of addr.
>>>
>>> addr = incident.findNextSibling('td')
>>> addr.append('%s;')
>>
>>
>> Is addr is really a string?  AFAIK, strings havn't an append methond.
>>
>> use += to extend strings:
>>
>> .>>> addr = 'abc'
>> .>>> addr += '%s;'
>> .>>> addr
>> 'abc%s;'
>>
>> -- 
>> Qiangning Hong
>>
>> I'm usually annoyed by IDEs because, for instance, they don't use VIM
>> as an editor. Since I'm hooked to that, all IDEs I've used so far have
>> failed to impress me.
>>-- Sybren Stuvel @ c.l.python
>>
>> Get Firefox!
>> <http://www.spreadfirefox.com/?q=affiliates&id=67907&t=1>
>>
> 


-- 
Qiangning Hong

I'm usually annoyed by IDEs because, for instance, they don't use VIM
as an editor. Since I'm hooked to that, all IDEs I've used so far have
failed to impress me.
-- Sybren Stuvel @ c.l.python

Get Firefox!
<http://www.spreadfirefox.com/?q=affiliates&id=67907&t=1>
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: need help with my append syntax

2005-08-12 Thread Qiangning Hong
yaffa wrote:
> dear folks,
> 
> i'm trying to append a semicolon to my addr string and am using the
> syntax below.  for some reason the added on of the ; doesn't work.
> when i print it out later on it only shows the original value of addr.
> 
> addr = incident.findNextSibling('td')
> addr.append('%s;')

Is addr is really a string?  AFAIK, strings havn't an append methond.

use += to extend strings:

.>>> addr = 'abc'
.>>> addr += '%s;'
.>>> addr
'abc%s;'

-- 
Qiangning Hong

I'm usually annoyed by IDEs because, for instance, they don't use VIM
as an editor. Since I'm hooked to that, all IDEs I've used so far have
failed to impress me.
-- Sybren Stuvel @ c.l.python

Get Firefox!
<http://www.spreadfirefox.com/?q=affiliates&id=67907&t=1>
-- 
http://mail.python.org/mailman/listinfo/python-list


what's wrong with my code using subprocess?

2005-07-23 Thread Qiangning Hong
I decide to seperate my data collection routine from my data analysis
and storage program to a seperate process, so I try to use the new
subprocess model in Python 2.4.

The main program spawns the subprocess and receives data from the
pipe.  When some event occurs (e.g. the user clicks the 'Stop' button
on GUI), the main program will send the subprocess a command to change
its behavior or ask it to exit.

However, my code (attached below) doesn't work.  Under Linux, the output is:

waiting subprocess exit
Traceback (most recent call last):
  File "receiver.py", line 19, in ?
main()
  File "receiver.py", line 13, in main
print >>p.stdin, 'exit'
IOError: [Errno 32] Broken pipe


And Under Windows XP, p.wait() never returns:

waiting subprocess exit
[hanging here]


What's wrong?  

# collector.py
import threading

class Main(object):
def __init__(self):
self.keep_going = True
self.t = threading.Thread(target=self.work)
self.t.start()

cmd = raw_input()
while cmd != 'exit':
cmd = raw_input()

self.keep_going = False
self.t.join()

def work(self):
while self.keep_going:
print '$' * 82

if __name__ == '__main__':
Main()


# receiver.py (the main program)
from subprocess import Popen, PIPE

def main():
p = Popen(['python', 'collector.py'], stdout=PIPE, stdin=PIPE)
count = 0
for line in p.stdout:
data = line.strip()
# process(data)
count += 1
if count >= 1000:
print >>p.stdin, 'exit'
print 'waiting subprocess exit'
p.wait()

if __name__ == '__main__':
main()


-- 
Qiangning Hong

I'm usually annoyed by IDEs because, for instance, they don't use VIM
as an editor. Since I'm hooked to that, all IDEs I've used so far have
failed to impress me.
   -- Sybren Stuvel @ c.l.python

Get Firefox! <http://www.spreadfirefox.com/?q=affiliates&id=67907&t=1>
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: tuple to string?

2005-07-22 Thread Qiangning Hong
Francois De Serres wrote:
> hiho,
> 
> what's the clean way to translate the tuple (0x73, 0x70, 0x61, 0x6D) to 
> the string 'spam'?

Use ''.join and chr() as others have pointed out.  Here are just-for-fun 
versions ;)

.>>> t = (0x73, 0x70, 0x61, 0x6D)

(use string formatter):
.>>> '%c%c%c%c' % t
'spam'

(use struct model):
.>>> import struct
.>>> struct.pack('', *t)
'spam'

-- 
Qiangning Hong

I'm usually annoyed by IDEs because, for instance, they don't use VIM
as an editor. Since I'm hooked to that, all IDEs I've used so far have
failed to impress me.
 -- Sybren Stuvel @ c.l.python

Get Firefox! 
<http://www.spreadfirefox.com/?q=affiliates&id=67907&t=1>
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: tuple to string?

2005-07-22 Thread Qiangning Hong
On 7/22/05, Francois De Serres <[EMAIL PROTECTED]> wrote:
> what's the clean way to translate the tuple (0x73, 0x70, 0x61, 0x6D) to
> the string 'spam'?

Use ''.join and chr() as others have pointed out.  Here are
just-for-fun versions  ;)

.>>> t = (0x73, 0x70, 0x61, 0x6D)

(use string formatter):
.>>> '%c%c%c%c' % t
'spam'

(use struct model):
.>>> import struct
.>>> struct.pack('', *t)
'spam' 

-- 
Qiangning Hong

I'm usually annoyed by IDEs because, for instance, they don't use VIM
as an editor. Since I'm hooked to that, all IDEs I've used so far have
failed to impress me.
   -- Sybren Stuvel @ c.l.python

Get Firefox! <http://www.spreadfirefox.com/?q=affiliates&id=67907&t=1>
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: PyArg_ParseTuple help

2005-07-10 Thread Qiangning Hong
[EMAIL PROTECTED] wrote:
> hello all,
>   how to parse the arguments of a c function with PyArg_ParseTuple?
>  The prototype of the c function is :
>   int func(unsigned char * in , int inlen, unsigned char * v, unsigned
> char * out, int * outlen);
> 
> The problem is , when the func returns, there will be results in out
> and outlen. Is the following format is correct?

As the ``out'' and ``outlen'' are used for output, you should not pass
them to a python function as parameters.  Instead, you'd better make the
return value of the python function to represent them (if the python
caller wants them, of course).  In your case, returning a python string
object will be a good choice if outlen is the length of out array, since
a python string knows its length.

So, you need only parse the parameters of ``in'', ``inlen'' and ``v'' in
the wrap function.  And even further, the python caller can pass a
python string to replace ``in'' and ``inlen''.  Then, the prototype of
the function is something like:

def func(ins, v)

which returns a string.

[...]

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


Re: frozenset question

2005-07-06 Thread Qiangning Hong
On 7/6/05, Will McGugan <[EMAIL PROTECTED]> wrote:
> Hi,
> 
> Are there any benefits in using a frozenset over a set, other than it
> being immutable?

A frozenset can be used as a key of a dict:

.>> s1 = set([1])
.>> s2 = frozenset([2])
.>> {s1: 1}
Traceback (most recent call last):
  File "", line 1, in ?
TypeError: set objects are unhashable
.>> {s2:1}
{frozenset([2]): 1}

-- 
Qiangning Hong
Get Firefox! <http://www.spreadfirefox.com/?q=affiliates&id=67907&t=1>
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Create datetime instance using a tuple.

2005-07-06 Thread Qiangning Hong
On 6 Jul 2005 02:01:55 -0700, Negroup <[EMAIL PROTECTED]> wrote:
> Hi, all.
> I would like to know if it is possible to create a datetime instance
> using a tuple instead of single values.
> 
> I mean:
> >>> from datetime import datetime
> >>> t = (1, 2, 3)
> >>> dt = datetime(t)
> Traceback (most recent call last):
>   File "", line 1, in ?
> TypeError: function takes at least 3 arguments (1 given)
> 
> (class datetime(year, month, day[, hour[, minute[, second[,
> microsecond[, tzinfo])

Use:
dt = datetime(*t)

-- 
Qiangning Hong
Get Firefox! <http://www.spreadfirefox.com/?q=affiliates&id=67907&t=1>
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: debugger?

2005-07-03 Thread Qiangning Hong
Detlev Offenbach wrote:
> Qiangning Hong wrote:
> 
> 
>>I have read a lot of posts discussing python IDEs, but most of them
>>focus on the editor, GUI builder, project management, customizability,
>>etc  Some talked about debugging capability, but only on an
>>available/unavailable level.
>>
>>I use vim to edit my code, wxGlade to build the GUI or hand-code it,
>>and I still prefer that. So, until now, I haven't tried many IDEs.
>>
>>However, while I use pdb or inserting "print" statement to debug my
>>apps, sometimes it is a pain.  I think I need a good GUI debugger to
>>help me.  The debugger should meet _most_ of the following
>>requirements:
>>
>>1. can debug wxPython applications (and other GUI lib).
>>2. an intuitive way to set/clear/enable/disable breakpoints.
>>3. can set conditional breakpoints (i.e. break when some condition
>>satisfied). 4. variable watch list, namescope watching (local, global)
>>5. evaluate expression, change variable values, etc within debugging.
>>6. change the running routine, (i.e. go directly to a statement, skip
>>some statements, etc)
>>7. clever way to express objects, not just a string returned by repr()
>>8. perform profiling
>>9. a clear interface.
>>10. cross-platform.
>>11. free, or better, open source.
>>
>>What debugger will you suggest?
>>Or what more polish feature you want to see in an ideal python
>>debugger? -- hope this thread will help IDE developers to fill their
>>todo list with some shining ideas :)
>>
> 
> 
> Eric3 should be compliant with your list. Try it at
> http://www.die-offenbachs.de/detlev/eric3.html
> 
> Regards,
> Detlev

Eric3 need pyqt so it is not free under windows platform.


-- 
Qiangning Hong

 _
/ Humanity has advanced, when it has advanced, not because it \
| has been sober, responsible, and cautious, but because it   |
| has been playful, rebellious, and immature. |
| |
\ -- Tom Robbins  /
 -
   \ ,,
\   /()`
 \  \ \___   / |
/- _  `-/  '
   (/\/ \ \   /\
   / /   | `\
   O O   ) /|
   `-^--'`< '
  (_.)  _  )   /
   `.___/`/
 `-' /
<. __ / __   \
<|O)))==) \) /
<'`--' `.__,' \
 ||
  \   /
__( (_  / \__
  ,'  ,-'   |\
  `--{__)\/
-- 
http://mail.python.org/mailman/listinfo/python-list


debugger?

2005-07-03 Thread Qiangning Hong
I have read a lot of posts discussing python IDEs, but most of them
focus on the editor, GUI builder, project management, customizability,
etc  Some talked about debugging capability, but only on an
available/unavailable level.

I use vim to edit my code, wxGlade to build the GUI or hand-code it,
and I still prefer that. So, until now, I haven't tried many IDEs.

However, while I use pdb or inserting "print" statement to debug my
apps, sometimes it is a pain.  I think I need a good GUI debugger to
help me.  The debugger should meet _most_ of the following
requirements:

1. can debug wxPython applications (and other GUI lib).
2. an intuitive way to set/clear/enable/disable breakpoints.
3. can set conditional breakpoints (i.e. break when some condition satisfied).
4. variable watch list, namescope watching (local, global)
5. evaluate expression, change variable values, etc within debugging.
6. change the running routine, (i.e. go directly to a statement, skip
some statements, etc)
7. clever way to express objects, not just a string returned by repr()
8. perform profiling
9. a clear interface.
10. cross-platform.
11. free, or better, open source.

What debugger will you suggest?  
Or what more polish feature you want to see in an ideal python
debugger? -- hope this thread will help IDE developers to fill their
todo list with some shining ideas :)

-- 
Qiangning Hong
Get Firefox! <http://www.spreadfirefox.com/?q=affiliates&id=67907&t=1>
-- 
http://mail.python.org/mailman/listinfo/python-list


how to shrink a numarray array?

2005-06-29 Thread Qiangning Hong
To draw a large array of data on a small panel, I need to shrink it to a
given size. e.g:

To draw numarray.arange(1) on a panel of width of 100, I only need
to draw the points of (0, 100, 200, 300, ...) instead of (0, 1, 2, ...).
  So I need a method to shrink it to an 100-item array.

x[::len(x)/panel_width] will not work. If the panel's width is 60, that
expression will return an array of 61 elements.

I believe there is an existing function in numarray to do this, however
English is not my mother tongue and math is not my speciality, I can't
find it in the document full of math terms...

-- 
Qiangning Hong

 __
(  Michael:)
(  )
( Hi. I'm Michael Jackson, from The Jacksons.  )
(  )
( Homer: I'm Homer Simpson, from the Simpsons. )
(  )
( Stark Raving Dad )
 --
  o
   o   \
\ /\
( )
  .( o ).
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: utf8 silly question

2005-06-21 Thread Qiangning Hong
Catalin Constantin wrote:
> i have the following code:
> 
> c=chr(169)+" some text"
> 
> how can i utf8 encode the variable above ?
> something like in php utf8_encode($var);?!
> 
> chr(169) is the © (c) sign !
> 
> 10x for your help !
> 
> p.s.: i tryed using codecs, etc but always get an error message
> like: 'ascii' codec can't decode byte 0xa9 in position 0...

ascii can not handle chr(169), so you need to use some other encoding to
make unicode string first, such as latin-1.

In [1]:c = chr(169) + ' some text'

In [2]:u = unicode(c, 'latin-1')

In [3]:u8 = u.encode('UTF-8')

In [4]:u8
Out[4]:'\xc2\xa9 some text'

In [5]:print u8
© some text

*NOTE*: At the last statement "print u8" I can print a UTF-8 string
directly because my console is UTF-8 encoding.  On your machine the
© sign maybe not show.

-- 
Qiangning Hong

 ___
( Do you like "TENDER VITTLES"? )
 ---
 o /\  ___  /\
  o   // \/   \/ \\
 ((O O))
  \\ / \ //
   \/  | |  \/
|  | |  |
|  | |  |
|   o   |
| |   | |
|m|   |m|
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Can we pass some arguments to system("cmdline")?

2005-06-19 Thread Qiangning Hong
Didier C wrote:
> Hi!
>I was wondering if we can pass some arguments to system("cmdline")?
> 
> E.g in Perl, we can do something like:
> 
> $dir="/home/cypher";
> 
> system("ls $dir");
> 
> which would instruct Perl to do an "ls /home/cypher"
> 
> But in python, doing something like
> 
> dir="/home/cypher"
> system("ls dir")
> 
> would cause python to execute "ls dir" where "dir" might not exist at
> all! Is there a way to reproduce the same thing in Python?
> 
> Thanks for any insights.
> 
> cheers,
> Didier.

You should use something like this:

dir = "/home/cypher"
system("ls %s" % dir)

-- 
Qiangning Hong

 ___
/ lp1 on fire   \
|   |
\ -- One of the more obfuscated kernel messages /
 ---
\
 \
  \
  ___   _ ___
 /   \ //|   /   \
| |   // |  | |
| |  //  |  | |
| |  ||  |  | |
| |  | {} | /   | |
| |  ||/| |
| ||==| | |
|  \___/  |
| |
| |
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: collect data using threads

2005-06-14 Thread Qiangning Hong
James Tanis wrote:

> # > > > A class Collector, it spawns several threads to read from serial port.
> # > > > Collector.get_data() will get all the data they have read since last
> # > > > call.  Who can tell me whether my implementation correct?
> # > > >  
> # Here's the original code:
> # 
> # class Collector(object):
> #def __init__(self):
> #self.data = []
> #spawn_work_bees(callback=self.on_received)
> # 
> #def on_received(self, a_piece_of_data):
> #"""This callback is executed in work bee threads!"""
> #self.data.append(a_piece_of_data)
> # 
> #def get_data(self):
> #x = self.data
> #self.data = []
> #return x
> # 
> I may be wrong here, but shouldn't you just use a stack, or in other 
> words, use the list as a stack and just pop the data off the top. I 
> believe there is a method pop() already supplied for you. Since 
> you wouldn't require an self.data = [] this should allow you to safely 
> remove the data you've already seen without accidentally removing data 
> that may have been added in the mean time.
> 

I am the original poster.

I actually had considered Queue and pop() before I wrote the above code.
 However, because there is a lot of data to get every time I call
get_data(), I want a more CPU friendly way to avoid the while-loop and
empty checking, and then the above code comes out.  But I am not very
sure whether it will cause serious problem or not, so I ask here.  If
anyone can prove it is correct, I'll use it in my program, else I'll go
back to the Queue solution.

To Jeremy Jones: I am very sorry to take you too much effort on this
weird code.  I should make it clear that there is only *one* thread (the
main thread in my application) calls the get_data() method,
periodically, driven by a timer.  And for on_received(), there may be up
to 16 threads accessing it simultaneously.


-- 
Qiangning Hong

 ___
/ BOFH Excuse #208: \
|   |
| Your mail is being routed through Germany ... and they're |
\ censoring us. /
 ---
  \ ._  .
   \|\_|/__/|
   / / \/ \  \
  /__|O||O|__ \
 |/_ \_/\_/ _\ |
 | | () | ||
 \/\___/\__/  //
 (_/ ||
  |  ||
  |  ||\
   \//_/
\__//
   __ || __||
  (()
-- 
http://mail.python.org/mailman/listinfo/python-list


collect data using threads

2005-06-14 Thread Qiangning Hong
A class Collector, it spawns several threads to read from serial port.
Collector.get_data() will get all the data they have read since last
call.  Who can tell me whether my implementation correct?

class Collector(object):
def __init__(self):
self.data = []
spawn_work_bees(callback=self.on_received)

def on_received(self, a_piece_of_data):
"""This callback is executed in work bee threads!"""
self.data.append(a_piece_of_data)

def get_data(self):
x = self.data
self.data = []
return x

I am not very sure about the get_data() method.  Will it cause data lose
if there is a thread is appending data to self.data at the same time?

Is there a more pythonic/standard recipe to collect thread data?

-- 
Qiangning Hong

 ___
< Those who can, do; those who can't, simulate. >
 ---
\  ___---___
 \ _-~~ ~~-_
  \ _-~/~-_
 /^\__/^\ /~  \   /\
   /|  O|| O|/  \___/\
  | |___||__|  /   /\  \
  |  \/  /\  \
  |   (___) /__/\_ \
  | / / \  /\
   \ \^\\ \  /   \ /
 \ ||   \__/  _-_   //\__//
   \   ||--_-~~-_ - \ --/~   ~\|| __/
 ~-||/~ |==|   |/~
  (_(__/  ./ /\_\  \.
 (_(___/ \_)_)
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Show current ip on Linux

2005-06-13 Thread Qiangning Hong
David Van Mosselbeen wrote:
> Hi,
> Im a newbie in Python, and also in Fedora Core 3. (Yes, Linux is fine
> man :-)
> 
> My question is : How can i rwite a script that show my current ip. If i have
> more than one network card, the script must then show all used ip.
> 
> It's important that this will work on a linux. i Have rwite some piece of
> code that realy work under Windows XP, but the same script wil not work on
> Linux. 
> 
> Verry thanks to all vulunteers.
> 

How about use the shell command "ifconfig | grep inet" ?


-- 
Qiangning Hong

 _
(  so  when I do a chroot /path /bin/bash, i can  )
( see the processes   )
( )
( outside of the chroot  and i can kill those processes )
( * klieber claps for zhen  oh go die   )
 -
   o   \___
 v__v   o  \   O   )
 (OO)  ||w |
 (__)  || ||  \/\

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


Re: compare two voices

2005-04-30 Thread Qiangning Hong
Jeremy Bowers wrote:
> No matter how you slice it, this is not a Python problem, this is an
> intense voice recognition algorithm problem that would make a good
> PhD thesis.

No, my goal is nothing relative to voice recognition.  Sorry that I
haven't described my question clearly.  We are not teaching English, so
the voice recognition isn't helpful here.

I just want to compare two sound WAVE file, not what the students or
the teacher really saying.  For example, if the teacher recorded his
"standard" pronouncation of "god", then the student saying "good" will
get a higher score than the student saying "evil"  because "good"
sounds more like "god".

Yes, this not a Python problem, but I am a fan of Python and using
Python to develop the other parts of the application (UI, sound play
and record, grammer training, etc), so I ask here for available python
module, and of cause, for any kindly suggestions unrelative to Python
itself (like yours) too.

I myself have tried using Python's standard audioop module, using the
findfactor and rms functions.  I try to use the value returned from
rms(add(a, mul(b, -findfactor(a, b as the score.  But the result is
not good.  So I want to know if there is a human-voice optimized
algorithm/library out there.

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


compare two voices

2005-04-30 Thread Qiangning Hong
I want to make an app to help students study foreign language.  I want
the following function in it:

The student reads a piece of text to the microphone. The software
records it and compares it to the wave-file pre-recorded by the
teacher, and gives out a score to indicate the similarity between them.

This function will help the students pronounce properly, I think.

Is there an existing library (C or Python) to do this?  Or if someone
can guide me to a ready-to-implement algorithm?

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


distutils question: different projects under same namespace

2005-04-16 Thread Qiangning Hong
To avoid namespace confliction with other Python packages, I want all
my projects to be put into a specific namespace, e.g. 'hongqn' package,
so that I can use "from hongqn.proj1 import module1", "from
hongqn.proj2.subpack1 import module2", etc.

These projects are developed and maintained and distributed seperately,
so I should not make a whole 'hongqn' package with one setup.py.  I
must write setup.py scripts for each project.  I meet a problem here.

For instance, I am writing setup.py script for proj1. I use the
following parameters when calling distutils.core.setup:

setup(
...
package_dir = {'hongqn.proj1': 'proj1'},
packages = ['hongqn.proj1'],
...
)

"python setup.py install" will create /usr/lib/python2.3/hongqn/proj1
directory and copy proj1/*.py there.  But this is UNUSABLE!  There is
NO __init__.py file under /usr/lib/python2.3/hongqn/ so that import
hongqn.proj1 will fail!

I am considering manually create this __init__.py by hacking the
install_lib command.  But before making my hands dirty,  I want to know
if there is an "official" solution exists or others have already
success stories on this matter.

As a note, I wish the solution can handle setup.py bdist and
bdist_wininst well.

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


distutils question: different projects under same namespace

2005-04-15 Thread Qiangning Hong
To avoid namespace confliction with other Python packages, I want all
my projects to be put into a specific namespace, e.g. 'hongqn' package,
so that I can use "from hongqn.proj1 import module1", "from
hongqn.proj2.subpack1 import module2", etc.

These projects are developed and maintained and distributed seperately,
so I should not make a whole 'hongqn' package with one setup.py. 
I must write setup.py scripts for each project.  I meet a problem
here.

For instance, I am writing setup.py script for proj1. I use the following parameters when calling distutils.core.setup:

setup(
    ...
    package_dir = {'hongqn.proj1': 'proj1'},
    packages = ['hongqn.proj1'],
    ...
)

"python setup.py install" will create /usr/lib/python2.3/hongqn/proj1
directory and copy proj1/*.py there.  But this is UNUSABLE! 
There is NO __init__.py file under /usr/lib/python2.3/hongqn/ so that
import hongqn.proj1 will fail!
I am considering manually create this __init__.py by hacking the
install_lib command.  But before making my hands dirty,  I
want to know if there is an "official" solution exists or others have
already success stories on this matter.

As a note, I wish the solution can handle setup.py bdist and bdist_wininst well.
-- Qiangning HongGet Firefox! -- 
http://mail.python.org/mailman/listinfo/python-list

Re: distutils: package data

2005-03-30 Thread Qiangning Hong
ehh..  I did a little more reading and found that this function can be
easily done by the new distutils parameter "package_data" in 2.4.

However, I am using python2.3 :(

So, my question becomes: how to emulate the package_data function in
python 2.3?

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


distutils: package data

2005-03-29 Thread Qiangning Hong
I am writing a setup.py for my package.  I have a pre-compiled
myextmod.pyd file in my package and I want the distutils to
automatically copy it to
C:\Python23\Lib\site-packages\mypackage\myextmod.pyd.

I try to add the following parameter to setup():

   data_file = [('mypackage', ['mypackage/myextmod.pyd'])],

but it installs the pyd file to C:\Python23\mypackage\myextmod.pyd,
this is not what I want.

And I don't want to specify the path in data_file to the absolution
path 'C:\Python23\Lib\site-packages\mypackage', for portability, of
course.

Any hints?

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


unittest help

2005-03-24 Thread Qiangning Hong
I want to apply TDD (test driven development) on my project.  I am
working on a class like this (in plan):

# file: myclass.py
import _extmod

class MyClass(object):
def __init__(self):
self.handle = _extmod.open()

def __del__(self):
_extmod.close(self.handle)

def some_stuff(self):
_extmod.foobar(self.handle)

...

As you see, it is an OO wrapper on _extmod, which is a pyrex extension
module.  The question is: how to unittest this class?  As the _extmod
is hardware-dependent, I want to use a mock class to replace it in unit
test.  But how can I let myclass in unittest to import the mock class?
Like the following:

class MyClassTest(unittest.TestCase):
def setUp(self):
import myclass
import mocklib
myclass.change_extmod(mocklib.MockExtMod())
self.testobj = myclass.MyClass()  # here MyClass.__init__ will
call the open
  # method of MockExtMod class
instead of
  # _extmod.open()
...

How to implement the change_extmod?  (Or maybe my idea is totally
wrong?)

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


how to absolute import?

2005-03-10 Thread Qiangning Hong
>From Guido's PEP8:

  - Relative imports for intra-package imports are highly
  discouraged.  Always use the absolute package path for all
  imports.

Does it mean I should put my develop directory into PYTHONPATH (e.g.
/home/hongqn/devel/python) and use "import myproj1.package1.module1 as
module1" all the way?  Or should I make a "./setup.py install" for
every package whenever I made a change?

I'm using python 2.3 so from .foo import bar doesn't work for me.

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


predict directory write permission under windows?

2004-12-13 Thread Qiangning Hong
I want to know if I can write files into a directory before I actually 
perferm the write behavor.  I found os.access(path, os.W_OK) but it uses 
real uid/gid to check instead of euid/egid so it doesn't fit my problem.

I don't know how to get euid/egid under windows so I cannot use the mode 
infomation returned by os.stat().

Anybody give me a hint?
--
http://mail.python.org/mailman/listinfo/python-list


Re: notification for cd insertion

2004-12-04 Thread Qiangning Hong
Qiangning Hong wrote:
I want one of my function to execute when a cdrom is inserted.  How can 
I achieve that?

Further more, I want to do different things depend on the inserted disc 
type: if it is a normal cd-rom, read from it; if it is a recordable cd, 
write data on it.  So, how can I get the inserted disc type infomation 
in Python code?

thanks in advance.
Sorry I forgot the important info:  my platform is a Windows 2000 box.
--
http://mail.python.org/mailman/listinfo/python-list


notification for cd insertion

2004-12-04 Thread Qiangning Hong
I want one of my function to execute when a cdrom is inserted.  How can 
I achieve that?

Further more, I want to do different things depend on the inserted disc 
type: if it is a normal cd-rom, read from it; if it is a recordable cd, 
write data on it.  So, how can I get the inserted disc type infomation 
in Python code?

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