Inheritance...

2005-10-14 Thread KraftDiner
I have a base class

class geometry(object):
   def __init__(self):
  self.blue = 1
  self.red = 2
   def render(self):
  pass

class square(geometry):
def __init__(self):
 super(square, self).__init__()
   def render(self)
  print 'square'

class circle(geometry):
def __init__(self):
 super(square, self).__init__()
   def render(self)
  print 'square'

objLst = []
objLst.append(square())
objLst.append(circle())

for obj in objLst:
   obj.render()
print obj.blue

What is wrong with this... I will not print blue... (1)

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


Inheritance...

2005-10-14 Thread KraftDiner
I have a base class

class geometry(object):
   def __init__(self):
  self.blue = 1
  self.red = 2
   def render(self):
  pass

class square(geometry):
def __init__(self):
 super(square, self).__init__()
   def render(self)
  print 'square'

class circle(geometry):
def __init__(self):
 super(square, self).__init__()
   def render(self)
  print 'square'

objLst = []
objLst.append(square())
objLst.append(circle())

for obj in objLst:
   obj.render()
print obj.blue

What is wrong with this... I will not print blue... (1)

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


Having trouble deleting objects from a list...

2005-10-19 Thread KraftDiner
I have a list, and within it, objects are marked for deletion.
However when I iterate through the list to remove objects
not all the marked objects are deleted..
here is a code portion:

i = 0
for obj in self.objList:
if obj.mouseHit:
print 'delete + ' + `i`
self.objList.pop(i)
flag = True
else:
i = i + 1
print `i`

I can't see what I'm doing wrong!
It seems like the last selected object is the one that is not
deleted...

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


destroy your self????

2005-10-19 Thread KraftDiner
if I create an object like...

obj = None
...
obj = anObject()

can obj set itself to none in some method of the class?

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


Re: destroy your self????

2005-10-19 Thread KraftDiner
Well I guess what I'm trying to achive is the invalidate the instance
of the object.
I have been using None to denote an invalide or uninitialized instance
of an object.

There is a degenerate case in my code where a polygon has less than 3
points and
I want to class to flag this instance of the object as invalid.

so.. like super.self = None  :)

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


Question about inheritance...

2005-10-22 Thread KraftDiner
I have a base class called Shape
And then classes like Circle, Square, Triangle etc, that inherit from
Shape:

My quesiton is can a method of the Shape class call a method in Circle,
or Square etc...?

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


Re: Question about inheritance...

2005-10-22 Thread KraftDiner
Well here is a rough sketch of my code...
This is giving my two problems.

1) TypeError: super() argument 1 must be type, not classobj
2) I want to be sure the the draw code calls the inherited classes
outline and not its own...

class Shape:
def __init__(self):
pass
def render(self):
print self.__class___
self.outline()
def outline(self):
pass

class Rect(Shape):
def __init__(self):
super(self.__class__, self).__init__()
def render(self):
super(self.__class__, self).draw()
def outline(self):
print 'outline' + self.__class__

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


Re: Question about inheritance...

2005-10-22 Thread KraftDiner
This is what I've got so far:
class Shape(object):
def __init__(self):
pass
def render(self):
print 'Shape render'
self.outline()
def outline(self):
pass

class Rect(Shape):
def __init__(self):
super(self.__class__, self).__init__()
def render(self):
super(self.__class__, self).render()
def outline(self):
print 'Rect outline'

r = Rect()
r.render()

The output:

Shape render
Rect outline

Cool.. I guess its working..

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


How do I sort these?

2005-10-28 Thread KraftDiner
I have two lists.
I want to sort by a value in the first list and have the second list
sorted as well... Any suggestions on how I should/could do this?

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


Re: How do I sort these?

2005-10-28 Thread KraftDiner
yes... Thats the idea...

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


Re: How do I sort these?

2005-10-28 Thread KraftDiner
In C++ you can specify a comparision method, how can I do this with
python...
Say for instance the first list was a dictionary and I wanted to sort
on one of the keys in the dictionary?

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


Re: How do I sort these?

2005-10-28 Thread KraftDiner
unzip doesn't seem to work for me...

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


A simple list question.

2005-12-22 Thread KraftDiner
Is there a cleaner way to implement this code?

if len(self.listOfObjects) == 0:
self.listOfObjects.append(self.currentObject)
elif:
self.listOfObjects[self.currentSlice] = 
self.currentObject

listOfObjects is a list of lists
If the listOfObjects is [] then I have to use append to add the first
list
otherwise I can access each list using the index into the list.

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


Re: A simple list question.

2005-12-22 Thread KraftDiner
I am trying to implement a two dimensional array.
mylist = [[a,b,c],[d,e,f,c],[g,h,i]]

So the array is of length 3 here...
So how do I initialize this array and then set each object?
At some point in my code I know there will be 3 lists in the list.
So how do I initialize this list such that I can access them
as elements... like
mylist[1] = [c,f,g]
in random order...

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


Re: What is unique about Python?

2005-12-23 Thread KraftDiner
I like python.. Its ok.. One thing that I find a bit dangerous it the
use
of the tab character for indentation.. I've had copy and pasts loose
indentation on me and its theoretically impossible to really figure out
what the indentation should be.  I think for the extra effort it would
have
taken to put brackets in the language it would increase a lot of
peoples
confidence in using it for codeing...

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


Simple question on Parameters...

2005-12-28 Thread KraftDiner
I have defined a method as follows:

def renderABezierPath(self, path, closePath=True, r=1.0, g=1.0, b=1.0,
a=1.0, fr=0.0, fg=0.0, fb=0.0, fa=.25):

Now wouldn't it be simpler if it was:

def renderABezierPath(self, path, closePath=True, outlineColor,
fillColor):

But how do you set default vaules for outlineColor and fillColors?
Like should these be simple lists or should they be structures or
classes...

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


Re: Simple question on Parameters...

2005-12-28 Thread KraftDiner
I guess its all good...
I just am not crazy about using fillColor[0]  id rather use fillColor.r
So is that a structure I need to define or a class...
I'm kind of new to python what would that class or structure look like?
and then do the default parameters still work?

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


Re: Simple question on Parameters...

2005-12-28 Thread KraftDiner
NICE! :)

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


Is this a refrence issue?

2005-12-28 Thread KraftDiner
I understand that everything in python is a refrence

I have a small problem..

I have a list and want to make a copy of it and add an element to the
end of the new list,
but keep the original intact

so:
tmp = myList
tmp.append(something)
print tmp, myList
 
should be different...

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


zip unzip?

2005-12-28 Thread KraftDiner
I have two lists...

a=[1,2,3]
b=[4,5,6]

n=0
for i in a:
   print i, b[n]
   n=n+1

how can i have two iterators on my for loop?
rather than have to use the counter n?
like:
for i,j in a,b:
   print i, j

Do you know what I mean?

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


Translate this to python?

2006-01-03 Thread KraftDiner
I'm porting a routing from C++ to python.
There is a complex for loop that I don't know how to code in python

for (i = nPoints-1, j = 0; j < nPoints; i = j, j++)

Thanks.

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


Re: Translate this to python?

2006-01-03 Thread KraftDiner
Thanks all!

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


Is 'everything' a refrence or isn't it?

2006-01-04 Thread KraftDiner
I was under the assumption that everything in python was a refrence...

so if I code this:
lst = [1,2,3]
for i in lst:
   if i==2:
  i = 4
print lst

I though the contents of lst would be modified.. (After reading that
'everything' is a refrence.)
so it seems that in order to do this I need to code it like:

lst = [1,2,3]
for i in range(len(lst)):
   if lst[i] == 2:
  lst[i]=4
print lst

Have I misunderstood something?

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


Inheritance problem?

2006-01-06 Thread KraftDiner
I have a class

class MyClass(MyBaseClass)
   def __init__(self)
  super(self.__class__, self).__init__()
  self.type = MyClassType
  return self

It has a few methods...
I have another class and the only difference is the __init__ method..

I tried this:
class MySpecialClass(MyClass)
   def __init__(self)
  super(self.__class__, self).__init__()
  self.type = MySpecialClassType   # This is the only line
that's different between the two classes.
  return self

At runtime I get an error:
RuntimeError: maximum recursion depth exceeded
What have I done wrong?

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


Spelling mistakes!

2006-01-06 Thread KraftDiner
I've spent hours trying to find a bug that was a simple spelling
mistake.

in an init method I declare a variable  self.someLongName

later in a different method of the class I use
self.sumLongName
Now I really meant self.someLongName.
In fact I don't want a variable called sumLongName.
Frankly how are you ever to know if this type of error is occuring?

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


Re: Spelling mistakes!

2006-01-06 Thread KraftDiner
try this:

class x(object):
   def __init__(self):
  self.someName = "hello"
   def someMethod(self):
  self.sumName = "bye"

find that bug.

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


Copy an Object (Again?)

2006-01-06 Thread KraftDiner
I'm having trouble getting a copy of and object... (a deep copy)

I'm writing a method that creates a mirror image of an object (on
screen)
In order to do this i need to get a copy of the object and then modify
some
of its attributes.

I tried:
objs = myListOfObjects
for obj in objs:
   if obj.flag:
  newObject = copy.deepcopy(obj)
  newObject.mirror()
  myListOfObjects.append(newObject)

That doesn't seem to work.. the new object seems to disapear from
existance.
I'm wondering if its a bug in my application or if this is my shallow
understanding of the language.

TIA
B.

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


Re: Inheritance problem?

2006-01-06 Thread KraftDiner
So ok I've written a piece of code that demonstrates the problem.
Can you suggest how I change the Square class init?

class Shape(object):
def __init__(self):
print 'MyBaseClass __init__'

class Rectangle(Shape):
def __init__(self):
super(self.__class__, self).__init__()
self.type = Rectangle
print 'Rectangle'

class Square(Rectangle):
def __init__(self):
super(self.__class__, self).__init__()
self.type = Square
print 'Square'

r = Rectangle()
s = Square()

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


Python and Boost.

2006-07-25 Thread KraftDiner
Hi, I'm trying to call a C++ class from python.
I've looked around and the solution would appear to be boost.
I'm not sure but maybe I've downloaded and installed the entire boost
library,
when there is probably a separate tar ball for python / C++
integration.
Can someone please point me to a tar ball for mac os x so that I can
call
my C++ class from Python?

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


Python and C++

2006-07-25 Thread KraftDiner
What ways can I call my C++ classes from within Python.
I've looked at boost but it would appear that there is little
support or knowledge on boost in the python community.

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


Need help building boost python on mac os x.

2006-08-04 Thread KraftDiner
Could someone point me to step by step instructions on building boost
python on mac os x?
I have bjam running.. I have the boost source... but the tests are
failing..
Probably something to do with environement variables...
Anyone with time?

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


Recurse Directories and process files in directory

2006-08-12 Thread KraftDiner
Hi I need help writing a python script that traverses (recursivly) a
directory and its sub directories and processes all files in the
directory.  So at each directory if there are files in it I must build
a list of those files and process them by exectuing a system command
(exec?)

Can some one tell me what methods to use to:
a) Walk the directory tree
b) execute a system command with parameters.

TIA.

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


A little assistance with os.walk please.

2006-08-14 Thread KraftDiner
The os.walk function walks the operating systems directory tree.

This seems to work, but I don't quite understand the tupple that is
returned...
Can someone explain please?

for root, dirs, files in os.walk('/directory/'):
print root
#   print dirs
#   print files

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


Re: A little assistance with os.walk please.

2006-08-14 Thread KraftDiner

Larry Bates wrote:
> KraftDiner wrote:
> > The os.walk function walks the operating systems directory tree.
> >
> > This seems to work, but I don't quite understand the tupple that is
> > returned...
> > Can someone explain please?
> >
> > for root, dirs, files in os.walk('/directory/'):
> > print root
> > #   print dirs
> > #   print files
> >
>
> Actually returns two tuples: dirs and files
>
> root - is the directory branch you are currently walking
> dirs - are the directory branches that are subdirectories of this
>directory branch
> files - are the files that live in this directory branch
>
>
> To process all the files here you do something like:
>
> for afile in files:  # resist the urge to call it 'file'
>  fullpath=os.path.join(root, afile)
>  #
>  # Do something  with fullpath
>  #
>
> Hard to figure out item - If you wish to NOT process some of the
> dirs, you can delete them from the dirs list here and they won't
> get walked.  You MUST delete them in place with del dirs[n] or
> dirs.pop or some other function that deletes in-place.
>
> You might want to type: help(os.walk) to get some more info.
>
Yep done that.   Thanks.
Two things..
1) there seems to be an optional topdown flag.  Is that passed to
os.walk(path, topdownFlag)
2) I only want to process files that match *.txt for example...  Does
that mean I need to parse the list of files for the .txt extention or
can I pass a wildcard in the path parameter?


> -Larry Bates

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


Re: A little assistance with os.walk please.

2006-08-14 Thread KraftDiner

Tim Chase wrote:
> > 1) there seems to be an optional topdown flag.  Is that passed to
> > os.walk(path, topdownFlag)
>
> Yes.
>
> > 2) I only want to process files that match *.txt for example...  Does
> > that mean I need to parse the list of files for the .txt extention or
> > can I pass a wildcard in the path parameter?
>
>  >>> for path, dirs, files in os.walk("."):
> ... for f in files:
> ... if not f.lower().endswith(".txt"): continue
> ... print os.path.join(path, f)
>
> If you want to be more complex:
>
>
>  >>> from os.path import splitext
>  >>> allowed = ['.txt', '.sql']
>  >>> for path, dirs, files in os.walk("."):
> ... for f in files:
> ... if splitext(f)[1].lower() not in allowed: continue
> ... fn = os.path.join(path, f)
> ... print "do something with %s" % fn
> 
> 
> Just a few ideas,
> 
> -tkc


Many thanks all.
B.

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


file object and eof

2006-08-15 Thread KraftDiner
I open a file in python by
f = open('filename', mode='rb')

how can I tell if I am at the end of file?
f.eof() isn't implmented.

How can I implement its functionallity?

Thanks.
B.

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


How to tell machines endianism.

2006-08-15 Thread KraftDiner
How can you tell if the host processor is a big or little endian
machine?

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


Re: file object and eof

2006-08-15 Thread KraftDiner

Grant Edwards wrote:
> On 2006-08-15, KraftDiner <[EMAIL PROTECTED]> wrote:
>
> > I open a file in python by
> > f = open('filename', mode='rb')
> >
> > how can I tell if I am at the end of file?
>
> I don't believe you can unless you try to read from the file.
>
> > f.eof() isn't implmented.
> >
> > How can I implement its functionallity?
>
> You don't, generally.
>
> There's probably a better, "more Pythonic" way to accomplish
> your goal, but you're going to have to tell us what it is.
>

how about?!:

def eof(fileobj):
   curloc = fileobj.tell()
   ofloc = fileobj.seek(0,2)
   fileobj.seek(curloc, 0)
   if ofloc >= curloc:
  return True
   return False

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


include a python class in another python script.

2006-08-15 Thread KraftDiner
I have a class that is defined in a file called MyClass.py

How do I use that class in another python script..
import MyClass ?  (Does it need to be in a specific location?)

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


Global Objects...

2006-08-15 Thread KraftDiner
I have a question..

myGlobalDictionary = dictionary()


class someClass:
   def __init__(self):
  self.x = 0;
   def getValue(self, v)
  myGlobalDictionary.getVal(v)


myGlobalDictionary doesn't seem to be visible to my someClass methods.
Why?  What should I do?

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


inheritance?

2006-08-15 Thread KraftDiner
I have two classes:

class implicitClass:
   def __init__(self):
   def isIVR(self):  #This is a class private method.
   def fromfile(self, fileObj, byteOrder):
   def getVR(self):
   def getGroup(self):
   def getElement(self):
   def getSize(self):
   def getData(self):

class explicitClass:
   def __init__(self):
   def fromfile(self, fileObj):
   def getVR(self):
   def getGroup(self):
   def getElement(self):
   def getSize(self):
   def getData(self):


As you can see the interface is almost identical.

How can I define a base class that will abstract
the type such that I don't know if its really and inplicit
or explicit object?

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


Re: inheritance?

2006-08-16 Thread KraftDiner

Steven D'Aprano wrote:
> On Tue, 15 Aug 2006 19:35:11 -0700, KraftDiner wrote:
>
> > I have two classes:
> >
> > class implicitClass:
> >def __init__(self):
> >def isIVR(self):  #This is a class private method.
>
> The convention is to flag classes as "private" with a leading underscore:
>
> def _isIVR(self):
>
> or double underscores for "really private, no, honestly":
>
> def __isIVR(self):
>
> but google on "python name mangling" before using that.
>
> [snip]
>
> > As you can see the interface is almost identical.
> >
> > How can I define a base class that will abstract
> > the type such that I don't know if its really and inplicit
> > or explicit object?
>
> Something like this?
>
>
> class baseClass:
>def __init__(self):
>raise NotImplementedError("Don't instantiate the base class!")
>def fromfile(self):
>def getElement(self):
># etc.
>
>
> class implicitClass(baseClass):
>def __init__(self):
># code
>def _isIVR(self):
># code
>def fromfile(self, fileObj, byteOrder):
># code
> # etc.
>
> Now, you can define instance = implicitClass() or explicitClass(). When
> you come to use instance, you don't need to know whether it is one or the
> other. If you need to type-test, call "isinstance(instance, baseClass)".
>
> The only minor issue is that the fromfile method has a different
> interface. If you really want to do duck typing, they need to have the
> same interface. That might be as simple as:
>
> class explicitClass(baseClass):
>def fromfile(self, fileObj, byteOrder=None):
># byteOrder is ignored; it is included only for
># compatibility with implicitClass
>
>
> Is that what you're asking for?
>
Yes I believe so but in fromfile I want to call the appropriate
method depending on the in a parameter in fromfile...
like:
class baseClass:
   def fromfile(self, fileObj, byteOrder=None, explicit=False):
  if explicit:
 call fromfile of explicit class
  else:
call fromfile of implicit class

How is that done?


> 
> 
> -- 
> Steven D'Aprano

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


Re: inheritance?

2006-08-16 Thread KraftDiner

Steven D'Aprano wrote:
> On Tue, 15 Aug 2006 19:35:11 -0700, KraftDiner wrote:
>
> > I have two classes:
> >
> > class implicitClass:
> >def __init__(self):
> >def isIVR(self):  #This is a class private method.
>
> The convention is to flag classes as "private" with a leading underscore:
>
> def _isIVR(self):
>
> or double underscores for "really private, no, honestly":
>
> def __isIVR(self):
>
> but google on "python name mangling" before using that.
>
> [snip]
>
> > As you can see the interface is almost identical.
> >
> > How can I define a base class that will abstract
> > the type such that I don't know if its really and inplicit
> > or explicit object?
>
> Something like this?
>
>
> class baseClass:
>def __init__(self):
>raise NotImplementedError("Don't instantiate the base class!")
>def fromfile(self):
>def getElement(self):
># etc.
>
>
> class implicitClass(baseClass):
>def __init__(self):
># code
>def _isIVR(self):
># code
>def fromfile(self, fileObj, byteOrder):
># code
> # etc.
>
> Now, you can define instance = implicitClass() or explicitClass(). When
> you come to use instance, you don't need to know whether it is one or the
> other. If you need to type-test, call "isinstance(instance, baseClass)".
>
> The only minor issue is that the fromfile method has a different
> interface. If you really want to do duck typing, they need to have the
> same interface. That might be as simple as:
>
> class explicitClass(baseClass):
>def fromfile(self, fileObj, byteOrder=None):
># byteOrder is ignored; it is included only for
># compatibility with implicitClass
>
>
> Is that what you're asking for?
>
>
Here I tried this example and maybe this will explain the difficulties
I'm having.
1) at the time the baseClass is constructed shouldn't the constructor
of the appropriate
type be called.
2) getName is doing nothing...

class baseClass:
def __init__(self):
pass
def fromfile(self, str):
if (str == 'A'):
a = typeA()
else:
a = typeB()
def getName(self):
pass

class typeA(baseClass):
def __init__(self):
self.name='A'
print 'typeA init'
def fromfile(self, str=None):
print 'typeA fromfile'
def getName(self):
print self.name

class typeB(baseClass):
def __init__(self):
self.name='B'
print 'typeB init'
def fromfile(self, str=None):
print 'typeB fromfile'
def getName(self):
print self.name

bc = baseClass()
bc.fromfile('A')
bc.getName()
bc.fromfile('B')
bc.getName()
bc.getName()

log:
typeA init
typeB init






> 
> -- 
> Steven D'Aprano

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


trouble understanding inheritance...

2006-08-16 Thread KraftDiner
This is not working the way I think it should
it would appear that fromfile and getName are calling the baseClass
methods which are
simple passes What have I done wrong?

class baseClass:
def __init__(self, type):
if type == 'A':
self = typeA()
else:
self = typeB()
def fromfile(self):
pass
def getName(self):
pass

class typeA(baseClass):
def __init__(self):
self.name='A'
print 'typeA init'
def fromfile(self):
print 'typeA fromfile'
def getName(self):
print self.name

class typeB(baseClass):
def __init__(self):
self.name='B'
print 'typeB init'
def fromfile(self):
print 'typeB fromfile'
def getName(self):
print self.name

a = baseClass('A')
a.fromfile()
a.getName()

b = baseClass('B')
b.fromfile()
b.getName()

log:
typeA init
typeB init

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


Re: trouble understanding inheritance...

2006-08-16 Thread KraftDiner

Fredrik Lundh wrote:
> KraftDiner wrote:
>
> > This is not working the way I think it should
> > it would appear that fromfile and getName are calling the baseClass
> > methods which are
> > simple passes What have I done wrong?
> >
> > class baseClass:
> > def __init__(self, type):
> > if type == 'A':
> > self = typeA()
> > else:
> > self = typeB()
>
> __init__ is not a constructor, and assigning to self doesn't change the
> type of the constructed object.
>
> looks like you need to get a better tutorial.
>
> 

Well how does one select which class baseClass really is when you
contruct the object?
What am I missing?

a = typeA()
b = typeB()
c = baseClass(a)

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


Re: trouble understanding inheritance...

2006-08-16 Thread KraftDiner

Simon Forman wrote:
> KraftDiner wrote:
> > Fredrik Lundh wrote:
> > > KraftDiner wrote:
> > >
> > > > This is not working the way I think it should
> > > > it would appear that fromfile and getName are calling the baseClass
> > > > methods which are
> > > > simple passes What have I done wrong?
> > > >
> > > > class baseClass:
> > > > def __init__(self, type):
> > > > if type == 'A':
> > > > self = typeA()
> > > > else:
> > > > self = typeB()
> > >
> > > __init__ is not a constructor, and assigning to self doesn't change the
> > > type of the constructed object.
> > >
> > > looks like you need to get a better tutorial.
> > >
> > > 
> >
> > Well how does one select which class baseClass really is when you
> > contruct the object?
> > What am I missing?
> >
> > a = typeA()
> > b = typeB()
> > c = baseClass(a)
>
> a = typeA()
> b = typeB()
>
> You're done. Stop there.
>
I can see that this might work...
c = [a, b]
for c in [a,b]:
   c.getName()

but when does baseClass ever get used?
Why did i even have to define it?



> You can't "select which class baseClass really is"-- it really is
> baseClass.  You "select" which class your object is by choosing which
> class to use to construct the object.
> 
> HTH,
> ~Simon

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


Re: trouble understanding inheritance...

2006-08-16 Thread KraftDiner

Simon Brunning wrote:
> On 16 Aug 2006 12:53:12 -0700, KraftDiner <[EMAIL PROTECTED]> wrote:
> > I can see that this might work...
> > c = [a, b]
> > for c in [a,b]:
> >c.getName()
> >
> > but when does baseClass ever get used?
> > Why did i even have to define it?
>
> Well, quite.
>
I agree... welll... quite... :)


> --
> Cheers,
> Simon B,
> [EMAIL PROTECTED],
> http://www.brunningonline.net/simon/blog/

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


modify element of a list.

2006-08-17 Thread KraftDiner
Hi I have a list of Ojbects... I want to change one of the objects in
the list for a new object
How do I replace an existing object with a new one and maintain the
list order..

This is what I have...

def setAttribute(self, desc, value):
   n = anObject(desc, value)
   for o in self.Objects:
  if o.getDescription() == desc:
 self.Objects.replace(o, n) #Replace o with n?
 return
   self.Objects.append(n)

It's the replace in place that I don't know how to do...

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


Type conversion?

2006-08-18 Thread KraftDiner
I have the following code...

import array
len32 = array.array('L')
len16 = array.array('H')

len32.append(0)
len16.append(0)

y = len32[0]
print y.__class__

z = len16[0]
print z.__class__


how can I change Zs type to long?
Or how how can I change an arrays type?

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


Re: Type conversion?

2006-08-18 Thread KraftDiner

Rob Cowie wrote:
> KraftDiner wrote:
> > I have the following code...
> >
> > import array
> > len32 = array.array('L')
> > len16 = array.array('H')
> >
> > len32.append(0)
> > len16.append(0)
> >
> > y = len32[0]
> > print y.__class__
> > 
> > z = len16[0]
> > print z.__class__
> > 
> >
> > how can I change Zs type to long?
>
> z_long = long(z)
> type(z_long)
> 
>
> > Or how how can I change an arrays type?


In C++ you can cast one class type to another if you override the
operator=
Then you can convert one class type to another...
In Python it would appear that the left hand side of the assignment
operator
is not used to determine if a cast is necessary.
So how would I do this in python?

a = classA()
b = classB()
b = a

In the end b would end up just being a refrence to a
no conversion would have been done.

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


key not found in dictionary

2006-08-22 Thread KraftDiner
I have a dictionary and sometime the lookup fails...
it seems to raise an exception when this happens.
What should I do to fix/catch this problem?

desc = self.numericDict[k][2]
KeyError: 589824   < This is the error that is being produced,
because there is no key
589824.

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


range of int() type.

2006-08-23 Thread KraftDiner
What is the range of a variable of type int()

eg:
i = int()
print i.max()  # 0x
print i.min() # 0x

is it a signed 16 bit or 32 bit or is it unsigned 16 or 32...
I've noticed that it can be incremented into a new class of type
long...

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


Re: range of int() type.

2006-08-23 Thread KraftDiner

Simon Forman wrote:
> KraftDiner wrote:
> > What is the range of a variable of type int()
> >
> > eg:
> > i = int()
> > print i.max()  # 0x
> > print i.min() # 0x
> >
> > is it a signed 16 bit or 32 bit or is it unsigned 16 or 32...
> > I've noticed that it can be incremented into a new class of type
> > long...
>
> |>> import sys
> |>> sys.maxint
> 2147483647
>

So what type / class should one use to represent a 16 bit integers
(signed or unsigned)?




> >From the sys module docs:
> maxint
> The largest positive integer supported by Python's regular integer
> type. This is at least 2**31-1. The largest negative integer is
> -maxint-1 -- the asymmetry results from the use of 2's complement
> binary arithmetic.
> 
> Peace,
> ~Simon
> 
> P.S. ints and longs are becoming unified soon.

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


Re: range of int() type.

2006-08-23 Thread KraftDiner

Terry Reedy wrote:
> "KraftDiner" <[EMAIL PROTECTED]> wrote in message
> news:[EMAIL PROTECTED]
> >
> > So what type / class should one use to represent a 16 bit integers
> > (signed or unsigned)?
>
> For most purposes, Python just has integers, with 'small' ones (depending
> on the machine) handled more efficiently.  For special purposes, you may
> want to use the array or struct modules.
>
> tjr

Ok so the bottom line is..
if I have two arrays...

a = array.array('L')
a.append(65536L)
b = array.array('H')
b.append(a[0])

I will get the error:
  File "", line 1, in ?
OverflowError: unsigned short is greater than maximum

This is obvious... but how do I crop off the high order bits if
necessary?
a[0]&0x ?

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


array tofile to a socket?

2006-08-24 Thread KraftDiner
Can pythons array.tofile method be used for a TCP/IP Socket?
ie can the socket be a file object?

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


get a line of text from a socket...

2006-08-25 Thread KraftDiner
If you don't know how long your input data is going to be how can you
at least treat it a text line at a time... like looking for new line in
the data... Right now recv blocks.   Yes I could do a select, but the
examples seem a bit complicated for a simple line oriented input...

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


Re: get a line of text from a socket...

2006-08-25 Thread KraftDiner

Marc 'BlackJack' Rintsch wrote:
> In <[EMAIL PROTECTED]>, KraftDiner
> wrote:
>
> > If you don't know how long your input data is going to be how can you
> > at least treat it a text line at a time... like looking for new line in
> > the data... Right now recv blocks.   Yes I could do a select, but the
> > examples seem a bit complicated for a simple line oriented input...
>
> This is already done in the `asyncore` and `asynchat` modules of the
> standard library.
>
> Ciao,
>   Marc 'BlackJack' Rintsch

Thanks I can't seem to get this example to do anything except sit
there
http://docs.python.org/lib/asyncore-example.html
And still it seems like a lot of work for a simple send/expect script.
I got the makefile to work.. there is a readline function how does one
use writelines to write one single line?

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


Re: get a line of text from a socket...

2006-08-28 Thread KraftDiner

Bryan Olson wrote:
> KraftDiner wrote:
>
> > Thanks I can't seem to get this example to do anything except sit
> > there
> > http://docs.python.org/lib/asyncore-example.html
>
> Yeah, the example code, by itself, will just sit there.
> As an example, it should probably include the calls to make it
> do something. Try adding the following lines to the given code:
>
>http_client('www.python.org', '/')
>asyncore.loop()
>
> The call: "asyncore.loop()"  is what says to stop just sitting
> there and do something.
>
>
What makes asyncore.loop exit?


> > And still it seems like a lot of work for a simple send/expect script.
> > I got the makefile to work.. there is a readline function how does one
> > use writelines to write one single line?
>
> I don't yet have my head around what you are asking. Reading
> exactly up to end-of-line from a socket can be a bit tricky,
> but I think I can explain. Managing multiple input sources and
> their blocking behavior is a basic problem -- so basic that we've
> already examined and debated the alternatives.  Writing exactly
> one line is trivial.
>
> I found Marc 'BlackJack' Rintsch's response a bit misleading.
> The asyncore module offers nothing to read a line. The asynchat
> module will respond to lines if you pass set_terminator() the
> end-of-line marker. I had to read both the doc and the source
> to figure out what should work.
>
> Sybren Stuvel pointed out socket.makefile(), which will read up
> to the end of line, but does not play nice with others. Its
> local buffering pretty much breaks select(). If you set any
> timeout and the timeout raises, the documented interface does
> not provide any way to tell what data was sent and received.
>
> Jean-Paul Calderone suggested Twisted; it has a lot of fans, and
> I'm not competent to say how well it would work in this case.
> I've never been willing, nor seen the need, to re-write all code
> in Twisted's deferred form.
> 
> 
> 
> -- 
> --Bryan

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


Re: get a line of text from a socket...

2006-08-31 Thread KraftDiner

Sybren Stuvel wrote:
> KraftDiner enlightened us with:
> > What makes asyncore.loop exit?
>
> Why ask questions about something you're unable to use, when I've
> given you something that does work?
>
Who said it didn't work?

> Sybren
> --
> The problem with the world is stupidity. Not saying there should be a
> capital punishment for stupidity, but why don't we just take the
> safety labels off of everything and let the problem solve itself?
>  Frank Zappa

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


nDimensional sparse histogram in python.

2006-02-01 Thread KraftDiner
Hi, I wrote a C++ class that implements an n dimensional histogram in
C++, using stl maps and vectors.  I want to code this up now in Python
and would like some input from this group.

The C++ class was VERY simple..

std::map, unsigned long> histo;

Say for example I want a 3D histogram then std::vector
would contains
the x, y, and z points in space and the unsigned long is the number of
counts.
If I want to know the value at a specific point in space I pass in a
vector [3,2,1] and if
it is found in the map then the count is returned other wise zero is
returned.
If I want to increment a count then histo[vector]++ will either set the
count to 1 or increment
the count if it is found...

So as you can see this is a very simple way to implement a multi
dimensional sparse histogram.

I'm thinking that in python this must also be as simple as a
dictionary, where the key is the vector x,y,z and the value is the
count.

My python is ok, but not the best and I was hoping some one here might
want to help me out?
This doesn't work for example...

histo = {[0,0,0]:1, [0,0,1]:2}
Would indicate that there is one sample at 0,0,0 and two samples at
0,0,1
but python tells me TypeError: list objects are unhashable

So any suggestions would be welcome.
TIA.

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


Re: nDimensional sparse histogram in python.

2006-02-01 Thread KraftDiner
Cool.
Ok so my histogram class had two methods 1) To increment a point and 2)
to get the point.

def class histo:
   def __init__(self):
   histo = {}
def update(point):
   '''searches the dictionary for point and if it exists increment the
value.
if it doesn't exist set the value to 1'''

def get(point):
   return self.histo[point]

I'm not sure how to code up the update routine...

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


Re: nDimensional sparse histogram in python.

2006-02-01 Thread KraftDiner
The dictionary is sorted by the point key.
Is there a way to sort the dictionary by the value?
Seems to me this goes against the purpose of a dictionary but
thats what I need to do..

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


Re: nDimensional sparse histogram in python.

2006-02-01 Thread KraftDiner
Ok so this is nice.. Just one thing.. When you try to get a value from
a dictionary
and it isn't found in the dictionary things go bad...

Take this for example:

class histogram(object):
def __init__(self):
self.histo = {}

def update(self, point):
if self.histo.get(point) != None:
self.histo[point] = self.histo[point] + 1
else:
self.histo[point] = 1

def get(self, point):
return self.histo[point]


hist = histogram()
hist.update((0,0,0))
hist.update((0,0,1))
hist.update((0,0,1))
hist.get((0,0,0))
hist.get((0,0,1))
hist.get((0,0,2))

spews out this error:

Traceback (most recent call last):
  File "histogram.py", line 21, in ?
hist.get((0,0,2))
  File "histogram.py", line 12, in get
return self.histo[point]
KeyError: (0, 0, 2)

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


Re: nDimensional sparse histogram in python.

2006-02-01 Thread KraftDiner
Many thanks everyone.

One last quick question...
The dictionary, I believe, is sorted by the key.
Is there a way to sort it by the value?
Say I wanted to put out a list of the frequency sorted by highest to
lowest?

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


Need help initializing a list or tuple.

2006-03-06 Thread KraftDiner
I need a matrix of 256 x 256 unsigned short values...
The matrix can be implemented as a list of lists or a tuple...
So for example:
[[1][2][3], [4][5][6], [7][8][9]]
or
((1,2,3),(4,5,6), (7,8,9))

This is what I have so far but its not working...

a = [][]
for i in range(0,256):
for j in range(0,256):
a[i][j] = i**2

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


Re: Need help initializing a list or tuple.

2006-03-06 Thread KraftDiner
Thank you that worked great!

a = [[None] * 256] * 256
for i in range(0,256):
for j in range(0,256):
a[i][j] = i**2

Now.. Can I change this quickly into a 1 dimensional list of length
65536?

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


Re: Need help initializing a list or tuple.

2006-03-06 Thread KraftDiner
Strange behaviour...

a = [[None] * 256] * 256
for i in range(0,256):
for j in range(0,256):
a[i][j] = i**2
a = sum(a, [])
print a[0]
print a[65535]

Prints:

65025
65025

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


Re: Need help initializing a list or tuple.

2006-03-06 Thread KraftDiner
Yes I missed the fine print that said that the code had 'side effects'
So:

a = [None]*256
for i in range(256):
 a[i] = [None] * 256
for i in range(0,256):
for j in range(0,256):
a[i][j] = i**2

a = sum(a, [])
print a[0]
print a[65535]

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


Re: Need help initializing a list or tuple.

2006-03-06 Thread KraftDiner
Oh by the way the b = sum(a, []) worked well to convert the two
dimensional array to a 1D array...
Is there a way to bring that 1D array back to a 2D array?

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


Re: Need help initializing a list or tuple.

2006-03-06 Thread KraftDiner
nope.. sorry its not...
I meant x = [[1,2,3],[3,4,5],[5,6,7]]

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


reshape a list?

2006-03-06 Thread KraftDiner
I have a list that starts out as a two dimensional list
I convert it to  a 1D list by:

b = sum(a, [])

any idea how I can take be and convert it back to a 2D list?

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


Re: reshape a list?

2006-03-07 Thread KraftDiner
I'm wondering if your solution fits my requirements
I need to be able to pass these objects which are python lists
to a pyObjC (on MAC OS X) framework.  At the moment the
framework is expecting NSArray or NSData as input and this works
for 'generic' python lists I don't know about numpy arrays...

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


Reading binary from a file...

2006-03-07 Thread KraftDiner
Hi!
In python I'm able to read in binary data from a file.
data = file.read()  # Reads in an entire file.

However the data is 16bits per sample and python is storing the
data in a string.  How do I convert that 8bit data into a list of 16
bit integers?

Note: I want generic python lists or tupels not numpy or numeric etc
etc...

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


reshape an array?

2006-03-08 Thread KraftDiner
I have a 2D array.  Say it is 10x10 and I want a 1D Array of 100
elements...
What is the syntax?

oneD = reshape(twoD, (100,1))
or
oneD = reshape(twoD, (1,100))

One I guess is the transpose of the other but both seem to be
arrays of arrays...

help?!

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


a question about zip...

2006-03-08 Thread KraftDiner
I had a structure that looked like this
((0,1), (2, 3), (4, 5), (6,7)

I changed my code slightly and now I do this:
odd = (1,3,5,7)
even = (0,2,4,6)
all = zip(even, odd)

however the zip produces:
[(0, 1), (2, 3), (4, 5), (6, 7)]

Which is a list of tuples.. I wanted a tuple of tuples...

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