Does Running Python modules dump stuff on "C" drive?

2006-06-19 Thread Warren
I am running win2k pro and was wondering if writing python scripts or
running modules dumps anything in the "C" drive?  I have 3 partitions
and run python on the 3rd one or "E"drive.
My "C" drive is almost full and keeps loading up with stuff I don't
want or need.  Some of it I can find some not.
So until I can get a new HD I need to keep weeding out the weeds!

TIA.

Warren

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


Rollover/wraparound time of time.clock() under win32?

2005-09-28 Thread Russell Warren
Does anyone know how long it takes for time.clock() to roll over under
win32?

I'm aware that it uses QueryPerformanceCounter under win32... when I've
used this in the past (other languages) it is a great high-res 64-bit
performance counter that doesn't roll-over for many (many) years, but
I'm worried about how many bits Python uses for it and when it will
roll over.  I need it to take years to roll over.  I'm also aware that
the actual rollover 'time' will be dependent on
QueryPerformanceFrequency, so I guess my real question is how Python
internally tracks the counter (eg: a 32 bit float would be no good),
but the first question is easier to ask. :)

time.time() is not an option for me since I need ms'ish precision and
under win32 it is at best using the 18.2Hz interrupt, and at worst it
is way worse.

I'd like to avoid having to make direct win32api calls if at all
possible.

In general - if anyone has any other approaches (preferrably platform
independent) for getting a < 1 ms resolution timer that doesn't roll
over for years, I'd like to hear it!

For now I have (for win32 and linux only):
#---
from sys import platform
if platform == 'win32':
  from time import clock as TimeStamp
else:
  from time import time as TimeStamp
print TimeStamp()
#---

This works for me as long as the clock rollover is ok and precision is
maintained.

Thanks,
Russ

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


Re: Rollover/wraparound time of time.clock() under win32?

2005-09-28 Thread Russell Warren
Thanks!  That gets me exactly what I wanted.  I don't think I would
have been able to locate that code myself.

Based on this code and some quick math it confirms that not only will
the rollover be a looong way out, but that there will not be any loss
in precision until ~ 30 years down the road.  Checking my math:

  (float(10**16 + 1) - float(10**16)) == 0
  (float(10**15 + 1) - float(10**15)) == 1
  ie: our double precision float can resolve unity differences out to
  at least 10**15
  Assuming 1 us/count we have 10**15 us / (3.15E13 us/year) = 31.7 yrs

Past this we won't roll over since the long keeps counting for a long
time, but some precision will be lost.

For those interested, the relevant win32 time code is below.  Thanks
again!

time_clock(PyObject *self, PyObject *args)
{
static LARGE_INTEGER ctrStart;
static double divisor = 0.0;
LARGE_INTEGER now;
double diff;

if (!PyArg_ParseTuple(args, ":clock"))
return NULL;

if (divisor == 0.0) {
LARGE_INTEGER freq;
QueryPerformanceCounter(&ctrStart);
if (!QueryPerformanceFrequency(&freq) || freq.QuadPart == 0) {
/* Unlikely to happen - this works on all intel
   machines at least!  Revert to clock() */
return PyFloat_FromDouble(clock());
}
divisor = (double)freq.QuadPart;
}
QueryPerformanceCounter(&now);
diff = (double)(now.QuadPart - ctrStart.QuadPart);
return PyFloat_FromDouble(diff / divisor);
}

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


scope of socket.setdefaulttimeout?

2005-09-29 Thread Russell Warren
Does anyone know the scope of the socket.setdefaulttimeout call?  Is it
a cross-process/system setting or does it stay local in the application
in which it is called?

I've been testing this and it seems to stay in the application scope,
but the paranoid side of me thinks I may be missing something... any
confirmation would be helpful.

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


Threads and socket.setdefaulttimeout

2005-10-12 Thread Russell Warren
It appears that the timeout setting is contained within a process
(thanks for the confirmation), but I've realized that the function
doesn't play friendly with threads.  If I have multiple threads using
sockets and one (or more) is using timeouts, one thread affects the
other and you get unpredictable behavior sometimes.  I included a short
script at the end of this that demonstrates the threading problem.

I'm trying to get around this by forcing all locations that want to set
a timeout to use a 'safe' call immediately prior to socket creation
that locks out setting the timeout again until the lock is released.
Something like this:

try:
  SafeSetSocketTimeout(Timeout_s)
  #lock currently acquired to prevent other threads sneaking in here
  CreateSocket()
finally:
  ReleaseSocketTimeoutSettingLock()
UseSocket()

However - this is getting increasingly painful because I don't have
easy access to all of the socket creations where I'd like to do this.
The biggest pain right now is that I'm using xmlrpclib which has some
seriously/frustratingly heavy use of __ prefixes that makes getting
inside to do this at socket creation near impossible (at least I think
so).  Right now the best I can do is surround the xmlrpclib calls with
this (effectively putting the lock release after the UseSocket), but
then other threads get hung up for the duration of the call or timeout,
rather than just the simple socket creation.

It would be nice if the timeout were implemented as an argument in the
socket constructor rather than having this global method.  Is there a
reason for this?  I tried sifting through the cvs source and got lost -
couldn't even find the call definition for socket(family, type, proto)
and gave up...

Does anybody have any idea of another way to do what I need (indpendent
socket timeouts per thread), or have suggestions on how to break into
xmlrpclib (actually down into httplib) to do the methdo I was trying?

Related question: Is there some global way that I'm unaware of to make
it so that some few lines of code are atomic/uninterruptable and no
other thread can sneak in between?

All suggestions appreciated!  Hopefully I'm just missing something
obvious.

Russ

#--- This script confirms that settimeout's affect is across threads
import threading, xmlrpclib, socket

def st():
  socket.setdefaulttimeout(0.1)

try:
  proxy = xmlrpclib.ServerProxy("http://localhost:1";)
  print proxy.NonExistentCallThatShouldTimeout()
except Exception, E:  print "Exception caught: %s" % (E,)

cbThread = threading.Thread(target = st)
cbThread.start()

try:
  print proxy.NonExistentCallThatShouldTimeout()
except Exception, E:  print "Exception caught: %s" % (E,)

#Output is:
#Exception caught: (10061, 'Connection refused')
#Exception caught: timed out

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


Re: Threads and socket.setdefaulttimeout

2005-10-19 Thread Russell Warren
Thanks for the detailed repsone... sorry for the lag in responding to
it.

After reading and further thought, the only reason I was using
setdefaulttimeout in the first place (rather then using a direct
settimeout on the socket) was because it seemed like the only way (and
easy) of getting access to the seemingly deeply buried socket being
used by xmlrpclib.  That was prior to me using threads of course.  I
then started trying to make this solution work with thread, but it is
now too convoluted as you say.  Now I think the best solution is likely
to redirect my efforts at getting access to the socket used by
xmlrpclib so that I can set it's timeout directly.  I'm still unclear
how to do this cleanly, though.

Getting to some of your comments.

> When you say "one thread affects another", I see that your example uses
> the same function for both threads. IMHO it's much better to override
> the thread's run() method than to provide a callable at thread creating
> time. That way you can be sure each thread's execution is firmly in the
> context of the particular thread instance's namespace.
>
> having said all this, I don't think that's your issue.

Correct - the bottom code is nothing to do with my code and was only to
quickly prove that it was cross-thread.

> This seems extremely contorted, and I'm pretty sure we can find a better
> way.

Couldn't agree more!

> The threads' network calls should be yielding process control during
> their timeout period to allow other runnable threads to proceed. That's

Yep.  This is not causing me any problem.

> You are aware, I presume, that you can set a timeout on each socket
> individually using its settimeout() method?

Yes, but I momentarily had forgot about it... as mentioned I ended up
making the since-bad choice of using setdefaulttimeout to get timeouts
set on the inaccessible sockets.  Then I carried it too far...

> See above. However, this *does* require you to have access to the
> sockets, which is tricky if they are buried deep in some opaque object's
> methods.

Any help on how to crack the safe would be appreciated.

> There are locks! I suspect what you need is a threading.Rlock object,
> that a thread has to hold to be able to modify the (global) default
> timeout. This isn't a full solution to your problem, though, as you have
> correctly deduced.

Not quite what I was after I don't think since potentially interfering
code needs to check the lock (via acquire) to avoid conflict.  What I
guess I mean is something general for the process saying "never ever
interrupt this block og code by running code on another thread,
regardless of whether the other thread(s) check a lock".  Thinking more
about it it seems unreasonable so I'll drop the question.

Russ

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


Any royal road to Bezier curves...?

2005-11-20 Thread Warren Francis
I'm fairly new to Python (2-3 months) and I'm trying to figure out a simple 
way to implement Bezier curves...  So far I've tried the following:

http://runten.tripod.com/NURBS/
...which won't work because the only compiled binaries are for Windows 2000, 
python 2.1.  I'm on Windows XP (for now), using Python 2.4.  I downloaded 
the source distribution, but the header files aren't included, so I'm not 
sure how to compile it.

It appears there's some bezier functionality in the python that comes 
Blender... but I'm not savvy enough yet to try and extract whatever makes 
beziers work there, to make it work in my general-purpose Python programs.

Basically, I'd like to specify a curved path of an object through space.  3D 
space would be wonderful, but I could jimmy-rig something if I could just 
get 2D...  Are bezier curves really what I want after all?

Any thoughts would be much appreciated.  I've got some ideas I want to test, 
but if I can't find a simple implementation of curves, I'm going to get so 
bogged down in trying to do that part, I'll never get to what I'm excited 
about. :-P

Warren 


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


Re: Any royal road to Bezier curves...?

2005-11-22 Thread Warren Francis
For my purposes, I think you're right about the natural cubic splines. 
Guaranteeing that an object passes through an exact point in space will be 
more immediately useful than trying to create rules governing where control 
points ought to be placed so that the object passes close enough to where I 
intended it to go.  Thanks for the insight, I never would have found that on 
my own.  At least not until Google labs comes out with a search engine that 
gives names for what you're thinking of. ;-)

I know this is a fairly pitiful request, since it just involves parsing your 
code, but I'm new enough to this that I'd benefit greatly from an couple of 
lines of example code, implementing your classes... how do I go from a set 
of coordinates to a Natural Cubic Spline, using your python code?

Thanks for all the help, everybody!

Warren

"Tom Anderson" <[EMAIL PROTECTED]> wrote in message 
news:[EMAIL PROTECTED]
> On Mon, 21 Nov 2005, Tom Anderson wrote:
>
>> On Sun, 20 Nov 2005, Warren Francis wrote:
>>
>>> Basically, I'd like to specify a curved path of an object through space. 
>>> 3D space would be wonderful, but I could jimmy-rig something if I could 
>>> just get 2D...  Are bezier curves really what I want after all?
>>
>> No. You want a natural cubic spline:
>
> In a fit of code fury (a short fit - this is python, so it didn't take 
> long), i ported my old java code to python, and tidied it up a bit in the 
> process:
>
> http://urchin.earth.li/~twic/splines.py
>
> That gives you a natural cubic spline, plus my blended quadratic spline, 
> and a framework for implementing other kinds of splines.
>
> tom
>
> -- 
> Gin makes a man mean; let's booze up and riot! 


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


Re: Any royal road to Bezier curves...?

2005-11-22 Thread Warren Francis
> If you go right to the foot of my code, you'll find a simple test routine,
> which shows you the skeleton of how to drive the code.

Oops... my request just got that much more pitiful. :-)  Thanks for the 
help.

Warren 


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


DbfilenameShelf instance has no attribute 'writeback'

2005-01-20 Thread warren ali
Anyone have any idea why this is failing with the following error

class _IndexFile:
"""An _IndexFile is an implementation class that presents a
Sequence and Dictionary interface to a sorted index file."""

def __init__(self, pos, filenameroot):
self.pos = pos
self.file = open(_indexFilePathname(filenameroot), _FILE_OPEN_MODE)
self.offsetLineCache = {}   # Table of (pathname, offset) -> (line,
nextOffset)
self.rewind()
self.shelfname = os.path.join(WNSEARCHDIR, pos + ".pyidx")
try:
import shelve
self.indexCache = shelve.open(self.shelfname, 'r')
except:
pass

Exception exceptions.AttributeError: "DbfilenameShelf instance has no
attribute 'writeback'" in Exception exceptions.AttributeError:
"DbfilenameShelf instance has no attribute 'writeback'" in Exception
exceptions.AttributeError: "DbfilenameShelf instance has no attribute
'writeback'" in Exception exceptions.AttributeError: "DbfilenameShelf
instance has no attribute 'writeback'" in

The code still returns the correct restults but each result come back
with this exception.

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


what is your opinion of zope?

2005-06-26 Thread Avery Warren
I am investigating converting a wiki site to plone. I am having
a lot of difficulty finding good documentation programmatically
accessing the ZODB API.

A lot of the user feedback is centered on how difficult it is to
get good documentation on developing using these technologies. My
question to comp.lang.python is "what is your opinion of zope?"

For those who don't know, zope is a web application server for
python developers.

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


Re: what is your opinion of zope?

2005-06-28 Thread Avery Warren
The real problem is that the version of wiki we currently use 
doesn't support any concept of workflow. That is fine for the 
company now but as it matures in its processes, a more mature 
solution will become more and more compelling. 
Various solutions include...

1. The current wiki implementation is open source, modify the 
freely available and unencumbered code to support workflow.

2. Convert the wiki files to plone documents that are already
workflow enabled.

3. Write a new app that uses the current wiki docs but with the
desired features.

My inquiry to comp.lang.python is part of this investigation. I'm
guessing that writing a zope application would be mandatory
for option 2 and a desireable possibility for option 3. I'm also
just curious as to the viability of zope from a developer's perspective.

On Sun, 26 Jun 2005 23:14:48 -0500, Terry Hancock wrote:

> However, I'm not sure why you want this information. If you are
> trying to import data into Zope, you are more likely going to be
> using Zope, not accessing ZODB directly.
> 
> Without more specifics about what you are looking for, it would be
> hard to reply further than this.
> 
> Cheers,
> Terry
> 
> --
> Terry Hancock ( hancock at anansispaceworks.com )
> Anansi Spaceworks  http://www.anansispaceworks.com

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


Re: Faster way to do this...

2005-03-01 Thread Warren Postma
Will McGugan wrote:
Isn't that equivalent to simply..
nums= range(100)
I remember the day I first realized that 900 lines of some C++ program I 
was working on could be expressed in three lines of python.  Ahh.
Rebirth.  Then there was the phase of the python-newbie so enamored of 
map and lambda.  ... Wait, actually, I'm not out of that yet. :-)

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


Re: Python 2.4 removes None data type?

2005-03-04 Thread Warren Postma
[EMAIL PROTECTED] wrote:
I just read in the 'What's New in Python 2.4' document that the None
data type was converted to a constant:
http://python.org/doc/2.4/whatsnew/node15.html
Implication: A long standing wart in Python now gone.  Its time to 
gloat. Are there any really evil glitches LEFT in Python? Now go look at 
  Perl and come back and say "Thank-deity-of-my-choice-I'm-using-Python".

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


Re: shuffle the lines of a large file

2005-03-07 Thread Warren Postma
Joerg Schuster wrote:
Unfortunately, none of the machines that I may use has 80G RAM.
So, using a dictionary will not help.
Any ideas?
Why don't you index the file?  I would store the byte-offsets of the 
beginning of each line into an index file. Then you can generate a 
random number from 1 to Whatever, go get that index from the index file,
then open your text file, seek to that position in the file, read one 
line, and close the file. Using this process you can then extract a 
somewhat random set of lines from your 'corpus' text file.

You probably should consider making a database of the file, keep the raw 
text file for sure, but create a converted copy in bsddb or pytables format.

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


Re: intigrate the PyGame module with my Python

2005-03-07 Thread Warren Postma

1. Downloaded the windows binary for python 1.5.2 from python.org.
Pygame uses Python 1.5.2 still!? :-) Oi.
Warren
--
http://mail.python.org/mailman/listinfo/python-list


Re: Python 2.4 removes None data type?

2005-03-07 Thread Warren Postma
Michael Hoffman wrote:
The fact that True and False are not constants?
Yowza.
a = True
b = False
False = a
True = b
if (1==2)==True:
print "Doom"
--
http://mail.python.org/mailman/listinfo/python-list


Re: Python 2.4 removes None data type?

2005-03-07 Thread Warren Postma
Michael Hoffman wrote:
The fact that True and False are not constants?
Yowza.
a = True
b = False
False = a
True = b
if (1==2)==True:
print "Doom"
--
http://mail.python.org/mailman/listinfo/python-list


ADO, Python and MS Exchange

2004-12-23 Thread warren ali
Hi all!

I'm new to python and I seem to have a hit a of a brick wall. I hope
you guys can help.

I'm trying to rewrite some of my vbscripts in python. This particular
script connects to a mailbox in MS Exchange via ADO and calculates the
mailbox size. I seem to have run into a couple of issues getting python
to talk to MS Exchange via ADO though. This is the code i'm using:

from win32com.client import Dispatch

conn = Dispatch('ADODB.Connection')
conn.ConnectionString = "URL=http://ctmx01/exchange/warren.ali";
conn.Open()

rs = Dispatch('ADODB.RecordSet')
rs.ActiveConnection = conn

rs.Open("Select http://schemas.microsoft.com/exchange/foldersize from
scope ('deep traversal of http://ctex01/exchange/warren.ali')",
conn.ConnectionString)

But running the code, all i get is this error message:

Traceback (most recent call last):
File "C:\Python24\ad.py", line 12, in -toplevel-
rs.Open("Select http://schemas.microsoft.com/exchange/foldersize
from scope ('deep traversal of http://ctex01/exchange/warren.ali')",
conn.ConnectionString)
File
"C:\Python24\lib\site-packages\win32com\gen_py\2A75196C-D9EB-4129-B803-931327F72D5Cx0x2x8\_Recordset.py",
line 93, in Open
return self._oleobj_.InvokeTypes(1022, LCID, 1, (24, 0), ((12, 17),
(12, 17), (3, 49), (3, 49), (3, 49)),Source, ActiveConnection,
CursorType, LockType, Options)
com_error: (-2147352567, 'Exception occurred.', (0, None, '', None, 0,
-2147217900), None)

Does anyone have any suggestions? I've kinda put the code together
based on this tutorial: http://www.mayukhbose.com/python/ado/index.php
but cant seem to adapt it to talk to exchange

This is the code that works via vbscript

http://support.microsoft.com/kb/2913

Set Rec = CreateObject("ADODB.Record")
Set Rs = CreateObject("ADODB.Recordset")

strURL = "http://exchangeserver/exchange/warren.ali";
Rec.Open strURL

sSQL = "Select"
sSQL = sSQL & "
""http://schemas.microsoft.com/exchange/foldersize""; "
'sSQL = sSQL & ", ""DAV:displayname"" "
sSQL = sSQL & " from scope ('deep traversal of " & Chr(34)
sSQL = sSQL & strURL & Chr(34) & "')"
'sSQL = sSQL & "Where ""DAV:isfolder""=true"

' Open the recordset.
Rs.Open sSQL, Rec.ActiveConnection

Thanks!!

--
Keeping it (sur)real since 1981. (now in
print:http://thinkingmachine.blogsome.com)

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


Re: boring the reader to death (wasRe: Lambda: the Ultimate DesignFlaw

2005-04-05 Thread Warren Postma
Donn Cave wrote:
That's an odd thing to say.  Poetry is verbose, florid?
Python is Dutch.
Ha. I'm vaguely dutch, whatever that means.
I would say if there is a sister word for "Programming" in the english 
language, it isn't Poetry and it surely isn't Prose. It's Dialectic.
I appreciate the idea of "Code Poetry", and I find more than a few 
coders out there with more than a rudimentary appreciation of Poetry as 
well, but I don't like the analogy. No slight to Poetry is intended. 
Rather, I intend it as a compliment. Code has an almost entirely 
practical purpose, Malbol et al excluded. Poetry, except in cases of 
extraordinarily bad poetry, may have little "practical" purpose.

Now dialectic. I like that word. And I think the art of programming is 
somewhere in the Late-Medeival period right now. From Merriam Webster, 
meanings 1,5,6 seem rather sympathetic to texts used in the creation of 
software:

Dialectic
1. LOGIC
5.  Any systematic reasoning, exposition, or argument that juxtaposes 
opposed or contradictory ideas and usually seeks to resolve their 
conflict/an intellectual exchange of ideas
6 : the dialectical tension or opposition between two interacting forces 
or elements

One way to look at Code is as a textual abstraction of a design. Having 
been flattened from brain-space into a two dimension matrix of latin 
glyphs, certain semantic/meta-data is typically omitted. Thus a 
classical programming problem in many languages: The "Write-only-code" 
syndrom. You wrote it, it runs, but you're afraid even to touch it 
yourself.  Python is stronger than other languages I have used.  When I 
go back to Python code I've written long enough ago to have forgotten 
everything about, I am more able to pick it up and work with it than I 
am with other less agile languages. I'm not merely talking about 
pedantic details of literal code-readability, I'm talking about the 
ability to intuit design from implementation, and the orthogonality of 
the design of the system to the to the faculty of human reason. (In not 
so many words: Python fits my brain.)

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


Re: Testing threading

2005-04-07 Thread Warren Postma
George Sakkis wrote:
How one goes on testing a threaded program, apart from doing a few
successful runs and crossing his fingers that it at least follows the
'correct 99.% of the time' rule ? 
If you haven't been in there and stepped through all the code, looked 
for a reasonable set of test cases, and tried those test cases, then you 
haven't tested! :-)

You test a threaded program the same way you test a non-threaded
program. Thoroughly.  Test cases. YOu don't *have* to write unit tests,
but it sure helps. Code running a few times isn't tested.  If you
prefer to test manually, really want to be thorough you have to find 
ways to make sure you've done coverage of a reasonable set of test 
cases.  How do I get myself a set of test cases? Personally I like to 
walk through my code in a debugger and ask myself "how could this go 
wrong?".  "What if I pass a huge amount of data in here? How 
unresponsive can I find ways to make this thread get?" "If this is a 
network communications thread, let's try all the various error 
conditions that can happen on a real network.". ...

When I think of an idea on how things could go wrong ("What happens if
the data isn't here or is invalid?"/"Where should I be catching 
exceptions and how should I be handling them?")

Regards,
Warren
--
http://mail.python.org/mailman/listinfo/python-list


Re: cross platform printing

2005-04-07 Thread Warren Postma
David Isaac wrote:
OK, I'll assume silence means "no", so new question:
What is the current best practice for cross platform printing of PostScript
files from Python?
Well since printing postscript files on most Unix systems (probably 
including Mac OSX although I don't really know this for sure) is 
trivially easy, why not investigate using cygwin  on Windows and 
launching an "lpr" task from your python script that prints the given 
postscript file. Implementation time on Unix: 0 minutes, 0 seconds. 
Implementation time on Windows; the time it takes make a cygwin batch 
file that prints using ghostscript.

Regards,
Warren
--
http://mail.python.org/mailman/listinfo/python-list


Whither python24.dll? {WinCvs 2.0 and Python}

2005-04-12 Thread Warren Postma
It seems that WinCvs needs a python??.dll runtime but that when I 
install Python2.4 it doesn't include this dll.

Python 2.3 does.
What's the recommendation here?
Warren
--
http://mail.python.org/mailman/listinfo/python-list


Re: Islam is not a Religion of Extremism

2014-06-22 Thread Warren Post

On 06/22/2014 03:53 PM, Terry Reedy wrote:

On 6/22/2014 2:16 PM, bv4bv4...@gmail.com wrote:
[more repeated off topic religious spam]

Google-goups users: if you would prefer not to see more of this, please
send or forward short
Complaints-To: groups-ab...@google.com


And if you're not a Google Gropes user and would prefer not to see more 
of this, try:


http://twovoyagers.com/improve-usenet.org/

--
Warren Post
https://warrenpost.wordpress.com/
--
https://mail.python.org/mailman/listinfo/python-list


Re: OT: usenet reader software

2014-07-18 Thread Warren Post

On 07/18/2014 01:10 PM, memilanuk wrote:

... is there anything out there worth
trying - on Linux - that I'm missing?


You've already tried them, but I bounce between Thunderbird and Pan. The 
former because it's integrated with the most of the rest of my messaging 
(mail, RSS); the latter for its great filtering. I too have had 
stability problems with Pan, but compiling from source fixed that for me.


--
Warren Post
https://warrenpost.wordpress.com/
--
https://mail.python.org/mailman/listinfo/python-list


Implied instance attribute creation when referencing a class attribute

2006-01-16 Thread Russell Warren
I just ran across a case which seems like an odd exception to either
what I understand as the "normal" variable lookup scheme in an
instance/object heirarchy, or to the rules regarding variable usage
before creation.  Check this out:

>>> class foo(object):
...   I = 1
...   def __init__(self):
... print self.__dict__
... self.I += 1
... print self.__dict__
...
>>> a=foo()
{}
{'I': 2}
>>> foo.I
1
>>> a.I
2
>>> del a.I
>>> a.I
1
>>> del a.I
Traceback (most recent call last):
  File "", line 1, in 
AttributeError: I
>>> non_existent_var += 1
Traceback (most recent call last):
  File "", line 1, in 
NameError: name 'non_existent_var' is not defined


In this case, 'self.I += 1' clearly has inserted a surprise
behind-the-scenes step of 'self.I = foo.I', and it is this which I find
interesting.

As I understand it, asking for self.I at this point should check
self.__dict__ for an 'I' entry, and if it doesn't find it, head on up
to foo.__dict__ and look for it.

So... I initially *thought* there were two possibilities for what would
happen with the 'self.I += 1':
  1. 'self.I += 1' would get a hold of 'foo.I' and increment it
  2. I'd get an AttributeError

Both were wrong.  I thought maybe an AttributeError because trying to
modify 'self.I' at that point in the code is a bit fuzzy... ie: am I
really trying to deal with foo.I (in which case, I should properly use
foo.I) or am I trying to reference an instance attribute named I (in
which case I should really create it explicitly first or get an error
as with the non_existent_var example above... maybe with 'self.I =
foo.I').

Python is obviously assuming the latter and is "helping" you by
automatically doing the 'self.I = foo.I' for you.  Now that I know this
I (hopefully) won't make this mistake again, but doing this seems
equivalent to taking my 'non_existent_var += 1' example above and
having the interpreter interpret as "oh, you must want to deal with an
integer, so I'll just create one for you with 'non_existent_var = 0'
first".  Fortunately this is not done, so why do it with the instance
attribute reference?

Does anyone have any solid reasoning behind the Python behavior?  It
might help drive it home more so than just taking it as "that's the way
it is" and remembering it.

It gets even more confusing for me because the behaviour could be
viewed as being opposite when dealing with mutable class members.  eg:

>>> class foo(object):
...   M = [1,2,3]
...   def __init__(self):
... self.M.append(len(self.M) + 1)
... print self.M
...
>>> a=foo()
[1, 2, 3, 4]
>>> foo.M
[1, 2, 3, 4]
>>> del a.M
Traceback (most recent call last):
  File "", line 1, in 
AttributeError: 'foo' object attribute 'M' is read-only

By opposite I mean that with immutable objects, a sloppy self.I
reference doesn't get you to the base class object, whereas with a
mutable one you do get to the base object (although I do recognize that
in both cases if you just remember that the interpreter will always
stuff in a 'self.x = BaseClass.x' it works as expected in both the
immutable and mutable case).

After all that, I guess it boils down to me thinking that the code
*should* interpret the attempted instance modification with one of the
two possibilities I mentioned above (although after typing this I'm now
leaning more towards an AttributeError rather than allowing 'self.I' to
be synonymous with 'foo.I' if no local override).

Russ

PS: Apologies if I mangled the "proper" terminology for talking about
this... hopefully it makes sense.

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


Re: Implied instance attribute creation when referencing a class attribute

2006-01-16 Thread Russell Warren
> I can see how this can be confusing, but I think the confusion here is
> yours, not Pythons ;)

This is very possible, but I don't think in the way you describe!

> self.I += 10 is an *assignment*. Like any assignment, it causes the
> attribute in question to be created

... no it isn't.  The += is an operator.  Look at the example I
included with non_existent_var above.  If that doesn't do it for you,
pop open a clean python shell and do this one:

>>> x += 2
Traceback (most recent call last):
  File "", line 1, in 
NameError: name 'x' is not defined

Note that x doesn't exists and it does not create it.  You can't
normally operate on something before it is created - Python won't
create it for you (which is why I was surprised by the class attribute
behavior in the first post).

> If you write out the longhand for += it becomes totally obvious what
> is happening and why it makes sense:

Not true as above.  The longhand for 'self.I += 1' is 'self.I = self.I
+ 1', which normally needs self.I to exist due to the RHS of this.

> So your case 1 is actually exactly what is happening! Python is
> getting a hold of foo.I and incrementing it

Nope.  My case 1 would have the 'self.I += 1' modifying the actual
class attribute, not some new instance attribute and this is definitely
NOT happening.  Maybe my example was bad?  Try this one instead:

>>> class foo(object):
...   I = 1
...   def __init__(self):
... self.I += 123455
...
>>> a=foo()
>>> a.I
123456
>>> foo.I
1
>>> del a.I
>>> a.I
1

Note that we ended up binding a new "I" to the 'a' instance with the
'self.I += 1' statement, and it started with the value of 1 (the value
of the base class attribute).  I tried to make it clear in the example
by wiping out the local copy, which then reveals the base class
attribute when you go for it again.

The fact that there is a local I being made with the value of the base
class attribute says that Python is essentially adding the line 'self.I
= foo.I' as in the code below.

>>> class foo(object):
...   I = 123455
...   def __init__(self):
... self.I = foo.I  # unnecessary since python seems to do it in
the next line
... self.I += 1
...
>>> a=foo()
>>> b=foo()
>>> c=foo()
>>> print c.I, foo.I
123456 1

For kicks I added the b and c creations to show that at no time did the
+= operator get a hold of the foo base class as you state.  It stayed
untouched at 1 the whole time.  To do that you need to reference foo
itself as in the following case:

>>> class foo(object):
...   I = 0
...   def __init__(self):
... foo.I += 1
... self.I = foo.I
...
>>> a=foo()
>>> b=foo()
>>> c=foo()
>>> print a.I, b.I, c.I, foo.I
1 2 3 3
>>> del a.I
>>> a.I
3

Here it of course *did* increment the base foo attribute since it was
directly referenced.  'a.I' stays as 1 here because I rebound a new
instance attribute I on top with a copy of the base foo.I value due to
it being immutable (a bit weird to use the same name, but I'm trying to
show something) and it is what is retrieved first by Python (local
dictionary first, if not found it goes to the base class).  When I
clear I from the local __dict__ with the del, you see that future
self.I references skip out to the base class attribute since there is
no instance I attribute anymore.

A bit of a sidetrack there... still curious why python decides to
auto-create the variable for you in this particular case.  Any other
takers?

Russ

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


Re: Implied instance attribute creation when referencing a class attribute

2006-01-16 Thread Russell Warren
D'oh... I just realized why this is happening.  It is clear in the
longhand as you say, but I don't think in the way you descibed it (or
I'm so far gone right now I have lost it).

  self.I += 1

is the same as

  self.I = self.I + 1

and when python tries figures out what the 'self.I' is on the right
hand side. it of course ends up having to move up to the base class
foo.__dict__ because there is no 'I' in self.__dict__ yet.  So it ends
up effectively being:

  self.I = foo.I + 1

which explains where the "self.I = foo.I' that I was claiming was being
done magically comes from.

What my head was thinking was that the 'self.I' lookup would move up to
get foo.__dict__['I'], and that I would effectively get 'foo.I += 1',
but this is a bit of a brain fart and is just plain wrong.

I should have seen that earlier... oh well.  I'm happy that it is
perfectly clear where it comes from, now.  It still does look odd when
you do a simplistic comparison of the behaviour of 'x += 1' and 'self.I
+= 1', but I suppose that that's just the way the lookup scheme
crumbles.  An unfortunate (and rare?) quirk, I guess.

It still might be nice were python to just block out this potential
confusion with an Exception... it seems that class vs instance
attribute referencing is confusing enough for people without having
this type of potential confusion lurking around the syntax.  It seems
like  such a simple thing, but to understand the outcomes requires
knowing how the name lookup scheme works, how mutable/immutable objects
are dealt with, and what the += keystroke-saver/macro operator is
actually doing.  That this is stuff that someone coding in python
should understand could certainly be argued, though...

Russ

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


Re: Implied instance attribute creation when referencing a class attribute

2006-01-16 Thread Russell Warren
Thanks for the additional examples, David (didn't see this before my
last post).  All of it makes sense now, including those examples.

Russ

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


How to convert arbitrary objects directly to base64 without initial string conversion?

2006-07-13 Thread Russell Warren
I've got a case where I want to convert binary blocks of data (various
ctypes objects) to base64 strings.

The conversion calls in the base64 module expect strings as input, so
right now I'm converting the binary blocks to strings first, then
converting the resulting string to base64.  This seems highly
inefficient and I'd like to just go straight from binary to a base64
string.

Here is the conversion we're using from object to string...

import ctypes
def ObjAsString(obj):
  sz = ctypes.sizeof(obj)
  charArray = ctypes.c_char * sz
  buf = charArray.from_address(ctypes.addressof(obj))
  return buf.raw[:sz]

The returned string can then be sent to base64 for conversion (although
we're actually using xmlrpc.Binary), but there is obviously some waste
in here.

import base64
b64 = base64.b64encode(ObjAsString(foo))

Is there a canned/pre-existing way to convert a block of memory to a
base64 string more efficiently?  I'd like to avoid writing my own
base64 conversion routine if possible.  Anyone have any good ideas?
Even a mroe efficient/less clunky way of conevrting an arbitrary object
to a string would be appreciated.

Thanks,
Russ

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


Re: How to convert arbitrary objects directly to base64 without initial string conversion?

2006-07-13 Thread Russell Warren
> Many functions that operate on strings also accept buffer objects as 
> parameters,
> this seems also be the case for the base64.encodestring function.  ctypes 
> objects
> support the buffer interface.
>
> So, base64.b64encode(buffer(ctypes_instance)) should work efficiently.

Thanks!  I have never used (or even heard of) the buffer objects.  I'll
check it out.

Russ

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


Re: How to convert arbitrary objects directly to base64 without initial string conversion?

2006-07-13 Thread Russell Warren
After some digging around it appears there is not a tonne of
documentation on buffer objects, although they are clearly core and
ancient... been sifting through some hits circa 1999, long before my
python introduction.

What I can find says that buffer is deprecated (Python in a Nutshell),
or non-essential/for-older-versions (Python documentation).

At least it no longer seems terribly weird to me that I never noticed
this built-in before... I got this from the python docs in reference to
buffer and others:

"Python programmers, trainers, students and bookwriters should feel
free to bypass these functions without concerns about missing something
important".

Is buffer safe to use?  Is there an alternative?

> ctypes objects support the buffer interface

How can you tell what objects support the buffer interface?  Is
anything visible at the python level, or do you need to dig into the C
source?

Regarding documentation, I assume the C PyBufferObject is the
underlying thing for the python-level buffer?  If so, is the best place
for docs on this ancient object to glean what I can from this link:
http://www.python.org/doc/1.5.2p2/api/bufferObjects.html ?

Any help is appreciated... I'd like to understand what I can about this
object if I'm to use it... I'm wary of nasty surprises.

Russ

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


Intermittent "permission denied" errors when using os.rename and a recently deleted path??

2006-07-26 Thread Russell Warren
I've been having a hard time tracking down a very intermittent problem
where I get a "permission denied" error when trying to rename a file to
something that has just been deleted (on win32).

The code snippet that gets repeatedly called is here:

  ...
  if os.path.exists(oldPath):
os.remove(oldPath)
  os.rename(newPath, oldPath)
  ...

And I get the permission denied exception on the os.rename line.
Somehow the rename target is still locked?  I don't get it.

I found a post that seemed to refer to precisely this problem:
http://groups.google.com/group/comp.lang.python/browse_frm/thread/496625ca3b0c3874/e5c19db11d8b6d4e?lnk=gst&q=os.remove+delay&rnum=1#e5c19db11d8b6d4e

However - this post describes a case where there are multiple threads
making use of other os calls.  I am running a single threaded
application and still getting this problem.  ie: the suggested fix does
not work for me.

I'm trying to see if implementing a "trap the exception and try again,
but not too many times" hack fix will do the trick, but I'm not a big
fan of this "solution", and at this point I'm not entirely certain it
will work because confirming that it *did* work is tough (it is very
difficult to repeatably create the problem).

Does anyone know of a real solution to this problem, or know what
exactly is happening so that I can work out a proper solution?

Thanks,
Russ

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


Re: Threads vs Processes

2006-07-26 Thread Russell Warren
> Another issue is the libraries you use. A lot of them aren't
> thread safe. So you need to watch out.

This is something I have a streak of paranoia about (after discovering
that the current xmlrpclib has some thread safety issues).  Is there a
list maintained anywhere of the modules that are aren't thread safe?

Russ

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


Re: Threads vs Processes

2006-07-26 Thread Russell Warren
Oops - minor correction... xmlrpclib is fine (I think/hope).  It is
SimpleXMLRPCServer that currently has issues.  It uses
thread-unfriendly sys.exc_value and sys.exc_type... this is being
corrected.

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


Re: Intermittent "permission denied" errors when using os.rename and a recently deleted path??

2006-07-26 Thread Russell Warren
> Are you running a background file accessing tool like Google Desktop
> Search or an anti-virus application? If so, try turning them off as a test.

I'm actually running both... but I would think that once os.remove
returns that the file is actually gone from the hdd.  Why would either
application be blocking access to a non-existent file?

Of course, my thinking is obviously wrong since I do get the permission
problem... I will definitely try disabling those.  Now if only I could
reproducably repeat it to make testing easier. :(

Another thing is that I certainly do want the code to work in the
presence of such tools.

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


Re: Intermittent "permission denied" errors when using os.rename and a recently deleted path??

2006-07-27 Thread Russell Warren
> Does it actually tell you the target is the problem? I see an
> "OSError: [Errno 17] File exists" for that case, not a permission error.
> A permission error could occur, for example, if GDS has the source open
> or locked when you call os.rename.

No it doesn't tell me the target is the issue... you are of course
right that it could be either.  I did some looking to see if/why GDS
would lock files at any time while scanning but didn't turn up anything
useful so far.  I'd be surprised if it did as that would be one heck of
an annoying design flaw.

Anyway - the retry-on-failure workaround seems to prevent it from
happening, although it still seems very hackish and I don't like it:

  ...
  if os.path.exists(path1): os.remove(path1)
  startTime = time.clock()
  while 1:
try:
  os.rename(self.path2, self.path1)
  break
except OSError:
  if (time.clock() - startTime) > MAX_RETRY_DURATION_s:
raise
  else:
time.sleep(0)
   ...

It feels very weird to have to verify a simple operation like this, but
if it works it works.

Russ

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


Why do this?

2006-10-05 Thread Matthew Warren
Ok, not really python focused, but it feels like the people here could
explain it for me :)

Now, I started programming when I was 8 with BBC Basic.

I never took any formal classes however, and I have never become an
expert programmer. I'm an average/hobbyist programmer with quite a few
languages under my belt but I cant do any really fancy tricks with any
of them. (although Python might be nudging me into more advanced things,
now I'm starting to get what all the __method__ thingies and operators
are all about)

I learned over the years to do things like the following, and I like
doing it like this because of readability, something Python seems to
focus on :-

Print "There are "+number+" ways to skin a "+furryanimal

But nowadays, I see things like this all over the place;

print("There are %s ways to skin a %s" % (number, furryanimal))

Now I understand there can be additional formatting benefits when
dealing with numbers, decimal places etc.. But to me, for strings, the
second case is much harder to read than the first.

I hope I'm not being dense.

The result is that I have pathalogically avoided print "%s" % (thing)
because it seems to just over complicate things.


Ta, :)

Matt.





This email is confidential and may be privileged. If you are not the intended 
recipient please notify the sender immediately and delete the email from your 
computer. 

You should not copy the email, use it for any purpose or disclose its contents 
to any other person.
Please note that any views or opinions presented in this email may be personal 
to the author and do not necessarily represent the views or opinions of Digica.
It is the responsibility of the recipient to check this email for the presence 
of viruses. Digica accepts no liability for any damage caused by any virus 
transmitted by this email.

UK: Phoenix House, Colliers Way, Nottingham, NG8 6AT UK
Reception Tel: + 44 (0) 115 977 1177
Support Centre: 0845 607 7070
Fax: + 44 (0) 115 977 7000
http://www.digica.com

SOUTH AFRICA: Building 3, Parc du Cap, Mispel Road, Bellville, 7535, South 
Africa
Tel: + 27 (0) 21 957 4900
Fax: + 27 (0) 21 948 3135
http://www.digica.com
-- 
http://mail.python.org/mailman/listinfo/python-list


RE: Why do this?

2006-10-05 Thread Matthew Warren
> 
> | Now, I started programming when I was 8 with BBC Basic.
> 
> Hey, likewise! (Except I was 12 when it came out!)

I think it came out before I was 8, and I started out with print and
input. Not sure if that's 'real' programming - I don't think I graduated
to ifs and thens and gotos and gosubs for a while.. Uhm, Or was it 'def
proc' et al for BBC Basic? Brain holes leaking old stuff out :)

> | Print "There are "+number+" ways to skin a "+furryanimal
> 
> perfectly sound Python code, as far as it goes, altho' obviously
> "Print" is spelt "print" in Python, and if that number is in fact
> a number it'll need to be str ()-ed first.

Blame outlook and AutoCaps. If number were a number I would write

print "There are",number,"ways to skin a "+furryanimal

..something I like about python, 'cause having been pathalogically
avoiding %'s etc.. I have learned to hate going "string "+str(number)+"
string"

> 
> | But nowadays, I see things like this all over the place;
> | 
> | print("There are %s ways to skin a %s" % (number, furryanimal))
> 
> The outermost brackets are (at the moment) unnecessary in python,

Oops :)

> altho' print is slated for replacement by a function in Python 3.0
> at which point they'll be necessary.

? Why do that, point me at a PEP if required and I'll read it :)

> 
> number = 3
> animal = "cat"
> print "There are %d ways to skin a %s" % (number, animal)
> 
> | Now I understand there can be additional formatting benefits when
> | dealing with numbers, decimal places etc.. But to me, for 
> strings, the
> | second case is much harder to read than the first.
> 
> I think what it comes down to is just what's most readable in the
> situation in which you're using it. Imagine a rather longer
> string, and one where  and  are not short names,
> but calls to some function. Sometimes, since %s will call __str__
> on anything it's passed, it's a quick way to get a string 
> representation
> of a more complex object, which you'd otherwise have to str () at
> least.
>
> Also, for me, substitution often reads more naturally because you're 
> not breaking the string up with lots of " +  + "..." + 
> yyy + "...  
> stuff. Again, though, depends on what your code looks like, and how 
> readable you find it. Certainly, I wouldn't advocate *pathologically* 
> avoiding the "%s" % blah style, but it needn't always be the right
> thing.
>

I am a big fan of easily human readable meaningful names for things, to
the extent that I will quite readily use var or function names
ThatCanBeQuiteLongAndExplanatory() For me, it helps retain the
readbility say, when breaking up a string with lots of + xxx + "etc..
Etc.." + lalala

>It may be easier 
> to use the 
> dictionary form of the substitution then, eg:
> 
> print """
> Long string with %(valueA)s and %(valueB)s and %(valueC)s embedded in
> it at some distance from the end...
> ...
> """ % dict (valueA=1, valueB="blah", valueC=datetime.datetime.now ())
> 
> Obviously that dict would likely be defined elsewhere, or could even
> be locals (). Once you get to this stage, though, you might want to 
> start looking at templating toolkits like Cheetah, Genshi or any of
> the many others knocking around.
>

That's something I wasn't aware of, and I think I'll try if I find
myself going

"something"+dict['thingummy']+" red ones and "+dict['colour']+" ones"

The dict substitution does feel a bit easier to read compared to the
concatenation, because of the dict['']  noise.

Matt.




This email is confidential and may be privileged. If you are not the intended 
recipient please notify the sender immediately and delete the email from your 
computer. 

You should not copy the email, use it for any purpose or disclose its contents 
to any other person.
Please note that any views or opinions presented in this email may be personal 
to the author and do not necessarily represent the views or opinions of Digica.
It is the responsibility of the recipient to check this email for the presence 
of viruses. Digica accepts no liability for any damage caused by any virus 
transmitted by this email.

UK: Phoenix House, Colliers Way, Nottingham, NG8 6AT UK
Reception Tel: + 44 (0) 115 977 1177
Support Centre: 0845 607 7070
Fax: + 44 (0) 115 977 7000
http://www.digica.com

SOUTH AFRICA: Building 3, Parc du Cap, Mispel Road, Bellville, 7535, South 
Africa
Tel: + 27 (0) 21 957 4900
Fax: + 27 (0) 21 948 3135
http://www.digica.com
-- 
http://mail.python.org/mailman/listinfo/python-list


RE: Why do this?

2006-10-05 Thread Matthew Warren
 > Also, having a variable of type str called 'number' seems 
> perverse (and 
> probably error prone), so I suspect I might need something like:
>

And not something I would normally do, but for hastily written contrived
examples I might :)

 
>print "There are "+str(number)+" ways to skin a "+furryanimal
> 
> but the format string does the conversion for free.
> 
> The other main reason for preferring format strings is that 
> they make it 
> easier to refactor the code. If you ever want to move the 
> message away from 
> where the formatting is done then it's a lot easier to 
> extract a single 
> string than it is to clean up the concatenation.
> -- 


This is a benefit of pythons implementation of format strings I hadn't
considered, that __str__() is called to get the representation. And
something I like about python, althgouh at the moment the __xxx__
methods available and their use is something I'm just getting into


This email is confidential and may be privileged. If you are not the intended 
recipient please notify the sender immediately and delete the email from your 
computer. 

You should not copy the email, use it for any purpose or disclose its contents 
to any other person.
Please note that any views or opinions presented in this email may be personal 
to the author and do not necessarily represent the views or opinions of Digica.
It is the responsibility of the recipient to check this email for the presence 
of viruses. Digica accepts no liability for any damage caused by any virus 
transmitted by this email.

UK: Phoenix House, Colliers Way, Nottingham, NG8 6AT UK
Reception Tel: + 44 (0) 115 977 1177
Support Centre: 0845 607 7070
Fax: + 44 (0) 115 977 7000
http://www.digica.com

SOUTH AFRICA: Building 3, Parc du Cap, Mispel Road, Bellville, 7535, South 
Africa
Tel: + 27 (0) 21 957 4900
Fax: + 27 (0) 21 948 3135
http://www.digica.com
-- 
http://mail.python.org/mailman/listinfo/python-list


RE: Why do this?

2006-10-05 Thread Matthew Warren
 > Duncan Booth wrote:
> 
> > print "There are"+number+"ways to skin a"+furryanimal
> > 
> > or at least something equivalent to it. If I try to make 
> the same mistake 
> > with a format string it jumps out to me as wrong:
> > 
> > "There are%sways to skin a%s" % (number, furryanimal)
> 
> Related to this, formatting with sequences is also much more readable 
> when there are complex interpunction and quoting characters present, 
> like this:
> 
> print "'"+var1+"','"+var2'"+","+var3
> 
> the above is much more readable as
> 
> print "'%s', '%s', %s" % (var1, var2, var3)

Hmm, with my hastily written contrived example, I had forgotten about
sequences like that.

Oddly, in the case above I prefer reading the bottom example,

And shouldn't that top one be

print "'"+var1+"','"+var2+"',"+var3

;)

So to conclude, in future I will read it, and see how readable it is
before I decide. But now at least I do see a reason not to
pathalogically avoid such string formatting :)

Matt.


This email is confidential and may be privileged. If you are not the intended 
recipient please notify the sender immediately and delete the email from your 
computer. 

You should not copy the email, use it for any purpose or disclose its contents 
to any other person.
Please note that any views or opinions presented in this email may be personal 
to the author and do not necessarily represent the views or opinions of Digica.
It is the responsibility of the recipient to check this email for the presence 
of viruses. Digica accepts no liability for any damage caused by any virus 
transmitted by this email.

UK: Phoenix House, Colliers Way, Nottingham, NG8 6AT UK
Reception Tel: + 44 (0) 115 977 1177
Support Centre: 0845 607 7070
Fax: + 44 (0) 115 977 7000
http://www.digica.com

SOUTH AFRICA: Building 3, Parc du Cap, Mispel Road, Bellville, 7535, South 
Africa
Tel: + 27 (0) 21 957 4900
Fax: + 27 (0) 21 948 3135
http://www.digica.com
-- 
http://mail.python.org/mailman/listinfo/python-list


RE: Why do this?

2006-10-05 Thread Matthew Warren
> [Matthew Warren]
> 
> | Blame outlook and AutoCaps. If number were a number I would write
> | 
> | print "There are",number,"ways to skin a "+furryanimal
> 
> You see now that strikes me as a bit mixed up. Why not simply use?
> 
> print "a", number, "c", string
> 

Habit (not always a good thing..), and it helps keep the distinction as
to what is a number and what is s a string.


> 
> Ultimately it's down to you, but I think you may be doing
> your code a disservice by so assiduously avoiding the %s-style
> of string building. Your last example suggests that you
> see the dict-subtitution flavour simply as an alternative to doing
> "a" + dict[key1] + "b" + dict[key2] etc. I doubt if I've *ever* 
> started with the one and ended with the other; rather I've seen that
> my code would be more readable if I put/had what I wanted into a
> dict (or a dict-like object; just has to support __getitem__).


Not quite. I was meaning that where I had things naturally in a dict and
found myself concatenating the string, I would now probably use the
substitution method.


> 
> An easy example of this is where -- like many, I believe -- I 
> prefer my
> database rows to come in dict-like objects, rather than the 
> tuples which
> the dbapi stipulates. Again, like many, I've used or rolled my own
> wrapper
> which means I can do things like this, where my dbutils.fetch function
> returns some kind of object which traps __getitem__ calls for column
> names and returns the appropriate entry in the underlying tuple:
> 
> 
> import dbutils
> db = db_module.connect (...)
> for row in dbutils.fetch (db, "SELECT * FROM blah"):
>   print "Customer %(name)s (%(id)d) has %(n_orders)d 
> outstanding orders
> since %(last_order_date)s" % row
> 
> 
> 
> TJG

I'm only just really looking into the effects of using things like
__getitem__ etc.., I imagine my approach will become more sophisticated
once I have looked into them.


This email is confidential and may be privileged. If you are not the intended 
recipient please notify the sender immediately and delete the email from your 
computer. 

You should not copy the email, use it for any purpose or disclose its contents 
to any other person.
Please note that any views or opinions presented in this email may be personal 
to the author and do not necessarily represent the views or opinions of Digica.
It is the responsibility of the recipient to check this email for the presence 
of viruses. Digica accepts no liability for any damage caused by any virus 
transmitted by this email.

UK: Phoenix House, Colliers Way, Nottingham, NG8 6AT UK
Reception Tel: + 44 (0) 115 977 1177
Support Centre: 0845 607 7070
Fax: + 44 (0) 115 977 7000
http://www.digica.com

SOUTH AFRICA: Building 3, Parc du Cap, Mispel Road, Bellville, 7535, South 
Africa
Tel: + 27 (0) 21 957 4900
Fax: + 27 (0) 21 948 3135
http://www.digica.com
-- 
http://mail.python.org/mailman/listinfo/python-list


RE: dictionary of list from a file

2006-10-05 Thread Matthew Warren
 

> -> > Python 2.5 introduced a dictionary type with automatic 
> > creation of values,
> > ala Perl:
> > 
> > ===
> > from collections import defaultdict
> > 
> > d = defaultdict(list)
> > for line in fl:
> >  k, v = line.strip().split()
> >  d[k].append(v)
> > 
> > for k,v in d.items():
> >  print k, v
> > ===
> > 
> > Notice that Python is always more strongly typed, so you have 
> > to specify a
> > factory function.
> 
> 
> Yay! Python2.5 fixed my approach to this, I tried
> 
> from collections import defaultdict
> f=file('c:\\test.txt')
> lines=f.readlines()
> f.close()
> d=defaultdict(list)
> [ d[l.split()[0]].append(l.split()[1]) for l in lines ]
> 
> But, if I try your (to see if I've actually got it right)
> 
> For k,v in d.items():
>   print k,v
> 
> I get
> 
> AttributeError: 'list' object has no attribute 'items'
> 


Okok, I'm silly.


This email is confidential and may be privileged. If you are not the intended 
recipient please notify the sender immediately and delete the email from your 
computer. 

You should not copy the email, use it for any purpose or disclose its contents 
to any other person.
Please note that any views or opinions presented in this email may be personal 
to the author and do not necessarily represent the views or opinions of Digica.
It is the responsibility of the recipient to check this email for the presence 
of viruses. Digica accepts no liability for any damage caused by any virus 
transmitted by this email.

UK: Phoenix House, Colliers Way, Nottingham, NG8 6AT UK
Reception Tel: + 44 (0) 115 977 1177
Support Centre: 0845 607 7070
Fax: + 44 (0) 115 977 7000
http://www.digica.com

SOUTH AFRICA: Building 3, Parc du Cap, Mispel Road, Bellville, 7535, South 
Africa
Tel: + 27 (0) 21 957 4900
Fax: + 27 (0) 21 948 3135
http://www.digica.com
-- 
http://mail.python.org/mailman/listinfo/python-list


RE: dictionary of list from a file

2006-10-05 Thread Matthew Warren
 

> -Original Message-
> From: 
> [EMAIL PROTECTED] 
> [mailto:[EMAIL PROTECTED]
> rg] On Behalf Of Giovanni Bajo
> Sent: 04 October 2006 15:17
> To: python-list@python.org
> Subject: Re: dictionary of list from a file
> 
> [EMAIL PROTECTED] wrote:
> 
> > while(){
> >   @info=split(/ +/,$_);
> >   push (@{$tmp{$info[0]}},$info[1]);
> > }
> >
> > and then
> > foreach $key (keys %tmp){
> >print "$key -> @{$tmp{$key}}\n";
> > }
> 
> Python 2.5 introduced a dictionary type with automatic 
> creation of values,
> ala Perl:
> 
> ===
> from collections import defaultdict
> 
> d = defaultdict(list)
> for line in fl:
>  k, v = line.strip().split()
>  d[k].append(v)
> 
> for k,v in d.items():
>  print k, v
> ===
> 
> Notice that Python is always more strongly typed, so you have 
> to specify a
> factory function.


Yay! Python2.5 fixed my approach to this, I tried

from collections import defaultdict
f=file('c:\\test.txt')
lines=f.readlines()
f.close()
d=defaultdict(list)
[ d[l.split()[0]].append(l.split()[1]) for l in lines ]

But, if I try your (to see if I've actually got it right)

For k,v in d.items():
print k,v

I get

AttributeError: 'list' object has no attribute 'items'




This email is confidential and may be privileged. If you are not the intended 
recipient please notify the sender immediately and delete the email from your 
computer. 

You should not copy the email, use it for any purpose or disclose its contents 
to any other person.
Please note that any views or opinions presented in this email may be personal 
to the author and do not necessarily represent the views or opinions of Digica.
It is the responsibility of the recipient to check this email for the presence 
of viruses. Digica accepts no liability for any damage caused by any virus 
transmitted by this email.

UK: Phoenix House, Colliers Way, Nottingham, NG8 6AT UK
Reception Tel: + 44 (0) 115 977 1177
Support Centre: 0845 607 7070
Fax: + 44 (0) 115 977 7000
http://www.digica.com

SOUTH AFRICA: Building 3, Parc du Cap, Mispel Road, Bellville, 7535, South 
Africa
Tel: + 27 (0) 21 957 4900
Fax: + 27 (0) 21 948 3135
http://www.digica.com
-- 
http://mail.python.org/mailman/listinfo/python-list


RE: building strings from variables

2006-10-05 Thread Matthew Warren

> -Original Message-
> From: 
> [EMAIL PROTECTED] 
> [mailto:[EMAIL PROTECTED]
> rg] On Behalf Of Gal Diskin
> Sent: 05 October 2006 16:01
> To: python-list@python.org
> Subject: building strings from variables
> 
> Following a discussion with an associate at work about various ways to
> build strings from variables in python, I'd like to hear your opinions
> and preferred methods. The methods we discussed are:
> 1.  some_string = "cd %s ; %s  %d %s %s" % ( working_dir, ssh_cmd,
> some_count, some_param1, some_param2)
> 
> 2. import string
> template = string.Template("cd $dir ; $cmd $count $param1
> $param2")
> some_string = template.substitute(dict(dir=working_dir,
> 
> cmd=ssh_cmd,
> 
> count=some_count,
> 
> pararm1=some_param1,
> 
> param2=some_param2))
> here you can use a couple of nice tricks by using class.__dict__ and
> globals() \ locals() dictionaries.
> 
> 3. some_string = "cd "+working_dir+" ; "+ssh_cmd+ "
> "+str(some_count)+" "+some_param1+" "+some_param2
> (all these are supposed to produce the same strings)
> 
> Which methods do you know of \ prefer \ think is better because...?
> I will appreciate any opinions about the matter.

:D

I think, it would depend really on what your aims are (readable code,
fast string generation...), and how the vars you want to build the
string from are respresented in your code (is it natural to use a dict
etc..)

I kicked off a conversation similar to this earlier today, and that was
my conclusion after helpful debate & advice.

Matt.


This email is confidential and may be privileged. If you are not the intended 
recipient please notify the sender immediately and delete the email from your 
computer. 

You should not copy the email, use it for any purpose or disclose its contents 
to any other person.
Please note that any views or opinions presented in this email may be personal 
to the author and do not necessarily represent the views or opinions of Digica.
It is the responsibility of the recipient to check this email for the presence 
of viruses. Digica accepts no liability for any damage caused by any virus 
transmitted by this email.

UK: Phoenix House, Colliers Way, Nottingham, NG8 6AT UK
Reception Tel: + 44 (0) 115 977 1177
Support Centre: 0845 607 7070
Fax: + 44 (0) 115 977 7000
http://www.digica.com

SOUTH AFRICA: Building 3, Parc du Cap, Mispel Road, Bellville, 7535, South 
Africa
Tel: + 27 (0) 21 957 4900
Fax: + 27 (0) 21 948 3135
http://www.digica.com
-- 
http://mail.python.org/mailman/listinfo/python-list


Using twisted, not telnetlib for interactive telnet (WAS: RE: Improving telnetlib)

2006-10-06 Thread Matthew Warren
 

> >The trouble is, I havent got a clue where to start and would 
> appreciate
> >a couple of pointers to get me going...
> >
> 
> I'd suggest taking a look at Twisted, which contains a more complete
> telnet implementation (not as important for being able to launch vi),
> an ssh implementation (which you might want to use instead of telnet),
> a VT102 implementation (which is actually what will help you 
> run programs
> that want to fiddle around with the cursor in fancy ways), as 
> well as a
> fair amount of work towards a simple terminal emulator (to 
> help you keep
> track of what vi has done to your virtual terminal).
> 
> API docs for insults:
> 
> http://twistedmatrix.com/documents/current/api/twisted.conch.i
> nsults.html
> 
> And for the telnet implementation:
> 
> http://twistedmatrix.com/documents/current/api/twisted.conch.t
> elnet.html
>


Looking through those docs quickly leads me into quite a bewildering
maze.

As a kind of way to perhaps get me heading in the right direction and
understanding how I start to hang all that together to get what I want,
I would appreciate it if anyone could look at this little code snippet I
have, and illustrate how I can end up doing the same thing with twisted,
but end up with an interactive connection that can handle vi...  From
there I will happily trundle off by myself.

I think I'm looking for help in getting that AhA! moment :)

Snippet;

C=telnetlib.Telnet(self.TCPAddress)
.
.
((connection / password handling etc..))
.
.
if AliasedSubbedCmd=='__interact':
if system=='UNIX':
retkey='^D'
else:
retkey='^Z'
  print '\nTELNET entity '+self.Name+' entering interactive
mode. Use '+retkey+' to come back\n'
C.mt_interact()


...at this point I am essentially on the remote system command line,
with a very dumb terminal. How could I do this in twisted and end up
with a fairly  clever terminal?


Thanks,

Matt.



This email is confidential and may be privileged. If you are not the intended 
recipient please notify the sender immediately and delete the email from your 
computer. 

You should not copy the email, use it for any purpose or disclose its contents 
to any other person.
Please note that any views or opinions presented in this email may be personal 
to the author and do not necessarily represent the views or opinions of Digica.
It is the responsibility of the recipient to check this email for the presence 
of viruses. Digica accepts no liability for any damage caused by any virus 
transmitted by this email.

UK: Phoenix House, Colliers Way, Nottingham, NG8 6AT UK
Reception Tel: + 44 (0) 115 977 1177
Support Centre: 0845 607 7070
Fax: + 44 (0) 115 977 7000
http://www.digica.com

SOUTH AFRICA: Building 3, Parc du Cap, Mispel Road, Bellville, 7535, South 
Africa
Tel: + 27 (0) 21 957 4900
Fax: + 27 (0) 21 948 3135
http://www.digica.com
-- 
http://mail.python.org/mailman/listinfo/python-list


Funky file contents when os.rename or os.remove are interrupted

2006-10-10 Thread Russell Warren
I've got a case where I'm seeing text files that are either all null
characters, or are trailed with nulls due to interrupted file access
resulting from an electrical power interruption on the WinXP pc.

In tracking it down, it seems that what is being interrupted is either
os.remove(), or os.rename().  Has anyone seen this behaviour, or have
any clue what is going on?

On first pass I would think that both of those calls are single step
operations (removing/changing an entry in the FAT, or FAT-like thing,
on the HDD) and wouldn't result in an intermediate, null-populated,
step, but the evidence seems to indicate I'm wrong...

Any insight from someone with knowledge of the internal operations of
os.remove and/or os.rename would be greatly appreciated, although I
expect the crux may be at the os level and not in python.

Russ

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


Bad Code (that works) help me re-write!

2006-10-11 Thread Matthew Warren
I have the following piece of code, taken from a bigger module, that
even as I was writing I _knew_ there were better ways of doing it, using
a parser or somesuch at least, but learning how wasn't as fun as coding
it... And yes alarm bells went off when I found myself typing eval(),
and I'm sure this is an 'unusual' use of for: else: . And I know it's
not very robust. As an ex cuse/planation, this is what happens when you
get an idea in your head and code it until it works, rather than doing
any kind of planning / designing first :)

I have a file that indicates the syntax of commands. User input is
checked against this file until it matches one of the lines in the file.
Then the indicated commands are run.

I think what I have ended up doing is writing a kind of a brute-force
parser for the file format I have defined. The trouble is, I'm sure
there are much more 'agile' and 'elegant' ways of achieveing this, and
also getting rid of eval() !

I'm posting here in the hope that it will criticised, laughed at, picked
apart and hence I shall learn a bit about python and how to do this
properly :)


The code follows, followed by the file defining the syntax of commands.

import re

def dbg(Msg,Fn):
try:
if (TRACE[Fn] or TRACE["ALL"]):
print 'DBG:'+Fn+': '+Msg
except KeyError: 
pass

def processsubstitutions(RawCmd):
DBGBN='processsubstitutions'
infprotect=1
subhit=1
while subhit==1:
subhit=0
for sub in Substitutions:
SubCheck=RawCmd
RawCmd=re.sub('~'+sub,' '.join(Substitutions[sub]),RawCmd)
if SubCheck!=RawCmd:
#dbg('Made Substitution '+sub+' to get '+RawCmd,DBGBN)
subhit=1
infprotect=infprotect+1
if infprotect>100:
return "ERROR: Infinitely deep substitution levels
detected."
return RawCmd

#
#...processcommand is called with a string entered by the user.
#

def processcommand(Command):
DBGBN='processcommand'
dbg('Before Substitution: '+Command,DBGBN)
Command=processsubstitutions(Command)  #  if ! aliascmd then flow
is,  RawCmd->subbed->executed
# is IS aliascmd
then flow is   RawCmd->Subbed->aliashit->subbed->executed
dbg('After Substitution: '+Command,DBGBN)
AliasHit=0
CommandHit=0
SplitCmd=Command.split()
SplitLen=len(SplitCmd)
Cmd=SplitCmd[0]
InEtcRun=0
Error=0
CommandDefs=file(installroot+'FatControllerCommands.sav','r')
for Def in CommandDefs:
dbg("Scanning cdef ::"+Def,DBGBN)
if Def!='ENDCOMMANDDEFS':
DefTokens=Def.split()
ctr=0
for Token in DefTokens:
dbg("Doing token "+Token,DBGBN)
if re.search('input:',Token):
if SplitLen>ctr:
dbg("Is an input tag. ValExp=",DBGBN)

ValidateExpression=Token.replace('input:','').replace('<<',SplitCmd[ctr]
).replace('::SPACE::',' ')
dbg(ValidateExpression,DBGBN)
else:
Error=1
ErrorText='Error: Missing parameter.'
break
if not eval(ValidateExpression):##NOT SAFE NOT SAFE.
Need to come up with entirely new
##way to do all this
Error=1
ErrorText='Error: Parameter incorrect.'
break
elif re.search('create:',Token):

CreateExpression=Token.replace('create:','').replace('::SPACE::',' ')
elif Token!='+*':
if ctr>=SplitLen:
Error=1
ErrorText='Error: Bad command.'
break
if Token!=SplitCmd[ctr]:
Error=1
ErrorText='Error: Bad command.'
break
ctr+=1
CommandHit=1
else: #{EndOf for Token} all tokens found for else
eval(CreateExpression)
break
else:   #{EndOf for Def} Check aliases
for AliasName in Aliases:
if Cmd==AliasName: #then, make cmdstring alias cmd string
and re-process
AliasCmd=' '.join(Aliases[AliasName])
AliasHit=1
dbg('Made alias hit to get '+AliasCmd,DBGBN)
break
else: #FOR loop else  not an alias, so try execute as last
entity command
if not CommandHit:
global LastExecutedEntity
if LastExecutedEntity!='':
#global LastExecutedEntity

EntityManager.execute(LastExecutedEntity,SplitCmd[0:])
else:
print '\nError: Dont know which entity to use.\n'
else:
print '\nError: Bad command.\n'
if 

RE: Bad Code (that works) help me re-write!

2006-10-11 Thread Matthew Warren
 

> -Original Message-
> From: 
> [EMAIL PROTECTED] 
> [mailto:[EMAIL PROTECTED]
> rg] On Behalf Of Giles Brown
> Sent: 11 October 2006 12:38
> To: python-list@python.org
> Subject: Re: Bad Code (that works) help me re-write!
> 
> Matthew Warren wrote:
> > I have the following piece of code,
> 
> No doubt you will get some kind soul to suggest some things, 
> but if you
> want really good
> answers I think you need explain why you want this command file (as
> opposed to using
> say a python script itself).  Are you attempting to create a simpler
> syntax than Python?
> A restricted set of operations?
> 

The code is from a larger project called the FatController.

FatController is currently a cli based utility for administering &
monitoring devices & applications in an enterprise environment.

Users enter commands at the prompt do things such as, define a managed
entity, execute instructions against that entity, etc

The processcommand() function and the command file are my attempt at
implementing the 'commands' that are available to the user. Rather than
hard-code all of the syntax and commands inside the module, I wanted an
external file to maintain the definitions of the commands available to
the user. The idea is that users will be able to implement their own
'entity' modules, which FatController will import and then use to manage
devices & applications the given entty applies to. So, at the time of
writing the code  thought It would be a good idea to have the command
definitions file external to the code, so and-users who write their own
modules can also extend the cammands available to Fatcontroller for
using those modules, if required.



Matt.








This email is confidential and may be privileged. If you are not the intended 
recipient please notify the sender immediately and delete the email from your 
computer. 

You should not copy the email, use it for any purpose or disclose its contents 
to any other person.
Please note that any views or opinions presented in this email may be personal 
to the author and do not necessarily represent the views or opinions of Digica.
It is the responsibility of the recipient to check this email for the presence 
of viruses. Digica accepts no liability for any damage caused by any virus 
transmitted by this email.

UK: Phoenix House, Colliers Way, Nottingham, NG8 6AT UK
Reception Tel: + 44 (0) 115 977 1177
Support Centre: 0845 607 7070
Fax: + 44 (0) 115 977 7000
http://www.digica.com

SOUTH AFRICA: Building 3, Parc du Cap, Mispel Road, Bellville, 7535, South 
Africa
Tel: + 27 (0) 21 957 4900
Fax: + 27 (0) 21 948 3135
http://www.digica.com
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Funky file contents when os.rename or os.remove are interrupted

2006-10-11 Thread Russell Warren
Thanks, guys... this has all been very useful information.

The machine this is happening on is already running NTFS.

The good news is that we just discovered/remembered that there is a
write-caching option (in device manager -> HDD -> properties ->
Policies tab) available in XP.  The note right beside the
write-cache-enable checkbox says:

"This setting enables write caching to improve disk performance, but a
power outage or equipment failure might result in data loss or
corruption."

Well waddya know...  write-caching was enabled on the machine.  It is
now disabled and we'll be power-cycle testing to see if it happens
again.

Regarding the comment on journaling file systems, I looked into it and
it looks like NTFS actually does do journaling to some extent, and some
effort was expended to make NTFS less susceptible to the exact problem
I'm experiencing.  I'm currently hopeful that the corrupted files we've
seen are entirely due to the mistake of having write-caching enabled
(the default).

> Then, Windows has nothing to do with it, either. It calls the routines
> of the file system driver rather directly.

It looks like that is not entirely true... this write-caching appears
to sit above the file system itself.  In any case, it is certainly not
a Python issue!

One last non-python question... a few things I read seemed to vaguely
indicate that the journaling feature of NTFS is an extension/option.
Wording could also indicate a simple feature, though.  Are there
options you can set on your file system (aside from block size and
partition)?!  I've certainly never heard of that, but want to be sure.
I definitely need this system to be as crash-proof as possible.

Thanks again,
Russ

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


RE: Any idea how to do this in Python?

2006-10-17 Thread Matthew Warren
> On 17 Oct 2006 02:56:45 -0700, Lad <[EMAIL PROTECTED]> wrote:
> >
> > Dennis,
> > Thank you for your reply
> > You say:
> > >Pretend you are the computer/application/etc.  How would YOU
> > > perform such a ranking?
> > That is what I do not know , how to perform such ranking.
> > Do you have any idea?
>  
Maybe something like,

As you capture the input from the web form in your python app on the
server, pull out the keywords you are interested in and store those
together with logon/user information in a database.

Alongside that, store the text for all your adverts in a database along
with a list of keywords for each advert.

Then, when it comes to displaying the targeted adverts on a webpage,
grab the user keywords from the database, use them to match against
adverts that have the same keywords stored in the database, then choose
from those adverts and include the output into your webpage.

Something like that should at least ensure the relevant ad's turn up on
the page. As to a more sophisticated 'weighting' scheme, you could start
doing things like associating values with keywords, matching up which
keywords a user has used against the keywords stored against different
ads, sum up the total score for each ad, the ads with the highest scores
based on the user keywords given should be displayed first..

I havent a clue if there are 'official' ways of doing it, but that's the
kind of thing I'd start looking at, anyhoo

Matt.

To the list: apologies for appended text, I  cant get to google groups
to post today :/



This email is confidential and may be privileged. If you are not the intended 
recipient please notify the sender immediately and delete the email from your 
computer. 

You should not copy the email, use it for any purpose or disclose its contents 
to any other person.
Please note that any views or opinions presented in this email may be personal 
to the author and do not necessarily represent the views or opinions of Digica.
It is the responsibility of the recipient to check this email for the presence 
of viruses. Digica accepts no liability for any damage caused by any virus 
transmitted by this email.

UK: Phoenix House, Colliers Way, Nottingham, NG8 6AT UK
Reception Tel: + 44 (0) 115 977 1177
Support Centre: 0845 607 7070
Fax: + 44 (0) 115 977 7000
http://www.digica.com

SOUTH AFRICA: Building 3, Parc du Cap, Mispel Road, Bellville, 7535, South 
Africa
Tel: + 27 (0) 21 957 4900
Fax: + 27 (0) 21 948 3135
http://www.digica.com
-- 
http://mail.python.org/mailman/listinfo/python-list


Return returns nothing in recursive function

2006-10-17 Thread Matthew Warren
Hallo people,

I have the following code that implements a simple recursive tree like
structure.

The trouble is with the GetTreeBranch function, the print statement
prints a valid value but the return immediatley afterward doesn't return
anything.

Can anyone help me with why?


Thanks,

Matt.

Code and output follow;

'''
dirtree

A simple implementation of a filesystem-like tree
datastructure.
'''

def MakeNewTree(rootname='root'):
'''MakeNewTree

Creates an empty tree with a 'root' node.
If the parameter rootname is not given, it
defaults to 'root'.
'''
return [{rootname:[]}]

def DoThingsToTree(path,value,tree,delete=False):
'''_DoThingsToTree

You should not use this function.
'''
if type(path)==type(''):
path=path.lstrip('/').rstrip('/').split('/')
for item in tree:
if type(item)==type({}) and item.has_key(path[0]):
if len(path)==1:
if not delete:
try:
item[path[0]].append(value)
except KeyError:
item[path[0]]=value
else:
if value is not None:
del(item[path[0]][value])
else:
del(tree[tree.index(item)])
break
else:
_DoThingsToTree(path[1:],value,item[path[0]],delete)

def GetTreeBranch(path,tree):
if type(path)==type(''):
path=path.lstrip('/').rstrip('/').split('/')
for item in tree:
if type(item)==type({}) and item.has_key(path[0]):
if len(path)==1:
print 'returning',tree[tree.index(item)]
return tree[tree.index(item)]
else:
GetTreeBranch(path[1:],item[path[0]])

def AddItemToTree(path,value,tree):
'''AddITemToTree

Add an item onto a 'branch' of the tree.
pathshould be a '/' separated string that
defines a path to the branch required.
value   an object to put onto the branch
treethe tree to operate on.
'''
DoThingsToTree(path,value,tree)

def AddBranchToTree(path,branch,tree):
'''AddBranchToTree

Add a new branch to the tree.
pathshould be a '/' separated string that
defines a path to the branch required.
branch  an object used as the branch identifier
(usually a string)
treethe tree to operate on
'''
DoThingsToTree(path,{branch:[]},tree)

def DelItemFromTree(path,index,tree):
'''DelItemFromTree

Remove an item from the tree.
pathshould be a '/' separated string that
defines a path to the branch required.
index   the numeric index of the item to delete
on the branch to delete. items are indexed
in the order they are added, from 0
'''
DoThingsToTree(path,index,tree,delete=True)


def DelBranchFromTree(path,tree):
'''DelBranchFromTree

Remove a branch and all items and subbranches.
pathshould be a '/' separated string that
defines a path to the branch required.
treeThe tree to delete the branch from.
'''
DoThingsToTree(path,None,tree,delete=True)


OUTPUT;

>>> from dirtree import *
>>> MyTree=MakeNewTree('Matt')
>>> AddBranchToTree('Matt','Good',MyTree)
>>> AddBranchToTree('Matt','Bad',MyTree)
>>> AddItemToTree('Matt/Good','Computers',MyTree)
>>> AddItemToTree('Matt/Bad','Drinking',MyTree)
>>> a=GetTreeBranch('Matt/Bad',MyTree)
returning {'Bad': ['Drinking']}
>>> a
>>>


This email is confidential and may be privileged. If you are not the intended 
recipient please notify the sender immediately and delete the email from your 
computer. 

You should not copy the email, use it for any purpose or disclose its contents 
to any other person.
Please note that any views or opinions presented in this email may be personal 
to the author and do not necessarily represent the views or opinions of Digica.
It is the responsibility of the recipient to check this email for the presence 
of viruses. Digica accepts no liability for any damage caused by any virus 
transmitted by this email.

UK: Phoenix House, Colliers Way, Nottingham, NG8 6AT UK
Reception Tel: + 44 (0) 115 977 1177
Support Centre: 0845 607 7070
Fax: + 44 (0) 115 977 7000
http://www.digica.com

SOUTH AFRICA: Building 3, Parc du Cap, Mispel Road, Bellville, 7535, South 
Africa
Tel: + 27 (0) 21 957 4900
Fax: + 27 (0) 21 948 3135
http://www.digica.com
-- 
http://mail.python.org/mailman/listinfo/python-list


RE: Return returns nothing in recursive function

2006-10-17 Thread Matthew Warren

> break
> else:
> _DoThingsToTree(path[1:],value,item[path[0]],delete)
> 



The '_' in front of DoThingsToTree shouldn't be there. That's what I get
for trimming off the '_' elsewhere after I pasted the code in. 


Matt.


This email is confidential and may be privileged. If you are not the intended 
recipient please notify the sender immediately and delete the email from your 
computer. 

You should not copy the email, use it for any purpose or disclose its contents 
to any other person.
Please note that any views or opinions presented in this email may be personal 
to the author and do not necessarily represent the views or opinions of Digica.
It is the responsibility of the recipient to check this email for the presence 
of viruses. Digica accepts no liability for any damage caused by any virus 
transmitted by this email.

UK: Phoenix House, Colliers Way, Nottingham, NG8 6AT UK
Reception Tel: + 44 (0) 115 977 1177
Support Centre: 0845 607 7070
Fax: + 44 (0) 115 977 7000
http://www.digica.com

SOUTH AFRICA: Building 3, Parc du Cap, Mispel Road, Bellville, 7535, South 
Africa
Tel: + 27 (0) 21 957 4900
Fax: + 27 (0) 21 948 3135
http://www.digica.com
-- 
http://mail.python.org/mailman/listinfo/python-list


RE: making a valid file name...

2006-10-18 Thread Matthew Warren
 
> 
> Hi I'm writing a python script that creates directories from user
> input.
> Sometimes the user inputs characters that aren't valid 
> characters for a
> file or directory name.
> Here are the characters that I consider to be valid characters...
> 
> valid =
> ':./,^0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ '
> 
> if I have a string called fname I want to go through each character in
> the filename and if it is not a valid character, then I want 
> to replace
> it with a space.
> 
> This is what I have:
> 
> def fixfilename(fname):
>   valid =
> ':.\,^0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ '
>   for i in range(len(fname)):
>   if valid.find(fname[i]) < 0:
>   fname[i] = ' '
>return fname
> 
> Anyone think of a simpler solution?
> 

I got;

>>> import re
>>> badfilename='£"%^"£^"£$^ihgeroighroeig3645^£$^"knovin98u4#346#1461461'
>>> valid=':./,^0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ '
>>> goodfilename=re.sub('[^'+valid+']',' ',badfilename)
>>> goodfilename
'   ^  ^   ^ihgeroighroeig3645^  ^ knovin98u4 346 1461461'



This email is confidential and may be privileged. If you are not the intended 
recipient please notify the sender immediately and delete the email from your 
computer. 

You should not copy the email, use it for any purpose or disclose its contents 
to any other person.
Please note that any views or opinions presented in this email may be personal 
to the author and do not necessarily represent the views or opinions of Digica.
It is the responsibility of the recipient to check this email for the presence 
of viruses. Digica accepts no liability for any damage caused by any virus 
transmitted by this email.

UK: Phoenix House, Colliers Way, Nottingham, NG8 6AT UK
Reception Tel: + 44 (0) 115 977 1177
Support Centre: 0845 607 7070
Fax: + 44 (0) 115 977 7000
http://www.digica.com

SOUTH AFRICA: Building 3, Parc du Cap, Mispel Road, Bellville, 7535, South 
Africa
Tel: + 27 (0) 21 957 4900
Fax: + 27 (0) 21 948 3135
http://www.digica.com
-- 
http://mail.python.org/mailman/listinfo/python-list


RE: Tkinter--does anyone use it for sophisticated GUI development?

2006-10-20 Thread Matthew Warren
 

> -Original Message-
> From: 
> [EMAIL PROTECTED] 
> [mailto:[EMAIL PROTECTED]
> rg] On Behalf Of Fredrik Lundh
> Sent: 20 October 2006 06:43
> To: python-list@python.org
> Subject: Re: Tkinter--does anyone use it for sophisticated 
> GUI development?
> 
> Kevin Walzer wrote:
> 
> > Coming from Tcl/Tk, where there are a huge number of 
> extension packages
> > to enhance the Tk widgets and which allow you to make 
> really polished
> > GUI's, I'm struck mainly by how little of this stuff has 
> made it over
> > into Tkinter/Python. For instance, I've developed several Tcl
> > applications that use the core Tk widgets, the Tile theming 
> package, the
> > Bwidget set (great tree widget and listbox, which allows 
> you to embed
> > images), and tablelist (an extremely flexible muti-column listbox
> > display). I've found Python wrappers for some of this 
> stuff, but almost
> > no documentation on how to use them, and very little in the way of
> > actual applications making use of them--which is itself a red flag.
> 
> on the other hand, such wrappers are usually extremely 
> simple, and the 
> mapping between Python and Tk is trivial.  there's simply not much to 
> add to the existing Tk module docs.
> 
> > Am I better off biting the bullet and learning wxPython--a 
> different GUI
> > paradigm to go with the new language I'm trying to learn?
> 
> that's almost designed to get "wx rul3z d00d" replies from 
> the wx crowd. 
>   let's see if they bite.
> 

Weell,  I'm in no position to evangelise it, but alongised other things
for the past two or three weeks I've been looking into different gui
building tools for python, wxDesigner, BoaConstructor, pythonCard and a
couple of others using both tkInter and wxPython.

I've given up trying to find a good one who's method of operation was
quick to pick up, and I've since written the basics of my GUI by hand in
TkInter and now wxPython (was doing that last night as it goes).

..aand so far wxPython is winning easily on the hand-coded front,
especially once you find the demo package and use it. TkInter took me 3
or 4 days without help to work out and build what I needed. WxPython
took an evening and 1 usenet post. And I think it looks much nicer.
I've yet to see what happens with the event loop of either when I start
to use the threaded scheduler I need in the app but hey...


Matt.

(in wrong place to get to google groups again.. As always apologies for
appended text)
--


This email is confidential and may be privileged. If you are not the intended 
recipient please notify the sender immediately and delete the email from your 
computer. 

You should not copy the email, use it for any purpose or disclose its contents 
to any other person.
Please note that any views or opinions presented in this email may be personal 
to the author and do not necessarily represent the views or opinions of Digica.
It is the responsibility of the recipient to check this email for the presence 
of viruses. Digica accepts no liability for any damage caused by any virus 
transmitted by this email.

UK: Phoenix House, Colliers Way, Nottingham, NG8 6AT UK
Reception Tel: + 44 (0) 115 977 1177
Support Centre: 0845 607 7070
Fax: + 44 (0) 115 977 7000
http://www.digica.com

SOUTH AFRICA: Building 3, Parc du Cap, Mispel Road, Bellville, 7535, South 
Africa
Tel: + 27 (0) 21 957 4900
Fax: + 27 (0) 21 948 3135
http://www.digica.com
-- 
http://mail.python.org/mailman/listinfo/python-list


RE: [Tutor] How to get the width of teh button widget..??

2006-10-20 Thread Matthew Warren



 

 

  
  
  
  Folks,
   
  Sorry for asking you such a trivial question.!!! But i want to size up 
  all the buttons with the same size as the largest one in the interface.. And 
  thats why I am asking this question..
   
  Regards,
  Asrarahmed 
   
   
  Hi 
  Asrarahmed.  I think, from your recent posts asking questions regarding 
  help with python on this list, you may benefit from reading this 
  document;
   
  http://catb.org/~esr/faqs/smart-questions.html
   
  You'll find you wont feel a 
  need to apologise so much for your questions 
  too.
   
  As 
  to this question, people will really need to know which gui toolkit you 
  are using, and how you have used it to build your buttons. So please post 
  more information regarding the problem, and code you have written and tried to 
  use so far.
   
  :)
   
   
  Matt.  
   
  -- 

 
This email is confidential and may be privileged. If you are not the intended recipient please notify the sender immediately and delete the email from your computer. 
You should not copy the email, use it for any purpose or disclose its contents to any other person.Please note that any views or opinions presented in this email may be personal to the author and do not necessarily represent the views or opinions of Digica.It is the responsibility of the recipient to check this email for the presence of viruses. Digica accepts no liability for any damage caused by any virus transmitted by this email.
UK: Phoenix House, Colliers Way, Nottingham, NG8 6AT UKReception Tel: + 44 (0) 115 977 1177Support Centre: 0845 607 7070Fax: + 44 (0) 115 977 7000http://www.digica.com
SOUTH AFRICA: Building 3, Parc du Cap, Mispel Road, Bellville, 7535, South AfricaTel: + 27 (0) 21 957 4900Fax: + 27 (0) 21 948 3135http://www.digica.com
 
 
This message has been scanned for viruses by BlackSpider in Partnership with Digica
 

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

Telnetlib to twisted

2006-10-27 Thread Matthew Warren
Hallo,

>>> import telnetlib
>>> l=telnetlib.Telnet('dbprod')
>>> l.interact()
telnet (dbprod)

Login:


Could anyone show how the above would be written using the twisted
framework? All I'm after is a more 'intelligent' interactive telnet
session (handles 'vi' etc..) rather than the full capabilities of the
twisted framework.


Thanks,

Matt.


This email is confidential and may be privileged. If you are not the intended 
recipient please notify the sender immediately and delete the email from your 
computer. 

You should not copy the email, use it for any purpose or disclose its contents 
to any other person.
Please note that any views or opinions presented in this email may be personal 
to the author and do not necessarily represent the views or opinions of Digica.
It is the responsibility of the recipient to check this email for the presence 
of viruses. Digica accepts no liability for any damage caused by any virus 
transmitted by this email.

UK: Phoenix House, Colliers Way, Nottingham, NG8 6AT UK
Reception Tel: + 44 (0) 115 977 1177
Support Centre: 0845 607 7070
Fax: + 44 (0) 115 977 7000
http://www.digica.com

SOUTH AFRICA: Building 3, Parc du Cap, Mispel Road, Bellville, 7535, South 
Africa
Tel: + 27 (0) 21 957 4900
Fax: + 27 (0) 21 948 3135
http://www.digica.com
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Can Python format long integer 123456789 to 12,3456,789 ?

2006-06-02 Thread Warren Block
John Machin <[EMAIL PROTECTED]> wrote:
> On 2/06/2006 9:08 AM, A.M wrote:
>> Hi,
>> 
>> Is there any built in feature in Python that can format long integer 
>> 123456789 to 12,3456,789 ?
>> 
>> Thank you,
>> Alan
>
> Not that I know of, but this little kludge may help:
> 8<---
> import re
>
> subber = re.compile(r'^(-?\d+)(\d{3})').sub
>
> def fmt_thousands(amt, sep):
>  if amt in ('', '-'):
>  return ''
>  repl = r'\1' + sep + r'\2'
>  while True:
>  new_amt = subber(repl, amt)
>  if new_amt == amt:
>  return amt
>  amt = new_amt
>
> if __name__ == "__main__":
>  for prefix in ['', '-']:
>  for k in range(11):
>  arg = prefix + "1234567890.1234"[k:]
>  print "<%s> <%s>" % (arg, fmt_thousands(arg, ","))
> 8<---

Why not just port the Perl "commify" code?  You're close to it, at least
for the regex:

# From perldoc perlfaq5
# 1 while s/^([-+]?\d+)(\d{3})/$1,$2/;

# Python version

import re

def commify(n):
while True:
(n, count) = re.subn(r'^([-+]?\d+)(\d{3})', r'\1,\2', n)
if count == 0: break
return n

-- 
Warren Block * Rapid City, South Dakota * USA
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Installation Problem

2006-06-04 Thread Warren Block
Marshall Dudley <[EMAIL PROTECTED]> wrote:
> Sorry, this is a FreeBSD system 4.8-RELEASE
>
> I found another set of documents that say to use the following to
> install::
>
> python setup.py install
>
> but after running it, I still have the same problem.

[top-posting trimmed, please don't do that]

Doesn't the port work for 4.8?  It does work on FreeBSD 4.11, but there 
may have been changes to the ports system since 4.8.  (You should 
consider updating to 4.11.)

There are several patch files in the FreeBSD port, including
one to setup.py.

The easiest way is to cvsup your ports tree and then

cd /usr/ports/lang/python
make
make install
make clean

-- 
Warren Block * Rapid City, South Dakota * USA
-- 
http://mail.python.org/mailman/listinfo/python-list


Recommended way to fix core python distribution issues in your own apps?

2006-06-19 Thread Russell Warren
I've got a case where I need to tweak the implementation of a default
python library due to what I consider to be an issue in the library.

What is the best way to do this and make an attempt to remain
compatible with future releases?

My specific problem is with the clock used in the threading.Event and
threading.Timer.  It currently uses time.time, which is affected by
changes in system time.  eg: if you change the system clock somehow at
some time (say, with an NTP broadcast) you may get a surprise in the
timing of your code execution.

What I do right now is basically this:

import sys
import time
import threading
if sys.platform == 'win32':
  threading._time = time.clock

in which case I'm simply forcing the internal clock used in the
Event/Timer code to use a time-independent performance timer rather
than the system time.

I figured this is a much better way to do it than snagging a private
copy of threading.py and making a direct change to it, but am curious
if anyone has a better way of doing this type of thing?  For example, I
have no way of guaranteeing that this hack will work come a change to
2.5 or later.

Thanks,
Russ

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


Is Queue.Queue.queue.clear() thread-safe?

2006-06-22 Thread Russell Warren
I'm guessing no, since it skips down through any Lock semantics, but
I'm wondering what the best way to clear a Queue is then.

Esentially I want to do a "get all" and ignore what pops out, but I
don't want to loop through a .get until empty because that could
potentially end up racing another thread that is more-or-less blindly
filling it asynchronously.

Worst case I think I can just borrow the locking logic from Queue.get
and clear the deque inside this logic, but would prefer to not have to
write wrapper code that uses mechanisms inside the object that might
change in the future.

Also - I can of course come up with some surrounding architecture to
avoid this concern altogether, but a thread-safe Queue clear would do
the trick and be a nice and short path to solution.

If QueueInstance.queue.clear() isn't thread safe... what would be the
best way to do it?  Also, if not, why is the queue deque not called
_queue to warn us away from it?

Any other comments appreciated!

Russ

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


Re: better Python IDE? Mimics Maya's script editor?

2006-06-22 Thread Russell Warren
Check out the Wing IDE - www.wingware.com .

As part of it's general greatness it has a "debug probe" which lets you
execute code snippets on active data in mid-debug execution.

It doesn't have precisely what you are after... you can't (yet)
highlight code segments and say "run this, please", but I think it
might almost have what you want for general workflow improvement.

The main drawback is that it is a commercial product, albeit "cheap".
The extra drawback is that the debug probe feature requires the
professional version which is "less cheap", but still < $200.  Well
worth it for professional development IMO.

They have a great demo policy... you should check it out.  I tried
several different IDEs (I've become accustomed to using IDEs over
supe'd up text editors) and Wing was/is my favorite.

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


Re: Is Queue.Queue.queue.clear() thread-safe?

2006-06-27 Thread Russell Warren
Thanks guys.  This has helped decipher a bit of the Queue mechanics for
me.

Regarding my initial clear method hopes... to be safe, I've
re-organized some things to make this a little easier for me.  I will
still need to clear out junk from the Queue, but I've switched it so
that least I can stop the accumulation of new data in the Queue while
I'm clearing it.  ie: I can just loop on .get until it is empty without
fear of a race, rather than needing a single atomic clear.

My next Queue fun is to maybe provide the ability to stuff things back
on the queue that were previously popped, although I'll probably try
and avoid this, too (maybe with a secondary "oops" buffer).

If curious why I want stuff like this, I've got a case where I'm
collecting data that is being asynchronously streamed in from a piece
of hardware.  Queue is nice because I can just have a collector thread
running and stuffing the Queue while other processing happens on a
different thread.  The incoming data *should* have start and stop
indications within the stream to define segments in the stream, but
stream/timing irregularities can sometimes either cause junk, or cause
you to want to rewind the extraction a bit (eg: in mid stream-assembly
you might realize that a stop condition was missed, but can deduce
where it should have been).  Fun.

Russ

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


BoaConstructor

2006-09-29 Thread Matthew Warren



..I'm just about to 
start a project, I have a threaded python app currently around 3000 lines / 
12-15 source files that is cli driven, and I am about to start using 
boaConstructor to build a GUI around it.
 
Has anyone here any 
advice on wether boaConstructor is actually a good tool for this? The only IDE I 
have used before was netbeans with Java, and not very extensivley. I'm not 
neccesarily into getting a boat-load of bells and whistles with my IDE, to start 
I will be using it generally just as a tool to build a GUI and edit the 
code.
 
eventually, the App 
will become big and complicated.
 
 
Thanks,
 
MattW

 
This email is confidential and may be privileged. If you are not the intended recipient please notify the sender immediately and delete the email from your computer. 
You should not copy the email, use it for any purpose or disclose its contents to any other person.Please note that any views or opinions presented in this email may be personal to the author and do not necessarily represent the views or opinions of Digica.It is the responsibility of the recipient to check this email for the presence of viruses. Digica accepts no liability for any damage caused by any virus transmitted by this email.
UK: Phoenix House, Colliers Way, Nottingham, NG8 6AT UKReception Tel: + 44 (0) 115 977 1177Support Centre: 0845 607 7070Fax: + 44 (0) 115 977 7000http://www.digica.com
SOUTH AFRICA: Building 3, Parc du Cap, Mispel Road, Bellville, 7535, South AfricaTel: + 27 (0) 21 957 4900Fax: + 27 (0) 21 948 3135http://www.digica.com
 
 
This message has been scanned for viruses by BlackSpider in Partnership with Digica
 

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

RE: Problems with Python 2.5 installer.

2006-09-29 Thread Matthew Warren

-Original Message-
From: [EMAIL PROTECTED]
[mailto:[EMAIL PROTECTED] On
Behalf Of paw
Sent: 29 September 2006 11:01
To: python-list@python.org
Subject: Re: Problems with Python 2.5 installer.


John Machin wrote:
> paw wrote:
> > I have ran the MSI installer for Python 2.5 several times attempting
to
> > install to C:
> > Python, however all of the files are placed in C:\ .  The installer
is
> > told to only install files for me, beyond that I have only chosen
the
> > defaults.
>
> What do you mean by "install to C:  Python"? Can you tell us
> (unambiguously, on one line) what directory you chose? Is there any
> good reason why you didn't take the default, which is
> C:\Python25
> ?


Could this happen if c:\python does not exists and creating it fails for
some reason, or if permissions are incorrect? 


This email is confidential and may be privileged. If you are not the intended 
recipient please notify the sender immediately and delete the email from your 
computer. 

You should not copy the email, use it for any purpose or disclose its contents 
to any other person.
Please note that any views or opinions presented in this email may be personal 
to the author and do not necessarily represent the views or opinions of Digica.
It is the responsibility of the recipient to check this email for the presence 
of viruses. Digica accepts no liability for any damage caused by any virus 
transmitted by this email.

UK: Phoenix House, Colliers Way, Nottingham, NG8 6AT UK
Reception Tel: + 44 (0) 115 977 1177
Support Centre: 0845 607 7070
Fax: + 44 (0) 115 977 7000
http://www.digica.com

SOUTH AFRICA: Building 3, Parc du Cap, Mispel Road, Bellville, 7535, South 
Africa
Tel: + 27 (0) 21 957 4900
Fax: + 27 (0) 21 948 3135
http://www.digica.com
-- 
http://mail.python.org/mailman/listinfo/python-list


RE: Making sure script only runs once instance at a time.

2006-09-29 Thread Matthew Warren



 

  
  
  From: 
  [EMAIL PROTECTED] 
  [mailto:[EMAIL PROTECTED] On Behalf 
  Of Hari SekhonSent: 29 September 2006 14:55To: 
  python-list@python.orgSubject: Re: Making sure script only runs 
  once instance at a time.
  
  I'm not sure if that is a very old way of doing it, which is why I was 
  reluctant to do it. My way actually uses the process list of the os (linux) 
  and counts the number of instances. If it is more than 0 then another process 
  is running and the script exits gracefully.Also, apart from the fact 
  the using lockfiles feels a bit 1970s, I have found that in real usage of 
  other programs within the company that use lockfiles, it sometimes causes a 
  bit of troubleshooting time when it stops working due to a stale lockfile. 
  This especially happens when the program is killed, the lockfile remains and 
  causes minor annoyance (to somebody who knows that is, more annoyance to 
  somebody who doesn't). 
   

 
This email is confidential and may be privileged. If you are not the intended recipient please notify the sender immediately and delete the email from your computer. 
You should not copy the email, use it for any purpose or disclose its contents to any other person.Please note that any views or opinions presented in this email may be personal to the author and do not necessarily represent the views or opinions of Digica.It is the responsibility of the recipient to check this email for the presence of viruses. Digica accepts no liability for any damage caused by any virus transmitted by this email.
UK: Phoenix House, Colliers Way, Nottingham, NG8 6AT UKReception Tel: + 44 (0) 115 977 1177Support Centre: 0845 607 7070Fax: + 44 (0) 115 977 7000http://www.digica.com
SOUTH AFRICA: Building 3, Parc du Cap, Mispel Road, Bellville, 7535, South AfricaTel: + 27 (0) 21 957 4900Fax: + 27 (0) 21 948 3135http://www.digica.com
 
 
This message has been scanned for viruses by BlackSpider in Partnership with Digica
 

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

RE: Making sure script only runs once instance at a time.

2006-09-30 Thread Matthew Warren



Apologies for repost. not sure what 
happened.

 
This email is confidential and may be privileged. If you are not the intended recipient please notify the sender immediately and delete the email from your computer. 
You should not copy the email, use it for any purpose or disclose its contents to any other person.Please note that any views or opinions presented in this email may be personal to the author and do not necessarily represent the views or opinions of Digica.It is the responsibility of the recipient to check this email for the presence of viruses. Digica accepts no liability for any damage caused by any virus transmitted by this email.
UK: Phoenix House, Colliers Way, Nottingham, NG8 6AT UKReception Tel: + 44 (0) 115 977 1177Support Centre: 0845 607 7070Fax: + 44 (0) 115 977 7000http://www.digica.com
SOUTH AFRICA: Building 3, Parc du Cap, Mispel Road, Bellville, 7535, South AfricaTel: + 27 (0) 21 957 4900Fax: + 27 (0) 21 948 3135http://www.digica.com
 
 
This message has been scanned for viruses by BlackSpider in Partnership with Digica
 

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

Raw strings and escaping

2006-10-03 Thread Matthew Warren
Hi,

I would expect this to work,

rawstring=r'some things\new things\some other things\'

But it fails as the last backslash escapes the single quote.

..although writing this I think I have solved my own problem. Is \' the
only thing escaped in a raw string so you can place ' in a raw string?
Although I thought the correct thing in that case would be;

rawstring=r"rawstring'with single-quote"


This email is confidential and may be privileged. If you are not the intended 
recipient please notify the sender immediately and delete the email from your 
computer. 

You should not copy the email, use it for any purpose or disclose its contents 
to any other person.
Please note that any views or opinions presented in this email may be personal 
to the author and do not necessarily represent the views or opinions of Digica.
It is the responsibility of the recipient to check this email for the presence 
of viruses. Digica accepts no liability for any damage caused by any virus 
transmitted by this email.

UK: Phoenix House, Colliers Way, Nottingham, NG8 6AT UK
Reception Tel: + 44 (0) 115 977 1177
Support Centre: 0845 607 7070
Fax: + 44 (0) 115 977 7000
http://www.digica.com

SOUTH AFRICA: Building 3, Parc du Cap, Mispel Road, Bellville, 7535, South 
Africa
Tel: + 27 (0) 21 957 4900
Fax: + 27 (0) 21 948 3135
http://www.digica.com
-- 
http://mail.python.org/mailman/listinfo/python-list


RE: Escapeism

2006-10-03 Thread Matthew Warren
 

> -Original Message-
> From: 
> [EMAIL PROTECTED] 
> [mailto:[EMAIL PROTECTED]
> rg] On Behalf Of Kay Schluehr
> Sent: 30 September 2006 18:02
> To: python-list@python.org
> Subject: Re: Escapeism
> 
> Sybren Stuvel wrote:
> > Kay Schluehr enlightened us with:
> > > Usually I struggle a short while with \ and either 
> succeed or give up.
> > > Today I'm in a different mood and don't give up. So here is my
> > > question:
> > >
> > > You have an unknown character string c such as '\n' , 
> '\a' , '\7' etc.
> > >
> > > How do you echo them using print?
> > >
> > > print_str( c ) prints representation '\a' to stdout for c = '\a'
> > > print_str( c ) prints representation '\n' for c = '\n'
> > > ...
> > >
> > > It is required that not a beep or a linebreak shall be printed.
> >
> > try "print repr(c)".
> 
> This yields the hexadecimal representation of the ASCII character and
> does not simply echo the keystrokes '\' and 'a' for '\a' ignoring the
> escape semantics. One way to achieve this naturally is by prefixing
> '\a' with r where r'\a' indicates a "raw" string. But unfortunately
> "rawrification" applies only to string literals and not to string
> objects ( such as c ). I consider creating a table consisting of pairs
> {'\0': r'\0','\1': r'\1',...}  i.e. a handcrafted mapping but maybe
> I've overlooked some simple function or trick that does the same for
> me.
> 
> Kay
> 
> -- 
> http://mail.python.org/mailman/listinfo/python-list
> 
> 
>  
> 


This email is confidential and may be privileged. If you are not the intended 
recipient please notify the sender immediately and delete the email from your 
computer. 

You should not copy the email, use it for any purpose or disclose its contents 
to any other person.
Please note that any views or opinions presented in this email may be personal 
to the author and do not necessarily represent the views or opinions of Digica.
It is the responsibility of the recipient to check this email for the presence 
of viruses. Digica accepts no liability for any damage caused by any virus 
transmitted by this email.

UK: Phoenix House, Colliers Way, Nottingham, NG8 6AT UK
Reception Tel: + 44 (0) 115 977 1177
Support Centre: 0845 607 7070
Fax: + 44 (0) 115 977 7000
http://www.digica.com

SOUTH AFRICA: Building 3, Parc du Cap, Mispel Road, Bellville, 7535, South 
Africa
Tel: + 27 (0) 21 957 4900
Fax: + 27 (0) 21 948 3135
http://www.digica.com
-- 
http://mail.python.org/mailman/listinfo/python-list


RE: Escapeism

2006-10-03 Thread Matthew Warren
 

> -Original Message-
> From: 
> [EMAIL PROTECTED] 
> [mailto:[EMAIL PROTECTED]
> rg] On Behalf Of Matthew Warren
> Sent: 03 October 2006 16:07
> To: python-list@python.org
> Subject: RE: Escapeism
> 
>  
> 
> > -Original Message-
> > From: 
> > [EMAIL PROTECTED] 
> > [mailto:[EMAIL PROTECTED]
> > rg] On Behalf Of Kay Schluehr
> > Sent: 30 September 2006 18:02
> > To: python-list@python.org
> > Subject: Re: Escapeism
> > 
> > Sybren Stuvel wrote:
> > > Kay Schluehr enlightened us with:
> > > > Usually I struggle a short while with \ and either 
> > succeed or give up.
> > > > Today I'm in a different mood and don't give up. So here is my
> > > > question:
> > > >
> > > > You have an unknown character string c such as '\n' , 
> > '\a' , '\7' etc.
> > > >
> > > > How do you echo them using print?
> > > >
> > > > print_str( c ) prints representation '\a' to stdout for c = '\a'
> > > > print_str( c ) prints representation '\n' for c = '\n'
> > > > ...
> > > >
> > > > It is required that not a beep or a linebreak shall be printed.
> > >
> > > try "print repr(c)".
> > 
> > This yields the hexadecimal representation of the ASCII 
> character and
> > does not simply echo the keystrokes '\' and 'a' for '\a' 
> ignoring the
> > escape semantics. One way to achieve this naturally is by prefixing
> > '\a' with r where r'\a' indicates a "raw" string. But unfortunately
> > "rawrification" applies only to string literals and not to string
> > objects ( such as c ). I consider creating a table 
> consisting of pairs
> > {'\0': r'\0','\1': r'\1',...}  i.e. a handcrafted mapping but maybe
> > I've overlooked some simple function or trick that does the same for
> > me.
> > 
> > Kay
> > 

((I have no idea why the following was missing from my first attempt to
post, and since posting the thread has gone over my head, but wasn't the
OP just after a way to print user-inputted strings with '\' in
them?))...

I cant test this where I am, but would the following work

We have our literal user-inputted strings in a list,

l=['\a','\b','\c']etc..

For s in l:
for c in s:
print c,
print



This email is confidential and may be privileged. If you are not the intended 
recipient please notify the sender immediately and delete the email from your 
computer. 

You should not copy the email, use it for any purpose or disclose its contents 
to any other person.
Please note that any views or opinions presented in this email may be personal 
to the author and do not necessarily represent the views or opinions of Digica.
It is the responsibility of the recipient to check this email for the presence 
of viruses. Digica accepts no liability for any damage caused by any virus 
transmitted by this email.

UK: Phoenix House, Colliers Way, Nottingham, NG8 6AT UK
Reception Tel: + 44 (0) 115 977 1177
Support Centre: 0845 607 7070
Fax: + 44 (0) 115 977 7000
http://www.digica.com

SOUTH AFRICA: Building 3, Parc du Cap, Mispel Road, Bellville, 7535, South 
Africa
Tel: + 27 (0) 21 957 4900
Fax: + 27 (0) 21 948 3135
http://www.digica.com
-- 
http://mail.python.org/mailman/listinfo/python-list


Improving telnetlib

2006-10-04 Thread Matthew Warren
 
Hi,

I use telnetlib in an app I am writing, and would like to add
functionality to it to support interactive terminal sessions , IE: be
able to 'vi' a file.

Currently it seems telnetlib isnt quite sophisticated enoguh to support
such a thing.

The trouble is, I havent got a clue where to start and would appreciate
a couple of pointers to get me going...




Thanks,

Matt.


This email is confidential and may be privileged. If you are not the intended 
recipient please notify the sender immediately and delete the email from your 
computer. 

You should not copy the email, use it for any purpose or disclose its contents 
to any other person.
Please note that any views or opinions presented in this email may be personal 
to the author and do not necessarily represent the views or opinions of Digica.
It is the responsibility of the recipient to check this email for the presence 
of viruses. Digica accepts no liability for any damage caused by any virus 
transmitted by this email.

UK: Phoenix House, Colliers Way, Nottingham, NG8 6AT UK
Reception Tel: + 44 (0) 115 977 1177
Support Centre: 0845 607 7070
Fax: + 44 (0) 115 977 7000
http://www.digica.com

SOUTH AFRICA: Building 3, Parc du Cap, Mispel Road, Bellville, 7535, South 
Africa
Tel: + 27 (0) 21 957 4900
Fax: + 27 (0) 21 948 3135
http://www.digica.com
-- 
http://mail.python.org/mailman/listinfo/python-list


RE: Raw strings and escaping

2006-10-04 Thread Matthew Warren
 

> -Original Message-
> From: 
> [EMAIL PROTECTED] 
> [mailto:[EMAIL PROTECTED]
> rg] On Behalf Of Scott David Daniels
> Sent: 03 October 2006 18:11
> To: python-list@python.org
> Subject: Re: Raw strings and escaping
> 
> Matthew Warren wrote:
> > Hi,
> > 
> > I would expect this to work,
> > 
> > rawstring=r'some things\new things\some other things\'
> > 
> > But it fails as the last backslash escapes the single quote.
> 
> Note something many people don't when looking over the string rules:
> 
>   astring = r'some things\new things\some other things' '\\'
> 
> gives you exactly what you want, doesn't imply a string concatenation
> at run time, and various other things.  Two strings in succession are
> concatenated at source translation time.
> 
> 


Thanks for this and the other explanations.

And you hit the nail on the head with this;

>By the way, I renamed the result from being rawstring.  It 
> gives people
> bad intuitions to refer to some strings as "raw" when what you really
> mean is that the notation you are using is a "raw" notation for a
> perfectly normal string.

This is exactly the mistake I had made. I was assuming I was dealing
with 'raw' in this context


Matt.


This email is confidential and may be privileged. If you are not the intended 
recipient please notify the sender immediately and delete the email from your 
computer. 

You should not copy the email, use it for any purpose or disclose its contents 
to any other person.
Please note that any views or opinions presented in this email may be personal 
to the author and do not necessarily represent the views or opinions of Digica.
It is the responsibility of the recipient to check this email for the presence 
of viruses. Digica accepts no liability for any damage caused by any virus 
transmitted by this email.

UK: Phoenix House, Colliers Way, Nottingham, NG8 6AT UK
Reception Tel: + 44 (0) 115 977 1177
Support Centre: 0845 607 7070
Fax: + 44 (0) 115 977 7000
http://www.digica.com

SOUTH AFRICA: Building 3, Parc du Cap, Mispel Road, Bellville, 7535, South 
Africa
Tel: + 27 (0) 21 957 4900
Fax: + 27 (0) 21 948 3135
http://www.digica.com
-- 
http://mail.python.org/mailman/listinfo/python-list


Finding the public callables of self

2006-02-09 Thread Russell Warren
Is there any better way to get a list of the public callables of self
other than this?

myCallables = []
classDir = dir(self)
for s in classDir:
  attr = self.__getattribute__(s)
  if callable(attr) and (not s.startswith("_")):
myCallables.append(s) #collect the names (not funcs)

I don't mean a shorter list comprehension or something that just drops
the line count, but whether or not I need to go at it through dir and
__getattribute__.  This seems a bit convoluted and with python it often
seems there's something already canned to do stuff like this when I do
it.  At first I thought self.__dict__ would do it, but callable methods
seem to be excluded so I had to resort to dir, and deal with the
strings it gives me.

Thanks,
Russ

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


Re: Finding the public callables of self

2006-02-09 Thread Russell Warren
> import inspect
> myCallables = [name for name, value in inspect.getmembers(self) if not
> name.startswith('_') and callable(value)]

Thanks.  I forgot about the inspect module.  Interestingly, you've also
answered my question more than I suspect you know!  Check out the code
for inspect.getmembers():

def getmembers(object, predicate=None):
"""Return all members of an object as (name, value) pairs sorted by
name.
Optionally, only return members that satisfy a given predicate."""
results = []
for key in dir(object):
value = getattr(object, key)
if not predicate or predicate(value):
results.append((key, value))
results.sort()
return results

Seems familiar!  The fact that this is using dir(), getattr(), and
callable() seems to tell me there is no better way to do it.  I guess
my method wasn't as indirect as I thought!

And thanks for the reminder about getattr() instead of
__getattribute__() and other streamlining tips.

Russ

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


Profiling/performance monitoring in win32

2006-02-17 Thread Russell Warren
The application we're working on at my company currently has about
eleventy billion independent python applications/process running and
talking to each other on a win32 platform.  When problems crop up and
we have to drill down to figure out who is to blame and how, we
currently are using the (surprisingly useful) perfmon tool that comes
with Windows.

Perfmon does a pretty decent job, but is pretty raw and sparse on the
usability front.  Does anyone know of any alternative windows (not cpu)
profilers out there?  Commercial packages ok,  Of course one that can
introspect it's way into what a python app is doing would be a bonus
(but truthfully I'm probably just adding that last bit to make this
post a bit more appropriate for c.l.py :) ).

Thanks!

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


Re: Strings for a newbie

2005-05-28 Thread Warren Block
James Hofmann <[EMAIL PROTECTED]> wrote:
> Malcolm Wooden  dtptypes.com> writes:
>> 
>> I'm trying to get my head around Python but seem to be failing miserably. I 
>> use RealBasic on a Mac and find it an absolute dream! But PythonUGH!
>> 
>> I want to put a sentence of words into an array, eg "This is a sentence of 
>> words"
>> 
>> In RB it would be simple:
>> 
>> Dim s as string
>> Dim a(-1) as string
>> Dim i as integer
>> 
>> s = "This is a sentence of words"
>> For i = 1 to CountFields(s," ")
>>   a.append NthField(s," ",i)
>> next
>> 
>> That's it an array a() containing the words of the sentence.

[snip]

> Now a "slim" version:
>
> s = "This is a sentence of words"
> print s.split()

To match the original program, it should be:

s = "This is a sentence of words"
a = s.split()

...assigning the list returned by split() to a variable called a.

-- 
Warren Block * Rapid City, South Dakota * USA
-- 
http://mail.python.org/mailman/listinfo/python-list


RE: c[:]()

2007-05-31 Thread Warren Stringer
> >> c[:] holds many behaviors that change dynamically.
> >
> > I've absolutely no clue what that sentence means.  If c[:] does
> > behave differently than c, then somebody's done something
> > seriously weird and probably needs to be slapped around for
> > felonious overriding.

I'm still a bit new at this, was wondering why c[:]() doesn't work, and
implicitly wondering why it *shouldn't* work. 

> >> So c[:]() -- or the more recent go(c)() -- executes all those
> >> behaviors.

Oops meant to say do(c)(), not "go", which matches a prior post. 

> > Still no clue.
> >
> >> This is very useful for many performers.
> >
> > What are "performers"?

Real people, like musicians, and their proxies in code that passes around
real-time events that may be rendered, recorded, and played back.

> >> The real world example that I'm working one is a collaborative
> >> visual music performance. So c can contain wrapped MIDI events
> >> or sequencer behaviors. c may get passed to a scheduler to
> >> execute those events, or c may get passed to a pickler to
> >> persist the performance.
> >
> > I still don't see how c[:] is any different from c.
> >
> It isn't. The OP is projecting a wish for a function call on a list to
> be interpreted as a call on each member of the list with the same
> arguments. The all-members slice notation is a complete red herring.

Just looked up "red herring wiki" hope I wasn't being misleading -- at least
not intentionally. c[:] is the simplest case for a broad range of behaviors.
Perhaps, I should have said c[selector()]() ???  but, no, that makes the
question more complex ... still 

> It would require a pretty fundamental re-think to give such a construct
> sensible and consistent semantics, I think.

What do you mean?

If c[:]() works, the so does this, using real world names
 
orchestra[:].pickle()
orchestra[conductor()].sequence() 

Though, I'm already starting to prefer:

do(orchestra).pickle() 
do(orchestra(conductor)).sequence()  

Perhaps, this is what you mean by "sensible and consistent semantics"

I just read Alex Martelli's post in the "rats! Varargs" thread about how
list and tupples are implemented. I want to understand implementation before
suggesting changes. Maybe c[:]() isn't so simple to fix, after all?


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


RE: Is PEP-8 a Code or More of a Guideline?

2007-05-31 Thread Warren Stringer
Wildemar Wildenburger wrote:
> This may be a nice
> idea for the Next Overwhelming Programming Escapade (Codename: NOPE) 
> ...
> You may want to elaborate on the "new way to think about names". Maybe
> you have a point which I just don't see.

Is it considered pythonic to LOL?

Nietzsche would love NOPE ... and so would his psychiatrist.
Nope, I'm proposing: "Yo! Extend that Python" (Codename: YEP) 

I'm treating classes as nested dictionaries. Not *all* classes; only the
ones that I want to use and code from a cell phone. 

I've been porting a scripting language, that was written in C++ to Python.
It allows declare a structure like this: 

mouse
position x,y
button
left x,y
right x,y

and pipe the whole tree to a recorder, like this:

record << mouse//

which is like doing this: 

record << [mouse.position.x,
   mouse.position.y, 
   mouse.button.left.x,   
   mouse.button.left.y,
   mouse.button.right.y,
   mouse.button.right.y]

So, how is this related to the addinfourl example? It isn't. At least, not
for rewriting old API. But for fresh APIs, add.info.url() forces you to
think about names, and functionality in a fine grained way. This translates
roughly to:

class add(object)...
class info(object)...
class url(object)...

or as a dict:

{'add' : { 'info': 'url' { ...

Then it becomes easier to think about decorators, generators, and closures
in a way that is quite amorphous. (Though slow?)

Tschüs,

\~/


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


RE: c[:]()

2007-05-31 Thread Warren Stringer
Quotes out of context with mistaken assumptions, now follow:

>  So c[:]() -- or the more recent go(c)() -- executes all those
>  behaviors.
> 
> No it doesn't. See below.
> >
> > If c[:]() works, the so does this, using real world names
> >
> > orchestra[:].pickle()
> > orchestra[conductor()].sequence()
> >
> > Though, I'm already starting to prefer:
> >
> > do(orchestra).pickle()
> > do(orchestra(conductor)).sequence()
> >
> Yes, dammit, but c[:]() *DOESN'T WORK* unless you have made some pretty
> crufty changes to the underlying object.

I started this thread asking why c[:]() doesn't work. 

> This is what I'm having difficulty understanding. You said, in your
> original post (which, by the way, hijacked another thread about
> something completely different):

What?!? I started this thread. 

> > I want to call every object in a tupple, like so:
> >
> [By the way, that's "tuple", not "tupple"]
> > #--
> > def a: print 'a'
> > def b: print 'b'
> > c = (a,b)
> >
>  >>>c[:]()  # i wanna
> >  TypeError: 'tupple' object is not callable
> >
>  >>>c[0]()  # expected
> > a
>  >>>c[:][0] # huh?
> > a

> This is what I just don't believe. And, of course, the use of "tupple"
> above tells us that this *wasn't" just copied and pasted from an
> interactive session.

Try it.
 
>  >>> [i() for i in c] # too long and ...huh?
> > a
> > b
> > [None,None]
> > #--
> This is also clearly made up.

I repeat: try it.

> In a later email you say:
> 
> > why does c[:][0]() work but c[:]() does not?
> 
> The reason for this ...

Stated elsewhere, but thanks

> > Why does c[0]() has exactly the same results as c[:][0]() ?
> 
> The reason for this is that c is exactly the same as c[:]. The slicing
> notation "[:]" tells the interpreter to use a tuple consisting of
> everything in the tuple to which it's applied. Since the interpreter
> knows that tuples are immutable (can't be changed), it just uses the
> same tuple -- since the immutability there's no way that a difference
> could arise between the tuple and a copy of the tuple, Python doesn't
> bother to make a copy.
> 
> This behavior is *not* observed with lists, because lists are mutable.

But neither tupples or lists work, so immutability isn't an issue.

> I realise you are trying to find ways to make Python more productive for
> you, and there's nothing wrong with that. But consider the following,
> which IS copied and pasted:
> 
>  >>> def a():
> ...   print "A"
> ...
>  >>> def b():
> ...   print "B"
> ...
>  >>> c = (a, b)
>  >>> c
> (, )
>  >>> c[:]
> (, )
>  >>> c[0]()
> A
>  >>> c[1]()
> B
>  >>> c()
> Traceback (most recent call last):
>File "", line 1, in 
> TypeError: 'tuple' object is not callable
>  >>> c[:]()
> Traceback (most recent call last):
>File "", line 1, in 
> TypeError: 'tuple' object is not callable
>  >>>

I never said that c() would execute a list nor did I ever say that c[:]()
would execute a list. 

> I think the fundamental mistake you have made is to convince yourself that
> 
>  c[:]()
> 
> is legal Python. It isn't, it never has been.

In summation: 
I started this thread asking why c[:]() wouldn't work
I did not hijack another thread
I posted working examples (with one typo, not quoted here)
I am extremely annoyed by this post

Tis best not to assume what other people are thinking 


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


RE: c[:]()

2007-05-31 Thread Warren Stringer
> > What?!? I started this thread.
> >
> No you didn't. Your original post was a reply to a message whose subject
> line was 'Re: "is" and ==', and included the header
> 
> In-Reply-To: <[EMAIL PROTECTED]>

You're right, thanks.

> >> I think the fundamental mistake you have made is to convince yourself
> that
> >>
> >>  c[:]()
> >>
> >> is legal Python. It isn't, it never has been.

In my opinion, it is better to ask me directly what I think than to assume
what I am thinking. Describing a situation in second person -- using "you"
-- tends to have more interpretation than fact. When the interpretation is
wrong, the person who is at the receiving end of that "you" may be compelled
to reply. Sometimes that correction includes another "you"; interpretation
spawns interpretation -- soon, facts are replaced by projectiles. 

> >
> > In summation:
> > I started this thread asking why c[:]() wouldn't work
> > I did not hijack another thread
> > I posted working examples (with one typo, not quoted here)
> > I am extremely annoyed by this post
> >
> > Tis best not to assume what other people are thinking
> >
> Probably best not to try to help them too, if this is the response. 

I do appreciate your help. Responding on topic helps. Negative feedback
regarding typos helps. And, your statement:

> Perhaps you didn't express yourself too clearly.

makes me feel all warm and fuzzy like ... why thank you! 

> Next
> time you want assistance try to ensure that you copy and paste your
> examples instead of trying to duplicate them from memory.

Agreed. Fortunately, others got the gist of what I was saying.

Oddly enough, as annoying as it was, your post helped my form, while other
posters have helped my content. I bet you'd make a good editor. 

Cheers,

\~/ 

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


RE: c[:]()

2007-05-31 Thread Warren Stringer


> > I did not hijack another thread
> 
> You really did.  In the first message you sent, we see the following
> header:
> 
> > In-Reply-To: <[EMAIL PROTECTED]>
...

Damn! I suck. Outlook as a newsreader sucks. I need to use something else.

> I retyped the code you posted in the first post, and did not get the
> same results as you.  Specifically:
> >>> def a: print 'a'
> SyntaxError: invalid syntax
> >>> def b: print 'b'
> SyntaxError: invalid syntax

> those obviously were not copied from working code.
> 
> >>> c[:]()
> TypeError: 'tuple' object is not callable
> 
> this has the correct spelling of 'tuple' in it.  Your post misspelled it.
> 
> and finally,
> >>> c[0]()
> a
> >>> c[:][0]
> 
> 
> I don't know how you could have gotten c[:][0] to print 'a', but it
> wasn't by running all of the code you presented to us.
> 
> --
> Jerry

They were copied from working code. Copied *badly*? Yes. Running python via:
   Windows -> start -> run -> python 
doesn't allow cut and paste 

Here is what I tested, now cut and past from visual studio IDE 

#
def a(): print 'a'
def b(): print 'b'
c = (a,b) 
c[:]()  # typeError 
c[0]()  # expected
c[:][0]() # huh?
[i() for i in c] # too long and ...huh?
#--

Now, I'm extremely annoyed at my own original post, for misleading typos.

Yeah, Mikael's example is really cool! And I followed his suggestion of
making it cellphone friendly by shorting the name, like so:

#--
class do(list):
def __call__(self,*args,**kwargs):
return [f(*args,**kwargs) for f in self]


def a(): print 'a called'
def b(): print 'b called'
c = do()
c = [a,b]
do(c[:])() 
do(c)()
#--

I prefer that last line, because [:] is very expensive to type from a cell
phone. 

In summary:
Sorry about the hijacked thread
Sorry about the typos
Outlook is an odd newsreader
Mikael's solution works
No PEP needed for this example

Now, if I could only could do this:

do(orchestra(conductor)).play() 

Cheers,

\~/


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


RE: c[:]()

2007-05-31 Thread Warren Stringer
As mentioned a while back, I'm now predisposed towards using `do(c)()`
because square brackets are hard with cell phones. The one mitigating factor
for more general use, outside of cell phones, is speed. If a PEP enables a
much faster solution with c[selector()]() then it may be worthwhile. But, I
feel a bit circumspect about suggesting a change as I haven't looked at
Python source, nor have I looked at the BNF, lately. I think Martelli's
recent post on implantation may be relevant:

> Tuples are implemented as compact arrays of pointer-to-PyObject (so are
> lists, BTW).  So, for example, a 10-items tuple takes 40 bytes (plus a
> small overhead for the header) on a 32-bit build, not 80 as it would if
> implemented as a linked list of (pointer-to-object, pointer-to-next)
> pairs; addressing sometuple[N] is O(1), NOT O(N); etc, etc.

The questions about implementing a working c[:]() are:
1) Does it break anything?
2) Does it slow anything down?
3) Could it speed anything up?
4) Does it make code easier to read?
5) What question(s) did I forget to ask?

1) No? The fact that c() currently fails on a container implies that
enabling it would not break existing client code. Would this break the
language definition? A couple years ago, I hand transcribed the BNF of
version 2.2 to an alternative to BNF. I would love to know if c[:]() would
break the BNF in a fundamental way. 

2) I don't know. I've always assumed that Python objects are hash table
entries. How would this change? Would it change? Does the message
"TypeError: 'list' object is not callable" guarantee a short path between
bytecode and hash table? Is there some other mitigating factor?

3) Maybe? Does overriding __call__ create any extra indirection? If yes,
then I presume that `do(c)()` would be slower the `c[:]()`. I am writing
rather amorphous code. This may speed it up.

4) I posit yes. Am I missing something? What idiom does would c[:]() break?
This correlates with whether `c[:]()` breaks the language definition, in
question 1)


Erik Max Francis wrote:
> Warren Stringer wrote:
> 
> > I'm still a bit new at this, was wondering why c[:]() doesn't work, and
> > implicitly wondering why it *shouldn't* work.
> 
> It does work.  It means "make a sliced copy of `c`, and then call it
> with no arguments."  Functionally that is _no different_ from `c()`,
> which means "take `c` and call it with no arguments," because presuming
> `c` is a list, `c[:]` makes a shallow copy.
> 
> So if you think `c()` and `c[:]()` should do something different in this
> case, you are profoundly confused about Python's semantics of what
> objects are, what it means to shallow copy a list, and what it means to
> make a function call.  That you keep including the slice suggests that
> there's something about its meaning that's not yet clicking.

I use `c[:]()` because it is unambiguous about using a container
 
> If you really want syntax where a function call on a container calls all
> of its elements, then that is trivially easy to do by creating such an
> object and overriding its `__call__` method.
> 
> If you're not willing to do that, but still insisting that `c[:]()`
> makes sense, then perhaps it would be more advisable to learn more about
> Python rather than try to suggest profound changes to the language and
> its conventions.

You're right. At the same time, version 3 is coming up soon. There is a
short window of opportunity for profound changes. 

Also, my audacious suggestion has spawned illuminating replies. For this
instance, I have read about shallow copy, before, but haven't been told
where it is used, before now. Other posts led to insights about closures,
and yielding a real solution. This is incredibly useful. I've learned a lot.

Thanks,

\~/

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


RE: c[:]()

2007-05-31 Thread Warren Stringer
Steve Holden Wrote
> The general rule in Python is that you provide the right objects and
> expect error tracebacks if you do something wrong. So I don't really see
> why you feel it's necessary to "[be] unambiguous about using a
> container" when you don't appear to feel the same about its containing
> only functions.

Give that everything is a first class object

"Look ma! I'm an object"()  # causes
Traceback (most recent call last) ...
TypeError: 'str' object is not callable

def funky(): lookma()
funky()  # causes   

Traceback (most recent call last)...
NameError: global name 'lookma' is not defined

Means that any exception can be caught, thus equal.

`c[:]()` is unambiguous because:

def c(): print 'yo'

c() # works, but
c[:]()  # causes:

Traceback (most recent call last)...
c[:]()  # causes:
TypeError: unsubscriptable object

There are many `c()` to be found in the wild and no `c[:]()`, thus
unambiguous. To be honest, I wasn't sure, until testing this, just now.
 

> > You're right. At the same time, version 3 is coming up soon. There is a
> > short window of opportunity for profound changes.
> >
> Which closed at the end of April as far as PEPs affecting the initial
> implementation of version 3 was concerned. The python3000 and
> python-ideas lists are the correct forum for those issues, though you
> will of course get all sorts of opinions on c.l.py.

Oh well. Perhaps I can relax and actually write functioning code ;-)
What do you mean by 'c.l.py' ? The first thing that comes to mind is
'clippy' that helpful little agent in Word that helped pay for Simonyi's
trip into space.

Ching ching,

Warren


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


RE: c[:]()

2007-06-01 Thread Warren Stringer
> Warren Stringer wrote:
> 
> > `c[:]()` is unambiguous because:
> >
> > def c(): print 'yo'
> >
> > c() # works, but
> > c[:]()  # causes:
> >
> > Traceback (most recent call last)...
> > c[:]()  # causes:
> > TypeError: unsubscriptable object
> >
> > There are many `c()` to be found in the wild and no `c[:]()`, thus
> > unambiguous. To be honest, I wasn't sure, until testing this, just now.
> 
>  >>> c = 'not quite'
>  >>> c[:]
> 'not quite'
>  >>> c[:]()
> Traceback (most recent call last):
>File "", line 1, in ?
> TypeError: 'str' object is not callable


Interesting example. By extension
#-
>>> def c(): print 'yo'
...
>>> c

>>> c(bad)
Traceback (most recent call last):
  File "", line 1, in ?
NameError: name 'bad' is not defined
#-

Both your example and mine show well formed and ill formed statements at the
command line.

> You also seem to be under the impression that `x[:]()` is somehow
> special syntax that is treated differently than `y = x[:]; y()`.  It is
> not.

No; I was under the impression that `x[:]()` didn't work and the reason why
it didn't work wasn't obvious. 

I like your use case. Am I correct in assuming that `y = x[:]; y()` is NOT
to be found in working code? If that is the case, then nothing gets broken.
It would be instructive to have a use case where enabling c[:]() would break
existing code. 

> Besides, _ambiguity_ was never the problem.  _Functionality_ is the
> problem.

Ambiguity is one of many parameters. It was a problem with another poster. I
wish I had McConnel's Code Complete book handy. Let's see ... a search
yields: debugging, testing, performance, portability, design, interfaces,
style, and notation. 

My use case is this:

do(orchestra(score)).pickle()
do(orchestra(conductor)).sequence()

And so now, that I can, I am happy. Life is good. Time to sleep.

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


RE: c[:]()

2007-06-01 Thread Warren Stringer
> Warren Stringer wrote:
> 
> > As mentioned a while back, I'm now predisposed towards using `do(c)()`
> > because square brackets are hard with cell phones. The one mitigating
> factor
> > for more general use, outside of cell phones, is speed.
> 
> The speed at which you can type code is almost _never_ a valid reason to
> make something brief.  Code gains readability from verbosity.  (That can
> be taken too far, of course, but that certainly doesn't apply here.)

There is code that you type which persists and code that you type from a
command line. Two completely different idioms. A credo inside the cell phone
game industry is that you lose half your audience with each menu keystroke.
That's how precious keystrokes are. 

What may be confusing is speaking about both idioms at once. 



> In short, your repeated use of `c[:]()` indicates a fundamental
> misunderstanding about Pythonic style _and_ substance.

Please define Pythonic. Is this like a certain US senator who defined porn
as "I know it when I see it."

28 years ago, I wrote a hierarchical DBMS that used lexical indentation. 15
years ago I wrote a multimedia script language that used lexical indentation
and automatic garbage collection - it was deployed on millons of clients. 2
years ago I hand coded every line of the Python 2.2 BNF into another style
of language description. Up until last month, I had tokenized a subset of
said definition in C++, using templates to manage cardinality. Recently, I
decided to forgo the C++ parser and am now rewriting it in Python. This is a
risk. So, what hoop does one jump though to earn that oh so coveted
"pythonic" merit badge?

Your other post responds with working examples. So, I'll jump over to that
one.   

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


RE: c[:]()

2007-06-01 Thread Warren Stringer
> And that your
> insisting on ``c[:]()`` instead of just ``c()`` seems to indicate you want
> a change that is quite surprising.  It would mean that a slice of a list
> returns an other type with the __call__ method implemented.

I am not insisting on anything. I use ``c[:]()`` as shorthand way of saying
"c() for c in d where d is a container"

Having c() support containers seems obvious to me. It jibes with duck
typing. Perhaps the title of this thread should have been: "Why don't
containers quack?"

A change is surprising only if it breaks something. I still haven't seen any
code that breaks by making such a change. Seeing such code would teach a
great deal.

 > Grok The Zen of Python (``import this`` at the interpreter prompt)?

I think this is incredibly cool.

> Write "pythonic" code?

Probably not yet; I'm still learning - I have yet to perfect my silly walk.
http://grampyshouse.net/cliches/images/sillywalk.jpg 

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


RE: c[:]()

2007-06-01 Thread Warren Stringer
> > [Please quit saying "a container" if you mean lists and tuples.
> > "A container" is way too general.  There most probably _are_
> > containers for which c() does not fail.]
> 
> One example of such a container is any folderish content in Zope:
> subscripting gets you the contained pages, calling renders the default
> view.

Cool. Could you point me to some example code?

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


RE: c[:]()

2007-06-01 Thread Warren Stringer
Thanks, Dakz for taking the time to reply:

> This discussion has gone in more circles than Earth has gone 'round
> the Sun, but it seems you should consider the following:

Yes, I've been feeling a bit dizzy

> 1) Sequences and functions serve fundamentally different purposes in
> Python. One is for containing objects, the other is for executing an
> action. (And yes, I'm aware that these concepts can differ in other
> contexts. But we're talking about Python.)

I guess I've been arguing for fewer semantics, not more
 
> 2) It seems--at least to me--a bit dubious to change an entire general
> purpose programming language to suit a very limited--and frankly
> strange--use case.

Agreed.

> 3) You'd probably be better off making a very simple and concise
> domain specific language.

Hence: http://muse.com/tr3/Tr3%20Overview.pdf 

I'm going to take Steve Holden's advice and hang out at c.l.py 

Cheers,

\~/

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


RE: c[:]()

2007-06-01 Thread Warren Stringer
Gabriel wrote:
> I begin to think you are some kind of Eliza experiment with Python
> pseudo-knowledge injected.

Tell me more about your feelings that I am an Eliza experiment with Python
with pseudo knowledge injected.

Thanks for the code example.

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


RE: c[:]()

2007-06-02 Thread Warren Stringer
Andre Engels wrote:
> > I am not insisting on anything. I use ``c[:]()`` as shorthand way of
> saying
> > "c() for c in d where d is a container"
> >
> > Having c() support containers seems obvious to me. It jibes with duck
> > typing. Perhaps the title of this thread should have been: "Why don't
> > containers quack?"
> >
> > A change is surprising only if it breaks something. I still haven't seen
> any
> > code that breaks by making such a change. Seeing such code would teach a
> > great deal.
> 
> I think it very much bites duck typing. Currently, if I try to execute
> a string or a list or whatever, I get:
> 
> TypeError: 'str' object is not callable
> 
> But under your proposed semantics, suppose a is a list with some
> executable and some non-executable elements. What should
> 
> a()
> 
> now give? It cannot be a TypeError, because a list (in your semantics)
> is callable. Whatever error it gives, and whether or not the preceding
> executables are executed first, it will not be an existing error
> acting the way it normally does ...

Since `a()` translates to `a() for a in b` then the error would be the exact
same `TypeError: 'str' object is not callable`

> - there is no Python error for "you
> cannot do this with this object, but you can do it with other objects
> of the same type". 

Yes there is:

#
def yo(): print "yo"
def no(): print blah
yo()
no()

Starting Python debug run ...
yo
Traceback (most recent call last):...
NameError: global name 'blah' is not defined
#


> And that does not seem to be a case of "We have
> never needed it yet" - the join method seems to have been specifically
> tailored so that no such error is needed.

The join example is pretty cool - I want to study it a bit more - maybe I'm
missing your point? 

There are two domain discussions, here: 

1) is c[:]() is a good idea for python 
2) is c[:]() a good idea in general

Where c[:]() symbolizes [c() for c in a]

In my opinion:

1a) might break something in Python 2.x - 
1b) may or may not be a good idea for Python 3K

2a) is a great idea for a specific range of applications
2b) should attempt to coincide with existing idioms

Pragmatically speaking:

I'm not interested in 1a because it may break something. There may be an
edge case where catching a `TypeError: 'str' object is not callable` may
change the behavior of existing code. 

I am somewhat interested in 1b but intend to lurk on the python.ideas and
python3k lists before entering a discussion. I tend to shy away from
proposing changes from ignorance. This is QUITE DIFFERENT from asking WHY
something works a certain way. Consider people who ask dumb questions, like
me, to be an opportunity to discover what isn't obvious. 

I am intensely interested in 2a. Specifically visual music performances over
the web. I have spent 7 full years and my life's savings on developing a
platform. This particular application will involve millions of simultaneous
events that get passed around as objects. Two of those years have been spent
on developing a domain specific language that needs a very fast `c[:]()` --
which relates to 1b.

I am very interested in 2b. The two most inspiring languages to me are
Python and Occam (a now defunct language Transputers) Both use lexical
indentation. Although I am building a domain specific language I want make
it seamlessly integrate with Python. So that a 12-year-old may start by
patching performances, but ultimately, she may become inspired in writing
python script. 

So, this thread has been of keen interest to me, mostly because of 2b. 1b is
of interest because extending Python with C has historically required
separate distributions between various versions of 2.x  

So, Andre, you and others have a point, in regards to 1a. I find your post
particularly interesting, because it points out some history with join,
which helps me better understand how to approach 2b.

It wasn't my intent to ruffle feathers, but that in its own regard is
instructive, both in the topic, at hand, and in regard to the Python
community. 

Meanwhile, I have written very little code, in the last couple days. It
means that I've taken everyone's suggestions very seriously, often leading
me to blogs and blogs of blogs. Looking back, there have been a couple of
helpful suggestions that I didn't respond to directly. So, for them, my
apologies and thanks!

For now, I am out of here!

Cheers,

\~/

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


RE: c[:]()

2007-06-02 Thread Warren Stringer
Oops, forgot to cut and paste the point, to this:
 
> > - there is no Python error for "you
> > cannot do this with this object, but you can do it with other objects
> > of the same type".
> 
> Yes there is:
> 
> #
> def yo(): print "yo"
> def no(): print blah
> yo()
> no()
> 
> Starting Python debug run ...
> yo
> Traceback (most recent call last):...
> NameError: global name 'blah' is not defined
> #

The point is that if the object is ill formed, then you get a traceback
regardless.  

But, as this is an addendum to the other post, please read that first. 

Now, I really am out of here.

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


RE: c[:]()

2007-06-03 Thread Warren Stringer

> Anyway, the code below defines a simple "callable" list; it just calls
> each contained item in turn. Don't bother to use [:], it won't work.
> 
> py> class CallableList(list):
> ...   def __call__(self):
> ... for item in self:
> ...   item()
> ...
> py> def a(): print "a"
> ...
> py> def b(): return 4
> ...
> py> def c(): pass
> ...
> py> def d():
> ...   global z
> ...   z = 1
> ...
> py> z="A"
> py> x = CallableList([a,b,c,d])
> py> x()
> a
> py> z
> 1
 
I just ran this example. I think the class is simpler than Mikael's example:

class CallableList(list):
def __call__(self,*args,**kwargs):
return [f(*args,**kwargs) for f in self]

def a(): return 'a called'
def b(): return 'b called'
c = CallableList([a,b])()

Though Mikael's returns a list containing all the returns of each item,
which comes in handy for some use cases. So, your examples with Mikael's
CallableList, yields a list [None,4,None,None]

Mikael's shows how such a construct could simplify homogenous lists. Yours
shows how it might obfuscate heterogeneous lists. 

Your example is less of an edge condition than ["string", func], as these
are all funcs. It best shows why supporting a general case of c[:]() might
lead to more obscure code.
 
My use case is very homogenous. I am porting a C++ parsed script that
contain 1000's of calls with no return value. So, I'll probably be using a
cross between your version and Mikael's. 

There is another incentive for a callable list. Much of my script has deep
nested namespaces, like a.b.c.d(). In C++, deep nesting is cheap, but in
python, it is expensive because each dot is, in its own right, a function
call to __getattr__. Collecting all the calls into list preprocesses all of
the dots.

Thanks for the example

> I begin to think you are some kind of Eliza experiment with Python
> pseudo-knowledge injected.

BTW, my favorite Eliza implementation of all time is the one written by
Strout, Eppler, and Higgins ... in Python, of course.

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


RE: c[:]()

2007-06-04 Thread Warren Stringer
> "Marc 'BlackJack' Rintsch" <[EMAIL PROTECTED]> wrote in message
> news:[EMAIL PROTECTED]
> || Warren Stringer wanted to call the functions just for the side effects
> | without interest in the return values.  So building a list of return
> | values which is immediately thrown away is a waste of time and memory.
> 
> Also unnecessary: for f in callables: f()

What do you mean?

This is very relevant to what I need to implement now. I am converting a
domain specific language script into python statements. For debugging the
script gets parsed and generates a .py file. The final bypasses the
intermediate step; instead it parses the script and does something like
this:

code = compile(_call,"ParseCall",'exec')
for coname in code.co_names:
 ... cleanup goes here
exec code in self._dict

I am already worried about speed. There are about 2000 macro statements that
look like this:

demo4:demo.stop()
  ball.smooth()
  video.live() 
  preset.video.straight()
  view.front3d() 
  luma.real() 
  seq[:]lock(1)

In this example, the preprocessor translates the statements into a list of
10 callable objects. That last `seq[:]lock(1)` statement generates 4 objects
on its own. All of the __getattr__ resolution is done in the preprocessor
step. For the sort term version are no return values. For the long term
version, there may be return statements, but prefer simplest, for now.

It sounds like list comprehension may be slower because it builds a list
that never gets used. I'm curious if eval statements are faster than def
statements? Any bytecode experts?
 
Sorry if I got sidetracked on philosophical discussion, earlier. The above
example is lifted from a working c++ version with a tweaked syntax. This is
a real problem that I need to get working in a couple weeks. 

As an aside, the code base will be open source.

Much appreciated,

\~/



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


RE: c[:]()

2007-06-05 Thread Warren Stringer
Roland Puntaier [mailto:[EMAIL PROTECTED]
> Warren, can you please restate your point.

Hey Roland, where were you a few days ago ;-) I think most suggestions were
valid, in their own context. Only yesterday, was I finally able to get it in
perspective, so here goes:

There are two idioms: a domain specific language, called Tr3
http://muse.com/tr3/Tr3%20Overview.pdf , and Python. The idioms both
complement each other and conflict. Most of this thread has explored that
conflict.

Tr3 has three main components: a namespace tree, a directed graph of inputs
and a directed graph of outputs. By analogy: the namespace functions like
XML, inputs function like callbacks, and outputs function like standard
procedures. 

The precursor to Tr3 was written C++. The inputs were script while the
outputs were in C++. So, any changes in procedure required a recompile. The
new version still allows C++ plugins, but the procedures can be written in
Python.

Inputs to Tr3 are queries to other parts of the namespace. By analogy,
inputs behave like an Xpath query. So `a//z` searches for all descendants of
`a` named `z`. The results of a query yields a list that can change state
for the node on the tree which made the query. Another example, more in line
with the title of this thread, is the query  `a[:4]b`, which returns a list
`[a[0].b, a[1].b, a[2].b, a[3].b]`

Outputs are Python. This thread was my way of exploring to what degree that
the `a[:4]b` of inputs could be applied to outputs as `a[:4]b()`. It would
be idiomatic to Tr3 but not as idiomatic to Python. Here's why:

What should this mean?
if a[:4]b():  .

Should it mean:
if a[0].b() and a[1].b() and a[2].b() and a[3].b(): .
or
if a[0].b() or a[1].b() or a[2].b() or a[3].b(): .
or
if a[0].b(): .
if a[1].b(): .
if a[2].b(): .
if a[3].b(): .

Each interpretation could be justified. Thus, the implicit iteration is
ambiguous. 

I am porting code that only uses this form
a[:4]b()

Which translates to:

for i in range(4):  
a[i].b()

There is only one interpretation and thus unambiguous.

I have also explored writing client code that interprets  `a[:4]b` as:
 if a[0].b() and a[1].b() and a[2].b() and a[3].b(): .
A few algorithms, like Huffman codes, wind up looking much simpler, with
implicit iterators. Be that as it may, making such algorithms easier to read
may not be worth the cost of adding ambiguity to a general purpose
procedural language. 

So, you're all right. Relax. Get outdoors. Fall in love. Make someone happy.

And thanks again,

\~/

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


RE: c[:]()

2007-06-06 Thread Warren Stringer
': .' means ': ...' (its an outlook thing)



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


Hooking exceptions outside of call stack

2007-06-09 Thread Warren Stringer
Here is what I would like to do:

#
a = Tr3()  # implements domain specific language
a.b = 1# this works, Tr3 overrides __getattr__
a.__dict__['b'] = 2# just so you know that b is local
a[b] = 3   # I want to resolve locally, but get:

Traceback (most recent call last): ...
exec cmd in globals, locals ...
NameError: global name 'b' is not defined
#

So far, I've tried capturing exceptions in __getattr__, __setitem__, and
__setattr__, with no luck. I do NOT want to put `a[b]=3` inside a
try...except block because I want to enable a command line `>>>a[b]=3` 

Is there a way to hook a NameError exception outside of the call stack?
Perhaps, inside Tr3.__init__ ? Is there a way of putting back unhandled
NameError exceptions, so that they unwind the stack normally?

This is intended for production code.

Many thanks!

Warren

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


RE: Hooking exceptions outside of call stack

2007-06-09 Thread Warren Stringer
Am still trying to hook a NameError exception and continue to run. After a
few more hours of searching the web and pouring over Martelli's book, the
closest I've come is:

>>> import sys
>>> def new_exit(arg=0):
... print 'new_exit called'
... #old_exit(arg)
...
>>> def hook(type, value, tb):
... print 'hook called with', type
... return
...
>>> sys.excepthook = hook
>>> old_exit = sys.exit
>>> sys.exit = new_exit
>>> a[b]=1 # would like to get new_exit called
hook called with exceptions.NameError
>>>
>>> def test():
... sys.exit()
...
>>> test()
new_exit called

The interactive session is different from running in the IDE (am using
ActiveState's Visual Python) which exits just after the `a[b]=1` without
calling hook.  

Am I missing something? Perhaps this is the wrong approach? I want Python to
check a specific set of locals first, before checking globals. For instance:


a.b[c]  

will call: 

a.__getattr__('b')  

but an exception is thrown before: 

a.b.__getitem__(c) 

Is there a way of intervening as `exec cmd in globals, locals` attempts to
translate 'c' into an object? I thought that trapping a NameError might
work. But now, I'm not so sure.

Many thanks,

\~/

> Here is what I would like to do:
> 
> #
> a = Tr3()  # implements domain specific language
> a.b = 1# this works, Tr3 overrides __getattr__
> a.__dict__['b'] = 2# just so you know that b is local
> a[b] = 3   # I want to resolve locally, but get:
> 
> Traceback (most recent call last): ...
> exec cmd in globals, locals ...
> NameError: global name 'b' is not defined
> #
> 
> So far, I've tried capturing exceptions in __getattr__, __setitem__, and
> __setattr__, with no luck. I do NOT want to put `a[b]=3` inside a
> try...except block because I want to enable a command line `>>>a[b]=3`
> 
> Is there a way to hook a NameError exception outside of the call stack?
> Perhaps, inside Tr3.__init__ ? Is there a way of putting back unhandled
> NameError exceptions, so that they unwind the stack normally?
> 
> This is intended for production code.


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


RE: Hooking exceptions outside of call stack

2007-06-09 Thread Warren Stringer
> Yes.  Python doesn't have restartable exceptions.  Perhaps you would like
> to take a look at CL or Smalltalk?
> 
> Jean-Paul

Hmmm, I wonder if anyone suggest to Philippe Petit, as stepped out 110
stories off the ground, that perhaps he would like to take a look at a
different tightrope? 

Oddly enough, you suggestion regarding "restartable exceptions" turn up a
solution:
http://www.chneukirchen.org/blog/archive/2005/03/restartable-exceptions.html


Oops, wrong language.

\~/

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


RE: Ableton Live Python API is out!

2007-06-09 Thread Warren Stringer
Alia Khouri Write
> I have been waiting for this ages and it's finally happened! Python
> meet Live, Live meet Python!

Wow. This is very cool; thanks for the announcement!
 
> I rushed to update  http://wiki.python.org/moin/PythonInMusic but lo

Thanks for this link, as well. Very useful.  


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


RE: Hooking exceptions outside of call stack

2007-06-09 Thread Warren Stringer
Josiah Carlson wrote:
>  >>> foo = type(foo)(foo.func_code, d, foo.func_name, foo.func_defaults,
> foo.func_closure)

Wow! I've never seen that, before. Is there documentation for `type(n)(...)`
somewhere? I did find a very useful "Decorator for Binding Constants, by
Raymond Hettinger", that uses this technique.

The calls are made from embedded classes that are constructed on the fly:

  class a(object): ...
class b(object): ...
   class c(object): ...

for `a.b[c]=1`, a.__getattr__('b') is called but 'c' need to be resolved as
an object before a.b.__getitem__(c) is called. Could a.b.__init__ set
globals so that 'c' gets resolved? Is this global namespace the same as the
`globals()` namespace? 

Cheers,

\~/

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


  1   2   >