[FAQTS] Python Knowledge Base Update -- June 9th, 2000

Fiona Czuczman fiona at sitegnome.com
Fri Jun 9 05:26:54 EDT 2000


Hi Guys!!

Below are the entries I've entered into http://python.faqts.com

Cheers,

Fiona Czuczman


## New Entries #################################################


-------------------------------------------------------------
Has anyone successfully compiled the Python Interpreter on a Solaris 2.6?  If so, do you know where DIR is declared?
http://www.faqts.com/knowledge-base/view.phtml/aid/3612
-------------------------------------------------------------
Fiona Czuczman
Thomas Wouters,Dieter Maurer

Problem:

I downloaded the source for the Python Interpreter from
http://www.python.org/download/download_source.html.  When trying to
build it on Solaris 2.6 (SunOS 5.6), using gcc for Solaris 2.6, I get 
the following errors:

---------------------------------------------------------------------
./posixmodule.c: In function `posix_listdir':
./posixmodule.c:944: `DIR' undeclared (first use in this function)
./posixmodule.c:944: (Each undeclared identifier is reported only once
./posixmodule.c:944: for each function it appears in.)
./posixmodule.c:944: `dirp' undeclared (first use in this function)
./posixmodule.c:945: parse error before `struct'
./posixmodule.c:958: `ep' undeclared (first use in this function)
make: *** [posixmodule.o] Error 1
----------------------------------------------------------------------

Solution:

Did you run "./configure" before "make"?

We compiled Python on Solaris 2.4/5/6/7 without any problems.

"DIR" usually is defined in "sys/dir.h" or "ndir.h" or "sys/ndir.h".

If you did run "configure", then maybe you "gcc" is not properly 
installed.

DIR is a standard define in one of the standard UNIX header files, 
usually dirent.h (either directly or indirectly.) If you are missing 
dirent.h, you have a very broken system. Are you able to compile 
anything else ? Did you install the proper Solaris 'packages' ? (From 
what I recall, long ago, about SunOS installs and compilers, and gcc and 
the like, you have to be careful about what you install where.)


-------------------------------------------------------------
I am new to python and have tried to install numpy, but the darn thing doesn't recognise it's existence.
http://www.faqts.com/knowledge-base/view.phtml/aid/3617
-------------------------------------------------------------
Fiona Czuczman
David Porter

What happens if you start python from the directory that numpy is in and
then import Numeric? If that works, then you need to put the directory 
on your path. See site.py in the standard library for how this works. [ 
Quick explanation: if you have numpy at 
/usr/lib/python/site-packages/numpy, then create a file named numpy.pth 
in /usr/lib/python/site-packages with the word numpy as its contents. ]


-------------------------------------------------------------
Is there a site that lists what functions and or libraries are available?
http://www.faqts.com/knowledge-base/view.phtml/aid/3613
-------------------------------------------------------------
Fiona Czuczman
Shae Erisson, Justin Sheehy

If you want a listing of freely distributed functions and libraries that
don't come with the base Python install, check out parnassus:

http://www.vex.net/parnassus/

You may also not have been aware of the very nice online documentation 
for Python itself, at:

http://www.python.org/doc/

Current library references are found at:

http://www.python.org/doc/current/lib/


-------------------------------------------------------------
How do you get the IP number from a request for a web page?
http://www.faqts.com/knowledge-base/view.phtml/aid/3614
-------------------------------------------------------------
Fiona Czuczman
Michal Wallace, Chuck

Try putting this in your script:

#--start
import os
print os.environ["REMOTE_HOST"]
#--end

By the way, using the weblib wrapper ( http://weblib.sourceforge.net/ ), 
that's the entire script.

----------

Ah, but using the CGI Wrapper from Webware, it would only be:

#--start
print environ["REMOTE_HOST"]
#--end

  ;-)

http://webware.sourceforge.net    -- (forgot the URL)


-------------------------------------------------------------
I am looking for a treeview widget.  Is there one available?
http://www.faqts.com/knowledge-base/view.phtml/aid/3595
-------------------------------------------------------------
Fiona Czuczman
Richard Chamberlain

Idle uses a tree widget in 0.5 that you could look at.


-------------------------------------------------------------
I'm trying to capture an Entry box's value and then print it out. How can I do this?
http://www.faqts.com/knowledge-base/view.phtml/aid/3596
-------------------------------------------------------------
Fiona Czuczman
richard_chamberlain

from Tkinter import *
root=Tk()
entry=Entry(root)
var=StringVar()
entry2=Entry(root,textvariable=var)
entry.pack(side=TOP)
entry2.pack(side=TOP)

def calculate():
    var.set(entry.get())

button=Button(root,text="Calculate",command=calculate)
button.pack(side=RIGHT)
root.mainloop()

Getting and setting an Entry widget is straightforward. In the above 
example I've used entry.get() to get the contents of a widget and I've 
used the textvariable option to set the value.

var=StringVar()

This creates an instance of StringVar that we can assign to our entry.

entry2=Entry(root,textvariable=var)

So every change to var is now reflected in the widget.

var.set(entry.get())

I'm simply changing var to the contents of entry. Obviously here you'll 
need to do some calculations and pass that as a string.

In the Grayson book look at the appendices at the back to check out the
methods and options of Entry.


-------------------------------------------------------------
Is there any drag-and-drop feature now in Tkinter or any other Python public graphical interface?
http://www.faqts.com/knowledge-base/view.phtml/aid/3618
-------------------------------------------------------------
Fiona Czuczman
Richard Chamberlain

Have a look at Tkdnd.py in the standard distribution.


-------------------------------------------------------------
Can I explicitly request garbage collection from C?
http://www.faqts.com/knowledge-base/view.phtml/aid/3615
-------------------------------------------------------------
Fiona Czuczman
Courageous

Things are destroyed instantly when their reference counts reach zero. 
There's a function somewhere that prints out an object's reference 
count; that's probably what you want. Either that, or if you're writing 
a C extension, you can examine the object's reference count directly. In 
virtually all cases, it will be one higher than you expect, because the 
tuple that was sent as an argument to your external function also 
happens to own a reference to your object.


-------------------------------------------------------------
How do I print out the current time?
http://www.faqts.com/knowledge-base/view.phtml/aid/3600
-------------------------------------------------------------
Mike Hostetler
http://www.python.org/doc/current/lib/module-time.html

I didn't think that the official documentation was very clear on this (b 
ut it is there), so I'm adding it here.

If you do

>> import time
>> print time.strftime("%Y-%m-%d %H:%M:%S",time.localtime(time.time()))

You'll see something like:

2000-06-08 15:05:09

To dissect this:

time.time() gets the time in seconds since the epoch

time.localtime(secs) creates a tuple

time.strftime(string,tuple) creates a string of the time, using the 
formatting in "string" (that is what the "%"'s are doing there).
To see the full table of formatting options, see 
http://www.python.org/doc/current/lib/module-time.html


-------------------------------------------------------------
Is there such a thing as a toString() for objects, like java has, that will be automatically called when an object is used "as a string"?
http://www.faqts.com/knowledge-base/view.phtml/aid/3616
-------------------------------------------------------------
Fiona Czuczman
Andres Corrada-Emmanuel, Quinn Dunkan

Problem:

ie. when you create an object, __init__ gets called automatically, so if 
I say:

x = Spam()
print x

Can I put a method in Spam such that it will print out something useful 
based on the class fields rather than the ugly data definition?

Solution:

Yes, it's __str__  See the Language Reference 3.3 "Special method names" 
for all the double-underscore magic you can accomplish.

In your class definition for Spam include a __str__ method, something 
like the following:

class Spam:
  .
  .
  .
  def __str__( self ):
     return self.someAttribute


## Edited Entries ##############################################


-------------------------------------------------------------
How can I call Python scripts from C/C++ program?
Is it possible for C   to call Python routines and vice-versa?
http://www.faqts.com/knowledge-base/view.phtml/aid/2633
-------------------------------------------------------------
Fiona Czuczman
Phil Austin

Here's a solution using CXX (http://CXX.sourceforge.net).  

You'll want the current CVS snapshot, as there have been several
bugs fixed in CXX-4.2

Note: put FibSeries into a file called myfun.py:

callable.cxx:

#include "Python.h"
#include "CXX_Objects.h"
#include <iostream>

using namespace Py;
using namespace std;

extern "C" void Py_Initialize();
extern "C" void Py_Finalize();

int
main(int argc, char* argv[])
{
  Py_Initialize();
  Module myfun("myfun");
  Callable fib = myfun.getAttr("FibSeries");
  Tuple argtuple(3);
  argtuple[0]=Int(2);
  argtuple[1]=Int(2);
  argtuple[2]=Int(30);
  Object output = fib.apply(argtuple);
  cout << List(output) << endl;
  Py_Finalize();
  return 0;
}


Compiling and executing this gives:

<peacock ~> callable
2
4
6
10
16
26
[2, 4, 6, 10, 16, 26]







More information about the Python-list mailing list