Re: function object.func_default off the console

2007-04-26 Thread castironpi
On Apr 25, 1:56 am, Peter Otten <[EMAIL PROTECTED]> wrote:
> Aaron Brady wrote:
>  f.func_defaults[0]
> > [2, 3]
>  f.func_defaults[0]+=[4]
> > Traceback (most recent call last):
> >File "", line 1, in 
> > TypeError: 'tuple' object does not support item assignment
>  f.func_defaults[0]
> > [2, 3, 4]
>
> > V. interesting.  Operation succeeds but with a throw.  Er, raise.
>
> This is not specific to func_defaults:
>
> >>> t = ([1],)
> >>> t[0] += [2]
>
> Traceback (most recent call last):
>   File "", line 1, in 
> TypeError: 'tuple' object does not support item assignment>>> t
>
> ([1, 2],)
>
> t[0] += [2]
>
> is resolved to
>
> t.__setitem__(t[0].__iadd__([2]))
>
> where list.__iadd__() succeeds but __setitem__() fails (because tuples don't
> have that method).
>
> Peter

Curious why t.__setitem__ is called.  Wouldn't it still refer to the
same list?

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


Re: Memory addressing

2007-04-27 Thread castironpi
On Apr 27, 4:00 pm, Carsten Haese <[EMAIL PROTECTED]> wrote:
> On Fri, 2007-04-27 at 12:56 -0700, Simon Berube wrote:
> > When you call certain objects __repr__() strings in python you often
> > get the :  happen. I am simply trying to
> > understand how that information can be used to recreate a certain
> > object that failed as per the given purpose of the __repr__()
> > functions.
>
> It's not a requirement of repr() that the resulting string be suitable
> for recreating the object. For many built-in object types, calling
> eval() on their repr() will result in a copy of the object, but in
> general eval(repr(obj))==obj will not be true.
>
> > In short, how do I used  strings to recreate
> > a an object.
>
> You don't. What you should do instead depends on what you actually need
> to do, which you haven't said yet. Do you want to pass an object to
> another function, do you want to make a copy of an object, or do you
> want to serialize/unserialize an object to send it through time and/or
> space?
>
> -Carsten

That's what we need: a CopyMemory() routine.

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


exclusive shelve open

2007-04-29 Thread castironpi
I'm trying to be safe and make sure only one python instance opens the
shelf.  Any ideas?

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


Re: exclusive shelve open

2007-04-29 Thread castironpi
On Apr 29, 2:12 pm, [EMAIL PROTECTED] wrote:
> I'm trying to be safe and make sure only one python instance opens the
> shelf.  Any ideas?

Using Windows.

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


Re: Counting

2007-04-29 Thread castironpi
On Apr 29, 2:11 pm, Andy <[EMAIL PROTECTED]> wrote:
> Hi, the file below will print all the keywords in a file and also the
> line # of the keyword. What I couldn't figure out is to count those
> keywords per line. For example - "Line #1 has 3 keywords"
>
> Can I do like -
>
> total[j] = total[j] + numwords(k)
> "Line number %d has %d keywords" % (j, total[j])
>
> Seems sort of "illegal" in Python?
>
> -
> import keyword, sys, string, fileinput
> def numwords(s):
> list = string.split(s)
> return len(list)
>
> # Get the file name either from the command-line or the user
> if len(sys.argv) != 2:
>name = raw_input("Enter the file name: ")
> else:
>name = sys.argv[1]
>
> inp = open(name,"r")
> linelist = inp.readlines()
> total, words,lines = 0, 0, 0
>
> for i in range(len(linelist)):
> line = linelist[i]
> tempwords = line.split()
> for k in tempwords:
> if keyword.iskeyword(k):
> total = total + numwords(k)
> j = i + 1
> print" The word * %s * belongs in line number: %d" % (k,
> j)
>
> print "Total keywords in this file are: %d" %(total)

> tempwords = line.split()
> for k in tempwords:
   linec = 0
> if keyword.iskeyword(k):
> total = total + numwords(k)
> j = i + 1
   linec += 1
> print" The word * %s * belongs in line number: %d" % (k,
> j)
   print "%i characters in line" % linec

And less readably,

> tempwords = line.split()
> for k in tempwords:
   linec = j
> if keyword.iskeyword(k):
> total = total + numwords(k)
> j = i + 1
> print" The word * %s * belongs in line number: %d" % (k,
> j)
   print "%i characters in line" % ( j - linec )

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


module console

2007-05-05 Thread castironpi
Can I get the console to behave like it's in a module?

So far I have inspect.getsource() working by setting the filename and
linenumbers of the return from compiler.parse().  I'm looking too.  -me

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


Re: module console

2007-05-05 Thread castironpi
On May 5, 7:29 pm, [EMAIL PROTECTED] wrote:
> Can I get the console to behave like it's in a module?
>
> So far I have inspect.getsource() working by setting the filename and
> linenumbers of the return from compiler.parse().  I'm looking too.  -me

This at least gets a instance loaded; we'll see.
import imp
m=imp.new_module('aname')
class A: pass

m.A=A

from pickle import *
m.A.__module__='aname'
import sys
sys.modules['aname']=m
a=m.A()
loads(dumps(a))
#fb: 

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


inspected console

2007-05-07 Thread castironpi
Presents a console permitting inspection.  Input as well as output
saved in Python-readable form.
Python 2.5.1 memoryconsole4.py logging to My Documents\console.log
>>> class A:
... def f( self ):
... print 2
...
>>> a=A()
>>> import inspect
>>> inspect.getsource( a.f )
'\tdef f( self ):\n\t\tprint 2\n'

This enabled by a log file, optionally set to console.log.  Contents
are:

#Mon May 07 2007 06:33:42 PM Python win32 2.5.1 in memoryconsole4.py
class A:
def f( self ):
print 2

a=A()
import inspect
inspect.getsource( a.f )
#fb: '\tdef f( self ):\n\t\tprint 2\n'

Line 10 Microsoft Win32 convenience binding; line 49 a little
confusing.  Give it a shot.

from code import InteractiveConsole
import sys
from os import environ
from datetime import datetime
from StringIO import StringIO
from re import sub
from os.path import join,split,abspath

class LoggedStdOut(StringIO):
deflog= environ['USERPROFILE']+\
'\\My Documents\\console.log'
def __init__( self, log=None ):
StringIO.__init__( self )
self.stdout= None
self.trip,self.head= True,''
self.logname= log or LoggedStdOut.deflog
self.prettyname=join(split(split(abspath(
self.logname))[0])[1],split(abspath(self.logname))[1])
for x,_ in enumerate( open( self.logname,'r' ) ): continue
self._lineno= x #can use linecache
self._log= open( self.logname,'a' )
def catch( self,head='#fb: ' ):
self.stdout= sys.stdout
sys.stdout= self
self.head= head
self.trip= True
def throw( self ):
sys.stdout= self.stdout
self.stdout= None
def getlineno( self ):
return self._lineno
def logwrite( self, data ):
self._log.write( data )
self._lineno+= data.count('\n')
def logflush( self ):
self._log.flush()
def write( self, data ):
datal= sub( '\n([^$])','\n%s\\1'%self.head,data )
if self.trip: self.logwrite( self.head )
self.logwrite( datal )
self.trip= data.endswith('\n')
return self.stdout.write( data )
def writelines( self, data ):
raise 'Branch uncoded'

class LoggedInteractiveConsole(InteractiveConsole):
def __init__( self,log=None,locals=None,filename=None ):
self.out= LoggedStdOut( log )
if filename is None: filename= split(self.out.logname)[1]
InteractiveConsole.__init__( self,locals,filename )
self.locals.update( __logname__= abspath(
self.out.logname ) )
def push( self,line ):
self.out.logwrite( '%s\n'%line )
self.out.logflush()
self.out.catch()
more= InteractiveConsole.push( self,line )
self.out.throw()
return more
def write( self,data ):
return sys.stdout.write( data )
def interact( self,banner=None,*args ):
self.out.logwrite( '\n#%s Python %s %s in %s\n'%\
( datetime.now().strftime(
'%a %b %d %Y %I:%M:%S %p' ),
sys.platform,sys.version.split()[0],
split(sys.argv[0])[1] ) )
if banner is None: banner=\
"Python %s %s logging to %s"%\
( sys.version.split()[0],split(sys.argv[0])[1],
self.out.prettyname )
return InteractiveConsole.interact( self,banner,*args )


import compiler
import linecache
class NotatedConsole(LoggedInteractiveConsole):
"""-Code object- intercepted in runsource, and rerun with
stored source before runsource.  Built-in runsource
does not modify source between call and runcode."""
def runsource( self,sc,filename='',*args ):
self._runsourceargs= sc,filename
return LoggedInteractiveConsole.runsource( self,sc,
filename,*args )
def runcode( self,*args ):
sc,filename= self._runsourceargs
linecache.checkcache( filename )
#custom second compile (fourth actually)
t= compiler.parse( sc )
compiler.misc.set_filename( filename,t )
def set_lineno( tree, initlineno ):
worklist= [ tree ]
while worklist:
node= worklist.pop( 0 )
if node.lineno is not None:
node.lineno+= initlineno
worklist.extend( node.getChildNodes() )
 

Re: inspected console

2007-05-07 Thread castironpi
On May 7, 6:52 pm, [EMAIL PROTECTED] wrote:
> Presents a console permitting inspection.  Input as well as output
> saved in Python-readable form.
> Python 2.5.1 memoryconsole4.py logging to My Documents\console.log>>> class A:
>
> ... def f( self ):
> ... print 2
> ...>>> a=A()
> >>> import inspect
> >>> inspect.getsource( a.f )
>
> '\tdef f( self ):\n\t\tprint 2\n'
>
> This enabled by a log file, optionally set to console.log.  Contents
> are:
>
> #Mon May 07 2007 06:33:42 PM Python win32 2.5.1 in memoryconsole4.py
> class A:
> def f( self ):
> print 2
>
> a=A()
> import inspect
> inspect.getsource( a.f )
> #fb: '\tdef f( self ):\n\t\tprint 2\n'
>
> Line 10 Microsoft Win32 convenience binding; line 49 a little
> confusing.  Give it a shot.
>
> from code import InteractiveConsole
> import sys
> from os import environ
> from datetime import datetime
> from StringIO import StringIO
> from re import sub
> from os.path import join,split,abspath
>
> class LoggedStdOut(StringIO):
> deflog= environ['USERPROFILE']+\
> '\\My Documents\\console.log'
> def __init__( self, log=None ):
> StringIO.__init__( self )
> self.stdout= None
> self.trip,self.head= True,''
> self.logname= log or LoggedStdOut.deflog
> self.prettyname=join(split(split(abspath(
> self.logname))[0])[1],split(abspath(self.logname))[1])
> for x,_ in enumerate( open( self.logname,'r' ) ): continue
> self._lineno= x #can use linecache
> self._log= open( self.logname,'a' )
> def catch( self,head='#fb: ' ):
> self.stdout= sys.stdout
> sys.stdout= self
> self.head= head
> self.trip= True
> def throw( self ):
> sys.stdout= self.stdout
> self.stdout= None
> def getlineno( self ):
> return self._lineno
> def logwrite( self, data ):
> self._log.write( data )
> self._lineno+= data.count('\n')
> def logflush( self ):
> self._log.flush()
> def write( self, data ):
> datal= sub( '\n([^$])','\n%s\\1'%self.head,data )
> if self.trip: self.logwrite( self.head )
> self.logwrite( datal )
> self.trip= data.endswith('\n')
> return self.stdout.write( data )
> def writelines( self, data ):
> raise 'Branch uncoded'
>
> class LoggedInteractiveConsole(InteractiveConsole):
> def __init__( self,log=None,locals=None,filename=None ):
> self.out= LoggedStdOut( log )
> if filename is None: filename= split(self.out.logname)[1]
> InteractiveConsole.__init__( self,locals,filename )
> self.locals.update( __logname__= abspath(
> self.out.logname ) )
> def push( self,line ):
> self.out.logwrite( '%s\n'%line )
> self.out.logflush()
> self.out.catch()
> more= InteractiveConsole.push( self,line )
> self.out.throw()
> return more
> def write( self,data ):
> return sys.stdout.write( data )
> def interact( self,banner=None,*args ):
> self.out.logwrite( '\n#%s Python %s %s in %s\n'%\
> ( datetime.now().strftime(
> '%a %b %d %Y %I:%M:%S %p' ),
> sys.platform,sys.version.split()[0],
> split(sys.argv[0])[1] ) )
> if banner is None: banner=\
> "Python %s %s logging to %s"%\
> ( sys.version.split()[0],split(sys.argv[0])[1],
> self.out.prettyname )
> return InteractiveConsole.interact( self,banner,*args )
>
> import compiler
> import linecache
> class NotatedConsole(LoggedInteractiveConsole):
> """-Code object- intercepted in runsource, and rerun with
> stored source before runsource.  Built-in runsource
> does not modify source between call and runcode."""
> def runsource( self,sc,filename='',*args ):
> self._runsourceargs= sc,filename
> return LoggedInteractiveConsole.runsource( self,sc,
> filename,*args )
> def runcode( self,*args ):
> sc,filename= self._runsourceargs
> linecache.checkcache( filename )
> #custom second compile (fourth actually)
> t= compiler.parse( sc )
> compiler.misc.set_filename( filename,t )
> def set_lineno( tree, initlineno ):
> worklist= [ tree ]
> w

Re: inspected console

2007-05-07 Thread castironpi
On May 7, 7:59 pm, [EMAIL PROTECTED] wrote:
> On May 7, 6:52 pm, [EMAIL PROTECTED] wrote:
>
>
>
> > Presents a console permitting inspection.  Input as well as output
> > saved in Python-readable form.
> > Python 2.5.1 memoryconsole4.py logging to My Documents\console.log>>> class 
> > A:
>
> > ... def f( self ):
> > ... print 2
> > ...>>> a=A()
> > >>> import inspect
> > >>> inspect.getsource( a.f )
>
> > '\tdef f( self ):\n\t\tprint 2\n'
>
> > This enabled by a log file, optionally set to console.log.  Contents
> > are:
>
> > #Mon May 07 2007 06:33:42 PM Python win32 2.5.1 in memoryconsole4.py
> > class A:
> > def f( self ):
> > print 2
>
> > a=A()
> > import inspect
> > inspect.getsource( a.f )
> > #fb: '\tdef f( self ):\n\t\tprint 2\n'
>
> > Line 10 Microsoft Win32 convenience binding; line 49 a little
> > confusing.  Give it a shot.
>
> > from code import InteractiveConsole
> > import sys
> > from os import environ
> > from datetime import datetime
> > from StringIO import StringIO
> > from re import sub
> > from os.path import join,split,abspath
>
> > class LoggedStdOut(StringIO):
> > deflog= environ['USERPROFILE']+\
> > '\\My Documents\\console.log'
> > def __init__( self, log=None ):
> > StringIO.__init__( self )
> > self.stdout= None
> > self.trip,self.head= True,''
> > self.logname= log or LoggedStdOut.deflog
> > self.prettyname=join(split(split(abspath(
> > 
> > self.logname))[0])[1],split(abspath(self.logname))[1])
> > for x,_ in enumerate( open( self.logname,'r' ) ): continue
> > self._lineno= x #can use linecache
> > self._log= open( self.logname,'a' )
> > def catch( self,head='#fb: ' ):
> > self.stdout= sys.stdout
> > sys.stdout= self
> > self.head= head
> > self.trip= True
> > def throw( self ):
> > sys.stdout= self.stdout
> > self.stdout= None
> > def getlineno( self ):
> > return self._lineno
> > def logwrite( self, data ):
> > self._log.write( data )
> > self._lineno+= data.count('\n')
> > def logflush( self ):
> > self._log.flush()
> > def write( self, data ):
> > datal= sub( '\n([^$])','\n%s\\1'%self.head,data )
> > if self.trip: self.logwrite( self.head )
> > self.logwrite( datal )
> > self.trip= data.endswith('\n')
> > return self.stdout.write( data )
> > def writelines( self, data ):
> > raise 'Branch uncoded'
>
> > class LoggedInteractiveConsole(InteractiveConsole):
> > def __init__( self,log=None,locals=None,filename=None ):
> > self.out= LoggedStdOut( log )
> > if filename is None: filename= split(self.out.logname)[1]
> > InteractiveConsole.__init__( self,locals,filename )
> > self.locals.update( __logname__= abspath(
> > self.out.logname ) )
> > def push( self,line ):
> > self.out.logwrite( '%s\n'%line )
> > self.out.logflush()
> > self.out.catch()
> > more= InteractiveConsole.push( self,line )
> > self.out.throw()
> > return more
> > def write( self,data ):
> > return sys.stdout.write( data )
> > def interact( self,banner=None,*args ):
> > self.out.logwrite( '\n#%s Python %s %s in %s\n'%\
> > ( datetime.now().strftime(
> > '%a %b %d %Y %I:%M:%S %p' ),
> > sys.platform,sys.version.split()[0],
> > split(sys.argv[0])[1] ) )
> > if banner is None: banner=\
> > "Python %s %s logging to %s"%\
> > ( sys.version.split()[0],split(sys.argv[0])[1],
> > self.out.prettyname )
> > return InteractiveConsole.interact( self,banner,*args )
>
> > import compiler
> > import linecache
> > class NotatedConsole(LoggedInteractiveConsole):
> > """-Code object- intercepted in runsource, and rerun with
> > stored source before runsource.  Built-in runsource
> > does not modify source between call and runcode."""
> > def runsource( self,sc,filename='',*args ):
> > self._runsourceargs= sc,filename
> > return LoggedInteractiveConsole.runsource( self,sc,
> > filename,*args )
> > def runcode( self,*args ):
> > sc,filename= self._runsourceargs
> > linecache.checkcache( filename )
> > 

Re: interesting exercise

2007-05-07 Thread castironpi
On May 7, 10:45 pm, Michael Tobis <[EMAIL PROTECTED]> wrote:
> I want a list of all ordered permutations of a given length of a set
> of tokens. Each token is a single character, and for convenience, they
> are passed as a string in ascending ASCII order.
>
> For example
>
> permute("abc",2)
>
> should return ["aa","ab","ac","ba","bb","bc","ca","cb","cc"]
>
> and permute("13579",3) should return a list of 125 elements
> ["111","113", ... ,"997","999"]
>
> permute("axc",N) or permute("2446",N) should raise ValueError as the
> alphabet is not strictly sorted.
>
> I have a reasonably elegant solution but it's a bit verbose (a couple
> dozen lines which I'll post later if there is interest). Is there some
> clever Pythonism I didn't spot?
>
> thanks
> mt

Post yours.

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


Re: interesting exercise

2007-05-07 Thread castironpi
On May 7, 11:34 pm, [EMAIL PROTECTED] wrote:
> On May 7, 10:45 pm, Michael Tobis <[EMAIL PROTECTED]> wrote:
>
>
>
> > I want a list of all ordered permutations of a given length of a set
> > of tokens. Each token is a single character, and for convenience, they
> > are passed as a string in ascending ASCII order.
>
> > For example
>
> > permute("abc",2)
>
> > should return ["aa","ab","ac","ba","bb","bc","ca","cb","cc"]
>
> > and permute("13579",3) should return a list of 125 elements
> > ["111","113", ... ,"997","999"]
>
> > permute("axc",N) or permute("2446",N) should raise ValueError as the
> > alphabet is not strictly sorted.
>
> > I have a reasonably elegant solution but it's a bit verbose (a couple
> > dozen lines which I'll post later if there is interest). Is there some
> > clever Pythonism I didn't spot?
>
> > thanks
> > mt
>
> Post yours.

Oh well, as I'm not the first.
def p(a,b):
if list( a ) != sorted( list( a ) ): raise ValueError, "String not
ordered."
if not b: return ['']
a = sorted( set( a ) )
return [i+j for i in a for j in p(a,b-1)]

p('abc',3)
#fb: ['aaa', 'aab', 'aac', 'aba', 'abb', 'abc', 'aca', 'acb', 'acc',
'baa', 'bab', 'bac', 'bba', 'bbb', 'bbc', 'bca', 'bcb', 'bcc', 'caa',
'cab', 'cac', 'cba', 'cbb', 'cbc', 'cca', 'ccb', 'ccc']
p('abc',2)
#fb: ['aa', 'ab', 'ac', 'ba', 'bb', 'bc', 'ca', 'cb', 'cc']
len(p("13579",3))
#fb: 125
edit()

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


Re: interesting exercise

2007-05-07 Thread castironpi
On May 7, 11:42 pm, [EMAIL PROTECTED] wrote:
> On May 7, 11:34 pm, [EMAIL PROTECTED] wrote:
>
>
>
> > On May 7, 10:45 pm, Michael Tobis <[EMAIL PROTECTED]> wrote:
>
> > > I want a list of all ordered permutations of a given length of a set
> > > of tokens. Each token is a single character, and for convenience, they
> > > are passed as a string in ascending ASCII order.
>
> > > For example
>
> > > permute("abc",2)
>
> > > should return ["aa","ab","ac","ba","bb","bc","ca","cb","cc"]
>
> > > and permute("13579",3) should return a list of 125 elements
> > > ["111","113", ... ,"997","999"]
>
> > > permute("axc",N) or permute("2446",N) should raise ValueError as the
> > > alphabet is not strictly sorted.
>
> > > I have a reasonably elegant solution but it's a bit verbose (a couple
> > > dozen lines which I'll post later if there is interest). Is there some
> > > clever Pythonism I didn't spot?
>
> > > thanks
> > > mt
>
> > Post yours.
>
> Oh well, as I'm not the first.
> [working solution snip]

Or even,
def p(a,b):
if list( a ) != sorted( list( a ) ): raise ValueError, "String not
ordered."
if not b: return ['']
return [i+j for i in list(a) for j in p(a,b-1)]

p('abc',3)
#fb: ['aaa', 'aab', 'aac', 'aba', 'abb', 'abc', 'aca', 'acb', 'acc',
'baa', 'bab', 'bac', 'bba', 'bbb', 'bbc', 'bca', 'bcb', 'bcc', 'caa',
'cab', 'cac', 'cba', 'cbb', 'cbc', 'cca', 'ccb', 'ccc']
p('abc',2)
#fb: ['aa', 'ab', 'ac', 'ba', 'bb', 'bc', 'ca', 'cb', 'cc']
len(p("13579",3))
#fb: 125
edit()

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


Re: interesting exercise

2007-05-07 Thread castironpi
On May 8, 12:24 am, [EMAIL PROTECTED] (Alex Martelli) wrote:
> <[EMAIL PROTECTED]> wrote:
>
>...
>
> > def p(a,b):
> >   if list( a ) != sorted( list( a ) ): raise ValueError, "String not
> > ordered."
> >   if not b: return ['']
> >   return [i+j for i in list(a) for j in p(a,b-1)]
>
> No need for 2/3 of the list(...) calls.  sorted(a) and sorted(list(a))
> will ALWAYS be the same sequence; "for i in a" and "for i in list(a)"
> will always iterate on the same sequence [as long as you're not changing
> a inside the iteration, which, in this case, you aren't].
>
> Alex

Ah quite true.  list( a ) != sorted( a ).  ...for i in a for...

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


Re: interesting exercise

2007-05-08 Thread castironpi
On May 8, 3:55 pm, James Stroud <[EMAIL PROTECTED]> wrote:
> Steven D'Aprano wrote:
> > On Tue, 08 May 2007 10:22:05 +, James Stroud wrote:
>
> >>This takes annoying past annoying to some new level of hell to which
> >>even satan himself wouldn't venture.
>
> > And thank you for sharing that piece of spam with us again. It was so much
> > less enjoyable to see it the second time.
>
> > Seriously James, with more and more people using automated spam filters,
> > it might not be such a wise idea to keep having your name associated with
> > spam content.
>
> Thank you for the tip.
>
> James

We also have:

p=lambda a,n: [ ''.join( y ) for y in eval('[%%s %s]'%' '.join(['for x
%i in a'%i for i in range(n)]) %'(%s)'%','.join(['x%i'%i for i in
range(n)]) ) ]
p('abc',2)
#fb: ['aa', 'ab', 'ac', 'ba', 'bb', 'bc', 'ca', 'cb', 'cc']
len(p('13579',3))
#fb: 125
edit()

File under obscurities. acb

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


Re: interesting exercise

2007-05-08 Thread castironpi
On May 8, 4:55 pm, [EMAIL PROTECTED] wrote:
> On May 8, 3:55 pm, James Stroud <[EMAIL PROTECTED]> wrote:
>
>
>
> > Steven D'Aprano wrote:
> > > On Tue, 08 May 2007 10:22:05 +, James Stroud wrote:
>
> > >>This takes annoying past annoying to some new level of hell to which
> > >>even satan himself wouldn't venture.
>
> > > And thank you for sharing that piece of spam with us again. It was so much
> > > less enjoyable to see it the second time.
>
> > > Seriously James, with more and more people using automated spam filters,
> > > it might not be such a wise idea to keep having your name associated with
> > > spam content.
>
> > Thank you for the tip.
>
> > James
>
> We also have:
>
> p=lambda a,n: [ ''.join( y ) for y in eval('[%%s %s]'%' '.join(['for x
> %i in a'%i for i in range(n)]) %'(%s)'%','.join(['x%i'%i for i in
> range(n)]) ) ]
> p('abc',2)
> #fb: ['aa', 'ab', 'ac', 'ba', 'bb', 'bc', 'ca', 'cb', 'cc']
> len(p('13579',3))
> #fb: 125
> edit()
>
> File under obscurities. acb

Slightly clearer:
p=lambda a,n:[ ''.join( y ) for y in eval('[(%s) %s]'%(','.join(['x
%i'%i for i in range(n)]),' '.join(['for x%i in a'%i for i in
range(n)])))]
p('abc',2)
#fb: ['aa', 'ab', 'ac', 'ba', 'bb', 'bc', 'ca', 'cb', 'cc']
len(p('13579',3))
#fb: 125
edit()

where for n=4:
('[(%s) %s]'%(','.join(['x%i'%i for i in range(n)]),' '.join(['for x%i
in a'%i for i in range(n)])))
#fb: '[(x0,x1,x2,x3) for x0 in a for x1 in a for x2 in a for x3 in a]'

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


tokenize generate_tokens token[0]

2007-05-08 Thread castironpi
Is token[0] guaranteed to be OP for parentheses, commas, etc.?

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


Re: interesting exercise

2007-05-09 Thread castironpi
On May 9, 1:13 am, Charles Sanders <[EMAIL PROTECTED]>
wrote:
> Michael Tobis wrote:
> > Here is the bloated mess I came up with. I did see that it had to be
> > recursive, and was proud of myself for getting it pretty much on the
> > first try, but the thing still reeks of my sorry old fortran-addled
> > mentality.
>
> Recursion is not necessary, but is much, much clearer.
>
> Here is one non-recursive version from another aging
> fortran programmer. I agree it is less clear than most
> of the recursive alternatives. No checks for sorted
> input etc, these are left as an exercise for the reader.
>
> def permute( s, n ):
>def _perm( m, n ):
>  ilist = [0]*n
>  while True:
>yield ilist
>i = n-1
>while i >= 0 and ilist[i]>=m-1: i = i - 1
>if i >= 0:
>  ilist = ilist[0:i] + [ilist[i]+1] + [0]*(n-i-1)
>else:
>  return
>
>return [ ''.join([s[i] for i in ilist])
>  for ilist in _perm(len(s),n) ]
>
> print "permute('abc',2) = ", permute('abc',2)
> print "len(permute('13579',3)) = ", len(permute('13579',3))
>
> permute('abc',2) =  ['aa', 'ab', 'ac', 'ba', 'bb', 'bc',
> 'ca', 'cb', 'cc']
> len(permute('13579',3)) =  125
>
> or even this monstrosity ...
>
> def permute2( s, n ):
>return [ ''.join([ s[int(i/len(s)**j)%len(s)]
>  for j in range(n-1,-1,-1)])
>for i in range(len(s)**n) ]
>
> print "permute2('abc',2) =", permute2('abc',2)
> print "len(permute2('13579',3)) =", len(permute2('13579',3))
>
> permute2('abc',2) = ['aa', 'ab', 'ac', 'ba', 'bb', 'bc',
>   'ca', 'cb', 'cc']
> len(permute2('13579',3)) = 125
>
> Charles

Could you explain, this one, actually?  Don't forget StopIteration.

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


Re: Checking if string inside quotes?

2007-05-09 Thread castironpi
On May 9, 4:31 pm, "Michael Yanowitz" <[EMAIL PROTECTED]> wrote:
> Thanks, but it is a little more complicated than that,
>   the string could be deep in quotes.
>
>The problem is in string substitution.
> Suppose I have a dictionary with MY_IP : "172.18.51.33"
>
>   I need to replace all instances of MY_IP with "172.18.51.33"
> in the file.
>   It is easy in cases such as:
>   if (MY_IP == "127.0.0.1"):
>
>   But suppose I encounter:"
>  ("(size==23) and (MY_IP==127.0.0.1)")
>
>In this case I do not want:
>  ("(size==23) and ("172.18.51.33"==127.0.0.1)")
> but:
>  ("(size==23) and (172.18.51.33==127.0.0.1)")
> without the internal quotes.
>  How can I do this?
>   I presumed that I would have to check to see if the string
> was already in quotes and if so remove the quotes. But not
> sure how to do that?
>   Or is there an easier way?
>
> Thanks in advance:
> Michael Yanowitz
>
> -Original Message-
> From: [EMAIL PROTECTED]
>
> [mailto:[EMAIL PROTECTED] Behalf
> Of [EMAIL PROTECTED]
> Sent: Wednesday, May 09, 2007 5:12 PM
> To: [EMAIL PROTECTED]
> Subject: Re: Checking if string inside quotes?
>
> On May 9, 1:39 pm, "Michael Yanowitz" <[EMAIL PROTECTED]> wrote:
> > Hello:
>
> >If I have a long string (such as a Python file).
> > I search for a sub-string in that string and find it.
> > Is there a way to determine if that found sub-string is
> > inside single-quotes or double-quotes or not inside any quotes?
> > If so how?
>
> > Thanks in advance:
> > Michael Yanowitz
>
> I think the .find() method returns the index of the found string.  You
> could check one char before and then one char after the length of the
> string to see.  I don't use regular expressions much, but I'm sure
> that's a more elegant approach.
>
> This will work. You'll get in index error if you find the string at
> the very end of the file.
>
> s = """
> foo
> "bar"
> """
> findme = "foo"
> index = s.find(findme)
>
> if s[index-1] == "'" and s[index+len(findme)] == "'":
> print "single quoted"
> elif s[index-1] == "\"" and s[index+len(findme)] == "\"":
>print "double quoted"
> else:
>print "unquoted"
>
> ~Sean
>
> --http://mail.python.org/mailman/listinfo/python-list

In "nearby" quotes or in quotes at all?
import re
a='abc"def"ghijk'
b=re.sub( r'([\'"])[^\1]*\1', '', a )
b.replace( 'ghi', 'the string' )
#fb: 'abcthe stringjk'
edit()

Here, you get the entire file -in b-, strings omitted entirely, so you
can't write it back.

I've used `tokenize' to parse a file, but you don't get precisely your
original back.  Untokenize rearrages your spacings.  Equivalent
semantically, so if you want to compile immedately afterwords, you're
alright with that.  Short example:
from tokenize import *
import token
from StringIO import StringIO
a= StringIO( 'abc "defghi" ghi jk' )
from collections import deque
b= deque()
for g in generate_tokens( a.readline ):
if g[0]== token.NAME and g[1]== 'ghi':
b.append( ( token.STRING, '"uchoose"' ) )
else:
b.append( g )

untokenize( b )
#fb: 'abc "defghi""uchoose"jk '
edit()
acb

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


Re: Behavior of mutable class variables

2007-05-09 Thread castironpi
On May 9, 5:49 pm, [EMAIL PROTECTED] wrote:
> Thanks for the insights. I solved the problem as follows: I created a
> new class method called cleanUp, which resets NStocks to an empty list
> and N1 to 0. Works like a charm - it's the first time I've used a
> class method, and I immediately see its utility. Thanks again
>
> class Stock(object):
> NStocks = [] #Class variables
> N1 = 0
>
> @classmethod
> def cleanUp(cls):
> Stocks.NStocks = []
> Stocks.N1 = 0
>
> def simulation(N, par1, par2, idList, returnHistoryDir):
>
> Stock.cleanUp()
> results = ..
> print results.

class A:
b= 0

A.b
a= A()
a.b
a.b+= 1
a.b
A.b
A.b=20
a.b
A.b
a1= A()
a1.b
a.b
A.b
a1.b+=10
a1.b
a.b
A.b

It looks like an instance gets its own copy of A's dictionary upon
creation, and -can- no longer affect A's dictionary, though both can
be changed elsewhere.

Doesn't seem prudent to -obscure- a class by an instance, but if
that's not what goes on, then I'm missing something.

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


Re: Checking if string inside quotes?

2007-05-09 Thread castironpi
On May 9, 8:48 pm, [EMAIL PROTECTED] wrote:
> On May 9, 2:31 pm, "Michael Yanowitz" <[EMAIL PROTECTED]> wrote:
>
>
>
> > Thanks, but it is a little more complicated than that,
> >   the string could be deep in quotes.
>
> >The problem is in string substitution.
> > Suppose I have a dictionary with MY_IP : "172.18.51.33"
>
> >   I need to replace all instances of MY_IP with "172.18.51.33"
> > in the file.
> >   It is easy in cases such as:
> >   if (MY_IP == "127.0.0.1"):
>
> >   But suppose I encounter:"
> >  ("(size==23) and (MY_IP==127.0.0.1)")
>
> >In this case I do not want:
> >  ("(size==23) and ("172.18.51.33"==127.0.0.1)")
> > but:
> >  ("(size==23) and (172.18.51.33==127.0.0.1)")
> > without the internal quotes.
> >  How can I do this?
> >   I presumed that I would have to check to see if the string
> > was already in quotes and if so remove the quotes. But not
> > sure how to do that?
> >   Or is there an easier way?
>
> > Thanks in advance:
> > Michael Yanowitz
>
> > -Original Message-
> > From: [EMAIL PROTECTED]
>
> > [mailto:[EMAIL PROTECTED] Behalf
> > Of [EMAIL PROTECTED]
> > Sent: Wednesday, May 09, 2007 5:12 PM
> > To: [EMAIL PROTECTED]
> > Subject: Re: Checking if string inside quotes?
>
> > On May 9, 1:39 pm, "Michael Yanowitz" <[EMAIL PROTECTED]> wrote:
> > > Hello:
>
> > >If I have a long string (such as a Python file).
> > > I search for a sub-string in that string and find it.
> > > Is there a way to determine if that found sub-string is
> > > inside single-quotes or double-quotes or not inside any quotes?
> > > If so how?
>
> > > Thanks in advance:
> > > Michael Yanowitz
>
> > I think the .find() method returns the index of the found string.  You
> > could check one char before and then one char after the length of the
> > string to see.  I don't use regular expressions much, but I'm sure
> > that's a more elegant approach.
>
> > This will work. You'll get in index error if you find the string at
> > the very end of the file.
>
> > s = """
> > foo
> > "bar"
> > """
> > findme = "foo"
> > index = s.find(findme)
>
> > if s[index-1] == "'" and s[index+len(findme)] == "'":
> > print "single quoted"
> > elif s[index-1] == "\"" and s[index+len(findme)] == "\"":
> >print "double quoted"
> > else:
> >print "unquoted"
>
> > ~Sean
>
> > --http://mail.python.org/mailman/listinfo/python-list
>
> In that case I suppose you'd have to read the file line by line and if
> you find your string in the line then search for the indexes of any
> matching quotes.  If you find matching quotes, see if your word lies
> within any of the quote indexes.
>
> #!/usr/bin/env python
>
> file = open("file", 'r')
> findme= "foo"
> for j, line in enumerate(file):
> found = line.find(findme)
> if found != -1:
> quotecount = line.count("'")
> quoteindexes = []
> start = 0
> for i in xrange(quotecount):
> i = line.find("'", start)
> quoteindexes.append(i)
> start = i+1
>
> f = False
> for i in xrange(len(quoteindexes)/2):
> if findme in
> line[quoteindexes.pop(0):quoteindexes.pop(0)]:
> f = True
> print "Found %s on line %s: Single-Quoted" % (findme, j
> +1)
> if not f:
> print "Found %s on line %s: Not quoted" % (findme, j+1)
>
> It's not pretty but it works.
>
> ~Sean

This approach omits double-quoted strings, escaped single-quotes "'a
\'b' my tag", triple-quoted strings, as well as multi-line strings of
any type.

Depends what constraints you can sacrifice.  Maybe character-at-a-
time, or manually untokenize the solution above.  For generic input,
use mine.

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


Re: interesting exercise

2007-05-09 Thread castironpi
On May 9, 7:49 pm, Charles Sanders <[EMAIL PROTECTED]>
wrote:
> [EMAIL PROTECTED] wrote:
> > On May 9, 1:13 am, Charles Sanders <[EMAIL PROTECTED]>
> > wrote:
> [snip]
> >> or even this monstrosity ...
>
> >> def permute2( s, n ):
> >>return [ ''.join([ s[int(i/len(s)**j)%len(s)]
> >>  for j in range(n-1,-1,-1)])
> >>for i in range(len(s)**n) ]
>
> >> print "permute2('abc',2) =", permute2('abc',2)
> >> print "len(permute2('13579',3)) =", len(permute2('13579',3))
>
> >> permute2('abc',2) = ['aa', 'ab', 'ac', 'ba', 'bb', 'bc',
> >>   'ca', 'cb', 'cc']
> >> len(permute2('13579',3)) = 125
>
> >> Charles
>
> > Could you explain, this one, actually?  Don't forget StopIteration.
>
> As Michael said, simple counting in base n with the
> string as the digits. No attempt at clarity or efficiency (I
> did say it was a "monstrosity").
>
> Also, as a python beginner I didn't know about divmod,
> and have no idea what you (castironpi) mean by "Don't forget
> StopIteration."
>
> Charles

Please disregard.  I have just learned that:
"If the generator exits without yielding another value, a
StopIteration exception is raised."
"exception StopIteration
Raised by an iterator's next() method to signal that there are no
further values."
Means normal generator termination.

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


the inspect thing

2007-05-10 Thread castironpi
-the code:
class A:
b=2

import inspect
print inspect.getsource(A)

class A:
c=2
print inspect.getsource(A)

-unavailable from the console, but gets you:
class A:
b=2

class A:
b=2

One thought is, in inspect, could at least:
def findsource(object):
   #snip
if candidates:
# this will sort by whitespace, and by line number,
# less whitespace first
candidates.sort()
return lines, candidates[0][1]
be
return lines, candidates[-1][1]
to get the most recent?

Why no cl_firstlineno in the object for the class, or access to the
code?-acb

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


How to find C source

2007-05-10 Thread castironpi
How do I get to the source for parser.suite()?

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


Re: How to find C source

2007-05-10 Thread castironpi
On May 10, 8:36 pm, "Gabriel Genellina" <[EMAIL PROTECTED]>
wrote:
> En Thu, 10 May 2007 21:47:39 -0300, <[EMAIL PROTECTED]> escribió:
>
> > How do I get to the source for parser.suite()?
>
> Are you looking for function parser_suite in parsermodule.c?
>
> --
> Gabriel Genellina

Looks like -it-.  Thank you for your time.

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


any() and all() shorthand

2008-01-07 Thread castironpi
any( iterab ) and all( iterab )

as shorthand for reduce( operator.or_, iterab ) and
reduce( operator.and_, iterab ).

What do you think?
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: any() and all() shorthand

2008-01-07 Thread castironpi
> You are too late, any and all are built-in into python 2.5

Hi, excellent.  Now how about something more generic, possibly:

[ x.y() for x or _next_ in c ]

where the context of _next_ is limited in complexity, and/or can only
occur in a generator?
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: any() and all() shorthand

2008-01-07 Thread castironpi
On Jan 7, 1:29 pm, "Guilherme Polo" <[EMAIL PROTECTED]> wrote:
> 2008/1/7, [EMAIL PROTECTED] <[EMAIL PROTECTED]>:
>
> > > You are too late, any and all are built-in into python 2.5
>
> > Hi, excellent.  Now how about something more generic, possibly:
>
> > [ x.y() for x or _next_ in c ]
>
> > where the context of _next_ is limited in complexity, and/or can only
> > occur in a generator?
>
> Would you care to explain what that syntax supposedly means ? By
> _next_ you mean something like the next method in generators ? _next_
> executes if x is false ? so whatever _next_ returns is named as x, so
> you can call x.y() ? I really didn't get your new syntax inside that
> list comprehension, neither its uses.
>

The idea is a shorthand for reduce.  Here, _next_ meant the next item
in the iterable c.

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


Re: any() and all() shorthand

2008-01-07 Thread castironpi
On Jan 7, 1:45 pm, [EMAIL PROTECTED] wrote:
> On Jan 7, 1:29 pm, "Guilherme Polo" <[EMAIL PROTECTED]> wrote:
>
> The idea is a shorthand for reduce.  Here, _next_ meant the next item
> in the iterable c.

'Only' is another known quantifier in logic: 'all and only'.  Any
(there exists) and all (for all) are too.  'Only' (and not others)
could be useful, from the theoretical standpoint.  But where?
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: any() and all() shorthand

2008-01-07 Thread castironpi
>print 'lookahead2(initial=3.14159, last=42)'
>for this, next in lookahead2([1,2,3,4,5],
>initial=3.14159, last=42):
>  print this, next

No, actually.  But my mistake.

[ a.b() or _previous_ for a in c ]

means

1 or 2 or 3 or 4 or 5
where c= [ 1, 2, 3, 4, 5].

The mistake: this was not a list comprehension; I wanted to reduce to
a single value.

It's equivalent to reduce( operator.or_, [ 1, 2, 3, 4, 5 ] ), but
disnecessitates lambdas for slightly more complex reductions.  But the
example is out of stock.  Do we have one?
-- 
http://mail.python.org/mailman/listinfo/python-list


removeall() in list

2008-01-11 Thread castironpi
Any ideas for a thread-safe list.removeall( X ): removing all
occurrences of X within list L, when L might be modified concurrently?

Sincerely,
Aaron
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: removeall() in list

2008-01-11 Thread castironpi
On Jan 11, 2:57 pm, Paul Rubin  wrote:
> [EMAIL PROTECTED] writes:
> > Any ideas for a thread-safe list.removeall( X ): removing all
> > occurrences of X within list L, when L might be modified concurrently?
>
> That way lies madness.  Do something sensible instead.  Put a lock
> around the list, or put all mutators for the list into a single
> thread, or whatever.  Don't do what you're describing.

This function just wants X out of the list.  It doesn't matter if this
happens before, during, or after something else; so long as it happens.
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: removeall() in list

2008-01-11 Thread castironpi
On Jan 11, 5:26 pm, Paul Rubin  wrote:
> [EMAIL PROTECTED] writes:
> > This function just wants X out of the list.  It doesn't matter if this
> > happens before, during, or after something else; so long as it happens.
>
> 2. Associate a lock with the list.  Anything wanting to access the
> list should acquire the lock, do its stuff, then release the lock.
> This gets confusing after a while.

To keep it generic, how about:

listA.op( insert, x )
listA.op( remove, x )
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: removeall() in list

2008-01-11 Thread castironpi
On Jan 11, 5:43 pm, [EMAIL PROTECTED] wrote:
> On Jan 11, 5:26 pm, Paul Rubin  wrote:
>
> > [EMAIL PROTECTED] writes:
> > > This function just wants X out of the list.  It doesn't matter if this
> > > happens before, during, or after something else; so long as it happens.
>
> > 2. Associate a lock with the list.  Anything wanting to access the
> > list should acquire the lock, do its stuff, then release the lock.
> > This gets confusing after a while.
>
> To keep it generic, how about:
>
> listA.op( insert, x )
> listA.op( remove, x )

However, in reality, your rock and hard place are:
listA.op( listA.insert, x )
listA.op( listA.remove, x )

or

listA.op( 'insert', x )
listA.op( 'remove', x )
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: removeall() in list

2008-01-11 Thread castironpi
On Jan 11, 5:51 pm, Paul Rubin  wrote:
> [EMAIL PROTECTED] writes:
> > listA.op( insert, x )
> > listA.op( remove, x )
>
> Sure, there are various ways you can make the code look uniform.  What
> gets messy is if you want to (say) operate on several lists at the
> same time, which means you need to hold multiple locks simultaneously,
> and some other thread is also trying to do the same thing.  If you
> acquire the locks in the wrong order, you can get a situation where
> both threads deadlock forever.
And:
> However, in reality, your rock and hard place are:
> listA.op( listA.insert, x )
> listA.op( listA.remove, x )
>
> or
>
> listA.op( 'insert', x )
> listA.op( 'remove', x )

For a standard library, you may not want to be exposing the potential
for deadlock-- not that users can't do that themselves.

lockerA.op( listA.extend, [ x ] )
lockerB.op( listB.reverse )
def thA():
gui.listbox.append( lockerA.op( listA.pop ) )
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: removeall() in list

2008-01-11 Thread castironpi
On Jan 11, 6:17 pm, [EMAIL PROTECTED] wrote:
> On Jan 11, 5:51 pm, Paul Rubin  wrote:
>
>
>
> > [EMAIL PROTECTED] writes:
> > > listA.op( insert, x )
> > > listA.op( remove, x )
>
> > Sure, there are various ways you can make the code look uniform.  What
> > gets messy is if you want to (say) operate on several lists at the
> > same time, which means you need to hold multiple locks simultaneously,
> > and some other thread is also trying to do the same thing.  If you
> > acquire the locks in the wrong order, you can get a situation where
> > both threads deadlock forever.
> And:
> > However, in reality, your rock and hard place are:
> > listA.op( listA.insert, x )
> > listA.op( listA.remove, x )
>
> > or
>
> > listA.op( 'insert', x )
> > listA.op( 'remove', x )
>
> For a standard library, you may not want to be exposing the potential
> for deadlock-- not that users can't do that themselves.
>
> lockerA.op( listA.extend, [ x ] )
> lockerB.op( listB.reverse )
> def thA():
> gui.listbox.append( lockerA.op( listA.pop ) )

Could you:

lockerA= Locker( listA, listB )
lockerA.op( listB.reverse )
lockerA.op( listA.pop )

Where lockerA ops acquire the locks on all its threads?
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: removeall() in list

2008-01-12 Thread castironpi
On Jan 11, 8:04 pm, Paul Rubin  wrote:
> [EMAIL PROTECTED] writes:
> > Could you:
>
> > lockerA= Locker( listA, listB )
> > lockerA.op( listB.reverse )
> > lockerA.op( listA.pop )
>
> > Where lockerA ops acquire the locks on all its threads?
>
> I don't understand that question.  The main thing to understand is
> that multi-threaded programming is complicated (especially if you're
> after high performance), and very difficult to get right without
> knowing exactly what you're doing.  The generally preferred approach
> in Python is to keep things simple at some performance cost.
>
> Your Locker approach above looks sort of reasonable if you can be
> absolutely sure that nothing else can mess with listA or listB
> concurrently with those locker operations.  Normally you would put
> listA and listB into a single object along with a lock, then do
> operations on that object.
>
> You might check the Python Cookbook for some specific recipes and
> sample code for this stuff.  If you've used Java, Python's general
> threading mechanisms are similar, but they are in the library rather
> than built into the language (i.e. there is no "synchronized"
> keyword, you have to do that locking explicitly).
>
> What is the actual application, if you don't mind saying?  Are you
> sure that you really need concurrency?

I'm writing an NxN observer pattern, mostly for my own personal
exploration.  Two threads -might- be calling 'Disconnect' at the same
time, and I can't even guarantee that the function runs properly.

for emelem in [ e for e in emlist if e.func is func ]:
try:
emlist.remove( emelem )
except ValueError:
pass
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: removeall() in list

2008-01-12 Thread castironpi
On Jan 12, 2:37 am, [EMAIL PROTECTED] wrote:
> On Jan 11, 8:04 pm, Paul Rubin  wrote:
>
>
>
> > [EMAIL PROTECTED] writes:
> > > Could you:
>
> > > lockerA= Locker( listA, listB )
> > > lockerA.op( listB.reverse )
> > > lockerA.op( listA.pop )
>
> > > Where lockerA ops acquire the locks on all its threads?
>
> > I don't understand that question.  The main thing to understand is
> > that multi-threaded programming is complicated (especially if you're
> > after high performance), and very difficult to get right without
> > knowing exactly what you're doing.  The generally preferred approach
> > in Python is to keep things simple at some performance cost.
>
> > Your Locker approach above looks sort of reasonable if you can be
> > absolutely sure that nothing else can mess with listA or listB
> > concurrently with those locker operations.  Normally you would put
> > listA and listB into a single object along with a lock, then do
> > operations on that object.
>
> > You might check the Python Cookbook for some specific recipes and
> > sample code for this stuff.  If you've used Java, Python's general
> > threading mechanisms are similar, but they are in the library rather
> > than built into the language (i.e. there is no "synchronized"
> > keyword, you have to do that locking explicitly).
>
> > What is the actual application, if you don't mind saying?  Are you
> > sure that you really need concurrency?
>
> I'm writing an NxN observer pattern, mostly for my own personal
> exploration.  Two threads -might- be calling 'Disconnect' at the same
> time, and I can't even guarantee that the function runs properly.
>
> for emelem in [ e for e in emlist if e.func is func ]:
> try:
> emlist.remove( emelem )
> except ValueError:
> pass

Though:

class A:
  @synch( 'lockA' )
  def Connect( self, target ): ...
  @synch( 'lockA' )
  def Disconnect( self, target ): ...

where decorator 'synch' is defined as:

take the 'lockA' attribute of the first function parameter, call
acquire(), call the function, call release(); has the right idea.
Another is on __init__, go through and wrap every member function in
self.lockA.acquire() and .release().
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: removeall() in list

2008-01-12 Thread castironpi
On Jan 12, 3:51 am, Fredrik Lundh <[EMAIL PROTECTED]> wrote:
> [EMAIL PROTECTED] wrote:
> > I'm writing an NxN observer pattern, mostly for my own personal
> > exploration.  Two threads -might- be calling 'Disconnect' at the same
> > time, and I can't even guarantee that the function runs properly.
>
> >for emelem in [ e for e in emlist if e.func is func ]:
> >try:
> >emlist.remove( emelem )
> >except ValueError:
> >pass
>
> so use a lock.  it's a whopping two lines of code:
>
> creation:
>
>  lock = threading.Lock()
>
> usage:
>
>  with lock:
>  for emelem in ...
>  ...
>
> more here:
>
>  http://effbot.org/zone/thread-synchronization.htm
>
> and btw, looping over a list to figure out what you want to remove from
> that list is a bit pointless.  better just create a new list:
>
>  with lock:
>  # get rid of all func instances
>  emlist = [e for e in emlist if e.func is not func]
>
> an alternative approach would be to replace emlist with a dictionary,
> keyed on func objects.  that'll let you remove all items associated with
> a given function with a single atomic operation:
>
>  del emdict[func]
>
> 

-> so use a lock.  it's a whopping two lines of code:
Yes.
1) I'm wondering what the specifics are on [Rubin]:

>>2. Associate a lock with the list.  Anything wanting to access the
list should acquire the lock, do its stuff, then release the lock.
This gets confusing after a while.<<

I considered these suggestions early on.  They apply often but I ruled
them out for reasons:

2) List is referenced by others; concurrent modifications may be going
on; can not replace it.  Can I make asynchronous modifications and
merge the changes, SCM-style?
3) Dictionary returns non-static order; order is important.  Create a
int-func tuple and sort dictionary results?  Perhaps.  That's sounding
pretty good.
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: removeall() in list

2008-01-12 Thread castironpi
On Jan 11, 5:26 pm, Paul Rubin  wrote:
> [EMAIL PROTECTED] writes:
>
> 1. Put a single thread in charge of the list, and communicate with it
> by message passing through Queues.  To get X out of the list, you'd
> send the mutator thread a message asking for removal.  The mutator
> thread would loop reading and processing messages from the queue,
> blocking when no requests are pending.  This is sort of the preferred
> Python style and is pretty simple to get correct, but if there are
> many such objects you can end up with more threads than you really
> want.

I've heard this called 'fire and forget'.  You can insure that
mutations are honored one-at-a-time  and in the order received.  How
do you make a -read- operation; wait for queued mutations, that is
lock for a turn on the queue?  Can you optionally read whatever the
state is, regardless of what's happened in the meantime?  Thing is,
one thread needs its -own- preceding operations completed before a
reading operation.
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: removeall() in list

2008-01-12 Thread castironpi
On Jan 12, 8:04 am, [EMAIL PROTECTED] wrote:
> On Jan 11, 5:26 pm, Paul Rubin  wrote:
>
> > [EMAIL PROTECTED] writes:
>
> > 1. Put a single thread in charge of the list, and communicate with it
> > by message passing through Queues.  To get X out of the list, you'd
> > send the mutator thread a message asking for removal.  The mutator
> > thread would loop reading and processing messages from the queue,
> > blocking when no requests are pending.  This is sort of the preferred
> > Python style and is pretty simple to get correct, but if there are
> > many such objects you can end up with more threads than you really
> > want.
>
> I've heard this called 'fire and forget'.  You can insure that
> mutations are honored one-at-a-time  and in the order received.  How
> do you make a -read- operation; wait for queued mutations, that is
> lock for a turn on the queue?  Can you optionally read whatever the
> state is, regardless of what's happened in the meantime?  Thing is,
> one thread needs its -own- preceding operations completed before a
> reading operation.

Brainstorm.

First, isolation of problem:  Terminates at 2000 or so, on my
computer.

import thread
import time
import random
counter= 0
def simplecounter():
global counter
ret= counter
counter+= 1
time.sleep( random.uniform( 0, .001 ) )
return counter
glist= []
def th3():
while 1:
ret= simplecounter()
glist.append( ret )
print ret,
assert glist== range( 1, len( glist )+1 )
thread.start_new_thread( th3, () )
time.sleep(1)
thread.start_new_thread( th3, () )
time.sleep( 1000 )

Second, the thoughts:  'with a.callbacklock():' looks best currently.

'''multithreading ideas:
1.  Put a single thread in charge
a.k.a. fire and forget.
- Lots of extra threads
+ But most are blocking most of the time
+ honored one-at-a-time, and in order received
+ ...optionally including read-access, blocking on
to get return values
a. synchronous callbacks, for read-access
+ multi-step, user-definitionized operations
- one consumer can hang an entire object
i.  with a.callbacklock():?
+ only call acquire() from curr. thread, enqueue
lock obj., released from producer thread "soon"
using message-queue semantics
b. mini-transaction, guarantees all and only
consumer's ops occur in succession
- can't do anything amidst an indivdual locking
- no multi-step ops
2.  Lock mutation and/or all operations
a. Locker.op
b. with Locker.withop
- In Python, other programmers always have access
to your data; nothing guarantees they'll use "with locker"
+ User-definitioning quite easy
3.  @mutation decorator
def mutation( func ):
def newfunc( self, *a, **k ):
self.lock.acquire()
func( *a, **k )
self.lock.release()
4.  List-only solution:
Use a dictionary, map item to its index.
To retrieve, sort on value, not key
'''
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: removeall() in list

2008-01-12 Thread castironpi
On Jan 12, 11:22 am, Rhamphoryncus <[EMAIL PROTECTED]> wrote:
> On Jan 12, 1:37 am, [EMAIL PROTECTED] wrote:
>
>
>
> > On Jan 11, 8:04 pm, Paul Rubin  wrote:
>
> > > [EMAIL PROTECTED] writes:
> > > > Could you:
>
> > > > lockerA= Locker( listA, listB )
> > > > lockerA.op( listB.reverse )
> > > > lockerA.op( listA.pop )
>
> > > > Where lockerA ops acquire the locks on all its threads?
>
> > > I don't understand that question.  The main thing to understand is
> > > that multi-threaded programming is complicated (especially if you're
> > > after high performance), and very difficult to get right without
> > > knowing exactly what you're doing.  The generally preferred approach
> > > in Python is to keep things simple at some performance cost.
>
> > > Your Locker approach above looks sort of reasonable if you can be
> > > absolutely sure that nothing else can mess with listA or listB
> > > concurrently with those locker operations.  Normally you would put
> > > listA and listB into a single object along with a lock, then do
> > > operations on that object.
>
> > > You might check the Python Cookbook for some specific recipes and
> > > sample code for this stuff.  If you've used Java, Python's general
> > > threading mechanisms are similar, but they are in the library rather
> > > than built into the language (i.e. there is no "synchronized"
> > > keyword, you have to do that locking explicitly).
>
> > > What is the actual application, if you don't mind saying?  Are you
> > > sure that you really need concurrency?
>
> > I'm writing an NxN observer pattern, mostly for my own personal
> > exploration.  Two threads -might- be calling 'Disconnect' at the same
> > time, and I can't even guarantee that the function runs properly.
>
> > for emelem in [ e for e in emlist if e.func is func ]:
> > try:
> > emlist.remove( emelem )
> > except ValueError:
> > pass
>
> Is there a reason you're using a list, rather than a dict?  Note that
> each call to list.remove() is O(n), whereas deleting a key from a dict
> is O(1).

4.  List-only solution:
Use a dictionary, map item to its index.
To retrieve, sort on value, not key

Sure: you need to maintain order of the things you're guarding.  I
need this to happen first, then this second.  My application is event
callbacks, that you register in a certain order; think GUIs and the
Gamma et al. Chain of Responsibility pattern.  This is a circumstance;
in a lot of applications order doesn't matter and you can use a hash.

But even so, you still can't check membership during concurrent
operation.  In other words, to remove all elements E such that E.func
is FuncF, the simple
   for e in setofe[:]:
  if e.func is not FuncF: continue
  setofe.remove( e )
still doesn't work.
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: removeall() in list

2008-01-12 Thread castironpi
On Jan 12, 12:26 pm, Paul Rubin  wrote:
> [EMAIL PROTECTED] writes:
> > 2) List is referenced by others; concurrent modifications may be going
> > on; can not replace it.  Can I make asynchronous modifications and
> > merge the changes, SCM-style?
>
> Nothing else should have direct access to the list.
>
> > 3) Dictionary returns non-static order; order is important.  Create a
> > int-func tuple and sort dictionary results?  Perhaps.  That's sounding
> > pretty good.
>
> If the list is not too long, that is reasonable.  If it -is- long, the
> remove operations can be slow, but that's ok if there's not too many.
> If the list is long -and- there's a lot of adds/removes, maybe you
> want something like an AVL tree.

But items[ item ]= index is the best I can do in Python.
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: removeall() in list

2008-01-12 Thread castironpi
On Jan 12, 1:03 pm, Paul Rubin  wrote:
> [EMAIL PROTECTED] writes:
> > > Nothing else should have direct access to the list.
> > Impossible to guarantee in Python.  If you do, the reference to you does.
>
> Well, ok.  Nothing else should USE that access.

Ah, very agreed.  Access directly at your own risk.

Will you engage with me over e-mail to discuss the Locker
implementation I'm developing?  Aaron
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: removeall() in list

2008-01-12 Thread castironpi
On Jan 12, 12:26 pm, Paul Rubin  wrote:
> [EMAIL PROTECTED] writes:
> > 2) List is referenced by others; concurrent modifications may be going
> > on; can not replace it.  Can I make asynchronous modifications and
> > merge the changes, SCM-style?
>
> Nothing else should have direct access to the list.
Impossible to guarantee in Python.  If you do, the reference to you
does.
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: removeall() in list

2008-01-12 Thread castironpi
On Jan 12, 1:28 pm, Paul Rubin  wrote:
> [EMAIL PROTECTED] writes:
> > Will you engage with me over e-mail to discuss the Locker
> > implementation I'm developing?  Aaron
>
> I really can't, sorry.  I'm finding it hard enough to follow over the
> newsgroup.  If you just have a single one of these lists, it's
> probably simplest to do what Frederik Lundh suggested.  The other
> stuff we've discussed is mostly about how to organize having a lot of
> them.

Highlight from the working solution:

def onfulfill( self, func, *args, **kwargs ):
'''decorator launches its function in separate thread
upon completion of func.  todo: cancel and reference
check.'''
locfunc= Ref()
def honorth():
result= self.op( func, *args, **kwargs )
locfunc.val( result )
def prefulfill( func ):
locfunc.val= func
th= threading.Thread( target= honorth )
th.start()
return prefulfill
-- 
http://mail.python.org/mailman/listinfo/python-list


Function wrappers

2008-01-23 Thread castironpi
def f( callback, *bar, **bkwar ):
def preg ( callfore, *far, **fkwar ):
return g( callback, callfore, bar, bkwar, far, fkwar )
return preg

Does anyone see a way to rewrite this, perhaps along the lines of
partial( partial, partial )?  Ok to modify 'g' call.
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: handling asynchronous callbacks from c++ in a python script

2008-01-23 Thread castironpi
On Jan 23, 5:46 pm, Tim Spens <[EMAIL PROTECTED]> wrote:
> I have a c++ program running that has boost python hooks for the c++ api.
> I'm running a python client that makes calls into the c++ api.  The problem 
> is there are c++
> asynchronous callbacks that need to pass information to the python client.  
> What I was hoping to
> do is call a python function from c++ that resides in the running "main()" 
> python client
> while in the c++ callback handlers.
> Is this possible and if you can point me to an example or documentation on 
> how to do this it would be much appreciated?
>
> NOTE: I've been asking on the c++-sig mailing list about this and David 
> Abrahams (very well versed in boost python) said:
> "I'm not an expert on asynchronous python and this is really not a 
> Boost.Python question. I suggest you ask on the regular Python mailing list.  
> Sorry."

PyObject_CallFunction.

You might need to pass -out- a Thread instance, initialized with your
callback, then just call start, or just the pointer to start.

import threading
import time
def f( func )
func()

def g( caption ):
for _ in range( 1000 ):
 print '%s\n'% caption,
 time.sleep( .01 )

th= threading.Thread( target= g, args= ( 'what' ) )
f( th.start )
time.sleep( 10 )
-- 
http://mail.python.org/mailman/listinfo/python-list


functools possibilities

2008-02-01 Thread castironpi
1. functools.partialpre: partialpre( f, x, y )( z )-> f( z, x, y )
2. functools.pare: pare( f, 1 )( x, y )-> f( y )
3. functools.parepre: parepre( f, 1 )( x, y )-> f( x )
4. functools.calling_default: calling_default( f, a, DefaultA, b )->
f( a, , b )
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: functools possibilities

2008-02-02 Thread castironpi
On Feb 2, 12:13 pm, Steven Bethard <[EMAIL PROTECTED]> wrote:
> [EMAIL PROTECTED] wrote:
> > 1. functools.partialpre: partialpre( f, x, y )( z )-> f( z, x, y )
> > 2. functools.pare: pare( f, 1 )( x, y )-> f( y )
> > 3. functools.parepre: parepre( f, 1 )( x, y )-> f( x )
> > 4. functools.calling_default: calling_default( f, a, DefaultA, b )->
> > f( a, , b )
>
> There are lots of possibilities for functools.  If you actually want
> anything added, you'll need to back up your suggestions with use cases.
>   Unless something is currently in widespread use in existing Python
> code, it's unlikely to be included in the standard library.
>
> STeVe

Feel, hunch, affinity, and instinct are acceptable criteria for
judging.  Otherwise, you're someone, and you have statistics on what
constitutes widespread use; I can tell what's good as well as you.

Guido says, "Programmer time is important."  If it saves time, on the
whole, writing plus learning plus reading, it's likely to be included.

urllib was not "in widespread use" prior to inclusion, but they did,
was it?

"> There are lots of possibilities for functools."

So many, in fact, they're thinking of adding a separate "wrappertools"
module.

Monarchies are effective on small scales.  Is Python destined for the
small-time?

I've seen these suggestions before; they did not receive impartial
address.

Functools and the candidate "wrappertools" should be populated fairly
liberally.
-- 
http://mail.python.org/mailman/listinfo/python-list


future multi-threading for-loops

2008-02-04 Thread castironpi
Some iterables and control loops can be multithreaded.  Worries that
it takes a syntax change.

for X in A:
def f( x ):
normal suite( x )
start_new_thread( target= f, args= ( X, ) )

Perhaps a control-flow wrapper, or method on iterable.

@parallel
for X in A:
normal suite( X )

for X in parallel( A ):
normal suite( X )

Discussion presued about multi-core systems.  Allow user certain
control over what runs on multi-core.  Clearly, not generally
applicable.  -- But, from __future__ import does change syntax.
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: future multi-threading for-loops

2008-02-04 Thread castironpi
On Feb 4, 9:22 pm, [EMAIL PROTECTED] wrote:
> Some iterables and control loops can be multithreaded.  Worries that
> it takes a syntax change.
>
> for X in A:
>     def f( x ):
>         normal suite( x )
>     start_new_thread( target= f, args= ( X, ) )
>
> Perhaps a control-flow wrapper, or method on iterable.
>
> @parallel
> for X in A:
>     normal suite( X )
>
> for X in parallel( A ):
>     normal suite( X )
>
> Discussion presued about multi-core systems.  Allow user certain
> control over what runs on multi-core.  Clearly, not generally
> applicable.  -- But, from __future__ import does change syntax.

Some timing stats: On Windows XP, Python 3.0a2.

[timing code, 10,000 calls]
[ f( X ) ]: 0.0210021106034
[ start_new_thread( f, X ) ]: 1.15759908033
[ Thread( f, X ).start() ]: 1.85400099733
[ Thread( f, X ).start and .join() ]: 1.93716743329

Are threads an OS bottleneck?
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: future multi-threading for-loops

2008-02-04 Thread castironpi
On Feb 5, 12:26 am, Gabriel Genellina <[EMAIL PROTECTED]> wrote:
> On 5 feb, 03:46, [EMAIL PROTECTED] wrote:
>
> > Some timing stats: On Windows XP, Python 3.0a2.
>
> > [timing code, 10,000 calls]
> > [ f( X ) ]: 0.0210021106034
> > [ start_new_thread( f, X ) ]: 1.15759908033
> > [ Thread( f, X ).start() ]: 1.85400099733
> > [ Thread( f, X ).start and .join() ]: 1.93716743329
>
> > Are threads an OS bottleneck?
>
> I don't understand your threading issues, but I would not use 3.0a2
> for benchmarking anything (except benchmarking Python 3.0 itself).
> AFAIK the developers are first trying to get it right and stable;
> speed issues will be addressed later.
>
> --
> Gabriel Genellina

Multi-threaded control flow is a worthwhile priority.
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: future multi-threading for-loops

2008-02-05 Thread castironpi
On Feb 5, 1:21 am, Marc 'BlackJack' Rintsch <[EMAIL PROTECTED]> wrote:
> On Mon, 04 Feb 2008 19:22:29 -0800, castironpi wrote:
> > Some iterables and control loops can be multithreaded.  Worries that
> > it takes a syntax change.
>
> > for X in A:
> >     def f( x ):
> >         normal suite( x )
> >     start_new_thread( target= f, args= ( X, ) )
>
> > Perhaps a control-flow wrapper, or method on iterable.
>
> > @parallel
> > for X in A:
> >     normal suite( X )
>
> > for X in parallel( A ):
> >     normal suite( X )
>
> > Discussion presued about multi-core systems.  Allow user certain
> > control over what runs on multi-core.  Clearly, not generally
> > applicable.  -- But, from __future__ import does change syntax.
>
> Why not simply writing a function?
>
> def execute_parallel(f, A):
>     for args in A:
>         start_new_thread(target=f, args=args)
>
> def f(x):
>     normal_suit(x)
>
> parallel(f, A)
>
> Ciao,
>         Marc 'BlackJack' Rintsch- Hide quoted text -
>
> - Show quoted text -

Are code blocks first-class objects, without customizing syntax?

from functools import partial
from threading import Thread
from random import uniform

A= range(10)

@partial( partial, partial )
def execute_parallel( f, A ):
for args in A:
Thread( target= f, args= ( args, ) ).start()

from time import sleep

@execute_parallel( A= A )
def f(x):
sleep( uniform( 0, .2 ) )
print(x)

[Windows XP threading timing code, 10,000 calls]: [ allocate,
CreateEvent, CreateThread, WaitForMultipleObjects ]: 0.887416 secs
-- 
http://mail.python.org/mailman/listinfo/python-list


Code block function syntax, anonymous functions decorator

2008-02-06 Thread castironpi
def run3( block ):
   for _ in range( 3 ):
  block()

run3():
   normal_suite()

Introduces new syntax; arbitrary functions can follow 'colon'.

Maintains readability, meaning is consistent.

Equivalent to:

def run3( block ):
   for _ in range( 3 ):
  block()

@run3
def anonfunc():
   normal_suite()

Simplification in cases in which decorators are use often.
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Code block function syntax, anonymous functions decorator

2008-02-06 Thread castironpi
On Feb 6, 5:45 pm, Jean-Paul Calderone <[EMAIL PROTECTED]> wrote:
> On Wed, 06 Feb 2008 23:59:27 +0100, "Diez B. Roggisch" <[EMAIL PROTECTED]> 
> wrote:
>
>
>
>
>
> >[EMAIL PROTECTED] schrieb:
> >> def run3( block ):
> >>    for _ in range( 3 ):
> >>       block()
>
> >> run3():
> >>    normal_suite()
>
> >> Introduces new syntax; arbitrary functions can follow 'colon'.
>
> >> Maintains readability, meaning is consistent.
>
> >> Equivalent to:
>
> >> def run3( block ):
> >>    for _ in range( 3 ):
> >>       block()
>
> >> @run3
> >> def anonfunc():
> >>    normal_suite()
>
> >> Simplification in cases in which decorators are use often.
>
> >This is non-sensical - how do you invoke anonfunc? They would all bind
> >to the same name, run3. Or to no name as all, as your "spec" lacks that.
>
> As he said, the decorator version is the _equivalent_ to the syntax he
> was proposing.  The point isn't to decorate the function, so perhaps he
> shouldn't have used decorator syntax, but instead:
>
>     def anonfunc():
>         normal_suite()
>     run3(anonfunc)
>     del anonfunc
>
> So it's not non-sensical.  It's a request for a piece of syntax.
>
>
>
> >Besides, it's butt-ugly IMHO. But taste comes after proper definition...
>
> It's properly defined.  Not that I'm endorsing this or anything.  I'd
> rather not see half-assed syntax proposals at all, even if they're super
> great (and some of the syntax that's made it into Python is much worse
> than this).
>
> Jean-Paul- Hide quoted text -
>
> - Show quoted text -

Yes.  @run3( anonfunc ) runs -in-place-.  Jean-Paul's was a closer
equivalent.

It's used for a piece of code that won't get called like with
statements.
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Code block function syntax, anonymous functions decorator

2008-02-06 Thread castironpi
On Feb 6, 8:10 pm, [EMAIL PROTECTED] wrote:
> On Feb 6, 5:45 pm, Jean-Paul Calderone <[EMAIL PROTECTED]> wrote:
>
>
>
>
>
> > On Wed, 06 Feb 2008 23:59:27 +0100, "Diez B. Roggisch" <[EMAIL PROTECTED]> 
> > wrote:
>
> > >[EMAIL PROTECTED] schrieb:
> > >> def run3( block ):
> > >>    for _ in range( 3 ):
> > >>       block()
>
> > >> run3():
> > >>    normal_suite()
>
> > >> Introduces new syntax; arbitrary functions can follow 'colon'.
>
> > >> Maintains readability, meaning is consistent.
>
> > >> Equivalent to:
>
> > >> def run3( block ):
> > >>    for _ in range( 3 ):
> > >>       block()
>
> > >> @run3
> > >> def anonfunc():
> > >>    normal_suite()
>
> > >> Simplification in cases in which decorators are use often.
>
> > >This is non-sensical - how do you invoke anonfunc? They would all bind
> > >to the same name, run3. Or to no name as all, as your "spec" lacks that.
>
> > As he said, the decorator version is the _equivalent_ to the syntax he
> > was proposing.  The point isn't to decorate the function, so perhaps he
> > shouldn't have used decorator syntax, but instead:
>
> >     def anonfunc():
> >         normal_suite()
> >     run3(anonfunc)
> >     del anonfunc
>
> > So it's not non-sensical.  It's a request for a piece of syntax.
>
> > >Besides, it's butt-ugly IMHO. But taste comes after proper definition...
>
> > It's properly defined.  Not that I'm endorsing this or anything.  I'd
> > rather not see half-assed syntax proposals at all, even if they're super
> > great (and some of the syntax that's made it into Python is much worse
> > than this).
>
> > Jean-Paul- Hide quoted text -
>
> > - Show quoted text -
>
> Yes. [EMAIL PROTECTED]( anonfunc ) runs -in-place-.  Jean-Paul's was a closer
> equivalent.
>
> It's used for a piece of code that won't get called like with
> statements.- Hide quoted text -
>
> - Show quoted text -

I know of two examples.  Thread starting, and event binding in a
framework GUI.  Of course, not too much farther along, for X in A as
C, storing the code block and/or for loop "object" as a object in "C",
and likewise for if.  These met with disfavor in another place.

start_new_thread( codeblock as target, args= () ):
   normal_suite()

bind( "", codeblock as A ):
   handle_event( event )

But where does 'event' come from?

mybind( "", codeblock as A )( event ):
   handle_event( event )

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


Re: Code block function syntax, anonymous functions decorator

2008-02-06 Thread castironpi
On Feb 6, 4:59 pm, "Diez B. Roggisch" <[EMAIL PROTECTED]> wrote:
> [EMAIL PROTECTED] schrieb:
>
>
>
>
>
> > def run3( block ):
> >    for _ in range( 3 ):
> >       block()
>
> > run3():
> >    normal_suite()
>
> > Introduces new syntax; arbitrary functions can follow 'colon'.
>
> > Maintains readability, meaning is consistent.
>
> > Equivalent to:
>
> > def run3( block ):
> >    for _ in range( 3 ):
> >       block()
>
> > @run3
> > def anonfunc():
> >    normal_suite()
>
> > Simplification in cases in which decorators are use often.
>
> This is non-sensical - how do you invoke anonfunc? They would all bind
> to the same name, run3. Or to no name as all, as your "spec" lacks that.
>
> Besides, it's butt-ugly IMHO. But taste comes after proper definition...
>
> Diez- Hide quoted text -
>
> - Show quoted text -

The fallacy here is that @run3 as described is not a typical
decorator, such one that returns a function.  It is called at
"definition"-time, that is, when the function definition is executed,
and executes the code.

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


Re: Code block function syntax, anonymous functions decorator

2008-02-07 Thread castironpi
On Feb 7, 2:48 am, "Diez B. Roggisch" <[EMAIL PROTECTED]> wrote:
> Jean-Paul Calderone schrieb:
>
>
>
>
>
> > On Wed, 06 Feb 2008 23:59:27 +0100, "Diez B. Roggisch"
> > <[EMAIL PROTECTED]> wrote:
> >> [EMAIL PROTECTED] schrieb:
> >>> def run3( block ):
> >>>    for _ in range( 3 ):
> >>>       block()
>
> >>> run3():
> >>>    normal_suite()
>
> >>> Introduces new syntax; arbitrary functions can follow 'colon'.
>
> >>> Maintains readability, meaning is consistent.
>
> >>> Equivalent to:
>
> >>> def run3( block ):
> >>>    for _ in range( 3 ):
> >>>       block()
>
> >>> @run3
> >>> def anonfunc():
> >>>    normal_suite()
>
> >>> Simplification in cases in which decorators are use often.
>
> >> This is non-sensical - how do you invoke anonfunc? They would all bind
> >> to the same name, run3. Or to no name as all, as your "spec" lacks that.
>
> > As he said, the decorator version is the _equivalent_ to the syntax he
> > was proposing.  The point isn't to decorate the function, so perhaps he
> > shouldn't have used decorator syntax, but instead:
>
> >    def anonfunc():
> >        normal_suite()
> >    run3(anonfunc)
> >    del anonfunc
>
> > So it's not non-sensical.  It's a request for a piece of syntax.
>
> >> Besides, it's butt-ugly IMHO. But taste comes after proper definition...
>
> > It's properly defined.  Not that I'm endorsing this or anything.  I'd
> > rather not see half-assed syntax proposals at all, even if they're super
> > great (and some of the syntax that's made it into Python is much worse
> > than this).
>
> Yeah, I missed somehow that the decorator doesn't actually return anything.
>
> Diez- Hide quoted text -
>
> - Show quoted text -

Even to myself, the idea is not sounding too bad.  You could require
that the function have a 'codeblock' argument, (but that goes against
pythonic "self" style customs), or pass it as an implicit first
parameter.  Hybrid context manager and function.
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Code block function syntax, anonymous functions decorator

2008-02-07 Thread castironpi
On Feb 7, 7:13 pm, [EMAIL PROTECTED] wrote:
> On Feb 7, 2:48 am, "Diez B. Roggisch" <[EMAIL PROTECTED]> wrote:
>
>
>
>
>
> > Jean-Paul Calderone schrieb:
>
> > > On Wed, 06 Feb 2008 23:59:27 +0100, "Diez B. Roggisch"
> > > <[EMAIL PROTECTED]> wrote:
> > >> [EMAIL PROTECTED] schrieb:
> > >>> def run3( block ):
> > >>>    for _ in range( 3 ):
> > >>>       block()
>
> > >>> run3():
> > >>>    normal_suite()
>
> > >>> Introduces new syntax; arbitrary functions can follow 'colon'.
>
> > >>> Maintains readability, meaning is consistent.
>
> > >>> Equivalent to:
>
> > >>> def run3( block ):
> > >>>    for _ in range( 3 ):
> > >>>       block()
>
> > >>> @run3
> > >>> def anonfunc():
> > >>>    normal_suite()
>
> > >>> Simplification in cases in which decorators are use often.
>
> > >> This is non-sensical - how do you invoke anonfunc? They would all bind
> > >> to the same name, run3. Or to no name as all, as your "spec" lacks that.
>
> > > As he said, the decorator version is the _equivalent_ to the syntax he
> > > was proposing.  The point isn't to decorate the function, so perhaps he
> > > shouldn't have used decorator syntax, but instead:
>
> > >    def anonfunc():
> > >        normal_suite()
> > >    run3(anonfunc)
> > >    del anonfunc
>
> > > So it's not non-sensical.  It's a request for a piece of syntax.
>
> > >> Besides, it's butt-ugly IMHO. But taste comes after proper definition...
>
> > > It's properly defined.  Not that I'm endorsing this or anything.  I'd
> > > rather not see half-assed syntax proposals at all, even if they're super
> > > great (and some of the syntax that's made it into Python is much worse
> > > than this).
>
> > Yeah, I missed somehow that the decorator doesn't actually return anything.
>
> > Diez- Hide quoted text -
>
> > - Show quoted text -
>
> Even to myself, the idea is not sounding too bad.  You could require
> that the function have a 'codeblock' argument, (but that goes against
> pythonic "self" style customs), or pass it as an implicit first
> parameter.  Hybrid context manager and function.- Hide quoted text -
>
> - Show quoted text -


Sometimes, it's more appropriate to write

@call
def f():
   normal_suite()

than

def f():
   normal_suite()
f().

It's clearer to the eye and reader, and truer to the meaning of the
code.  From reading the docs, it's pretty clear that it's not what the
author meant for decorators.  So, even though it's good and practical,
as well as Pythonic, it doesn't get in.  However, from the writing, he
or she is most reasonable himself and practical.  Or herself.  How
does one reconcile these observations?  Not just anyone can invent a
computer language, but we all know what happens later to emperors and
the like.  Will they listen to reason?  Depends.  Is Python someone's
baby to raise, as court says, "any way he or she wants?"  Does he or
she want people to like it?  Does he or she only want to please
himself?  Or herself?  Anyone for a controlling share, hostile
takeover, or court injunction or protection order?  Gollum was a
Hobbit too once

Maybe someone is getting back at "us", "the rest", "the world", for
ideas he or she had early on that weren't accepted.  Does he or she
even read these things down here?  What factors in to his or her
decision?  If someone "on top" doesn't like an idea, is it a strong
indicator that it's bad?  That it's good?  If myself, I don't
understand, it's one thing.  If "they" are wrong, it's another.  If he
or she is just shy, and won't get behind something clean and cool,
only without first making a friend, then maybe, after all, I can
relate.

If it's bad, they should be able to tell me why.  Is it?  Can they?
Will they?

Though don't forget: there's only so much that can fit in a language.
There are only so many letters, so many punctuation marks (many of
which are in use), and syntax is pricey-pricey.  Clearly, extensions
should be general.

$ to execute, .. to download, ## to get the size of a list, etc., cf.
Perl, are one side of this coin.  Math and logic are the other: it is
so general, in fact, that it's outside cause altogether.

How well you speak English is well-defined.  How well you speak Python
is too.  Your choice of custom-syntax languages, though, may not be.
Once a syntax is defined in a language, is the rest of the program in
it?  Say it like this: There's only so many specifics that -ought- to
go into any given line of code.

If it's not clear by now, I agree with many of the constraints that
"our someone" has imposed on Python-- or, more accurately, demanded of
us, its speakers, requests they have made for the future of it.  I've
had some proposals rejected.  They didn't fit in with-- what?  The
powers that be are not impartial; they are human, prejudiced emotions,
battered childhoods, and all.

I like to think that a good idea would get in to a language I create.
Does it?  No holds barred, no strings attached, no further
prerequisites, only that its good?  Perhaps "up there", prioritie

Re: Code block function syntax, anonymous functions decorator

2008-02-08 Thread castironpi
On Feb 8, 1:08 am, Arnaud Delobelle <[EMAIL PROTECTED]> wrote:
> On Feb 8, 6:50 am, [EMAIL PROTECTED] wrote:
>
>
>
>
>
> > Sometimes, it's more appropriate to write
>
> > @call
> > def f():
> >    normal_suite()
>
> > than
>
> > def f():
> >    normal_suite()
> > f().
>
> > It's clearer to the eye and reader, and truer to the meaning of the
> > code.  From reading the docs, it's pretty clear that it's not what the
> > author meant for decorators.  So, even though it's good and practical,
> > as well as Pythonic, it doesn't get in.
>
> As I remarked in a recent post, it's already almost in, but it's
> called '@apply' (I don't know what your @call returns):
>
> @apply
> def the_answer(x=6):
>     return x*(x+1)
>
> print the_answer
>
> :-)
>
> --
> Arnaud- Hide quoted text -
>
> - Show quoted text -

Python 3.0a2 (r30a2:59405M, Dec  7 2007
>>> apply
Traceback (most recent call last):
  File "", line 1, in 
NameError: name 'apply' is not defined

I understand it's "To be removed" [1].  These proposals are similar
[2] [3].

What is not in is a full-fledged anonymous function.  That is a code
block.

I personally have been focussing on multi-threading, looking to the
future, so forgive the one-sided example.

@start_new_thread
def anonA():
normal_suite()


Similar.
start_new_thread():
normal_suite()

Or, if you're using other arguments, or the function does not take a
function as its first parameter, rearrange parameters with a standard
function.

Makes it hard to join or call if you ever need it more than once.  I
don't believe that assigning the return to a name fits in with the
grand scheme.

Yes:

def convenientstart( func ):
Thread( target= func ).start()

@convenientstart
def anonA():
normal_suite()

Yes:

def convenientstart( func ):
th= Thread( target= func, args= ( th, ) )
th.start()
return th#, but where does it go?

convenientstart():
normal_suite()

No:

def convenientstart( func ):
Thread( target= func ).start()

convenientstart() as thA:
normal_suite()

I dream:

def convenientstart( func, A ):
th= Thread( target= func, args= ( th, A ) )
th.start()
return th#, but where does it go?

th= convenientstart( A ):
normal_suite()

Cf. bound object instance calls: code block is the applicable "self".

Another day, perhaps.

I am extremely open to feedback.  Asking to brainstorm.  Thoughts
welcome.  Any?


[1] http://www.python.org/dev/peps/pep-3100/ .
[2] http://mail.python.org/pipermail/python-ideas/2007-October/001083.html
[3] http://mail.python.org/pipermail/python-ideas/2007-October/001086.html
-- 
http://mail.python.org/mailman/listinfo/python-list


C function in a Python context

2008-02-09 Thread castironpi
To write quick C things that Python won't do up to speed.  So it's got
a redundancy.

import ext
extA= ext.Ext()
extA[ 'enumfactors' ]= r"""
int enumfactors( int a, const char* sep ) {
int ret= 0, i;
for( i= 1; i<= a; i++ ) {
if( a% i== 0 ) {
ret+= 1;
if( i> 1 ) {
printf( "%s", sep );
}
printf( "%i", i );
}
}
printf( "\n" );
return ret;
}
""", ("i","i","s")

factorsn= extA.enumfactors( 209677683, ', ' )
print( "%i factors total."% factorsn )

import sys
sys.exit()


1, 3, 23, 69, 131, 393, 3013, 9039, 23197, 69591, 533531, 1600593,
3038807, 9116
421, 69892561, 209677683
16 factors total.
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: C function in a Python context

2008-02-09 Thread castironpi
On Feb 9, 1:48 pm, [EMAIL PROTECTED] wrote:
> To write quick C things that Python won't do up to speed.  So it's got
> a redundancy.
>
> import ext
> extA= ext.Ext()
> extA[ 'enumfactors' ]= r"""
>     int enumfactors( int a, const char* sep ) {
>         int ret= 0, i;
>         for( i= 1; i<= a; i++ ) {
>             if( a% i== 0 ) {
>                 ret+= 1;
>                 if( i> 1 ) {
>                     printf( "%s", sep );
>                 }
>                 printf( "%i", i );
>             }
>         }
>         printf( "\n" );
>         return ret;
>     }
>     """, ("i","i","s")
>
> factorsn= extA.enumfactors( 209677683, ', ' )
> print( "%i factors total."% factorsn )
>
> import sys
> sys.exit()
>
> 1, 3, 23, 69, 131, 393, 3013, 9039, 23197, 69591, 533531, 1600593,
> 3038807, 9116
> 421, 69892561, 209677683
> 16 factors total.

'''Prototype implementation, slightly rigid.  If anyone knows how to
compile and link without intermediate object file, and from a string,
memory, or stdin, let me know.  Change first four lines.  If you are
not using gcc, look at regenpyd().'''

compilercommand='c:/programs/mingw/bin/gcc'
pythondll=  'python30'
pythonpathinclude=  'c:/programs/python/include'
pythonpathlibs= 'c:/programs/python/libs'

class Ext:
strctypes= { 'i': 'int', 's': 'const char*' }
class ExtElem:
def __init__( self, name, code, types ):
self.name, self.code= name, code
self.types= types
def __init__( self ):
self.__dict__[ 'exts' ]= []
def regenc( self ):
extcode= open( 'extcode.c', 'w' )
wr= extcode.write
wr( '#include <%s'% pythonpathinclude )
wr( '/Python.h>\n\n' )
for ext in self.exts:
wr( ext.code )
wr( '\n' )
for ext in self.exts:
wr( 'static PyObject *\n' )
wr( 'extcode_%s'% ext.name )
wr( '(PyObject *self, ' )
wr( 'PyObject *args) {\n' )
wr( '\t%s result;\n'%
Ext.strctypes[ext.types[0]] )
for i, type in enumerate( ext.types[1:] ):
wr( '\t%s arg%i;\n'%
( Ext.strctypes[type], i ) )
wr( '\tPyArg_ParseTuple(args, "' )
wr( ''.join( ext.types[1:] ) )
wr( '"' )
for i, type in enumerate( ext.types[1:] ):
wr( ', &arg%i'% i )
wr( ' );\n' )
wr( '\tresult= %s( '% ext.name )
wr( ', '.join( [ 'arg%i'% i for i
in range( len( ext.types[1:] ) ) ] ) )
wr( ' );\n' )
wr( '\treturn Py_BuildValue' )
wr( '( "%s", result );\n'% ext.types[0] )
wr( '}\n\n' )
wr( 'static PyMethodDef ExtcodeMethods[] = {\n' )
for ext in self.exts:
wr( '\t{ "%s", extcode_%s, '%
( ext.name, ext.name ) )
wr( 'METH_VARARGS, "" },\n' )
wr( '\t{NULL, NULL, 0, NULL}\n' )
wr( '};\n\n' )
wr( 'PyMODINIT_FUNC\n' )
wr( 'initextcode(void) {\n' )
wr( '\t(void) Py_InitModule' )
wr( '("extcode", ExtcodeMethods);\n' )
wr( '}\n\n' )
extcode.close()
def regenpyd( self ):
import os, os.path
if os.path.exists( 'extcode.pyd' ):
os.remove( 'extcode.pyd' )
import subprocess
retcompile= subprocess.call(
'%s extcode.c -c -I%s'%
( compilercommand, pythonpathinclude ) )
assert not retcompile, 'Compiler error'
retlink= subprocess.call(
'%s -shared extcode.o -o extcode.pyd -L%s -l%s'
% ( compilercommand, pythonpathlibs,
pythondll ) )
assert not retlink, 'Linker error'
os.remove( 'extcode.o' )
os.remove( 'extcode.c' )
def __setitem__( self, key, value ):
code, types= value
self.exts.append( Ext.ExtElem( key, code, types ) )
self.regenc()
self.regenpyd()
import extcode
setattr( self, key, getattr( extcode, key ) )
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Why not a Python compiler?

2008-02-10 Thread castironpi
On Feb 10, 7:29 am, Stefan Behnel <[EMAIL PROTECTED]> wrote:
> Steven D'Aprano wrote:
> > On Sat, 09 Feb 2008 01:11:09 +, Marc 'BlackJack' Rintsch wrote:
>
> >> On Fri, 08 Feb 2008 05:12:29 -0800, Ryszard Szopa wrote:
>
> >>> Expressing simple loops as C for loops...
> >> You mean simple loops like ``for i in xrange(1000):``?  How should the
> >> compiler know what object is bound to the name `xrange` when that loop
> >> is executed?
>
> > Assuming the aim is to optimize for speed rather than memory, the
> > solution is for the compiler to create something like this pseudo-code:
>
> > if xrange is Python's built-in xrange:
> >     execute optimized for-loop at C-like speed
> > else:
> >     execute unoptimized normal loop at Python speed
>
> > (In case it's not obvious, the decision of which branch to take is made
> > at run time, not compile time.)
>
> > I understand that is more or less what psycho already does.
>
> ... and Cython, when iterating over lists, for example. That's one of the
> reasons why looping is so much faster in Cython than in Pyrex.
>
> Stefan- Hide quoted text -
>
> - Show quoted text -

There's always the Visitor pattern.

in xrange(1).for x:
loop_of_x_at_forlike_speed()

Do identifiers get hashed once at compile/ definition-execution time,
or every time they're encountered?  This could be fast...
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: dream hardware

2008-02-12 Thread castironpi
On Feb 12, 7:31 pm, Jeff Schwab <[EMAIL PROTECTED]> wrote:
> Steven D'Aprano wrote:
> > On Tue, 12 Feb 2008 10:05:59 -0800, castironpi wrote:
>
> >> What is dream hardware for the Python interpreter?
>
> > I'm not sure that the Python interpreter actually does dream, but if it's
> > anything like me, it's probably a giant computer the size of a bus, made
> > out of broccoli and oven-roasted garlic, that suddenly turns into
> > Sylvester Stallone in a tutu just before my program returns its result.
>
> *Oven-roasted* garlic?  OK, that's just weird.

Is it anything special over multi-use cores/cpus/systa?

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


Re: call 'the following function' using decorators

2008-02-12 Thread castironpi
On Feb 12, 12:10 pm, [EMAIL PROTECTED] wrote:
> On Feb 12, 12:05 pm, "Gabriel Genellina" <[EMAIL PROTECTED]>
> wrote:
>
>
>
>
>
> > En Tue, 12 Feb 2008 15:20:32 -0200, <[EMAIL PROTECTED]> escribi�:
>
> > > I assert it's easier to write:
>
> > > start_new_thread( this_func )
> > > def thrA():
> > >     normal_suite()
>
> > > than
>
> > > def thrA():
> > >     normal_suite()
> > > start_new_thread( thrA )
>
> > > If you don't, stop reading.  If you do, accomplish it like this:
>
> > > @decwrap( start_new_thread, Link, ( 2, 3 ) )
> > > def anonfunc( a, b ):
> > >    print( a, b )
>
> > And I have to *guess* that start_new_thread is called?
> > A @threaded decorator might be useful, but the above isn't clear at all.
>
> > `import this` inside the interpreter.
>
> > --
> > Gabriel Genellina- Hide quoted text -
>
> > - Show quoted text -
>
> No new guessing.  It's called in
>
>                 ret= func( *ar2, **kwar2 )
>
> You might suggest a better name, though, than decwrap.  Something like
> @call_here
> with_specified_function.  What?  This probably goes under "Complex is
> better than complicated."
>
> It's not a typical decorator, but the f= g( f ) semantics come handily.- Hide 
> quoted text -
>
> - Show quoted text -

And Link could do well to call itself Blank/FuncBlank/NextFunc/
something clever
-- 
http://mail.python.org/mailman/listinfo/python-list

Re: dream hardware

2008-02-12 Thread castironpi
On Feb 12, 4:51 pm, "Martin P. Hellwig" <[EMAIL PROTECTED]> wrote:
> Bjoern Schliessmann wrote:
> > Jeff Schwab wrote:
>
> >> The only "dream hardware" I know of is the human brain.
>
> > Nah. Too few storage capacity, and too slow and error-prone at
> > simple calculations. The few special but very advanced features are
> > all hard-wired to custom hardware, it's a real nightmare
> > interfacing with it.
>
> > Regards,
>
> > Björn
>
> Yes and don't try to do a direct interface if you are not absolutely,
> certainly, 100% sure, that the understanding of the API is mutual.
>
> If not, be prepare to handle exceptions. Some you can ignore in a
> try/except clause like SyntaxError, MemoryError, RuntimeError and
> ReferenceError.
>
> Others should be more interpreted like a progress indication, for
> example: ArithmeticError which may be raised like a plain StandardError.
>
> But if you are lucky and get things more or less running watch out for
> LookupError, BaseException, EnvironmentError and NameError, does can
> ruin your day and may even be fatal.
>
> Absolutely fatal are: ValueError and TypeError, nothing really you can
> do against, even if you can catch these errors the program will usually
> still halt on a SystemExit.
>
> And if you think that is the worst can happen, he!
> There is still: AttributeError, ImportError, IOError and OverflowError.
>
> And it the end if all that didn't happen there is quite a chance either
> you or the other go for a NotImplementedError.
>
> If that didn't happen you always get after random() time an EOFError,
> however the good thing is that the process continues due to os.fork()
> (not available on all platforms).
>
> Don't count on return(0).
>
> --
> mph

Fortunately, import curses comes linked in the assembly.
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: dream hardware

2008-02-12 Thread castironpi
On Feb 12, 3:42 pm, Bjoern Schliessmann  wrote:
> Jeff Schwab wrote:
> > The only "dream hardware" I know of is the human brain.
>
> Nah. Too few storage capacity, and too slow and error-prone at
> simple calculations. The few special but very advanced features are
> all hard-wired to custom hardware, it's a real nightmare
> interfacing with it.
>
> Regards,
>
> Björn
>
> --
> BOFH excuse #373:
>
> Suspicious pointer corrupted virtual machine

Hold only one- recognition.
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: dream hardware

2008-02-12 Thread castironpi
On Feb 12, 2:15 pm, Carl Banks <[EMAIL PROTECTED]> wrote:
> On Feb 12, 1:05 pm, [EMAIL PROTECTED] wrote:
>
> > What is dream hardware for the Python interpreter?
>
> A 10 GHz single core.
>
> (Dual core if doing lots of I/O.)
>
> Carl Banks

Handle a dual 6GHz core.  Code sometimes happens in order.  Other
times not, and then you can dual-pipeline / dual-bus.
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: dream hardware

2008-02-12 Thread castironpi
On Feb 12, 1:03 pm, Tim Chase <[EMAIL PROTECTED]> wrote:
> >>> What is dream hardware for the Python interpreter?
>
> > The only "dream hardware" I know of is the human brain.  I have a
> > slightly used one myself, and it's a  pretty mediocre Python interpreter.
>
> the human brain may be a pretty mediocre Python interpreter, but
> darn if I don't miss
>
>   >>> import dwim
>
> on other platforms...
>
> -tkc

You ever go to bars?  import dwis.  sigh()
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: C function in a Python context

2008-02-12 Thread castironpi
On Feb 9, 3:04 pm, [EMAIL PROTECTED] wrote:
> On Feb 9, 1:48 pm, [EMAIL PROTECTED] wrote:
>
>
>
>
>
> > To write quick C things that Python won't do up to speed.  So it's got
> > a redundancy.
>
> > import ext
> > extA= ext.Ext()
> > extA[ 'enumfactors' ]= r"""
> >     int enumfactors( int a, const char* sep ) {
> >         int ret= 0, i;
> >         for( i= 1; i<= a; i++ ) {
> >             if( a% i== 0 ) {
> >                 ret+= 1;
> >                 if( i> 1 ) {
> >                     printf( "%s", sep );
> >                 }
> >                 printf( "%i", i );
> >             }
> >         }
> >         printf( "\n" );
> >         return ret;
> >     }
> >     """, ("i","i","s")
>
> > factorsn= extA.enumfactors( 209677683, ', ' )
> > print( "%i factors total."% factorsn )
>
> > import sys
> > sys.exit()
>
> > 1, 3, 23, 69, 131, 393, 3013, 9039, 23197, 69591, 533531, 1600593,
> > 3038807, 9116
> > 421, 69892561, 209677683
> > 16 factors total.
>
> '''Prototype implementation, slightly rigid.  If anyone knows how to
> compile and link without intermediate object file, and from a string,
> memory, or stdin, let me know.  Change first four lines.  If you are
> not using gcc, look at regenpyd().'''
>
> compilercommand=    'c:/programs/mingw/bin/gcc'
> pythondll=          'python30'
> pythonpathinclude=  'c:/programs/python/include'
> pythonpathlibs=     'c:/programs/python/libs'
>
> class Ext:
>     strctypes= { 'i': 'int', 's': 'const char*' }
>     class ExtElem:
>         def __init__( self, name, code, types ):
>             self.name, self.code= name, code
>             self.types= types
>     def __init__( self ):
>         self.__dict__[ 'exts' ]= []
>     def regenc( self ):
>         extcode= open( 'extcode.c', 'w' )
>         wr= extcode.write
>         wr( '#include <%s'% pythonpathinclude )
>         wr( '/Python.h>\n\n' )
>         for ext in self.exts:
>             wr( ext.code )
>             wr( '\n' )
>         for ext in self.exts:
>             wr( 'static PyObject *\n' )
>             wr( 'extcode_%s'% ext.name )
>             wr( '(PyObject *self, ' )
>             wr( 'PyObject *args) {\n' )
>             wr( '\t%s result;\n'%
>                 Ext.strctypes[ext.types[0]] )
>             for i, type in enumerate( ext.types[1:] ):
>                 wr( '\t%s arg%i;\n'%
>                     ( Ext.strctypes[type], i ) )
>             wr( '\tPyArg_ParseTuple(args, "' )
>             wr( ''.join( ext.types[1:] ) )
>             wr( '"' )
>             for i, type in enumerate( ext.types[1:] ):
>                 wr( ', &arg%i'% i )
>             wr( ' );\n' )
>             wr( '\tresult= %s( '% ext.name )
>             wr( ', '.join( [ 'arg%i'% i for i
>                 in range( len( ext.types[1:] ) ) ] ) )
>             wr( ' );\n' )
>             wr( '\treturn Py_BuildValue' )
>             wr( '( "%s", result );\n'% ext.types[0] )
>             wr( '}\n\n' )
>         wr( 'static PyMethodDef ExtcodeMethods[] = {\n' )
>         for ext in self.exts:
>             wr( '\t{ "%s", extcode_%s, '%
>                             ( ext.name, ext.name ) )
>             wr( 'METH_VARARGS, "" },\n' )
>         wr( '\t{NULL, NULL, 0, NULL}\n' )
>         wr( '};\n\n' )
>         wr( 'PyMODINIT_FUNC\n' )
>         wr( 'initextcode(void) {\n' )
>         wr( '\t(void) Py_InitModule' )
>         wr( '("extcode", ExtcodeMethods);\n' )
>         wr( '}\n\n' )
>         extcode.close()
>     def regenpyd( self ):
>         import os, os.path
>         if os.path.exists( 'extcode.pyd' ):
>             os.remove( 'extcode.pyd' )
>         import subprocess
>         retcompile= subprocess.call(
>             '%s extcode.c -c -I%s'%
>             ( compilercommand, pythonpathinclude ) )
>         assert not retcompile, 'Compiler error'
>         retlink= subprocess.call(
>             '%s -shared extcode.o -o extcode.pyd -L%s -l%s'
>             % ( compilercommand, pythonpathlibs,
>             pythondll ) )
>         assert not retlink, 'Linker error'
>         os.remove( 'extcode.o' )
>         os.remove( 'extcode.c' )
>     def __setitem__( self, key, value ):
>         code, types= value
>         self.exts.append( Ext.ExtElem( key, code, types ) )
>         self.regenc()
>         self.regenpyd()
>         import extcode
>         setattr( self, key, getattr( extcode, key ) )- Hide quoted text -
>
> - Show quoted text -

This is- and returns a list, of the enumerated factors.  Is it
starting to get bulky.


import ext
extA= ext.Ext()
extA[ 'enumfactors' ]= r"""
#include 
#include 
using namespace std;
PyObject* enumfactors( int a, const char* sep ) {
string fmt= "[";
vector< long > resv;
for( int i= 1; i<= a; i++ ) {
if( a% i== 0 ) {
resv.push_back( i );
fmt.append( "i" );
if( i> 1 ) {
printf( "%s", sep );
}
printf( "%i", i );
}
}

Re: dream hardware

2008-02-12 Thread castironpi
On Feb 12, 12:31 pm, Jeff Schwab <[EMAIL PROTECTED]> wrote:
> > On Feb 12, 2008 1:05 PM,  <[EMAIL PROTECTED]> wrote:
> >> What is dream hardware for the Python interpreter?
> Warren Myers wrote:
>
>  > A Cray?
>  >
>  > What are you trying to do? "dream" hardware is a very wide question.
>
> The only "dream hardware" I know of is the human brain.  I have a
> slightly used one myself, and it's a  pretty mediocre Python interpreter.

Someone mentioned language-specific chips earlier.  Or, if you could
dedicate processes on certain chips or cores.  Paralell, serial, cache
size, pick.
-- 
http://mail.python.org/mailman/listinfo/python-list


dream hardware

2008-02-12 Thread castironpi
What is dream hardware for the Python interpreter?
-- 
http://mail.python.org/mailman/listinfo/python-list


call 'the following function' using decorators

2008-02-12 Thread castironpi
I assert it's easier to write:

start_new_thread( this_func )
def thrA():
normal_suite()

than

def thrA():
normal_suite()
start_new_thread( thrA )

If you don't, stop reading.  If you do, accomplish it like this:

@decwrap( start_new_thread, Link, ( 2, 3 ) )
def anonfunc( a, b ):
print( a, b )

where 'Link' replaces the following func, plus in keywords too:

@decwrap( Thread, None, target= Link, args= ( 2, 3 ) )
def sampleth( a, b ):
print( a, b )
sampleth.start()
sampleth.join()

'Link' is a pseudo-constant.

Link= object()

@decwrap follows.

def decwrap( func, *ar, **kwar ):
def predec( func2 ):
ar2= list( ar )
while Link in ar2:
ar2[ ar2.index( Link ) ]= func2
kwar2= kwar.copy()
for k, v in kwar2.items():
if v is not Link: continue
kwar2[ k ]= func2
ret= func( *ar2, **kwar2 )
return ret
return predec

Further applications:

@decwrap( button.bind, "", Link )
def anonfunc():
print( 'Key X pressed' )

Optional stylism for readability:

@decwrap( start_new_thread, ~Link, ( 2, 3 ) )
@decwrap( Thread, None, target= ~Link, args= ( 2, 3 ) )
@decwrap( button.bind, "", ~Link )

where 'Link' is:

class NegMarking:
def __invert__( self ): return self

Link= NegMarking()
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: call 'the following function' using decorators

2008-02-12 Thread castironpi
On Feb 12, 12:05 pm, "Gabriel Genellina" <[EMAIL PROTECTED]>
wrote:
> En Tue, 12 Feb 2008 15:20:32 -0200, <[EMAIL PROTECTED]> escribi�:
>
>
>
>
>
> > I assert it's easier to write:
>
> > start_new_thread( this_func )
> > def thrA():
> >     normal_suite()
>
> > than
>
> > def thrA():
> >     normal_suite()
> > start_new_thread( thrA )
>
> > If you don't, stop reading.  If you do, accomplish it like this:
>
> > @decwrap( start_new_thread, Link, ( 2, 3 ) )
> > def anonfunc( a, b ):
> >    print( a, b )
>
> And I have to *guess* that start_new_thread is called?
> A @threaded decorator might be useful, but the above isn't clear at all.
>
> `import this` inside the interpreter.
>
> --
> Gabriel Genellina- Hide quoted text -
>
> - Show quoted text -

No new guessing.  It's called in

ret= func( *ar2, **kwar2 )

You might suggest a better name, though, than decwrap.  Something like
@call_here
with_specified_function.  What?  This probably goes under "Complex is
better than complicated."

It's not a typical decorator, but the f= g( f ) semantics come handily.
-- 
http://mail.python.org/mailman/listinfo/python-list

Re: dream hardware

2008-02-13 Thread castironpi
On Feb 13, 2:01 pm, Laurent Pointal <[EMAIL PROTECTED]>
wrote:
> Le Tue, 12 Feb 2008 10:05:59 -0800, castironpi a écrit :
>
> > What is dream hardware for the Python interpreter?
>
> Dream... I dont know, but hardware for the Python interpreter, yes.
>
> http://www.telit.co.it/product.asp?productId=96
>
> --
> Laurent POINTAL - [EMAIL PROTECTED]

Maybe something with hardware string hashing, plus maybe other
primitives.
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: dream hardware

2008-02-13 Thread castironpi
On Feb 13, 2:23 pm, [EMAIL PROTECTED] wrote:
> On Feb 13, 2:01 pm, Laurent Pointal <[EMAIL PROTECTED]>
> wrote:
>
> > Le Tue, 12 Feb 2008 10:05:59 -0800, castironpi a écrit :
>
> > > What is dream hardware for the Python interpreter?
>
> > Dream... I dont know, but hardware for the Python interpreter, yes.
>
> >http://www.telit.co.it/product.asp?productId=96
>
> > --
> > Laurent POINTAL - [EMAIL PROTECTED]
>
> Maybe something with hardware string hashing, plus maybe other
> primitives.

sum= 0
for x in xrange(1):
sum+= x

takes the sum of 0...  The formula, of course, is (*1)/2.
The assembly "is":

0: seti r0 0 #r0= 0
1: seti r1 1 #r1= 1 (count backwards)
2: seti r2 -1#r2= -1
3: seti r3 4 #r3= instruction #4
4: add r1 r0 #add= r1+ r0
5: set add r0#r0= add
6: add r1 r2 #add= r1+ r2 (r1 - 1)
7: set add r1#r1= add (r1 - 1)
8: cjmpnz r1 r3  #jump to #4 (r3) if r1 <> 0

The Python equivalent is, to say the least, more complicated.
Retrieve the iterator value from the heap, maybe cache, the integer
values from the heap (!), also maybe, iterator increment method, and
writes to instance table properties, which may or may not involve a
string hash and hash lookup every time.  Stack control clearly isn't
anything simple, either, with inspect and all (right?).  I understand
the resulting assembly isn't outputted anywhere furthermore.  -What is
it?-

Disclaimer: The assembly was based on CS Architecture from an
undergrad 'MIPS' course- possibly not Intel, x86, or anything else.
-- 
http://mail.python.org/mailman/listinfo/python-list


XML pickle

2008-02-13 Thread castironpi
Readability of the Pickle module.  Can one export to XML, from cost of
speed and size, to benefit of user-readability?

It does something else: plus functions do not export their code,
either in interpreter instructions, or source, or anything else; and
classes do not export their dictionaries, just their names.  But it
does export in ASCII.

Pickle checks any __safe_for_unpickling__ and __setstate__ methods,
which enable a little encapsulating, but don't go far.

At the other end of the spectrum, there is an externally-readable
datafile:




 
  
   
abc
123
   
  
 


Classes can be arranged to mimic this hierarchy:

class XMLable:
 def __init__( self, **kwar ):
  self.attrs= kwar
class Workbook( XMLable ):
 cattrs= {
  'xmlns': "urn:schemas-microsoft-com:office:spreadsheet",
  'xmlns:ss': "urn:schemas-microsoft-com:office:spreadsheet" }
class Worksheet( XMLable ):
 cattrs= { 'name': 'ss:Name' }
class Table( XMLable ): pass
class Row( XMLable ): pass
class Cell( XMLable ): pass
class Data( XMLable ):
 cattrs= { 'type': 'ss:Type' }

data= Data( content= 'abc', type= 'String' )
cell= Cell( data= data )
row= Row( cells= [ cell ] )
table= Table( rows= [ row ] )
sheet= Worksheet( table= table, name= "Sheet1" )
book= Workbook( sheets= [ sheet ] )

(These might make things cleaner, but are not allowed:

#data= Data( 'abc', 'ss:Type'= 'String' )
#sheet= Worksheet( table= table, 'ss:Name'= "Sheet1" )

For keys can only be identifiers in keyword argument syntax.)

How close to this end can the standard library come?  Is it more
prevalent than something else that's currently in it?  What does the
recipie look like to convert this to XML, either using import xml or
not?

import pickle
print( pickle.dumps( book ) )

is not quite what I have in mind.

I guess I'm not convinced that 'is currently in use' has always been
or even is the standard by which standard library additions are
judged.  If it's not, then I hold that XML is a good direction to go.
Will core developers listen to reason?  Does +1 = +1?
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: fromfunc functions

2008-02-13 Thread castironpi
On Feb 13, 1:32 pm, azrael <[EMAIL PROTECTED]> wrote:
> Thaks guys. this helped

May I point you to partial:

f= partial( func, arg )
f() -> func( arg )
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Regular Expression for Prime Numbers (or How I came to fail at them, and love the bomb)

2008-02-13 Thread castironpi
On Feb 13, 9:48 am, Carsten Haese <[EMAIL PROTECTED]> wrote:
> On Wed, 2008-02-13 at 07:31 -0800, [EMAIL PROTECTED] wrote:
> >     return re.match("^1?$|^(11+?)\1+$", convert)
>
> That needs to be either
>
> return re.match(r"^1?$|^(11+?)\1+$", convert)
>
> or
>
> return re.match("^1?$|^(11+?)\\1+$", convert)
>
> in order to prevent "\1" from being read as "\x01".
>
> --
> Carsten Haesehttp://informixdb.sourceforge.net

re.match(r"^(oo+?)\1+$", 'o'*i ) drops i in [0,1]

and

re.match(r"^(ooo+?)\1+$", 'o'*i ), which only drops i in [0,1,4].

Isn't the finite state machine "regular expression 'object'" really
large?
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Word document accessing using python

2008-02-13 Thread castironpi
On Feb 13, 12:07 pm, Juan_Pablo <[EMAIL PROTECTED]> wrote:
> > import win32com.client
>
>  but, window32com.client is only functional in  windows

Excel can read XML.




 
  
   
abc
123
   
  
 


Word command line to save as text, or the reader from Microsoft?
-- 
http://mail.python.org/mailman/listinfo/python-list


standardization allows?

2008-02-13 Thread castironpi
Standardization helps avoid the readability and reliability problems
which arise when many different individuals create their own slightly
varying implementations, each with their own quirks and naming
conventions.
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: dream hardware

2008-02-13 Thread castironpi
On Feb 13, 10:14 am, Florian Diesch <[EMAIL PROTECTED]> wrote:
> Jeff Schwab <[EMAIL PROTECTED]> wrote:
> >> On Feb 12, 2008 1:05 PM,  <[EMAIL PROTECTED]> wrote:
> >>> What is dream hardware for the Python interpreter?
>
> > Warren Myers wrote:
> >> A Cray?
>
> >> What are you trying to do? "dream" hardware is a very wide question.
>
> > The only "dream hardware" I know of is the human brain.  I have a
> > slightly used one myself, and it's a  pretty mediocre Python
> > interpreter.
>
> It comes without any documentation, the source code is not available
> and has lots of known bugs that did't get fixed for ages. The typical
> crap from a monopolist.
>
>    Florian
> --
> 
> ---
> **  Hi! I'm a signature virus! Copy me into your signature, please!  **
> ---

Fortran is better than what?  Ideal semantics beat pain and pleasure
any day, they are practical goals.
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Regular Expression for Prime Numbers (or How I came to fail at them, and love the bomb)

2008-02-13 Thread castironpi
On Feb 13, 5:43 pm, Mark Dickinson <[EMAIL PROTECTED]> wrote:
> On Feb 13, 5:14 pm, [EMAIL PROTECTED] wrote:
>
> > Isn't the finite state machine "regular expression 'object'" really
> > large?
>
> There's no finite state machine involved here, since this isn't a
> regular expression in the strictest sense of the term---it doesn't
> translate to a finite state machine, since backreferences are
> involved.
>
> Mark

What is it?
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: XML pickle

2008-02-13 Thread castironpi
On Feb 13, 10:41 pm, George Sakkis <[EMAIL PROTECTED]> wrote:
> On Feb 13, 4:43 pm, [EMAIL PROTECTED] wrote:
>
> > Readability of the Pickle module.  Can one export to XML, from cost
> > of speed and size, to benefit of user-readability?
>
> Take a look at gnosis.xml.pickle, it seems a good starting point.
>
> George

The way the OP specifies it, dumps-loads pairs are broken: say if
Table and Worksheet are defined in different modules.  He'd have to
have some kind of unifying pair sequence, that says that "Worksheet"
document elements come from WS.py, etc.
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: dream hardware

2008-02-13 Thread castironpi
On Feb 13, 8:28 pm, Steven D'Aprano <[EMAIL PROTECTED]
cybersource.com.au> wrote:
> On Wed, 13 Feb 2008 17:45:33 -0800, castironpi wrote:
> > Fortran is better than what?  Ideal semantics beat pain and pleasure any
> > day, they are practical goals.
>
> Okay, are you a bot or something? Almost all your posts in this thread
> have been slightly off: non sequitors like this response, or just
> slightly weird.
>
> If you're not a bot, then please don't take offense. Many human beings on
> the Internet get mistaken for bots:
>
> http://northernplanets.blogspot.com/2006/06/failing-turing-test.html
>
> --
> Steven

No, YOU are!  The brain isn't dream hardware at all, let alone for
Python.  Notwithstanding, maybe looking at the machine-level
instructions that execute during would lend a hand to the question...
(...)
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Regular Expression for Prime Numbers (or How I came to fail at them, and love the bomb)

2008-02-14 Thread castironpi
On Feb 14, 5:26 am, [EMAIL PROTECTED] wrote:
> cokofree:
>
> > Sadly that is pretty slow though...
>
> It's quadratic, and it's not even short, you can do (quadratic still):
>
> print [x for x in range(2, 100) if all(x%i for i in range(2, x))]
>
> In D you can write similar code.
> Bye,
> bearophile

all(x%i ha
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: XML pickle

2008-02-14 Thread castironpi
On Feb 14, 12:45 am, Stefan Behnel <[EMAIL PROTECTED]> wrote:
> Hi,
>
> [EMAIL PROTECTED] wrote:
> > Readability of the Pickle module.  Can one export to XML, from cost of
> > speed and size, to benefit of user-readability?
>
> Regarding pickling to XML, lxml.objectify can do that:
>
> http://codespeak.net/lxml/objectify.html
>
> however:
>
> > It does something else: plus functions do not export their code,
> > either in interpreter instructions, or source, or anything else; and
> > classes do not export their dictionaries, just their names.  But it
> > does export in ASCII.
>
> > Pickle checks any __safe_for_unpickling__ and __setstate__ methods,
> > which enable a little encapsulating, but don't go far.
>
> I'm having a hard time to understand what you are trying to achieve. Could you
> state that in a few words? That's usually better than asking for a way to do X
> with Y. Y (i.e. pickling in this case) might not be the right solution for 
> you.
>
> Stefan

The example isn't so bad.  It's not clear that it isn't already too
specific.  Pickling isn't what I want.  XML is persistent too.

XML could go a couple ways.  You could export source, byte code, and
type objects.  (Pickle could do that too, thence the confusion
originally.)

gnosis.xml and lxml have slightly different outputs.  What I'm going
for has been approached a few different times a few different ways
already.  If all I want is an Excel-readable file, that's one end of
the spectrum.  If you want something more general, but still include
Excel, that's one of many decisions to make.  Ideas.

How does lxml export: b= B(); a.b= b; dumps( a )?

It looks like he can create the XML from the objects already.
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: XML pickle

2008-02-14 Thread castironpi
On Feb 14, 12:31 pm, Stefan Behnel <[EMAIL PROTECTED]> wrote:
> [EMAIL PROTECTED] wrote:
> > On Feb 14, 12:45 am, Stefan Behnel <[EMAIL PROTECTED]> wrote:
> >> [EMAIL PROTECTED] wrote:
> >>> Readability of the Pickle module.  Can one export to XML, from cost of
> >>> speed and size, to benefit of user-readability?
> >> Regarding pickling to XML, lxml.objectify can do that:
>
> >>http://codespeak.net/lxml/objectify.html
>
> >> however:
>
> >>> It does something else: plus functions do not export their code,
> >>> either in interpreter instructions, or source, or anything else; and
> >>> classes do not export their dictionaries, just their names.  But it
> >>> does export in ASCII.
> >>> Pickle checks any __safe_for_unpickling__ and __setstate__ methods,
> >>> which enable a little encapsulating, but don't go far.
> >> I'm having a hard time to understand what you are trying to achieve. Could 
> >> you
> >> state that in a few words? That's usually better than asking for a way to 
> >> do X
> >> with Y. Y (i.e. pickling in this case) might not be the right solution for 
> >> you.
>
> >> Stefan
>
> > The example isn't so bad.  It's not clear that it isn't already too
> > specific.  Pickling isn't what I want.  XML is persistent too.
>
> > XML could go a couple ways.  You could export source, byte code, and
> > type objects.  (Pickle could do that too, thence the confusion
> > originally.)
>
> What I meant was: please state what you are trying to do. What you describe
> are the environmental conditions and possible solutions that you are thinking
> of, but it doesn't tell me what problem you are actually trying to solve.

What problem -am- I trying to solve?  Map the structure -in- to XML.

> > gnosis.xml and lxml have slightly different outputs.  What I'm going
> > for has been approached a few different times a few different ways
> > already.  If all I want is an Excel-readable file, that's one end of
> > the spectrum.  If you want something more general, but still include
> > Excel, that's one of many decisions to make.  Ideas.
>
> > How does lxml export: b= B(); a.b= b; dumps( a )?
>
> > It looks like he can create the XML from the objects already.
>
> In lxml.objectify, the objects *are* the XML tree. It's all about objects
> being bound to specific elements in the tree.
>
> Stefan- Hide quoted text -
>
> - Show quoted text -

Objects first.  Create.  The use case is a simulated strategy
tournament.
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: XML pickle

2008-02-14 Thread castironpi
On Feb 14, 1:49 pm, Stefan Behnel <[EMAIL PROTECTED]> wrote:
> Hi,
>
> [EMAIL PROTECTED] wrote:
> > Stefan Behnel wrote:
> >> What I meant was: please state what you are trying to do. What you describe
> >> are the environmental conditions and possible solutions that you are
> >> thinking of, but it doesn't tell me what problem you are actually trying
> >> to solve.
>
> http://catb.org/~esr/faqs/smart-questions.html#goal
>
> > What problem -am- I trying to solve?  Map the structure -in- to XML.
>
> http://catb.org/~esr/faqs/smart-questions.html#beprecise
>
> Is it a fixed structure you have, or are you free to use whatever you like?
>
> > Objects first.  Create.
>
> http://catb.org/~esr/faqs/smart-questions.html#writewell
>
> My guess is that this is supposed to mean: "I want to create Python objects
> and then write their structure out as XML". Is that the right translation?
>
> There are many ways to do so, one is to follow these steps:
>
> http://codespeak.net/lxml/objectify.html#tree-generation-with-the-e-f...http://codespeak.net/lxml/objectify.html#element-access-through-objec...http://codespeak.net/lxml/objectify.html#python-data-types
> then maybe 
> this:http://codespeak.net/lxml/objectify.html#defining-additional-data-cla...
> and finally this:http://codespeak.net/lxml/tutorial.html#serialisation
>
> But as I do not know enough about the problem you are trying to solve, except:
>
> > The use case is a simulated strategy tournament.
>
> I cannot tell if the above approach will solve your problem or not.
>
> Stefan

I was trying to start a discussion on a cool OO design.  Problem's
kind of solved; downer, huh?

I haven't completed it, but it's a start.  I expect I'll post some
thoughts along with progress.  Will Excel read it?  We'll see.

A design difference:

Worksheet= lambda parent: etree.SubElement( parent, "Worksheet" )
Table= lambda parent: etree.SubElement( parent, "Table" )
sheet= Worksheet( book ) #parent
table= Table( sheet )
vs.

table= Table() #empty table
sheet= Worksheet( table= table ) #child

I want to call sheet.table sometimes.  Is there a lxml equivalent?
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: XML pickle

2008-02-14 Thread castironpi
On Feb 14, 5:31 pm, [EMAIL PROTECTED] wrote:
> On Feb 14, 1:49 pm, Stefan Behnel <[EMAIL PROTECTED]> wrote:
>
>
>
>
>
> > Hi,
>
> > [EMAIL PROTECTED] wrote:
> > > Stefan Behnel wrote:
> > >> What I meant was: please state what you are trying to do. What you 
> > >> describe
> > >> are the environmental conditions and possible solutions that you are
> > >> thinking of, but it doesn't tell me what problem you are actually trying
> > >> to solve.
>
> >http://catb.org/~esr/faqs/smart-questions.html#goal
>
> > > What problem -am- I trying to solve?  Map the structure -in- to XML.
>
> >http://catb.org/~esr/faqs/smart-questions.html#beprecise
>
> > Is it a fixed structure you have, or are you free to use whatever you like?
>
> > > Objects first.  Create.
>
> >http://catb.org/~esr/faqs/smart-questions.html#writewell
>
> > My guess is that this is supposed to mean: "I want to create Python objects
> > and then write their structure out as XML". Is that the right translation?
>
> > There are many ways to do so, one is to follow these steps:
>
> >http://codespeak.net/lxml/objectify.html#tree-generation-with-the-e-f...
> > then maybe 
> > this:http://codespeak.net/lxml/objectify.html#defining-additional-data-cla...
> > and finally this:http://codespeak.net/lxml/tutorial.html#serialisation
>
> > But as I do not know enough about the problem you are trying to solve, 
> > except:
>
> > > The use case is a simulated strategy tournament.
>
> > I cannot tell if the above approach will solve your problem or not.
>
> > Stefan
>
> I was trying to start a discussion on a cool OO design.  Problem's
> kind of solved; downer, huh?
>
> I haven't completed it, but it's a start.  I expect I'll post some
> thoughts along with progress.  Will Excel read it?  We'll see.
>
> A design difference:
>
> Worksheet= lambda parent: etree.SubElement( parent, "Worksheet" )
> Table= lambda parent: etree.SubElement( parent, "Table" )
> sheet= Worksheet( book ) #parent
> table= Table( sheet )
> vs.
>
> table= Table() #empty table
> sheet= Worksheet( table= table ) #child
>
> I want to call sheet.table sometimes.  Is there a lxml equivalent?- Hide 
> quoted text -
>
> - Show quoted text -

Minimize redundancy.  Are there some possibilities ignored, such as
reading a class structure from an existing Excel XML file, downloading
the official spec, and if one is coding in Windows, how bulky is the
equiavelent COM code?  One doesn't want to be re-coding the "wheel" if
it's big and hairy.
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: XML pickle

2008-02-14 Thread castironpi
Great!

--
 \          "I moved into an all-electric house. I forgot and left the
|
  `\   porch light on all day. When I got home the front door wouldn't
|
_o__)                                         open."  -- Steven Wright
|
Ben Finney

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


Re: XML pickle

2008-02-14 Thread castironpi
> I cannot tell if the above approach will solve your problem or not.

Well, declare me a persistent object.

from lxml import etree

SS= '{urn:schemas-microsoft-com:office:spreadsheet}'
book= etree.Element( 'Workbook' )
book.set( 'xmlns', 'urn:schemas-microsoft-com:office:spreadsheet' )
sheet= etree.SubElement(book, "Worksheet")
sheet.set( SS+ 'Name', 'WSheet1' )
table= etree.SubElement(sheet, "Table")
row= etree.SubElement(table, "Row")
cell1= etree.SubElement(row, "Cell")
data1= etree.SubElement(cell1, "Data" )
data1.set( SS+ 'Type', "Number" )
data1.text= '123'
cell2= etree.SubElement(row, "Cell")
data2= etree.SubElement(cell2, "Data" )
data2.set( SS+ 'Type', "String" )
data2.text= 'abc'
out= etree.tostring( book, pretty_print= True, xml_declaration=True )
print( out )
open( 'xl.xml', 'w' ).write( out )

Can you use set( '{ss}Type' ) somehow?  And any way to make this look
closer to the original?  But it works.



  

  

  123


  abc

  

  

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


Re: XML pickle

2008-02-15 Thread castironpi
On Feb 15, 12:07 pm, [EMAIL PROTECTED] wrote:
> On Feb 15, 11:10 am, [EMAIL PROTECTED] wrote:
>
> > > > Can you use set( '{ss}Type' ) somehow?
>
> > > What is 'ss' here? A prefix?
>
> > > What about actually reading the tutorial?
>
> > >http://codespeak.net/lxml/tutorial.html#namespaces
>
> > > > And any way to make this look
> > > > closer to the original?
>
> > > What's the difference you experience?
>
> Something else that crept up is:
>
> 
> 
>   
>   
>   
>     
>   
> 
>
> Which xmlns:ns1 gets "redefined" because I just didn't figure out how
> get xmlns:ns0 definition into the Workbook tag.  But too bad for me.

In Economics, they call it "Economy to Scale"- the effect, and the
point, and past it, where the cost to produce N goods on a supply
curve on which 0 goods costs 0 exceeds that on one on which 0 goods
costs more than 0: the opposite of diminishing returns.  Does the
benefit of encapsulating the specifics of the XML file, including the
practice, exceed the cost of it?

For an only slightly more complex result, the encapsulated version is
presented; and the hand-coded, unencapsulated one is left as an
exercise to the reader.

book= Workbook()
sheet= Worksheet( book, 'WSheet1' )
table= Table( sheet )
row= Row( table, index= '2' )
style= Style( book, bold= True )
celli= Cell( row, styleid= style )
datai= Data( celli, 'Number', '123' )
cellj= Cell( row )
dataj= Data( cellj, 'String', 'abc' )

46 lines of infrastructure, moderately packed.  Note that:

etree.XML( etree.tostring( book ) )

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


Re: XML pickle

2008-02-15 Thread castironpi
> > Can you use set( '{ss}Type' ) somehow?
>
> What is 'ss' here? A prefix?
>
> What about actually reading the tutorial?
>
> http://codespeak.net/lxml/tutorial.html#namespaces
>
> > And any way to make this look
> > closer to the original?
>
> What's the difference you experience?

Target:



 
  
   
abc
123
   
  
 


It helped get me the working one, actually-- the tutorial.  'ss' is,
and I don't know the jargon for it, a local variable, or namespace
variable, prefix?, or something. xmlns:ss="urn:schemas-microsoft-
com:office:spreadsheet".  The ElementMaker example is closest, I
think, but it's working, so, ...

I'm more interested in a simplification of the construction code, and
at this point I can get goofy and brainstorm.  Ideas?




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


Re: XML pickle

2008-02-15 Thread castironpi
> In Economics, they call it "Economy to Scale"- the effect, and the
> point, and past it, where the cost to produce N goods on a supply
> curve on which 0 goods costs 0 exceeds that on one on which 0 goods
> costs more than 0: the opposite of diminishing returns.  Does the
> benefit of encapsulating the specifics of the XML file, including the
> practice, exceed the cost of it?

And for all the management out there, yes.  As soon as possible does
mean as crappy as possible.  Extra is extra.  Assume the sooner the
crappier and the theorem follows.  (Now, corroborate the premise...)

P.S.  Gluttony is American too.
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: QOTW: Re: dream hardware

2008-02-15 Thread castironpi
On Feb 14, 10:50 pm, [EMAIL PROTECTED] (Aahz) wrote:
> In article <[EMAIL PROTECTED]>,
> Steven D'Aprano  <[EMAIL PROTECTED]> wrote:
>
> >On Tue, 12 Feb 2008 10:05:59 -0800, castironpi wrote:
>
> >> What is dream hardware for the Python interpreter?
>
> >I'm not sure that the Python interpreter actually does dream, but if it's
> >anything like me, it's probably a giant computer the size of a bus, made
> >out of broccoli and oven-roasted garlic, that suddenly turns into
> >Sylvester Stallone in a tutu just before my program returns its result.
>
> IHNTA, IJWTSA

IJWTW?  Anyone set up to profile CPython?... or step through?
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: XML pickle

2008-02-15 Thread castironpi
On Feb 15, 11:10 am, [EMAIL PROTECTED] wrote:
> > > Can you use set( '{ss}Type' ) somehow?
>
> > What is 'ss' here? A prefix?
>
> > What about actually reading the tutorial?
>
> >http://codespeak.net/lxml/tutorial.html#namespaces
>
> > > And any way to make this look
> > > closer to the original?
>
> > What's the difference you experience?

Something else that crept up is:



  
  
  

  


Which xmlns:ns1 gets "redefined" because I just didn't figure out how
get xmlns:ns0 definition into the Workbook tag.  But too bad for me.
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: XML pickle

2008-02-15 Thread castironpi
On Feb 15, 2:58 pm, [EMAIL PROTECTED] wrote:
> > In Economics, they call it "Economy to Scale"- the effect, and the
> > point, and past it, where the cost to produce N goods on a supply
> > curve on which 0 goods costs 0 exceeds that on one on which 0 goods
> > costs more than 0: the opposite of diminishing returns.  Does the
> > benefit of encapsulating the specifics of the XML file, including the
> > practice, exceed the cost of it?
>
> And for all the management out there, yes.  As soon as possible does
> mean as crappy as possible.  Extra is extra.  Assume the sooner the
> crappier and the theorem follows.  (Now, corroborate the premise...)

The sooner the crappier or the parties waste time.
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: call 'the following function' using decorators

2008-02-15 Thread castironpi
> > > > I assert it's easier to write:
>
> > > > start_new_thread( this_func )
> > > > def thrA():
> > > >     normal_suite()
>
> > > > than
>
> > > > def thrA():
> > > >     normal_suite()
> > > > start_new_thread( thrA )
>
> > > > If you don't, stop reading.

Nothing beats if forkthread(): but what are the chances of getting it
in Python?
-- 
http://mail.python.org/mailman/listinfo/python-list


  1   2   3   4   5   6   7   8   9   10   >