Python in a desktop environment

D

David Cramer

If you had an application that you were about to begin development on
which you wanted to be cross platform (at least Mac and Windows),
would you suggest using c++ and Python?

I'm asking because we were originally thinking about doing c# but
after attending PyCon this year I'm reconsidering. We are already
using Python for the website and I figure a c++ backend w/ a Python
GUI may work really well, and would be pretty easy to port.

Any opinions?
 
M

MonkeeSage

If you had an application that you were about to begin development on
which you wanted to be cross platform (at least Mac and Windows),
would you suggest using c++ and Python?

Depending on what exactly you're trying to do, a pure python solution
may be sufficient, though obviously a C/C++ backend would be portable
as well. In terms of GUI; GTK or QT would probably be your best bet
(my personal preference is for pygtk), though Wxidgets may be a viable
option if the others are not (I'm not sure how advanced GTK/QT support
is on OSX). Your question is kind of vague, so this response is vague
as well. More info is needed to answer in a more specific way.

Regards,
Jordan
 
S

sjdevnull

David said:
If you had an application that you were about to begin development on
which you wanted to be cross platform (at least Mac and Windows),
would you suggest using c++ and Python?

I'd strongly consider a pure python solution (I'd choose wxpython),
but if I needed to code backend stuff in a lower level language I'd
use C rather than C++.
 
D

David Cramer

I'd strongly consider a pure python solution (I'd choose wxpython),
but if I needed to code backend stuff in a lower level language I'd
use C rather than C++.

Well we want it to be very robust, and python isn't exactly the
fastest language, or one with the lowest overhead :)
 
M

Mike Schinkel

Hi,

I'm relatively new to Python but have lots of prior programming experience
as a developer, instructor, and author (ASP/VBScript/SQL Server and
Clipper.)

I'm trying to write an app that parses a text file containing an outline
useing essentially the same indentation rules as Python source code, i.e.
the first level has no indent, the second level has one indent, third level
has two indents, and so on. However, I can't know for sure that the
indentations are tabs or spaces, or even mixed tabs and spaces. What's
more, I can't know for sure what tab spacing the persons editor was using if
they saved as spaces, i.e. tab='N' spaces where N= (2,3,4,5,6,7,8,...)

Clearly the source code for Python has to figure this out, but I wonder if
anyone knows how to do this in Python. Frankly I'm stumped on how to find an
elegant algorithm that does require multipass parsing and lots of code. Any
help would be appreciated.
 
D

Diez B. Roggisch

David said:
Well we want it to be very robust, and python isn't exactly the
fastest language, or one with the lowest overhead :)

Erm, and what has _robustness_ with fast & less overhead to do? It's
exactly the other way round, a python program is much less likely to
crash that C/C++, and if so in manners that are much easier to manage
and fix.

I've done extensive GUI-development with PyQt, and can say that it's a
very productive and powerful combination. And GUI-code usually isn't
troublesome performance-wise.

Diez
 
D

Duncan Booth

David Cramer said:
Well we want it to be very robust, and python isn't exactly the
fastest language, or one with the lowest overhead :)
So if you want it to be robust it's a no brainer: Python and no C++ or C.

I'd advise you to start off doing it in pure Python, then when you get it
working see if it is fast enough, and if not profile it and find the
bottlenecks. Those bits which really do need speeding up you should then
rewrite using Pyrex, which won't take long at all since it is basically the
same syntax. Also see whether Psyco helps.

Python can be slow, but if you leverage the built in functionality
correctly you'll find it isn't much slower than C/C++ for many tasks, and
can even be faster than you would get from C/C++ without a spending a very
long time optimising it (that's because people have already spent a very
long time optimising Python's builtin functionality).

Even if you do eventually find you have to rewrite parts of the code in
C/C++ you should first have done what you can to optimise it in Python.
Usually the best way to speed up something which is too slow is to use a
different algorithm. That sort of change is comparatively easy to do in
Python but an expensive programming exercise in C/C++. When you come to
reimplement it then you'll be converting the already optimised code, which
in many cases will be much easier than writing it in C/C++ from scratch.

If you look at http://page.mi.fu-berlin.de/~prechelt/Biblio/jccpprtTR.pdf
you'll see that, at least for that task, the relative runtimes for C/C++
and Python widely overlap. Yes, the C/C++ implementation included some very
fast ones, but they also included slows ones (and only Python of all the
languages tested had no unusable implementations). OTOH, time to write the
programs does not overlap. Of course, since that paper was written Python
has changed a lot.
 
G

Gabriel Genellina

I'm trying to write an app that parses a text file containing an outline
useing essentially the same indentation rules as Python source code, i.e.
the first level has no indent, the second level has one indent, third

You can get some ideas from tabnanny.py or reindent.py, but perhaps they
are too specific at parsing Python code.

I hope this informal description may help:

Start with IC = Previous IC = 0, and a stack with a single 0 element
For each line in the file:
compute the indentation column IC (that is, count the number of leading
whitespace characters; perhaps replacing tabs as 8 spaces)
compare IC with the Previous IC:
same: continue with next line
IC > previous ("indent"): push IC onto indent stack
IC < previous ("dedent"):
discard top of stack
look at the new top of stack (but dont discard it); if not the same,
indentation error.
Previous IC = IC

Note: You can rewrite the above without using Previous IC, only the
stack... left to the reader :)
Note 2: At each stage, the "indentation level" is the current stack size.
 
H

Hendrik van Rooyen

David Cramer said:
If you had an application that you were about to begin development on
which you wanted to be cross platform (at least Mac and Windows),
would you suggest using c++ and Python?

I'm asking because we were originally thinking about doing c# but
after attending PyCon this year I'm reconsidering. We are already
using Python for the website and I figure a c++ backend w/ a Python
GUI may work really well, and would be pretty easy to port.

Any opinions?

Why drag in the C++ ?
What do you intend to do in it that you can't do in python?
Seems unnecessarily complex to me.
And on a desktop, speed issues are kind of irrelevant - Python
is snappy enough on modern hardware for most things.

My style would be to have the gui connect via pipes to the
local backend as a separate process, with a third process if there
are any communications needed to off box dbs. But that is just
the way I think - YMMV

- Hendrik
 
C

ce

Well we want it to be very robust, and python isn't exactly the
fastest language, or one with the lowest overhead :)

only one word caught my attention "robust".. do u mean python is not
robust???
if this is your feeling, i don't recommend u to "reconsider" anymore,
turn back to ur previous programming language (what did'ya say it
was??? c# .. i remember one ad. about it's robustness .. a piece of
jelly!)

and by the way if u know something about C++, code the GUI with QT
(which would make it portable), and enjoy a high speed execution :p
 
M

Mike Schinkel

Gabriel said:
Start with IC = Previous IC = 0, and a stack with a single 0
element For each line in the file:
compute the indentation column IC (that is, count the
number of leading whitespace characters; perhaps replacing
tabs as 8 spaces)
compare IC with the Previous IC:
same: continue with next line
IC > previous ("indent"): push IC onto indent stack
IC < previous ("dedent"):
discard top of stack
look at the new top of stack (but dont discard
it); if not the same, indentation error.
Previous IC = IC

I went away and reviewed this, but it appears it doesn't tackle the
difficult part which was what made me ask the question in the first place.

The problem is, how do I figure out how many spaces represent a tab? In one
case, someone could have their editor configured to allow tabs to use 3
spaces and the user could intermingle tabs and spaces. In other cases, a
user might have their editor configured to have a tab equal 8 spaces yet
also intermingle tabs and spaces. When a human looks at the document it is
obvious the setting but how can I make it obvious to my program?

I could force the user to specify tabwidth at the top of the file, but I'd
rather not. And since Python doesn't either, I know it is possible to write
a parser to do this. I just don't know how.
 
S

Stef Mientki

The problem is, how do I figure out how many spaces represent a tab? In one
case, someone could have their editor configured to allow tabs to use 3
spaces and the user could intermingle tabs and spaces. In other cases, a
user might have their editor configured to have a tab equal 8 spaces yet
also intermingle tabs and spaces. When a human looks at the document it is
obvious the setting but how can I make it obvious to my program?
then statistics can do it too.
 
G

Gabriel Genellina

The problem is, how do I figure out how many spaces represent a tab? In

You can't, unless you have more context.
one
case, someone could have their editor configured to allow tabs to use 3
spaces and the user could intermingle tabs and spaces. In other cases, a
user might have their editor configured to have a tab equal 8 spaces yet
also intermingle tabs and spaces. When a human looks at the document it
is
obvious the setting but how can I make it obvious to my program?

"it is obvious the setting?"
How do you infer that? From other properties of the document, semantics?
Just from the content, and the number of tabs and spaces, you can't get
anything.
I could force the user to specify tabwidth at the top of the file, but
I'd
rather not. And since Python doesn't either, I know it is possible to
write
a parser to do this. I just don't know how.

Python simply assumes 8 spaces per tab.
If your Python source ONLY uses tabs, or ONLY spaces, it doesn't matter.
If you mix tabs+spaces, Python simple replaces each tab by 8 spaces. If
you edited that using 4 spaces, Python will get it wrong. That's why all
people always say "never mix tabs and spaces"

If you know "somehow" that a certain block -using tabs- has the same
indentation that another block -using spaces- you could infer the number
of spaces per tab.
 
M

Mike Schinkel

Gabriel said:
You can't, unless you have more context.

How does Python do it?
"it is obvious the setting?" How do you infer that? From
other properties of the document, semantics? Just from
the content, and the number of tabs and spaces, you can't
get anything.
From looking at it. It might require changing tab widths in the editor, but
one setting will make it clear.
Python simply assumes 8 spaces per tab. If your Python
source ONLY uses tabs, or ONLY spaces, it doesn't matter.
If you mix tabs+spaces, Python simple replaces each tab
by 8 spaces. If you edited that using 4 spaces, Python
will get it wrong. That's why all people always say
"never mix tabs and spaces"

Ah. I didn't know that. Like I said at first, I am new to Python (but I've
been reading a lot about it! 3 books thus far. Nutshell, Cookbook,
wxPython)

Okay, I'll just use a directive at the top of the file and let the user
specify. Not perfect, but such is life.

Thanks again.
 
P

Paul McGuire

If you mix tabs+spaces, Python simple replaces each tab by 8 spaces.

Are you sure about this? This is not the tab problem I am familiar
with in the past. In the following sample, the columnar text labeled
'col2' should all be aligned at column 9:

x<tab>col2
xxx<tab>col2
<tab>col2

In the first line, the tab is replaced with 7 spaces, the second line
by 5, and the third line by 6. So tab replacement is a bit more
involved than simply "s/\t/ /g".

As I recall, the number of spaces to replace a tab by is something
like 8-(col % 8) where col is the column location of the tab with the
left most column being 0.

-- Paul
 
J

John Nagle

Mike said:
Gabriel Genellina wrote:

That's the one rule in this area Python ought to enforce.
If you've got that, you can convert tabs to spaces, or vice
versa, safely.

John Nagle
 
G

Gabriel Genellina

On Mar 11, 8:37 am, "Gabriel Genellina" <[email protected]>
wrote:
As I recall, the number of spaces to replace a tab by is something
like 8-(col % 8) where col is the column location of the tab with the
left most column being 0.

Yes, each tab advances to the next multiple-of-8 column. The problem is
that "8" is fixed.
 
B

Bruno Desthuilliers

David Cramer a écrit :
If you had an application that you were about to begin development on
which you wanted to be cross platform (at least Mac and Windows),
would you suggest using c++ and Python?

I'm asking because we were originally thinking about doing c# but
after attending PyCon this year I'm reconsidering. We are already
using Python for the website and I figure a c++ backend w/ a Python
GUI may work really well, and would be pretty easy to port.

Any opinions?
Yes : forget about the c++ backend.
 
B

Bruno Desthuilliers

David Cramer a écrit :
Well we want it to be very robust,

Which is a pretty good reason to favor Python over C or C++.
and python isn't exactly the
fastest language, or one with the lowest overhead :)

It's not exactly one of the slowest languages nor one with the highest
overhead neither. Chances are you'll have a fully-functional production
level app in Python *way* before you have a first alpha in C++. If by
then you *really* have some performance problem, it will be time to port
the relevant modules to C extensions.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,774
Messages
2,569,598
Members
45,161
Latest member
GertrudeMa
Top