Wrapping C functions in Pyrex and distutils problem

2005-11-09 Thread TPJ
I'm trying to get a wrapper for my C code in order to be able to use it
as a module in Python. I'm doing it as follows:

C code (file_c.c):
---
#include 

void hello( int size )
{
 printf("Hello! %d\n", size);
}
---

Pyrex code (file.pyx):
---
cdef extern void hello( int size )
---

Python code (setup.py):
---
from distutils.core import setup
from distutils.extension import Extension
from Pyrex.Distutils import build_ext

setup(
name = "File",
ext_modules=[
Extension( "file", ["file.pyx", "file_c.c"] )
],
cmdclass = { 'build_ext': build_ext }
)
---

After

$ python setup.py build_ext --inplace

all is compiled ok, but the shared library (file.so) is built only from
one file (file_c.o) - and the second object file (file.o) is ignored.
Of course it's imposible to import such a module in Python.

What am I doing wrong?

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


A tool for Python - request for some advice

2005-06-20 Thread TPJ
First I have to admit that my English isn't good enough. I'm still
studying and sometimes I just can't express what I want to express.


A few weeks ago I've written 'Python Builder' - a bash script that
allows anyone to download, compile (with flags given by user) and
install Python and some external modules (e.g. wxPython, PyGTK,
Numeric...). I use Python every day and when new version of Python (or
some external module) is released, I want to check if my projects will
run on this new version (sometimes I want to know if my projects will
run on specific combination of older Python and some older external
modules...). It happens four - five times a year. Frequently not enough
to memorize all commands used to configure, compile and install all of
this software (so I have to read documentation every time I'm doing it)
- but frequently enough to consume considerable amounts of my working
time. So I thought that writing a script to do it all automatically
(download sources, unpack them, configure them, compile them and then
install them; all of this with options given by me - the user) would be
a good idea.

(It's not about using emerge, get-apt or some another tool of this
kind. My need is very special - I want to have Python, it's modules and
sometimes documentation installed in particular place; usually
somewhere in my home directory. My script installs some additional
scripts which I can use to run Python. These additional scripts have
properly set LD_LIBRARY_PATH so they are able to run Python and
external modules that require libraries installed in non-standard
locations.)

I've written this script in bash, because I thought it would be better
to have a script which would run in environment without Python (it all
was about installing Python anyway!). I used bash, dialog, wget... And
now someone suggested, that I shuld use Python. That using Python would
lead to clearer and - probably - smaller code. (I have to admit it - my
code in bash is just messy.)

And now I'm considering this idea. Python is already present on
(almost?) every distribution today, so why worry about it's presence?
Writing in Python would lead to easier i18n (for now it's all in
Polish...) and to easier implementation of future enhancements (due to
language's features and much more clearer coding style in Python). Well
- I've already found it hard to implement new features in pure bash.

But, on the other hand, I'm thinking that writing in bash is more
universal solution. I mean that requirements to run bash scripts are
lower than requirements to run Python scripts. Could this requirements
be decisive for some users (or is it only my imagination)? Sometimes
users just have no access to Python (e.g. LFS, some stages of Gentoo,
some rescue and specialized distros).


And there's also something more. For now "Python Builder" works in text
mode. It was written in bash and I assumed it should be some kind of
"basic" tool - so text mode and bash seemed to be the best choice. But
if I rewrote all code in Python I could make some GUI. It could works
even better - I could easily check if GUI mode is available and run in
proper (GUI or text) mode. But is GUI needed by people who just want it
to do it's job and quit?


Well, what do you think?

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


Re: A tool for Python - request for some advice

2005-06-24 Thread TPJ
> (...) I've seen the claim that every Linux
> distro comes with Python installed, but can't verify it.

So have I. And I think it might be truth. The only problem is that
different distros might be released with different versions of Python.



> Then again, the same comments apply to bash. Distributions that have
> their roots in AT&T Unix probably don't come with bash by default,
> with Mac OS X being an exception. This makes depending on bash a bad
> idea if you want to write a script that portable across Unix distros.

Good to know. So my script written in bash will be primary targeted for
Linux distros. OK, I can live with that.

> If your target platform is Linux, indications are that python is as
> portable as bash.

I've thought about it for a few days and I disagree with you. Python
isn't as portable as bash because of one reason. The problem is that
development of Python is much more dynamic than development of bash. If
I wrote my script in Python it would be possible that this script
wouldn't run on the same distro with _different_ version of Python.

(This is problem because someone has sugested that he'd like to run
this script on RH 9. So older distros must be considered as well as
newer.)

As long as I'm using bash, I'll deal with a tool that has the same
features on different distros (well... _almost_ the same features)

(This is about portability, of course. There are also another
advantages of using bash.)

> If your target platform is Unix, then the same is true - except you
> shouldn't be writing bash if you want portability.

Well, my primary goal is Linux. I'm working on Linux and I have no
access to any *BSD system (or another flavour of Unix). I thought that
bash scripts are portable, but now I can see I was wrong.

Thanks for all your help.

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


Re: A tool for Python - request for some advice

2005-06-24 Thread TPJ


> So, to make sure I've understood (...) : you want to provide
> a specialized install script for Python (3rd party modules,
> non-standard locations, etc.)

yes

> You started in bash to deal with minority cases without an extant
> Python install.

My motivation was rather to avoid a bootstrap-like problems. I thought
that writing a script in _Python_ for compiling _Python_ is just silly
idea. I thought that bash (available even in rescue distros!) would be
a better tool.

> But clearly, there will be a Python install by the
> time your bash script is done, or something will have gone very wrong.

yes

> That suggests implementing the custom installation work in Python, and
> having a bash script that will



Well... not exactly.

My primary goal is to save time spent on downloading, uncompressing,
reading documentation, configuring, compiling and installing software.
So I want to write a tool that would allow me to spent a few minutes to
tell it what to do - and then it would do it all. I would pay attention
to this tool for a few minutes, instead of wasting much more time doing
it all myself.

I could check for a Python installation and install Python in standard
way in case Python wouldn't be available but this would have following
disadvantages:
1) It would be a large waste of time spent on installing Python in
standard way. (Especially if user wouldn't like _standard_ Python
installation. I'm working on this script because in most situations I'm
not happy with such standard installation!)
2) It would lead to download a particular Python's version for a
temporary purpose - and this means additional waste of time and
bandwith.

For now my script works as follows:
1) It allows user to configure all options and actions.
2) When user is ready it just does what it is supposed to do.

The first stage is very important - it takes a few minutes and then all
is done automatically.


Thanks for your advice - it was really helpful.

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


Re: A tool for Python - request for some advice

2005-06-24 Thread TPJ
I've heard about this "EasyInstall" and I like this idea.

If EI becomes a part of Python's standard library, my script will use
it.

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


PyGTK or wxPython (not a flame war) on Windows

2005-07-22 Thread TPJ
GUI's etc: PyGtk on Windows
"(...) So if someone develops mainly for X and just wants to make sure
that it is not impossible to run on Windows, you can use PyGTK. (...)",
July 2nd, 1999

pyGTK on Windows
"(...)
> can i use pyGTK under
> Windows???

It's probably doable (...) but not worthy in my oppinion (...). A much
better choice is wxPython (...)"

"(...)even if it is made to work under windows, pygtk would not have a
windows look and feel.  wxPython is probably your best bet (...)"

pygtk vs. wxPython
4. "(...) If you want cross-platform capabilities (...) then go
wxWindows." May 17 2002

7. "(...) The pygtk (and gtk port in general) does not yet support
threading on windows. (...) GTK 2.0 is supposed to fix it but support
isn't available *yet*. (...)" May 17 2002

PyGTK vs. wxPython
7. "(...) wxPython would indeed be a better choice if your applications
are only to run on a certain infamous legacy operating system from the
Pacific Northwest.  The PyGTK is a better choice if you are writing for
Linux and want your application to also be able to run on windows.
(...)" Apr 25

11. "(...) I'm using wxPython because GTK for windows wasn't ready
three years ago when I initially had to write my first Windows
application.  If I evaluated both of them again today, I might choose
GTK, and I might not." Apr 27



In the nearest future I will have to decide what to use: PyGTK or
wxPython. I like those both APIs. wxPython has more widgets, but PyGTK
seems to be faster. I can use them both for free (it's very important).
My only concern is that although I'm doing development on Linux, I'd
like to make my application runnable on Windows as well (Py2Exe). I'd
like to choose PyGTK (because of its rich documentation), but I'm not
sure if PyGTK is stable on Windows... For now I know that wxPython runs
well on Windows.

For now I haven't experienced any problems with wxPython on Linux
(Slackware, Aurox /Polish RH-like distro/). I used wxPython on Linux,
but I stopped because of its poor documentation (mainly C++ docs, not
Python docs). But recently I noticed this documentation got better
(*much* better!).

How well does PyGTK run on Windows (98, 2K, XP)? How stable is it? Will
I be able to make an executable (using Py2Exe) of an application that
uses PyGTK?

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


Re: PyGTK or wxPython (not a flame war) on Windows

2005-07-24 Thread TPJ
> I've used pygtk with success on windows. (...)
> > [will] I be able to make an executable (using Py2Exe) of an application
> > that uses PyGTK?
>
> Yes.

So PyGTK is now my favourite. Better documentation, runs on Linux and
Windows, the possibility to make an executable program with Py2Exe.
It's enough for me.

> One point against: requires X11 on a Mac; definitely not native there,
> though that's where I do a lot of my pygtk development.

I suppose that 95% of my application's users will work on Win. The rest
will work on *nix. So my primary concern is PC world.

AFAIK PyGTK doesn't look native on Win as well, but I don't care.

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


Re: PyGTK or wxPython (not a flame war) on Windows

2005-07-24 Thread TPJ
> PyQt works equally well on both systems.

I believe you. The problem is I don't like GPL.

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


Re: PyGTK or wxPython (not a flame war) on Windows

2005-07-24 Thread TPJ
Thanks a lot!

Now I know I can choose PyGTK. I really like it because of its rich
documentation.

> You could also bundle the runtime DLLs with your py2exe'd application

That's great. I think my clients will appreciate a single one
executable.

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


Re: Friend wants to learn python

2005-07-24 Thread TPJ
Why not start with Python's standard documentation? There are Python
Tutorial and Library Reference. IMHO it's the best place to start.

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


Re: PyGTK or wxPython (not a flame war) on Windows

2005-08-04 Thread TPJ
I have encountered some problems with PyGTK only when I was trying to
install a PyGTK version that was different from the installed GTK+
version. When those both versions were the same, I had no problems at
all.

(Another problem with PyGTK is that it's installation is somewhat more
complicated that installation of wxPython, but I wrote a script that
can download and install PyGTK. The user have to tell where all this
stuff should be installed to and the script just does the rest.)

Tkinter is out of question. It looks ugly. I'd like to write a program
that would look nice.

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


Re: installing python2.4.1

2005-08-05 Thread TPJ
There are two things I don't like about messages you got.

>  checking for g++ ... no

and

>  C++ compiler cannot create executables

There are two possibilities I can think of now. The first is you have
no g++ installed. In this case you should install it (I think it would
be very easy, you could install it from the proper deb package).

The second is there is a working g++, but ./configure fails to detect
it.

BTW: Have you tried to compile a simple C program by yourself?

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


Some simple performace tests (long)

2005-08-06 Thread TPJ
"The advantage of xrange() over range() is minimal (since xrange()
still has to create the values when asked for them) except when a very
large range is used on a memory-starved machine or when all of the
range's elements are never used (such as when the loop is usually
terminated with break)." - from Python Library Reference.

I decided to measure the performance of range and xrange. I did it with
the following functions:

def rprint( n ):
  a = time.time()
  for i in range(n): print i
  print time.time() - a

def xrprint( n ):
  a = time.time()
  for i in xrange(n): print i
  print time.time() - a

def rpass( n ):
  a = time.time()
  for i in range(n): pass
  print time.time() - a

def xrpass( n ):
  a = time.time()
  for i in xrange(n): pass
  print time.time() - a


The results were as follows:

n   rprint  xrprint

10^40.37 s  0.34 s  <- (1)
10^54.26 s  4.25 s
10^642.57 s 42.57 s
10^7431.94 s438.32 s<- (2)

n   rpass   xpass

10^40.0012 s0.0011 s
10^50.0220 s0.0139 s
10^60.1463 s0.1298 s
10^71.4818 s1.1807 s

The values are the average times printed by tested functions.

Conclusions:

1) According to (1) I could say that xrange might be somewhat faster
than range with lower numbers of iterations.
2) According to (2) I could say that xrange might be slower than range
with higher number of iterations.

The test with pass is not so important as the test with print (because
we usually do something inside of loops). So despite xpass has beaten
rpass, I would say that range is not slower than xrange (especially for
higher numbers of iterations). The final conclusion is : if you want
speed, you should use xrange privided that there aren't many
iterations. If you want less memory usage, you should use xrange.


I've also made more tests. The code was as follows:

-
import array, random, time

def test1( n, size ):
  a = time.time()
  for i in xrange(n):
l = []
for i in xrange(size):
  l.append( random.randint( 1,10 ) )
e = sum(l) / float(size)  # just for taking some time
  print time.time() - a


def test2( n, size ):
  a = time.time()
  l = []
  for i in xrange(n):
del l[:]
for i in xrange(size):
  l.append( random.randint( 1,10 ) )
e = sum(l) / float(size)
  print time.time() - a


def test3( n, size ):
  a = time.time()
  l = range(size)
  for i in xrange(n):
for i in xrange(size):
  l[i] = random.randint( 1,10 )
e = sum(l) / float(size)
  print time.time() - a


def test4( n, size ):
  a = time.time()
  l = array.array( 'L', xrange(size) )
  for i in xrange(n):
for i in xrange(size):
  l[i] = random.randint( 1,10 )
e = sum(l) / float(size)
  print time.time() - a


def test5( n, size ):
  a = time.time()
  ind1 = range(size)
  ind2 = range(size)
  for i in xrange(n):
des1 = []
des2 = []
point = random.randint( 1, size-1 )
des1 = ind1[:point] + ind2[point:]
des2 = ind2[:point] + ind1[point:]
  print time.time() - a


def test6( n, size ):
  a = time.time()
  ind1 = range(size)
  ind2 = range(size)
  des1 = []
  des2 = []
  for i in xrange(n):
del des1[:]
del des2[:]
point = random.randint( 1, size-1 )
des1 = ind1[:point] + ind2[point:]
des2 = ind2[:point] + ind1[point:]
  print time.time() - a


def test7( n, size ):
  a = time.time()
  ind1 = range(size)
  ind2 = range(size)
  des1 = range(size)
  des2 = range(size)
  for i in xrange(n):
point = random.randint( 1, size-1 )
des1[:point] = ind1[:point]
des1[point:] = ind2[point:]
des2[:point] = ind2[:point]
des2[point:] = ind1[point:]
  print time.time() - a


def test8( n, size ):
  a = time.time()
  ind1 = array.array( 'L', xrange(size) )
  ind2 = array.array( 'L', xrange(size) )
  des1 = array.array( 'L', xrange(size) )
  des2 = array.array( 'L', xrange(size) )
  for i in xrange(n):
point = random.randint( 1, size-1 )
des1[:point] = ind1[:point]
des1[point:] = ind2[point:]
des2[:point] = ind2[:point]
des2[point:] = ind1[point:]
  print time.time() - a
-

And this is my session with Python 2.4.1:

>>> for i in xrange(5): test.test1( 1, 10 )
...
2.27345108986
2.51863479614
2.49968791008
2.68024802208
2.28194379807
>>> for i in xrange(5): test.test2( 1, 10 )
...
2.54866194725
2.36415600777
2.71178197861
2.32558512688
2.71971893311
>>> for i in xrange(5): test.test3( 1, 10 )
...
2.29083013535
2.5563249588
2.32064318657
1.90063691139
2.30613899231
>>> for i in xrange(5): test.test4( 1, 10 )
...
2.55809211731
2.42571187019
2.59921813011
2.19631099701
2.16659498215
>>> for i in xrange(5): test.test5( 1, 10 )
...
0.318142175674
0.442049980164
0.367480039597
0.327154874802
0.322648048401
>>> for i in xrange(5): test.test6( 1, 10 )
...
0.356222867966
0.471677780151
0.332046031952
0.339803934097
0.48833990097
>>> for i

Re: Standalone applications ?

2005-08-14 Thread TPJ
> I should have added that my platform is Linux.

In this case you shouldn't bother yourself with executables. Python is
available on any major distribution.

My Python apps are available as pyc files for Linux (and for those
Windows users, who have Python installed) and as executables for Win
(for those Windows users, who have no Python installed).

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


Re: Standalone applications ?

2005-08-15 Thread TPJ
It's just the same as with Java, GTK+, Qt or any other
library/platform. If you're doing development with Java, you can use
any version that you like (for example the version 1.5) - and your
application probably won't be able to run by an older version of Java.
The same is with, for example, GTK+ library - if you use the version
2.6, your application might not work on systems with older version of
the GTK+ library.

And the same is with Python. When you develop application with Python,
you must use some version of Python - and this is your dependency. You
might say that your application needs a particular version of Python
(and eventually particular versions of external modules).

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


Re: GUI tookit for science and education

2005-08-15 Thread TPJ
Recently I was considering the choice of PyGTK or wxPython. They are
both rich GUI libraries, and they both are cross-platform ones (well...
they work on GNU/Linux and on Windows).

I chose PyGTK, because it has *much* better documentation (I wasn't
very happy when I had to look for information in documentation of
wxPython - and lose a couple of hours - when I wanted to do something
really simple...) and it seems to work stable on Windows.

> Let's say someone has big amount of algorithms and
> statistical models implemented in Pascal
> (not well designed console apps).

OT: I would recommend the use of Python + Numeric and, eventually, C
(when the performance becomes really crucial). I'm working on genetic
algorithms and - for now - the combination of Python and C was the best
I got ever. In a few days I will run the first version of GAs
implemented with Numeric.

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


Re: Standalone applications ?

2005-08-17 Thread TPJ
Well, I think I must answer to my own post...

There is at least one reason to bother with executables. I have just
read a message. Someone wanted to run a program written in Python +
wxPython. But he has failed. Why? Because the version of wxPython on
his system was newer and it's API was different.

Of course he could install an older (the proper) version of wxPython as
well... But he's not a developer. He don't know how to install Python,
wxPython and so on... And he's not interested in such knowledge.

So there *is* a reason for making executables.

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


Re: PyGTK or wXPython?

2005-09-13 Thread TPJ
> Beside this, wxPython (and wxWidgets) is often told to be more complete,
> better documented and better supported than GTK/PyGTK.

Is wxPython often told to be more documented? By who?

Several months ago I wanted to choose a nice GUI for Python (Tkinter
was out of question). I choosed PyGTK for one reason - it was much,
much better documented than wxPython.

I have tried to write programs using wxPython and I have always ended
looking for some details in wxPython's docs for hours. I think that
wxPython has more widgets (and therefore one can say it's more
complete) - but it's an advantage only when these extra widgets are
needed. It has also another advantage over PyGTK - the native look and
feel. Write a program in wxPython and it will look natively on the
system it will be run. But the documentation of wxPython is just ugly
in comparison to the documentation of PyGTK. (Well - it was so in June
this year.)

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


Re: Python game coding

2005-09-19 Thread TPJ
OT:

> BTW: I wonder if and when someone will use stackless python (...)

And what is this stackless python? I have visited it's homepage, but I
wasn't able to find any answer. (Well, I have found out, that stackles
python is python's implementation that doesn't use C stack, but it
tells me nothing...)

Is this stackless python faster or slower than CPython? Does anybody
know something?

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


A problem with some OO code.

2006-02-04 Thread TPJ
Help me please, because I really don't get it. I think it's some stupid
mistake I make, but I just can't find it. I have been thinking about it
for three days so far and I still haven't found any solution.

My code can be downloaded from here:
http://www.tprimke.net/konto/PyObject-problem.tar.bz2. There are some
scripts for GNU/Linux system (bash to be precise).

All you need to know is that there are four classes. (Of course, you
may generate all the documentation by typing just "./make_docs". All
you need is Python with Epydoc installed.)

In the PyObject.py there are two classes: Object and OManager. When
Object wants to know if some link is safe (in another words: it wants
to know if some another object exists), it calls the proper method of
its objects' manager (the OManager instance). Details are not important
here, so I'll just say, that if the link is safe, the objects' manager
calls the proper method of the object, that sent him request. This
method of the object just stores the information about that link (that
it's safe), in the so-called private field (the fields used to store
the information about the links are called __ls_existing_links and
__ls_demanded_links).

And now the tests. In the test.py file this is a class called Obj. That
class is just an Object. This is also a class called System1 - this
class is used only to create a more complex system composed of many
objects and one objects' manager.

You can run all the tests by just typing "./test" in console (or a
terminal emulator). I'm still getting the same error:

--
Traceback (most recent call last):
  File "test.py", line 142, in test04CreateSystems
system1 = System1()
  File "test.py", line 81, in __init__
self.objA.demandNewLink( 'B' )
  File "/home/tpj/opt/Praca/Programy/PyObject/workdir/PyObject.py",
line 503, in demandNewLink
s_object )
  File "/home/tpj/opt/Praca/Programy/PyObject/workdir/PyObject.py",
line 800, in _makeNewLink
o_srcobj._makeLinkSafe( s_target )
  File "/home/tpj/opt/Praca/Programy/PyObject/workdir/PyObject.py",
line 608, in _makeLinkSafe
self.__makeLinkSafe( s_object )
  File "/home/tpj/opt/Praca/Programy/PyObject/workdir/PyObject.py",
line 582, in __makeLinkSafe
i = self.__ls_demanded_links.index( s_object )
AttributeError: 'Obj' object has no attribute
'_Object__ls_demanded_links'
--

Perhaps I'll explain what's going on there. First, the objects' manager
is created, and then also some objects are created. After doing that,
the object called "objA" demands a new link to the other object, called
"objB" (this is the line 81 in the error message above). As the result,
the "demandNewLink" method of the object "objA" is called. This method
calls the "_makeNewLink" method of the objects' manager, and this
method calls the "_makeLinkSafe" method of the object, that demanded a
new link. The last method, called "__makeNewLink" (so this is a
so-called private method), is called then. This last method is supposed
to check if the link was really demanded and then make it "safe" by
placing the information about this link in the proper dictionary
(__ls_existing_links).

All these methods are the methods of the Object class (defined in
PyObject.py), not of the Obj class (defined in test.py). According to
my knowledge about the OO programming Python should be able to realize,
that the so-called private fields, that are being referred from these
Object class methods, are the private fields of the *Obj class
instance*, not of the *Object class one*.

I even wrote a simple program to test the usage of the so-called
private fields in Python classes:

--
class A( object ):
  def __init__( self ):
self.__a = 1
  def _add( self ):
print "self.__a =", self.__a
self.__a += 1
  def print_a( self ):
print self.__a

class B( A ):
  def __init__( self ):
A.__init__( self )
self.__b = 2
  def print_b( self ):
print self.__b

def test():
  o = B()
  o.print_a()
  o.print_b()
  o._add()
  o.print_a()
  o.print_b()
--

The code works just as I thought it should work: all is fine here. The
B class instance (the b object) is able to refer to the so-called
private field of the A class (through a A-class method, that was
inherited by the class B).

Why I can't do the same thing in test.py and in PyObject.py?

I just don't get it.

(And sorry for the subject of this topic. I really had no better
idea...)

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


Re: A problem with some OO code.

2006-02-04 Thread TPJ
Thank you VERY much!

These other errors are not so serious. I think I'll fix them soon. The
most important thing is that now I'm ready to work on that code again.
Life is beautiful again!

Damn, I knew it is some very stupid mistake. I must learn how to use
that pylint, instead of asking people on the web such a stupid
questions.

(The sad thing is that I'm the only one developer, who works on that
code. I think I had just stuck before I sent the first message in this
topic.)

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


Re: A problem with some OO code.

2006-02-04 Thread TPJ
 manager, what's the name of the object-receiver.

The only problem is that the object-sender has no idea if the
object-receiver exists.

A situation, when object-receiver doesn't exist, shouldn't happen, but
as long as code is written by people, such a mistake can be done.
Therefore I developed a mechanism, that may prevent such a situation.

Every object-sender must create a link to the object-receiver. A link
is just the name (id) of the object-receiver. There's only one
OO-object in the system, that knows, if the object-receiver exists.
It's the objects' manager. So the object-sender "asks" its objects'
manager, if the link from that object-sender to the object-receiver is
"safe" (in another words: if the object-receiver exists). If the
object-receiver exists, the objects' manager just informs the
object-sender (that might be also called the object-requester), that
the link is safe (and can be used).

But if the object-receiver doesn't exist, the objects' manager doesn't
inform the object-requester about anything.

Now, there are two different scenarios, that might happen:

a) The object-receiver will be created - this is the desired
situation. When the object-receiver will be registered with the
objects' manager, the objects' manager will inform the
object-requester, that the object-receiver exists and the link is
safe.

b) The object-receiver won't be created. Because the object-requester
tracks all the demanded links, it knows, that not all of these links
are safe - and therefore it knows, that it's not ready to work. In the
final version of the module, the objects' manager will be able to
check, if there are any objects, that aren't ready to work. In such a
case the whole system just won't work (and it'll explain the
developer why).

It's not so complicated, and I suppose it will help me to create less
error-prone code in the future.

> I see you have a line "i = self.__ls_demanded_links.index( s_object )". It
> looks like you are storing your data in a chunk of text, and then running
> index on that text to find the bit you want to work with. If you wanted to
> do that much work, just write your script in bash and be done with it!
> Python has lists and dicts and other advanced data structures. Use them.
> You will be glad.

The reason, why a list was used in this case, is now obsolete and it
will probably change in the future.

But, in general, you're right. The dictionary would be much better in
this case even in the previous version of my module.

And... I know how great are lists, dicts and tuples. That's the reason
I threw out Java, C++ and (in most cases) C, and fell in love with
Python. In my personal opinion the adventure with Python begins in the
moment, when you realize, how easy are lists, dicts and tuples to use.
(Yes, I know, there are also sets, but I don't use them at all. So
far...)

> So Obj is an alias for Object? What's an Object? Is it the same as the
> Python built-in type "object"?
>
> But later on you say:
>
> "All these methods are the methods of the Object class (defined in
> PyObject.py), not of the Obj class (defined in test.py)."
>
> So Object and Obj are not the same.

No. I think that the declaration of the Obj class will explain
everything:

class Obj( Object ):
  ...

So the Obj class is an *extension* of the Object class. Therefore I
wrote that "Obj is just an Object". I (mis?)used the vocabulary from
OO: something may *be* a kind of something else (inheritance), or it
may *has* something else (composition). But I'm not sure about those
words; I have read many books in Polish, not in English.

> > --
> > Traceback (most recent call last):
> >   File "test.py", line 142, in test04CreateSystems
> > system1 = System1()
> >   File "test.py", line 81, in __init__
> > self.objA.demandNewLink( 'B' )
> >   File "/home/tpj/opt/Praca/Programy/PyObject/workdir/PyObject.py",
> > line 503, in demandNewLink
> > s_object )
> >   File "/home/tpj/opt/Praca/Programy/PyObject/workdir/PyObject.py",
> > line 800, in _makeNewLink
> > o_srcobj._makeLinkSafe( s_target )
> >   File "/home/tpj/opt/Praca/Programy/PyObject/workdir/PyObject.py",
> > line 608, in _makeLinkSafe
> > self.__makeLinkSafe( s_object )
> >   File "/home/tpj/opt/Praca/Programy/PyObject/workdir/PyObject.py",
> > line 582, in __makeLinkSafe
> > i = self.__ls_demanded_links.index( s_object )
> > AttributeError: 'Obj' object has no attribute
> > '_Object__ls_demanded_links'
>
> [snip]
>
> > Perhaps I'll e

Building Python extensions from sources on Windows

2006-03-25 Thread TPJ
Hello, I have another, probably stupid, question.

I'm working on some Python project, and I use some extensions written
in C. I do all the development on my GNU/Linux box, so my setup.py
script works just as it's supposed to work on a GNU/Linux system. But
in the nearest future I'll have to make an executable program for
Windows.

I know, that there are some useful tools to make executables from
Python programs for Windows (Py2Exe, PyInstaller - that's what I have
heard about), but, as far as I understand the subject, I'll need the
extensions modules (dll files? on my GNU/Linux system I always get some
so files - shared libraries) in the compiled form in order to make any
executable program.

In the Python standard documentation I have read, that:

"(...) Since the metadata is taken from the setup script, creating
Windows installers is usually as easy as running:

python setup.py bdist_wininst
(...)

If you have a non-pure distribution, the extensions can only be created
on a Windows platform, and will be Python version dependent. (...)"

And that's the problem: I understand the fact, that in order to build a
non-pure distrubution, all the C sources have to be compiled (to dll
libraries?). But there's the problem: I don't know which one compiler
should I use. Do I have to use the same compiler, that the Python has
been compiled with? If so, which one of the Windows compilers has been
used to compile Python?

Or perhaps could I use an another compiler? Which one, then?

I have no idea what to do. I haven't done anything on Windows for so
long...

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


A problem with exec statement

2006-04-13 Thread TPJ
I have the following code:

---
def f():

  def g():
a = 'a' # marked line 1
exec 'a = "b"' in globals(), locals()
print "g: a =", a

  a = 'A'   # marked line 2
  exec 'a = "B"' in globals(), locals()
  print "f: a =", a
  g()

f()
---

I don't understand, why its output is:

f: a = A
g: a = a

instead of:

f: a = B
g: a = b

All works as intended, if the marked lines are commented out. I just
don't understand, why. (I suppose I don't understand, how the exec
statement works, or the way Python handles objects, their names and
namespaces...) In my opinion (according to my knowledge about Python),
with or without the marked lines commented, the code should work the
same. Well - I think I have to learn more about Python...

According to my knowledge, the most important are the namespaces: the
local ones, in this case. When Python calls the f function, its
namespace is created. This namespace contains only the g function.
Then the a variable is created (and the "a" name is added to the f
function namespace).

The next statement is the exec one. Since the statement "knows" the
local namespace (obtained from the locals() function), it should
replace the value of the a variable in the local namespace with the
value of the new string "B". I don't understand, why this is not done.

The situation in the g function is similar, the only difference is
that the local namespace contains the "a" name, that refers to a
different Python object.

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


Re: A problem with exec statement

2006-04-14 Thread TPJ
> Use the exec statement without the in-clause to get the desired effect:
>
> >>> def f():
> ... a = "a"
> ... exec "a = 'B'"
> ... print a
> ...
> >>> f()
> B
> 

Well... I *do* realize that. But this is *not* my problem. I have a
function with another nested one. If I used "exec ..." instead of "exec
... in some_dict, some_dict" I would get the "SyntaxError: unqualified
exec is not allowed in function 'f' it contains a nested function with
free variables".

To be honest, the message cited above is the answer to the question
"Why have I put those globals(), locals() in the exec statments?".

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


Re: A problem with exec statement

2006-04-14 Thread TPJ
> 
> So when you exec 'a = "B"' in globals(), locals() you might think you
> were changing the local namespace. In fact you are changing a copy of
> the local namespace:
> 

Well, that explains much, but not all that I want to be explained. Why?
Because now I understand, that by invoking

exec "a = 'B'" in globals(), locals()

I can modify only *copies* of the global and local namespaces dicts,
not the dicts themselves. OK, that's why my code doesn't work as I want
it to work.

But why on Earth *the same code* will work, if I remove the assignments
from the marked lines? Why then operating on copies of the local
namespaces dicts *will work* ?

> (...) Even allowing for the
> difficulties you've already experienced, it's nearly always better in
> practical cases to use assignment to the keys of a dictionary. Then no
> exec is required, and you have direct control over your own namespace.

Well... Is this a sugestion, that instead of messing up with the exec
statements used to modify local namespaces I should use dictionaries?

Perhaps you're right. In fact, the problem, that I'm trying to solve is
as follows:

def funcA():

  def funcB():
...
var1, var2, var3, ..., varN = ( None, ) * N
t = ( (regexp1, 'var1'), (regexp2, 'var2'), ..., (regexpN, 'varN')
)
for regexp, var_name in t:
  match = regexp.match( some_string )
  if match != None:
  # now the *magic* exec statement comes...
exec var_name + ' = match.groups()[0]' in globals(), locals()
return var1, var2, var3, ..., varN

  ...
  k1, k2, k3, ..., kN = funcB()

Of course, the code presented above doesn't work. It works, if one
change is done in the function funcB:

  def funcB():
...
# no creation of any local variables called var1, var2, ..., varN
here
t = ( (regexp1, 'var1'), (regexp2, 'var2'), ..., (regexpN, 'varN')
)
for regexp, var_name in t:
  match = regexp.match( some_string )
  if match != None:
  # now the *magic* exec statement comes...
exec var_name + ' = match.groups()[0]' in globals(), locals()
  else:
  # here we put the code, that will assign None to the variable
exec var_name + ' = None'
return var1, var2, var3, ..., varN

But I *still* don't understand, why this code works, if I operate on a
copy of the local namespace dict...

Of course, I can do the same thing in a different matter - by using a
dictionary. And perhaps I will. But I still want to know, how the exec
statement works.

* * *

My problem is more complicated, that the presented example. In general,
my problem is: how to create a local variable by executing the Python
code, that isn't known at the moment of writing the program? In another
words: I have to create a local variable, whose name will be known at
the runtime, in a nested function.

Is it possible, or have I to use dictionaries, instead of exec
statement used to modify local namespaces?

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