dictionary of operators

2008-02-14 Thread rbossy
Hi,

In the standard library module "operator", it would be nice to have a dictionary
mapping operators strings with their respective functions. Something like:

  {
'+': add,
'-': sub,
'in': contains,
'and': and_,
'or': or_,
...
  }



Rationale:

Recently I had to implement a small language interpreter and I ended up building
such a dictionary. Indeed the parser (I used PLY then pybison) returned an AST,
where all arithmetic were represented by a single node type, like this:

  class Arithmetic(Expression):
  def __init__(self, op, lft, rgt):
  self.op = op
  self.lft = lft
  self.rgt = rgt

  def eval(self, scope):
  return self.op(self.lft.eval(scope), self.rgt.eval(scope))

The dictionary allowed the parser to have a single rule action like this:

  Arithmetic(opstr[term[1]], term[0], term[2])



Does such a dictionary already exist? Is it really a good and useful idea?

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


Re: Python Memory Manager

2008-02-18 Thread rbossy
Quoting Steve Holden <[EMAIL PROTECTED]>:

> [...]
> Not only that, but all pointers to an object have to be updated when it
> is relocated.

"Any problem in computer science can be solved by another level of indirection"
-- David John Wheeler

;-)

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


Re: why not bisect options?

2008-03-01 Thread rbossy
Selon Raymond Hettinger <[EMAIL PROTECTED]>:

> [Robert Bossy]
> > I thought it would be useful if insort and consorts* could accept the
> > same options than list.sort, especially key and cmp.
>
> If you're going to do many insertions or searches, wouldn't it be
> *much* more efficient to store your keys in a separate array?
>
> The sort() function guarantees that it calls the key function exactly
> once for each member of the list.  With and bisect/insort, successive
> searches can call the key function over and over again with the same
> value.

Yeah, sure. Thanks for pointing that out.

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


Re: why not bisect options?

2008-03-04 Thread rbossy
Quoting Raymond Hettinger <[EMAIL PROTECTED]>:

> [Robert Bossy]
> > I thought it would be useful if insort and consorts* could accept the
> > same options than list.sort, especially key and cmp.
>
> If you're going to do many insertions or searches, wouldn't it be
> *much* more efficient to store your keys in a separate array?
>
> The sort() function guarantees that it calls the key function exactly
> once for each member of the list.  With and bisect/insort, successive
> searches can call the key function over and over again with the same
> value.

I factored your remark into the code. I actually don't have any particular
question about it, that's just an acknowledgment. Though I feel that if one
uses the default key, then an awkward list of twins is stored...



from operator import itemgetter


def identity(x): return x


class Bisect:
def __init__(self, it, key=identity, cmp=cmp):
self.lst = [ (key(x),x,) for x in it ]
self.lst.sort(cmp=cmp, key=itemgetter(0))
self.key = key
self.cmp = cmp

def insort_right(self, x, lo = 0, hi = None):
if hi is None:
hi = len(self.lst)
xk = self.key(x)
while lo < hi:
mid = (lo + hi) // 2
if self.cmp(xk, self.lst[mid][0]) < 0:
hi = mid
else:
lo = mid + 1
self.lst.insert(lo, (kx,x,))

# skipped insort_left, bisect_right, bisect_left
# also skipped the container methods __len__, __getitem__, __iter__


Cheers,
RB
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: DOM parsing not working!

2008-03-08 Thread rbossy
Quoting Mike D <[EMAIL PROTECTED]>:

> Hello, I've spent the morning trying to parse a simple xml file and have the
> following:
> import sys
> from xml.dom import minidom
>
> doc=minidom.parse('topstories.xml')
>
> items = doc.getElementsByTagName("item")
> text=''
> for i in items:
> t = i.firstChild
> print t.nodeName
> if t.nodeType == t.TEXT_NODE:
> print "TEXT_NODE"
> print t.nodeValue
> text += t.data
>
> print text
>
> I can't figure out how to print the text value for a text node type. There
> must be something obvious I'm missing, any suggestions?

Yes quite a trivial thing. t is assigned to the first child node of 
which, in your example, is a text node containg just a newline.
It will be shown if you replace your print statements with something like:

print 't.nodeValue:', t.nodeValue, '### end of t.nodeValue'
...
print 'text:', text, '### end of text'


What is that you're trying to do? Do you want to extract all text nodes inside
?


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


Re: os.chdir

2008-03-08 Thread rbossy
Quoting Maryam Saeedi <[EMAIL PROTECTED]>:

> I have a problem using os.chdir on linux. What should I do if I want to
> change to root directory? The below does not work:
>
> os.chdir("~/dir1")

It is not mentioned in the documentation but I'm pretty sure os.dir() doesn't do
tilde expansion since this is usually performed by a shell.

You should use instead:

os.chdir(os.join(os.environ['HOME'], 'dir1'))

Cheers,
RB
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: os.chdir

2008-03-08 Thread rbossy
Quoting "Martin v. Löwis" <[EMAIL PROTECTED]>:

> >> os.chdir("~/dir1")
> >
> > It is not mentioned in the documentation but I'm pretty sure os.dir()
> doesn't do
> > tilde expansion since this is usually performed by a shell.
> >
> > You should use instead:
> >
> > os.chdir(os.join(os.environ['HOME'], 'dir1'))
>
> Or
>
> os.chdir(os.path.expanduser("~/dir1"))

Well... duh!
Thanks for reminding me.
Cheers,
RB
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Creating a file with $SIZE

2008-03-15 Thread rbossy
Quoting Bryan Olson <[EMAIL PROTECTED]>:

> Robert Bossy wrote:
> > Bryan Olson wrote:
> >> Robert Bossy wrote:
>  Robert Bossy wrote:
> > Indeed! Maybe the best choice for chunksize would be the file's buffer
> > size...
> >>
> >> That bit strikes me as silly.
> >>
> > The size of the chunk must be as little as possible in order to minimize
> > memory consumption. However below the buffer-size, you'll end up filling
> > the buffer anyway before actually writing on disk.
>
> First, which buffer? The file library's buffer is of trivial size,
> a few KB, and if we wanted to save even that we'd use os.open and
> have no such buffer at all. The OS may set up a file-specific
> buffer, but again those are small, and we could fill our file much
> faster with larger writes.
>
> Kernel buffers/pages are dynamically assigned on modern operating
> systems. There is no particular buffer size for the file if you mean
> the amount of kernel memory holding the written data. Some OS's
> do not buffer writes to disk files; the write doesn't return until
> the data goes to disk (though they may cache it for future reads).
>
> To fill the file fast, there's a large range of reasonable sizes
> for writing, but user-space buffer size - typically around 4K - is
> too small. 1 GB is often disastrously large, forcing paging to and
> from disk to access the memory. In this thread, Matt Nordhoff used
> 10MB; fine size today, and probably for several years to come.
>
> If the OP is writing to a remote disk file to test network
> throughput, there's another size limit to consider. Network file-
> system protocols do not steam very large writes; the client has to
> break a large write into several smaller writes. NFS version 2 had
> a limit of 8 KB; version 3 removed the limit by allowing the server
> to tell the client the largest size it supports. (Version 4 is now
> out, in hundreds of pages of RFC that I hope to avoid reading.)

Wow. That's a lot knowledge in a single post. Thanks for the information, Bryan.

Cheers,
RB
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: decorator to prevent adding attributes to class?

2008-07-12 Thread rbossy
>> class Foo(Freezeable):
>> def __init__(self):
>> self.bar = 42
>> self.freeze() # ok, we set all variables, no more from here
>>
>>
>> x = Foo()
>> print x.bar
>> x.bar = -42
>> print x.bar
>> x.baz = "OMG! A typo!"
>>
>
>Pretty nice, but unfortunately the subclass has to remember to call freeze
>in it's init.  Too bad that can't be automated.

Not to mention that subclasses constructors could be bit, like that:

class Foo(Freezeable):
...

class Bar(Foo):
def __init__(self, *args, **kw):
Foo.__init__(self, *args, **kw)
self.something_bar = 42 # this should raise AttributeError


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