Re: A difficulty with lists

2012-08-20 Thread Cheng
On Monday, August 6, 2012 12:50:13 PM UTC-7, Mok-Kong Shen wrote:
> I ran the following code:
> 
> 
> 
> def xx(nlist):
> 
>print("begin: ",nlist)
> 
>nlist+=[999]
> 
>print("middle:",nlist)
> 
>nlist=nlist[:-1]
> 
>print("final: ",nlist)
> 
> 
> 
> u=[1,2,3,4]
> 
> print(u)
> 
> xx(u)
> 
> print(u)
> 
> 
> 
> and obtained the following result:
> 
> 
> 
> [1, 2, 3, 4]
> 
> begin:  [1, 2, 3, 4]
> 
> middle: [1, 2, 3, 4, 999]
> 
> final:  [1, 2, 3, 4]
> 
> [1, 2, 3, 4, 999]
> 
> 
> 
> As beginner I couldn't understand why the last line wasn't [1, 2, 3, 4].
> 
> Could someone kindly help?
> 
> 
> 
> M. K. Shen

When you pass a list (mutable object) to a function, the pointer to the list is 
passed to the function and the corresponding argument points to the same memory 
location as the pointer passed in. So in this case, nlist points to the same 
memory location which u points to when xx is called,  i.e. nlist and u points 
to same memory location which contains [1,2,3,4].

nlist += [999]  is equivalent to nlist.extend([999]). This statement adds the 
argument list to the original list, i.e. the memory location pointed by nlist  
and u now contains [1,2,3,4,999]. So, print(u) after calling xx will print 
[1,2,3,4,999].

 nlist += [999] is not the same as nlist = nlist + [999]. In the later case, 
nlist + [999] will create a new memory location containing the two lists 
combined and rebind nlist to the new location, i.e. nlist  points to a new 
memory location that has [1,2,3,4,999]. So if nlist = nlist +[999] is used, the 
original memory location containing [1,2,3,4] is untouched, and print(u) after 
calling xx will print [1,2,3,4]
-- 
http://mail.python.org/mailman/listinfo/python-list


a re problem

2005-05-20 Thread cheng
>>> p.sub('','a\nbc')
'abc'
>>> p.sub('','%s') % "a\nbc"
'a\nbc'

is it anyone got some idea why it happen?

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


Re: a re problem

2005-05-20 Thread cheng
thx for help :)

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


a html parse problem

2005-05-27 Thread cheng
hi,all

if the html like:
 
 

if i use:
def handle_starttag(self, tag, attrs):
if tag == 'meta':
   self.attr = attrs
self.headers += ['%s' % (self.attr)]
self.attr = ''

will get the output:
[('name', 'description'), ('content', 'a test page')]

[('name', 'keywords'), ('content', 'keyword1 keyword2')]

is it some way that only take the content like " a test page, keyword1
, keywork2"

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


a re problem

2005-05-28 Thread cheng
hi,all, i try to replace every target word found in the text

 for target in splitText:
if stopwords.find(target) >= 0 :
text = re.sub(r'\b%s\b','',text) &target

when i using the statment:

text = re.sub(r'\b%s\b','',text) &target

 get error : unsupported operand type(s) for &: 'str' and 'str'

is it some idea that can modity it and make it work?

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


a dict problem

2005-05-28 Thread cheng
hi all..it a problem about dict:

print target, dict[target]

get output:

keyword
{page3.html, page2.html, page1.html}

is it some ways to change it to:

keyword
{page1.html, page2.html, page3.html}

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


need help of RE

2005-05-29 Thread cheng
hi all
a string like

"(word1 & (Word2|woRd3))"

how can i use the re to split it to

['word1', 'word2', 'word3']

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


Re: need help of RE

2005-05-29 Thread cheng
im sorry, my engilsh is not vell well,

the string not only contain '&' and '|' and it can be anyting

i just want to split out the "whole word" inside the string

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


Re: need help of RE

2005-05-29 Thread cheng
i try

query = query.lower()
print re.split(r'\W+',theString)

the reslut is :
['', 'word1', 'word2', 'word3', '']

how can i fix the statment to get 

['word1', 'word2', 'word3']

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


Re: need help of RE

2005-05-29 Thread cheng
i try

theString= theString.lower()
print re.split(r'\W+',theString)

the reslut is :
['', 'word1', 'word2', 'word3', '']

how can i fix the statment to get 

['word1', 'word2', 'word3']

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


Re: need help of RE

2005-05-29 Thread cheng
thx for help..i got it now :)

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


when to use import statements in the header, when to use import statements in the blocks where they are used?

2012-02-07 Thread Lei Cheng
Hi all,

   In a py file, when to use import statements in the header, when to use
import statements in the blocks where they are used?
   What are the best practices?
   Thanks!

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


Re:Re: Where can I find a lexical spec of python?

2011-09-21 Thread Shaofei Cheng
Yes, I'm using this document now but I was wondering if there is a formal spec 
for lexical grammar?  It looks like some part of the doc 
"http://docs.python.org/py3k/reference/grammar.html";  is missing. 
We can find some replacement in lexical_analysis.html but it seems this 
document is write for a python user instead of a guy trying to implement python.
在 2011-09-22 00:55:45,"Thomas Jollans"  写道:
>On 21/09/11 18:33, 程劭非 wrote:
>> Thanks Thomas.
>> I've read the document 
>> http://docs.python.org/py3k/reference/lexical_analysis.html 
>> 
>> but I worried it might leak some language features like "tab magic".
>> 
>> For I'm working on a parser with JavaScript I need a more strictly defined 
>> spec. 
>> 
>> Currently I have a highlighter here 
>> ->http://shaofei.name/python/PyHighlighter.html
>> (Also the lexer  http://shaofei.name/python/PyLexer.html)
>> 
>> As you can see, I just make its behavior align with CPython, but I'm not 
>> sure what the real python lexical grammar is like.
>> 
>> Does anyone know if there is a lexical grammar spec like other 
>> languages(e.g. http://bclary.com/2004/11/07/#annex-a)?
>
>I believe the language documentation on docs.python.org is all the
>documentation of the language there is. It may not be completely formal,
>and in parts it concentrates not on the actual rules but on the original
>implementation, but, as far as I can tell, it tells you everything you
>need to know to write a new parser for the Python language, without any
>ambiguity.
>
>You appear to be anxious about implementing the indentation mechanism
>correctly. The language documentation describes a behaviour precisely.
>What is the problem?
>
>Thomas
>
>> 
>> Please help me. Thanks a lot.
>> 在 2011-09-21 19:41:33,"Thomas Jollans"  写道:
>>> On 21/09/11 11:44, 程劭非 wrote:
 Hi, everyone, 
 I've found there was several tokens used in python's
 grammar(http://docs.python.org/reference/grammar.html) but I didn't see
 their definition anywhere.  The tokens listed here: 
>>>
>>> They should be documented in
>>> http://docs.python.org/py3k/reference/lexical_analysis.html - though
>>> apparently not using these exact terms.
>>>
 NEWLINE
>>> Trivial: U+000A
>>>
 ENDMARKER
>>> End of file.
>>>
 NAME
>>> documented as "identifier" in 2.3
>>>
 INDENT
 DEDENT
>>> Documented in 2.1.8.
>>>
 NUMBER
>>> Documented in 2.4.3 - 2.4.6
>>>
 STRING
>>> Documented in 2.4.2
>>>
 I've got some infomations from the source
 code(http://svn.python.org/projects/python/trunk/Parser/tokenizer.c) but
 I'm not sure which feature is only for this specified implementaion.  (I
 saw tabstop could be modified with comments using "tab-width:",
 ":tabstop=", ":ts=" or "set tabsize=", is this feature really in spec?)
>>>
>>> That sounds like a legacy feature that is no longer used. Somebody
>>> familiar with the early history of Python might be able to shed more
>>> light on the situation. It is inconsisten with the spec (section 2.1.8):
>>>
>>> """
>>> Indentation is rejected as inconsistent if a source file mixes tabs and
>>> spaces in a way that makes the meaning dependent on the worth of a tab
>>> in spaces; a TabError is raised in that case.
>>> """
>>>
>>> - Thomas
>>> -- 
>>> http://mail.python.org/mailman/listinfo/python-list
>> 
>

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


Getting Python to run Python Scripts in cygwin

2008-06-19 Thread Calvin Cheng
Hi guys,

This may be a cygwin issue but I was hoping to get some answers here
as well if someone has fixed this problem before.

Basically, I am able to run "python .py" python files in
command prompt.  Unfortunately, I can't seem to get it to work in
cygwin.  I always get an error that says:
python: can't open file '.py': [Errno 2] No such file or directory

I have looked at my environment variables and it seems to be correct.

Would be grateful if someone who has gotten python to work correctly
on cygwin could point me in the right direction.

It is very cumbersome switching back-and-forth between command prompt
and cygwin just to run python scripts.

Thank you in advance,
Calvin
--
http://mail.python.org/mailman/listinfo/python-list


Docutils rst2html.py gives Unknown Directive type "toctree"

2008-06-19 Thread Calvin Cheng
Hi,

I am attempting to convert a bunch of .txt files into html using the
docutils package.

It works for most of the txt files except for the index.txt file which
gives 2 errors:
(1)  Unknown Directive type "toctree"
(2) (ERROR/3) Unknown interpreted text role "ref".

Any idea how I can fix this?

Would be glad if someone who is familiar with docutils could point me
in the right direction.

Thank you,
Calvin
--
http://mail.python.org/mailman/listinfo/python-list


Re: Web forum (made by python)

2004-12-19 Thread Choe, Cheng-Dae
try http://kldp.net/projects/pybb/

example site is http://bbs.pythonworld.net:9080/pybbs.py


On Sun, 19 Dec 2004 22:09:52 +0200, Gezer Punta <[EMAIL PROTECTED]> wrote:
> hi all
> I am looking for a forum which was produced by python
> 
> link pls
> --
> http://mail.python.org/mailman/listinfo/python-list
> 


-- 
Choe, Cheng-Dae(ììë)
Blog: http://www.comdongin.com/
--
http://mail.python.org/mailman/listinfo/python-list


Unable to decode file written by C++ wostringstream

2010-12-23 Thread Yan Cheng CHEOK
Currently, I have the following text file 
(https://sites.google.com/site/yanchengcheok/Home/TEST.TXT?attredirects=0&d=1) 
written by C++ wostringstream.

What I want to do it, I want to write a python script which accept user browser 
request, and then send over the entire file for user to download. The 
downloaded file, should be exactly same as the original text file inside server 
itself.

The code is written as follow :

import cgi

print "Content-Type: text/plain"
print "Content-Disposition: attachment; filename=TEST.txt"
print

filename = "C:\\TEST.TXT"
f = open(filename, 'r')
for line in f:
print line

However, when I open up the downloaded file, the file is all having weird 
characters. I try to use rb flag, it doesn't either.

Is there anything I had missed out? What I wish is, the file (TEST.TXT) 
downloaded by the client by making query to the above script, will be exactly 
same as the one in server.

I also try to specific the encoding explicitly.

import cgi

print "Content-Type: text/plain; charset=UTF-16"
print "Content-Disposition: attachment; filename=TEST.txt"
print

filename = "C:\\TEST.TXT"
f = open(filename, 'r')
for line in f:
print line.encode('utf-16')

It doesn't work either. Here is the screen shoot for original text file 
(http://i.imgur.com/S6SjX.png) and file after downloaded from a web browser. 
(http://i.imgur.com/l39Lc.png)

Is there anything I had missed out?

Thanks and Regards
Yan Cheng CHEOK


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


automatic multiprocessing

2009-07-07 Thread Cheng Soon Ong

Hi all,

I'm trying to automate the use of multiprocessing when it is available. The 
setting I have is quite simple, with a for loop where the operations inside are 
independent of each other. Here's a bit of code. function_inputs is a list of 
dictionaries, each of which match the signature of function_handle.


if multiprocessing_present:
# Passing keyword arguments to map still doesn't work
cpus = multiprocessing.Pool()
function_outputs = cpus.map(function_handle, function_inputs)
else:
function_outputs = []
for kwargs in function_inputs:
cur_out = function_handle(**kwargs)
function_outputs.append(cur_out)

Am I missing something here? I cannot seem to get map to pass on keyword 
arguments.

Thanks in advance,
Cheng Soon
--
http://mail.python.org/mailman/listinfo/python-list


Bit fields in python?

2010-09-06 Thread Kwan Lai Cheng
Hi,

I'm trying to rewrite a c program in python & encountered several problems. I 
have some data structures in my c program like below:

typedef struct
{
unsigned short size;

unsigned short reserved:8;
unsigned short var_a1:2;
unsigned short var_a2:2;
unsigned short var_a3:2;
unsigned short var_a4:2;

unsigned int var_a5;
}structa;

 typedef struct
{
unsigned short size;

unsigned char reserved:4;
unsigned char var_b1:1;
unsigned char var_b2:1;
unsigned char var_b3:1;
unsigned char var_b4:1;

structa var_structa;
}structb;

I tried to code the above in python but only got this far:

class StructA(object):
def __init__(self, size=0)
self.size = size

class StructB(object):
def __init__(self, size=0)

Any equivalent for c data structures & bit fields in python? And how do I 
define var_structa (in structb) in python?

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


Re: Bit fields in python?

2010-09-07 Thread Kwan Lai Cheng
- Original Message - 
From: Stefan Behnel

To: 
Sent: Tuesday, September 07, 2010 2:55 PM
Subject: Re: Bit fields in python?




If you can tell us what these structs are being used for in the original C
code, we might be able to point you to a suitable way to implement the 
same

thing efficiently in Python.

Stefan

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



It's a test program for testing the DSP (Digital Signal Processor). 
Basically what it does is that it sends commands to the DSP via a driver and 
waits for a response from the DSP. The structures represent the data that I 
might receive from the DSP. For example if I send command A, DSP will send 
data in the format of structa to me, so I will cast the data I received to 
structa, and print the struct field values on screen. Each field in the 
structures represent some status or info about the DSP, different bits in an 
unsigned char or unsigned short field might represent different DSP status.


I just found out that there's a ctypes module 
(http://docs.python.org/library/ctypes.html) in python that might solve my 
problem. I understand that it is already included in Python 2.5 & I'm using 
Python 2.6.2. However when I tried "from ctypes import *", I got "No module 
named _ctypes". What am I missing here?



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


Re: Bit fields in python?

2010-09-08 Thread Kwan Lai Cheng
Thanks everyone for all the suggestions! New to python, so reading up on the 
struct module & Construct library now, hope to get my problem fixed soon.
  - Original Message - 

  Bitfields are most commonly used for extreme space optimization - i.e. 
shoving several variables and flags with carefully limited ranges into a single 
work. In Python you rarely work this way (where such an optimization is 
warranted, Python isn't the best tool for the job). However, as in your use 
case, it is sometimes needed in Python in order to communicate with other 
devices over the network or some other link.

  In my work with Python and embedded devices I've found the construct library 
(http://construct.wikispaces.com/) very useful. It allows to you very easily 
define complex formats for frames/messages on the bit and byte level. The idea 
is to use construct to encode and decode messages being sent to an embedded 
device. It works great.

  If you have further questions about this approach, feel free to ask.

  Eli







--


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