Re: function-class frustration

2008-03-22 Thread Gabriel Genellina
En Sat, 22 Mar 2008 07:07:18 -0300, John Machin <[EMAIL PROTECTED]>  
escribi�:

> On Mar 22, 7:19 pm, [EMAIL PROTECTED] wrote:
>
>> On the other hand, are you willing to make changes to the console?
>> How big and what?  I was thinking of __ to keep the second-to-last
>> most recent command.
>
> Access to and editing of previous input lines is already provided by
> the cooked mode of the console (e.g. on Windows, lean on the up-arrow
> key) or can be obtained by installing the readline package. Perhaps by
> analogy with _, you mean the *result* of the penultimate *expression*.
> This functionality would be provided by the Python interactive
> interpreter but it's not ... do have a look at IPython (http://

There is no need to change the interpreter, one can use sys.displayhook to  
achieve that.
Create or add the following to your sitecustomize.py module:


# sitecustomize.py

import sys
import __builtin__ as builtin

def displayhook(value):
 if value is not None:
 sys.stdout.write('%s\n' % value)
 builtin._ = value
 if not hasattr(builtin, '__'):
 __ = builtin.__ = displaylist()
 else:
 __ = builtin.__
 __.append(value)

sys.displayhook = displayhook

class displaylist(list):
 """List of expressions kept by displayhook.
 It has a maximum size and a custom display, enumerating lines.
 Lines are trimmed at the middle to fit the maximum width"""

 maxlen = 20
 maxwidth = 80-1-4

 def __str__(self):
 def genlines(self):
 for i, value in enumerate(self):
 s = str(value)
 if len(s) > self.maxwidth:
 p = (self.maxwidth - 3) // 2
 s = s[:p] + '...' + s[-(self.maxwidth - p - 3):]
 yield '%2d: %s' % (i, s)
 return '\n'.join(genlines(self))

 def append(self, value):
 # don't keep duplicates
 # obey the maximum size
 if value is self:
 return
 if value in self:
 self.remove(value)
 if len(self) > self.maxlen - 1:
 del self[:-self.maxlen - 1]
 list.append(self, value)


py> 24*60*60*365
31536000
py> "hello world".split()
['hello', 'world']
py> help
Type help() for interactive help, or help(object) for help about object.
py> quit
Use quit() or Ctrl-Z plus Return to exit
py> __
  0: 31536000
  1: ['hello', 'world']
  2: Type help() for interactiv...ct) for help about object.
  3: Use quit() or Ctrl-Z plus Return to exit
py> __[1]
['hello', 'world']
py> __[0] // 3
10512000
py> __
  0: 31536000
  1: Type help() for interactiv...ct) for help about object.
  2: Use quit() or Ctrl-Z plus Return to exit
  3: ['hello', 'world']
  4: 10512000
py>

-- 
Gabriel Genellina

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

Re: function-class frustration

2008-03-22 Thread Arnaud Delobelle
On Mar 22, 10:07 am, John Machin <[EMAIL PROTECTED]> wrote:
> On Mar 22, 7:19 pm, [EMAIL PROTECTED] wrote:
>
>
>
> > On the other hand, are you willing to make changes to the console?
> > How big and what?  I was thinking of __ to keep the second-to-last
> > most recent command.
>
> Access to and editing of previous input lines is already provided by
> the cooked mode of the console (e.g. on Windows, lean on the up-arrow
> key) or can be obtained by installing the readline package. Perhaps by
> analogy with _, you mean the *result* of the penultimate *expression*.
> This functionality would be provided by the Python interactive
> interpreter but it's not ... do have a look at IPython (http://
> ipython.scipy.org/moin/About); I don't use it but I get the impression
> that if it doesn't already keep a stack or queue of recent results,
> you could implement it yourself easily.

As you guessed, __ and ___ have the expected meaning in IPython.
moreover the nth output in the session is stored in _n.

--
Arnaud

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


Re: function-class frustration

2008-03-22 Thread John Machin
On Mar 22, 7:19 pm, [EMAIL PROTECTED] wrote:

>
> On the other hand, are you willing to make changes to the console?
> How big and what?  I was thinking of __ to keep the second-to-last
> most recent command.

Access to and editing of previous input lines is already provided by
the cooked mode of the console (e.g. on Windows, lean on the up-arrow
key) or can be obtained by installing the readline package. Perhaps by
analogy with _, you mean the *result* of the penultimate *expression*.
This functionality would be provided by the Python interactive
interpreter but it's not ... do have a look at IPython (http://
ipython.scipy.org/moin/About); I don't use it but I get the impression
that if it doesn't already keep a stack or queue of recent results,
you could implement it yourself easily.
-- 
http://mail.python.org/mailman/listinfo/python-list


function-class frustration

2008-03-22 Thread castironpi
>>> FunctionType.__eq__= 0
Traceback (most recent call last):
  File "", line 1, in 
TypeError: can't set attributes of built-in/extension type 'function'
>>> type( 'F', ( FunctionType, ), { '__eq__': e } )
Traceback (most recent call last):
  File "", line 1, in 
TypeError: type 'function' is not an acceptable base type

On the other hand, are you willing to make changes to the console?
How big and what?  I was thinking of __ to keep the second-to-last
most recent command.
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: How to find parent function/class of generators?

2007-08-31 Thread Lawrence D'Oliveiro
In message <[EMAIL PROTECTED]>, J. A.
Aczel wrote:

> Unfortunately, generator objects don't seem to include any information
> about the parent object or function that created them.

So add your own.

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


Re: How to find parent function/class of generators?

2007-08-25 Thread Terry Reedy

"J. A. Aczel" <[EMAIL PROTECTED]> wrote in message 
news:[EMAIL PROTECTED]
| Hello all. I think I should explain my problem in detail, so everyone
| understands what I'm trying to do and why. There may be a better
| approach to the whole problem then I am using.
|
| I am working on a game, with most of my AI code & such divided up into
| generator functions (really coroutines that don't take any input)
| stored in a dictionary called Director.tasks, and handled by a simple
| cooperative multitasking engine. The dictionary key is the generator
| object to be advanced, and the value is the frame number at which it
| should next be executed. The generator objects yield the delay before
| their next execution.
|
|def realtick(self):
|Director.frame += 1
|for task in Director.tasks.items():
|if task[1] <= Director.frame:
|try:
|result = task[0].next()
|if result == -1:
|Director.tasks.pop(task[0])
|else:
|Director.tasks[task[0]] = Director.frame + result
|except:
|Director.tasks.pop(task[0])
|
| Why did I use a dictionary? Originally I had a list of lists, but I
| thought converting it to a dictionary would make removing tasks from
| the queue easier, the main difficulty I'm having. But it didn't,
| because I can't identify the

The standard way of storing multiple tasks for future execution at a 
varietyu of times is a priority queue.  Python's heap class (module) is one 
way to implement such.

| Due to the framework I'm using, I think (or possibly just the Python
| language?) in-game objects aren't actually destroyed when they're
| removed from the game world. Thus, when an enemy or something with
| associated tasks 'dies', the tasks merrily continue executing... and
| the game will have a bunch of enemies who no longer have sprites,
| movement abilities or collision checks, but otherwise are none the
| worse for wear for having been destroyed. It's a big mess. So I need a
| way to remove all tasks associated with a game object from the queue,
| upon the object's  'destruction'.

I suspect you need to keep a list of tasks associated with a creature with 
each creature.  When creature dies, remove them from the queue or keep a 
set of dead tasks that tasks removed from the queue are checked against 
before being executed.

Good luck.

tjr





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


How to find parent function/class of generators?

2007-08-25 Thread J. A. Aczel
Hello all. I think I should explain my problem in detail, so everyone
understands what I'm trying to do and why. There may be a better
approach to the whole problem then I am using.

I am working on a game, with most of my AI code & such divided up into
generator functions (really coroutines that don't take any input)
stored in a dictionary called Director.tasks, and handled by a simple
cooperative multitasking engine. The dictionary key is the generator
object to be advanced, and the value is the frame number at which it
should next be executed. The generator objects yield the delay before
their next execution.

def realtick(self):
Director.frame += 1
for task in Director.tasks.items():
if task[1] <= Director.frame:
try:
result = task[0].next()
if result == -1:
Director.tasks.pop(task[0])
else:
Director.tasks[task[0]] = Director.frame + result
except:
Director.tasks.pop(task[0])

Why did I use a dictionary? Originally I had a list of lists, but I
thought converting it to a dictionary would make removing tasks from
the queue easier, the main difficulty I'm having. But it didn't,
because I can't identify the

Due to the framework I'm using, I think (or possibly just the Python
language?) in-game objects aren't actually destroyed when they're
removed from the game world. Thus, when an enemy or something with
associated tasks 'dies', the tasks merrily continue executing... and
the game will have a bunch of enemies who no longer have sprites,
movement abilities or collision checks, but otherwise are none the
worse for wear for having been destroyed. It's a big mess. So I need a
way to remove all tasks associated with a game object from the queue,
upon the object's  'destruction'.

Unfortunately, generator objects don't seem to include any information
about the parent object or function that created them. I ran the dir()
builtin on a sample generator object, created from an object method,
and got this:
['__class__', '__delattr__', '__doc__', '__getattribute__',
'__hash__', '__init__', '__iter__', '__new__', '__reduce__',
'__reduce_ex__', '__repr__', '__setattr__', '__str__', 'close',
'gi_frame', 'gi_running', 'next', 'send', 'throw']

__class__ just contains . If __doc__'s the
docstring, I suppose I could use that to identify the generator
object, but it seems hackish nothing else in that list looks
likely. Is this python's message to me that I'm abusing generators for
entirely the wrong purpose? In any case, since I can't figure out how
to determine which generator object corresponds to which game object,
I'm stuck.

I'd appreciate any pointers or suggestions. :) I might have missed
something very obvious, since I'm no pythonista and have generally
been working on this in short spurts late at night. If so, sorry for
making you read all of this!
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: function & class

2007-02-19 Thread Bjoern Schliessmann
jupiter wrote:

> I am getting this error when I am using sql command within
> class.function accessing from another class
> 
> how can I create seperate instance for sqlite objects ???

Trying to help you gets boring. I suggest reading the material
that's being offered.

Regards,


Björn

-- 
BOFH excuse #241:

_Rosin_ core solder? But...

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


Re: function & class

2007-02-18 Thread jupiter
c.execute("select symbol,tvolume,date,time from "+self.dbase+"
where symbol=
?",t)
ProgrammingError: SQLite objects created in a thread can only be used
in that sa
me thread.The object was created in thread id 976 and this is thread
id 944

I am getting this error when I am using sql command within
class.function accessing from another class

how can I create seperate instance for sqlite objects ???


@nil

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


Re: function & class

2007-02-18 Thread Bjoern Schliessmann
jupiter wrote:

> My problem is I want to use threading 

You're right. Why do you think you want to use (multi-)threading?

> and I might need to pass values between function and classes. I am
> not sure how this can be done. I have read about classes and I
> know they are like function however does not return anything where
> as function does. If I define class and then function in this 
> class how do I access this function ?

I think you could read a tutorial about OOP, for example one of
those:

http://www.google.de/search?q=python+oop+tutorial

Feel free to post here for further questions.

> I am not sure and confused about classes and  functions as how to
> go about them is there any simple way to understand difference
> between them and when to use what and how to pass data/reference
> pointer between them ?

BTW, please use some punctuation. Reading your multiline sentences
isn't easy.

Regards,


Björn

-- 
BOFH excuse #393:

Interference from the Van Allen Belt.

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


Re: function & class

2007-02-18 Thread [EMAIL PROTECTED]
Bruno Desthuilliers wrote:
> Classes are used to define and create ('instanciate' in OO jargon)

Good info from Bruno, just a quick note that it's spelled
"instantiate" (I'm not usually big on spelling corrections but since
you were teaching a new word I thought it might be worthwile).

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


Re: function & class

2007-02-18 Thread Bruno Desthuilliers
jupiter a écrit :
> hi friends,
> 
> I have a program like
> 
> some variable definations
> 
> a function ( arg1, agr2):
>   do something with arg1 & arg2
>   return a list
> 
> if condition
>do this function(arg1,arg2)
> else
>   if condition
> do this function
>else
>   do this
> 
> 
> My problem is I want to use threading and I might need to pass values
> between function and classes.

which classes ? I see no class statement in your above pseudo-code.

> I am not sure how this can be done. I
> have read about classes and I know they are like function

Hmm. While technically, yes, Python classes are "callable" just like 
functions are, this is mostly an implementation detail (or at least you 
can consider it as such for now).  Now from a conceptual POV, functions 
and classes are quite different beasts.

> however does
> not return anything where as function does.

Where did you get such "knowledge", if I may ask ? Looks like you need a 
better source of informations !-)

> If I define class and then
> function in this class how do I access this function ?

Classes are used to define and create ('instanciate' in OO jargon) 
objects. Usually, one first creates an object, then calls methods on 
this object:

class Greeter(object):
   def __init__(self, name, saywhat=None):
 self.name = name
 if self.saywhat is None:
   self.saywhat = "Hello %(who)s, greetings from %(name)s"
 else:
   self.saywhat = saywhat

   def greet(self, who):
 return self.saywhat % dict(name=self.name, who=who)


bruno = Greeter('Bruno')
print bruno.greet('Jupiter')

> I am not sure and confused about classes and  functions as how to go
> about them is there any simple way to understand difference between
> them

Read a tutorial on OO ? Here's one:
http://pytut.infogami.com/node11-baseline.html

> and when to use what and how to pass data/reference pointer
> between them ?

In Python, everything you can name, pass to a function or return from a 
function is an object.


> @nil
> Pythonist
> 
-- 
http://mail.python.org/mailman/listinfo/python-list


function & class

2007-02-18 Thread jupiter
hi friends,

I have a program like

some variable definations

a function ( arg1, agr2):
  do something with arg1 & arg2
  return a list

if condition
   do this function(arg1,arg2)
else
  if condition
do this function
   else
  do this


My problem is I want to use threading and I might need to pass values
between function and classes. I am not sure how this can be done. I
have read about classes and I know they are like function however does
not return anything where as function does. If I define class and then
function in this class how do I access this function ?

I am not sure and confused about classes and  functions as how to go
about them is there any simple way to understand difference between
them and when to use what and how to pass data/reference pointer
between them ?

@nil
Pythonist

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