Re: Recursive generator

2008-02-13 Thread Erich
On Feb 12, 5:15 am, Ben C <[EMAIL PROTECTED]> wrote:
> I think this works OK, but it seems a bit odd. Is there something more
> "Pythonic" I should be doing?

I have a similar tree to the one you describe here at work. I have a
visit function that is very similar to yours, but takes function
arguments for doing pre- and/or post- order operations on the node
(code below). It also yeilds the nodes, but in a postorder manner. The
flexibility is quite useful.

Regards,
Erich

def visit(self,prefunc = None, postfunc = None):
if prefunc:
prefunc(self)

for child in self.children:
for y in child.visit(prefunc, postfunc):
yield y

if postfunc:
postfunc(self)

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


Generator woes

2008-03-12 Thread Erich
Hi all,

I am trying to get the following generator to work to these goals:

1. When it recieves an exception (via a throw()) it yeilds the value
of handler.remaining. Otherwise it yeilds None.
2. Send adds data to the generator.

Goal 2 is working great. Goal 1 on the other hand, is not working. The
result of a throw is always None.

Any reasons why this does not work as I expect? If not, what is wrong?

Code:
def make_handler():
def handler():
eol = '\r\n'

handler.remaining = 1
response = ''
data = ''

while not response.endswith(eol):
trv = None
try:
ndata = (yield trv)
if ndata: response += ndata
trv = None
except:
trv = handler.remaining
response = response.strip()
yield response * 2
res = handler()
res.next()
return res

x = make_handler()
y = x.send('a')
print 'y (should be None):',y
y = x.send('b')
print 'y (should be None):',y
y = x.throw(Exception)
print 'y (should be 1):',y
y = x.send('c\r\n')
print 'y (should be abcabc):',y

Output:
y (should be None): None
y (should be None): None
y (should be 1): None
y (should be abcabc): abcabc

Thanks,
Erich
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Generator woes

2008-03-12 Thread Erich
On Mar 13, 12:33 am, Erich <[EMAIL PROTECTED]> wrote:
> Hi all,
>
> I am trying to get the following generator to work to these goals:
>
> 1. When it recieves an exception (via a throw()) it yeilds the value
> of handler.remaining. Otherwise it yeilds None.
> 2. Send adds data to the generator.
>
> Goal 2 is working great. Goal 1 on the other hand, is not working. The
> result of a throw is always None.
>
> Any reasons why this does not work as I expect? If not, what is wrong?
>
> Code:
> def make_handler():
> def handler():
> eol = '\r\n'
>
> handler.remaining = 1
> response = ''
> data = ''
>
> while not response.endswith(eol):
> trv = None
> try:
> ndata = (yield trv)
> if ndata: response += ndata
> trv = None
> except:
> trv = handler.remaining
> response = response.strip()
> yield response * 2
> res = handler()
> res.next()
> return res
>
> x = make_handler()
> y = x.send('a')
> print 'y (should be None):',y
> y = x.send('b')
> print 'y (should be None):',y
> y = x.throw(Exception)
> print 'y (should be 1):',y
> y = x.send('c\r\n')
> print 'y (should be abcabc):',y
>
> Output:
> y (should be None): None
> y (should be None): None
> y (should be 1): None
> y (should be abcabc): abcabc
>
> Thanks,
> Erich

Never mind its obviously a case of "I need to look foolish before I
can see the simple error". Im going to blush and hide now.

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


Re: Pycon disappointment

2008-03-20 Thread Erich
On Mar 20, 12:39 pm, Ed Leafe <[EMAIL PROTECTED]> wrote:
> On Mar 20, 2008, at 11:54 AM, [EMAIL PROTECTED] wrote:
>
> > Number Three:  Too much code, not enough concept.
>
> > Presenters this one's for you.  I can't count the number of
> > presentations I attended where the presenter would click through three
> > slides of pycode just to show us a two or three-line snippet that
> > illustrated their point.  Worse yet, it was often at the bottom of the
> > screen so no one but the front row could see it.  This goes for text
> > two.  I saw some great presentations as well, and they had limited
> > text on each slide.  The last thing your audience wants to see is a
> > slide drenched in text of any kind.
>
> This is good advice: simple slides serve as organization cues, but
> the content should come from the speaker. The worst case (only saw
> this twice at this year's PyCon) is when there is a text-heavy slide
> that the presenter simply reads. We can all read it ourselves! Your
> job is to elaborate on the topic.
>
> I'd like to see two things regarding slides: first, if at all
> possible, set a limit on the percentage of the talk that can consist
> of slides. I would much rather see the presenter show actual
> demonstrations of what they're talking about than simply talking about
> it. If that's not possible, then in the session description, clearly
> state the % of the talk that will be slides. Perhaps there are people
> who like to sit in a room and watch long PowerPoint (-type)
> presentations, but I'm not one of them. Let's see some code! Let's see
> stuff working (and sometimes crashing!), and how changes affect the
> results. When I've presented at PyCon and other conferences, that's
> the part that I spend the most time on: preparing demonstrations. It's
> not easy to do; certainly much more difficult than creating a slide
> that sums up what the demo does. But it makes for a much more
> interesting session!
>
> -- Ed Leafe

I'd like to see code listings made available to download where
appropriate. That way the slides dont have much hard to read content,
and we can look at the bits of code we find tricky as we see fit. And
if we get bored with bits, we can play with code!

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


Re: Commercial Products in Python

2008-10-21 Thread Erich
On Oct 21, 12:50 pm, "Paulo J. Matos" <[EMAIL PROTECTED]> wrote:
> Hi all,
>
> I was just wondering, if you wish to commercialize an application
> developed in Python, what's the way to go?
> I guess the only way is to sell the source, right?
>

If this application is a Windows app, and can use .Net, you may wish
to look at how Resolver Systems [1] did it. Their products are written
in IronPython, a python implementation for  .Net.

1. http://www.resolversystems.com/products/

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


Re: Learning Python for no reason

2008-05-12 Thread Erich
On May 12, 12:27 pm, "John Salerno" <[EMAIL PROTECTED]> wrote:

> The *process* of learning is enough fun for me, and every now and then I do
> find a small use for Python that really pays off, but for the most part I'm
> wondering what people's thoughts are as far as simply learning it for the
> sake of learning. Does it seem like a silly endeavor to most people? Did
> anyone here learn a programming language when you didn't need to? If so, how
> much and in what capacity did you use it after you learned it?
>

I am of the belief that there is no such thing as "useless learning",
or "bad learning"*. I have found that the more I learn about anything,
the better I am at everything. I think this is because more knowledge/
understanding does:
1. gives me more 'entry points' for new knowledge, I can relate more
bits to something I know, making the whole learning process easier.
2. allows me to better relate to people who I need to communicate
with, becuase it is more likely there is a common point of knowledge/
interest to build from
3. gives me personal satisfaction in my life (self-actualization).

When I learned python, I was a bartender, and was just learning it for
fun. Only later did I become a computer programmer. I was lucky enough
to find a job where I get to do a lot of my work in python.

* There are times when learning new info set A is more productive than
new info set B, depending on other constraints of time, energy, money,
etc, B could be classified as "unwise learning"

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


Re: Python is slow

2008-05-22 Thread Erich
On May 22, 1:18 pm, Arnaud Delobelle <[EMAIL PROTECTED]> wrote:

> My mum's fast...

> Arnaud

Was it a good idea to include that bit in a troll response?

If English isn't your first language, it's one of those language
idioms that's not very nice to say about someone's mother (especially
your own mother).

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


Re: Self-referencing decorator function parameters

2008-04-02 Thread Erich
On Apr 2, 10:13 am, Thomas Dimson <[EMAIL PROTECTED]> wrote:
>
> I guess my real question is: why does wrapping the call to be
> "call=lambda x: DecorateMe.callMe(x)" somehow fix the issue with this
> temporary namespace? It seems strange to me that defining an
> additional function (through lambda) would allow me to see/add more
> members to the namespace.

This works because of the very same mechanism that allows decorators
to work.  When the decorator in your example is called, it then
evaluates the inner statements, including the creation of the function
inner (the def ...).  Similarly, when you do the lambda above, python
points call to the lambda function, but does not evaluate it until
later, when the youBet method is run. By the time youBet is run, the
DecorateMe class exists, so this will properly evaluate.

I hope the above is clear, I haven't had my coffee yet.

regards,
Erich
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Google App Engine

2008-04-09 Thread Erich
On Apr 9, 3:51 am, Duncan Booth <[EMAIL PROTECTED]> wrote:

> The backend data store, while it has a vaguely SQLish query language is an
> object database not a relational database, i.e. more like ZODB than MySQL.
> It uses similar concepts to django's data api but isn't the same. It should
> be possible to write something simple to replace it, but given that you
> only need to migrate away from Google when your data or page hits get
> large, you need to replace it with something scalable.

I have a bet with a coworker that someone will implement the api, in a
way that makes migration away from Google easy, within a month.
(actually, the bet is just that there will be a project with this
goal). The reason I mention it here, is that he had the same comments
as you regarding this. My thinking is that (if past experiences can be
used as a yardstick) the python community will see this deficiency and
work to correct it.  As a result of that thinking, I am fairly certain
that this concern is not a big concern.  The authentication thing,
thats a bit different.
-- 
http://mail.python.org/mailman/listinfo/python-list


Recurring patterns: Am I missing it, or can we get these added to the language?

2008-04-15 Thread Erich
Hello all,

Today I found myself once again defining two functions that I use all
the time: nsplit and iterable.  These little helper functions of mine
get used all the time when I work. Im sick of having to define them
(but am very good at it these days, less than 1 typo per function!).
It leads me to the following questions

1. Is this functionality already built in and im just missing it
2. Is there some well known, good technique for these that I missed?
3. Insert question I need to ask here (with a response)

These are the funtions w/ explaination:

def nsplit(s,p,n):
n -= 1
l = s.split(p, n)
if len(l) < n:
l.extend([''] * (n - len(l)))
return l

This is like split() but returns a list of exactly lenght n. This is
very useful when using unpacking, e.g.:
x, y = nsplit('foo,bar,baz', ',', 2)

def iterable(item, count_str=False):
if not count_str and isinstance(item, str):
return False
try:
iter(item)
except:
return False
return True
This is just simple boolean test for whether or not an object is
iterable. I would like to see this in builtins, to mirror callable.
The optional count_str adds flexibility for string handling, since
sometimes I need to iterate over a string, but usually not. I
frequently use it to simplify my case handling in this type of
costruct:

def foo(bar):
bar = bar if iterable(bar) else [bar]
for x in bar:
    

Thanks for feeback,
Erich
-- 
http://mail.python.org/mailman/listinfo/python-list


Premature wakeup of time.sleep()

2005-09-12 Thread Erich Schreiber
In the Python Library Reference the explanation of the time.sleep()
function reads amongst others:

> The actual suspension time may be less than that requested because 
> any caught signal will terminate the sleep() following execution 
> of that signal's catching routine. Also, the suspension time may 
> be longer than requested by an arbitrary amount because of the 
> scheduling of other activity in the system.

I don't understand the first part of this passage with the premature
wakeup. What signals would that be?

I've written a script that tries to bench the responsiveness of a
virtual Linux server. My script sleeps for a random time and on waking
up calculates the difference of the proposed and actual wakeup time.

The essential code fragment is

while True:
ts = tDelay()
t1 = time.time()
time.sleep(ts)
t2 = time.time()
twu = str(datetime.datetime.utcfromtimestamp(t1 + ts))
logResults(LOGFILE, twu, ts, int((t2-t1-ts)*1000))

Whereas tDelay() returns a (logarithmically) randomly distributed real
number in the range [0.01, 1200] which causes the process to sleep
from 10 ms to 20 minutes.

In the logs I see a about 1% of the wake-up delays beeing negative
from -1ms to about -20ms somewhat correlated with the duration of the
sleep. 20 minute sleeps tend to wake-up earlier then sub-second
sleeps. Can somebody explain this to me? 

Regards,

Erich Schreiber

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


Re: Premature wakeup of time.sleep()

2005-09-21 Thread Erich Schreiber
Thank you every body for your comments.

Especially Jeff Epler for your hint about NTP. You're right. I see a
(constant but somewhat huge)time drift of about 1 ms/min which I can
correct for.

Thank you Steve Horsley for the clarification of the interrupts that
would end the delay. I didn't think of something as trivial as ^C.

And thank you Nick Craig-Wood for the shell script to get hands on the
HZ value. I had to modify it a little as I do not have
/proc/interrupts, but I've got the idea. I measure about 100 and so I
have a 10 ms granularity in my VPS as you predicted.

Erich


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


Re: concatenating strings

2006-12-15 Thread Erich Pul
thank you, i just plainly overlooked it ; )

now it works

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