Re: It's ...

2009-06-25 Thread Kirk Strauser
At 2009-06-24T19:53:49Z, Angus Rodgers twir...@bigfoot.com writes:

 stop = 3   # Tab stops every 3 characters
 from types import StringType   # Is this awkwardness necessary?
 detab = lambda s : StringType.expandtabs(s, stop)  # Or use def
 f = open('h071.txt')   # Do some stuff to f, perhaps, and then:
 f.seek(0)
 print ''.join(map(detab, f.xreadlines()))
 f.close()

An equivalent in modern Pythons:

 print ''.join(line.expandtabs(3) for line in file('h071.txt'))

In short: expandtabs is a method on strings, there's no need to seek to the
beginning, and files are closed when they are garbage collected (although I
can't make myself not close output files after years of doing so), and map()
is largely deprecated in favor of list comprehensions and generator
functions.
-- 
Kirk Strauser
The Day Companies
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: tricky nested list unpacking problem

2008-12-15 Thread Kirk Strauser
At 2008-12-15T19:06:16Z, Reckoner recko...@gmail.com writes:

 The problem is that I don't know ahead of time how many lists there are or
 how deep they go. In other words, you could have:

Recursion is your friend.

Write a function to unpack one sublist and call itself again with the new
list.  For instance, something like:

def unpack(pattern):
# Find the first subpattern to replace
# [...]
results = []
for number in subpattern:
results.append(pattern.replace(subpattern, number))
return results

Calling unpack([1,2,3,[5,6],[7,8,9]]) would look cause it to call
unpack([1,2,3,5,[7,8,9]]) and unpack([1,2,3,6,[7,8,9]]), compile the
results, and return them.
-- 
Kirk Strauser
The Day Companies
--
http://mail.python.org/mailman/listinfo/python-list


Re: tricky nested list unpacking problem

2008-12-15 Thread Kirk Strauser
At 2008-12-15T20:03:14Z, Chris Rebert c...@rebertia.com writes:

 You just need a recursive list-flattening function. There are many
 recipes for these. Here's mine:

 flattened = flatten([1,2,3,[5,6,[10, 11]],7,[9,[1, 2, 3, 4, 5 ]]])
 flattened
 [1, 2, 3, 5, 6, 10, 11, 7, 9, 1, 2, 3, 4, 5]
 '-'.join(str(num) for num in flattened)
 '1-2-3-5-6-10-11-7-9-1-2-3-4-5'

He doesn't want to flatten them directly.  He's using [1,2,3] sort of like a
regular expression, so that 1,[2,3],4 means 1,2,4 or 1,3,4, not
1,2,3,4.
-- 
Kirk Strauser
The Day Companies
--
http://mail.python.org/mailman/listinfo/python-list


Re: newbie question: if var1 == var2:

2008-12-12 Thread Kirk Strauser
At 2008-12-12T15:35:11Z, J. Cliff Dyer j...@sdf.lonestar.org writes:

 Python has a version equally good:

 def chomp(s):
 return s.rstrip('\r\n')

 You'll hardly miss Perl at all. ;)

I haven't missed Perl in years!  I just wish there was a basestring.stripeol
method because I seem to end up writing the inline version of chomp every
time I iterate across a file.
-- 
Kirk Strauser
The Day Companies
--
http://mail.python.org/mailman/listinfo/python-list


Re: Removing None objects from a sequence

2008-12-12 Thread Kirk Strauser
At 2008-12-12T15:51:15Z, Marco Mariani ma...@sferacarta.com writes:

 Filip GruszczyƄski wrote:

 I am not doing it, because I need it. I can as well use if not elem
 is None,

 I suggest if elem is not None, which is not quite the same.

So what's the difference exactly?  foo is not None is actually surprising
to me, since not None is True.  0 is True is False, but 0 is not None
is True.  Why is that?
-- 
Kirk Strauser
The Day Companies
--
http://mail.python.org/mailman/listinfo/python-list


Re: (Very Newbie) Problems defining a variable

2008-12-12 Thread Kirk Strauser
At 2008-12-12T18:12:39Z, Tim Rowe digi...@gmail.com writes:

 Is there a tidy way of making rates and thresholds local to get_rate,
 without recalculating each time? I suppose going object oriented is
 the proper way.

 #Py3k,UTF-8

 rates = {0: 0.006, 1: 0.0085, 25000: 0.0124, 5: 0.0149, 10: 
 0.0173}
 thresholds = list(rates.keys())
 thresholds.sort()
 thresholds.reverse()

 def get_rate(balance):
 for threshold in thresholds:
 if balance = threshold:
 return rates[threshold]
 else:
 return 0.0

How 'bout:

def get_rate(balance):
for threshold, rate in ((10, .0173),
(5, .0149),
(25000, .0124),
(1, .0085),
(0, .006)):
if balance  threshold:
return rate
return .0
-- 
Kirk Strauser
The Day Companies
--
http://mail.python.org/mailman/listinfo/python-list


Re: (Very Newbie) Problems defining a variable

2008-12-12 Thread Kirk Strauser
At 2008-12-12T19:20:52Z, John Machin sjmac...@lexicon.net writes:

 (1) you meant if balance  threshold:

balance = threshold.  We both mistyped.  :-)

 (2) sequential search can be very fast if the sequence is in
 descending order of probability of occurence ... you might like to
 consider reversing the order

Actually, I just wanted to point out a simplified version of the exact same
algorithm.  Given enough RAM and if the speed was indeed critical, you could
turn that into a tuple of interest rates and jump straight to rate[balance]
in O(1).
-- 
Kirk Strauser
The Day Companies
--
http://mail.python.org/mailman/listinfo/python-list


Re: newbie question: if var1 == var2:

2008-12-11 Thread Kirk Strauser
At 2008-11-29T04:02:11Z, Mel [EMAIL PROTECTED] writes:

 You could try

 for item in fname:
 item = item.strip()

This is one case where I really miss Perl's chomp function.  It removes a
trailing newline and nothing else, so you don't have to worry about losing
leading or trailing spaces if those are important to you.
-- 
Kirk Strauser
The Day Companies
--
http://mail.python.org/mailman/listinfo/python-list


Re: Do more imported objects affect performance

2008-12-11 Thread Kirk Strauser
At 2008-12-01T11:30:44Z, Nick Craig-Wood [EMAIL PROTECTED] writes:

 Importing the module is actualy slower...  If you import the name into
 your namespace then there is only one lookup to do.  If you import the
 module there are two.

Note that if you're importing the entire module but want to call a function
that many times, you can do something like:

import timeit
Timer = timeit.Timer
for _ in xrange(100):
Timer
-- 
Kirk Strauser
The Day Companies
--
http://mail.python.org/mailman/listinfo/python-list


Re: newbie question: if var1 == var2:

2008-12-11 Thread Kirk Strauser
At 2008-12-11T17:24:44Z, rdmur...@bitdance.com writes:

  '  ab c  \r\n'.rstrip('\r\n')
 '  ab c  '
  '  ab c  \n'.rstrip('\r\n')
 '  ab c  '
  '  ab c  '.rstrip('\r\n')
 '  ab c  '

I didn't say it couldn't be done.  I just like the Perl version better.
-- 
Kirk Strauser
The Day Companies
--
http://mail.python.org/mailman/listinfo/python-list


Re: newbie question: if var1 == var2:

2008-12-11 Thread Kirk Strauser
At 2008-12-11T19:49:23Z, Steve Holden st...@holdenweb.com writes:

 ... and it's so hard to write

  item = item[:-1]

It's easy - and broken.  Bad things happen if you're using something other
than '\n' for EOL.
-- 
Kirk Strauser
The Day Companies
--
http://mail.python.org/mailman/listinfo/python-list


Re: Python and Its Libraries--Who's on First?

2008-11-18 Thread Kirk Strauser
At 2008-11-17T11:44:00Z, W. eWatson [EMAIL PROTECTED] writes:

 See the post by Chris R.

In general, it is incumbent on the asker to provide additional information
as needed, rather than being the job of the would-be answerer to go
searching for it.
-- 
Kirk Strauser
--
http://mail.python.org/mailman/listinfo/python-list


Re: Printing with interspersed element

2008-11-06 Thread Kirk Strauser
At 2008-10-30T21:10:09Z, Paulo J. Matos [EMAIL PROTECTED] writes:

 Thanks for the tip but that has an issue when dealing with potentially
 millions of objects. You are creating a string in memory to then dump
 to a file [or screen] while you could dump to the file [or screen] as
 you go through the original string. Right?

How about:

def alternator(lst, sep):
for index, item in enumerate(lst):
if index:
yield sep
yield item

for item in alternator(list_of_objects, 10):
print item,

-- 
Kirk Strauser
The Day Companies
--
http://mail.python.org/mailman/listinfo/python-list


Re: Weird behavior with lexical scope

2008-11-06 Thread Kirk Strauser
At 2008-11-06T16:57:39Z, mrstevegross [EMAIL PROTECTED] writes:

 class Outer:
   class Inner:
 def __init__(self):
   pass
   def __init__ (self):
 a = Inner()
 Outer()

Try instead:

class Outer:
def __init__(self):
a = self.Inner()


-- 
Kirk Strauser
The Day Companies
--
http://mail.python.org/mailman/listinfo/python-list


Re: Python/Numeric users be aware!

2008-10-29 Thread Kirk Strauser
At 2008-10-29T17:53:43Z, Benyang [EMAIL PROTECTED] writes:

 It is totally screwed up on 64-bit linux machines:
 [1 1 1 1 1 1 1 1 1 1]

And on 64-bit FreeBSD machines.
-- 
Kirk Strauser
The Day Companies
--
http://mail.python.org/mailman/listinfo/python-list


Re: Question about scope

2008-10-28 Thread Kirk Strauser
At 2008-10-24T01:08:12Z, Pat [EMAIL PROTECTED] writes:

 --- myGlobals.py file:

 class myGlobals():
 remote_device_enabled = bool

 --- my initialize.py file:

 from myGlobals import *
 def initialize():
 myGlobals.remote_device_enabled = True

 --- my main.py file:

 import from myGlobals import *
 RDE =  myGlobals.remote_device_enabled

 def main():
 if RDE:# this will not give me the correct value
 process_device()

If you *really* want to organize your settings like this, may I suggest:

--- myGlobals.py file:

# This does nothing more than create a placeholder
remote_device_enabled = None

--- my initialize.py file:

import myGlobals
def initialize():
myGlobals.remote_device_enabled = True

--- my main.py file:

import myGlobals
import initialize
initialize.initialize()

def main():
if myGlobals.remote_device_enabled:
process_device()


-- 
Kirk Strauser
The Day Companies
--
http://mail.python.org/mailman/listinfo/python-list


Re: Python barcode decoding

2008-10-28 Thread Kirk Strauser
At 2008-10-24T17:05:25Z, Robocop [EMAIL PROTECTED] writes:

 Does anyone know of any decent (open source or commercial) python
 barcode recognition tools or libraries.  I need to read barcodes from
 pdfs or images, so it will involve some OCR algorithm.  I also only
 need to read the code 93 symbology, so it doesn't have to be very
 fancy.  The most important thing to me is that it outputs in some
 python friendly way, or ideally that it is written in python.  Any
 tips would be great!

How precise are these images?  I mean, are they computer-generated, or
scanned in from other material?

If they're nice and crisp, I wrote a simple and pretty quick decoder at 
http://pypi.python.org/pypi/Daycos/1.0 .  Look in the
Daycos.Imageproc.Barcode module.
-- 
Kirk Strauser
The Day Companies
--
http://mail.python.org/mailman/listinfo/python-list


Re: Append a new value to dict

2008-10-13 Thread Kirk Strauser
At 2008-10-13T13:14:15Z, [EMAIL PROTECTED] writes:

 jdd:
 foo = {'bar': 'baz'}
 foo.update({'quux': 'blah'})

 That creates a new dict, to throw it away. Don't do that.

I use that if I'm changing many values at once, eg:

foo.update({
'quux': 'blah',
'baz' : 'bearophile',
'jdd' : 'dict',
})

instead of:

foo['quux'] = 'blah'
foo['baz'] = 'bearophile'
foo['jdd'] = 'dict'

because it seems to more clearly indicate what I'm doing, and has fewer
opportunities for types.  Still, there is a performance penalty.  Running
my way 10,000,000 times took 8.7s, and your way only took 4.7s.  If
you're doing this in an inner loop, that may be significant.  While we're on
the subject, use keyword arguments to dict like:

foo.update(dict(quux='blah', baz='bearophile', jdd='dict'))

was *much* slower, at 11.8s.
-- 
Kirk Strauser
The Day Companies
--
http://mail.python.org/mailman/listinfo/python-list


Re: Suggestion for the PythonDevelopment for next version

2008-10-13 Thread Kirk Strauser
At 2008-10-13T16:11:26Z, azrael [EMAIL PROTECTED] writes:

 I know that. enumerate is a great function. But this way it always
 adds some complexity. I think that it is more better to give a man a
 better tool then to let him play with a not so good one. People like
 Python because of his simplicity in comparison with c++. Maybe People
 would like him even more it would be a bit more simple but the same
 powerfull.

enumerate doesn't add complexity: it adds specificity.  When you use it,
there is no question of intent.  You are saying, explicitly, that you're
going to want to use the index.

Your way adds an enormous amount of complexity in that it's magic behavior
with no obviously good implementation.  What if the objects in my list
already have an attribute named index and I don't want your loop
contstruct to overwrite it?  What if I never use enumerate as it is and
don't want to pay for the extra overhead of modify every object I'll ever
get from an iterator?

Thanks, but no.  One of the things I love about Python is its lack of magic.
-- 
Kirk Strauser
The Day Companies
--
http://mail.python.org/mailman/listinfo/python-list


Re: RegExp: wontmatch-function

2008-10-13 Thread Kirk Strauser
At 2008-10-13T16:40:07Z, [EMAIL PROTECTED] writes:

def nomatch(value):
return not(value == '' or pattern.match(value))

-- 
Kirk Strauser
The Day Companies
--
http://mail.python.org/mailman/listinfo/python-list


Re: More regex help

2008-09-24 Thread Kirk Strauser
At 2008-09-24T16:25:02Z, Support Desk [EMAIL PROTECTED] writes:

 I am working on a python webcrawler, that will extract all links from an
 html page, and add them to a queue, The problem I am having is building
 absolute links from relative links, as there are so many different types of
 relative links. If I just append the relative links to the current url, some
 websites will send it into a never-ending loop. 

 import urllib
 urllib.basejoin('http://www.example.com/path/to/deep/page',
'/foo')
'http://www.example.com/foo'
 urllib.basejoin('http://www.example.com/path/to/deep/page',
'http://slashdot.org/foo')
'http://slashdot.org/foo'

-- 
Kirk Strauser
The Day Companies
--
http://mail.python.org/mailman/listinfo/python-list


Suggestions for creating a PDF table

2008-07-28 Thread Kirk Strauser
Short question:

Is there a good library for generating HTML-style tables with the equivalent
of colspans, automatically sized columns, etc. that can render directly to
PDF?

Longer question:

I'm re-doing a big chunk of locally-written code.  I have a
report-generating function that takes a list of lists of lists as input and
returns either a PDF, an HTML table, or an Excel spreadsheet as requested. 
For example, input might look like:

makereport('html',
   headers=['Invoice number', 'Customer', 'Price'],
   data=[
 [['123', 'John Doe', '$50.00'],
  ['Ordered on 2008-01-01 via the website']],
 [['124', 'Peter Bilt', '$25.99'],
  ['Mail via African swallow']]
   ])

This would result in HTML like:

table
  tr
thInvoice number/th
thCustomer/th
thPrice/th
  /tr
  tr class=lightbackground
td123/td
tdJohn Doe/td
td$50.00/td
  /tr
  tr class=lightbackground
td colspan=3Ordered on 2008-01-01 via the website/td
  /tr
  tr class=darkerbackground
td124/td
tdPeter Bilt/td
td$25.99/td
  /tr
  tr class=darkerbackground
td colspan=3Mail via African swallow/td
  /tr
/table

Note particularly how the explanatory notes for each invoice are similar in
appearance to the primary report lines they're associated with.

Now, I have a similar transformation to PDF via pdflatex.  This works fairly
well but requires a bunch of temp files and subprocesses, and I've never
been 100% happy with the LaTeX output (you have to calculate your own
column widths, for instance).  Since I plan to re-write this anyway, I'd
like to find a more widely used library if one was available.

ReportLab seemed *almost* perfect, except that it doesn't support colspans. 
As I hope I demonstrated in the example, most of our reports depend on that
ability.

So, again, any thoughts on a PDF generator that can generate tables with the
same kind of flexibility as HTML?
-- 
Kirk Strauser
--
http://mail.python.org/mailman/listinfo/python-list


Re: How do web templates separate content and logic?

2008-07-01 Thread Kirk Strauser
At 2008-06-30T19:34:53Z, Mike [EMAIL PROTECTED] writes:

 I should have say why embedding HTML into Python is not good enough? ;=)

I'm a programmer and would be able to cope with embedding HTML in assembler
if the need arose.

Having said that, embedding HTML in anything but HTML tends to be a bad idea
in the long run.  Inevitably, you're going to want to move stuff around, and
find out that you can't just move

print ul
for item in itemlist:
print li%s/li % item
print /ul

up near the top because you don't actually define itemlist until near the
end of your function, because it's built on the results of a bunch of other
code.

So then you solve the problem by leaving that snippet where it is and
moving all the other HTML print commands after it.  Oh, crud!  You realize
too late that some of your print commands have side effects:

for item in deletelist:
print pDeleting %s: %s/p % (str(item), mangle(item))

and if you run that code *after* you generate itemlist, the results will be
all screwed up.

So you go back and move all of your logic to the top of the function and all
of the HTML print statements to the bottom.  And then you realize you've
written your very own page template, and that it's ugly, and that you
should've used something different from the very beginning.

That's why you don't embed HTML in Python, at least not for anything more
complicated than Hello, world.
-- 
Kirk Strauser
The Day Companies
--
http://mail.python.org/mailman/listinfo/python-list


Re: newb question on strings

2008-06-27 Thread Kirk Strauser
At 2008-06-24T20:27:33Z, regex_jedi [EMAIL PROTECTED] writes:

 Notice that the 4th value has a single quote in it. Well, I need to
 make sure that the single quote is escaped before handing it off for
 further processing to a class I later call for some other processing.

Out of curiosity, what kind of processing?  The only time I've really had to
mess with escaping quotes involved SQL.  There is *no* reason why you'd ever
directly generate SQL strings yourself today unless you're writing a
database connector.
-- 
Kirk Strauser
The Day Companies
--
http://mail.python.org/mailman/listinfo/python-list


Django or TurboGears for a new project

2008-06-27 Thread Kirk Strauser
We're looking to migrate a Zope site to Django, but before getting beyond
the dreaming stage, I thought I'd see what others are doing these days.

If you were going to start a fairly complex site today with lots of DB
integration, would you begin with Django or TurboGears, or something else
entirely?

I'm more interested in social, rather than technical reasons (unless there's
something you absolutely love or despise about one or the other).
Popularity is actually a pretty big consideration because I'd like to work
with an eager community who's doing cool new things.

I know asking for comparisons like this is potentially flamebait-ish, but I
really don't mean it that way.  It's just that I don't have a lot of friends
in the industry in this particular development niche who I can ask for
recommendations, and this seems like as good a place as any to find subject
matter experts.

Thanks,
-- 
Kirk Strauser
The Day Companies
--
http://mail.python.org/mailman/listinfo/python-list


Re: dict order

2008-06-18 Thread Kirk Strauser
At 2008-06-18T10:32:48Z, [EMAIL PROTECTED] writes:

 # untested 2.5
 for keys in dict_one.items():
   if keys in dict_two:
 if dict_one[keys] != dict_two[keys]:
   # values are different
   else:
 # key is not present

That fails if there is an item in dict_two that's not in dict_one.
-- 
Kirk Strauser
The Day Companies
--
http://mail.python.org/mailman/listinfo/python-list


Re: question relateding to parsing dbf files.

2008-06-18 Thread Kirk Strauser
At 2008-06-18T12:50:20Z, Krishnakant Mane [EMAIL PROTECTED] writes:

 hello all.
 I need to parse some dbf files through python.
 the reason being that I have to migrate some old data from dbf files
 to postgresql.
 all that I need to know is if some one has got a working code sample
 using dbfpy.
 I found this module suitable for my work but can't figure out how to use it.
 else, if there is any other module, please recommend so.
 happy hacking.
 Krishnakant.

Does it have to by in Python?  I host this project, written in C++:
http://honeypot.net/project/xbasetopg .  If that's too complicated, I've
written a replacement in straight C but I haven't published it yet.

I use these programs to sync our legacy FoxPro database to our new
PostgreSQL servers on an hourly basis.  Syncing 4GB of data tables about 10
minutes.
-- 
Kirk Strauser
The Day Companies
--
http://mail.python.org/mailman/listinfo/python-list


Re: Pattern Matching Over Python Lists

2008-06-17 Thread Kirk Strauser
At 2008-06-17T05:55:52Z, Chris [EMAIL PROTECTED] writes:

 Is anyone aware of any prior work done with searching or matching a
 pattern over nested Python lists? I have this problem where I have a
 list like:

 [1, 2, [1, 2, [1, 7], 9, 9], 10]

 and I'd like to search for the pattern [1, 2, ANY] so that is returns:

 [1, 2, [1, 2, [6, 7], 9, 9], 10]
 [1, 2, [6, 7], 9, 9]

Hint: recursion.  Your general algorithm will be something like:

def compare(list, function):
if function(list):
print list
for item in list:
if item is a list:
compare(item, function)

def check(list):
if list starts with [1, 2] and length of the list  2:
return True
else:
return False
-- 
Kirk Strauser
The Day Companies
--
http://mail.python.org/mailman/listinfo/python-list


Good cross-host IPC?

2008-06-16 Thread Kirk Strauser
We've been using NetWorkSpaces
(http://www.ddj.com/web-development/21971) for IPC on programs running
on several different machines.  Since it uses a central, shared server for
storing values, you don't have to write socket code in your various programs
to pass data back and forth.

For example, a program can request some work to be done by a random machine
on the network like so:

 from nws.client import NetWorkSpace
 space = NetWorkSpace('test')
 space.store('value', 42)
 ws.fetch('results')
43

...and a worker process can listen for work to do and return the results by
doing:

 from nws.client import NetWorkSpace
 space = NetWorkSpace('test')
 value = space.fetch('value')
 space.store('results', value + 1)

Since space.fetch() is a blocking call and the NetWorkSpaces server answers
requests in the order that they're received, this is a nice way to
coordinate a cluster.

This is pretty spiffy and works great in practice, but it feels like we're
the only people using it.  Parallel Python lives in kind of the same problem
space, but not all of our code is easily bent to its will.  Is there
another, more common way of doing this stuff?

Popularity isn't the most important thing in the world, but I like the warm
fuzzies of knowing that thousands of others are testing and using the same
project as we are.
-- 
Kirk Strauser
The Day Companies
--
http://mail.python.org/mailman/listinfo/python-list


Calling instance methods from a decorator

2008-05-30 Thread Kirk Strauser
I'm trying to write a decorator that would do something like:

def trace(before, after):
def middle(func):
def inner(*args, **kwargs):
func.im_self.debugfunction(before)
result = func(*args, **kwargs)
func.im_self.debugfunction(after)
return result
return inner
   return middle

class Foo(object):
def __init__(self, myname):
self.name = myname

def debugfunction(self, message):
print 'Instance %s says: %s' % (self.name, message)

@trace('calling', 'finished')
def bar(self, arg):
print arg

 Foo('snake').bar(123)
Instance snake says: calling
123
Instance snake says: finished

The gotcha seems to be that there's no way to get to 'self' from within the
inner function, since func will only have the normal attributes:

 print dir(func)
['__call__', '__class__', '__delattr__', '__dict__', '__doc__', '__get__', 
'__getattribute__', '__hash__', '__init__', '__module__', '__name__', 
'__new__', '__reduce__', '__reduce_ex__', '__repr__', '__setattr__', '__str__', 
'func_closure', 'func_code', 'func_defaults', 'func_dict', 'func_doc', 
'func_globals', 'func_name']

There's no nice im_self to bounce off of or anything.  I seem to be going
about this all wrong.  What's a good approach to get the desired effect?
-- 
Kirk Strauser
The Day Companies
--
http://mail.python.org/mailman/listinfo/python-list


Re: Calling instance methods from a decorator

2008-05-30 Thread Kirk Strauser
At 2008-05-30T17:40:17Z, Diez B. Roggisch [EMAIL PROTECTED] writes:

 Of course you can get the self - just use the first paramter, because it
 *is* self. Self is just a parameter - nothing special.

If I blame it on being a long week, can I keep my geek card?
-- 
Kirk Strauser
The Day Companies
--
http://mail.python.org/mailman/listinfo/python-list


Re: accumulator generators

2008-05-30 Thread Kirk Strauser
At 2008-05-30T19:50:43Z, Cameron [EMAIL PROTECTED] writes:

 Why does that work, but not this:

 def foo(n):
   s = n
   def bar(i):
 s += i
 return s
   return bar

Assume that n is an int, making s one also.  Ints are immutable; you can
only copy them.  So your bar is taking s, adding i to it, then assigning the
value back to the local variable named s.
-- 
Kirk Strauser
The Day Companies
--
http://mail.python.org/mailman/listinfo/python-list


Subclassing list the right way?

2008-04-25 Thread Kirk Strauser
I want to subclass list so that each value in it is calculated at call
time.  I had initially thought I could do that by defining my own
__getitem__, but 1) apparently that's deprecated (although I can't
find that; got a link?), and 2) it doesn't work.

For example:

 class Foo(list):
... def __getitem__(self, index):
... return 5
...
 a = Foo([1, 2, 3, 4, 5])
 print a
[1, 2, 3, 4, 5]
 print a[2:4]
[3, 4]
 print a[3]
5

I first expected that to instead behave like:

 print a
[5, 5, 5, 5, 5]
 print a[2:4]
[5, 5]

Is there a right way to do this?
--
Kirk Strauser
--
http://mail.python.org/mailman/listinfo/python-list


Auto-parallelizing with decorators?

2007-07-06 Thread Kirk Strauser
I was thinking about how a lot of Lisp proponents claim that Lisp is
inherently parallelizable because its functions don't have (or are not
supposed to have) side effects, and therefore the compiler can easily tell
which calls may be run in parallel instead of strictly serially.  I'm not a
Lisp expert so I can't say whether that's true or not, but it seems like an
interesting idea for Python.

Suppose that Python had a new decorator, say @parallelizable.  Functions
so decorated would be eligible for multi-processed or multi-threaded
execution by such native constructs as list comprehensions, or the map()
function.  Their logic could be something along the lines of:

1) Is every function in the function expression of map() or a list
comprehension decorated with @parallelizable?  If not, use the normal
implementation.
2) Spawn an appropriate number of child threads and queue values to be
processed.
3) As results return, either store them in the result list (for map()), or
insert them into their place in the output queue to be consumed by users of
a list comprehension.

In #2, appropriate could default to something like 2 times the number of
CPUs detected in the system (which could be found the first time it was
needed so that programs that don't use this functionality don't pay for
finding that information).  I could imagine that it'd be useful to pass
hints to the decorator to more explicitly size the thread pool, such as:

@parallelizable(number=100)
def lightweightfunction:
Launch as many parallel copies as you can

@parallelizable(perproc=1)
def heavierfunction:
Only run one copy per CPU

@parallelizable(number=2)
def bigslowfunction:
This benefits from parallel execution, but don't kill the system

Would something like this have merit?  I like the idea because it's
completely optional; if you don't use it, you never have to deal with the
side effects.  However, if you take care to make your functions loosely
coupled and not dependent on execution order, you could get a nice speedup
every single time you give Python permission to schedule your repeated
function calls.
-- 
Kirk Strauser
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Auto-parallelizing with decorators?

2007-07-06 Thread Kirk Strauser
Stefan Behnel wrote:

 Wouldn't that require parallelism in the interpreter first? Mind the
 GIL...

That may be.  I'd bet though that I could whip up a native Python workalike
using os.fork() that would work around GIL, at least on Unix (just because
I don't know if Windows has os.fork(), having never looked for it).

I know something like this wouldn't be the easiest thing to implement, but
honestly, having such beautiful constructs as map() and list comprehensions
available is just begging for an application like this.
-- 
Kirk Strauser
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Auto-parallelizing with decorators?

2007-07-06 Thread Kirk Strauser
In article [EMAIL PROTECTED],
 Kirk Strauser [EMAIL PROTECTED] wrote:

 I was thinking about how a lot of Lisp proponents claim that Lisp is
 inherently parallelizable because its functions don't have (or are not
 supposed to have) side effects, and therefore the compiler can easily tell
 which calls may be run in parallel instead of strictly serially.  I'm not a
 Lisp expert so I can't say whether that's true or not, but it seems like an
 interesting idea for Python.

By the way, I uploaded a sample implementation (in Python) of what I had 
in mind to http://www.honeypot.net/multi-processing-map-python .  Please 
let me know what you think of it and whether it seems remotely 
interesting or goofy.
-- 
Kirk Strauser
-- 
http://mail.python.org/mailman/listinfo/python-list


Recommended validating XML parser?

2007-05-07 Thread Kirk Strauser
We're looking for a current, supported, validating XML parser.  Since it
seems like there are a few thousand out there, I though we'd see what
everyone else is using.

Bonus points if it can do something like:

 foo = XMLParser(
xml
weight3000/weight
/xml
, dtd=file('/etc/weightfile.dtd'))

 print foo.weight
3000

...or some variant on that theme.
-- 
Kirk Strauser
-- 
http://mail.python.org/mailman/listinfo/python-list


Finding the name of a class

2006-08-01 Thread Kirk Strauser
Given a class:

 class foo(object):
 pass

how can I find its name, such as:

 b = foo
 print something(b)
'foo'

I'm writing a trace() decorator for the sake of practice, and am trying to
print the name of the class that a traced method belongs to.  This seems
like it should be easy, but I think I've been staring at the problem too
long.
-- 
Kirk Strauser
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Finding the name of a class

2006-08-01 Thread Kirk Strauser
Larry Bates wrote:

 print print b.__class__.__name__  gives what you want

That doesn't seem to do it, though.  Here's the result of importing a module
from my company's internally-developed library:

 from Daycos.TableCopier.copyfro import StateProcessor
 print StateProcessor.__class__.__name__
type

I'm looking for something that would print 'StateProcessor' but am not
having much luck.
-- 
Kirk Strauser
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Finding the name of a class

2006-08-01 Thread Kirk Strauser
Bruno Desthuilliers wrote:

 Kirk Strauser wrote:

 class foo(object):
 pass
 
 how can I find its name, such as:
 
 b = foo

 I suppose you mean b = foo() ?

Actually, I meant 'b = foo' in this case - I want to find the name of the
class that b references, but the name of an instance (which may have zero
or many references to it).

 The name of a class is in the attribute '__name__' of the class. The
 class of an object is in the attribute '__class__' of the object.

I swear that didn't work earlier.  Honest.  :-)

OK, now for the good stuff.  In the code below, how can I find the name of
the class that 'bar' belongs to:

 class Foo(object):
... def bar(self):
... pass
...
 b = Foo.bar
 dir(b)
['__call__', '__class__', '__cmp__', '__delattr__', '__doc__', '__get__', 
'__getattribute__', '__hash__', '__init__', '__new__', '__reduce__', 
'__reduce_ex__', '__repr__', '__setattr__', '__str__', 'im_class', 'im_func', 
'im_self']
 b.__class__
type 'instancemethod'
 b.__class__.__name__
'instancemethod'

I was sort of hoping that it would print 'Foo'.
-- 
Kirk Strauser
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Python article in Free Software Magazine

2006-01-04 Thread Kirk Strauser
Terry Hancock wrote:

 I find that it's not difficult to explain Python object handling if you
 simply insist on the concept of name binding instead of variable 
 assignment:  

That was a pretty good explanation.  I'll probably use that next time.
 
 Makes for a nice figure, too, which Free Software Magazine needs more of.

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


Re: Python article in Free Software Magazine

2006-01-04 Thread Kirk Strauser
Michele Simionato wrote:

 when I think Zope is the less Pythonic application I have ever seen;)

You do?  Why so?  I'm not arguing, but that's different than my experience
with it and I'm curious about how you reached that conclusion.
-- 
Kirk Strauser
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Python article in Free Software Magazine

2006-01-01 Thread Kirk Strauser
On Sunday 01 January 2006 01:06 am, Steven D'Aprano wrote:

 I don't want to nit-pick all my way through the article,

There's nothing wrong with that, and if I hadn't been prepared for it, I
wouldn't have posted the link in here.

You have fair points.  Unfortunately, though, the word length of the article
just didn't provide enough space to go into the level of detail those
subjects would have required.  I tried to compromise by giving the answer
that most closely fit the situation without being exactly correct.

By the way, the author style guides say to write for an audience with
technical background.  That article was meant for you, not your boss.  :-)
-- 
Kirk Strauser

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


Python article in Free Software Magazine

2005-12-31 Thread Kirk Strauser
I wrote this article which was published in Free Software Magazine:

http://www.freesoftwaremagazine.com/free_issues/issue_09/intro_zope_1/

It's intended as a high-level overview of the language, and therefore
glosses over some of the details.  For example, I describe its function
calling mechanism as pass-by-reference, because that's close enough for
newcomers to get the gist of it.

Anyway, the article's available under an open license.  If you like it, feel
free to pass it around.  Enjoy!
-- 
Kirk Strauser

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


Pickling and inheritance are making me hurt

2005-02-04 Thread Kirk Strauser
I have a module that defines a Search class and a SearchResult class.  I use
these classes by writing other modules that subclass both of them as needed
to interface with particular search engines.

My problem is that Search defines a method (called automatically by __del__)
to save its results between invocations:

def _saveresults(self):
self._oldresults = self._results
file = open(self._storefile(), 'w')
pickle.dump(self._oldresults, file)
file.close()

The problem I'm having is the the pickle.dump call is failing whenever the
objects in self.data are instances of derivatives of SearchResult rather
than instances of SearchResult itself (which is pretty much always the
case):

Exception pickle.PicklingError: pickle.PicklingError instance at
0xb7f7ad6c in bound method Search.__del__ of __main__.Search object at
0xb7ec954c ignored


Now, if I overload _saveresults inside a subclass of Search, then it works. 
It seems like the problem is that _saveresults is only looking inside the
same namespace as Search (where it's originally defined), even if it's
actually been inherited by another class in a different module.  Is there a
way around this?

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