PYTHONSITEDIR environment variable
Dear all, These days I often find myself using working-env.py, virtual-python.py co. The main reason in most cases is to insulate my working environment so that I can use custom site-package libs together with the default stdlib. PYTHONHOME and/or PYTHONPATH (particularly with python -S) get close, but not quite there, and the above scripts seem a bit over the top. Wouldn't it be possible to support a PYTHONSITEDIR environment variable in site.py for this purpose? I have attached a possible patch. In what follows, if PYTHONSITEDIR is defined, that dir is used as the only source of site-packages, extra paths can easily be added by creating a .pth file in there. A different variable could be used to prepend a sitedir to the list of default sitedirs (similar code without return None). --- /usr/lib/python2.5/site.py 2008-05-29 22:03:04.0 +0100 +++ /tmp/site.py2008-09-09 18:06:42.0 +0100 @@ -167,6 +167,10 @@ def addsitepackages(known_paths): Add site-packages (and possibly site-python) to sys.path +sitedir = os.environ.get('PYTHONSITEDIR') +if sitedir and os.path.isdir(sitedir): +addsitedir(sitedir, known_paths) +return None prefixes = [sys.prefix] if sys.exec_prefix != sys.prefix: prefixes.append(sys.exec_prefix) -- http://mail.python.org/mailman/listinfo/python-list
Re: PYTHONSITEDIR environment variable
On Sep 9, 6:56 pm, Christian Heimes [EMAIL PROTECTED] wrote: I had a similar idea, wrote a PEP and implemented it for 2.6 and 3.0: http://www.python.org/dev/peps/pep-0370/ Christian Christian, I like your pep :), The only thing I would add is that in my experience I often use different working-envs for different projects, so I'd prefer to have a more generic solution as opposed to a single working-env per user. The latter could still be achieved by setting the appropriate environment variable in the user profile. Do you think it would be possible to accommodate for the above in your PEP? -- http://mail.python.org/mailman/listinfo/python-list
Re: PYTHONSITEDIR environment variable
Small variation on the above patch, using 2 environment variables: PYTHONSITEDIR allows for local site-packages (that override system site packages), and PYTHONNOSYSSITES skips system site-packages for clean-room operation (similar to virtual-python.py --no-site- packages). --- /usr/lib/python2.5/site.py 2008-05-29 22:03:04.0 +0100 +++ /tmp/site.py2008-09-09 19:56:44.0 +0100 @@ -167,6 +167,11 @@ def addsitepackages(known_paths): Add site-packages (and possibly site-python) to sys.path +sitedir = os.environ.get('PYTHONSITEDIR') +if sitedir and os.path.isdir(sitedir): +addsitedir(sitedir, known_paths) +if os.environ.get('PYTHONNOSYSSITES'): +return None prefixes = [sys.prefix] if sys.exec_prefix != sys.prefix: prefixes.append(sys.exec_prefix) -- http://mail.python.org/mailman/listinfo/python-list
Re: PYTHONSITEDIR environment variable
On Sep 9, 7:45 pm, Christian Heimes [EMAIL PROTECTED] wrote: ago wrote: The only thing I would add is that in my experience I often use different working-envs for different projects, so I'd prefer to have a more generic solution as opposed to a single working-env per user. The latter could still be achieved by setting the appropriate environment variable in the user profile. Do you think it would be possible to accommodate for the above in your PEP? Isn't PYTHONUSERBASE sufficient for your needs? The env var alters the base directory. I can neither change the PEP nor the implementation at this stage of the development cycle. Python 2.6 and 3.0 are in beta and the API is set in stone. Christian I had missed that, yes I think that PYTHONUSERBASE will do fine! It might be useful though to be able to skip other system site dirs altogether so that only local site packages are used, as in the second patch I sent (see PYTHONNOSYSSITES). Thanks a lot, Ago -- http://mail.python.org/mailman/listinfo/python-list
Generalizing PEP 370 (per user site-packages directory) via .pth files
Dear all, I was reading pep 370, Per user site-packages directory http://www.python.org/dev/peps/pep-0370/, and was wondering if the concept couldn't be generalized by having ways to pass a .pth file as commandline argument and/or via an environment variable (PYTHONPATH could also be used to feed .pth files) and/or via special named files such as ~/.python2.6-user.pth or ./python2.6-local.pth, or possibly even reusing the paths in the distutils configuration files (under [install]). Any path in the above files would be added to sys.path and scanned recursively for other pth files. The system would also load default.pth from a pre-defined location (e.g. /etc/python2.6/ default.pth), which would point to the default site-packages directory. There should also be a mechanism to disable/override default.pth for situations where a clean environment is desired. This would make it easier to setup special testing environments, perform local installations, and allow for file-based deployments (in simple scenarios), without resorting to special tools such as virtual- python, editing site.py and/or requiring sysadmin intervention. It would be particularly useful in environments where there is a clear separation between IT and developer roles. I just started giving some thoughts to the concept and I am not fully aware of the implications and requirements of the proposal, but even if the above turns out to be impractical, I hope that a debate on the topic will be beneficial. Ago -- http://mail.python.org/mailman/listinfo/python-list
enum 0.4.1 not pickle-firendly
I have started using the enum module () , at them moment pickling an object instance that has an EnumValues assigned to one of its attributes pickles the full enumeration, this is not efficient. I am not sure what is the best way around (I do not like the idea of having to use __getstate__/__setstate__ every time), but I guess that a EnumValue should not have a reference to an Enum instance. Maybe a better approach might be to have Enum as a metaclass so that each EnumValue only references the particular Enum class. -- http://mail.python.org/mailman/listinfo/python-list
Very nice python IDE (windows only)
I have just discovered Python Scripter by Kiriakos Vlahos and it was a pleasant surprise. I thought that it deserved to be signalled. It is slim and fairly fast, with embedded graphical debugger, class browser, file browser... If you are into graphical IDEs you are probably going to enjoy it. Windows only unfortunately. http://mmm-experts.com/Products.aspx?ProductId=4 -- http://mail.python.org/mailman/listinfo/python-list
Matplotlib 0.87.2 incompatible with numpy 0.9.8
I was not able to make matplotlib 0.87.2 work with numpy 0.9.8 (there is an error in multiarray.pyd that crashes python). I have noticed that there is a new version 0.87.3, but I was waiting for the windows egg/exe version for python 2.4. I tried to compile from sources with little luck. Does the version 0.87.3 fix the problem? When will the egg/exe be released? -- http://mail.python.org/mailman/listinfo/python-list
Re: Matplotlib 0.87.2 incompatible with numpy 0.9.8
ago wrote: I was not able to make matplotlib 0.87.2 work with numpy 0.9.8 (there is an error in multiarray.pyd that crashes python). I have noticed that there is a new version 0.87.3, but I was waiting for the windows egg/exe version for python 2.4. I tried to compile from sources with little luck. Does the version 0.87.3 fix the problem? When will the egg/exe be released? I posted too soon, the egg for 0.87.3 just appeared on sourceforge... But I need the new version of wxWindwos now... Downloading it now... -- http://mail.python.org/mailman/listinfo/python-list
Re: Very nice python IDE (windows only)
Bernard Lebel wrote: Not me. I'll probably sound pedantic but - the editor text looks awful, changing the editor options had no effect at all - there is no network access of UNC paths other than through File Open and Python Paths. all of my code is on a network location - expanding and collapsing directories is done with a transition effect that drives me nuts You can certainly change the font of the editor (ToolsOptionsEditor OptionsDisplayEditor FontFont), not to be confused with the font of the Gutter which only changes the font inside the left bar (i.e. for line numbers). Admittedly there are not too many fonts to choose from. -- http://mail.python.org/mailman/listinfo/python-list
Re: win32com: how to connect to a specific instance of a running object?
Thanks, after some further digging I hit something... The following seems to do the trick: import win32gui WINDOW_CLASS = 'XLMAIN' WINDOW_TITLE = 'Microsoft Excel - MySpreadsheet.xls' hwindow = win32gui.FindWindow(WINDOW_CLASS,WINDOW_TITLE) Now the next question is: how do I use the window-handle returned? I would like to work with a com object like the one returned by win32com.client.Dispatch('ExcelApplication'). Any hint? PS I could also use win32ui instead win32gui, in this case I get a window object instead of an handle. But I still do not know how to transform it into a nice pythoncom excel object. -- http://mail.python.org/mailman/listinfo/python-list
Re: win32com: how to connect to a specific instance of a running object?
The other approach I tried (as suggested by Tim, thanks) involves browsing the ROT. import pythoncom SPREADSHEET_NAME = r'\MySpreadsheet.xls' lenstr = len(SPREADSHEET_NAME) obj = None rot = pythoncom.GetRunningObjectTable() rotenum = rot.EnumRunning() while True: monikers = rotenum.Next() if not monikers: break ctx = pythoncom.CreateBindCtx(0) name = monikers[0].GetDisplayName(ctx, None); if name[-lenstr:] == SPREADSHEET_NAME: obj = rot.GetObject(monikers[0]) break The obj I get is a PyIUnknown... How do I use it? -- http://mail.python.org/mailman/listinfo/python-list
Re: win32com: how to connect to a specific instance of a running object?
solved, if it can be useful to others here is my code: import pythoncom import win32com.client def getWorkbook(workbookName): lenstr = len(workbookName) workbook = None rot = pythoncom.GetRunningObjectTable() rotenum = rot.EnumRunning() while True: monikers = rotenum.Next() if not monikers: break ctx = pythoncom.CreateBindCtx(0) name = monikers[0].GetDisplayName(ctx, None); if name[-lenstr:] == workbookName: obj = rot.GetObject(monikers[0]) workbook = win32com.client.Dispatch(obj.QueryInterface(pythoncom.IID_IDispatch)) return workbook -- http://mail.python.org/mailman/listinfo/python-list
win32com: how to connect to a specific instance of a running object?
Is it possible to use win32com.client to connect to a specific instance of a running application? In particular I am interested in finding the instance of excel which has a particular spreadsheet opened considering that there might be more instances of excel running at the same time. I need to take a snapshot of the spreadsheet which contains live feeds. -- http://mail.python.org/mailman/listinfo/python-list
Re: Scipy: vectorized function does not take scalars as arguments
I have installed numpy-0.9.6 I haven't tried 0.9.8. -- http://mail.python.org/mailman/listinfo/python-list
Scipy: vectorized function does not take scalars as arguments
Once I vectorize a function it does not acccept scalars anymore. Es def f(x): return x*2 vf = vectorize(f) print vf(2) AttributeError: 'int' object has no attribute 'astype' Is this the intended behaviour? -- http://mail.python.org/mailman/listinfo/python-list
How do I set the __debug__ flag in a win32com server?
Hi all, When I use my win32com.server object from an excel client, the python process running the server always has __debug__==True. When using a python client the __debug__ flag for the server is determined by the client (i.e. if I start the client with 'python -o client.py' then __debug__ == False both for the server and the client). But when running it from excel I cannot do that. Registering the com server with debug=false does not seem to have anything to do with the __debug__ flag. Is it possible to change the flag at all? -- http://mail.python.org/mailman/listinfo/python-list
Re: win32com: error 80004005
I solved it. If someone else is in the same situation... It was due to a defective installation. I reinstalled python and pywin, re-registered the server and everything worked well. -- http://mail.python.org/mailman/listinfo/python-list
win32com: error 80004005
I am trying to make the win32com HelloWorld server work with a VBA client but I get: Run-time error '-2147467259(80004005)': Automation error Unspecified error I googled for the error but the suggested solutions (commenting out _reg_class_spec_ and putting the server on the python path) do not seem to make any difference (to be precise, unless I comment out _reg_class_spec_ I cannot register the server at all) . The server works under the python client but not on excel vba. I also tried to debug with win32traceutil.py but I can only see the trace when using the python client, not with excel. Any hint would be appreciated. ### PYTHON SERVER ### class HelloClass: _reg_desc_ = Python Test COM Server _reg_clsid_ = {91281AFC-25DF-4400-8868-FDBFCA2612A2} _reg_progid_ = Python.HelloHello _public_methods_ = ['Hello'] def __init__(self): pass def Hello(self): return Hello if __name__ == __main__: import win32com.server.register import sys #sys.argv.append(--debug) win32com.server.register.UseCommandLine(HelloClass) ### PYTHON CLIENT (WORKS) ### import win32com.client obj = win32com.client.Dispatch('Python.HelloHello') print obj.Hello() ### VBA CLIENT (ERROR 80004005 WHEN EXECUTING CreateObject) ### Public Sub test() Dim obj As Object Set obj = CreateObject(Python.HelloHello) MsgBox obj.Hello(ago) End Sub -- http://mail.python.org/mailman/listinfo/python-list
Re: Sudoku solver: reduction + brute force
Anton, Do you think it is possible to reduce the set of all possible solutions to a small enough set? I personally doubt it, but IF that was the case an efficient solver could be easily created. In reducing the set of all solutions for instance you could always swap the numbers (3rd axis) so that the first submatrix reads [[1,2,3],[4,5,6],[7,8,9]]. By this we reduced the set of solutions by 362880. You can then always move blocks, columns and rows to fix the following elements (1,4)=4, (4,1)=2, (9,9)=9. Further reductions are still possible, but I do not know how far can this go and if the end result is a small enough set. Just my 2c. -- http://mail.python.org/mailman/listinfo/python-list
Re: Sudoku solver: reduction + brute force
Your reduction-first approach makes short work of them, though. On the other hand, my version probably didn't take as long to write! Well, I started from the reduction-only algorithm so by the time I implemented the brute force solver I already had the code. Anyway the full code is just above 100 lines, it could be shorter (and it was in its first incarnation) but I strived to make it clean and more object oriented following the LinuxJournal article and by avoiding index operations (only contained in the __init__ methods). I like the idea of estimating difficulty... But for a subjective mesure from the point of view of the solver you probably need to give weights to the reduction techniques required to eliminatre cells, since those are the ones used by human beings. Some puzzles might be easy to solve by reduction but difficult to solve by brute force only. In this context for instance, a weight of 1 could be used every time one or more cells are eliminated thanks to Cell.Solve (the easiest approach), a weight of 2 when Cell.Skim was used to eliminate cells (more complex), and a weight of 3 every time BruteForce needs to be invoked (i.e. solutions must be guessed). One thing that my solver lacks is the ability to recognize multiple solutions. It will simply stop at the first admissible one whether it is unique or not. I am not sure what is an efficient way to detect it. -- http://mail.python.org/mailman/listinfo/python-list
Re: Sudoku solver: reduction + brute force
Do you think it is possible to reduce the set of all possible solutions to a small enough set? I personally doubt it, but IF that was the case an efficient solver could be easily created. To expand on the concept, assume for the argument sake that the universe of possible solutions can be reduced to a single grid (it is most likely an unrealistic assumption), an efficient solver (of linear/polinomial complexity) could then be created as follows: 1) Transform the starting puzzle grid to match the unique solution for the available cells 2) Apply inverse transformations to the unique solution to get the solution for the starting puzzle. So we shift the focus from finding the unique value of cells to finding equivalent transformations, which should be an easier problem to tackle. Note that the same process also applies if the universe of possible solutions can be reduced to a small set. For istance in 4X4 grid with 2X2 submatrices it can proven that all possible solutions are equivalent transformations of the following matrix: 1 2 3 4 3 4 1 2 4 1 2 3 2 3 4 1 If we now start with a given grid, what we want is to transform it so that the available cells match the grid above. Assume for instance that the cell (0,0)=3. The first transformation is to swap all the 3 into 1... Take a note of the transformations, apply them in reverse to the above grid and you get the solution. According to Anton the number of possible solutions can be reduced using 1) number swapping, 2) mirroring, 3) blocks/rows/columns swapping. All those operations create equivalent matrices. For a 9X9 grid, this should give a reduction factor = (9!)*(48)*(6^12) minus the number of duplicated combinations given by the methods above. I am not sure how to calculate the number of duplicated combinations and therefore do not know if the result is good enough. As mentioned, I doubt that it is a viable approach, but I find it an intriguing approach nevertheless. -- http://mail.python.org/mailman/listinfo/python-list
Re: Sudoku solver: reduction + brute force
But to inflate my ego beyond the known universe, here is my solver (that solves the avove mentioned grid reasonably fast). I suppose the only difference is that is uses 3, rather than 2, rules to simplify before starting tree-like search. Thanks for the nice problem and the nice post. The issue with my code was not due to the reduction algorithms used. In fact we used exactly the same set of rules, my Cell.solve was equivalent to your Clean1 method and my Cell.skim was equivalent to your Clean3 method (except that my algorithm was only doing for (o1, o2) in ((out1,out2),), but it did not make any difference in most cases). The real problem was due to an external loop inside my solveByBruteForce which was absolutely useless. I fixed that and now everything seems ok. It can solve the mentioned grid in about half the time. You can see my amended code in the link above. -- http://mail.python.org/mailman/listinfo/python-list
Sudoku solver: reduction + brute force
Inspired by some recent readings on LinuxJournal and an ASPN recipe, I decided to revamp my old python hack... The new code is a combination of (2) reduction methods and brute force and it is quite faster than the ASPN program. If anyone is interested I attached the code in http://agolb.blogspot.com/2006/01/sudoku-solver-in-python.html -- http://mail.python.org/mailman/listinfo/python-list
Re: Object default value
I am trying to write a generic DataAttribute class in order to simplify access to object attributes and attached attribute-metadata for end users with little programming experience. Requirement 1: accessing the default value should be easy (using assignment operator, via descriptors like __get__ and __set__). Requirement 2: access to the attribute-metadata should also be as intuitive as possible. Requirement 3: possibly the DataAttribute should also be callable, to return for instance, an history of its values. class DataAttribute(object): value=1 #default old=2 class Obj(object): attr=DataAttribute() #From end user prospective, ideally: obj=Obj() x = obj.attr #x=1 xold = obj.attr.old #xold=2 obj.attr = 3 #obj.attr.value=3 xold = obj.attr.old #xold=1 xhistory = obj.attr(startdate, enddate) #xhistory = [[date1, 4],[date2,5],[date3,6]] xtimestmap = x.attr.timestamp print obj.attr == obj.attr.value #True If I use __get__ then I cannot access the metadata. I could create a separate Obj attribute for each metadata item, but then I would litter the Obj namespace (typical object will have several attributes each with lots of metadata) and potentially create name conflicts. Not to mention that DataAttributes should be attached/deleted on the fly and if each attribute becames a set of attributes this operation is complex (metclasses?). If I use __call__ + __set__ but then I am introducing an asymmetry: x = obj.attr() obj.attr = 2 I could use getters and setters but that is not really convenient nor intuitive -- http://mail.python.org/mailman/listinfo/python-list
Re: Object default value
Is it safe to assume that the OP's next question will be how to invoke functions without the ()'s? To save you the trouble, then answer is 'no'. You probably nailed it, thanks for the answer. I suspected that was the case. I think I'll use __call__ + __set__ -- http://mail.python.org/mailman/listinfo/python-list
Re: Object default value
In fact even IF I could get a default value to work as mentioned, then I would be creating potential name conflicts between the DataAttribute.DefaultValue and the other metadata. I.e. when calling obj.attr.x I could refer to DataAttribute.x or DataAttribute.value.x. It's a no go. -- http://mail.python.org/mailman/listinfo/python-list