Re: Optimizing if statement check over a numpy value

2015-07-23 Thread Jeremy Sanders
Heli Nix wrote:

> Is there any way that I can optimize this if statement.

Array processing is much faster in numpy. Maybe this is close to what you 
want

import numpy as N
# input data
vals = N.array([42, 1, 5, 3.14, 53, 1, 12, 11, 1])
# list of items to exclude
exclude = [1]
# convert to a boolean array
exclbool = N.zeros(vals.shape, dtype=bool)
exclbool[exclude] = True
# do replacement
ones = vals==1.0
# Note: ~ is numpy.logical_not
vals[ones & (~exclbool)] = 1e-20

I think you'll have to convert your HDF array into a numpy array first, 
using numpy.array().

Jeremy


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


Re: A new module for performing tail-call elimination

2015-07-16 Thread Jeremy Sanders
Robin Becker wrote:

> I believe the classic answer is Ackermann's function
> 
> http://demonstrations.wolfram.com/RecursionInTheAckermannFunction/
> 
> which is said to be not "primitive recursive" ie cannot be unwound into
> loops; not sure whether that implies it has to be recursively defined or
> can perhaps be broken down some other way. For more eye-glazing

But am I right in thinking that TCO doesn't work for Ackermann's function, 
at least not as it's written down in the above page?

J.


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


Re: Python Random vs. Cython C Rand for Dice Rolls

2015-06-08 Thread Jeremy Sanders
C.D. Reimer wrote:

> Is there something in the Cython code that I need to change and/or find
> a better C random number generator?

This may not be helpful, but numpy is pretty helpful for this sort of thing:

import numpy
import numpy.random

a=numpy.random.randint(1,6,5000)
b=numpy.random.randint(1,6,5000)

numpy.bincount(a+b-1)

array([  0, 1999229, 4000369, 5999372, 7999232, 9998769, 8003430,
   5998538, 4001160, 101])

This takes a few seconds on my system.

Jeremy


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


Re: Python, C++ interaction

2014-12-04 Thread Jeremy Sanders
Michael Kreim wrote:

> What are you using to wrap C++ classes for Python?

I'm using SIP, as it fits nicely with my PyQt user interface. 
http://www.riverbankcomputing.com/software/sip/intro

It's a pretty flexible and fast way of wrapping C++ and C.

If you want to pass numpy arrays and such, it requires a bit more work, 
however.

Jeremy


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


Re: checking if two things do not equal None

2014-03-31 Thread Jeremy Sanders
contact.tri...@gmail.com wrote:

> if (a, b) != (None, None):
> or
> if a != None != b:
> 
> Preference? Pros? Cons? Alternatives?

I couldn't see anyone else give this, but I like

if None not in (a, b):
 pass

Jeremy


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


Re: GUI:-please answer want to learn GUI programming in python , how should i proceed.

2013-12-16 Thread Jeremy Sanders
Michael Torrie wrote:

> I think PyQt is slowly being pushed aside in favor of PySide, which is
> more license-friendly for use in closed or open projects.  I would
> recommend using PySide unless PyQt is a requirement for your project.

That's not the impression I get from the PySide mailing lists. Work seems 
slow now everyone is a volunteer. For example, Qt 5 is not yet supported 
(there's no effort towards this according to the mailing list) and bugs seem 
to take a long time to be fixed. PyQt support is much better, even when I'm 
using it for a free project.

Jeremy



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


Re: squeeze out some performance

2013-12-06 Thread Jeremy Sanders
Robert Voigtländer wrote:

> I try to squeeze out some performance of the code pasted on the link
> below. http://pastebin.com/gMnqprST
> 
> The code will be used to continuously analyze sonar sensor data. I set
> this up to calculate all coordinates in a sonar cone without heavy use of
> trigonometry (assuming that this way is faster in the end).
> 
> I optimized as much as I could. Maybe one of you has another bright idea
> to squeeze out a bit more?

This sort of code is probably harder to make faster in pure python. You 
could try profiling it to see where the hot spots are. Perhaps the choice of 
arrays or sets might have some speed impact.

One idea would be to use something like cython to compile your python code 
to an extension module, with some hints to the types of the various values.

I would go down the geometry route. If you can restate your problem in terms 
of geometry, it might be possible to replace all that code with a few numpy 
array operations.

e.g. for finding pixels in a circle of radius 50
import numpy as np
radiussqd = np.fromfunction(lambda y,x: (y-50)**2+(x-50)**2, (100,100) )
all_y, all_x = np.indices((100,100))
yvals = all_y[radiussqd < 50**2]

Jeremy


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


Re: Multiple scripts versus single multi-threaded script

2013-10-04 Thread Jeremy Sanders
Roy Smith wrote:

> Threads are lighter-weight.  That means it's faster to start a new
> thread (compared to starting a new process), and a thread consumes fewer
> system resources than a process.  If you have lots of short-lived tasks
> to run, this can be significant.  If each task will run for a long time
> and do a lot of computation, the cost of startup becomes less of an
> issue because it's amortized over the longer run time.

This might be true on Windows, but I think on Linux process overheads are 
pretty similar to threads, e.g.
http://stackoverflow.com/questions/807506/threads-vs-processes-in-linux

Combined with the lack of a GIL-conflict, processes can be pretty efficient.

Jeremy


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


Re: semicolon at end of python's statements

2013-09-05 Thread Jeremy Sanders
Chris Angelico wrote:

> Because s/he thought it made for better code, or as a joke? Usually I
> see this sort of thing as the latter...

http://oldhome.schmorp.de/marc/bournegol.html
http://utcc.utoronto.ca/~cks/space/blog/programming/BourneGol

Jeremy


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


Re: RE Module Performance

2013-07-25 Thread Jeremy Sanders
wxjmfa...@gmail.com wrote:

> Short example. Writing an editor with something like the
> FSR is simply impossible (properly).

http://www.gnu.org/software/emacs/manual/html_node/elisp/Text-Representations.html#Text-Representations

"To conserve memory, Emacs does not hold fixed-length 22-bit numbers that are 
codepoints of text characters within buffers and strings. Rather, Emacs uses a 
variable-length internal representation of characters, that stores each 
character as a sequence of 1 to 5 8-bit bytes, depending on the magnitude of 
its codepoint[1]. For example, any ASCII character takes up only 1 byte, a 
Latin-1 character takes up 2 bytes, etc. We call this representation of text 
multibyte.

...

[1] This internal representation is based on one of the encodings defined by 
the Unicode Standard, called UTF-8, for representing any Unicode codepoint, but 
Emacs extends UTF-8 to represent the additional codepoints it uses for raw 8-
bit bytes and characters not unified with Unicode.

"

Jeremy


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


Re: interactive plots

2011-07-06 Thread Jeremy Sanders
Mihai Badoiu wrote:

> How do I do interactive plots in python?  Say I have to plot f(x) and g(x)
> and I want in the plot to be able to click on f and make it disappear. 
> Any python library that does this?

You could try veusz, which is a python module and plotting program combined. 
You can also embed it in a PyQt program.

Jeremy


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


Re: Making Line Graphs

2011-02-19 Thread Jeremy Sanders
spam head wrote:

> Does anybody have any recommendations for a good program from
> generating these simple graphs?

Have a look at Veusz, written in python: http://home.gna.org/veusz/

(I am the lead author).

Jeremy


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


Re: Saving (unusual) linux filenames

2010-08-31 Thread Jeremy Sanders
amfr...@web.de wrote:

> i have a script that reads and writes linux paths in a file. I save the
> path (as unicode) with 2 other variables. I save them seperated by "," and
> the "packets" by newlines. So my file looks like this:
> path1, var1A, var1B
> path2, var2A, var2B
> path3, var3A, var3B

If you're generating the file and it is safe to do so (you're not getting 
the data from the internet), you could use repr((path1, v1, v2)) to save the 
line to the file and eval to interpret back the tuple.

Alternatively you could use // as a separator, making sure that you replace 
multiple slashes in the path which a single slash (which are equivalent).

Jeremy


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


Re: Multiline regex

2010-07-21 Thread Jeremy Sanders
Brandon Harris wrote:

> I'm trying to read in and parse an ascii type file that contains
> information that can span several lines.
> Example:

What about something like this (you need re.MULTILINE):

In [16]: re.findall('^([^ ].*\n([ ].*\n)+)', a, re.MULTILINE)
Out[16]: 
[('createNode animCurveTU -n "test:master_globalSmooth";\nsetAttr ".tan" 
9;\nsetAttr -s 4 ".ktv[0:3]"  101 0 163 0 169 0 201 0;\nsetAttr -s 4 
".kit[3]"  10;\nsetAttr -s 4 ".kot[3]"  10;\n',
  'setAttr -s 4 ".kot[3]"  10;\n'),
 ('createNode animCurveTU -n "test:master_res";\nsetAttr ".tan" 9;\n
setAttr ".ktv[0]"  103 0;\nsetAttr ".kot[0]"  5;\n',
  'setAttr ".kot[0]"  5;\n'),
 ('createNode animCurveTU -n "test:master_faceRig";\nsetAttr ".tan" 9;\n
setAttr ".ktv[0]"  103 0;\n',
  'setAttr ".ktv[0]"  103 0;\n')]

If you blocks start without a space and subsequent lines with a space.

Jeremy


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


Re: GUIs - A Modest Proposal

2010-06-13 Thread Jeremy Sanders
lkcl wrote:

>  * in neither gtk nor qt does there exist an "auto-layout" widget
> that's equivalent to putting some  DOM objects into a ,
> to "flow" widgets that wrap around.  yes, you can put words into a
> Label and get them to flow, but not _widgets_.

I'm pretty sure in PyQt4 that you can derive your own layout class from 
QLayout to get what you want. No C++ is required. You can easily extend PyQt 
without using C++.

There is even an example in the PyQt examples which does something similar 
to what you want: see examples/layouts/flowlayout.py

Personally I find the Qt layout to be much better than anything provided by 
CSS and HTML. Personally I'd rather be writing complex C++ templates that 
those, though it does give you a feeling of achievement when you get what 
you want with CSS.

Jeremy

-- 
Jeremy Sanders
http://www.jeremysanders.net/
-- 
http://mail.python.org/mailman/listinfo/python-list


ANN: Veusz 1.6

2010-01-26 Thread Jeremy Sanders
Veusz 1.6
-
Velvet Ember Under Sky Zenith
-
http://home.gna.org/veusz/

Veusz is Copyright (C) 2003-2010 Jeremy Sanders 
Licenced under the GPL (version 2 or greater).

Veusz is a Qt4 based scientific plotting package. It is written in
Python, using PyQt4 for display and user-interfaces, and numpy for
handling the numeric data. Veusz is designed to produce
publication-ready Postscript/PDF/SVG output. The user interface aims
to be simple, consistent and powerful.

Veusz provides a GUI, command line, embedding and scripting interface
(based on Python) to its plotting facilities. It also allows for
manipulation and editing of datasets. Data can be captured from
external sources such as internet sockets or other programs.

Changes in 1.6:
 * User defined constants, functions or external Python imports can be
   defined for use when evaluating expressions.
 * Import descriptor is much more tolerant of syntax, e.g. "x,+- y,+,-" can
   now be specified as "x +- y + -".
 * New SVG export (PyQt >= 4.6). Supports clipping and exports text
   as paths for full WYSIWYG.
 * Dataset names can now contain any character except "`". Names containing
   non-alphanumeric characters can be quoted in expressions `like so`*1.23
 * Widget names can contain any character except "/"
 * A transparency dataset can be provided to specify the per-pixel
   transparency of the image widget.
 * A polygon widget has been added.
 * There is a new option to place axis ticks outside the plot (outer ticks
   setting on axis widget)
 * Several new line styles have been added.
 * Several new plotting markers have been added.
 * The capture dialog can optionally retain the last N values captured.

Minor changes:
 * Use of flat cap line style for plotting error bars for exactness.
 * Add fixes for saving imported unicode text.
 * Fix image colors for big endian systems (e.g. Mac PPC).
 * Add boxfill error bar style, plotting errors as filled boxes.
 * Positive and negative error bars are forced to have the correct sign.

Features of package:
 * X-Y plots (with errorbars)
 * Line and function plots
 * Contour plots
 * Images (with colour mappings and colorbars)
 * Stepped plots (for histograms)
 * Bar graphs
 * Plotting dates
 * Fitting functions to data
 * Stacked plots and arrays of plots
 * Plot keys
 * Plot labels
 * Shapes and arrows on plots
 * LaTeX-like formatting for text
 * EPS/PDF/PNG/SVG/EMF export
 * Scripting interface
 * Dataset creation/manipulation
 * Embed Veusz within other programs
 * Text, CSV and FITS importing
 * Data can be captured from external sources

Requirements for source install:
 Python (2.4 or greater required)
   http://www.python.org/
 Qt >= 4.3 (free edition)
   http://www.trolltech.com/products/qt/  
 PyQt >= 4.3 (SIP is required to be installed first)
   http://www.riverbankcomputing.co.uk/pyqt/
   http://www.riverbankcomputing.co.uk/sip/
 numpy >= 1.0
   http://numpy.scipy.org/

Optional:
 Microsoft Core Fonts (recommended for nice output)
   http://corefonts.sourceforge.net/
 PyFITS >= 1.1 (optional for FITS import)
   http://www.stsci.edu/resources/software_hardware/pyfits
 pyemf >= 2.0.0 (optional for EMF export)
   http://pyemf.sourceforge.net/
 For EMF and better SVG export, PyQt >= 4.6 or better is
   required, to fix a bug in the C++ wrapping

For documentation on using Veusz, see the "Documents" directory. The
manual is in PDF, HTML and text format (generated from docbook). The
examples are also useful documentation.

Issues with the current version:

 * Due to Qt, hatched regions sometimes look rather poor when exported
   to PostScript, PDF or SVG.

 * Due to a bug in Qt, some long lines, or using log scales, can lead
   to very slow plot times under X11. It is fixed by upgrading to
   Qt-4.5.1 (or using a binary). Switching off antialiasing in the options
   may help.

If you enjoy using Veusz, I would love to hear from you. Please join
the mailing lists at

https://gna.org/mail/?group=veusz

to discuss new features or if you'd like to contribute code. The
latest code can always be found in the SVN repository.

Jeremy Sanders

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


Re: os.system function

2010-01-12 Thread Jeremy Sanders
Zabin wrote:

> Thanks for the pointersi had a look around and found the site:
> 
http://www.microsoft.com/resources/documentation/windows/xp/all/proddocs/en-
us/xcopy.mspx?mfr=true
> 
> to disable the prompt- i needed to include /y as below:
>  os.system ('xcopy /s %s %s /y ' % (dirExe, dirname_new))
> 
> 
> and just wondering- whats the drawback of using os.system() command

- It won't work across different platforms (unix, mac, windows)
- Spaces or special characters in the filename will mess up the command line 
and can lead to huge security flaws in your program.
- It's inefficient as you have to start a new program to do the work (slow 
on windows)
- Error handling from the xcopy process will not be easy

-- 
Jeremy Sanders
http://www.jeremysanders.net/
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Psyco on 64-bit machines

2009-11-16 Thread Jeremy Sanders
Russ P. wrote:

> Would it make sense to compile Python in the 32-bit compatibility mode
> so I can use Psyco? What would I lose in that mode, if anything?
> Thanks.

You won't be able to access large amounts of memory in 32 bit mode. Also, 
the x86-64 mode has more CPU registers than x86 mode, so Python will 
typically run faster in 64 bit mode (this is more pronounced in AMD 
processors, in my experience).

It will depend on your application whether 32 bit mode plus Psyco is faster 
than 64 bit mode.

Jeremy

-- 
Jeremy Sanders
http://www.jeremysanders.net/
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: PyQt4 - remember widget positions

2009-10-22 Thread Jeremy Sanders
nusch wrote:

> Is there any simple command which allows me to save position of all
> windows:  QMainWindow, QDialogs and qdockwidgets with their sizes,
> dock state and positions ? Or do I need to store those values
> manually, how can I do it fast?

You can use saveState() from QMainWindow to save the dockwidget geometries. 
I save the size and position of the main window separately and restore it 
with resize() and move().

You need to make sure all your toolbars and dockwidgets have unique object 
names for saveState to work.

Jeremy

-- 
Jeremy Sanders
http://www.jeremysanders.net/
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Python code for testing well parenthesized expression

2009-07-14 Thread Jeremy Sanders
Diez B. Roggisch wrote:

> Yep, you are:
> 
> "(((("
> 
> is certainly not "well parenthized".

Thanks for that!

-- 
Jeremy Sanders
http://www.jeremysanders.net/

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


Re: Python code for testing well parenthesized expression

2009-07-14 Thread Jeremy Sanders
candide wrote:

> I'm trying to implement in Python a function testing if an expression is
> well parenthesized. For instance the expression "zx4er(1(er(Yy)ol)ol)ik"
> is correctly parenthesized but this one "zx(4er(1(er(Yy)ol)ol)ik" is not.
> 
> My code follows at the end.
> 
> If you have a better algorithm or a better Python code (I'm a beginner in
> the Python world), don't hesitate ...

Don't you want to just test that the number of "("s equals the number of 
")"s or am I missing the point?

>>> a='aAAA(bbb(cc)))'
>>> a.count('(') == a.count(')')

Jeremy

-- 
Jeremy Sanders
http://www.jeremysanders.net/

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


Re: Package for fast plotting of many data points in Python?

2009-07-10 Thread Jeremy Sanders
tt-industries wrote:

> Hi,
> 
> I am programming a oscilloscope module in Python. For this reason, I
> want to plot very many data points as fast as possible. This can be
> more than 100 000 at once. So far I have been using the ploting module
> of wxPython. However, it becomes unstable for more than 25000 points.
> Can someone recommend me a faster plotting library? It would be really
> cool if one could embed this in wxPython. If someone has an idea I
> would be very glad about answer.

Veusz can plot a line with that many points with that many points in a 
couple of seconds on my system. It's a bit faster without antialiasing, 
slower if you want to actually plot markers at each position.

Jeremy

-- 
Jeremy Sanders
http://www.jeremysanders.net/

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


Re: Status of Python threading support (GIL removal)?

2009-06-21 Thread Jeremy Sanders
Jesse Noller wrote:

> Sorry, you're incorrect. I/O Bound threads do in fact, take 
advantage
> of multiple cores.

I don't know whether anyone else brought this up, but it looks 
like Python has problems with even this form of threading

http://www.dabeaz.com/python/GIL.pdf

It's certainly a very interesting read if you're interested in 
this subject.

-- 
Jeremy Sanders
http://www.jeremysanders.net/

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


Re: Once again, comparison wxpython with PyQt

2009-06-19 Thread Jeremy Sanders
Hans Müller wrote:

> Thanks for all your informative replies.
> 
> If I understand you right, for a commercial, closed source program I
> only need a commercial PyQt license for ~ 500€ ?

Why not ask the guys at riverbankcomputing?
 http://www.riverbankcomputing.co.uk/commercial/pyqt

This page
 http://www.riverbankcomputing.co.uk/software/pyqt/license

Says "The commercial version of PyQt can be used with both the commercial 
and LGPL versions of Qt."

> As far as I know I also need a Qt Licenses which is ~3500€ per OS.

Not true, now Qt is licensed under the LGPL.

You have to abide by the LGPL, however, which means that the user has to be 
able to relink a modified form of the library, Qt, with your application 
should they wish. You should check whether the LGPL is appropriate for the 
way you want to ship your program.

Jeremy

-- 
Jeremy Sanders
http://www.jeremysanders.net/

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


Re: subprocess.Popen inheriting

2008-12-17 Thread Jeremy Sanders
Aaron Brady wrote:

> I thought so too.  The web seems to say that on Linux they are, and on
> Windows, you need to call DuplicateHandle for it.

I hit this problem - it looks like pipes aren't very versatile on Windows.
There's also the complicating factor that the handles in windows aren't the
same as the file numbers that Python uses, so you have to convert between
them.

It would be nice if Python created pipes that are properly inheritable by
default by child processes, as they're mostly used for IPC.

It was so painful that I converted my code to use sockets instead, which
seem much more portable between Windows and Unix (though you don't get to
use socketpair and AF_UNIX in Windows).

Jeremy

-- 
Jeremy Sanders
http://www.jeremysanders.net/
--
http://mail.python.org/mailman/listinfo/python-list


Re: Best way to dynamically get an attribute from a module from within the same module

2008-12-01 Thread Jeremy Sanders
Rafe wrote:

> I guess, in the end, I'd use getattr() because it feels more pythonic,
> and more basic. I got pretty deep in to learning python before I had
> to learn what the globals() dict could do for me.

Why not store your individual classes inside another class or keep them in a
dict? That would be clearer, would not mess around with global namespace,
and more pythonic IMHO.

Jeremy

-- 
Jeremy Sanders
http://www.jeremysanders.net/
--
http://mail.python.org/mailman/listinfo/python-list


Re: calling python scripts as a sub-process

2008-11-19 Thread Jeremy Sanders
Dan Upton wrote:

> I think when I came across this error, I added shell=True, e.g.
> 
> sub1 = subprocess.Popen(command, shell=True)

That's really papering over the bug. You need to have the parameters
separately, including the name of the program, separately in the list. You
need to remove any shell quoting you may use on the unix/dos command line.

Jeremy

-- 
Jeremy Sanders
http://www.jeremysanders.net/
--
http://mail.python.org/mailman/listinfo/python-list


Re: calling python scripts as a sub-process

2008-11-19 Thread Jeremy Sanders
Catherine Moroney wrote:

> I have one script (Match1) that calls a Fortran executable as a
> sub-process, and I want to write another script (Match4) that
> spawns off several instances of Match1 in parallel and then waits
> until they all finish running.  The only way I can think of doing this
> is to call it as a sub-process, rather than directly.
> 
> I'm able to get Match1 working correctly in isolation, using the
> subprocess.Popen command, but calling an instance of Match1 as a
> subprocess spawned from Match4 isn't working.
> 
> The command (stored as an array of strings) that I'm executing is:
> 
> ['python ../src_python/Match1.py ',
> '--file_ref=MISR_AM1_GRP_ELLIPSOID_GM_P228_O003571_BF_F03_0024.hdf ',
> '--file_cmp=MISR_AM1_GRP_ELLIPSOID_GM_P228_O003571_DF_F03_0024.hdf ',
> '--block_start=62 ', '--block_end=62 ', '--istep=16 ', "--chmetric='M2'
> ", "--use_textid='true '"]
> 

If you want to avoid going by the shell, and you *should* for security
reasons, you need to have each of your arguments separately in the list
without the shell quoting and extra spaces, i.e.

['python', '../src_python/Match1.py',
 '--file_ref=.hdf', '--file_cmp=.hdf',
 '--block_start=xx', '--block_end=62', '--istep=16', '--chmetric=M2',
 '--use_texid=true']

Jeremy

-- 
Jeremy Sanders
http://www.jeremysanders.net/
--
http://mail.python.org/mailman/listinfo/python-list


Re: os.pipe and subprocess under Windows

2008-11-17 Thread Jeremy Sanders
Jeremy Sanders wrote:

> Hi - I have some code which works under linux. It starts a remote python
> process using subprocess and communicates to it via a pipe created by
> os.pipe. As far as I understand, child processes should inherit file
> descriptors from the parent if close_fds=False on the suprocess.Popen
> command line.

Hmm... examining the code for os.pipe in posixmodule.c, it looks like pipes
are create specifically to be non-inheritable in Windows. I can't see why
you would want a non-inheritable pipe, so I would call this a bug. 

I suppose I could try this trick from subprocess.py to make the pipes
inheritable:

def _make_inheritable(self, handle):
"""Return a duplicate of handle, which is inheritable"""
return DuplicateHandle(GetCurrentProcess(), handle,
   GetCurrentProcess(), 0, 1,
   DUPLICATE_SAME_ACCESS)

Pretty nasty to have to do this though, and I would have to add a win32api
dependency, or hack around with the _subprocess module.

Jeremy

-- 
Jeremy Sanders
http://www.jeremysanders.net/
--
http://mail.python.org/mailman/listinfo/python-list


Re: os.pipe and subprocess under Windows

2008-11-17 Thread Jeremy Sanders
Lawrence D'Oliveiro wrote:

> <http://docs.python.org/library/subprocess.html>:
> 
> If close_fds is true, all file descriptors except 0, 1 and 2 will be
> closed before the child process is executed. (Unix only). Or, on
> Windows, if close_fds is true then no handles will be inherited by the
> child process.
> 
> Windows has no fork(2).

Yes - I saw that - thanks. This suggests that as I have used
closed_fds=False, then the child process will inherit the handles under
Windows, which it doesn't seem to. This documentation looks wrong to me.

I know Windows has no fork - that's why I used subprocess. This MSDN page
suggests you you need to do something with SetHandleInformation to get them
to be inherited. Doesn't subprocess do that?

http://msdn.microsoft.com/en-us/library/ms682499(VS.85).aspx

-- 
Jeremy Sanders
http://www.jeremysanders.net/
--
http://mail.python.org/mailman/listinfo/python-list


os.pipe and subprocess under Windows

2008-11-17 Thread Jeremy Sanders
Hi - I have some code which works under linux. It starts a remote python
process using subprocess and communicates to it via a pipe created by
os.pipe. As far as I understand, child processes should inherit file
descriptors from the parent if close_fds=False on the suprocess.Popen
command line.

This code doesn't work under Window, but gives "bad file descriptor" when
trying to read from the pipe in the child process. I have some example code
which fails. It consists of two files, master.py and slave.py. The file
descriptor for reading from the pipe is passed as a argument to slave.py.


#!/usr/bin/env python
# This is master.py

import os
import os.path
import sys
import subprocess

def runMaster():
# create pipe to communicate with remote process
rpipe, wpipe = os.pipe()

# start remote process
cmdline = [sys.executable,
   os.path.join( os.path.dirname(
os.path.abspath(__file__)), 'slave.py' ),
str(rpipe) ]

remote = subprocess.Popen(cmdline, shell=False, bufsize=0,
  close_fds=False)

# send text to remote process via pipe
os.write(wpipe, 'hi there$')

# wait until remote exit
remote.wait()

if __name__ == '__main__':
runMaster()

--

# This is slave.py

import sys
import os

def runSlave(fd):
"""Copy text to stderr from file descriptor until a $ symbol."""
while True:
intext = os.read(fd, 1)
if intext == '$':
break
elif intext:
# write text from pipe to stderr
sys.stderr.write('* %s\n' % intext)

if __name__ == '__main__':
fd = int(sys.argv[1])
runSlave(fd)

---

Does anyone have any ideas how to get this to work under Windows? Is it
correct code under unix?

Thanks

Jeremy

-- 
Jeremy Sanders
http://www.jeremysanders.net/
--
http://mail.python.org/mailman/listinfo/python-list


Re: indirectly addressing vars in Python

2008-10-01 Thread Jeremy Sanders
Ross wrote:

>  >>> myList[1]= myList[1]+1

The problem is this makes myList[1] point to a new integer, and not the one
that peas points to.

Python 2.5.1 (r251:54863, Jul 10 2008, 17:25:56)
[GCC 4.1.2 20070925 (Red Hat 4.1.2-33)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> oats=[1]
>>> peas=[6]
>>> mylist = [oats, peas]
>>> mylist[1][0] = mylist[1][0]+1
>>> mylist
[[1], [7]]
>>> peas
[7]

This is because integers are immutable, but lists are mutable.

-- 
Jeremy Sanders
http://www.jeremysanders.net/
--
http://mail.python.org/mailman/listinfo/python-list


Re: Getting references to obect instances into a list

2008-08-27 Thread Jeremy Sanders
[EMAIL PROTECTED] wrote:

> I will read the article you told me to but first, please, have a look
> at this snippet:
> 
>>>> m = [2,3,4]
>>>> p = ['a','b','c']
>>>> q = [m,p]
>>>> q
> [[2, 3, 4, 'a', 'b', 'c'], ['a', 'b', 'c']]
>>>> del p
>>>> q
> [[2, 3, 4, 'a', 'b', 'c'], ['a', 'b', 'c']]
>>>>
> 
> 
> How come q is not updated after I deleted p?

q still holds a reference to p. Maybe you are after weak references. Have a
look at the documentation for the weakref module in the standard library.
Unfortunately you cannot store weak references to lists directly:

In [5]: class foo(object):
   ...: def __init__(self, lst):
   ...: self.lst = lst
In [6]: m = foo([2,3,4])
In [7]: p = foo(['a','b','c'])
In [8]: import weakref
In [20]: q = [weakref.proxy(m), weakref.proxy(p)]
In [23]: q[0].lst, q[1].lst
Out[23]: ([2, 3, 4], ['a', 'b', 'c'])
In [24]: del p
In [27]: q[1].lst
gives a reference error

-- 
Jeremy Sanders
http://www.jeremysanders.net/
--
http://mail.python.org/mailman/listinfo/python-list


Re: random numbers according to user defined distribution ??

2008-08-08 Thread Jeremy Sanders
Alex wrote:

> I wonder if it is possible in python to produce random numbers
> according to a user defined distribution?
> Unfortunately the random module does not contain the distribution I
> need :-(

Have you looked at the numpy random number module? It seems to have quite a
lot of distributions. See help(numpy.random).

Jeremy

-- 
Jeremy Sanders
http://www.jeremysanders.net/
--
http://mail.python.org/mailman/listinfo/python-list


Re: how to proccess the blank in the path on linux

2008-05-21 Thread Jeremy Sanders
A.T.Hofkamp wrote:

> Escape the space to prevent the shell from interpreting it as a word
> seperator. This of course also holds for all other shell meta characters,
> such as * [ ] \ > < & and a few others I probably have forgotten.

If the command was useful (unlike cd), it might be better to use subprocess
to launch it so that you don't need the escaping:

subprocess.call(['ls', '8000 dir'])

This avoids using the shell.

-- 
Jeremy Sanders
http://www.jeremysanders.net/
--
http://mail.python.org/mailman/listinfo/python-list


Re: Reversing a dict?

2008-05-06 Thread Jeremy Sanders
[EMAIL PROTECTED] wrote:

> Hi - further to my earlier query regarding partial matches (which with
> all your replies enabled me to advance my understanding, thanks), I
> now need to reverse a dict.

There is no guaranteed order to the items stored in a dictionary. They can
and will move around as the dict is modified. Have a look at
diveintopython:

http://www.diveintopython.org/getting_to_know_python/dictionaries.html

You'll have to store your keys in a list or tuple to keep them ordered.

Jeremy

-- 
Jeremy Sanders
http://www.jeremysanders.net/
--
http://mail.python.org/mailman/listinfo/python-list


Re: breaking out of outer loops

2008-01-29 Thread Jeremy Sanders
[EMAIL PROTECTED] wrote:

> Any elegant way of breaking out of the outer for loop than below, I
> seem to have come across something, but it escapes me
> 
> for i in outerLoop:
>for j in innerLoop:
>if condition:
>   break
>else:
>continue
> break

Perhaps Python needs a "continue N" or a "break N" statement :-)

for i in outerLoop:
  for j in innerLoop:
 if condition:
break 2

Seeing as we can't have a goto :-)

Jeremy

-- 
Jeremy Sanders
http://www.jeremysanders.net/
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: multidimensional "arrays"

2007-12-06 Thread Jeremy Sanders
Horacius ReX wrote:

> do you know how to do similar but in two dimensions ?

Investigate the numpy module if you are dealing with numbers.

Jeremy

-- 
Jeremy Sanders
http://www.jeremysanders.net/
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Extended date and time

2007-11-12 Thread Jeremy Sanders
John Machin wrote:

> What does "dates in the past" mean?? Please be more specific about the
> earliest date that you want to be able to handle. Python's datetime
> starts at 0001-01-01. Somebody mentioned the time module, which is
> implementation-dependent but typically starts at 1970-01-01 .
> 
> What functionality do you need, other than two-way conversion between
> days_since_epoch and (proleptic Gregorian) date/time?

I want to convert between seconds from the epoch (let's say 1970 in floating
point) and date and time. I also want it to work across all platforms.

Is there any way to convert a datetime into seconds from a certain date? Is
the most robust way of doing it just to subtract two datetime objects and
turn the timedelta into a floating point number?

Thanks

Jeremy

-- 
Jeremy Sanders
http://www.jeremysanders.net/
-- 
http://mail.python.org/mailman/listinfo/python-list


Extended date and time

2007-11-10 Thread Jeremy Sanders
Hi - I need to add support to a program for dates and times. The built-in
Python library seems to be okay for many purposes, but what I would like
would be Unix epoch style times (seconds relative to some date), covering a
large period from the past to the future. What would be nice would be a
library which can take floating point seconds from an epoch.

Does anyone know of a library which can convert from human style dates and
times to a floating point epoch and back again? I expect I could fudge the
fractional seconds with the built-in library, but I can't see how to get
dates in the past.

Thanks, Jeremy.

-- 
Jeremy Sanders
http://www.jeremysanders.net/
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Drawing charts in Qt

2007-11-06 Thread Jeremy Sanders
Michel Albert wrote:

> Has anyone ever successfully used these graphing libraries with PyQt?
> Or are there other graphing libraries available? In fact, my needs are
> modest. A Line- and Bar-Chart would solve the majority of problems.

Veusz does line charts, and stepped charts (which are almost like bar
charts...). It is implemented with PyQt4.

You can use the windows.PlotWindow widget in your PyQt4 app, but
unfortunately I haven't got round to documenting this properly... If you're
interested I can give instructions.

Jeremy

-- 
Jeremy Sanders
http://www.jeremysanders.net/
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: ANN: Veusz 1.0 - a scientific plotting package

2007-10-29 Thread Jeremy Sanders
Wildemar Wildenburger wrote:
> Oh, OK. I though it was a library. I now see that it is an actual
> application. Sorry to have bothered you :)

It's a library too :-)

-- 
Jeremy Sanders
http://www.jeremysanders.net/
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: ANN: Veusz 1.0 - a scientific plotting package

2007-10-29 Thread Jeremy Sanders
Wildemar Wildenburger wrote:

> Not that I don't value your effort, but why another plotting package
> while we have pyx and matplotlib already?

In addition to the Python based scripting command line and embedding
interface, it has a powerful graphical user interface for constructing
plots and importing data.  Neither matplotlib or pyx have this.
This is the main feature. It was originally intended to be mainly
command-line based, but I've found the GUI to be my main way of using it.

At the time of the first release of Veusz, and for quite long after,
matplotlib was too primitive to use as a backend for Veusz (see previous
threads on this subject). Maybe that has changed now, but IMHO Veusz output
still looks better.

Jeremy

-- 
Jeremy Sanders
http://www.jeremysanders.net/
-- 
http://mail.python.org/mailman/listinfo/python-list


ANN: Veusz 1.0 - a scientific plotting package

2007-10-29 Thread Jeremy Sanders
I'm pleased to announce Veusz 1.0. Source, windows and linux i386 binaries
are available. Jeremy Sanders

Veusz 1.0
-
Velvet Ember Under Sky Zenith
-
http://home.gna.org/veusz/

Veusz is Copyright (C) 2003-2007 Jeremy Sanders <[EMAIL PROTECTED]>
Licenced under the GPL (version 2 or greater).

Veusz is a scientific plotting package written in Python, using PyQt4
for display and user-interfaces, and numpy for handling the numeric
data. Veusz is designed to produce publication-ready Postscript/PDF
output. The user interface aims to be simple, consistent and powerful.

Veusz provides a GUI, command line, embedding and scripting interface
(based on Python) to its plotting facilities. It also allows for
manipulation and editing of datasets.

Feature changes from 0.99.0:
 * Import of Text datasets
 * Labels can be plotted next to X-Y points
 * Numbers can be directly plotted by entering into X-Y datasets as X and Y
 * More line styles
 * Loaded document and functions are checked for unsafe Python features
 * Contours can be labelled with numbers
 * 2D dataset creation to make 2D datasets from x, y, z 1D datasets

Bug and minor fixes from 0.99.0:
 * Zooming into X-Y images works now
 * Contour plots work on datasets with non equal X and Y sizes
 * Various fixes for datasets including NaN or Inf
 * Large changes to data import filter to support loading strings (and dates
   later)
 * Reduce number of undo levels for memory/speed
 * Text renderer rewritten to be more simple
 * Improved error dialogs
 * Proper error dialog for invalid loading of documents

Features of package:
 * X-Y plots (with errorbars)
 * Line and function plots
 * Contour plots
 * Images (with colour mappings and colorbars)
 * Stepped plots (for histograms)
 * Fitting functions to data
 * Stacked plots and arrays of plots
 * Plot keys
 * Plot labels
 * LaTeX-like formatting for text
 * EPS/PDF/PNG export
 * Scripting interface
 * Dataset creation/manipulation
 * Embed Veusz within other programs
 * Text, CSV and FITS importing

Requirements:
 Python (2.3 or greater required)
   http://www.python.org/
 Qt >= 4.3 (free edition)
   http://www.trolltech.com/products/qt/  
 PyQt >= 4.3 (SIP is required to be installed first)
   http://www.riverbankcomputing.co.uk/pyqt/
   http://www.riverbankcomputing.co.uk/sip/
 numpy >= 1.0
   http://numpy.scipy.org/
 Microsoft Core Fonts (recommended for nice output)
   http://corefonts.sourceforge.net/
 PyFITS >= 1.1 (optional for FITS import)
   http://www.stsci.edu/resources/software_hardware/pyfits

For documentation on using Veusz, see the "Documents" directory. The
manual is in pdf, html and text format (generated from docbook).

Issues:
 * Reqires a rather new version of PyQt, otherwise dialogs don't work.
 * Can be very slow to plot large datasets if antialiasing is enabled.
   Right click on graph and disable antialias to speed up output.
 * The embedding interface appears to crash on exiting.

If you enjoy using Veusz, I would love to hear from you. Please join
the mailing lists at

https://gna.org/mail/?group=veusz

to discuss new features or if you'd like to contribute code. The
latest code can always be found in the SVN repository.

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


Re: Stopping a fucntion from printing its output on screen

2007-10-17 Thread Jeremy Sanders
sophie_newbie wrote:

> Hi, in my program i need to call a couple of functions that do some
> stuff but they always print their output on screen. But I don't want
> them to print anything on the screen. Is there any way I can disable
> it from doing this, like redirect the output to somewhere else? But
> later on in the program i then need to print other stuff so i'd need
> to re-enable printing too. Any ideas?

If they are python functions, this hack should work...

import sys

class NullWriter(object):
def write(self, arg):
pass

def testfunc():
print "this is a test"

nullwrite = NullWriter()
oldstdout = sys.stdout
sys.stdout = nullwrite  # disable output
testfunc()
sys.stdout = oldstdout  # enable output
testfunc()


-- 
Jeremy Sanders
http://www.jeremysanders.net/
-- 
http://mail.python.org/mailman/listinfo/python-list


Wrapper functions and arguments

2007-10-01 Thread Jeremy Sanders
One little issue I have is to write a little wrapper which can generally
pass standard and keyword arguments to a callee:

def a(x, y, z):
  print x, y, z
def b(x, y, z='fruitbat')
  print x, y, z

for func in a, b:
  def wrapper(func=func, *args, **argsk):
 # do something
 func(*args, **argsk)
  x.append(wrapper)

x[0](1, 2, 3)
x[1](1, 2)
...

Is there any way to do this? Can you capture arguments in a tuple and dict,
but still receive other keyword arguments? The only solution I found was to
implement wrapper as a class (like I would in c++):

class wrapper(object):
  def __init__(self, func):
self.func = func
  def __call__(self, *args, **argsk):
self.func(*args, **argsk)

Jeremy

-- 
Jeremy Sanders
http://www.jeremysanders.net/
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: sorteddict PEP proposal [started off as orderedict]

2007-09-26 Thread Jeremy Sanders
Mark Summerfield wrote:

> The sorteddict API that has emerged so far is (1) apart from the
> constructor, everything is identical to dict, (2) the constructor
> takes the same args as sorted(), so if you want to seed with a dict or
> with keywords you write sorteddict(dict(a=1,b=2), ...), (or you could
> create a sorteddict and use update() since that takes the same args as
> dict's constructor).

first() and last() would also be nice on a sorted dict.

Jeremy

-- 
Jeremy Sanders
http://www.jeremysanders.net/
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: sorteddict PEP proposal [started off as orderedict]

2007-09-25 Thread Jeremy Sanders
Mark Summerfield wrote:

> If there is positive feedback I will submit the PEP to the reviewers,
> so if you think it is a good idea please say so. (I'm sure that if you
> _don't_ like it you'll tell me anyway:-)

It would be nice to have the ability to use numerical indexes and the key,
but I don't think they should share the same methods.

A useful use case would be to make a LRU (least recently used) dictionary,
where the keys are time-based (e.g. an incrementing counter). You should be
able to identify the least recently used object for discarding by just
accessing the last item in the dictionary.

By the way, I think a LRU cache dictionary would be a great addition to the
standard library.

Is there any speed advantage from implementing the sorteddict as a red-black
tree or something similar in C?

Jeremy

-- 
Jeremy Sanders
http://www.jeremysanders.net/
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: limiting memory consumption of Python itself or a dict in a Python program

2007-09-20 Thread Jeremy Sanders
Jonas Maurus wrote:

> I want to write a Python program that receives messages via SMTP and
> stores them in a dict or an array. For my purposes it would be
> important that all received mail would be kept in RAM and not cached
> out to disk. If a new message comes in that can't fit in the allocated
> memory, a number of old messages would be discarded.
> 
> As the server needs to have room for other tasks, I'd like to limit
> the overall memory consumption to a certain amount.

Since your data is all in one place, why not write a dict or list wrapper
which keeps track of the total space of the items stored (using len on the
strings)?

It could automatically clean out old entries when the memory usage becomes
too much.

Jeremy

-- 
Jeremy Sanders
http://www.jeremysanders.net/
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: newbie: self.member syntax seems /really/ annoying

2007-09-12 Thread Jeremy Sanders
Charles Fox wrote:

> I've just started playing around with Python, as a possible
> replacement for a mix of C++, Matlab and Lisp.  The language looks
> lovely and clean with one huge exception:  I do a lot of numerical
> modeling, so I deal with objects (like neurons) described
> mathematically in papers, by equations like

I thought it was horrible when I started, but now when looking at somebody
else's C++ code I find it very hard to work out whether something is a
global, a member or a local variable, unless they use some sort of naming
convention.

If you alias self as "s", it's only two more characters per variable access,
which is the same as the C++ "m_" naming convention.

-- 
Jeremy Sanders
http://www.jeremysanders.net/
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Co-developers wanted: document markup language

2007-08-25 Thread Jeremy Sanders
Torsten Bronger wrote:

> I don't know exactly what you mean but the answer is probably no.
> For example, I want the author to state the title, keywords, etc of
> his document, however, he should not state that he wants the title
> printed centred and 4cm from the top of the page.
> 
> The latter is defined in the "theme" which will be given as a set of
> ordinary LaTeX commands (for the LaTeX backend).

Isn't the problem that making such a theme will be very hard?

One of the annoying things about LaTeX is lack of control over positioning
(e.g. floats, page breaks...). The one thing most LaTeX users moan about is
trying to get their document to fit into an n page limit (e.g. for a
proposal). Maybe the theme could have some options to control spacing,
however, like some sort of CSS.

I think the one thing that would improve LaTeX is orthogonality in its
commands (e.g. why no 8pt option for the document, why the crazy \small,
\LARGE, etc commands?), and fixing the font system to be based around
modern fonts. Finally making bibtex part of the core and making it easy to
use would be great.

-- 
Jeremy Sanders
http://www.jeremysanders.net/
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Co-developers wanted: document markup language

2007-08-24 Thread Jeremy Sanders
Torsten Bronger wrote:

> Some LaTeX users in Aachen thought about a general-use markup
> language this spring.  I wrote some code and a rough project
> description, however, we could need some help.
> 
> If you are interested, visit the provisional project page at
> http://latex-bronger.sourceforge.net/gummi/

Sounds a good idea - LaTeX has so many historical hangovers. How many people
on earth can actually write a LaTeX style file?

I'm not sure about writing LaTeX output, however, due to the crude nasty
ways it handles fonts and so on. How are you going to get enough controls
for users over what they always complain about: fonts, page breaking, and
positioning of figures? Maybe it's an okay first step however.

Jeremy

-- 
Jeremy Sanders
http://www.jeremysanders.net/
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: best GUI library for vector drawing program

2007-08-17 Thread Jeremy Sanders
chewie54 wrote:

> I looked at your application, Veusz (it looks very nice),  and I see
> you have binary distrubitions
> for each os.  Is is difficult to build these binaries for each
> system.  Could
> you tell me how that is done?

I use pyinstaller to make the binaries (see the veusz_pyinst.spec file), and
NSIS to make a Windows installer from the Windows binary (see veusz.nsi).

The Linux binary, unfortunately, isn't 100% compatible, as I've found trying
to run on 64 bit systems. I assume it's some sort of glibc mismatch. Making
the linux binaries on an old distribution helps the compatibility (I use
centos 3 in a virtual environment).

jeremy

-- 
Jeremy Sanders
http://www.jeremysanders.net/
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: best GUI library for vector drawing program

2007-08-17 Thread Jeremy Sanders
chewie54 wrote:

> What would be the best cross-platform GUI library to use for a vector
> based CAD program ( something like Visio on Windows )   WxWidgets,
> Tk,   PyQt,  Java Swing,  Java SWT,   I need the capibility to
> draw and edit in a window that looks like a page of paper so WYSIWYG
> is very important,  and I need to save the drawings in vector based
> file formats like PS, EPS,  SVG, as well as image formats like jpg,
> png, and gif.  Also, the images need to be high resolution so that
> they can be pasted into various other programs in Windows OS,  and
> Linux OS,  and the Mac OS.

PyQt/Qt4 is capable of that (SVG export was added in Qt4.3).

I have a graph drawing application based around it (Veusz).

If you base everything around QPainter, you'll be able to write to any of
those output formats (including eps and pdf), and bitmaps. Antialiasing is
optional for bitmap formats.

Jeremy

-- 
Jeremy Sanders
http://www.jeremysanders.net/
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Smoother Lines in Turtle Graphics

2007-08-10 Thread Jeremy Sanders
Ant wrote:

> Python: Batteries and Turtles included!

I didn't know that! It looks like turtle is based on Tk, which doesn't have
antialiasing yet (see http://wiki.tcl.tk/10101 ), so it can't really be
made nice and smooth (unless you could somehow use tkzinc/tkpath to draw
with).

I suppose turtle wouldn't be that hard to reimplement it to use Qt/Gtk
instead.

Jeremy

-- 
Jeremy Sanders
http://www.jeremysanders.net/
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: questions about functions inside a function

2007-07-16 Thread Jeremy Sanders
[EMAIL PROTECTED] wrote:

> What I want is, the value of i should be bounded to the anonymous
> function. And the output should like this:
...
> How to achieve this?

This doesn't answer your question (others have), but another (perhaps
clearer) way to do such things is something like

class MyFunc(object):
  """A function object."""
  def __init__(self, val):
 self.val = val

  def __call__(self):
 """Return value squared"""
 return self.val**2

a = []
for i in range(4):
  a.append(MyFunc(i))

for f in a:
  f()


Jeremy

-- 
Jeremy Sanders
http://www.jeremysanders.net/
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: stripping the first byte from a binary file

2007-07-10 Thread Jeremy Sanders
rvr wrote:

> Would someone mind showing me how to strip the first byte from a
> binary file? For some reason I can't figure this out from the binary
> file editing examples I've read. Thanks.

Do you mean something like this?

f = open('test.dat', 'rb')
f.read(1)  # read 1st byte and ignore it
rest = f.read()  # read rest

or

data = f.read()
data = data[1:] # skip 1st byte

?

-- 
Jeremy Sanders
http://www.jeremysanders.net/
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Matrix Multiplication

2007-06-19 Thread Jeremy Sanders
sturlamolden wrote:

> That's what I wrote: "NumPy has a matrix type." It is called called
> numpy.matrix.
> 
> I did not suggest using the array type numpy.array.
> 
> Reading carefully is indeed important...

I know what you wrote and you are strictly correct. I was just clarifying it
for a reader who may not have instantly realised that there were multiple
array types in numpy (I didn't for a while), and could have wasted many
hours and been discouraged.

Explaining clearly is indeed important.

-- 
Jeremy Sanders
http://www.jeremysanders.net/
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Matrix Multiplication

2007-06-18 Thread Jeremy Sanders
sturlamolden wrote:

> Use numpy: www.scipy.org
> 
> NumPy has a matrix type that overloads the * operator.

Just a tiny followup, which may be important unless you carefully read the
documentation. The "*" operator doesn't do matrix multiplication for normal
numpy arrays - you do need to use its special matrix type to get this. You
can use the dot function to get matrix multiplication with its normal
arrays.

Jeremy

-- 
Jeremy Sanders
http://www.jeremysanders.net/
-- 
http://mail.python.org/mailman/listinfo/python-list


ANN: Veusz-0.99.0 - a scientific plotting package

2007-05-24 Thread Jeremy Sanders
I am pleased to announce a new beta of a largely rewritten Veusz plotting
package. This now uses Qt4 and numpy, adding support for Windows. Windows
and Linux binaries are provided. For details see below:


Veusz 0.99.0 (new Qt4/numpy beta)

Velvet Ember Under Sky Zenith
-
http://home.gna.org/veusz/

Veusz is Copyright (C) 2003-2007 Jeremy Sanders <[EMAIL PROTECTED]>
Licenced under the GPL (version 2 or greater).

Veusz is a scientific plotting package written in Python, using PyQt4
for display and user-interfaces, and numpy for handling the numeric
data. Veusz is designed to produce publication-ready Postscript/PDF
output. The user interface aims to be simple, consistent and powerful.

Veusz provides a GUI, command line, embedding and scripting interface
(based on Python) to its plotting facilities. It also allows for
manipulation and editing of datasets.

Changes from 0.10:
 This is the first release of a much rewritten version of Veusz
 It has been updated to run under Qt4 and numpy, and now supports Windows
 The user interface is also signficantly easier to use

 Other useful features include:
  * Colorbars for images (better color scaling for images too)
  * Grids of graphs with different sized subgraphs
  * Much better import dialog 
  * Antialiased screen output
  * Native PNG and PDF export
  * Separate formatting/properties dialog
  * Handling of INF/NaN in input data
  * Transparency of graphs (not for EPS output)
 Plus many more useful changes (see ChangeLog)

Features of package:
 * X-Y plots (with errorbars)
 * Line and function plots
 * Contour plots
 * Images (with colour mappings and colorbars)
 * Stepped plots (for histograms)
 * Fitting functions to data
 * Stacked plots and arrays of plots
 * Plot keys
 * Plot labels
 * LaTeX-like formatting for text
 * EPS/PDF/PNG export
 * Scripting interface
 * Dataset creation/manipulation
 * Embed Veusz within other programs
 * Text, CSV and FITS importing

Requirements:
 Python (2.3 or greater required)
   http://www.python.org/
 Qt >= 4.1 (free edition)
   http://www.trolltech.com/products/qt/  
 PyQt >= 4.1 (SIP is required to be installed first)
   http://www.riverbankcomputing.co.uk/pyqt/
   http://www.riverbankcomputing.co.uk/sip/
 numpy >= 1.0
   http://numpy.scipy.org/
 Microsoft Core Fonts (recommended for nice output)
   http://corefonts.sourceforge.net/
 PyFITS >= 1.1rc3 (optional for FITS import)
   http://www.stsci.edu/resources/software_hardware/pyfits

For documentation on using Veusz, see the "Documents" directory. The
manual is in pdf, html and text format (generated from docbook).

Issues:
 * This is a new beta, so there are likely to be a number of bugs, even
   though it has been used by a couple of people for some time.
 * Can be very slow to plot large datasets if antialiasing is enabled.
   Right click on graph and disable antialias to speed up output.
 * Some older versions of Qt (<4.2.2) can produce very large postscript
   output and random crashes. This may not be completely resolved
   (especially on windows).
 * The embedding interface appears to crash on exiting.

If you enjoy using Veusz, I would love to hear from you. Please join
the mailing lists at

https://gna.org/mail/?group=veusz

to discuss new features or if you'd like to contribute code. The
latest code can always be found in the SVN repository.

Jeremy Sanders

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


Re: Python Screen Scraper

2007-04-24 Thread Jeremy Sanders
Michael Bentley wrote:
> Possibly the easiest thing will be to read from firefox' cache.
> Otherwise I think your only real options are to either build a proxy
> or sniff the wire...

Maybe another way would be to write a firefox addon/plugin. I believe python
is now supported...

-- 
Jeremy Sanders
http://www.jeremysanders.net/
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Beginner: Formatting text output (PyQt4)

2007-04-19 Thread Jeremy Sanders
Glen wrote:

> What seems to be happening is that the font that pyqt is using is not
> fixed width, so I did this:
> qTxtFormat = QTextCharFormat()
> qTxtFormat.setFontFixedPitch(True)
> ui.textEdit.setCurrentCharFormat(qTxtFormat)

Does something like ui.textEdit.setCurrentFont(QFont('fixed')) work? It
seems to work for me if you use plain text.

Tabs or html/rich text formatting should be a better way to get the layout
(or just use a table).

Jeremy

-- 
Jeremy Sanders
http://www.jeremysanders.net/
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Managing a file buffer

2007-04-09 Thread Jeremy Sanders
David Johnson wrote:

> I have an application that reads video from a socket and saves it to a
> file, which I then play using GStreamer. The major problem with this
> approach is that the file keeps getting bigger and bigger until the disk
> is full ;-)
> 
> What I'd like to do is have a buffer, say 5MB in size, which I can point
> GStreamer at. So every time I write 60K to the tail of the file, I'd need
> to remove 60K from the head of the file.

A named pipe may be useful to you, depending on your buffering requirements,
and assuming gstreamer can play from one.

e.g. http://www2.linuxjournal.com/article/2156

You can create a named pipe from python.

Jeremy

-- 
Jeremy Sanders
http://www.jeremysanders.net/
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Database in memory

2007-04-09 Thread Jeremy Sanders
Jim wrote:

> I have an application that will maintain an in-memory database in the
> form of a list of lists.  Does anyone know of a way to search for and
> retreive "records" from such a structure?

The dictionary is the obvious way to index things:

# items consist of name and age
data = [
 ['fred', 42],
 ['jim', 16], ...
]

name_index = {}
for item in data:
 name_index[item[0]] = item

>>> name_index['fred']
['fred', 42]

Dictionaries are one of the most useful things in Python. Make sure you know
how to take adavantage of them...

Jeremy

-- 
Jeremy Sanders
http://www.jeremysanders.net/
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Console UI

2007-04-09 Thread Jeremy Sanders
Clement wrote:

> My project is based on console Application. Is there any console UI
> except urwid. If so, can i come to know.

You might try pytvision, http://pytvision.sourceforge.net/ , which are
python bindings for a  of the old borland turbovision environment (see
http://tvision.sourceforge.net/ ). I haven't used these, but I fondly
remember turbovision from my Turbo Pascal/Turbo C++ days. I think it is
still a good text based GUI.

Jeremy

-- 
Jeremy Sanders
http://www.jeremysanders.net/
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Watching a file another app is writing

2007-03-12 Thread Jeremy Sanders
Gordon Airporte wrote:

> I'm trying to find a way to take a file that another program has opened
> and writes to periodically, open it simultaneously in Python, and
> automatically update some of my objects in Python when the file is
> written to.
> I can open the file and manually readlines() from it to keep up to date,
> it's the automatic part I'm having trouble with. This is on Windows.

It occurs to me under Unix you could perhaps get your first program to write
to a "named pipe", which you 2nd program could read from. See

http://en.wikipedia.org/wiki/Named_pipe

Jeremy

-- 
Jeremy Sanders
http://www.jeremysanders.net/
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: PyQt4 strangeness

2007-01-23 Thread Jeremy Sanders
Tina I wrote:

> 
> self.connect(self.ui.testButton, QtCore.SIGNAL("clicked()"),
> self.doSomething)
> 
> Anyone know why this is? Or am I missing something very basic here? (I'm
> still very much a noob I guess)

If you want to import both you can do something like:

import PyQt4.Qt as Qt

which imports QtCore and QtGui

Jeremy

-- 
Jeremy Sanders
http://www.jeremysanders.net/
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: when format strings attack

2007-01-19 Thread Jeremy Sanders
Steven D'Aprano wrote:
 
> os.system('dir -l %s' % 'text.txt')
> 
> 
> Now, there is a security risk: you might set command1 yourself, and
> allow the user to set args. If command1 is an external application
> with a security hole, and the user provides arguments that trigger that
> bug, then naturally your application will inherit whatever security
> vulnerabilities the external application suffers from. No surprises there.

There are also big risks like this

filename = 'foo; rm importantfile'
cmd = 'ls %s' % filename
os.system(cmd)

oops!

-- 
Jeremy Sanders
http://www.jeremysanders.net/
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: maximum number of threads

2007-01-10 Thread Jeremy Sanders
Jean-Paul Calderone wrote:

> Indeed you are correct.  The actual limit you are hitting is the size
> of your address space.  Each thread is allocated 8MB of stack.  382
> threads consumes about 3GB of address space.  Even though most of this
> memory isn't actually allocated, the address space is still used up.  So,
> when you try to create the 383rd thread, the kernel can't find anyplace
> to put its stack.  So you can't create it.

Interesting. That's why I can get over 3000 on my x86-64 machine... Much
more address space.

-- 
Jeremy Sanders
http://www.jeremysanders.net/
-- 
http://mail.python.org/mailman/listinfo/python-list


RE: How to read the directory which the actively running python file islocated in?

2006-12-01 Thread Jeremy Sanders
Michael Malinowski wrote:

> Nevermind, I got it using the sys.argv[0]

That doesn't always work, as on unix the path isn't prepended onto
sys.argv[0] necessarily.

import os.path
...
os.path.dirname(os.path.abspath(__file__))

may be better.

-- 
Jeremy Sanders
http://www.jeremysanders.net/
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: PyQt app in seperate thread

2006-11-23 Thread Jeremy Sanders
anders wrote:

> OK I see that now. Thanks for pointing that out. So basically, I can't
> do what I want at all. That's a bit of a pain. Is there no way of
> tricking Qt into thinking I'm running it in the main thread?

I have an app which runs Qt in a separate thread and allows the user to send
it python commands from the main thread. Have a look at this code to see
how it works:

http://svn.gna.org/viewcvs/veusz/branches/qt4/embed.py?rev=530&view=markup

Jeremy

-- 
Jeremy Sanders
http://www.jeremysanders.net/
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Programmatically finding "significant" data points

2006-11-14 Thread Jeremy Sanders
erikcw wrote:

> I have a collection of ordered numerical data in a list.  The numbers
> when plotted on a line chart make a low-high-low-high-high-low (random)
> pattern.  I need an algorithm to extract the "significant" high and low
> points from this data.
> 
...
> 
> How do I sort through this data and pull out these points of
> significance?

Get a book on statistics. One idea is as follows. If you expect the points
to be centred around a single value, you can calculate the median or mean
of the points, calculate their standard deviation (aka spread), and remove
points which are more than N-times the standard deviation from the median.

Jeremy

-- 
Jeremy Sanders
http://www.jeremysanders.net/
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: How to choose the right GUI toolkit ?

2006-11-10 Thread Jeremy Sanders
Dan Lenski wrote:

> My apologies!  I'm glad to be corrected on this.  There are Cygwin
> packages for Qt as well, but I have heard about enough bugs to think I
> should avoid Qt.  I have used enough Gtk apps that run flawlessly under
> Windows to have my hopes that it works well.

You normally use PyQt/Qt on Windows without Cygwin. There are very few bugs
and lots of professional companies base their products on Qt Windows.

-- 
Jeremy Sanders
http://www.jeremysanders.net/
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: How to choose the right GUI toolkit ?

2006-11-09 Thread Jeremy Sanders
Dan Lenski wrote:

> Nick and John S., thank you for the tip on wxPython!  I'll look into it
> for my next project.  I too would avoid Qt, not because of the GPL but
> simply because I don't use KDE under Linux and because Qt is not well
> supported under Cygwin or on native Windows.  I too like to learn from
> actual printed books, so I'll check this one out.  O'Reilly should do a
> book on Python GUI stuff!

PyQt is well supported under native Windows. Qt-4 is now GPLd for Windows
too. I'd highly recommend it.

Jeremy

-- 
Jeremy Sanders
http://www.jeremysanders.net/
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Plot pkg - Multiple Y axes?

2006-11-07 Thread Jeremy Sanders
monkeyboy wrote:

> I'm searching for a plotting package that will allow multiple y axes of
> different scales. For example I'd like to overlay 4 or 5 time series
> with each series having a separate axis. Does anyone know of such a
> package?

My package veusz allows that... 

http://home.gna.org/veusz/

You can have any number of y-axes, see

http://home.gna.org/veusz/screenshots/screenshot1.png

The PyQt4 version is coming along nicely too...

Jeremy

-- 
Jeremy Sanders
http://www.jeremysanders.net/
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Python 123 introduction

2006-10-30 Thread Jeremy Sanders
[EMAIL PROTECTED] wrote:

> I'm not trying to minimize Jeremy's efforts in any way, but how is his
> tutorial a significant improvement over the original
> (http://www.python.org/doc/current/tut/)?

It's not intended as a replacement, but what I wanted to do was write a
quick 2 hour course for people to work through. It overlaps quite a bit
with the tutorial, but I tried to minimise any detail.

I just publicised it in case anybody wanted something similar.

Jeremy

-- 
Jeremy Sanders
http://www.jeremysanders.net/
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Python 123 introduction

2006-10-30 Thread Jeremy Sanders
[EMAIL PROTECTED] wrote:

> This is great! A excellent tutorial for somone who has prior experience
> in programming and is starting out in python. My friend keeps wanting
> me to teach him python, I think this would be the perfect link for him.

I'm glad you think it is useful. It needs a bit of cleaning up as it assumes
things such as python being in /usr/local/bin... I may try to improve this
later.

Jeremy

-- 
Jeremy Sanders
http://www.jeremysanders.net/
-- 
http://mail.python.org/mailman/listinfo/python-list


Python 123 introduction

2006-10-30 Thread Jeremy Sanders
Here is a brief simple introduction to Python I wrote for a computing course
for graduate astronomers. It assumes some programming experience. Although
it is not a complete guide, I believe this could be a useful document for
other groups to learn Python, so I'm making it available for others to
download, and modify for their own needs (some of the content is site
specific).

HTML version:
 http://www-xray.ast.cam.ac.uk/~jss/lecture/computing/notes/out/python_123/
Postscript LaTeX output:
 http://www-xray.ast.cam.ac.uk/~jss/lecture/computing/notes/out/python_123.ps
PDF LaTeX output:
 http://www-xray.ast.cam.ac.uk/~jss/lecture/computing/notes/out/python_123.pdf
LaTeX source:
 http://www-xray.ast.cam.ac.uk/~jss/lecture/computing/notes/out/python_123.tex

Jeremy

-- 
Jeremy Sanders
http://www.jeremysanders.net/
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Best way to handle large lists?

2006-10-03 Thread Jeremy Sanders
Jeremy Sanders wrote:

> Chaz Ginger wrote:
> 
>> What would sets do for me over lists?
> 
> It's faster to tell whether something is in a set or dict than in a list
> (for some minimum size).

As a footnote, this program

import random
num = 10

a = set( range(num) )
for i in range(10):
x = random.randint(0, num-1) in a

completes in less than a second, whereas

import random
num = 10

a = range(num)
for i in range(10):
x = random.randint(0, num-1) in a

takes a long time on my computer.

Jeremy

-- 
Jeremy Sanders
http://www.jeremysanders.net/
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Best way to handle large lists?

2006-10-03 Thread Jeremy Sanders
Chaz Ginger wrote:

> What would sets do for me over lists?

It's faster to tell whether something is in a set or dict than in a list
(for some minimum size).

Jeremy

-- 
Jeremy Sanders
http://www.jeremysanders.net/
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Writing 2d array in an ascci file

2006-09-28 Thread Jeremy Sanders
[EMAIL PROTECTED] wrote:

> I want to write an 2d array in an ascii file using numpy. There's the -
> tofile / fromfile - function but it doesn't work with nd array.

Is this good enough?

x = numpy.zeros( (10, 10) )
f = open('out.txt', 'w')
print >>f, str(x).replace('[',' ').replace(']', ' ')
f.close()


Jeremy

-- 
Jeremy Sanders
http://www.jeremysanders.net/
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: naming objects from string

2006-09-21 Thread Jeremy Sanders
manstey wrote:

> so they might provide a list of names, like 'bob','john','pete', with 3
> structures per name, such as 'apple','orange','red' and I need 9 tuples
> in my code to store their data:
> 
> bob_apple=()
> bob_orange=()
> ..
> pete_red=()

I really think you should be using dictionaries here. You don't want to be
messing around creating random variables in the local or global namespace.

For instance

myvals = {}
myvals['bob'] = {}
myvals['pete'] = {}
...
myvals['bob']['apple'] = (1,2,3,4)
myvals['bob']['orange'] = (2,3,4)
myvals['pete']['red'] = (4,5,6,7)

and so on

>>> myvals
{'pete': {'red': (4, 5, 6, 7)}, 'bob': {'orange': (2, 3, 4), 'apple': (1, 2,
3,4)}}

-- 
Jeremy Sanders
http://www.jeremysanders.net/
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Is it possible to save a running program and reload next time ?

2006-09-21 Thread Jeremy Sanders
[EMAIL PROTECTED] wrote:

> I have a program which will continue to run for several days. When it is
> running, I can't do anything except waiting because it takes over most
> of the CUP time.
> 
> Is it possible that the program can save all running data to a file when
> I want it to stop, and can reload the data and continue to run from
> where it stops when the computer is free ?

For Linux (and other Unix like OSs), there are several "checkpointing"
libraries available which allow programs to be saved to disk, and restarted
later.

If the other suggestions people have given to you (STOPping and CONTinuing
processes), or sleep statements, or saving state, don't work investigate
these.

e.g. http://www.cs.wisc.edu/~zandy/ckpt/

These programs have limitations on what can be restored (e.g. threads,
shared memory, network connections...). I don't know which ones work with
python.

-- 
Jeremy Sanders
http://www.jeremysanders.net/
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Force sleep to ignore interrupts

2006-09-14 Thread Jeremy Sanders
[EMAIL PROTECTED] wrote:

> It works as I want when used in the main application thread.
> That is, when you hit Ctr + C, it stops running.  However, if
> the class that subclasses it, also subclasses Thread, it breaks
> in that hitting Ctrl + C interrupts the call to sleep which puts
> the event loop out of sync with real time.

Maybe you could install a signal handler to ignore ctrl+c, when required

import signal
signal.signal(signal.SIGINT, signal.SIG_IGN)


-- 
Jeremy Sanders
http://www.jeremysanders.net/
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: ntp in python

2006-08-30 Thread Jeremy Sanders
Janto Dreijer wrote:

> Maybe I'd be better off writing my own implementation that synchronises
> the two pc clocks. Any hints? :-)

I think you might read up on how ntp synchronises clocks (their website is
very thorough), and use their algorithm. It is supposed to be very robust
algorithm. I saw something about ntp on the twisted mailing list, so you
could ask there.

Jeremy

-- 
Jeremy Sanders
http://www.jeremysanders.net/
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: ntp in python

2006-08-29 Thread Jeremy Sanders
Janto Dreijer wrote:

> I want to measure the packet delivery delays over various network
> links. For this I need to synchronise the times of the sender and
> receiver, either against NTP or eachother.

Couldn't you just use NTP itself to get the delivery delay? You can read the
delay out from the ntpdc console using dmpeers, or lopeers in ntpq. You
could have two peers either side of the link and measure the delay from
NTP.

You may also be able to query remote ntp servers to get their delays to
their peers.

Jeremy

-- 
Jeremy Sanders
http://www.jeremysanders.net/
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Python and STL efficiency

2006-08-22 Thread Jeremy Sanders
Mc Osten wrote:

> Here some results (I know that the fpoint optimizations are useless...
> it's is my "prebuilt" full optimization macro :) ):

Interesting. The opimisation makes no difference to the speed of the C++ one
for me. I just get

xpc17:~> g++4 -O2 test2.cpp
xpc17:~> ./a.out
What do you know?
chicken crosses road
fool
so long...
What do you know?
chicken crosses road
fool
so long...
Elapsed 2.11
Elapsed 1.11

(This is with an Althon 64 4600+ running Linux).

Unfortunately the Python on this computer doesn't have set as it is too old,
so I can't compare it.

-- 
Jeremy Sanders
http://www.jeremysanders.net/
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Python and STL efficiency

2006-08-21 Thread Jeremy Sanders
Licheng Fang wrote:

> I was using VC++.net and IDLE, respectively. I had expected C++ to be
> way faster. However, while the python code gave the result almost
> instantly, the C++ code took several seconds to run! Can somebody
> explain this to me? Or is there something wrong with my code?

It must be the debugging, the compiler or a poor STL implementation. With
gcc 4 it runs instantly on my computer (using -O2), even with 10x the
number of values.

If the problem is that C++ has to make lots of new strings, as other posters
have suggested, then you could do something like

const string foo = "What do you know?";

for (long int i=0; i<1 ; ++i){
   a.push_back(foo);
   ...
}

as many C++ implementations use reference counting for identical strings.

Jeremy

-- 
Jeremy Sanders
http://www.jeremysanders.net/
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Small Troll on notation of variables over time

2006-08-21 Thread Jeremy Sanders
Hendrik van Rooyen wrote:

> What do you guys think?

You could get something similar using an object, such as

class Hist(object):

def __init__(self):
self.vals = [None]

def __call__(self, index=-1):
return self.vals[index]

def set(self, val):
self.vals.append(val)

a = Hist()

a.set(5)
print a()

a.set('hi there')
print a()
print a(-2)

Which prints
5
hi there
5

-- 
Jeremy Sanders
http://www.jeremysanders.net/
-- 
http://mail.python.org/mailman/listinfo/python-list



Re: Nested function scope problem

2006-07-27 Thread Jeremy Sanders
Gerhard Fiedler wrote:

> Going back to the original question... What would be the most
> common/useful way to access variables from the outer function for writing
> from within the inner function?

I've done something like this (which doesn't look very nice)

def myfunc():

   tok = ['']

   def inner():
   tok[0] += 'hi'
   ...
   tok[0] = 'foo'

   inner()
   print tok[0]

-- 
Jeremy Sanders
http://www.jeremysanders.net/
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: pyqt scrollview layout

2006-07-21 Thread Jeremy Sanders
Felix Steffenhagen wrote:

> I have a problem with updating contents in a qscrollview.
> I've implementented two widgets (PremiseInput and PremiseList).
> You can find the source code under
> http://www.informatik.uni-freiburg.de/~steffenh/premiseinput.{html|py} and
> http://www.informatik.uni-freiburg.de/~steffenh/premiselist.{html|py}

You should ask on the PyQt mailing list - you're much more likely to get an
answer.

Jeremy

-- 
Jeremy Sanders
http://www.jeremysanders.net/
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: How to have application-wide global objects

2006-07-14 Thread Jeremy Sanders
Fredrik Lundh wrote:

> no, the "bar.py" *file* gets loaded twice, first as the "foo.bar"
> module, and then as the "bar" module.

True and I agree with your email, but suppose there is bar1.py and bar2.py
in foo, then they can refer to each other by importing bar2 and bar1,
respectively. These module objects will be the same modules that test.py
would get by importing foo.bar1 and foo.bar2.

By analogy you might expect the path munging example to work, but the
details are in the nitty-gritty of how python importing works. The import
docs say something like "Details of the module searching and loading
process are implementation and platform specific". The results can be a
little suprising! It would be include a check so that you could get a
warning with debugging switched on.

-- 
Jeremy Sanders
http://www.jeremysanders.net/
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: How to have application-wide global objects

2006-07-14 Thread Jeremy Sanders
Fredrik Lundh wrote:
 
> if you got some other result, you didn't just import the same thing
> twice...

I think you may be incorrect, or I have misinterpreted you.

Try this:

** In test.py 
import sys

import foo.bar

print foo.bar.myvar
foo.bar.myvar = 42
print foo.bar.myvar

sys.path.insert(0, 'foo')
import bar

print bar.myvar

** In foo/__init__.py 
# this is blank

** In foo/bar.py *
myvar = 10


If you run test.py, then you get the output
10
42
10

When I would have expected 10, 42, 42. The bar module gets imported twice,
once as foo.bar and secondly as bar. The value of 42 in myvar does not get
retained, as there are two copies of the module imported.

-- 
Jeremy Sanders
http://www.jeremysanders.net/
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: How to have application-wide global objects

2006-07-13 Thread Jeremy Sanders
Fredrik Lundh wrote:

> "Sanjay" wrote:
> 
>> Trying hard, I am not even being able to figure out how to create an
>> object in one module and refer the same in another one. "import"
>> created a new object, as I tried.
> 
> "import" doesn't create new objects, so that's not very likely.  can you
> post some code so we don't have to guess what you've tried and not ?

It does if you mess around with sys.path between doing two imports of the
same thing (at least I found out the hard way on Python 2.4). I'm not sure
this is considered a bug or a "don't do that then" problem.

-- 
Jeremy Sanders
http://www.jeremysanders.net/
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: import hook

2006-06-18 Thread Jeremy Sanders
Thomas Heller wrote:

> There are also other ways.  You could extend __path__ of foo, and the
> pkgutil module might also be useful.

The __path__ trick worked nicely, thanks. Here is the code in case anyone is
interested

# Allow veusz to be run even if not installed into PYTHONPATH
try:
import veusz
except ImportError:
# load in the veusz module, but change its path to
# the veusz directory, and insert it into sys.modules
import __init__ as veusz
thisdir = os.path.dirname( os.path.abspath(__file__) )
veusz.__path__ = [thisdir]
veusz.__name__ = 'veusz'
sys.modules['veusz'] = veusz

This is part of the main program. If it can't import it (i.e. it is not
installed), it imports the __init__ module, renames it, and corrects its
path, then sticks it into the list of imported modules.

Jeremy

-- 
Jeremy Sanders
http://www.jeremysanders.net/
-- 
http://mail.python.org/mailman/listinfo/python-list


import hook

2006-06-11 Thread Jeremy Sanders
Hi - Is it possible to override the import process so that if in my program
I do

import foo.bar

Python will look for bar in a directory which isn't called foo?

I want my module/program to be able to be run without being installed in
site-packages, so by doing "import foo.bar", it should start looking for
bar in the current directory which could be called "foo-0.43".

I've tried overriding __import__, chopping out "foo." from package names,
but that tends to break. I've also tried overriding imp.find_module() but
Python never appears to use my version.

Any ideas?

Thanks

Jeremy

-- 
Jeremy Sanders
http://www.jeremysanders.net/
-- 
http://mail.python.org/mailman/listinfo/python-list


Mutual interdependency problem

2006-06-07 Thread Jeremy Sanders
I'm getting problems with modules with interdependencies when using the
import form "import foo.bar as bar".

Here are a set of files which replicate the problem

::
importproblem/__init__.py
::
# This is blank

::
importproblem/dir1/__init__.py
::
from bar import *

::
importproblem/dir1/bar.py
::
import importproblem.dir2 as foo
def hello():
print "Hello world"

::
importproblem/dir2/__init__.py
::
from foo import *

::
importproblem/dir2/test.py
::
import importproblem.dir1.bar as bar
def hello():
print "Hello world"

::
importproblem/dir2/foo.py
::
import importproblem.dir1 as dir1
def hello():
print "Hello world"

If you now do
>>> import importproblem.dir1
Traceback (most recent call last):
  File "", line 1, in ?
  File "importproblem/dir1/__init__.py", line 1, in ?
from bar import *
  File "importproblem/dir1/bar.py", line 1, in ?
import importproblem.dir2 as foo
  File "importproblem/dir2/__init__.py", line 1, in ?
from foo import *
  File "importproblem/dir2/foo.py", line 1, in ?
import importproblem.dir1 as dir1
AttributeError: 'module' object has no attribute 'dir1'

[GCC 4.1.0 20060210 (Red Hat 4.1.0-0.24)]

If you remove the "as dir1" from the import line in dir2/foo.py then this
works. Can this be explained or fixed?

Maybe the "from foo import *" style in __init__.py is a bad idea, but it
allows you to expose a flat namespace without having to put all the code
into one file. Then naturally you need mutual interdependencies, and then
it breaks! I can work around it by removing the "as XXX" parts on the
import statement, but it gets annoying having to specify the full path.

Jeremy

-- 
Jeremy Sanders
http://www.jeremysanders.net/
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Why 3.0/5.0 = 0.59999...

2006-05-09 Thread Jeremy Sanders
Davy wrote:

> Is there some precision loss? And how to overcome it?


See
 http://docs.python.org/tut/node16.html
for some useful information.

-- 
Jeremy Sanders
http://www.jeremysanders.net/
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Printing a file

2006-03-02 Thread Jeremy Sanders
David Boddie wrote:

> That's where QPrintDialog comes in:
> 
>   http://doc.trolltech.com/4.1/qprintdialog.html
> 
> It's also secretly available in Qt 3 via the QPrinter.setup() method:
> 
>   printer = QPrinter()
>   printer.setup()
>   # Now, paint onto the printer as usual.

No - that was in my example. The work I was refering to was taking the
user's input to the dialog and writing the pages to the device in the right
order (I don't think this is done automatically).

Jeremy

-- 
Jeremy Sanders
http://www.jeremysanders.net/
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Printing a file

2006-02-28 Thread Jeremy Sanders
Fabian Steiner wrote:

> Unfortunately I don't know how to realize this, since also some images
> and different boxes should be printed out. As the whole application is
> based on QT, QPrinter might be used, but I couldn't find any examples
> how to use it.

QPrinter is easy to use. You just draw to the page the same way as you talk
to the screen with a QPainter.

prnt = qt.QPrinter()
# you can also vary options like colour, doc name, dpi here

# display dialog box to user (you can actually leave this out)
if prnt.setup():
 painter = qt.QPainter()
 painter.begin(printer)
 # do stuff to draw to painter
 painter.end(printer)
 # do this between each page
 printer.newPage()

 # ... more pages can be printed to a painter

It's very easy to do. If you want to handle multiple pages and so on,
there's a bit of work to do to interface to the dialog to get the
user-selected page range, etc.

Jeremy

-- 
Jeremy Sanders
http://www.jeremysanders.net/
-- 
http://mail.python.org/mailman/listinfo/python-list


  1   2   >