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?
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 s
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 it
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 ca
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 b
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
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
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 dicti
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 re
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
...
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
s
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 e
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
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 d
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 pa
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, '
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
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
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 Pyt
>>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
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(),
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 scri
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
[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 wit
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
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 pyt
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 functio
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 lengt
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
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 p
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 conf
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 argum
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
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. Si
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
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 i
> 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
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 con
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], readonl
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 re
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) un
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.p
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
d
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 w
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 availab
> 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. Af
> 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
>
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 a
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
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
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
functio
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 in
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
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
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.0002
> 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
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-calcu
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 ar
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
59 matches
Mail list logo