strategy pattern and non-public virtual functions
Hi python experts In C++ I can do something like this: class Base { public: void f() { this->f_(); } private: virtual void f_() = 0; }; class Derived : public Base { private: void f_() { // Do something } }; int main() { Derived d; d.f(); } The point of this is that the a number of classes will inherit from Base and only implement a private member function that only will be accessed from the base class public 'f' function. The Base::f() can then perform validation of input/return values, add logging and things like that. The users of the derived classes are unable to bypass this base class function. I've been wanting to do the same thing in python, to make sure that there is no confusion about what function to call. Just translating this code to python won't work, due to the name mangling of private functions: class B(object): def f(self): self.__f() class D(B): def __f(self): pass d = D() d.f() Traceback (most recent call last): File "", line 1, in ? File "", line 3, in f AttributeError: 'D' object has no attribute '_B__f' So my questions are: 1. Is there a "pythonic" way to do what I'm trying to do? 2. Should I be doing this at all? Any thoughts? -- http://mail.python.org/mailman/listinfo/python-list
Re: Core dump revisited
Sheldon wrote: > > Program received signal SIGSEGV, Segmentation fault. > [Switching to Thread 1077321856 (LWP 32710)] > 0x40297b83 in mallopt () from /lib/tls/libc.so.6 > (gdb) bt > #0 0x40297b83 in mallopt () from /lib/tls/libc.so.6 > #1 0x402958ba in free () from /lib/tls/libc.so.6 > #2 0x405e070a in MemoryFreeOrd () at msgppsmodule_area.c:1675 > #3 0x405dae0a in msgppsarea (self=0x0, args=0x4042aa7c, kwargs=0x0) at > msgppsmodule_area.c:328 > #4 0x40073b16 in PyCFunction_Call () from /usr/lib/libpython2.3.so.1.0 > >From my experience, segmentation faults in malloc/free is usually the result of memory corruption. My experience comes mostly from solaris and I realize the memory allocator might work differently in Linux, but that would be my guess. The cause for memory corruption can be writing outside arrays, using deallocated memory or something like that. Under Linux, there are tools like valgrind (free), electric fence (free), purify (not for free last time I checked) and probably a few more, that may help you track down memory corruption bugs. They are often hard to find using just a debugger. HTH -- http://mail.python.org/mailman/listinfo/python-list
C++ extension problem
Hi, I'm having a bit of trouble when writing a python extension. I can't seem to figure out what I did wrong. I tried to make a minimal example, but it's still quite a bit of code. It would be very appreciated if anyone could tell me what I've done wrong. First a short description of what I've done. The extension just wraps a string and the class in it will just hold the string data. A 'get' method is suppsed to just return the string value. There's a python part that will call C/C++ functions in the extension. I've tried using the same approach SWIG has, so the class in the extension doesn't really have any methods, it's all done in methods in the module. This is what it looks like when I try it out (also tried with different python version and compiler): % python Python 2.4.3 (#1, Aug 1 2006, 16:54:29) [GCC 3.4.6] on sunos5 Type "help", "copyright", "credits" or "license" for more information. >>> from example import StringWrapper >>> s = StringWrapper("S") >>> s.get() Traceback (most recent call last): File "", line 1, in ? File "example.py", line 7, in get def get(self): return _example.get(self._s) TypeError: argument 1 must be SillyString, not SillyString Now this is what confuses me: Why does it say that I have the wrong type when it's the same type as it suggests? setup.py file: ### from distutils.core import setup, Extension module1 = Extension('_example', sources = ['_example.cc']) setup (name = 'example', ext_modules = [module1]) example.py file: #!/usr/bin/env python import _example class StringWrapper(object): def __init__(self, value): self._s = _example.new_string(value) def get(self): return _example.get(self._s) and finally, the c/c++ file '_example.cc': (I need to use c++ compiler which means I couldn't use 'staticforward' and the 'cstring is used instead of 'string.h' ) #include "Python.h" #include // forward declaration extern PyTypeObject SillyStringType; typedef struct { PyObject_HEAD char s[21]; } SillyStringObject; PyObject * new_string(PyTypeObject *type, PyObject *args) { char *value = 0; if (!PyArg_ParseTuple(args, "s", &value)) return NULL; SillyStringObject *self = PyObject_NEW(SillyStringObject, &SillyStringType); if (self != NULL) { strncpy(self->s, value, 20); } return (PyObject *)self; } PyObject * get(PyTypeObject *type, PyObject *args) { SillyStringObject *o; if (!PyArg_ParseTuple(args, "O!", SillyStringType, &o)) return NULL; return (PyObject *)PyString_FromString(o->s); } PyMethodDef SillyStringMethods[] = { {"get", (PyCFunction)get, METH_VARARGS, "" }, {"new_string", (PyCFunction)new_string, METH_VARARGS, "" }, {NULL, NULL, 0, NULL}/* Sentinel */ }; PyTypeObject SillyStringType = { PyObject_HEAD_INIT(NULL) 0, /* ob_size */ (char *)"SillyString", /* tp_name */ sizeof(SillyStringObject), /* tp_basicsize */ 0, /* tp_itemsize */ (destructor)0, /* tp_dealloc */ (printfunc)0, /* tp_print */ (getattrfunc)0, /* tp_getattr */ (setattrfunc)0, /* tp_setattr */ (cmpfunc)0, /* tp_compare */ (reprfunc)0,/* tp_repr */ 0, /* tp_as_number */ 0, /* tp_as_sequence */ 0, /* tp_as_mapping */ (hashfunc)0,/* tp_hash */ (ternaryfunc)0, /* tp_call */ (reprfunc)0,/* tp_str */ 0, /* tp_getattro */ 0, /* tp_setattro */ 0, /* tp_as_buffer */ Py_TPFLAGS_DEFAULT, /* tp_flags */ "SillyString class." /* tp_doc */ }; extern "C" { PyMODINIT_FUNC init_example(void) { PyObject* m; SillyStringType.tp_new = PyType_GenericNew; if (PyType_Ready(&SillyStringType) < 0) return; m = Py_InitModule3("_example", SillyStringMethods, "_example module."); Py_INCREF(&SillyStringType); PyModule_AddObject(m, "SillyStringObject", (PyObject *)&SillyStringType); } } -- http://mail.python.org/mailman/listinfo/python-list
Re: script for seconds in given month?
On Apr 16, 6:22 pm, "edfialk" <[EMAIL PROTECTED]> wrote: > Hi, does anyone happen to know of a script that would return the > number of seconds in a month if I give it a month and a year? > something like this might work, it should event handle DST correctly. You could read up on mktime() if you want to make sure. from time import mktime def secondsInMonth(year, month): s1 = mktime((year,month,1,0,0,0,0,0,-1)) s2 = mktime((year,month+1,1,0,0,0,0,0,-1)) return s2-s1 /Matt -- http://mail.python.org/mailman/listinfo/python-list
Re: C++ extension problem
On Apr 16, 9:31 pm, Michael Hoffman <[EMAIL PROTECTED]> wrote: > I can't answer your question since I have no experience > writingextensiontypes. I know this is at least partially a learning exercise > for you, but might I suggest that your time might be better spent > learning Boost.Python instead? It is "aC++library which enables > seamless interoperability betweenC++and the Python programming language." > > http://www.boost.org/libs/python/doc/ > -- > Michael Hoffman Yes, that's good advice. Unfortunately, boost has not been working very well with the tools I'm forced to use (Sun studio). Otherwise I would have started there. /Matt -- http://mail.python.org/mailman/listinfo/python-list
Re: script for seconds in given month?
On Apr 17, 6:41 am, [EMAIL PROTECTED] (Alex Martelli) wrote: > edfialk <[EMAIL PROTECTED]> wrote: > > Hi, does anyone happen to know of ascriptthat would return the > > number ofsecondsin amonthif I give it amonthand a year? > > > My python is a little weak, but if anyone could offer some suggestions > > I think I could handle it myself, or if anyone happens to know of a > >scriptalready written that performs this I would be extremely > > grateful. > > import calendar > > def seconds_in_month(month, year): > nomatter, daysinmonth = calendar.monthrange(year,month) > return daysinmonth * 24 * 60 * 60 > > Alex That works if you define the number of seconds in a month a the number of days * 24 * 60 * 60. If you have DST then that's not always the truth. The difference in seconds (clock time) is according to your example, but the number of seconds that passed during the month differs. Not sure which one was requested in the original post though. -- http://mail.python.org/mailman/listinfo/python-list
Re: script for seconds in given month?
On Apr 16, 7:49 pm, "edfialk" <[EMAIL PROTECTED]> wrote: > Jim: I need years too, basically from 1960-2000. Don't want to > hardcode all those days :) > > Matt: Thanks, I will try this out. > > Paul: I don't believe we need leapseconds. Leap days definitely. > > I'll let you know how Matt's code works. Any other suggestions feel > free to let me know. > > Thanks all! > -Ed There's one thing I forgot to mention: mktime (like most things using the standard C functions) will work reliably from 1970 to 2038 or so. Also, I think posix explicitly says that leap seconds are ignored, so those are not covered. /Matt -- http://mail.python.org/mailman/listinfo/python-list
Re: C++ extension problem
On Apr 16, 11:44 pm, "Martin v. Löwis" <[EMAIL PROTECTED]> wrote: > > Now this is what confuses me: Why does it say that I have the wrong > > type when it's the same type as it suggests? > > When referring to the type, you must *always* form the address of the > type structure, including, but not limited to, the line > > > if (!PyArg_ParseTuple(args, "O!", SillyStringType, &o)) > > HTH, > Martin Yes, that did help. Thanks. I assumed that the compiler would warn me about that kind of problem, but I know better know. :) Still, the error message is somewhat confusing. -- http://mail.python.org/mailman/listinfo/python-list
Singleton
Hi, I've been trying to get some sort of singleton working in python, but I struggle a bit and I thought I'd ask for advice. The first approach was simply to use a module, and every variable in it will be seen by all who import the module. That works in some cases, but not if I have the following structure: one/ __init__.py mod1.py run.py two/ __init__.py mod2.py run.py looks like this: #!/usr/bin/env python import mod1 print mod1.number import two.mod2 print two.mod2.number mod1.py looks like this: import random number=random.randint(0,100) mod2.py looks like this import one.mod1 number = one.mod1.number PYTHONPATH is set to the directory containing the 'one' and 'two' directories. Now when I run the 'run.py', it will print two different numbers. sys.modules tells me that 'mod1' is imported as both 'one.mod1' and 'mod1', which explains the result. Looking at: http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/52558 I find a singleton class, but it has the same problem. If it's imported in two different ways, I have not a singleton but a ... 'doubleton' or something. It is possible to solve this by always importing with the complete path like 'one.mod1', even when inside the 'one' directory, but that's an error waiting to happen. So how do people solve this? Is there an obvious way that I missed? I'm thankful for any advice you might provide. -- http://mail.python.org/mailman/listinfo/python-list
Re: Singleton
Hi again, Just wanted to say thanks. I now see that I misunderstood where the problem was and I got useful information about how importing stuff works. My problem is solved and I'm grateful for your help. -- http://mail.python.org/mailman/listinfo/python-list
Re: Poor python and/or Zope performance on Sparc
On Nov 3, 2:35 pm, joa2212 <[EMAIL PROTECTED]> wrote: > Result: Almost even worse. The application is not scaling at all. > Every time you start a request it is hanging around on one cpu and is > devouring it at about 90-100% load. The other 31 CPUs which are shown > in "mpstat" are bored at 0% load. > Like others already pointed out: Performance is going to suffer if you run something that's single-threaded on a T1000. We've been using the T1000 for a while (but not for python) and it's actually really fast. The performance is way better than the 8 cores imply. In fact, it'll do work that's not too far from 32 times the work of a single thread. Of course, it all depends on what your application is like, but try experimenting a bit with parallelism. Try changing from 4 up to 64 parallel working processes to see what kind of effects you'll get. I'm guessing that whatever caching of persistent data you can do will be a good thing as well as the disk might end up working really hard. -- http://mail.python.org/mailman/listinfo/python-list
Strange problem when using imp.load_module
Hi, I'm having problem when I'm trying to import modules using the imp.load_module function. At the end of this post there's some code I wrote to illustrate the problem. The code istself doesn't make much sense, but what I'm trying to do in reality is allow people to customize an application by writing a small python module. If I find a file in a known location with a known name, I'll import it and use some data or function in it. I ran into problems when writing unit tests for it. What happens when run my code is this: .E == ERROR: test_2 (__main__.Test) -- Traceback (most recent call last): File "test_import.py", line 30, in test_2 getattr(imported, 'y') AttributeError: 'module' object has no attribute 'y' -- Ran 2 tests in 0.015s So it seems that test_2 fails. It does pretty much the same thing as test_1, but with different data. Changing test_2 to check for 'x' instead of 'y' makes it pass, but that's not the result I should get. So it seems that I still have the module that I imported in test_1. So, I'm thinking that I might be dealing with some sort of gc issue. I add the "time.sleep(1)", (commented in the code) and the tests pass. 'y' is found in test_2. Replacing the sleep with a gc.collect() makes the test fail again, so garbage collection doesn't seem to help. I've tried it on python 2.6.1 on windows, 2.5.2 in cygwin and 2.6.1 on solaris with the same results. Could anyone explain what I am missing? Thanks /Mattias Here's the code: import unittest import tempfile, os, imp, time, gc module_file_name=os.path.join(tempfile.gettempdir(), 'my_module.py') def write_module(data): f = open(module_file_name, 'w') f.write(data) f.close() def import_from_file(path): imported_module = imp.load_source(module_file_name, path) return imported_module class Test(unittest.TestCase): def tearDown(self): os.unlink(module_file_name) def test_1(self): module_data='''x=1''' write_module(module_data) imported=import_from_file(module_file_name) getattr(imported, 'x') def test_2(self): # time.sleep(1) module_data='''y=2''' write_module(module_data) imported=import_from_file(module_file_name) getattr(imported, 'y') if __name__ == "__main__": unittest.main() -- http://mail.python.org/mailman/listinfo/python-list
Re: Strange problem when using imp.load_module
Well spotted :) That does seem to be the problem. Adding removal of the .pyc file will make the tests pass. I guess that python doesn't use the higher resolution timestamp you can get from at least Solaris when doing 'stat' on a file. Thanks for the help. /Mattias On Apr 23, 10:28 pm, Arnaud Delobelle wrote: > > My guess is that without the sleep(1), the imp.load_source function will > use the compiled file 'my_module.pyc' created in test_1 instead of > compiling the new 'my_module.py' file. > > > HTH > > -- > Arnaud -- http://mail.python.org/mailman/listinfo/python-list
Re: how to get rid of pyc files ?
On May 24, 3:58 pm, John Machin wrote: > > What problems? Like avoiding having to recompile your .py files makes > your app run too fast? > There are real problems with this. I'm having similar problems when switching between Solaris and Windows. The code is in clearcase, which uses some sort of network file system. In our case, that means that I'll be accessing the same directories from both platforms, so .pyc-files from one platform will be visible when I run the code on the other platform. The .pyc-file will contain a string pointing to the file is was built from. The path to that file will be different under different platforms, so when the string is used, there will be error printouts. At least I think that's the problem, the error printouts contains paths that are only valid on the other platform. I don't have access to those computers atm, so I can't show the exact message. The only solution I've seen is to make sure to clean up the .pyc files each time I switch platform. -- http://mail.python.org/mailman/listinfo/python-list
Re: how to get rid of pyc files ?
On May 25, 12:08 am, Dave Angel wrote: > > Is Clearcase still around? I hope it works better than it did in 1992. > I don't know how it worked back then, but if it's worse than today, I don't know how they ever managed to get people to use it. I'm not a fan and I don't have a choice about using it :) > Somebody else has already pointed out that you can tell Python not to > create thosefiles(during your development stages). > Yes, that's probably the best way to sort out the problems. It would of course be nice if python could detect that these files aren't usable and just regenerate them. I realise it might not be a trivial feature to implement though. > > Perhaps Clearcase supports some form of "exclusion" parameter, wherein > you say not to do version control onfileswith certain patterns, like .pyc They're not actually version controlled, but "view private" as someone described. This means I'll see them whenever I'm using the view they were created in. -- http://mail.python.org/mailman/listinfo/python-list
Re: how to get rid of pyc files ?
On May 25, 12:07 am, John Machin wrote: > "switching" scarcely seems to be the right description. You appear to > be running the same code from one repository simultaneously available > to two different platforms. > > Try this: Instead of running your code straight from your repository, > set up a run-time "launch pad" in an appropriate place for each > platform. When there is a new release of your app, install it in each > launchpad. For releases, that's a good enough idea. But while developing and wanting to run units tests, it's sort of a big overhead. Especially since I tend to run the unit test quite frequently. > > I'm a bit lost here ... your clearcase repository uses a mysterious > "some sort of network file system" to which you have access > permissions which allow you to delete individualfiles?? Apparently I don't explain things well. :) Others have explained it better in this thread or you can have a look at http://en.wikipedia.org/wiki/Rational_ClearCase. -- http://mail.python.org/mailman/listinfo/python-list