Re: pickling or xml or other?

2006-08-20 Thread Bo
You could use a python file and use the builtin import facility to read
in configurations.

from http://dirtsimple.org/2004/12/python-is-not-java.html
   ... in Python, more often than not, code is easier to write
   than XML. And Python can process code much, much
   faster than your code can process XML. (Not only that,
   but you have to write the XML processing code, whereas
   Python itself is already written for you.)

placid wrote:
> Hi all,
>
> I have an application were i want the user to "configure" some settings
> which are variables within different classes.
>
> My question is should i just pickle out these variables to a file in
> some particular order then read it back. Or use xml to save the config
> values ? Which one scales better if settings increase?
> 
> Other sugestions? 
> 
> -Cheers

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


step through .py file in a .NET application written in C# in Visual Studio

2007-08-06 Thread Bo
Hello All,

I am experimenting IronPython in Visual Studio.  Here is what I have,
what I did and what I want

1.  I have installed Visual Studio SDK 4.0 according to this blog
http://blogs.msdn.com/aaronmar/archive/2006/02/16/a-bit-more-on-ironpython.aspx

If I run Visual Studio under Experimental Hive, a Python file opened
is color coded.

2.  Also, my computer has apparently installed some IronPython DLL
because my Visual Studio can recoginize the following class:
IronPython.Hosting.PythonEngine.

If I declare something like the following in Visual Studio:

public static class MyPythonClass {
   private static IronPython.Hosting.PythonEngine   MY_ENGINE;
  //suppose I have created an instance of PhythonEngine and assigned
to MY_ENGINE
   
}

If I do "Go to Definition" of PythonEngine, I can navigate to the
Definition of PythonEngine, but it is from MetaData.

3.  In my .NET application (written in C#) , I can invoke the
execution of an external python file using something like:
MyPythonClass.MY_ENGINE.ExecuteFile("myPythonFileWithAbsolutePath.py")

The python file (myPythonFileWithAbsolutePath.py) can be executed all
right, but I want to step through it to debug it.  Any suggestions on
how to do it?

Thanks a bunch.

Bo

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


How to port Python code into C++ code automatically?

2008-03-12 Thread Bo
I want to port a Python project (about 10,000 line python code) to C+
+. Is there any automatically tool to do this kind of things? e.g.,
SWIG(http://www.swig.org/)?

Any comment is welcome!

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


Installing Python 2.7 for all users on SLES 11

2016-08-04 Thread Bo Stone
I installed Python 2.7 on SLES 11 box that previously was running Python 2.6. 
To do so I used a script described in this post 
(http://stackoverflow.com/a/11371726/135946) and run it as a root user. 
Everything went well but when it was done I discovered few issues:

1. No symbolic links were created and no path updated so I had to manually 
update the path to link to the new installation bin directory /opt/python2.7/bin
2. Everything runs good until I switch from root to the normal user at which 
point Python shell runs but some modules I installed such as PyYAML are 
missing. Again, these are OK when I run Python as root
3. As a regular user I'm not able to run pip, easy_install and wheel. For pip I 
get "ImportError: No module named pkg_resources"

This question is also posted to Stackoverflow here: 
http://stackoverflow.com/q/38749809/135946

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


Re: Installing Python 2.7 for all users on SLES 11

2016-08-05 Thread Bo Stone
On Thursday, August 4, 2016 at 8:54:35 PM UTC-7, Bo Stone wrote:
> I installed Python 2.7 on SLES 11 box that previously was running Python 2.6. 
> To do so I used a script described in this post 
> (http://stackoverflow.com/a/11371726/135946) and run it as a root user. 
> Everything went well but when it was done I discovered few issues:
> 
> 1. No symbolic links were created and no path updated so I had to manually 
> update the path to link to the new installation bin directory 
> /opt/python2.7/bin
> 2. Everything runs good until I switch from root to the normal user at which 
> point Python shell runs but some modules I installed such as PyYAML are 
> missing. Again, these are OK when I run Python as root
> 3. As a regular user I'm not able to run pip, easy_install and wheel. For pip 
> I get "ImportError: No module named pkg_resources"
> 
> This question is also posted to Stackoverflow here: 
> http://stackoverflow.com/q/38749809/135946

The Stackoverflow question has now 100 points bounty
-- 
https://mail.python.org/mailman/listinfo/python-list


Re:

2019-05-29 Thread Bo YU
On Wed, May 29, 2019 at 1:19 PM Sri Tharun  wrote:

> Why I am unable to install packages
>
If you really want to get help from the list,please give us more info.

> --
> https://mail.python.org/mailman/listinfo/python-list
>
-- 
https://mail.python.org/mailman/listinfo/python-list


unittest.assertRaise and keyword arguments?

2005-12-02 Thread Bo Peng
Dear list,

The syntax for using assertRaise is

   assertRaise(exception, function, para1, para2,...)

However, I have a long list of arguments (>20) so I would like to test 
some of them using keyword arguments (use default for others). Is there 
a way to do this except for manually try...except?

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


Re: unittest.assertRaise and keyword arguments?

2005-12-02 Thread Bo Peng
Giovanni Bajo wrote:
> You can pass keyword arguments to assertRaises without problems:
> 
> self.assertRaises(ValueError, myfunc, arg1,arg2, arg3, arg4, abc=0, foo=1,
> bar="hello")

Well, I though abc=0 would be keyword arguments for assertRaisers and 
never tried it!

> 
> Or you can always do something like:
> 
> self.assertRaises(ValueError, lambda: myfunc(arg1,arg2, arg3, arg4, abc=0,
> foo=1, bar="hello"))

This is also useful.

I now have nothing to complain against assertTaises. :-)

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


exposing C array to python namespace: NumPy and array module.

2004-12-31 Thread Bo Peng
Dear list,
I am writing a Python extension module that needs a way to expose pieces 
of a big C array to python. Currently, I am using NumPy like the following:

 PyObject* res = PyArray_FromDimsAndData(1, int*dim, PyArray_DOUBLE, 
char*buf);

Users will get a Numeric Array object and can change its values (and 
actually change the underlying C array).

This works fine. However, when I deliver my module, I find NumPy is 
unnecessarily large for this simple task. As a matter of fact, I had to 
 build from source NumPy, ATLAS etc on Solaris, Linux, Mac and if a 
user would like to use my module, he has to do the same thing!

Python's array module is built-in, easy to use, but *without* a 
FromLenAndData function! Even the buffer interface provides only 'get 
buffer' but no 'set buffer' functions. Could anyone tell me how I can 
create an array object from existing data? Some vague ideas might be 
used: 1. PyCObject (I do not really understand the manual), 2. copy and 
modify arraymodule.c to my project (doable at all? License issue?) 3. 
Create an array object and hack it. (no api to do this.)

I would strongly suggest an arraymodule.h with Array_FromLenAndData.
Many thanks in advance.
Bo
--
http://mail.python.org/mailman/listinfo/python-list


Re: exposing C array to python namespace: NumPy and array module.

2005-01-01 Thread Bo Peng
Craig Ringer wrote:
On Sat, 2005-01-01 at 08:18, Bo Peng wrote:

Python's array module is built-in, easy to use, but *without* a 
FromLenAndData function! Even the buffer interface provides only 'get 
buffer' but no 'set buffer' functions. Could anyone tell me how I can 
create an array object from existing data?

Python has no array objects in the core language, only lists. The
distinction is important when discussing numarray etc, because Python
lists and NumPy etc arrays are very different.
Thank you very much for the detailed reply!
Sorry if I was not clear enough. I was talking about the differece 
between python array module 
(http://docs.python.org/lib/module-array.html, Modules/arraymodule.c in 
the source tree) and NumPy array. They both use C-style memory block 
arrangement for efficient memory access. While NumPy has both, the array 
module is designed to be used purely in Python so there is no header 
file and no function to build an array from a pointer.

One of the methods you suggested (creating a new type) already 
implemented in arraymodule.c. I am not sure if it is appropriate to add 
the file into my project and add a 'CreateFromLenAndBuf' function.

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


Re: exposing C array to python namespace: NumPy and array module.

2005-01-01 Thread Bo Peng
Scott David Daniels wrote:
Python's array module is not built to do this well.  It can re-size the
array, delete elements inside the array, and other things that don't
work very well with C-managed data.  I wrote "blocks and views" to
overcome this problem.  
As always the case, this problem have been encountered and solved! Your 
code is exactly what I needed. However, I was too impatient to wait for 
your reply. :-) I have realized the problems of arraymodule.c and added 
a ob_ownmem member in the object structure. Existing operations have 
been modified (mostly unchanged) according to this flag and the new 
module works well.

Thank everyone for the help. especially in thie new year's day.
Bo
--
http://mail.python.org/mailman/listinfo/python-list


compiler version mismatch when building ext_modules with MSVC

2005-01-09 Thread Bo Peng
Dear list,
I was using autoconf/automake etc to build my python extension. Since 
distutil seems to be easier, I have switched to this method. However, 
when I build the module under windows, I am told that compiler version 
mismatch... I am using msvc 7 and python was build by msvc 6.0.

I am not sure why compiler version matters here since
1. if proceed by removing corresponding lines in distutil, the generated 
module works fine (as far as I can tell).

2. I am told that I can use minGW gcc ... Isn't mingw differ more with 
msvc6 than msvc7?

3. I was using msvc7 (automake etc, not distutil) and everything was 
fine. I was not even warned about potential problems.

Since there are many different Python out there (Official, ActivePython, 
Enthough), do I have to figure out which compiler they use to build my 
module for them? Currently, my MSVC7 built module works fine with all of 
them.

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


Can't load bytecode with execfile ?

2005-01-25 Thread Bo Jacobsen
I have a number of files compiled to bytecode using py_compile.compile().
The .pyc files can be invoked by python directly ($python file.pyc), but
"loading" them by execfile just throws an exception ?

Any suggestions

Bo.


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


mixing SWIG generated and Python-level usertype?

2005-02-01 Thread Bo Peng
Dear list,
My SWIG generated module (myModule) needs an array-like object (carray) 
to work. Carray objects are created both internally (in C++ level) and 
through Python so I have to load it when myModule initializes.

carray is modified from arraymodule.c and is quite simple:
static PyMethodDef a_methods[] =
{
  {"carray", a_array, METH_VARARGS, a_array_doc},
  {   /* sentinel */
NULL,NULL
  }
};
Currently, I load a_methods directly using code (error checking ignored)
  PyObject* mm = PyImport_AddModule("__main__");
  PyObject* dict = PyModule_GetDict(mm);
  PyObject*  v = PyCFunction_New(a_methods, NULL);
  PyDict_SetItemString(dict, a_methods->ml_name, v);
There are several problems with this approach:
1. use of __main__? carray can not be accessed directly within other 
libraries. ('from myModule import *' DOES NOT import carray!) I tried to 
use __builtins__ but it does not work for some reason out of my 
understanding of Python. I am not sure how to add carray to myModule 
dictionary.

2. No type object? I am not sure what is the purpose of ArrayType but 
the usual init_module should be
m = Py_InitModule3("carray", a_methods, module_doc);
d = PyModule_GetDict(m);
PyDict_SetItemString(dict, "ArrayType", (PyObject *)&Arraytype);

When I add ArrayType to __main__ , access to ArrayType leads to a quick 
core dump.

I do not feel comfortable with my current approach. Could anyone tell me 
some better (more standard) way?

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


changing local namespace of a function

2005-02-04 Thread Bo Peng
Dear list,
I have many dictionaries with the same set of keys and I would like to 
write a function to calculate something based on these values. For 
example, I have

a = {'x':1, 'y':2}
b = {'x':3, 'y':3}
def fun(dict):
  dict['z'] = dict['x'] + dict['y']
fun(a) and fun(b) will set z in each dictionary as the sum of x and y.
My function and dictionaries are a lot more complicated than these so I 
would like to set dict as the default namespace of fun. Is this 
possible? The ideal code would be:

def fun(dict):
  # set dict as local namespace
  # locals() = dict?
  z = x + y
Many thanks in advance.
Bo
--
http://mail.python.org/mailman/listinfo/python-list


Re: changing local namespace of a function

2005-02-04 Thread Bo Peng
M.E.Farmer wrote:
def fun(d):
... __dict__ = d
... return __dict__
hth,

Does not work?
>>> a = { 'x':1, 'y':2}
>>> b = { 'x':2, 'y':9}
>>> def fun(d):
...   __dict__ = d
...   print locals()
...   z = x + y
>>> fun(a)
{'__dict__': {'y': 2, 'x': 1}, 'd': {'y': 2, 'x': 1}}
Traceback (most recent call last):
  File "", line 1, in ?
  File "/usr/tmp/python-6377tax.py", line 4, in fun
NameError: global name 'x' is not defined
--
http://mail.python.org/mailman/listinfo/python-list


Re: changing local namespace of a function

2005-02-04 Thread Bo Peng
Michael Spencer wrote:
As you no doubt have discovered from the docs and this group, that isn't 
doable with CPython.
Too bad to know this.
 >>> a = {'x':1, 'y':2}
 >>> b = {'x':3, 'y':3}
 ...
 >>> def funa(x,y, **kw):
 ... del kw #Careful of unwanted names in locals with this approach
 ... z = x + y
 ... return locals()
There are hundreds of items in the dictionary (that will be needed in 
the calculation) so passing the whole dictionary is a lot better than 
passing individual items.

Alternatively, you could use exec:
Yes. I thought of using exec or eval. If there are a dozen statements,
def fun(d):
  exec 'z = x + y' in globals(), d
seems to be more readable than
def fun(d):
  d['z'] = d['x'] + d['y']
But how severe will the performance penalty be?
Bo
--
http://mail.python.org/mailman/listinfo/python-list


Re: changing local namespace of a function

2005-02-04 Thread Bo Peng
Jeff Shannon wrote:
This sounds to me like you're trying to re-implement object orientation.
I have no control over the big dictionaries. All I need to do is 
processing them in situ --- that is to say, go into each map and 
manipulate numbers. Parameter passing should be avoid whenever possible 
since involved number of items are huge.

It is basically like
  exec 'statements' in d
but a function with local dictionary d would be best.
Bo
--
http://mail.python.org/mailman/listinfo/python-list


Re: changing local namespace of a function

2005-02-04 Thread Bo Peng
Thank all for your suggestions. I have tried all methods and compared 
their performance.

>>> import profile
>>> a = {'x':1, 'y':2}
>>> N = 10
>>> # solution one: use dictionary directly
... def fun1(d):
...   for i in xrange(0,N):
... d['z'] = d['x'] + d['y']
...
>>> # solution two: use exec
... def fun2(d):
...   for i in xrange(0,N):
... exec 'z = x + y' in globals(), d
...
>>> # solution three: update local dictionary
... # Note that locals() is *not* d after update() so
... #   z = x + y
... # does not set z in d
... def fun3(d):
...   exec "locals().update(d)"
...   for i in xrange(0,N):
... d['z'] = x + y
...
>>> # solution four: use dict wrapper
... # this makes code easier to write and read
... class wrapdict(object):
...   """Lazy attribute access to dictionary keys.  Will not access
...  keys that are not valid attribute names!"""
...   def __init__(self, mydict):
... object.__setattr__(self, "mydict",mydict)
...   def __getattr__(self, attrname):
... return self.mydict[attrname]
...   def __setattr__(self, attrname, value):
... self.mydict[attrname] = value
...
>>> # use wrapper
... def fun4(d):
...   wd = wrapdict(d)
...   for i in xrange(0,N):
... wd.z = wd.x + wd.y
...
>>> profile.run('fun1(a)')
 3 function calls in 0.070 CPU seconds
   Ordered by: standard name
   ncalls  tottime  percall  cumtime  percall filename:lineno(function)
10.0000.0000.0600.060 :1(?)
10.0100.0100.0700.070 profile:0(fun1(a))
00.000 0.000  profile:0(profiler)
10.0600.0600.0600.060 python-4645vcY.py:2(fun1)
>>> profile.run('fun2(a)')
 13 function calls (3 primitive calls) in 5.890 CPU seconds
   Ordered by: standard name
   ncalls  tottime  percall  cumtime  percall filename:lineno(function)
 11/10.4400.0005.8905.890 :1(?)
10.0000.0005.8905.890 profile:0(fun2(a))
00.000 0.000  profile:0(profiler)
15.4505.4505.8905.890 python-46458me.py:2(fun2)
>>> profile.run('fun3(a)')
 4 function calls (3 primitive calls) in 0.060 CPU seconds
   Ordered by: standard name
   ncalls  tottime  percall  cumtime  percall filename:lineno(function)
  2/10.0000.0000.0600.060 :1(?)
10.0000.0000.0600.060 profile:0(fun3(a))
00.000 0.000  profile:0(profiler)
10.0600.0600.0600.060 python-4645Jxk.py:5(fun3)
>>> profile.run('fun4(a)')
 34 function calls in 3.910 CPU seconds
   Ordered by: standard name
   ncalls  tottime  percall  cumtime  percall filename:lineno(function)
10.0000.0003.9103.910 :1(?)
10.0000.0003.9103.910 profile:0(fun4(a))
00.000 0.000  profile:0(profiler)
   100.5300.0000.5300.000 
python-4645W7q.py:10(__setattr__)
10.0000.0000.0000.000 python-4645W7q.py:6(__init__)
   200.9600.0000.9600.000 
python-4645W7q.py:8(__getattr__)
12.4202.4203.9103.910 python-4645jFx.py:1(fun4)

Exec is slow since compiling the string and calls to globals() use a lot 
of time.  The last one is most elegant but __getattr__ and __setattr__ 
are costly. The 'evil hack' solution is good since accessing x and y 
takes no additional time.

I guess I will go with solution 3. It is evil but it is most close to my 
original intention. It leads to most readable code (except for the first 
line to do the magic and the last line to return result) and fastest 
performance.

Thank again for everyone's help. I have learned a lot from the posts, 
especially the wrapdict class.

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


Re: changing local namespace of a function

2005-02-04 Thread Bo Peng
M.E.Farmer wrote:
 I really don't see your need.
Maybe it is just my laziness. It is almost intolerable for me to write 
lines and lines of code like

  d['z'] = func(d['x']+d['y']+d['whatever']['as']+d[a][0] )
It is ugly, unreadable and error prone. If I have to use this code, I 
would write

  _z = func(_x + _y + _whatever['as'] + _a[0])
and use a perl script to generate the real code. (See, I am not lazy :-)
Bo
--
http://mail.python.org/mailman/listinfo/python-list


Re: changing local namespace of a function

2005-02-04 Thread Bo Peng
Maybe it is just my laziness. It is almost intolerable for me to write 
lines and lines of code like

  d['z'] = func(d['x']+d['y']+d['whatever']['as']+d[a][0] )
By the way, will 'with statement', like the one in pascal and many other 
languages, be a good addition to python? For example,

with d do:
  z = x + y
would be equivalent to d['z']=d['x']+d['y'] or d.z = d.x + d.y in some 
other cases.

This would absolutely be the *best* solution to my problem.
Bo
--
http://mail.python.org/mailman/listinfo/python-list


Re: changing local namespace of a function

2005-02-05 Thread Bo Peng
Nick Coghlan wrote:
If you want to add more calculated properties to the data manipulator, 
simply define additional calculator methods, and define the attribute 
with make_prop.
This has became really appealing
You know, I have a deep root in C/C++ so performance is the king and 
hacking is part of my daily life. Time to change now. :)

My original problem was that I need to manipulate and create dictionary 
items in situ and since there are so many "d['key']"s, I would like to 
make my code easier to read (and save some key strokes) by using "d.key" 
or simply "key", without sacrificing performance. OOP seemed to be 
irrelevant.

But this on-demand calculation (quote: make first call to z automatic) 
is *really* good! I can calculate self._data.stat_a = self._data.stat_b 
+ 1 without worrying about the status of stat_b and users can access any 
  value in the dictionary, original or calculated, in a uniform and 
easier way! This makes my program instantly easier to use.

I can not say enough thank you for this.
Bo
--
http://mail.python.org/mailman/listinfo/python-list


Re: changing local namespace of a function

2005-02-05 Thread Bo Peng
Kent Johnson wrote:
You can part way there using keyword arguments. You just have to use 
dictionary syntax for changing values in the dictionary:

 >>> def f(d, x=None, y=None):
 ...   d['z'] = x + y
 ...
 >>> a = {'x':1, 'y':2}
 >>> b = {'x':3, 'y':3}
 >>>
 >>> f(a, **a)
 >>> a
{'y': 2, 'x': 1, 'z': 3}
 >>> f(b, **b)
 >>> b
{'y': 3, 'x': 3, 'z': 6}
This is not possible in my case since my dictionary have many more items 
than just x and y. So, if there is are other items in the dictionary

>>> a = {'x':1, 'y':2, 'else':4}
>>> f(a,**a)
Traceback (most recent call last):
  File "", line 1, in ?
  File "/usr/tmp/python-10176RtT.py", line 1, in ?
f(a,**a)
TypeError: f() got an unexpected keyword argument 'else'
Bo
--
http://mail.python.org/mailman/listinfo/python-list


Re: changing local namespace of a function

2005-02-05 Thread Bo Peng
Exec is slow since compiling the string and calls to globals() use a lot 
of time.  The last one is most elegant but __getattr__ and __setattr__ 
are costly. The 'evil hack' solution is good since accessing x and y 
takes no additional time.
Previous comparison was not completely fair since I could pre-compile 
fun2 and I used indirect __setattr__. Here is the new one:

>>> import profile
>>> a = {'x':1, 'y':2}
>>> N = 10
>>> # solution one: use dictionary directly
... def fun1(d):
...   for i in xrange(0,N):
... d['z'] = d['x'] + d['y']
...
>>> # solution two: use exec
... def makeFunction(funcStr, name):
...   code = compile(funcStr, name, 'exec')
...   def f(d):
... exec code in d
...   return f
...
>>> def fun2(d):
...   myfun = makeFunction('z = x + y', 'myfun')
...   for i in xrange(0,N):
... myfun(d)
...   del d['__builtins__']
...
... # solution three: update local dictionary
>>> # Note that locals() is NOT d after update() so
... #   z = x + y
... # does not set z in d
... def fun3(d):
...   exec "locals().update(d)"
...   for i in xrange(0,N):
... d['z'] = x + y
...
>>> # solution four: use dict wrapper
... # this makes code easier to write and read
... class wrapdict(object):
...   """Lazy attribute access to dictionary keys.  Will not access
...  keys that are not valid attribute names!"""
...   def __init__(self, mydict):
... self.__dict__ = mydict
...
... # use wrapper
>>> def fun4(d):
...   wd = wrapdict(d)
...   for i in xrange(0,N):
... wd.z = wd.x + wd.y
...
>>> profile.run('fun1(a)')
 3 function calls in 0.060 CPU seconds
   Ordered by: standard name
   ncalls  tottime  percall  cumtime  percall filename:lineno(function)
10.0000.0000.0600.060 :1(?)
10.0000.0000.0600.060 profile:0(fun1(a))
00.000 0.000  profile:0(profiler)
10.0600.0600.0600.060 python-10176FWs.py:2(fun1)
>>> profile.run('fun2(a)')
 24 function calls in 2.130 CPU seconds
   Ordered by: standard name
   ncalls  tottime  percall  cumtime  percall filename:lineno(function)
10.0000.0002.1302.130 :1(?)
   100.5200.0000.5200.000 myfun:1(?)
10.0000.0002.1302.130 profile:0(fun2(a))
00.000 0.000  profile:0(profiler)
10.5900.5902.1302.130 python-10176EqB.py:1(fun2)
10.0000.0000.0000.000 
python-10176Sgy.py:2(makeFunction)
   101.0200.0001.5400.000 python-10176Sgy.py:4(f)

>>> profile.run('fun3(a)')
 4 function calls (3 primitive calls) in 0.070 CPU seconds
   Ordered by: standard name
   ncalls  tottime  percall  cumtime  percall filename:lineno(function)
  2/10.0000.0000.0700.070 :1(?)
10.0000.0000.0700.070 profile:0(fun3(a))
00.000 0.000  profile:0(profiler)
10.0700.0700.0700.070 python-10176R0H.py:4(fun3)
>>> profile.run('fun4(a)')
 4 function calls in 0.100 CPU seconds
   Ordered by: standard name
   ncalls  tottime  percall  cumtime  percall filename:lineno(function)
10.0000.0000.1000.100 :1(?)
10.0000.0000.1000.100 profile:0(fun4(a))
00.000 0.000  profile:0(profiler)
10.0000.0000.0000.000 
python-10176e-N.py:6(__init__)
10.1000.1000.1000.100 python-10176rIU.py:1(fun4)

Since
  d['x'] is fast but cumbersome
  exec "z=x+y' is still slow.
  exec "locals().update(d)" is evil
  d.x is elegant and only a little slower than d['x']
I am announcing the winner of the contest: dictwrap! (applause)
Bo
--
http://mail.python.org/mailman/listinfo/python-list


Re: changing local namespace of a function

2005-02-05 Thread Bo Peng
Kent Johnson wrote:
Bo Peng wrote:
Exec is slow since compiling the string and calls to globals() use a 
lot of time.  The last one is most elegant but __getattr__ and 
__setattr__ are costly. The 'evil hack' solution is good since 
accessing x and y takes no additional time.

Previous comparison was not completely fair since I could pre-compile 
fun2 and I used indirect __setattr__. Here is the new one:
 >>> # solution two: use exec
... def makeFunction(funcStr, name):
...   code = compile(funcStr, name, 'exec')
...   def f(d):
... exec code in d
...   return f
...
 >>> def fun2(d):
...   myfun = makeFunction('z = x + y', 'myfun')
...   for i in xrange(0,N):
... myfun(d)
...   del d['__builtins__']

You are still including the compile overhead in fun2. If you want to see 
how fast the compiled code is you should take the definition of myfun 
out of fun2:

myfun = makeFunction('z = x + y', 'myfun')
def fun2(d):
  for i in xrange(0,N):
myfun(d)
  del d['__builtins__']
Kent
I assumed that most of the time will be spent on N times execution of 
myfunc.

>>> myfun = makeFunction('z = x + y', 'myfun')
>>> def fun2(d):
...   # myfun = makeFunction('z = x + y', 'myfun')
...   for i in xrange(0,N):
... myfun(d)
...   del d['__builtins__']
>>> def fun21(d):
...   myfun = makeFunction('z = x + y', 'myfun')
...   for i in xrange(0,N):
... myfun(d)
...   del d['__builtins__']
>>> profile.run('fun2(a)')
 23 function calls in 1.930 CPU seconds
   Ordered by: standard name
   ncalls  tottime  percall  cumtime  percall filename:lineno(function)
10.0000.0001.9301.930 :1(?)
   100.4600.0000.4600.000 myfun:1(?)
10.0000.0001.9301.930 profile:0(fun2(a))
00.000 0.000  profile:0(profiler)
   100.9800.0001.4400.000 python-162616Io.py:4(f)
10.4900.4901.9301.930 python-16261Ud0.py:1(fun2)
>>> profile.run('fun21(a)')
 24 function calls in 1.920 CPU seconds
   Ordered by: standard name
   ncalls  tottime  percall  cumtime  percall filename:lineno(function)
10.0000.0001.9201.920 :1(?)
   100.3900.0000.3900.000 myfun:1(?)
10.0000.0001.9201.920 profile:0(fun21(a))
00.000 0.000  profile:0(profiler)
10.0000.0000.0000.000 
python-162616Io.py:2(makeFunction)
   100.9300.0001.3200.000 python-162616Io.py:4(f)
10.6000.6001.9201.920 python-16261huu.py:1(fun21)
--
http://mail.python.org/mailman/listinfo/python-list


Is there something similar to ?: operator (C/C++) in Python?

2005-06-18 Thread Bo Peng
Hi,

I need to pass a bunch of parameters conditionally. In C/C++, I can do

func(cond1?a:b,cond2?c:d,.)

In Python, I am using temporary variables like

if cond1:
   para1 = a
else:
   para1 = b

# 
# a bunch of such if/else

func(para1, para2,...)

Is there an easier way to do this in Python?

BTW, I do not want to use the following since all of a,b,c,d etc will be 
evaluated.

def condValue(cond, val1,val2):
   if cond:
 return val1
   else:
 return val2

func(condValue(cond1,a,b), condValue(cond2,c,d),...)

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


Re: Is there something similar to ?: operator (C/C++) in Python?

2005-06-18 Thread Bo Peng
>>In Python, I am using temporary variables like
>>
>>if cond1:
>>   para1 = a
>>else:
>>   para1 = b
>>
>># 
>># a bunch of such if/else
>>
>>func(para1, para2,...)
> 
> 
> Yeah, that's how I would do it.  How many of these things do you have?

I have around 10 of them. Using these if/else, it will take 50 lines for 
a function call. It also bothers me to have 10 variables left in the 
namespace, in addition to finding 10 meaningful names.

The FAQ provides two solutions, neither of them are elegant. I guess I 
will use if/else for reasability purpose.

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


Re: Is there something similar to ?: operator (C/C++) in Python?

2005-06-19 Thread Bo Peng
Roy Smith wrote:

> Can you give us some idea of what it is that you're trying to do?  It pretty 
> unusual to see 
> a requirement like that.

def func(type_of_obj1, type_of_obj2, .):
   callfunc( [
 type_of_obj1 and obj1a() or obj1b(),
 type_of_obj2 and obj2a() or obj2b(),
 
 ])

callfunc can take arbitrary number of objects whose types are determined 
by type_of_obj1 etc. I was using a bunch of if/else to create objects 
and pass them to callfunc.

Since type_of_obj1 etc are usually binary and obj1a() etc will never be 
false, the and/or solution does not look so bad in this case.

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


How to obtain a 'interactive session' of a script?

2005-09-18 Thread Bo Peng
Dear list,

I have a long list of commands in the form of a script and would like to 
  obtain a log file as if I enter the commands one by one. (The output 
will be used in a tutorial.) What would be the best way to do it? Copy 
and paste is not acceptable since I make frequent changes tot he script.

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


Re: How to obtain a 'interactive session' of a script?

2005-09-18 Thread Bo Peng

Thank you for the suggestions and code!

> import code
> 
> SCRIPT = [line.rstrip() for line in open("myscript.py")]
> 
> script = ""
> prompt = ">>>"
> 
> for line in SCRIPT:
> print prompt, line
> script = script + line + "\n"
> co = code.compile_command(script, "", "exec")
> if co:
> # got a complete statement.  execute it!
> exec co
> script = ""
> prompt = ">>>"
> else:
> prompt = "..."
> 

This one fails at function definition.

def fun():
   a=1
   b=2   <--- not included.

Still trying other methods.
Bo
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: How to obtain a 'interactive session' of a script?

2005-09-18 Thread Bo Peng
[EMAIL PROTECTED] wrote:
> The 'code' module contains 'Utilities needed to emulate Python's interactive
> interpreter.'.  By subclassing code.InteractiveConsole and replacing the
> raw_input method with one which reads from a file, I think you can get what 
> you
> want.

This method works fine with only one minor problem. It would stop 
(waiting for user input) at help(str) command. I will have to find a way 
to feed the program with'q' etc.

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


Re: How to obtain a 'interactive session' of a script?

2005-09-18 Thread Bo Peng
Fredrik Lundh wrote:
> replacing sys.stdin with something that isn't a TTY will fix this.

This works like magic! Thank you!

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


Big latex subscript using python.sty and manual.cls

2005-02-15 Thread Bo Peng
Dear list,
I am writing a manual for my python extension using Python (2.4) latex 
classes. Everything seems to be fine except that the subscripts are as 
big as normal text ( see page 55 of 
http://bp6.stat.rice.edu:8080/simuPOP_doc/userGuide.pdf ). I doubt that 
the following piece of code in python.sty is causing the problem but can 
not fix it. (I get errors when commenting out this block of code.) Does 
anyone know what is happening here?

Note that the subscripts are OK if I use other classes like report.cls.
Many thanks in advance.
Bo
% in python.sty 
% This gets the underscores closer to the right width; the only change
% from standard LaTeX is the width specified.
\DeclareTextCommandDefault{\textunderscore}{%
  \leavevmode [EMAIL PROTECTED]
% Underscore hack (only act like subscript operator if in math mode)
%
% The following is due to Mark Wooding (the old version didn't work with
% Latex 2e.
\DeclareRobustCommand\hackscore{%
  \ifmmode_\else\textunderscore\fi%
}
\begingroup
\catcode`\_\active
\def\next{%
  \AtBeginDocument{\catcode`\_\active\def_{\hackscore{}}}%
}
\expandafter\endgroup\next
%%%
--
http://mail.python.org/mailman/listinfo/python-list


default value for list access?

2005-02-27 Thread Bo Peng
Dear list,
My program needs to do calculation based on a giving data structure 
(like a sparse matrix) with  lots of missing values (invalid 
indices/keys of lists/dictionaries).

The programing is difficult in that I have to use a try...except block 
for every item access. I have written a function

def getItem(item, idx, default):
  try:
return item[idx]
  except:
return default
Then I will have to write a[1]['a'][4] as
getItem( getItem( getItem(a,1,{}), 'a', []), 4, 0)
Is there any way to automatically return 0 when any exception is raised 
for such data acceses? (automatic wrapping a[...] as
 try:
   a[...]
 except:
   0
)

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


Re: default value for list access?

2005-02-27 Thread Bo Peng
To clearify the problem:
The data is the count of something, for example a[90]=10. a may be a 
dictionary if the range is huge and a list when the range is reasonably 
small. In the dictionary case, I can use a.setdefault(80, 0) if key 80 
does not exist. In the list case, I have to check the length of list 
before I access it (or use exception).

This becomes troublesome when I need to do the following a lot.
  b = a[80][1] + a[80][2] + a[80][3]
If I use the getItem function in my previous email, it will look like
  b = getItem(getItem(a, 80, []), 1, 0) + getItem(getItem(a, 80, []), 
2, 0) + getItem(getItem(a, 80, []), 3, 0)

What would be the best solution to handle this? Something like
  b = df( a[80][1], 0) + df( a[80][2], 0) + df( a[80][3], 0)
would be reasonable but df can not be easily defined since the exception 
will be raised before function df is called. Something like

  b = df( a, [80, 1], 0) + df( a, [80,2], 0) + df( a, [80, 3], 0)
would work if df is defined as
def df(a, idx, default):
  try:
for i in range(0, idx):
  a = a[ idx[i] ]
return a
  except:
return 0
but I am afraid that a for loop would bring serious performance problem.
Thanks.
Bo
--
http://mail.python.org/mailman/listinfo/python-list


Re: default value for list access?

2005-02-27 Thread Bo Peng

Sounds to me like the best solution is to simplify the implementation
and dispense with the list alternative.  Why use a list if a dictionary
is suitable?  Don't say performance: that's premature optimization.
Dictionaries already have what you need, apparently, with setdefault(),
so just use them and keep the code simple.
You are right that if I use all dictionaries, I can use
  a.setdefault(4,{}).setdefault(2,0)
to access a[4][2]. Not too bad compared to the getItem function.
However, this is an scientific application. Data are stored internally 
in C format and exposed to Python as python array (arraymodule). I guess 
the best way is to modify the arraymodule interface and stop it from 
raising an exception when index is out of range. (similar to Ruslan's 
solution). But this means too much trouble for now.

I will use the df(a, idx, default) method and see if its performance is 
acceptable.

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


parameter name conflict. How to solve?

2005-03-07 Thread Bo Peng
Dear list,
If you ask: why do you choose these names? The answer is: they need to 
be conformable with other functions, parameter names.

I have a function that pretty much like:
def output(output=''):
  print output
and now in another function, I need to call output function, with again 
keyword parameter output

def func(output=''):
  output(output=output)
Naturally, I get 'str' object is not callable. Is there a way to tell 
func that the first output is actually a function? (like in C++, 
::output(output) )

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


Re: parameter name conflict. How to solve?

2005-03-08 Thread Bo Peng
Delaney, Timothy C (Timothy) wrote:
Is this a style guide thing?
Why not just:
def func(output_param=''):
output(output=output_param)
This is exactly the problem. There are a bunch of other functions that 
use output='' parameter. Changing parameter name for this single 
function may cause confusion.

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


Re: parameter name conflict. How to solve?

2005-03-08 Thread Bo Peng
Kent Johnson wrote:
Bo Peng wrote:
def func(output=''):
  output(output=output)
Naturally, I get 'str' object is not callable. Is there a way to tell 
func that the first output is actually a function? (like in C++, 
::output(output) )

You could use a default argument:
def func(output='', output_fn=output):
  output_fn(output=output)
Kent
Thank everyone for the quick responses. All methods work in general. 
Since func() has to take the same parameter set as some other functins, 
I can not use  func(output='', output_fn=output). "output_alias=output" 
etc are fine.

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


How to pass parameter when importing a module?

2005-03-20 Thread Bo Peng
Dear list,
What I would like to do is something like:
In myModule.py ( a wrapper module for different versions of the module),
  if lib == 'standard':
from myModule_std import *
  elsif lib == 'optimized'
from myModule_op import *
but I do not know how to pass variable lib to myModule.py to achieve the 
following effect:

  >>> lib = 'standard'
  >>> from myModule import * # actually import myModule_std
From what I have read, from  does not take any parameter. Maybe I 
should use environmental variables?

  >>> os.putenv('lib', 'standard')
  >>> from myModule import *
  myModule.py
  -
  import os
  lib = os.getenv('lib')
  ...
or use a separate module?
  param.py
  -
  para = {}
  def setParam(key, val):
para[key] = val
  main session
  
  >>> import param
  >>> param.setParam('lib','standard')
  >>> from myModule import *
  in myModule.py
  --
  from param import para
  try:
lib = para['lib']
  except:
lib = 'standard'
  ...
Is there an established approach for this problem?
Many thanks in davance.
Bo
===
FULL STORY:
I have several modules all (SWIG) wrapped from the same C++ source code 
but with different compiling flags. For example,

  myModule_std  (standard module)
  myModule_op   (optimized, without error checking)
  ...
These modules are put directly under /.../site-packages . To load a 
module, I use, for example

  from myModule_op import *
This works fine until I need to write some helper functions for 
myModule_?? in another module myHelper.py since I do not know which 
myModule is being used

  from myModule?? import A,B
I find one solution
  # find out which module is being used
  import sys
  if 'myModule_op' in sys.modules.keys():
from myModule_op import A,B
  else:
from myModule_std import A,B
but not completely satisfied. Therefore, I am writing a 'wrapper' module 
myModule that can load one of the myModule_?? modules according to user 
supplied info.

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


Re: How to pass parameter when importing a module?

2005-03-20 Thread Bo Peng

Take a look at wxPython versioning:
http://wiki.wxpython.org/index.cgi/MultiVersionInstalls
The most simple usage looks like
  import wxversion
  wxversion.select("2.4")
  import wx
  Serge. 

This is essentially my second method: using another module to set 
parameter for myModule. Since wxPython uses this method, I suppose this 
is the standard approach for this problem.

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


Testing the availability of a module

2005-12-19 Thread Bo Peng
Dear list,

Is there a better way than doing

try:
   import aModule
except:
   has_aModule = False
else:
   has_aModule = True

The main concern here is that loading aModule is unnecessary (and may 
take time).

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


Re: Testing the availability of a module

2005-12-19 Thread Bo Peng
Peter Hansen wrote:
> Bo Peng wrote:
> 
>> Is there a better way than doing
>>
>> try:
>>import aModule
>> except:
>>has_aModule = False
>> else:
>>has_aModule = True
>>
>> The main concern here is that loading aModule is unnecessary (and may 
>> take time).

 > And if you aren't doing the above multiple times, then you *really*
 > shouldn't be worried about the time to load "aModule"

This is used in a scenario like:

   if wxPython is available:
 prepare wx based GUI
   elif Tkinter is available:
 prepare tk based GUI
   else:
 command line only

Since a user may prefer command line so wxPython/tkinter should not be 
actually loaded before the GUI function is called.

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


Re: Testing the availability of a module

2005-12-19 Thread Bo Peng
> if the user happens to prefer the command line interface, why bother looking
> for a GUI ?  sounds like you haven't really thought this through...
> 
> here's an outline that matches your use case:
> 
> if user wants command line:
> command line only
> else:
> if wxPython is available:
> prepare wx based GUI
> elif Tkinter is available:
> prepare tk based GUI
> else:
> command line only

This is exactly the logic I use,... Eliminating all unrelated stuff, I 
have something like (thanks for the imp hint):

def wxGUI():
   import wx
   wx...

def tkGUI():
   import Tkinter as tk
   tk

def cml():
   command line

def GUI():
   if options['cml']:
 cml()
   else:
 if imp.find_module('wx'):
   wxGUI()
 elif imp.find_module('Tkinter'):
   tkGUI()
 else:
   cml()

After a user loads this module (without wx loaded automatically as 
before), he can call cml() or GUI().

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


Weird memory consumption problem.

2005-12-19 Thread Bo Peng
Dear list,

I spent the last 12 hours in catching this bug (?) and what I found out 
is very difficult to explain:

Basically, I need to call a user-provided function many times, with a 
tuple as parameter.

C/C++ side: (class A, constructed using a python function m_func)

// create in the constructor, used to hold the parameter
m_numArray = PyTuple_New(m_len);
...

   // member function, will be called many times
   void fun(){

// set the tuple
for( j=0; jhttp://mail.python.org/mailman/listinfo/python-list


Re: Weird memory consumption problem.

2005-12-20 Thread Bo Peng
Steven D'Aprano wrote:

> Sorry, are you saying that the code you posted does NOT have a memory 
> leak, but you want us to find the memory leak in your real code sight 
> unseen?

valgrind does not detect anything so it does not look like memory leak. 
I just can not figure out why val[0], readonly access to a tuple 
element, can cause this kind of problem.

Anyway, is there a way to list all or newly added python objects? I 
guess I can trace the ref_cnt of the tuple element manually.

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


Re: Weird memory consumption problem.

2005-12-20 Thread Bo Peng
Bo Peng wrote:
>> Sorry, are you saying that the code you posted does NOT have a memory 
>> leak, but you want us to find the memory leak in your real code sight 
>> unseen?

Problem found. It is hidden in a utility function that converts the 
return value to a double.  The refcnt of the middle result was not 
decreased.

 PyObject* res = PyNumber_Float(obj);
 val = PyFloat_AsDouble(res);
 // missing
 Py_DECREF(res);

The problem is not that difficult to find, but it was 2am in the morning 
and I was misled by the different behavior of pyFun1 and pyFun2.

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


Why does Rpy/R-plot work under PythonWin, but not under commandline/IDLE?

2006-01-08 Thread Bo Peng
Dear list,

I am using rpy, a python module to control statistical package R from 
python. Running the following commands

 >>> from rpy import *
 >>> r.plot(0)

will pass command 'plot' to R and run it. I notice that the R-plot will 
not refresh (a window is created but the figure is not drawn) until the 
next plot command is executed,
   * when I use python (2.3, 2.4) command line or IDLE
   * NOT when I use pythonwin ( provided with pywin32 package)

Since rpy+R works under python+pythonwin, I assume that this is not a 
problem with rpy or R. Does anyone know why commandline/IDLE cause this 
problem, while PythonWin does not? I am interested to know what I can do 
in commandline (or in rpy) to fix this problem.

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


Re: Why does Rpy/R-plot work under PythonWin, but not under commandline/IDLE?

2006-01-08 Thread Bo Peng
Bo Peng wrote:
> Does anyone know why commandline/IDLE cause this 
> problem, while PythonWin does not? I am interested to know what I can do 
> in commandline (or in rpy) to fix this problem.

Just note that R/plot works fine both from command window and a R-GUI.

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


How to download a web page just like a web browser do ?

2006-08-23 Thread Bo Yang
Hi ,
 It is about one month passed since I post to this list
last time . Yes , I use python , I used it in every day normal
work , whenever I need to do some scripts or other little-scale
works , python is the first one I took consideration in . I must
say it is a powerful tool for me , and what is more important
is there is a friendly and flourish community here .
 Oh , I must stop appriciation and go to my question now .

Everyday , I receive a newsletter from NYTimes , but I
didn't want to read the news in evening the time the letter
came in . So , I am about to download the web page
contains the news and read them next morning ! I decide to
use python to write a tool , which should be feeded with a
URL , and then download the page to my disk . This
function just like the Browser's "save as..." function . I
know what shoud I do to accomplish that , I need to parse
the web page , and download all pages in the page , and
modify all the links to conrespond local disk links and ...

   So , is there any similar function any one have impelment?
Does anyone can share some code with me ? I really don't
want to some confusing code to process such as text findings
and substitutions .

 Thanks in advance !


        Bo

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


An algorithm problem

2006-05-30 Thread Bo Yang
Hi ,
I have writen a python program to slove a problem described as below:

(Forgive my poor English !)

Put the 2^n 0 or 1 to form a ring , and we can select any continuous n
ones from
the ring to constitute a binary number . And obviously we can get 2^n
selections ,
so the question is :
Given a number n and you must
Povide an algorithm by which the 0-1 ring is produced , and the 2^n
number we get
are just from 0 to 2^n-1 uniquely and entirely .

So I write the following program to slove the question , and it works
well for the n below 10


flag = 0
input = 10
number = 2**input
set = set()
list = []

def list2int(l , n ):
ret = 0
for x in l :
if( n == 0 ):
break
if x :
ret = ( ret << 1 ) | 1
else :
ret = ( ret << 1 ) | 0
n = n - 1
return ret


def ring( index , number ):
global list , set
temp = []
if( number < index ): #the range overflow
#detect whether the remain of the list is ok ?
for x in xrange(1,input):
begin = number - input + 1 + x
l = list[begin:] + list[0:x]
i = list2int(l , input )
if i in set:
for i in temp:
set.remove(i)
return
else:
set.add(i)
temp.append(i)
#index = index + 1
global flag
flag = 1
return

list.append(1)
if len(list) < input or list2int(list[index-input+1:index+2],input) not
in set:
if(len(list) >= input ):
set.add(list2int(list[index-input+1:index+2],input))
ring(index+1,number)
if(flag == 1):
return
if(len(list) >= input ):
set.remove(list2int(list[index-input+1:index+2],input))

list = list[:index]
list.append(0)
if len(list) < input or list2int(list[index-input+1:index+2],input) not
in set:
if(len(list) >= input ):
set.add(list2int(list[index-input+1:index+2],input))
ring(index+1,number)
if(flag == 1):
return
if(len(list) >= input ):
set.remove(list2int(list[index-input+1:index+2],input))

list = list[:index]

ring(0,number-1)

print list


But when the n reach 10 , Python report the stack is exhausted :

RuntimeError: maximum recursion depth exceeded

And my question is , where can I put some optimization to make the code can
work more smoothly with more greater n ?

Thanks in advance !

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

Re: An algorithm problem

2006-05-31 Thread Bo Yang
I am sorry , and thanks for your friendliness .
I have change my source code for more meaningful variable name and more 
comments follow the instructions
John has writen . I think that is useful for you to look my code .

And this time I put the code in the attachment . Indeed , I write that 
in the Eclipse Pydev , I don't know why
it lose its idention in the mailing list .

Also , I forget an example for the question , and I compensate for it :

Given the integer 2 for instance :
what we exapct from the algorithm is a list of 0 or 1 ,  for this 
instance it is  1 1 0 0 .
We can take it as a ring , and fetch any continuous 2 numbers from it , 
finally get four combinations :
11 , 10 , 00 , 01
and they are 3 , 2 , 0 , 1 .


I hope this time it is clear for all to understand what problem confront 
me .
And again I am sorry for the trouble , thanks for you again !

Best Regard
Bo Yang
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: An algorithm problem

2006-05-31 Thread Bo Yang

I am sorry , and thanks for your friendliness .
I have change my source code for more meaningful variable name and more 
comments follow the instructions

John has writen . I think that is useful for you to look my code .

And this time I put the code in the attachment . Indeed , I write that 
in the Eclipse Pydev , I don't know why

it lose its idention in the mailing list .

Also , I forget an example for the question , and I compensate for it :

Given the integer 2 for instance :
what we exapct from the algorithm is a list of 0 or 1 ,  for this 
instance it is  1 1 0 0 .
We can take it as a ring , and fetch any continuous 2 numbers from it , 
finally get four combinations :

11 , 10 , 00 , 01
and they are 3 , 2 , 0 , 1 .


I hope this time it is clear for all to understand what problem confront 
me .

And again I am sorry for the trouble , thanks for you again !

Best Regard
Bo Yang
#This is the file for the solution of the 0-1 ring problem
#in the csdn algorithm forum
#The input to this program is a integer n ,
#The ouuput of the algorithm is the a list of 2^n elements 
#in which every number in the range of 0~2^n-1 just appear
#once exactly .

flag = 0# whether we found the ring
input = 2  # the input n 
range = 2**input   #2^n
integers = set()#the set of the numbers we have in the ring now
ring = []  # the ring

def list2int(list , n ): #convert a list contains only 0 or 1 to a 
integer
ret = 0 
for x in list :
if n == 0 :
break 
if x :
ret = ( ret << 1 ) | 1
else :
ret = ( ret << 1 ) | 0
n = n - 1
return ret 


def getring( index , range ):#the main function for the problem
global ring , integers 
temp = []
if range < index : #the range overflow 
#detect whether the remain of the list contains the remain integers
for x in xrange(1,input): 
begin = range - input + 1 + x 
l = ring[begin:] + ring[0:x]
i = list2int(l , input )
if i in integers:
for i in temp:
integers.remove(i)
return
else:
integers.add(i)
temp.append(i)
#index = index + 1
#if we reach here , it indicat that we succeed
global flag
flag = 1
return

#test whether 1 in the position is ok?
ring.append(1)
if len(ring) < input or list2int(ring[index-input+1:index+2],input) not in 
integers:
if len(ring) >= input :
integers.add(list2int(ring[index-input+1:index+2],input))
getring(index+1,range)
if flag == 1:
return 
if len(ring) >= input :
integers.remove(list2int(ring[index-input+1:index+2],input))

#test whether 0 in this position is ok?
ring = ring[:index]
ring.append(0)
if len(ring) < input or list2int(ring[index-input+1:index+2],input) not in 
integers:
if len(ring) >= input :
integers.add(list2int(ring[index-input+1:index+2],input))
getring(index+1,range)
if flag == 1 :
return 
if len(ring) >= input :
integers.remove(list2int(ring[index-input+1:index+2],input))

ring = ring[:index]

#begin process
getring(0,range-1)

#output the result list
print ring-- 
http://mail.python.org/mailman/listinfo/python-list

An error ?

2006-06-11 Thread Bo Yang
Hi ,
I am confronted with an odd question in the python cgi module !

Below is my code :

import cgitb ; cgitb.enable()
import cgi

print "Hello World !"

How easy the script is , and the url is 202.113.239.51/vote/cgi/a.py
but apache give me a 'Server internal error !'
and the error log is :


[Fri Jun 16 14:06:45 2006] [error] [client 10.10.110.17] malformed
header from script. Bad header=Hello World!: a.py

I wish somebody could help me , thanks in advance !

Best Regard !


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

Re: An error ?

2006-06-12 Thread Bo Yang
It works , thank you everyone !

I think there is much more I need to learn before I can grasp a full 
understand of the C/S modle !

Thanks again !
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Python or Ajax?

2006-06-12 Thread Bo Yang
I think if you know java language very well but feel suffering with the 
error prone javascript ,
GWT is good choose for AJAX development .
With the well-known IDE Eclipse your development time efficiency will 
promote fast !
-- 
http://mail.python.org/mailman/listinfo/python-list


How do you use this list ?

2006-06-27 Thread Bo Yang
Hi everyone ,
I have join this list for about 4 months , and everyday I receive
hundreds of
mails . There is no means to read all of them , so I just read something
interesting
for me . But if so , there are too much mails pile up in my inbox , I
want to ask
how do you use this list , reading every mail come in or just read what
you think
interesting ?

Thank you !


Best Regard !
-- 
http://mail.python.org/mailman/listinfo/python-list

Re: How to download a web page just like a web browser do ?

2006-08-24 Thread Bo Yang

 Thank you , Max !
 I think HarvestMan is just what I need !
 Thanks again !

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


How to distribute an additional DLL to site-packages?

2006-12-21 Thread Bo Peng
Dear Python experts,

I am writing a python extension module that needs to link with a 
third-party DLL. How can I copy this DLL to the site-packages directory 
along with my extension modules? It seems that data_files parameter can 
do this, but I do not know how to get the absolute destination 
directory. After all, this directory is configurable.

I guess I need to determine /path/to/site-package for

setup(
 ...
 ext_modules = [...]
 data_files = [ ('/path/to/site-package', ['mydll.dll]) ]
)

using something like distutil.config.get_dest_dir().

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


Building python C++ extension modules using MS VC++ 2005?

2006-12-21 Thread Bo Peng
Dear list,

I have been using mingw to build a python extension module. I had to 
jump through a number of hooks like the _ctype problem caused by the use 
of msvcr71.dll, but the module was mostly usable.

Things become complicated when I use more and more boost libraries and 
mingw can not work well with some of the modules. I am trying to switch 
to VC but I was suggested that I need to use the identical version of VC 
that is used to build python, which means VC7.1, 2003. However, 
microsoft seems to stop distributing this version of VC, and its website 
points me to VC expression 2005... 
 
(http://msdn2.microsoft.com/en-us/visualc/aa336490.aspx)

Can I use the 2005 version to build python extension? What is the 
official or recommended way to build python extension module under windows?

Many thank in advance.
Bo
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Building python C++ extension modules using MS VC++ 2005?

2006-12-22 Thread Bo Peng
Sandra-24 wrote:
> You can use 2005 to build extensions for Python 2.5. I've done this
> with several extensions, both my own and others. I do not know if you
> can use it for Python 2.4, so I won't advise you on that. I thought
> Microsoft made its C/C++ compiler, version 7.1 (2003) freely available
> as a command line tool. If you can't find it on their site, ask around,
> I'm sure a lot of people have it. Probably you'll also find someone has
> put it up somewhere if you search on google. Try 2005 first and see
> what happens though.

But how exactly should I do it? I installed VC8 and get the all-familiar 
error message: Python was built with Visual Stuidio 2003 

Actually, I get a copy of VC6/2003 but this compiler can not compile 
boost 1.33.1/serialization so it can not be used to compile my extension 
module.

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


Re: How to distribute an additional DLL to site-packages?

2006-12-22 Thread Bo Peng
> I am writing a python extension module that needs to link with a 
> third-party DLL. How can I copy this DLL to the site-packages directory 
> along with my extension modules? It seems that data_files parameter can 
> do this, but I do not know how to get the absolute destination 
> directory. After all, this directory is configurable.

So nobody has ever done this???

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


Re: How to distribute an additional DLL to site-packages?

2006-12-22 Thread Bo Peng
> Use the package_data option. setup(..., packages=['yyy'],
> package_data={'yyy':['xxx.dll']}, ...)
> (Distutils documentation may be arcane sometimes, but this is easily
> found at http://docs.python.org/dist/node12.html)
> Absolute dirs are almost never necesary, usually all distutils commands
> operate on relative paths (relative to location of setup.py for source
> files, relative to some meaningful directory for targets).

I read that page but I am not using package so things are installed to 
site-packages directly. What should I put for 'yyy'?

Bo

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


Is there a way to profile underlying C++ code?

2006-01-24 Thread Bo Peng
Dear list,

I have a C++-SWIG-wrapped python module. The running time for one of the 
functions is pretty unpredictable so I would like to profile it. 
However, the python profiler does not seem to enter the compiled module 
(.so file). Is there a way to profile the C++ functions?

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


Re: Is there a way to profile underlying C++ code?

2006-01-25 Thread Bo Peng
Travis E. Oliphant wrote:

> On Linux you can use oprofile (which is pretty nice and easy to use --- 
> no recompiling.  Just start the profiler, run your code, and stop the 
> profiler).

Thank you very much for the tip. This is a great tool.

The source of the problem has been found: cache misses. Briefly, I have 
a long array and I need to choose numbers from it randomly. When the 
array is short, the performance is good. When the array is big, picking 
a number will cause a cache miss almost all the time. This results in 
almost doubled execution time.

Problem found, but I am out of idea how to fix it since the nature of my 
algorithm is working randomly on large arrays.

Cheers,
Bo
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Start a python interactive shell from python.

2006-01-26 Thread Bo Peng

I think I find what I need:

http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/355319

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


Start a python interactive shell from python.

2006-01-26 Thread Bo Peng
Dear list,

This may sound strange but I need to start a python shell from python. 
The motivation is that I have a bunch of (numeric) python functions to 
provide to a user. The best way I can think of is packing my python 
module using py2exe, and because it is easiest to let him run the 
functions through python syntax, I would like to start a python shell 
after the module is loaded.

Something like:

def myfunc(a):
   pass

if __name__ == '__namin__':
   ' myfunc has been defined '
   START A PYTHON SHELL?

so the user can type myfunc(0.5) and others. I can of course use 
raw_input and eval but I will lose the convenience of a real shell.

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


How many web framework for python ?

2006-02-17 Thread Bo Yang
Hello everybody ,
I am a student major in software engeering .
I need to do something for my course .

There are very good web framework for java and ruby ,
Is there one for python ?
I want to write a web framework for python based on
mod_python as my course homework , could you give some
advise ?
-- 
http://mail.python.org/mailman/listinfo/python-list

Re: How many web framework for python ?

2006-02-18 Thread Bo Yang
Thank you very much !
-- 
http://mail.python.org/mailman/listinfo/python-list


make a class instance from a string ?

2006-02-23 Thread Bo Yang
Hi,
I know in java , we can use

class.ForName("classname")


to get an instance of the class 'classname' from a
string , in python , how do I do that ?

Thanks in advance !

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

Module written in C does not repond to Ctrl-C interruption.

2006-02-24 Thread Bo Peng
Dear list,

I have not done a thorough test, but it occurs to me that

1. python code can be interrupted by Ctrl-C.
2. A C module, if I add a main() function and run independently, can be 
interrupted by Ctrl-C.
3. If I load the C module in python and run, the program will not 
respond to Ctrl-C interruption. I have to kill python explicitly.

If this is a known behavior or just a special case of mine? Any fix to 
it? I am using python 2.3.4 under Redhat EL4 with gcc 3.3.4.

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


Re: Module written in C does not repond to Ctrl-C interruption.

2006-02-24 Thread Bo Peng
Daniel Dittmar wrote:
> 
> You could set up your own signal handler when entering the C extension. 
> This should abort the extension (tricky) and call the Python signal 
> handler.

This can be done under linux using things in signal.h but I am not
sure whether or not there is a portable way to do it (max, win32).
Does anybody know a quick way under windows?

Bo

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


Re: do design patterns still apply with Python?

2006-03-04 Thread Bo Yang
Paul Novak 写道:
> A lot of the complexity of design patterns in Java falls away in 
> Python, mainly because of the flexibility you get with dynamic typing.
>
I agree with this very much !
In java or C++ or all such static typing and compiled languages , the 
type is fixed on
in the compile phrase , so for the flexible at the runtime , we often 
need to program to
interface . For example ,
we do in java :

implement I{...}
class A implement I{...}
class B implement I{...}

oprate(I var) // we can pass A's instance or B's instance here

and in C++ :

class Abstract{...}
class A : Abstract{...}
class B : Abstract{...}

oprate(Abstract var) // pass the A's instance or B's instance here

But in python , type is dynamic , and name is bind at runtime , so we 
can pass any variable as we want ! This feather make python not need for 
redundant class inherits
and interfaces which are the core of the GoF's design patterns I think !

> For a Pythonic Perspective on Patterns, "Python Programming Patterns" 
> by Thomas W. Christopher is definitely worth tracking down. It looks 
> like it is out of print, but you can find used copies on Amazon.
>
> Regards,
>
> Paul.
>
>
> This sounds like an article crying out to be written,
> "(Learning) Design Patterns with Python".
>
> Has it been written already?
>
> Cheers,
> Terry
>
>
> Bruce Eckel began writing "Thinking In Python" it was last updated
> in 2001.
>

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

Re: do design patterns still apply with Python?

2006-03-04 Thread Bo Yang
Paul Novak :
> A lot of the complexity of design patterns in Java falls away in 
> Python, mainly because of the flexibility you get with dynamic typing. 
>
I agree with this very much !
In java or C++ or all such static typing and compiled languages , the 
type is fixed on
in the compile phrase , so for the flexible at the runtime , we often 
need to program to
interface . For example ,
we do in java :

implement I{...}
class A implement I{...}
class B implement I{...}

oprate(I var) // we can pass A's instance or B's instance here

and in C++ :

class Abstract{...}
class A : Abstract{...}
class B : Abstract{...}

oprate(Abstract var) // pass the A's instance or B's instance here

But in python , type is dynamic , and name is bind at runtime , so we 
can pass any variable as we want ! This feather make python not need for 
redundant class inherits
and interfaces which are the core of the GoF's design patterns I think !
> For a Pythonic Perspective on Patterns, "Python Programming Patterns" 
> by Thomas W. Christopher is definitely worth tracking down.  It looks 
> like it is out of print, but you can find used copies on Amazon.
>
> Regards,
>
> Paul.
>
>  
>
> This sounds like an article crying out to be written,
> "(Learning) Design Patterns with Python".
>
> Has it been written already?
>
> Cheers,
> Terry
>
>
> Bruce Eckel began writing "Thinking In Python" it was last updated
> in 2001.
>

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


Re: Suggestions for documentation generation?

2006-03-04 Thread Bo Peng
kpd wrote:
> Hello,
> 
> I have written a C++ library that I've then wrapped with Pyrex.
> Any suggestions to the best-in-class tool to create documentation for
> the libraries?
> 
> I would love to document things in one spot (could be the code) and
> generate html and PDF from there.
> 
> Doxygen (www.doxygen.org) looks to be about the best so far. 
> 
>

I was facing the same problem and took the following approach:

1. write c++ code with doxygen-complaint comments
2. generate XML comments using doxygen
3. parse XML and generate Python docstring
4. insert the docstring into SWIG interface file

I can send you the parser but I do not know if pyrex support python 
docstring.

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


control precision for str(obj) output?

2005-05-03 Thread Bo Peng
Dear list,

I have enjoyed the convenience to output any object with str(obj) for a 
while. However, I get long output for things like str([0.0002]) in my 
output (which bothers my users more than me though). I also do not 
understand why the following is happening:

 >>> str([0.0002])
'[0.00020001]'
 >>> str(0.0002)
'0.0002'

Is there any way to fix this, other than checking the type of everything 
and output with %.4f?

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


Re: control precision for str(obj) output?

2005-05-03 Thread Bo Peng
> The unfortunate side effect of this is to make str(list of floats)
> ugly, but...

Yeah, Nothing is perfect. I can control the damage as long as I know 
what is going on. I guess this would fit in a faq list quite well.

I would have to thank Mike and Dan for your quick and detailed reply. As 
a matter of fact, this python list is the most friendly mailinglist I 
have ever joined. Thanks!

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


Re: control precision for str(obj) output?

2005-05-04 Thread Bo Peng
Dan Bishop wrote:
> Andrew Dalke wrote:
> 
>>Mike Meyer wrote:
>>
>>>Someone want to tell me the procedure for submitting FAQ entries,
> 
> so I
> 
>>>can do that for this?
>>
>>You mean more than what already exists at
>>
> 
> http://www.python.org/doc/faq/general.html#why-are-floating-point-calculations-so-inaccurate
> 
>>which has a link to an even more detailed chapter in the tutorial at
>>  http://docs.python.org/tut/node16.html
> 
> 
> Yes.  Those pages explain why repr(0.2) != '0.2', but they don't
> explain why str([x]) != '[%s]' % x .
> 

I am new to Python but has experience with other languages. I am quite 
aware of 'why-are-floating-point-calculations-so-inaccurate'. I would 
not be surprised if

 >>> str[0.0002)
0.0002001
 >>> str([0.0002])
[0.0002001]

It was the difference between str([x]) and '[%s]' % x str(x) that 
confused me.

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


Watch folder for new file and execute extern program

2007-02-05 Thread Michael Bo
Hi.
Can anyone guide me on how to minitor a folder for new files? And when
they appear I need to run and externe program with the file just
created (buy a 3rd program).
- Michael Bo

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


Re: Watch folder for new file and execute extern program

2007-02-07 Thread Michael Bo

> BTW-It helps us out here if you let us know what platform you
> are running on (e.g. Windows, Linux, Mac, etc.).
>
> -Larry

Sorry... I'm running on windows XP.
- Michael

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


Re: Building a Dynamic Library (libpython.so) for Python 2.4.3 Final

2006-04-26 Thread Bo Gus
In article <[EMAIL PROTECTED]>,
 Andrew MacIntyre <[EMAIL PROTECTED]> wrote:

> Dean wrote:
> > I've been trying to make python a dynamic library. I downloaded Python 
> > 2.4.3 Final from the Python web site and I cannot get it to create the 
> > library.
> > 
> > I've tried using the directive:
> > 
> > --enable-shared
> > 
> > and
> > 
> > --enable-shared=yes
> > 
> > and both of them had the same effect of creating a bunch of parts of 
> > the python interpreter in .so format but not in creating a single
> > libpython2.4.so.X.Y file. I could probably hack something together 
> > using ar but I would prefer to do it "correctly" by setting some 
> > options. I'm compiling under OpenBSD 3.5.
> 
> Well, I just confirmed that
> 
> ./configure --enable-shared
> 
> results in a libpython2.4.so.1 (and a libpython2.4.so) on FreeBSD 4.x.
> The result runs the regression test suite without significant problems
> (the test box didn't have a 2.4 installation).
> 
> A quick look at the configure script suggests that --enable-shared should
> work on OpenBSD.  While I believe that there's actually an OpenBSD based
> Python buildbot (though OpenBSD 3.[89] I think) running, I doubt that
> this configuration is tested there.
> 
> If you don't get any other responses, send me a copy of the full make log
> (ie "make >make.log 2>&1") and I'll compare it to the log from my test
> build.
Thanks for the help. It didn't build the shared library on its own but I 
did manage to find where it is built and a simple

make libpython2.4.so

Got it built for me. Thanks for the idea!

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


Python distutil: build libraries before modules

2006-05-01 Thread Bo Peng
Dear list,

My python modules depend on a few boost libraries. For convenience, I 
include the source code of these libraries with my distribution and 
treat them as standard source files. This results in huge cc.exe command 
lines and I get an error saying
   error: command 'cc' failed: Invalid argument
under win32/mingw. (I am not quite sure this is the reason though.)

How can I let setup.py build these boost libraries separately so that I 
can link them to my modules? This will also save some link time because 
all my python modules link to the same boost libraries.

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


Re: Python distutil: build libraries before modules

2006-05-01 Thread Bo Peng
Bo Peng wrote:

> How can I let setup.py build these boost libraries separately so that I 
> can link them to my modules? This will also save some link time because 
> all my python modules link to the same boost libraries.

Although this is nowhere documented, I find that I can use

from distutils.ccompiler import *

def buildStaticLibrary(sourceFiles, libName, libDir):
   '''Build libraries to be linked to simuPOP modules'''
   # get a c compiler
   comp = new_compiler()
   objFiles = comp.compile(sourceFiles)
   comp.create_static_lib(objFiles, libName, libDir)

to build the libraries

Cheers,
Bo
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: reusing parts of a string in RE matches?

2006-05-10 Thread Bo Yang
John Salerno 写道:
> I probably should find an RE group to post to, but my news server at 
> work doesn't seem to have one, so I apologize. But this is in Python 
> anyway :)
>
> So my question is, how can find all occurrences of a pattern in a 
> string, including overlapping matches? I figure it has something to do 
> with look-ahead and look-behind, but I've only gotten this far:
>
> import re
> string = 'abababababababab'
> pattern = re.compile(r'ab(?=a)')
>   
Below from the python library reference

*|(?=...)|*
Matches if ... matches next, but doesn't consume any of the string.
This is called a lookahead assertion. For example, Isaac (?=Asimov)
will match |'Isaac '| only if it's followed by |'Asimov'|.



> m = pattern.findall(string)
>
> This matches all the 'ab' followed by an 'a', but it doesn't include the 
> 'a'. What I'd like to do is find all the 'aba' matches. A regular 
> findall() gives four results, but really there are seven.
>
>   
I try the code , but I give seven results !
> Is there a way to do this with just an RE pattern, or would I have to 
> manually add the 'a' to the end of the matches?
>
> Thanks.
>   

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

How can I add space in to several numbers?

2009-03-02 Thread Bo Zhang
I want to parse a file and do this :

A  74.335 -86.474-129.317  1.00 54.12

then add space between -86.474 and -129.317. I can get the file with

A  74.335 -86.474 -129.317  1.00 54.12

How can I do this? Thanks.

--
Best regards,

Zhang Bo (Cindy)
SMA-CSB
--
http://mail.python.org/mailman/listinfo/python-list


help

2008-05-30 Thread ha bo
hi 
i wanna paginate my table (x lines per pages),the size of my table is changing 
according to the user.
i wanna know if paginator.py can help me ,if yes how?
some one can help me 
ps:i use django
thank you 

_
Découvrez Windows Live Spaces et créez votre site Web perso en quelques clics !
http://spaces.live.com/signup.aspx--
http://mail.python.org/mailman/listinfo/python-list

[no subject]

2008-04-11 Thread ha bo
hi i use this programme in my application django:
import structMASK_CCITT  = 0x1021 # CRC-CCITT mask (ISO 3309, used in X25, 
HDLC)MASK_CRC16  = 0xA001 # CRC16 mask (used in ARC files)def updcrc(crc, 
data, mask=MASK_CRC16): data_length = len(data) unpackFormat = '%db' % 
data_length unpackedData = struct.unpack(unpackFormat, data) for char 
in data:c = ord(char)c = c << 8for j in 
xrange(8):if (crc ^ c) & 0x8000:crc = (crc 
<< 1) ^ maskelse:crc = crc << 1 
   c = c << 1 return crc & 0x


and i call this function in other function in my view:


def encodekey(var, expires=None, user='', trusted=False):  import 
random, base64import updcrckey = "%02X" % len(var) + var
key += "%02X" % len(user) + userif expires is not None:key 
+= expires.strftime('%Y%m%d%H%M')else:year = 
random.choice(range(2000,2100))month = 23day = 
random.choice(range(1,32))hour = random.choice(range(1,25)) 
   minute = random.choice(range(1,60))key += "%04d%02d%02d%02d%02d" 
% (year, month, day, hour, minute)if trusted:checksum = 
updcrc(42, key)else:checksum = updcrc(0, key)
key += "%04X" % checksumreturn base64.b64encode(key)
but it give me this error:
unpack requires a string argument of length 31
someone can help me 


_
Lancez des recherches en toute sécurité depuis n'importe quelle page Web. 
Téléchargez GRATUITEMENT Windows Live Toolbar aujourd'hui !
http://toolbar.live.com-- 
http://mail.python.org/mailman/listinfo/python-list

RE: unpack

2008-04-11 Thread ha bo
thank you i did find solution i did have just change: 

 unpackedData = struct.unpack(unpackFormat, data)  to

 unpackedData = struct.unpack(unpackFormat, data.decode('string_escape'))




From: [EMAIL PROTECTED]
Subject: Python-list Digest, Vol 55, Issue 179
To: [email protected]
Date: Fri, 11 Apr 2008 15:50:04 +0200

Send Python-list mailing list submissions to
[email protected]
 
To subscribe or unsubscribe via the World Wide Web, visit
http://mail.python.org/mailman/listinfo/python-list
or, via email, send a message with subject or body 'help' to
[EMAIL PROTECTED]
 
You can reach the person managing the list at
[EMAIL PROTECTED]
 
When replying, please edit your Subject line so it is more specific
than "Re: Contents of Python-list digest..."
--Pièce jointe du message transmise--
From: [EMAIL PROTECTED]
To: [email protected]
Date: Fri, 11 Apr 2008 05:58:46 -0700
Subject: Re: Rounding a number to nearest even

On Apr 11, 2:14 pm, bdsatish <[EMAIL PROTECTED]> wrote:
> The built-in function round( ) will always "round up", that is 1.5 is
> rounded to 2.0 and 2.5 is rounded to 3.0.
>
> If I want to round to the nearest even, that is
>
> my_round(1.5) = 2# As expected
> my_round(2.5) = 2# Not 3, which is an odd num
>
> I'm interested in rounding numbers of the form "x.5" depending upon
> whether x is odd or even. Any idea about how to implement it ?
 
def even_round(x):
if x % 1 == .5 and not (int(x) % 2):
return float(int(x))
else:
return round(x)
 
nums = [ 3.2, 3.6, 3.5, 2.5, -.5, -1.5, -1.3, -1.8, -2.5 ]
for num in nums:
print num, '->', even_round(num)
 
3.2 -> 3.0
3.6 -> 4.0
3.5 -> 4.0
2.5 -> 2.0
-0.5 -> 0.0
-1.5 -> -2.0
-1.3 -> -1.0
-1.8 -> -2.0
-2.5 -> -2.0
 
 
--Pièce jointe du message transmise--
From: [EMAIL PROTECTED]
To: [email protected]
Date: Fri, 11 Apr 2008 09:07:42 -0400
Subject: Re: (unknown)

ha bo wrote:
> hi i use this programme in my application django:
> import struct
> MASK_CCITT  = 0x1021 # CRC-CCITT mask (ISO 3309, used in X25, HDLC)
> MASK_CRC16  = 0xA001 # CRC16 mask (used in ARC files)
> 
> def updcrc(crc, data, mask=MASK_CRC16):
> 
>  data_length = len(data)
>  unpackFormat = '%db' % data_length
>  unpackedData = struct.unpack(unpackFormat, data)
> 
>  for char in data:
> c = ord(char)
> c = c << 8
>
> for j in xrange(8):
> if (crc ^ c) & 0x8000:
> crc = (crc << 1) ^ mask
> else:
> crc = crc << 1
> c = c << 1
> 
>  return crc & 0x
> 
> 
> and i call this function in other function in my view:
> 
> 
> def encodekey(var, expires=None, user='', trusted=False):
>  
> import random, base64
> import updcrc
> key = "%02X" % len(var) + var
> key += "%02X" % len(user) + user
> if expires is not None:
> key += expires.strftime('%Y%m%d%H%M')
> else:
> year = random.choice(range(2000,2100))
> month = 23
> day = random.choice(range(1,32))
> hour = random.choice(range(1,25))
> minute = random.choice(range(1,60))
> key += "%04d%02d%02d%02d%02d" % (year, month, day, hour, minute)
> 
> if trusted:
> checksum = updcrc(42, key)
> else:   
> checksum = updcrc(0, key)
> key += "%04X" % checksum
> 
> return base64.b64encode(key)
> but it give me this error:
> 
> 
> unpack requires a string argument of length 31
> 
> 
> someone can help me
> 
Next time you report an error, please include the whole traceback, not 
just the exception message. That information is included for a reason.
 
You might also want to print out the value of data in your updcrc 
function, since that is where the problem is occurring.
 
Finally I might point out there is little point to the struct.unpack 
since you don't use its result, but I am guessing this is because your 
algorithm is currently in transition.
 
regards
  Steve
-- 
Steve Holden+1 571 484 6266   +1 800 494 3119
Holden Web LLC  http://www.holdenweb.com/
 
 
--Pièce jointe du message transmise--
From: [EMAIL PROTECTED]
CC: [email protected]
To: [EMAIL PROTECTED]
Date: Fri, 11 Apr 2008 08:36:02 -0500
Subject: Re: String Literal to Blob

Nope. Do not see it. My ugly stupid way works. I guess I will just proceed with 
that and write my howto accordingly.
Victor


On Thu, Apr 10

Re: Learning different languages

2006-03-08 Thread Bo Yang
Rich said :
> Hi,
>
> (this is a probably a bit OT here, but comp.lang seems rather
> desolated, so I'm not sure I would get an answer there. And right now
> I'm in the middle of learning Python anyway so...)
>
> Anyway, my question is: what experience you people have with working
> with different languages at the same time?
>
>   
I am an undergraduate now , majoring in  software engineering .I have 
learn three lanaguages , c/c++ , java , sql . And now I am taking a 
part-time job in a software
corporation which engage in enterprise mail system development . I must 
use php to
maintain their web sever page , while I am using perl script to process 
the mail message.
Meantime , I am very interested in python too .I can't say I am good at 
any one of these,
but I must use all of these at a time .
> Actually I did myself many years ago, on my Commodore machines, where
> I programmed a lot in both basic, assembler and machine code, and
> don't recall I had any problems with handling these parallel. But
> then, they are very different languages, so it's not easy to get their
> syntax etc. mixed up with each other. 
>
>   
Yes , I feel that too . I often use break statement in perl script only 
be warned an syntax error !
> I'm more thinking about Python, PHP, C++, Perl, Euphoria, which are
> languages I'm thinking of learning now. They look much more like each
> other than basic and MC, at places some even share the exact same
> syntax it seems, so your brain might get confused with what language
> you're actually working with?
>
> How is your experience with handling these paralell?. And what would
> you recommend - take one (or perhaps two) at a time, and then continue
> with the next? Or is it OK to go ahead with them all, at once?
>
>   
I think when anybody learn a new language , the most important thing is 
not the syntax of
that language but the builtin functions and the libraries the language 
provide !
My experience is : Learning a language is relatively easy , but being 
good at a language is a far more difficult thing!

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


How can I get the text under the cusor ?

2006-04-06 Thread Bo Yang
Hello ,
I want to develop an application to record some of the best words and
ideas in the web when I surfing with the Firefox or IE .
I would like the application have such a GUI :
1.it appear in the system tray area in the Windows ;
2.whenever I select some words (ether in an IE or MS word),I hope the
application to
display a small window to ask me wether I will add these words to the
application and whether I append some notes to the text .


So , how can I trace the cusor , and the text selected in Python ?



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

A question about the urllib2 ?

2006-04-11 Thread Bo Yang
Hi ,
Recently I use python's urllib2 write a small script to login our
university gateway .
Usually , I must login into the gateway in order to surf the web . So ,
every time I
start my computer , it is my first thing to do that open a browser to
login the gateway !

So , I decide to write such a script , sending some post information to
the webserver
directly to login automatic once the computer is on . And I write the
code below :

urllib2.urlopen(urllib2.Request(url="https://202.113.16.223/php/user_login.php";,
data="loginuser=0312889&password=o127me&domainid=1&refer=1& logintype=
#"))

In the five '#' above , I must submit some Chinese character , but the
urllib2 complain
for the non-ascii characters .

What do you think this ?

Any help will be appreciated very much , thanks in advance !
-- 
http://mail.python.org/mailman/listinfo/python-list

Re: A question about the urllib2 ?

2006-04-12 Thread Bo Yang
Fuzzyman 写道:
> Bo Yang wrote:
>   
>> Hi ,
>> Recently I use python's urllib2 write a small script to login our
>> university gateway .
>> Usually , I must login into the gateway in order to surf the web . So ,
>> every time I
>> start my computer , it is my first thing to do that open a browser to
>> login the gateway !
>>
>> So , I decide to write such a script , sending some post information to
>> the webserver
>> directly to login automatic once the computer is on . And I write the
>> code below :
>>
>> urllib2.urlopen(urllib2.Request(url="https://202.113.16.223/php/user_login.php";,
>> data="loginuser=0312889&password=o127me&domainid=1&refer=1& logintype=
>> #"))
>>
>> In the five '#' above , I must submit some Chinese character , but the
>> urllib2 complain
>> for the non-ascii characters .
>>
>> What do you think this ?
>>
>> 
> I haven't tried this, so I'm guessing :-)
>
> Do you pass in the string to urllib2 as unicode ? If so, try encoding
> it to UTF8 first...
>
> Otherwise you might try escaping it using ``urllib.quote_plus``. (Note
> ``urllib``, *not* ``urllib2``.)
>
> All the best,
>
> Fuzzyman
> http://www.voidspace.org.uk/python/index.shtml
>
>   
>> Any help will be appreciated very much , thanks in advance !
>> 
>
>   
Thank you , I have got it !
I quote the Chinese with urllib.quote() ,
Thanks again !
-- 
http://mail.python.org/mailman/listinfo/python-list

Re: A question about the urllib2 ?

2006-04-14 Thread Bo Yang
Serge Orlov 写道:
> Bo Yang wrote:
>   
>> Hi ,
>> Recently I use python's urllib2 write a small script to login our
>> university gateway .
>> Usually , I must login into the gateway in order to surf the web . So ,
>> every time I
>> start my computer , it is my first thing to do that open a browser to
>> login the gateway !
>>
>> So , I decide to write such a script , sending some post information to
>> the webserver
>> directly to login automatic once the computer is on . And I write the
>> code below :
>>
>> urllib2.urlopen(urllib2.Request(url="https://202.113.16.223/php/user_login.php";,
>> data="loginuser=0312889&password=o127me&domainid=1&refer=1& logintype=
>> #"))
>>
>> In the five '#' above , I must submit some Chinese character , but the
>> urllib2 complain
>> for the non-ascii characters .
>> 
>
> I guess the server expect that a browser is coming from page
> https://202.113.16.223/, so the url should be submitted in the encoding
> of page https://202.113.16.223/ which is gb2312.
>
> urllib2.urlopen(urllib2.Request(url="https://202.113.16.223/php/user_login.php";,
> data=u"loginuser=0312889&password=o127me&domainid=1&refer=1& logintype=
> 写道".encode('gb2312')))
>
> should work
>
>   
Yeah , and it works !
Thank you !

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

Re: Kross - Start of a Unified Scripting Approach

2006-04-15 Thread Bo Yang
RM 写道:
> This is from the new KOffice Announcement.
>
> http://www.koffice.org/announcements/announce-1.5.php
>
> '''This version of KOffice features a start of a unified scripting
> solution called Kross. Kross provides cross-language support for
> scripting (thus its name) and at present supports Python and Ruby.
>
> Kross is easy to include into programs previously lacking scripting
> abilities, and is included in this version as a technology preview. So
> far, only Krita and Kexi are improved by means of the Kross engine.We
> would also like to point out that the API might change in the future
> and expect Kross to be fully integrated into KOffice version 2.0.'''
>
> Interesting isn't it?
>
>   
Just like the .Net vision , Good idea !
-- 
http://mail.python.org/mailman/listinfo/python-list