Re: Automatic Generation of Python Class Files

2007-10-22 Thread Marc 'BlackJack' Rintsch
On Mon, 22 Oct 2007 11:05:52 -0700, Sunburned Surveyor wrote:

 On Oct 22, 10:26 am, Chris Mellon [EMAIL PROTECTED] wrote:

 You wrote:  can't think of a single reason why you would ever want to
 do this,
 since your list of method and property names would be just as
 verbose as just typing the actual python code.
 
 I don't think I understand how this would be the same amount of
 typing. Consider the following example that would generate a Monster
 class file from an input text file (I only did one property and method
 in generated class file.):

Really a monster class.

 Contents of input text file:
 
 [Name]
 Fire Breathing Dragon
 
 [Properties]
 Strength
 Scariness
 Endurance
 
 [Methods]
 eatMaiden argMaiden
 fightKnight argKnight
 
 Generated Python Class File:
 
 def class FireBreathingDragon:
 
def getStrength(self):
   
   Docstring goes here.
 
   @return
   @rtype
   
   return self.strength
 
def setStrength(self, argStrength):
   
   Docstring goes here.
 
   @param argStrength
   @ptype
   
   return self.strength
 
def eatMaiden(self, argMaiden):
   
   Docstring goes here.
 
   @param argMaiden
   @ptype
   

Okay this could be written as:

class FireBreathingDragon(object):
def __init__(self, strength):
self.strength = strength
self.scariness = scariness
self.endurance = endurance

def eat_maiden(self, maiden):
pass

def fight_knight(self knight):
pass

Even with all the stuff from your input file it's shorter than your
generated class.

I left out the docs because having no docs is as valuable as your stubs if
not even better.  This way you at least see that there is no real docs and
tools like `pylint` can point out the missing docs.

There are editors with good template support for the small amount of
boilerplate that's left.

Just in case you want to defend the default getters and setters: read up
on properties, i.e. the `property()` function, and the discussions in this
group about Python not being Java.

Ciao,
Marc 'BlackJack' Rintsch
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: ignoring chinese characters parsing xml file

2007-10-22 Thread Marc 'BlackJack' Rintsch
On Mon, 22 Oct 2007 21:24:40 +0200, Fabian López wrote:

 I am parsing an XML file that includes chineses characters, like ^
 uu啖啖才是w.扉L锍才是 or ヘアアイロン... The problem is that I get an error like:
 UnicodeEncodeerror:'charmap' codec can't encode characters in
 position..

You say you are *parsing* the file but this is an *encode* error.  Parsing
means *decoding*.

You have to show some code and the actual traceback to get help.  Crystal
balls are not that reliable.  ;-)

Ciao,
Marc 'BlackJack' Rintsch
-- 
http://mail.python.org/mailman/listinfo/python-list

Re: for loop

2007-10-22 Thread Marc 'BlackJack' Rintsch
On Mon, 22 Oct 2007 18:17:56 -0400, Shawn Minisall wrote:

 #Intro
 print *
 print  WELCOME TO THE POPULATION GROWTH CALCULATOR
 print *
 
 print This program will predict the size of a population of organisms.
 print
 print
 organisms=input(Please enter the starting number of organisms: )
 
 increase=input(Please enter the average daily population increase 
 as a percentage (20% = .20): )
 
 days=input(Please enter the number of days that they will multiply: )
 
 print  DayPopulation
 print --
 
 
 for p in range (days):
 
 population = organisms * population * increase
 
 print days,
 
 print \t\t\t\t,population
 
 I'm having problems with my for loop here to calculate estimated 
 population output to a table.  Instead of knowing how much I want to 
 loop it, the loop index is going to be whatever number of days the user 
 enters.  When I run my program, it asks the 3 questions above but then 
 just dead stops at a prompt which leads me to believe there's something 
 wrong with my loop.

It should not run at all as it is indented inconsistently.  If that
problem is corrected it will stop with a `NameError` because you try to
read `population` before anything was assigned to it.

Ciao,
Marc 'BlackJack' Rintsch
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Regular Expression

2007-10-22 Thread Marc 'BlackJack' Rintsch
On Mon, 22 Oct 2007 22:29:38 +, patrick.waldo wrote:

 I'm trying to learn regular expressions, but I am having trouble with
 this.  I want to search a document that has mixed data; however, the
 last line of every entry has something like C5H4N4O3 or CH5N3.ClH.
 All of the letters are upper case and there will always be numbers and
 possibly one .
 
 However below only gave me none.
 
 […]

 test = re.compile('\u+\d+\.')

There is no '\u'.  'u' doesn't have a special meaning so the '\' is
pointless.  Your expression matches one or more small 'u's followed by one
or more digits followed by a period.  Examples are 'u1.', '42.',
etc.

An expression that matches your first example would be: r'([A-Z]|\d|\.)+'.
That's a non-empty sequence of upper case letters, digits and periods.  To
limit this to just one optional period the expression gets a little
longer: r'([A-Z]|\d)+\.?([A-Z]|\d)+'

Does not match your second example because there is a lower case letter in
it.

Ciao,
Marc 'BlackJack' Rintsch
-- 
http://mail.python.org/mailman/listinfo/python-list

Re: General module name clash problem?

2007-10-19 Thread Marc 'BlackJack' Rintsch
On Fri, 19 Oct 2007 13:09:22 +0200, jipjip wrote:

 I mean, this is a general problem.
 Must i look for every module that gets importet for not clashing
 with my module files residing in the calling directory?

Yes.

 Is the python package system insufficient, is there something wrong with 
 my opinions or do i need a gotcha?

The package system is not insufficient but could solve your problem
actually.  Don't put all your modules simply in the same directory but in a
package so that your `whatever.pickle` does not clash with the standard
`pickle` anymore.

Ciao,
Marc 'BlackJack' Rintsch
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Problem of Readability of Python

2007-10-18 Thread Marc 'BlackJack' Rintsch
On Wed, 17 Oct 2007 15:01:09 -0700, kiilerix wrote:

 On Oct 17, 9:11 pm, Chris Mellon [EMAIL PROTECTED] wrote:
 On 10/17/07, [EMAIL PROTECTED] [EMAIL PROTECTED] wrote:
   o = object()
   o.foo = 7

 What makes you think it can't be instantiated directly? You just did
 it. It's not, however, suitable for use as an arbitrary thing to stick
 attributes on.

 Which is a little sad, but a necessary requirement for things like
 int() and str() to be small and fast.
 
 So it's an optimization with side effects, giving a special case where
 the simple and otherwise right way to do it doesn't work? Too bad :-
 (
 
 Ok; I'll continue to create dummy classes inheriting from object. And
 hope that one day it will be simpler.

I'm using the following dummy class with a little extra functionality:

def Bunch(object):
def __init__(self, **kwargs):
self.__dict__.update(kwargs)

person = Bunch(name='Eric', age=42)
print person.name
point = Bunch(x=4711, y=23)

Ciao,
Marc 'BlackJack' Rintsch
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Convert string to command..

2007-10-18 Thread Marc 'BlackJack' Rintsch
On Thu, 18 Oct 2007 08:41:30 -0700, Abandoned wrote:

 import cPickle as pickle
 a={2:3,4:6,2:7}
 s=pickle.dumps(a, -1)
 g=pickle.loads(s);
 print g
 '{2:3,4:6,2:7}'
 
 Thank you very much for your answer but result is a string ??

In Python terms yes, strings in Python can contain any byte value.  If you
want to put this into a database you need a BLOB column or encode it as
base64 or something similar more ASCII safe.

Ciao,
Marc 'BlackJack' Rintsch
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Static variable vs Class variable

2007-10-17 Thread Marc 'BlackJack' Rintsch
On Wed, 17 Oct 2007 00:33:59 -0700, paul.melis wrote:

 On Oct 10, 8:23 am, Diez B. Roggisch [EMAIL PROTECTED] wrote:
  However, it is not true that += always leads to a rebinding of a to the
  result of the operation +. The + operator for lists creates a new list.
  += for lists does an in-place modification:

 It still is true.

 a += b

 rebinds a. Period. Which is the _essential_ thing in my post, because
 this rebinding semantics are what confused the OP.
 
 Doesn't this depend on wether a supports __iadd__ or not?

No.  As shown several times in this thread already.

 Section 3.4.7 of the docs say
 
 
 If a specific method is not defined, the augmented operation falls
 back to the normal methods. For instance, to evaluate the expression x
 +=y, where x is an instance of a class that has an __iadd__() method,
 x.__iadd__(y) is called. If x is an instance of a class that does not
 define a __iadd__() method, x.__add__(y) and y.__radd__(x) are
 considered, as with the evaluation of x+y.
 
 
 So if a.__iadd__ exists, a += b is executed as a.__iadd__(b), in which
 case there's no reason to rebind a.

`__iadd__` *may* doing the addition in place and return `self` but it is
also allowed to return a different object.  So there is always a rebinding.

 However, this confuses the heck out of me:
 
 class A:
 ... l = []
 ...
 class B(A): pass
 ...
 B.__dict__
 {'__module__': '__main__', '__doc__': None}
 B.l
 []
 B.l.append('1')
 B.l
 ['1']
 B.__dict__
 {'__module__': '__main__', '__doc__': None}
 B.l.__iadd__('2')
 ['1', '2']

Here you see that the method actually returns an object!

 B.l
 ['1', '2']
 B.__dict__
 {'__module__': '__main__', '__doc__': None}
 B.l += '3'
 B.__dict__
 {'__module__': '__main__', '__doc__': None, 'l': ['1', '2', '3']}
 
 Why is B.l set for the += case only? B.l.__iadd__ obviously exists.

Because there is always a rebinding involved.

Ciao,
Marc 'BlackJack' Rintsch
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Static variable vs Class variable

2007-10-17 Thread Marc 'BlackJack' Rintsch
On Wed, 17 Oct 2007 13:41:06 +0200, Hrvoje Niksic wrote:

 Duncan Booth [EMAIL PROTECTED] writes:
 
 Hrvoje Niksic [EMAIL PROTECTED] wrote:

 I've recently been bitten by [rebinding the var to what __iadd__
 returns], and I don't understand the reasoning behind __iadd__'s
 design.  I mean, what is the point of an *in-place* add operation
 (and others) if it doesn't always work in-place?
 
 A very common use case is using it to increment a number:
 
 I'm aware of that; but remember that there's still __add__.  It would
 be sufficient for numbers not to implement __iadd__.  And, in fact,
 they already don't:
 
 1 .__add__(1)
 2
 1 .__iadd__(1)
 Traceback (most recent call last):
   File stdin, line 1, in module
 AttributeError: 'int' object has no attribute '__iadd__'
 
 The current implementation of += uses __add__ for addition and
 __iadd__ for addition that may or may not be in-place.  I'd like to
 know the rationale for that design.

Simply not to introduce special cases I guess.  If you write ``x.a += b``
then `x.a` will be rebound whether an `a.__iadd__()` exists or not. 
Otherwise one would get interesting subtle differences with properties for
example.  If `x.a` is a property that checks if the value satisfies some
constraints ``x.a += b`` would trigger the set method only if there is no
`__iadd__()` involved if there's no rebinding.

Ciao,
Marc 'BlackJack' Rintsch
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Static variable vs Class variable

2007-10-17 Thread Marc 'BlackJack' Rintsch
On Wed, 17 Oct 2007 05:57:50 -0700, Paul Melis wrote:

 On Oct 17, 2:39 pm, Duncan Booth [EMAIL PROTECTED] wrote:
  class C(object):

 def setx(self, value):
 if len(value)2:
 raise ValueError
 self._x = value
 def getx(self):
 return self._x
 x = property(getx, setx)

  o = C()
  o.x = []
  o.x += ['a']
  o.x += ['b']
  o.x += ['c']

 Traceback (most recent call last):
   File pyshell#27, line 1, in module
 o.x += ['c']
   File pyshell#22, line 4, in setx
 raise ValueError
 ValueError

  o.x
 ['a', 'b', 'c']
 
 Now that's really interesting. I added a print before and print
 after statement just before and after the self._x = value and these
 *do not get called* after the exception is raised when the third
 element is added.

Well, of course not.  Did you really expect that!?  Why?

Ciao,
Marc 'BlackJack' Rintsch
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Order by value in dictionary

2007-10-17 Thread Marc 'BlackJack' Rintsch
On Wed, 17 Oct 2007 08:09:50 -0700, Abandoned wrote:

 Very very thanks everbody..
 
 These are some method..
 Now the fastest method is second..

Maybe because the second seems to be the only one that's not processing
the whole dictionary but just 500 items less!?

You are building way too much intermediate lists in your functions.

  1 ===
 def sortt(d):
 items=d.items()

Builds a list of all items.

 backitems=[ [v[1],v[0]] for v in items]

Builds another list from all items.

 backitems.sort()
 a=[ backitems[i][1] for i in range(0,len(backitems))]

And again a new list *plus* a list of len(backitems) integers that is
built just to iterate over it.  Instead of iterating directly over
`backitems` without the index.

 a.reverse()
 return a

This whole function can be written as (untested):

def sortt(d):
sorted_items = sorted((item[1], item[0]) for item in d.iteritems(),
  reverse=True)
return map(operator.itemgetter(1), sorted_items)

  2 =
 import operator
 def sortt(d):
 backitems=d.items()
 boyut=len(backitems)
 backitems=backitems[boyut-500:]
 backitems=sorted(backitems, key=operator.itemgetter(1))
 a=[ backitems[i][0] for i in range(0,len(backitems))]
 a.reverse()
 return a

Without throwing away 500 items:

def sortt(d):
sorted_items = sorted(d.iteritems(),
  key=operator.itemgetter(1),
  reverse=True)
return map(operator.itemgetter(0), sorted_items)

Ciao,
Marc 'BlackJack' Rintsch
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Appending a list's elements to another list using a list comprehension

2007-10-17 Thread Marc 'BlackJack' Rintsch
On Wed, 17 Oct 2007 20:27:14 +, Debajit Adhikary wrote:

 I have two lists:
 
 a = [1, 2, 3]
 b = [4, 5, 6]
 
 What I'd like to do is append all of the elements of b at the end of
 a, so that a looks like:
 
 a = [1, 2, 3, 4, 5, 6]
 
 I can do this using
 
 map(a.append, b)

This is a bad idea as it creates a useless list of `None`\s, one for each
element in `b`.

 How do I do this using a list comprehension?

Not at all.  The obvious solution here is ``a.extend(b)``.

 (In general, is using a list comprehension preferable (or more
 pythonic) as opposed to using map / filter etc.?)

Some say yes.

Ciao,
Marc 'BlackJack' Rintsch
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: PIL and getpixel()

2007-10-16 Thread Marc 'BlackJack' Rintsch
On Tue, 16 Oct 2007 07:05:56 +, [EMAIL PROTECTED] wrote:

 i am trying to get the pixel data using im.getpixel()
 i am getting a tuple like (152,118,106) for a pixel in a RGB jpeg
 image .
 what i really want is an integer value representing a pixel ,like waht
 i can get from java's BufferedImage.getRGB(x,y) ..
 
 i am wondering if someone can advise me on how i can do this

Just pack the RGB values into an `int` by shifting and or-ing.  Untested:

red, green, blue = img.getpixel(x, y)
pixel_as_int = red  16 | green  8 | blue

Ciao,
Marc 'BlackJack' Rintsch
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: int to str in list elements..

2007-10-16 Thread Marc 'BlackJack' Rintsch
On Tue, 16 Oct 2007 06:18:51 +, Tim Roberts wrote:

 John Machin [EMAIL PROTECTED] wrote:
On Oct 15, 4:02 am, Abandoned [EMAIL PROTECTED] wrote:
 Hi..
 I have a list as a=[1, 2, 3  ] (4 million elements)
 and
 b=,.join(a)
 than
 TypeError: sequence item 0: expected string, int found
 I want to change list to  a=['1','2','3'] but i don't want to use FOR
 because my list very very big.

What is your worry: memory or time? The result string will be very
very very big.
 
 It's an interesting mental exercise to try to figure out just how large
 that string will be, without using Python.
 
 I get 30,888,889 bytes...

I think you have an off by one error here.  (One number, not one byte)  :-)

Ciao,
Marc 'BlackJack' Rintsch
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: confused on calculating date difference in days.

2007-10-16 Thread Marc 'BlackJack' Rintsch
On Tue, 16 Oct 2007 12:33:33 +0530, krishnakant Mane wrote:

 firstly, I can't get a way to convert a string like 1/2/2005 in a
 genuan date object which is needed for calculation.

Why?  Split the string up, convert the parts to `int` and just create a
`datetime.date` object.

 now once this is done I will create a another date object with
 today = datetime.datetime.now()
 and then see the difference between this today and the string that I
 converted to date.
 now in the first place I can't recall how I can convert a string to a date.
 then now I don't know how to calculate difference in days between
 today and the string converted date.

In [421]: '1/2/2005'.split('/')
Out[421]: ['1', '2', '2005']

In [422]: map(int, '1/2/2005'.split('/'))
Out[422]: [1, 2, 2005]

In [423]: month, day, year = map(int, '1/2/2005'.split('/'))

In [424]: a = datetime.date(year, month, day)

In [425]: b = datetime.date.today() - a

In [426]: b.days
Out[426]: 1017

Maybe you should read the docs next time.  ;-)

Ciao,
Marc 'BlackJack' Rintsch
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: confused on calculating date difference in days.

2007-10-16 Thread Marc 'BlackJack' Rintsch
On Tue, 16 Oct 2007 18:10:54 +1000, Ben Finney wrote:

 Marc 'BlackJack' Rintsch [EMAIL PROTECTED] writes:
 
 On Tue, 16 Oct 2007 12:33:33 +0530, krishnakant Mane wrote:
 
  firstly, I can't get a way to convert a string like 1/2/2005 in
  a genuan date object which is needed for calculation.
 
 Why?  Split the string up, convert the parts to `int` and just
 create a `datetime.date` object.
 
 What, re-implement 'strptime' in every program that needs it? And then
 debug the result every time?

Yes.  Seems easier to me.  :-)

 Even if one doesn't have Python 2.5 or above, surely getting the
 string parsed into int values by the standard 'time.strptime' is
 better than re-implementing it every time.
 
 Maybe you should read the docs next time.  ;-)
 
 Back at you.

Got me.  I didn't know that `datetime` has a `strptime` now.  I just
looked at `date`.

Ciao,
Marc 'BlackJack' Rintsch
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: ctypes Wrapping Complex Datatypes

2007-10-16 Thread Marc 'BlackJack' Rintsch
On Tue, 16 Oct 2007 06:47:06 -0700, gamename wrote:

 I've just started using ctypes and so far, its great.  But I'm running
 to some problems with complex datatypes.  I'm not sure how to wrap
 something like this:
 
/* This defines the Handle type in a header file.  I don't think
 this needs wrapping, its just to show the handle definition for
 context*/
 typedef struct DFFTSHandle_s *DFFTSHANDLE;
 ...
   /* Then, the handle is used like this later on.  This *is* what I
 want to wrap.*/
 status = DFFTSCreateSession(Handle);
 status = DFFTSSetSessionOption(Handle, DFFTSOPT_ITERATIONS,
 iteration, sizeof(iteration));

If this handle is always just treated as a pointer to an opaque data
structure you may just use a void pointer.

Ciao,
Marc 'BlackJack' Rintsch
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Why does the message send only once?

2007-10-16 Thread Marc 'BlackJack' Rintsch
On Tue, 16 Oct 2007 15:55:31 +, danfolkes wrote:

 Why does the message send only once?
 
 The node sends once, then fails after that.

Because the `Server` just seems to handle the first connection and is done
then.  Insert some ``print``\s to see what's going on.

Ciao,
Marc 'BlackJack' Rintsch
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: some questions about Python and tkinter

2007-10-16 Thread Marc 'BlackJack' Rintsch
On Tue, 16 Oct 2007 11:52:22 -0700, fabdeb wrote:

 the first: what is the differences between a function and a classe?

A class bundles data and functions into one object.

 In which case i should use a function ?
 In which case i should use a class ?

If you have several functions that operate on the same data it might make
sense to put all into a class to treat them as one unit.

 The second: there is some pincipals gui toolkit: tkinter , Python Mega-
 Widgets, PyGTK, PyQt, FxPy, WxPy
 
 what are the advantages of each one, and in which case i use each of
 them?

`Tkinter` is part of the standard library.  If you use that or one of the
others is a matter of taste to some degree.  In a GNOME environment PyGTK
may look more natural, under KDE a PyQt or PyKDE based GUI may feel more
native.  Another factor for a decision might be the license of the GUI
toolkit.

Ciao,
Marc 'BlackJack' Rintsch
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Newbi Q: Recursively reverse lists but NOT strings?

2007-10-15 Thread Marc 'BlackJack' Rintsch
On Mon, 15 Oct 2007 02:11:27 -0400, Victor B. Gonzalez wrote:

 On Sunday 14 October 2007 5:06:19 pm Dmitri O.Kondratiev wrote:
 The function I wrote (below) reverses lists all right:

 def reverse(xs):
 if xs == []:
 return []
 else:
 return (reverse (xs[1:])) + [xs[0]]

  reverse ([1,2,3])

 [3, 2, 1]

 Yet when I try to reverse a string I  get:
  reverse (abc)

 ...
 ...
 ...

   File C:\wks\python-wks\reverse.py, line 5, in reverse

 return (reverse (xs[1:])) + [xs[0]]

   File C:\wks\python-wks\reverse.py, line 5, in reverse

 return (reverse (xs[1:])) + [xs[0]]

   File C:\wks\python-wks\reverse.py, line 2, in reverse

 if xs == []:

 RuntimeError: maximum recursion depth exceeded in cmp



 What's wrong? Why recursion never stops?

Becauese you test if `xs` is an empty list which is never true when you
call the function with a string.  So it never ends.  '' != []

Ciao,
Marc 'BlackJack' Rintsch
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Memory Problems in Windows 2003 Server

2007-10-15 Thread Marc 'BlackJack' Rintsch
On Mon, 15 Oct 2007 11:31:59 +0200, amdescombes wrote:

 Are there any classes that implement disk based dictionaries?

Take a look at the `shelve` module from the standard library.

Or object databases like ZODB or Durus.

Ciao,
Marc 'BlackJack' Rintsch
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Simple Text Processing Help

2007-10-15 Thread Marc 'BlackJack' Rintsch
On Mon, 15 Oct 2007 10:47:16 +, patrick.waldo wrote:

 my sample input file looks like this( not organized,as you see it):
 200-720-769-93-2
 kyselina mocová  C5H4N4O3
 
 200-001-8   50-00-0
 formaldehyd  CH2O
 
 200-002-3
 50-01-1
 guanidínium-chlorid  CH5N3.ClH
 
 etc...

That's quite irregular so it is not that straightforward.  One way is to
split everything into words, start a record by taking the first two
elements and then look for the start of the next record that looks like
three numbers concatenated by '-' characters.  Quick and dirty hack:

import codecs
import re

NR_RE = re.compile(r'^\d+-\d+-\d+$')

def iter_elements(tokens):
tokens = iter(tokens)
try:
nr_a = tokens.next()
while True:
nr_b = tokens.next()
items = list()
for item in tokens:
if NR_RE.match(item):
yield (nr_a, nr_b, ' '.join(items[:-1]), items[-1])
nr_a = item
break
else:
items.append(item)
except StopIteration:
yield (nr_a, nr_b, ' '.join(items[:-1]), items[-1])



def main():
in_file = codecs.open('test.txt', 'r', 'utf-8')
tokens = in_file.read().split()
in_file.close()
for element in iter_elements(tokens):
print '|'.join(element)

Ciao,
Marc 'BlackJack' Rintsch
-- 
http://mail.python.org/mailman/listinfo/python-list

Re: Simple Text Processing Help

2007-10-14 Thread Marc 'BlackJack' Rintsch
On Sun, 14 Oct 2007 13:48:51 +, patrick.waldo wrote:

 Essentially I need to take a text document with some chemical
 information in Czech and organize it into another text file.  The
 information is always EINECS number, CAS, chemical name, and formula
 in tables.  I need to organize them into lines with | in between.  So
 it goes from:
 
 200-763-1 71-73-8
 nátrium-tiopentál   C11H18N2O2S.Na   to:

Is that in *one* line in the input file or two lines like shown here?

 200-763-1|71-73-8|nátrium-tiopentál|C11H18N2O2S.Na
 
 but if I have a chemical like: kyselina močová
 
 I get:
 200-720-7|69-93-2|kyselina|močová
 |C5H4N4O3|200-763-1|71-73-8|nátrium-tiopentál
 
 and then it is all off.
 
 How can I get Python to realize that a chemical name may have a space
 in it?

If the two elements before and the one element after the name can't
contain spaces it is easy:  take the first two and the last as it is and
for the name take from the third to the next to last element = the name
and join them with a space.

In [202]: parts = '123 456 a name with spaces 789'.split()

In [203]: parts[0]
Out[203]: '123'

In [204]: parts[1]
Out[204]: '456'

In [205]: ' '.join(parts[2:-1])
Out[205]: 'a name with spaces'

In [206]: parts[-1]
Out[206]: '789'

This works too if the name doesn't have a space in it:

In [207]: parts = '123 456 name 789'.split()

In [208]: parts[0]
Out[208]: '123'

In [209]: parts[1]
Out[209]: '456'

In [210]: ' '.join(parts[2:-1])
Out[210]: 'name'

In [211]: parts[-1]
Out[211]: '789'

 #read and enter into a list
 chem_file = []
 chem_file.append(input.read())

This reads the whole file and puts it into a list.  This list will
*always* just contain *one* element.  So why a list at all!?

 #split words and store them in a list
 for word in chem_file:
 words = word.split()

*If* the list would contain more than one element all would be processed
but only the last is bound to `words`.  You could leave out `chem_file` and
the loop and simply do:

words = input.read().split()

Same effect but less chatty.  ;-)

The rest of the source seems to indicate that you don't really want to read
in the whole input file at once but process it line by line, i.e. chemical
element by chemical element.

Ciao,
Marc 'BlackJack' Rintsch
-- 
http://mail.python.org/mailman/listinfo/python-list

Re: Simple Text Processing Help

2007-10-14 Thread Marc 'BlackJack' Rintsch
On Sun, 14 Oct 2007 16:57:06 +, patrick.waldo wrote:

 Thank you both for helping me out.  I am still rather new to Python
 and so I'm probably trying to reinvent the wheel here.
 
 When I try to do Paul's response, I get
tokens = line.strip().split()
 []

What is in `line`?  Paul wrote this in the body of the ``for`` loop over
all the lines in the file.

 So I am not quite sure how to read line by line.

That's what the ``for`` loop over a file or file-like object is doing. 
Maybe you should develop your script in smaller steps and do some printing
to see what you get at each step.  For example after opening the input
file:

for line in input:
print line # prints the whole line.
tokens = line.split()
print tokens   # prints a list with the split line.

 tokens = input.read().split() gets me all the information from the
 file.

Right it reads *all* of the file, not just one line.

  tokens[2:-1] = [u' '.join(tokens[2:-1])] works just fine, like
 in the example; however, how can I loop this for the entire document?

Don't read the whole file but line by line, just like Paul showed you.

 Also, when I try output.write(tokens), I get TypeError: coercing to
 Unicode: need string or buffer, list found.

`tokens` is a list but you need to write a unicode string.  So you have to
reassemble the parts with '|' characters in between.  Also shown by Paul.

Ciao,
Marc 'BlackJack' Rintsch
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: pydev code completion problem

2007-10-14 Thread Marc 'BlackJack' Rintsch
On Sun, 14 Oct 2007 19:45:07 +, Lukasz Mierzejewski wrote:

 Let's assume that we have something like this:
 
 class One:
   def fun(self):
   return 1
 
 class Two:
   li = []
   li.append(One())
 
   one = li[0]
   print one.fun()
 
   one2 = li.pop()
   print one2.fun()
 
   one3 = One()
   print one3.fun()

Indention is messed up here.  At least the ``return`` in `One.fun()`.  And
what is class `Two` meant to do?

 Only for 'one3' variable code completion is working fine (as expected it
 show fun()).
 For 'one' code completion shows something (but not fun()).
 For 'one2' code completion shows nothing :-(
 
 I use Eclipse 3.3.1 with PyDev 1.3.9 on Ubuntu 7.04.
 
 Can anyone confirm or deny this behavior of PyDev?

I can confirm and it's something I would expect.  It is obvious to *you*
that there is a `One` object in that list, but it would get very quickly
very complicated for an IDE to keep track of objects if not even
impossible.

Ciao,
Marc 'BlackJack' Rintsch
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: raw_input() and utf-8 formatted chars

2007-10-13 Thread Marc 'BlackJack' Rintsch
On Fri, 12 Oct 2007 19:09:46 -0700, 7stud wrote:

 On Oct 12, 2:43 pm, Marc 'BlackJack' Rintsch [EMAIL PROTECTED] wrote:
 You mean literally!?  Then of course I get A\xcc\x88 because that's what I
 entered.  In string literals in source code the backslash has a special
 meaning but `raw_input()` does not interpret the input in any way.

 
 Then why don't I end up with the same situation as this:
 
   s = 'A\xcc\x88'   #capital A with umlaut
   print s   #displays capital A with umlaut

I don't get the question!?  In string literals in source code the
backslash has a special meaning, like I wrote above.  When Python compiles
that above snippet you end up with a string of three bytes, one with the
ASCII value of an 'A' and two bytes where you typed in the byte value in
hexadecimal:

In [191]: s = 'A\xcc\x88'

In [192]: len(s)
Out[192]: 3

In [193]: map(ord, s)
Out[193]: [65, 204, 136]

In [194]: print s
Ä

The last works this way only if the receiving/displaying program expected
UTF-8 as encoding.  Otherwise something other than an Ä would have been
shown.

If you type in that text when asked by `raw_input()` then you get exactly
what you typed because there is no Python source code compiled:

In [195]: s = raw_input()
A\xcc\x88

In [196]: len(s)
Out[196]: 9

In [197]: map(ord, s)
Out[197]: [65, 92, 120, 99, 99, 92, 120, 56, 56]

In [198]: print s
A\xcc\x88

  And what is it that your keyboard enters to produce an 'a' with an
  umlaut?

 *I* just hit the  key.  The one right next to the ö key.  ;-)

 ...and what if you don't have an a-with-umlaut key?

I find other means to enter it.  Alt + some magic number on the numeric
keypad in windows, or Compose, a,  on Unix/Linux.  Some text editors
offer special sequences too.  If all fails there are character map
programs that show all unicode characters to choose from and copy'n'paste
them.

Ciao,
Marc 'BlackJack' Rintsch
-- 
http://mail.python.org/mailman/listinfo/python-list

Re: remove header line when reading/writing files

2007-10-12 Thread Marc 'BlackJack' Rintsch
On Thu, 11 Oct 2007 22:52:55 +, RyanL wrote:

 I'm a newbie with a large number of data files in multiple
 directories.  I want to uncompress, read, and copy the contents of
 each file into one master data file.  The code below seems to be doing
 this perfectly.  The problem is each of the data files has a header
 row in the first line, which I do not want in the master file.  How
 can I skip that first line when writing to the master file?  Any help
 is much appreciated.  Thank you.

Untested version with `itertools.islice()`:

import glob
import gzip
import os
from itertools import islice


def main():
zipdir = 'G:/Research/Data/'
outfilename = 'G:/Research/Data/master_data.txt'
out_file = open(outfilename, 'w')
for name in os.listdir(os.curdir):
if os.path.isdir(name):
os.chdir(name)
for zip_name in glob.glob('*.gz'):
in_file = gzip.GzipFile(zip_name, 'r')
out_file.writelines(islice(in_file, 1, None))
in_file.close()
os.chdir(os.pardir)
out_file.close()

Ciao,
Marc 'BlackJack' Rintsch
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: raw_input() and utf-8 formatted chars

2007-10-12 Thread Marc 'BlackJack' Rintsch
On Fri, 12 Oct 2007 13:18:35 -0700, 7stud wrote:

 On Oct 12, 1:18 pm, [EMAIL PROTECTED] wrote:
 On Oct 12, 1:53 pm, 7stud [EMAIL PROTECTED] wrote:

  s = 'A\xcc\x88'   #capital A with umlaut
  print s   #displays capital A with umlaut

  s = raw_input('Enter: ')   #A\xcc\x88
  print s#displays A\xcc\x88

  print len(input)   #9

  It looks like every character of the string I enter in utf-8 is being
  interpreted literally as 9 separate characters rather than one
  character.  How do I enter a capital A with an umlaut so that python
  treats it as one character?

 I don't know. This works for me:



  x = raw_input('Enter: ')
 Enter: 
  len(x)
 1

 I'm using Python 2.4 with Default Source Encoding set to None on
 Windows XP SP2.

 Mike
 
 Yeah, but what happens when you enter A\xcc\x88?

You mean literally!?  Then of course I get A\xcc\x88 because that's what I
entered.  In string literals in source code the backslash has a special
meaning but `raw_input()` does not interpret the input in any way.

 And what is it that your keyboard enters to produce an 'a' with an umlaut?

*I* just hit the ä key.  The one right next to the ö key.  ;-)

Ciao,
Marc 'BlackJack' Rintsch
-- 
http://mail.python.org/mailman/listinfo/python-list

Re: gdbm troubles.

2007-10-11 Thread Marc 'BlackJack' Rintsch
On Thu, 11 Oct 2007 11:49:05 +, Shriphani wrote:

 dictionary = gdbm.open('dictionary','c')
 dictionary['Ellipsize'] = 'Openbox'
 dictionary.get('Ellipsize')
 
 the last line generates an attribute error. Can someone tell me what I
 am doing wrong?

You are trying to use a method that does not exist.  That `gdbm` object
doesn't have a `get()` method.

Ciao,
Marc 'BlackJack' Rintsch
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Declarative properties

2007-10-11 Thread Marc 'BlackJack' Rintsch
On Thu, 11 Oct 2007 11:48:18 +, Artur Siekielski wrote:

 class Person(object):
def __init__(self, name):
   self._name = name
def _get_name(self):
   return self._name
def _set_name(self, new_name):
   self._name = new_name
name = property(_get_name, _set_name)

This is more easily spelled:

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

 I would like to have something like that:
 
 class Person(object):
name = property('_name')
 
 I assume that this causes generation of instance field '_name' and
 default getters and setters.

But why?  Default getters and setters are unnecessary and if you need
something other than the default you need to write it anyway more
explicitly.

Ciao,
Marc 'BlackJack' Rintsch
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Declarative properties

2007-10-11 Thread Marc 'BlackJack' Rintsch
On Thu, 11 Oct 2007 13:04:53 +, Artur Siekielski wrote:

 On Oct 11, 2:27 pm, Marc 'BlackJack' Rintsch [EMAIL PROTECTED] wrote:
 But why?  Default getters and setters are unnecessary and if you need
 something other than the default you need to write it anyway more
 explicitly.
 
 I see some problems with your approach:
 
 1. If I use instance field 'name' which is accessed directly by other
 classes,
 and later I decide to implement nonstandard getter, I must refactor
 'Person' class
 and in some places change 'name' to '_name' (assuming this is now the
 field's name).
 The problem is that I cannot automatically change 'name' to '_name'
 everywhere, because
 in some places I want public property value (eg. validated and
 formatted), and in other
 places raw property value.

So what?  Otherwise you carry *always* the baggage of a public property
and a private attribute whether you need this or not.  At least for me it
would be unnecessary in most cases.

 2. Properties define (a part of) public interface of a class. When
 using fields for public
 access, you must tell this explicitly in documentation, or use name
 convention. And public
 fields definition is mixed with private fields, which doesn't happen
 when using properties.

Attributes are public unless you start the name with an underscore which is
by convention a warning to not use that attribute unless you know what you
are doing.  With properties all over the place you use that convention
anyway because the real value for `name` is bound as `_name`.  So you
clutter your API with two attributes for non-method-attributes.

And a property is not automatically part of the public API, just if the
name does not start with an underscore.

 3. Using properties as first-class objects gives possibilities to use
 declarative approaches for constructing them.

No idea what you mean here.  Everything in Python is an object.

Ciao,
Marc 'BlackJack' Rintsch
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Declarative properties

2007-10-11 Thread Marc 'BlackJack' Rintsch
On Thu, 11 Oct 2007 15:39:29 +, Artur Siekielski wrote:

 On Oct 11, 4:21 pm, [EMAIL PROTECTED] [EMAIL PROTECTED] wrote:
  2. Properties define (a part of) public interface of a class. When
  using fields for public access, you must tell this explicitly in
  documentation, or use name convention.

 Property vs. attribute doesn't make any difference here: both of them
 are public, and part of the external interface, unless they're named
 with a leading underscore.
 
 Nice thing about properites is that they are defined in more declarative
 way - in class body. Attributes are defined in a piece of code (which
 can be long and can contain loops etc.) by assigning a value to 'self'.

The convention is to bind all attributes in `__init__()`, possibly to
`None` if the real value is not available at that time, and document at
least the public ones in the class' docstring.  Tools like `pylint` check
for attributes that are introduced in other methods than `__init__()` and
give a warning.

Ciao,
Marc 'BlackJack' Rintsch
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Declarative properties

2007-10-11 Thread Marc 'BlackJack' Rintsch
On Thu, 11 Oct 2007 09:58:48 -0700, Dan Stromberg wrote:

 On Thu, 11 Oct 2007 13:46:12 +, Marc 'BlackJack' Rintsch wrote:
 
 On Thu, 11 Oct 2007 13:04:53 +, Artur Siekielski wrote:
 
 1. If I use instance field 'name' which is accessed directly by other
 classes,
 and later I decide to implement nonstandard getter, I must refactor
 'Person' class
 and in some places change 'name' to '_name' (assuming this is now the
 field's name).
 The problem is that I cannot automatically change 'name' to '_name'
 everywhere, because
 in some places I want public property value (eg. validated and
 formatted), and in other
 places raw property value.
 
 So what?  Otherwise you carry *always* the baggage of a public property
 and a private attribute whether you need this or not.  At least for me it
 would be unnecessary in most cases.
 
 That baggage of carrying around unneeded methods is something the
 computer carries for you - IE, no big deal in 99.99% of all cases.

It shows twice as much attributes if I inspect the objects and I don't know
which are merely useless default getters and setters.  And it is more and
more complex code.  Code can be cut down a bit by some metaclass magic but
this brings in another complexity.

 The baggage of possibly fixing (AKA generalizing) how your attributes
 are accessed is something you lug around while your deadline looms.

Sorry I don't get it.  If I want to customize the access to a normal
attribute I simply turn it into a property.

 Here's some code that defines such methods for you:
 
 #!/usr/bin/env python
 
 def gimme_set_get(foo, attribute):
lst = [ \
   'def set_%s(self, value):' % attribute, \
   '  self._%s = value' % attribute, \
   'def get_%s(self):' % attribute, \
   '  return self._%s' % attribute, \
   'foo.set_%s = set_%s' % (attribute, attribute), \
   'foo.get_%s = get_%s' % (attribute, attribute) \
   ]
s = '\n'.join(lst)
code = compile(s, 'string', 'exec')
eval(code)
 
 class foo:
def __init__(self, value):
   self.public_value = value
 gimme_set_get(foo, 'via_accessor_method_only')
 
 f = foo(1)
 f.set_via_accessor_method_only(1/9.0)
 print f.get_via_accessor_method_only()
 
 print dir(f)

And the benefit of this evil ``eval`` dance is exactly what!?

Ciao,
Marc 'BlackJack' Rintsch
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: determining fully qualified package class name

2007-10-11 Thread Marc 'BlackJack' Rintsch
On Thu, 11 Oct 2007 11:18:33 -0700, patrimith wrote:

 I am used to the following with Java:
 
 import some.package.MyClass;
 name = MyClass.class.getName();
 
 The value for name will be some.package.MyClass.
 
 For Python, I find:
 
 from some.package.myclass import MyClass
 name = MyClass.__name__
 
 The value for  name will be MyClass
 
 Is there a comparable way to get the fully qualified name (package, module,
 and class name) in Python?

Take a look at the `__module__` attribute of the class.

Ciao,
Marc 'BlackJack' Rintsch
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: .join(string_generator()) fails to be magic

2007-10-11 Thread Marc 'BlackJack' Rintsch
On Thu, 11 Oct 2007 01:26:04 -0500, Matt Mackal wrote:

 I have an application that occassionally is called upon to process
 strings that are a substantial portion of the size of memory. For
 various reasons, the resultant strings must fit completely in RAM.
 Occassionally, I need to join some large strings to build some even
 larger strings.
 
 Unfortunately, there's no good way of doing this without using 2x the
 amount of memory as the result. You can get most of the way there with
 things like cStringIO or mmap objects, but when you want to actually
 get the result as a Python string, you run into the copy again.
 
 Thus, it would be nice if there was a way to join the output of a
 string generator so that I didn't need to keep the partial strings in
 memory. subject would be the obvious way to do this, but it of
 course converts the generator output to a list first.

Even if `str.join()` would not convert the generator into a list first,
you would have overallocation.  You don't know the final string size
beforehand so intermediate strings must get moved around in memory while
concatenating.  Worst case: all but the last string are already
concatenated and the last one does not fit into the allocated memory
anymore, so there is new memory allocates that can hold both strings -
double amount of memory needed.

Ciao,
Marc 'BlackJack' Rintsch
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: .join(string_generator()) fails to be magic

2007-10-11 Thread Marc 'BlackJack' Rintsch
On Thu, 11 Oct 2007 07:02:10 +, thebjorn wrote:

 On Oct 11, 8:53 am, Marc 'BlackJack' Rintsch [EMAIL PROTECTED] wrote:
 Even if `str.join()` would not convert the generator into a list first,
 you would have overallocation.  You don't know the final string size
 beforehand so intermediate strings must get moved around in memory while
 concatenating.  Worst case: all but the last string are already
 concatenated and the last one does not fit into the allocated memory
 anymore, so there is new memory allocates that can hold both strings -
 double amount of memory needed.

 Perhaps realloc() could be used to avoid this?  I'm guessing that's
 what cStringIO does, although I'm too lazy to check (I don't have
 source on this box). Perhaps a cStringIO.getvalue() implementation
 that doesn't copy memory would solve the problem?

How could `realloc()` solve that problem?  Doesn't `realloc()` copy the
memory too if the current memory block can't hold the new size!?

And `StringIO` has the very same problem, if the `getvalue()`
method doesn't copy you have to make copies while writing to the `StringIO`
object and the buffer is not large enough.

Ciao,
Marc 'BlackJack' Rintsch
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: unpickle from URL problem

2007-10-10 Thread Marc 'BlackJack' Rintsch
On Wed, 10 Oct 2007 05:58:51 +, Alan Isaac wrote:

 I am on a Windows box.
 
 I pickle a tuple of 2 simple objects with the pickle module.
 It pickles fine.  It unpickles fine.
 
 I upload to a server.
 I try to unpickle from the URL.  No luck.  Try it:
 x1, x2 = 
 pickle.load(urllib.urlopen('http://www.american.edu/econ/notes/hw/example1'))
 
 I change the filetype to unix.  I upload again.
 I try to unpickle from the URL.  Now it works.  Try it:
 x1, x2 = 
 pickle.load(urllib.urlopen('http://www.american.edu/econ/notes/hw/example2'))
 
 Why the difference?

Pickles are *binary* files, not text files, so make sure you always treat
them as binary, e.g. opening the files with mode 'rb' and 'wb' and don't
transmit them in text mode over FTP etc.

Ciao,
Marc 'BlackJack' Rintsch
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: function wrappers

2007-10-10 Thread Marc 'BlackJack' Rintsch
On Wed, 10 Oct 2007 12:39:24 +0200, Ramon Crehuet wrote:

 def require_int(func):
def wrapper(arg):
assert isinstance(arg, int)
return func(arg)
return wrapper
 p1(a):
print a
 
 p2=require_int(p1)
 
 My question is: why do p2 arguments become wrapper arguments? What is 
 the flux of the arguments in the program when you pass functions as 
 arguments?

The function `p1` is passed into `require_int`.  It is bound to the local
name `func` in `require_int`.  Everytime `require_int` is called a new
function object is created and bound to the local name `wrapper`.  The
name `func` in that new function object refers to the object bound to
`func` in the `require_int` namespace.  Then the new function is returned
still carrying a reference to the `func` object that was passed into
`require_int`.

Ciao,
Marc 'Blackjack' Rintsch
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: unpickle from URL problem

2007-10-10 Thread Marc 'BlackJack' Rintsch
On Wed, 10 Oct 2007 15:21:06 +, Alan Isaac wrote:

 Marc 'BlackJack' Rintsch wrote:
 Pickles are *binary* files, not text files
 
 Actually not:
 http://docs.python.org/lib/node316.html
 
 These were created with protocol 0.

Actually yes, the docs are wrong.  It's a binary file with bytes
constraint to ASCII values.  Nevertheless it breaks if you don't open the
files in binary mode and try to use it on a platform that treats line
endings differently.

Ciao,
Marc 'BlackJack' Rintsch
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: I'm starting to think like a Pythonista

2007-10-10 Thread Marc 'BlackJack' Rintsch
On Wed, 10 Oct 2007 17:03:41 -0400, brad wrote:

 Bjoern Schliessmann wrote:
 brad wrote:
 low_odds = [1,3,5,7,9]
 # make a list containing 10 - 98 evens only
 big_evens =  big_evens = [x for x in list(xrange(99)) if x % 2 ==
 0 and x 8]
 
 Why use xrange if you convert it to a full list in place? No
 advantage there.
 
 What is the dis-advantage of using xrange over range in this circumstance?

It's an unnecessary intermediate step.

Ciao,
Marc 'BlackJack' Rintsch
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Static variable vs Class variable

2007-10-09 Thread Marc 'BlackJack' Rintsch
On Tue, 09 Oct 2007 09:16:12 -0700, [EMAIL PROTECTED] wrote:

 I've got a question on the differences and how to define static and
 class variables.

First you have to define what you mean by static.

 AFAIK, class methods are the ones which receives the
 class itself as an argument, while static methods are the one which
 runs statically with the defining class.

`classmethod`\s receive the class as first arguments, `staticmethod`\s are
just functions bound to the class object.

 Hence, my understanding is that static variables must be bound to the
 class defining the variables and shared by children of parent class
 where the variable is defined. But, please have a look at this code in
 which a guy told me that the variable a is static:

Ask the guy what he means by static.

 class Foo:
   a = 1
   @classmethod
   def increment(cls):
   cls.a += 1
   print cls.a
 
 Here, I am defining variable a which, I believe is class variable,
 i.e., variable that is not bound to Foo itself.

No you define a class attribute that *is* bound to the class `Foo`.

 Rather, a is bound to the class which is accessing the variable. The code
 that corroborates this idea is as follows:
 
 class Child1(Foo):
   pass
 
 Child1.increment()
 4

Four!?  Hard to believe.

 class Child2(Foo):
   pass
 
 Child2.increment()
 4
 
 This means that Child1 and Child2 does not share variable a which means
 that variable a is class variable rather than static variable.
 
 Could you please comment on this? Is a static or class variable? What's
 the most recent way of defining 'class' and 'static' variables?

There is no such thing as a static variable.  Think of attributes that
are bound to objects.  All dynamically.

What happens is: you bind a 1 to the attribute `Foo.a` in the `Foo` class
definition.

When you call `Child1.increment()` the class method will be called with
`Child1` as first argument.  Now ``cls.a += 1`` is executed which is
somewhat like a short form of ``cls.a = cls.a + 1``.  So this is reading
the attribute `a` from `Child1` and then bind the result to `Child1`. 
`Child1` doesn't have an attribute `a`, so it is looked up in the parent
class.  But the result is then bound to `Child1`.  So you are reading from
`Foo` and writing to `Child1`.  That's it.

Ciao,
Marc 'BlackJack' Rintsch
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Static variable vs Class variable

2007-10-09 Thread Marc 'BlackJack' Rintsch
On Tue, 09 Oct 2007 18:08:34 +, Steven D'Aprano wrote:

 L = []
 id(L)
 3083496716L
 L += [1]
 id(L)
 3083496716L
 
 It's the same L, not rebound at all.

It *is* rebound.  To the same object, but it *is* assigned to `L` and not
just mutated in place.

In [107]: class A:
   .: a = list()
   .:

In [108]: class B(A):
   .: pass
   .:

In [109]: B.a += [42]

In [110]: A.a
Out[110]: [42]

In [111]: B.a
Out[111]: [42]

If it was just mutation then `B.a` would have triggered an `AttributeError`.

Ciao,
Marc 'BlackJack' Rintsch
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Static variable vs Class variable

2007-10-09 Thread Marc 'BlackJack' Rintsch
On Tue, 09 Oct 2007 22:43:16 +, Steven D'Aprano wrote:

 On Tue, 09 Oct 2007 19:46:35 +, Marc 'BlackJack' Rintsch wrote:
 
 On Tue, 09 Oct 2007 18:08:34 +, Steven D'Aprano wrote:
 
 L = []
 id(L)
 3083496716L
 L += [1]
 id(L)
 3083496716L
 
 It's the same L, not rebound at all.
 
 It *is* rebound.  To the same object, but it *is* assigned to `L` and
 not just mutated in place.
 
 Picky picky.

 Yes, technically there is an assignment of L to itself. I was sloppy to 
 say not rebound at all, because when you write an augmented assignment 
 method you have to return self if you want to implement in-place 
 mutation. But I hardly call rebinding to itself any sort of rebinding 
 worth the name :)

Maybe picky but that detail was the source of the OPs confusion because it
introduced a new attribute on the subclass.

Ciao,
Marc 'BlackJack' Rintsch
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: weakrefs and bound methods

2007-10-07 Thread Marc 'BlackJack' Rintsch
On Sun, 07 Oct 2007 16:51:33 +0200, Mathias Panzenboeck wrote:

 import weakref
 
 class Wrapper(object):
   def __init__(self,x):
   self.x = weakref.ref(x)
 
   def __call__(self,*args,**kwargs):
   x = self.x()
   if x is None:
   print lost reference
   else:
   return x(*args,**kwargs)
 
 class Foo(object):
   def __init__(self):
   self._methods = set()
   self._methods.add(Wrapper(self._foo))
 
   def _foo(self):
   print _foo
 
   def callMethods(self):
   for method in self._methods:
   method()
 
   def __del__(self):
   print del Foo
 
 class Bar(object):
   def __init__(self):
   self._methods = set()
   self._methods.add(self._foo)
 
   def _foo(self):
   print _foo
 
   def callMethods(self):
   for method in self._methods:
   method()
 
   def __del__(self):
   print del Bar
 
 
 
 Now look what happens when I do this:
 
 f=Foo()
 f.callMethods()
 lost reference
 del f
 del Foo
 b=Bar()
 b.callMethods()
 _foo
 del b

 
 Foo looses the reference to its method but Bar on the other hand has a 
 refloop and
 never gets deleted.

``del b`` just deletes the name `b`.  It does not delete the object. 
There's still the name `_` bound to it in the interactive interpreter. 
`_` stays bound to the last non-`None` result in the interpreter.

Drop all those `__del__()` methods as they prevent the garbage collector
from collecting cycles.

Ciao,
Marc 'BlackJack' Rintsch
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: module confusion

2007-10-06 Thread Marc 'BlackJack' Rintsch
On Sat, 06 Oct 2007 19:16:47 +1300, Lawrence D'Oliveiro wrote:

 In message [EMAIL PROTECTED], Marc 'BlackJack' Rintsch
 wrote:
 
 To me a `variable` is made of a name, a memory address, a data type, and
 a value.  In languages like C the address and type are attached to the
 name while in Python both are attached to the value.
 
 How does C++ with RTTI fit into your picture, then?

I'm no C++ expert but I'd say the type is attached to the value too there.
Same is true for Java.

Ciao,
Marc 'BlackJack' Rintsch
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: module confusion

2007-10-05 Thread Marc 'BlackJack' Rintsch
On Fri, 05 Oct 2007 19:51:05 +1300, Lawrence D'Oliveiro wrote:

 It is not the _name_ that is being reassigned, it is the _variable_ that
 the name is bound to. All names in Python are bound to variables at all
 times.

I think this is the source of the confusion.  Most people don't seem
to share your definition of `variable` above.

To me a `variable` is made of a name, a memory address, a data type, and
a value.  In languages like C the address and type are attached to the
name while in Python both are attached to the value.

Ciao,
Marc 'BlackJack' Rintsch
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: remove list elements..

2007-10-05 Thread Marc 'BlackJack' Rintsch
On Fri, 05 Oct 2007 11:45:07 -0400, Steve Holden wrote:

 [EMAIL PROTECTED] wrote:
 On Oct 5, 10:27 am, Abandoned [EMAIL PROTECTED] wrote:
 Hi..
 I have a problem..
 list1=[11, 223, 334, 4223...] 1 million element
 list2=[22,223,4223,2355...] 500.000 element

 I want to difference list1 to list2 but order very importent..

 My result must be:
 list3=[11,334,...]

 I do this use FOR easly but the speed very imported for me. I want to
 the fastest method please help me.
 
 Research the set data type. :)
 
 Probably not a very helpful suggestion given that ordering is stated to 
 be very important.

A `set()` can be part of such a solution.

Ciao,
Marc 'BlackJack' Rintsch
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: toprettyxml messes up with whitespaces

2007-10-03 Thread Marc 'BlackJack' Rintsch
On Wed, 03 Oct 2007 12:18:45 +0200, Jorgen Bodde wrote:

 Which part of the standard is this? Here's the XML 1.0 specification's
 section on whitespace:

 http://www.w3.org/TR/2006/REC-xml-20060816/#sec-white-space
 
 Well 2.10 if I quote:
 
 quote
 Such white space is typically not intended for inclusion in the
 delivered version of the document. On the other hand, significant
 white space that should be preserved in the delivered version is
 common, for example in poetry and source code.
 /quote
 
 I interpret significant whitespaces as the ones between the words,
 if whitespaces occur at the beginning of a line due to an indent like

Significant whitespace is all whitespace in nodes that may contain text. 
You need a DTD or schema to decide this, that's why all pretty printing
without a DTD or schema is broken IMHO.  Because you then simply don't
know if it is safe to strip or add whitespace.

 value
  This is indented text
 /value
 
 We can assume that the spaces in front of it are not significant
 whitespaces.

I can't.  You are just guessing.

 Because when I read the text node in python and it is not
 included, I see no reason why it should be preserved.

But it should be included.

Ciao,
Marc 'BlackJack' Rintsch
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: module confusion

2007-10-02 Thread Marc 'BlackJack' Rintsch
On Tue, 02 Oct 2007 19:34:29 +1300, Lawrence D'Oliveiro wrote:

 In message [EMAIL PROTECTED], Robert
 Kern wrote:
 
 Not all of the modules in a package are imported by importing the
 top-level package.
 
 You can't import packages, only modules.

Oh come on, this is unnecessary nitpicking.  Importing the module
`__init__` from a package using the name of the package is close enough to
justify the phrase I import the package IMHO.

Ciao,
Marc 'BlackJack' Rintsch
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: C Source Code Generator For Test Cases

2007-09-29 Thread Marc 'BlackJack' Rintsch
On Fri, 28 Sep 2007 12:57:49 -0700, gamename wrote:

 How about using c-types to access your C-stuff to test, and use python + the
 testcase-tables to invoke that?

 
 Sure, that's possible.  But the source code for tests (once all the
 parms are read)
 still needs to be generated.  Calling the lib from python or from C,
 there still
 needs to be a way to generate 100+ test routines. ;-)

Instead of reading the testcase tables and generating source for test
routines you simply can do the tests right away.

Ciao,
Marc 'BlackJack' Rintsch
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Python 3.0 migration plans?

2007-09-28 Thread Marc 'BlackJack' Rintsch
On Fri, 28 Sep 2007 09:42:49 -0700, TheFlyingDutchman wrote:

 Which of the common languages have higher order functions and what is
 the syntax?

C, C++, Pascal, Perl, PHP, Ruby have them.  And of course the functional
languages, most notably Lisp and Scheme as you asked for common languages.

Don't know if C#'s delegates qualify.

Ciao,
Marc 'BlackJack' Rintsch
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: ValueError: too many values to unpack,

2007-09-27 Thread Marc 'BlackJack' Rintsch
On Thu, 27 Sep 2007 12:36:58 -0400, Shawn Minisall wrote:

 With the multiple value lines, python says this ValueError: too many 
 values to unpack
 
 I've googled it and it says that happens when you have too few or too 
 many strings that don't match with the variables in number your trying 
 to assign them too.  Below are the lines in reading in:
 
 line 3 - 19.1829.1578.75212.10
 line 4 - 10020410.29   
 
 And this is the code I'm using:
 
 #read withdrawls from file on line3
 line = infile.readline()

 #split withdrawls up
 withdraw1, withdraw2, withdraw3, withdraw4 = string.split(line, \t)
 
 #read deposits from file on line4
 line = infile.readline()
 #split deposits up
 deposit1, deposit2, deposit3 = string.split(line, \t)
 
 I have 4 strings to match line 3 and 3 to match the 3 on line 4...any 
 thoughts?

First thought is to find out which of the two lines triggers the
exception.  This information is part of the full traceback.

Ciao,
Marc 'BlackJack' Rintsch
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: True of False

2007-09-27 Thread Marc 'BlackJack' Rintsch
On Thu, 27 Sep 2007 09:33:34 -0700, koutoo wrote:

 I tried writing a true and false If statement and didn't get
 anything?  I read some previous posts, but I must be missing
 something.  I just tried something easy:
 
 a = [a, b, c, d, e, f]
 
 if c in a == True:
  Print Yes
 
 When I run this, it runs, but nothing prints.  What am I doing wrong?

Wow that's odd:

In [265]: a = list('abcdef')

In [266]: a
Out[266]: ['a', 'b', 'c', 'd', 'e', 'f']

In [267]: 'c' in a
Out[267]: True

In [268]: 'c' in a == True
Out[268]: False

In [269]: ('c' in a) == True
Out[269]: True

In [270]: 'c' in (a == True)
---
type 'exceptions.TypeError' Traceback (most recent call last)

/home/bj/ipython console in module()

type 'exceptions.TypeError': argument of type 'bool' is not iterable


What's going on there?

Ciao,
Marc 'BlackJack' Rintsch
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: comparing elements of a list with a string

2007-09-27 Thread Marc 'BlackJack' Rintsch
On Thu, 27 Sep 2007 08:16:59 -0400, Steve Holden wrote:

 Shriphani wrote:
 Hello,
 Would that mean that if I wanted to append all the (date, time) tuples
 to a list, I should do something like:
 
 for file in list_of_backup_files:
 some_list.append(file)
 
 That would be one way to do it (assuming you started with some_list as 
 an empty list). But a faster way would be to use a list comprehension 
 and say
 
 some_list = [f for f in list_of_backup_files]

Or:

some_list = list(list_of_backup_files)

If `some_list` is not empty:

some_list.extend(list_of_backup_files)

Ciao,
Marc 'BlackJack' Rintsch
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: True of False

2007-09-27 Thread Marc 'BlackJack' Rintsch
On Thu, 27 Sep 2007 17:06:30 +, Duncan Booth wrote:

 Marc 'BlackJack' Rintsch [EMAIL PROTECTED] wrote:
 
 In [268]: 'c' in a == True
 Out[268]: False
 
 In [269]: ('c' in a) == True
 Out[269]: True
 
 In [270]: 'c' in (a == True)
 ---
  
type 'exceptions.TypeError' Traceback (most recent call
last) 
 
 /home/bj/ipython console in module()
 
type 'exceptions.TypeError': argument of type 'bool' is not iterable
 
 
 What's going on there?
 
 See http://docs.python.org/ref/comparisons.html
 
 Comparisons can be chained arbitrarily, e.g., x  y = z is equivalent
 to x  y and y = z, except that y is evaluated only once (but in both
 cases z is not evaluated at all when x  y is found to be false). 
 
 In exactly the same way:
 
'c' in a == True
 
 is equivalent to:
 
'c' in a and a == True
 
 which is False.

Aaah *enlightenment*, I'm using this for range checks like in the docs,
but it wasn't obvious to me in this case.  Thanks.

Ciao,
Marc 'BlackJack' Rintsch
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Inserting an element into existing xml file

2007-09-25 Thread Marc 'BlackJack' Rintsch
On Tue, 25 Sep 2007 03:30:05 -0700, Anand wrote:

 I'm Afraid to say, I can't use lxml or elementTree as it requires many
 legal approvals and there is high chances of not getting it through.

In what environment is it hard to get something BSD licensed through!?

Ciao,
Marc 'BlackJack' Rintsch
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Would Anonymous Functions Help in Learning Programming/Python?

2007-09-24 Thread Marc 'BlackJack' Rintsch
On Sun, 23 Sep 2007 18:32:28 -0700, Kay Schluehr wrote:

 On 22 Sep., 02:14, Bruno Desthuilliers
 [EMAIL PROTECTED] wrote:
 Kay Schluehr a crit :
 (snip)

  I checked out Io once and I disliked it. I expected Io's prototype OO
  being just a more flexible variant of class based OO but Io couples a
  prototype very closely to its offspring. When A produces B and A.f is
  modified after production of B also B.f is modified. A controls the
  state of B during the whole lifetime of B. I think parents shall not
  do this, not in real life and also not in programming language
  semantics.

 I may totally miss the point, but how is this different from:

 class A(object):
  def dothis(self):
  print A.dothis(%s) % self

 class B(A):
  pass

 b = B()

 b.dothis()

 def dothat(self):
  print function dothat(%s) % self

 A.dothis = dothat
 b.dothis()
 
 It's not a big deal because you do not use a class to propagate
 mutable state unless you are using a class attribute intentionally ( i
 guess you do not overuse it just to avoid object encapsulation and
 make everything global ). When using a prototype as in Io you need to
 protect the state of the child object yourself.

Just like in Python!  Here `things` are shared between all children:

class A(object):
things = list()

def add(self, thing):
self.things.append(thing)

This is what happens too when you use this Io snippet:

A := Object clone do(
things := list()

add := method(thing,
self things append(thing)
)
)

If you don't want `things` to be shared you write an `init` method, in
both Python and Io.  Python:

class B(object):
def __init__(self):
self.things = list()

def add(self, thing):
self.things.append(thing)

And Io:

B := Object clone do(
init := method(
self things := list()
self
)

add := method(thing,
self things append(thing)
)
)

The `init` is called by the default `clone` method automatically just like
`__init__()` in Python.  It is really much like the class/instance
relationship in Python with just the line between class and instance
blurred.

 You can do this by overwriting the objects slots but then you end up
 writing your own object constructors and the templates accordingly, also
 named classes by some. Not having dedicated object constructors,
 member variable initializations and the presumed class definition
 boilerplate comes at the price of introducing them on your own.

The mechanism is already there in Io, no need to invent, just use it.

Ciao,
Marc 'BlackJack' Rintsch
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Passing parameters at the command line (New Python User)

2007-09-24 Thread Marc 'BlackJack' Rintsch
On Mon, 24 Sep 2007 01:04:58 -0700, cjt22 wrote:

 for i in range(len(args)):
 if args[i] == -no:
 Initialise.init(0)
 Process.processCon(file2,1)
 Output.print()
 
 if args[i] == -not:
Initialise.init(1)
 Process1.process(stepStore, firstSteps)
 Output.print1()

That ``for`` loop is an anti-pattern in Python.  If you want to iterate
over the elements of `args` the just do it directly instead of using an
index:

for arg in args:
if arg == '-no':
# ...

If you need the element *and* an index:

for i, arg in enumarate(args):
# ...

Ciao,
Marc 'BlackJack' Rintsch
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Would Anonymous Functions Help in Learning Programming/Python?

2007-09-24 Thread Marc 'BlackJack' Rintsch
On Mon, 24 Sep 2007 11:43:59 +0200, Bruno Desthuilliers wrote:

 You already can create functions without using the def statement:
 
 Help on class function in module __builtin__:
 
 class function(object)
   |  function(code, globals[, name[, argdefs[, closure]]])
   |
   |  Create a function object from a code object and a dictionary.
   |  The optional name string overrides the name from the code object.
   |  The optional argdefs tuple specifies the default argument values.
   |  The optional closure tuple supplies the bindings for free variables.
   |

Where the heck does *this* come from?  Neither Python 2.5.1 nor the
3.0alpha has this in `__builtin__`.

Ciao,
Marc 'BlackJack' Rintsch
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: rules from an xml file

2007-09-24 Thread Marc 'BlackJack' Rintsch
On Mon, 24 Sep 2007 13:42:05 +0200, Diez B. Roggisch wrote:

 jonny wrote:
 
 rules
rule01
   if
 or
 lessthan par_1=glicemyAtMorning par_2=80/
 lessthan par_1=glicemyAtNight par_2=80/
 and
 greaterthan par_1=glicemyAtMorning par_2=0/
 or
 equalto par_1=urine par_2=0/
 equalto par_1=urine par_2=+/-/
 equalto par_1=urine par_2=+/
 /or
 /and
 /or
 /if
 then
sendmessageSomething is wrong!/sendmessage
 /then
/rule01
rule02 ...  /rule02
rule03 ...   /rule03
 /rules
 […]
 […]
 Just a note: the use of par_X as attributes is unfortunate, to say the
 least. It doesn't allow for easy argument swapping, can cause troubles
 because you delete one attribute and miss renaming the others, and in
 general it's not good design to have arbitrary numbers of parameters.

Quite the same is true for numbers in tag names.  If you (the OP) need to
number the rules better use an attribute for the numbers.

Ciao,
Marc 'BlackJack' Rintsch
-- 
http://mail.python.org/mailman/listinfo/python-list

Re: Can a base class know if a method has been overridden?

2007-09-24 Thread Marc 'BlackJack' Rintsch
On Mon, 24 Sep 2007 15:48:07 +, Ratko wrote:

 I have a base class EvtHandler that has methods defined to handle
 certain events. You then subclass from EvtHandler and override the
 methods for the events you want to receive. If a method has been
 overridden, the base class will automatically register for those
 events to make sure that they are even delivered to this handler
 (which is why I would need to know whether a method has been
 overridden or not). Of course, there are other ways of doing this
 which would require a bit more work from the subclass... I just
 thought this would be a neat automatic way of registering for
 events.
 
 For example:
 
 class EvtHandler:
 def __init__(self):
 if onKey is overridden:
  register_for_key_events()
 
 def onKey(self):
 pass
 
 
 class MyHandler(EvtHandler):
 def onKey(self):
 # do something here

Maybe tagging the original `on_key()`:

class EvtHandler:
def __init__(self):
if not hasattr(self.on_key, 'dummy'):
 print 'register_for_key_events()'

def _dummy_handler(self):
pass
_dummy_handler.dummy = True

on_key = _dummy_handler
on_whatever = _dummy_handler


class MyHandler(EvtHandler):
def on_key(self):
print 'Do something...'

Ciao,
Marc 'BlackJack' Rintsch
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: puzzled about floats

2007-09-24 Thread Marc 'BlackJack' Rintsch
On Mon, 24 Sep 2007 14:13:18 -0400, Neal Becker wrote:

 from math import modf
 
 class nco (object):
 def __init__ (self, delta):
 self.delta = delta
 self.phase = 0.0
 def __call__ (self):
 self.phase += self.delta
 f,i = modf (self.phase)
 print  modf (self.phase)
 if (self.phase  1.0):
 self.phase -= 1.0
 return self.phase
 
 n = nco (0.1)
 for x in xrange (100):
 print '%.12f' % n()
 
 prints out
 [...]
 (0.99978, 0.0)from modf
 1.from n()
 
 I'm baffled as to why 'print modf (self.phase)' prints out the first value,
 but the result printed in the 2nd case is different.  Without any precision
 spec on the first print, an approximate float value was printed, but even
 with %.12f, the second gives exactly 1.000

Tuples are printed with calling `repr()` on the objects:

In [144]: str(0.1)
Out[144]: '0.1'

In [145]: repr(0.1)
Out[145]: '0.10001'

In [146]: '%.12f' % 0.1
Out[146]: '0.1000'

In [147]: '%.50f' % 0.1
Out[147]: '0.1555111512312578270211815834045410'

Ciao,
Marc 'BlackJack' Rintsch
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Would Anonymous Functions Help in Learning Programming/Python?

2007-09-22 Thread Marc 'BlackJack' Rintsch
On Fri, 21 Sep 2007 19:52:41 -0400, Carl Banks wrote:

 First of all, let me say that I think functions as first class data is 
 helpful, but not crucial, to programming in Python, and there are many 
 people who simply don't need the lesson.  Especially someone like an 
 engineer (in the classical sense), who isn't building versatile software 
 packages, won't need to resort to functional programming much.

Of course you don't *need* functional programming as in there's no way
around it, but why are engineers such an exception!?  I find the stuff in
`itertools` very handy when it comes to filter, manipulate, group and
aggregate data from large log files in ad hoc scripts.  I'm not an
engineer but I guess there are similar tasks with log files or measurement
results.

 For straightforward tasks, like sorting lists with a custom ordering, 
 functional progamming merely a convenience, and can be done without.

``for`` loops are just a convenience, you can do without.  ;-)

Ciao,
Marc 'BlackJack' Rintsch
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Who can develop the following Python script into working application ?

2007-09-22 Thread Marc 'BlackJack' Rintsch
On Fri, 21 Sep 2007 17:09:04 -0700, http://members.lycos.co.uk/dariusjack/
wrote:

 On Sep 21, 1:26 pm, Diez B. Roggisch [EMAIL PROTECTED] wrote:
 snip/
 Is Python not fit to a task of this kind ?

It is, and it should be quite simple.

 Is there no a library of Python free scripts to accomplish the above
 like many for javascripts ?
 I am asking just of curosity, as an author of this script didn't
 respond to my second request  to tell me how complicated is the
 problem and what made him not to write that script as a fully working
 version.
 
 Is Python so difficult, complicated, even for experienced developers ?

What makes you think it is?

This group is about the Python programming language, discussing it and
helping people to learn it.  It seems that people here don't like fixing
code for free for people who are not interested in the language
themselves.  There are platforms like guru.com or rentacoder.com for such
assignments.

Ciao,
Marc 'BlackJack' Rintsch
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: I could use some help making this Python code run faster using only Python code.

2007-09-22 Thread Marc 'BlackJack' Rintsch
On Fri, 21 Sep 2007 16:25:20 -0700, Python Maniac wrote:

 On Sep 21, 3:02 pm, Matt McCredie [EMAIL PROTECTED] wrote:

 Isn't D compiled to machine code? I would expect it to win hands down.
 That is, unless it is horribly unoptimized.


 Well D code is compiled into machine code that runs via a VM.

About which D are we talking here?  Not digital mars' successor to C++,
right!?

Ciao,
Marc 'BlackJack' Rintsch
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Would Anonymous Functions Help in Learning Programming/Python?

2007-09-22 Thread Marc 'BlackJack' Rintsch
On Sat, 22 Sep 2007 00:47:37 -0700, Kay Schluehr wrote:

 On 22 Sep., 08:56, Paul Rubin http://[EMAIL PROTECTED] wrote:
 Kay Schluehr [EMAIL PROTECTED] writes:
  If you feel you can transform it into another unambigous grammar
  mixing statements and expressions it's up to you.

 We got rid of the print statement for python 3.0.  Why not get rid
 of the rest of them too?  Just use expressions for everything, as
 works fine for plenty of other languages.
 
 One might create a new dynamic functional+OO programming language,
 where small statements like print, assert, raise etc. all become
 functions and statements like while, for, with etc. become anonymous
 closures.

Before someone starts to create such a thing he should take a look at Io
which has just objects and methods.

  http://www.iolanguage.com/

Ciao,
Marc 'BlackJack' Rintsch
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Would Anonymous Functions Help in Learning Programming/Python?

2007-09-22 Thread Marc 'BlackJack' Rintsch
On Sat, 22 Sep 2007 02:44:35 -0700, Kay Schluehr wrote:

 I checked out Io once and I disliked it. I expected Io's prototype OO
 being just a more flexible variant of class based OO but Io couples a
 prototype very closely to its offspring. When A produces B and A.f is
 modified after production of B also B.f is modified.  A controls the
 state of B during the whole lifetime of B. I think parents shall not
 do this, not in real life and also not in programming language
 semantics.

Well it's like Python: inherited slots (attributes) are looked up in the
ancestors.  It should be easy to override `Object clone` in Io, so all
slots of the ancestor are shallow copied to the clone, but I guess this
might break existing code.  At least for your own code you could introduce
a `realClone` slot.

Ciao,
Marc 'BlackJack' Rintsch
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Writing Object Data to Disk

2007-09-22 Thread Marc 'BlackJack' Rintsch
On Sat, 22 Sep 2007 17:13:14 +0530, Amit Kumar Saha wrote:

 BTW, do we have something like array of objects here?

Like someone already said: lists.

Ciao,
Marc 'BlackJack' Rintsch
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Would Anonymous Functions Help in Learning Programming/Python?

2007-09-22 Thread Marc 'BlackJack' Rintsch
On Sat, 22 Sep 2007 06:58:57 -0700, Kay Schluehr wrote:

 On Sep 22, 1:15 pm, Marc 'BlackJack' Rintsch [EMAIL PROTECTED] wrote:
 On Sat, 22 Sep 2007 02:44:35 -0700, Kay Schluehr wrote:
  I checked out Io once and I disliked it. I expected Io's prototype OO
  being just a more flexible variant of class based OO but Io couples a
  prototype very closely to its offspring. When A produces B and A.f is
  modified after production of B also B.f is modified.  A controls the
  state of B during the whole lifetime of B. I think parents shall not
  do this, not in real life and also not in programming language
  semantics.

 Well it's like Python: inherited slots (attributes) are looked up in the
 ancestors.  It should be easy to override `Object clone` in Io, so all
 slots of the ancestor are shallow copied to the clone, but I guess this
 might break existing code.  At least for your own code you could introduce
 a `realClone` slot.
 
 But in class based OO most relevant attributes are defined during
 object construction and they can't be simply changed by setting a
 class attribute. Later you often create new attributes as but on
 object level. You do not overuse the object/class relationship to
 broadcast changes to all objects.

You don't do this in Io either.  It's really like Python in this respect.
There's an `init` slot that gets called by (the default) `clone` and you
set instance attributes there, for example mutables so they are not
shared by all clones.

Ciao,
Marc 'BlackJack' Rintsch
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: too many values with string.split

2007-09-22 Thread Marc 'BlackJack' Rintsch
On Sat, 22 Sep 2007 17:00:47 -0400, Shawn Minisall wrote:

 I'm trying to unpack a list of 5 floats from a list read from a file and 
 python is telling me 5 variables are too many for the string.split 
 statement.

Please post the *real* message which I suspect is something like 'too many
values to unpack', which is the other way around.  The 5 names are not
enough to take all the items from the split.

 #read in data line by line
 for line in infile:
 mylist = string.split(line)

Functions in the `string` module that are also available as methods on
strings are deprecated.

 firstName[counter] = mylist[0]
 lastName[counter] = mylist[1]
 grades[counter] = float(mylist[2])
 print firstName[counter], 
 lastName[counter],:,\t\t,grades[counter]
 #increment counter
 counter = counter + 1

Do you really need the counter?  Can't you just append the values to the
lists?

 #calculates and prints average score
 grades = str(grades)
 num1, num2, num3, num4, num5 = string.split(grades,,)
 average = float(num1 + num2 + num3 + num4 + num5) / 5

This is very strange.  You have a list of floats (I guess), convert that
list to a string, split that string at commas, concatenate the *strings*
between commas and then try to convert it to a `float`!?  This is likely
not what you want and should fail in most cases anyway.

Ciao,
Marc 'BlackJack' Rintsch
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: I could use some help making this Python code run faster using only Python code.

2007-09-22 Thread Marc 'BlackJack' Rintsch
On Sat, 22 Sep 2007 13:00:27 -0700, Python Maniac wrote:

 On Sep 21, 11:39 pm, Marc 'BlackJack' Rintsch [EMAIL PROTECTED] wrote:
 On Fri, 21 Sep 2007 16:25:20 -0700, Python Maniac wrote:

  Well D code is compiled into machine code that runs via a VM.

 About which D are we talking here?  Not digital mars' successor to C++,
 right!?
 
 Yes, Digital Mars D is what I was referring to and yes I know D is not
 as efficient as C++.

But D code doesn't run in a VM unless you see hardware processors as VMs.

Ciao,
Marc 'BlackJack' Rintsch
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Getting rid of bitwise operators in Python 3?

2007-09-22 Thread Marc 'BlackJack' Rintsch
On Sat, 22 Sep 2007 21:17:38 +, Bryan Olson wrote:

 The operator module offers pow(). Is there any good reason for
 pow() as a built-in?

The `operator.pow()` is  just the function for ``**``, it lacks the
optional third argument of the built in `pow()`.

Ciao,
Marc 'BlackJack' Rintsch
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Drawing a 640x480 Raw Image

2007-09-21 Thread Marc 'BlackJack' Rintsch
On Thu, 20 Sep 2007 20:49:59 -0700, W. Watson wrote:

 W. Watson wrote:
 I'm getting a 640x480 greyscale image from a video device. I'd like to 
 place it on a canvas and then draw on the image. Does PIL or some image 
 facility allow me to do that?

 Corrected misspelling in Subject. The image here is nothing more than a 
 640x480 byte array. Each byte is a gra[e]yscale value.

PIL can do this:

from PIL import Image

def main():
width = 640
height = 480
image = Image.new('L', (width, height))
data = [x * y % 256 for x in xrange(width) for y in xrange(height)]
image.putdata(data)
image.save('test.png')

`data` can be any iterable with byte values.

Ciao,
Marc 'BlackJack' Rintsch
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Sets in Python

2007-09-20 Thread Marc 'BlackJack' Rintsch
On Thu, 20 Sep 2007 03:46:08 +, prikar20 wrote:

 On Sep 19, 5:25 pm, Mark Dickinson [EMAIL PROTECTED] wrote:
 On Sep 19, 7:26 pm, Karthik Gurusamy [EMAIL PROTECTED] wrote:

  If I did, a = [10, 20] and I did d[a]= 'foo', then a.append(30).
  If dict complains key error on d[a] now, I won't be surprised. If I do
  d[[10, 20, 30]], I will be surprised if it doesn't find the item. Of
  course, in today's behavior the above is syntax error.

 It sounds as though you're proposing something like the following:

  k = mylist([1, 2])
  d = {k : 'test'}
  d[k]
 'test'
  k.append(3)
  d[k]

 Traceback (most recent call last):
   File stdin, line 1, in module
 KeyError: [1, 2, 3]

 So far, so good.  But how do you explain the following to a confused
 newcomer?

  d.keys()
 [[1, 2, 3]]
  k in d.keys()
 True
  k in d
 False
  d[k]

 Traceback (most recent call last):
   File stdin, line 1, in module
 KeyError: [1, 2, 3]

 In other words, to repeat Sion Arrowsmith's question, what would you
 expect d.keys() to return after a key of d has been modified?
 
 In the new model, it should be the value at the time of addition.
 That is [1,2] (not [1,2,3]). This does mean a copy of key in
 maintained internally in the dict.

A copy!?  That has to be a deep copy.  Which would make `dict`\s alot
slower and use more memory.  Plus you can't store objects that can't be
copied anymore.  That doesn't sound like a good trade off to me.

Ciao,
Marc 'BlackJack' Rintsch
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: os.popen and lengthy operations

2007-09-20 Thread Marc 'BlackJack' Rintsch
On Thu, 20 Sep 2007 10:31:43 +0400, Dmitry Teslenko wrote:

 I'm using os.popen to perform lengthy operation such as building some
 project from source.
 It looks like this:
 def execute_and_save_output( command, out_file, err_file):
 
 import os
 
 def execute_and_save_output( command, out_file, err_file):
   (i,o,e) = os.popen3( command )
   try:
   for line in o:
   out_file.write( line )
 
   for line in e:
   err_file.write( line )
   finally:
   i.close()
   o.close()
   e.close()
 
 ...
 execute_and_save_output( 'some long to run command', out_file, err_file)
 
 Problem is that script hangs on operations that take long to execute
 and have lots of output such as building scripts.

Your code reads from the process' stdout until there is nothin to read
anymore and then from stderr.  The process might output something to both.
The output is buffered.  And when the stderr buffer is full the process
blocks until your application reads something from `e`.  That's where the
whole thing hangs.  You wait for something on `o` and the process waits
for you to read from `e` → deadlock.

You have to use threads to read both `o` and `e` or the `select` module to
look which file has something to read.

Ciao,
Marc 'BlackJack' Rintsch
-- 
http://mail.python.org/mailman/listinfo/python-list

Re: Deserializing specific objects from a file

2007-09-19 Thread Marc 'BlackJack' Rintsch
On Tue, 18 Sep 2007 16:02:38 -0700, Aaron J. M. wrote:

 There are many objects that I want be able to move in and out of
 memory at runtime; namely the game levels.  I only want one level in
 memory at a time, so I want to be able to unpickle specific Level
 objects as the player moves between levels.  I would prefer my
 serialized objects to reside in one file.
 
 I haven't come across references that say how to do something like
 what I'm describing.  Does anyone here know what techniques I have to
 employ here?

Take a look at the `shelve`-Module.  Another option might be pickling to
individual files and store them in a ZIP archive.

Ciao,
Marc 'BlackJack' Rintsch
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: lambda-funcs problem

2007-09-19 Thread Marc 'BlackJack' Rintsch
On Wed, 19 Sep 2007 04:39:44 -0700, dmitrey.kroshko wrote:

 I need to create a Python list of lambda-funcs that are dependent on
 the number of the ones, for example
 
 F = []
 for i in xrange(N):
 F.append(lambda x: x + i)
 
 however, the example don't work - since i in end is N-1 it yields x+
 (N-1) for any func.
 
 So what's the best way to make it valid?

The variable is bound to the name `i` when the lambda function is
created not to the value that `i` had at that time.  The idiomatic way is
to use a default value for an argument because those are evaluated at
definition time of functions::

F.append(lambda x, i=i: x + i)

Ciao,
Marc 'BlackJack' Rintsch
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Memory Problem

2007-09-18 Thread Marc 'BlackJack' Rintsch
On Tue, 18 Sep 2007 14:06:22 +0200, Christoph Scheit wrote:

 Then the data is added to a table, which I use for the actual Post-Processing.
 The table is actually a Class with several Columns, each column internally
 being represented by array.

Array or list?

 # create reader
 breader = BDBReader(var, type, #)
  
 # read data
 bData = breader.readDB(dbFileList[0])
 
 # create table
 dTab = DBTable(breader.headings, breader.converters, [1,2])
 addRows(bData, dTab)
 
 Before I add a new entry to the table, I check if there is already an entry 
 like this. To do so, I store keys for all the entries with row-number in a
 dictionary. What about the memory consumption of the dictionary?

The more items you put into the dictionary the more memory it uses.  ;-)

 Here the code for adding a new row to the table:
 
 # check if data already exists
 if (self.keyDict.has_key(key)):
 rowIdx = self.keyDict[key]
 for i in self.mutableCols:
 self.cols[i][rowIdx] += rowData[i]
 return
 
  # key is still available - insert row to table
  self.keyDict[key] = self.nRows
 
  # insert data to the columns
  for i in range(0, self.nCols):
  self.cols[i].add(rowData[i])
 
  # add row i and increment number of rows
  self.rows.append(DBRow(self, self.nRows))
  self.nRows += 1
 
 Maybe somebody can help me. If you need, I can give more implementation
 details.

IMHO That's not enough code and/or description of the data structure(s). 
And you also left out some information like the number of rows/columns and
the size of the data.

Have you already thought about using a database?

Ciao,
Marc 'BlackJack' Rintsch
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: How do you limit the # of lines Read?

2007-09-18 Thread Marc 'BlackJack' Rintsch
On Tue, 18 Sep 2007 13:59:47 -0700, koutoo wrote:

 I am working on a loop for my code and was wondering if there is a way
 to limit the number of lines read through?  I'd hate to cut the test
 file in order to run the code in the testing phase.  Can you add a
 value in the parenthesis of the readline() function?
 
 t = string.readline()  # Limit this somehow?

Do you want to limit how much of *one* line is read or the number of lines!?

The latter can be done with `itertools.islice()`.

Ciao,
Marc 'BlackJack' Rintsch
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: where are the .pyc files?

2007-09-17 Thread Marc 'BlackJack' Rintsch
On Mon, 17 Sep 2007 01:23:20 +, Summercool wrote:

 On Sep 16, 10:36 am, Marc 'BlackJack' Rintsch [EMAIL PROTECTED] wrote:

 The `*.pyc` files are usually only created when you import a module, not
 when a module is run directly.
 
 how come a program that runs directly doesn't need to be optimized
 into bytecode first?

Programs need to be compiled to bytecode and they actually are.  The
bytecode is just not dumped into a file but compiled at every run.

Ciao,
Marc 'BlackJack' Rintsch
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Needless copying in iterations?

2007-09-16 Thread Marc 'BlackJack' Rintsch
On Sun, 16 Sep 2007 00:40:13 +, Steven D'Aprano wrote:

 On Sun, 16 Sep 2007 00:05:58 +, Marc 'BlackJack' Rintsch wrote:
 
 In *general* the compiler can't tell, but in specific cases it could. A 
 (hypothetical) optimizing compiler would tell the difference between:
 
 
 for item in alist[1:5]:
 print item # no possible side-effects

To expand on what Ben said:  After conversion to `str` the result is then
given to `sys.stdout.write()` and of course `sys.stdout` could be another
object that changes `alist`.

 for item in alist[1:5]:
 alist.append(item) # side-effects DON'T matter

Side effect do matter here, even in not so pathological cases.  There are
cases where you get a different result whether you copy or not even with
plain vanilla `list`\s:

In [153]: alist = [1, 2, 3]

In [154]: for item in alist[1:5]:
   .: alist.append(item)
   .:

In [155]: alist
Out[155]: [1, 2, 3, 2, 3]

In [156]: alist = [1, 2, 3]

In [157]: for item in islice(alist, 1, 5):
   .: alist.append(item)
   .:

In [158]: alist
Out[158]: [1, 2, 3, 2, 3, 2, 3]

Ciao,
Marc 'BlackJack' Rintsch
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Needless copying in iterations?

2007-09-16 Thread Marc 'BlackJack' Rintsch
On Sun, 16 Sep 2007 09:50:39 +, Steven D'Aprano wrote:

 The point is rather moot, since CPython (and probably other Pythons) do 
 almost no optimizations. But just because Python is a dynamic language 
 doesn't mean there are no optimizations possible: Haskell is a dynamic 
 language, and there are optimizing compilers for it. Of course, it is 
 much simpler for Haskell, because of the type system it uses.

What do you mean by Haskell is a dynamic language?  It is statically and
strict typed and the compiler usually knows all the functions.  No
surprises, no side effects, no duck typing.

Ciao,
Marc 'BlackJack' Rintsch
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Needless copying in iterations?

2007-09-16 Thread Marc 'BlackJack' Rintsch
On Sun, 16 Sep 2007 13:31:57 +, Steven D'Aprano wrote:

 On Sun, 16 Sep 2007 10:58:07 +, Marc 'BlackJack' Rintsch wrote:
 
 On Sun, 16 Sep 2007 09:50:39 +, Steven D'Aprano wrote:
 
 The point is rather moot, since CPython (and probably other Pythons) do
 almost no optimizations. But just because Python is a dynamic language
 doesn't mean there are no optimizations possible: Haskell is a dynamic
 language, and there are optimizing compilers for it. Of course, it is
 much simpler for Haskell, because of the type system it uses.
 
 What do you mean by Haskell is a dynamic language?  It is statically and
 strict typed and the compiler usually knows all the functions.  No
 surprises, no side effects, no duck typing.
 
 Haskell's IO monad (and possibly the do monad?) allows side effects. It 
 would be a pretty poor programming language that didn't allow input or 
 output!

Those side effects stay in the IO monad and don't leak into other areas
so the compiler still knows much more about the possible data flow than
the compilers of most other languages.

 Haskell uses type inference, and has a maybe type for those cases where 
 it can't tell what the type will be.

Haskell is statically typed, the compiler must know the type of every name
otherwise it stops with an error message.  The `Maybe` type is for
functions that may return something (typed) or `Nothing`.  For example a
`lookup` function can have the signature:

lookup :: Eq a = a - [(a,b)] - Maybe b
lookup key assocList = ...

The compiler infers the concrete types of `a` and `b` at compile time.

 If you still don't accept that Haskell is a dynamic language, for 
 whatever definition of dynamic language you use, I'll withdraw the claim 
 for the sake of not getting bogged down in long arguments over something 
 that was really just a minor point.

You said it must be possible to optimize a dynamic language because there
are optimizing compilers for a dynamic language like Haskell.  That's a
minor point now?

The main problem with optimizing Python code is that the language is
dynamically typed -- the types of objects bound to names are only checked
at runtime and can change at nearly every point in time while executing.
Haskell is statically typed, the types are all inferred at compile time so
it's possible to optimize the code for those types.  The compiler knows
the exact type of every name.  By what definition is that a dynamic
language!?

Ciao,
Marc 'BlackJack' Rintsch
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: We need PIGs :)

2007-09-16 Thread Marc 'BlackJack' Rintsch
On Sun, 16 Sep 2007 10:17:18 -0400, Colin J. Williams wrote:

 Marc 'BlackJack' Rintsch wrote:

 `getFoo()` is discouraged by PEP 8.  […]

 Perhaps PEP 8 needs rethinking.  I prefer getFoo().

Yeah, *your* preference is a very good reason to rethink PEP 8…  ;-)

Ciao,
Marc 'BlackJack' Rintsch
-- 
http://mail.python.org/mailman/listinfo/python-list

Re: where are the .pyc files?

2007-09-16 Thread Marc 'BlackJack' Rintsch
On Sun, 16 Sep 2007 17:28:30 +, Summercool wrote:

 so i have always heard of the .pyc files  but for some reason i
 don't see them on the Windows platform...  when i have a program
 called  try.py  and after running it for ages, i still don't have a
 try.pyc file in my folder even if i turn the show hidden file to on.

The `*.pyc` files are usually only created when you import a module, not
when a module is run directly.

Ciao,
Marc 'BlackJack' Rintsch
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: int('\x23') != 0x23 (a.k.a convert char to integer of its byte representation)

2007-09-15 Thread Marc 'BlackJack' Rintsch
On Sat, 15 Sep 2007 11:55:28 +, Boris Dušek wrote:

 I am looking for the best way to convert a string of length 1 (= 1
 character as string) to integer that has the same value as numeric
 representation of that character. Background: I am writing functions
 abstracting endianness, e.g. converting a string of length 4 to the
 appropriate integer value (e.g. '\x01\x00\x00\x00' = 2**24 for big
 endian memory, 2**0 for little endian memory). For this, I need to
 know the numeric value of each byte and sum them according to
 endianness.

So you are looking for the `struct` module in the standard library instead
of doing this yourself.  :-)

If you insist on doing it yourself take a look at the built-in `ord()`
function.

Ciao,
Marc 'BlackJack' Rintsch
-- 
http://mail.python.org/mailman/listinfo/python-list

Re: how to join array of integers?

2007-09-15 Thread Marc 'BlackJack' Rintsch
On Sat, 15 Sep 2007 12:36:02 +, Summercool wrote:

 i think in Ruby, if you have an array (or list) of integers
 
 foo = [1, 2, 3]
 
 you can use foo.join(,) to join them into a string 1,2,3
 
 in Python... is the method to use  ,.join() ?  but then it must take
 a list of strings... not integers...
 
 any fast method?

Convert them to strings before joining:

In [145]: foo = [1, 2, 3]

In [146]: ','.join(map(str, foo))
Out[146]: '1,2,3'

Ciao,
Marc 'BlackJack' Rintsch
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: string questions

2007-09-15 Thread Marc 'BlackJack' Rintsch
On Sat, 15 Sep 2007 19:52:47 -0400, Shawn Minisall wrote:

 Hi everyone, I'm a beginning programming student in Python and have a 
 few questions regarding strings.
 
 If s1 = spam
 
 If s2 = ni!
 
 1. Would string.ljust(string.upper(s2),4) * 3 start it at the left 
 margin and move it 12 spaces to the right because of the 4 *3?  If so, 
 why is it in the parathesis for the upper command and not the ljust?   I 
 already know that it would cap it to NI!

Fire up the interpreter and just try it.  And then the different parts to
know whats going on in detail.

But please don't use the functions in `string` that are also available as
methods on strings.  Those functions are deprecated.

 2.  To get the output Spam Ni! Spam Ni! Spam Ni! I could do something 
 like this string.join ([s1, s2]),
 
 But I'm a little lost how to get it repeated three times on one line.  
 Would I  just have to put the same command on the next two lines?

Try out the code from 1. and you should get an idea ho to repeat three
times.

 3. To change spam to spm, the string.replace seems to be the best 
 function to use.  However, when I use
 string.replace(s1, a,  ) in python to replace a with an empty space, 
 it doesn't work...I just get spam back when I print s1.   Any ideas?

Yes, read the documentation to find out that `replace()` does not alter the
string -- strings in Python are immutable -- but returns a new, changed
string.

Ciao,
Marc 'BlackJack' Rintsch
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Needless copying in iterations?

2007-09-15 Thread Marc 'BlackJack' Rintsch
On Sat, 15 Sep 2007 14:58:15 -0700, James Stroud wrote:

 I was staring at a segment of code that looked like this today:
 
 for something in stuff[x:y]:
   whatever(something)
 
 and was wondering if the compiler really made a copy of the slice from 
 stuff as the code seems to suggest, or does it find some way to produce 
 an iterator without the need to make a copy (if stuff is a built-in 
 sequence type)?

The compiler can't optimize this as it would change the semantics. 
There's no way for the compiler to tell if this copy really is needless.
`whatever()` may change `stuff[i]` where `i` is in `x:y` and this may lead
to different results wether it iterates over a copy or the original.

Ciao,
Marc 'BlackJack' Rintsch
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: recursion

2007-09-14 Thread Marc 'BlackJack' Rintsch
On Fri, 14 Sep 2007 13:40:17 +0200, Gigs_ wrote:

 sorry i think that i express wrong. having problem with english
 
 
 what i mean is how python knows to add all thing at the end of recursion

Because you have written code that tells Python to do so.  ;-)

   def f(l):
  if l == []:
  return []
  else:
  return f(l[1:]) + l[:1]
 
 
 f([1,2,3])
 
 recursion1   f([2,3]) + [1]
 
 recursion2   f([3]) + [2]  or [2, 1]?
 
 recursion3   f([]) + [3] or   [3, 2, 1]

Both alternatives in recursion 2 and 3 are wrong.  You have to simply
replace the function invocation by its result which gives:

f([1, 2, 3])
r1  f([2, 3]) + [1]
r2  f([3]) + [2] + [1]
r3  f([]) + [3] + [2] + [1]
r4  [] + [3] + [2] + [1]

And now the calls return:

r3  [3] + [2] + [1]
r2  [3, 2] + [1]
r1  [3, 2, 1]

 i dont get all this
 
   def f(l):
  if l == []:
   print l
  return []
  else:
  return f(l[1:]) + l[:1]
 
   f([1,2,3])
 []
 [3, 2, 1]  # how this come here? how python save variables from each
 recursion?

There is not just one `l` but one distinct `l` in each call.

Ciao,
Marc 'BlackJack' Rintsch
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: recursion

2007-09-14 Thread Marc 'BlackJack' Rintsch
On Fri, 14 Sep 2007 15:58:39 +0200, Gigs_ wrote:

   def factorial(n):
 print n =, n
 if n==0:
 return 1
 else:
 return n * factorial(n-1)
 
   factorial(3)
 n = 3
 n = 2
 n = 1
 n = 0
 6
 
 
 now i understand. but one question at the end this function return 1. how 
 python 
 knows that it needs to multiply 1 with all recursive returns. (why python 
 didnt 
 sum recursive return with 1?)

Because there is a ``*`` and not a ``+`` in the last line of the function.

Let's play this through (I abbreviate the function name to just `f()`):

Execution of f(3) leads to the second return:

r1 f(3):  return 3 * f(2)

This multiplication can't take place until ``f(2)`` is calculated so the
current function call is suspended and evaluated later, when the result
of ``f(2)`` is known.  The call in that line is replaces with the result
then.  Calling ``f(2)`` leads to:

r2 f(2):  return 2 * f(1)

The same again, another call to `f()` with 1 as argument:

r3 f(1):  return 1 * f(0)

Now the last call takes the other ``if`` branch:

r4 f(0):  return 1

The 1 is returned to the previus call:

r3 f(1):  return 1 * 1

This can be evaluated now and is returned to its caller:

r2 f(2):  return 2 * 1

Again this is evaluated and returned to its caller:

r1 f(3):  return 3 * 2

And here we have the final result that is returned from the first call to
`f()`.

Ciao,
Marc 'BlackJack' Rintsch
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: python osgi

2007-09-13 Thread Marc 'BlackJack' Rintsch
On Wed, 12 Sep 2007 18:13:13 -0700, Andreas Wapf wrote:

 Is there something like a osgi implementation in python? I am really
 impressed by the bundle system and the possibility to load and unload
 bundles without wasting memory.

I'm really impressed by the about 1000 pages OSGi specification.  8-)

 Is it even possible to do something like that in python? Would be nice to
 have a very small python interpreter and just load the modules as needed.

Modules are only loaded as you import them, so you can make it as lazy
as you want.  I don't know if it's possible to unload them again.  But is
code really a memory problem?  I've never thought OMG memory is getting
low, I wish I could unload a module to get some space.  In my experience
it's always data that eats my RAM.

Ciao,
Marc 'BlackJack' Rintsch
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Python 3K or Python 2.9?

2007-09-13 Thread Marc 'BlackJack' Rintsch
On Thu, 13 Sep 2007 10:10:57 -0700, TheFlyingDutchman wrote:

 Isn't one of the main ideas behind python that it doesn't force you to
 do (well, declare) anything? And by ideas I mean design decisions.
 Thats exactly what makes python great for prototyping; you just do it
 and see if it works. As soon as you need to declare things you have to
 change stuff in at least 2 places for every change of heart you have.

 (Can you tell I'm currently forced to developing in Java? ;) (Which I'm
 currently avoiding to do, by wasting my time on usenet.))

 But if you are looking at code you didn't write, it's nice to be able
 to see all the member variables that a class has listed separate from
 method code.

That information is usually in the `__init__()` method and the class
docstring.

 I think static typing helps in trying to deduce what code is doing,
 particularly when you are looking at function definitions. You don't
 have to work to find out what type of variables it takes.

This should either be obvious or in the docstring.

Ciao,
Marc 'BlackJack' Rintsch
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Python Problem

2007-09-12 Thread Marc 'BlackJack' Rintsch
On Wed, 12 Sep 2007 09:09:02 +0200, A.T.Hofkamp wrote:

 On 2007-09-11, Wiseman [EMAIL PROTECTED] wrote:

  Hi,

 OK - it works in WindowsXP.
 I installed enchant on my SuSE 10.0 (using YAST). 
 The enchant Suse package looks like a general Linux package, not a
 Python specific.
 
 You'd seem to be right judging by this web-page:
 http://www.novell.com/products/linuxpackages/suselinux/enchant.html
 
 As you can see, there is no file installed at a path with python in it.
 
 What am I missing?
 
 Python bindings to the library perhaps?
 You may be more lucky with an enchant-dev package (if it exists). I am however
 not a Suse user and not an echant user (well, not from my Python code at 
 least).

PyEnchant seems to be an independent project:

  http://pyenchant.sourceforge.net/

So there is either an extra package for SuSE or the OP has to build it
himself.

Ciao,
Marc 'BlackJack' Rintsch
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Implementing a fixed size stack for an RPN Calculator

2007-09-12 Thread Marc 'BlackJack' Rintsch
On Wed, 12 Sep 2007 20:55:22 +, [EMAIL PROTECTED] wrote:

 I have implemented an RPN calculator in python but now I would like to
 make the stack size fixed. how can I transform the user's RPN
 expression from a stack overflow to a computable expression.
 
 For instance, if my stack size is 2 then the some expression can't be
 computed but could be transformed as following:
 
 Non computable expression: 1 2 3 + +   -- stack size of 3 is needed
 Computable expression: 1 2 + 3 + -- stack size of 2 is needed
 
 How can I define a formal way of transforming the non computable
 expression to computable one.
 
 My RPN only implements: + - x %

Why does this homework assignment limit the stack so severely!?  ;-)

Ciao,
Marc 'BlackJack' Rintsch
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: printing list containing unicode string

2007-09-11 Thread Marc 'BlackJack' Rintsch
On Mon, 10 Sep 2007 19:26:20 -0700, Xah Lee wrote:

 ・  Many Internet standards are defined in terms of textual data, and
 can't handle content with embedded zero bytes. 
 
 Not sure what he mean by can't handle content with embedded zero
 bytes. Overall i think this sentence is silly, and he's probably
 thinking in unix/linux.

No he's probably thinking of all the text based protocols (HTTP, SMTP, …)
and that one of the most used programming languages, C, can't cope with
embedded null bytes in strings.

 ・  Encodings don't have to handle every possible Unicode
 character,  
 
 This is inane. A encoding, by definition, turns numbers into binary
 numbers (in our context, it means a encoding handles all unicode chars
 by definition).

How do you encode chinese characters with the ISO-8859-1 encoding? This
encoding obviously doesn't handle *all* unicode characters.

 ・  
 UTF-8 has several convenient properties:
 1. It can handle any Unicode code point.
 ...
  
 
 As mentioned before, by definition, any Unicode encoding encodes all
 unicode char set. The mentioning of above as a convenient property
 is inane.

You are being silly here.

Ciao,
Marc 'BlackJack' Rintsch
-- 
http://mail.python.org/mailman/listinfo/python-list

Re: IOError - list of all Errno numbers and their meanings?

2007-09-11 Thread Marc 'BlackJack' Rintsch
On Tue, 11 Sep 2007 13:08:25 +0100, Tim Couper wrote:

 As you know, when an IOError is raised, you get a helpful:
 
 [Errno N] useful descriptor of the error
 
 Is there a list of all possible values of N which can be returned, and 
 their meanings?

Take a look at the `errno` module and the `os.strerror()` function.

Ciao,
Marc 'BlackJack' Rintsch
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: memcpy

2007-09-11 Thread Marc 'BlackJack' Rintsch
On Tue, 11 Sep 2007 05:09:58 -0700, Tim wrote:

 On Sep 10, 3:31 pm, Marc 'BlackJack' Rintsch [EMAIL PROTECTED] wrote:
 On Mon, 10 Sep 2007 11:38:50 -0700, Tim wrote:
  How do I memcpy from a pointer to an array of floats in python?

  I get errors: NameError: global name 'row' is not defined

 Well than the (global) name `row` is not defined.  Quite clear message,
 isn't it?  ;-)

  I want to be able to get the row[i] array element. In C I would
  normally place the address of row as the first argument.

  cdll.msvcrt.memcpy( row, pData, 256 )

  If I define row as the following I also get the following error:

  row = ones( TOTAL_PARAMETER_ENTRIES, dtype=float )

  ArgumentError: argument 1: type 'exceptions.TypeError': Don't know
  how to convert parameter 1

 You don't give enough information so we have to guess.  For example I
 guess the `ones()` function comes from one of the packages `numeric`,
 `numarray` or `numpy`!?

 This function returns a Python object.  You can't use arbitrary Python
 objects with `ctypes`.  `memcpy` expects a pointer not an object.

 Can I initialize something in Python that I can get access to it's
 pointer?

It's pointer?  Then you have a pointer to a structure that represents
the Python object but still no idea what data is at that pointer.  This is
an implementation detail of the Python version and of the particular
object.

 Here is what I would like to write:
 
 shared_memory_pointer = windll.kernel32.MapViewOfFile(hMapObject,
 FILE_MAP_ALL_ACCESS,
 0, 0, TABLE_SHMEMSIZE)
 
 memcpy( self.data, shared_memory_pointer, my_size )

I haven't tested but it should be possible to declare the return type of
`windll.kernel32.MapViewOfFile()` as ``ctypes.POINTER(ctypes.c_double *
256)`` and then do:

test_data = numpy.ones(1000)
shared_memory_pointer.contents[0:256] = test_data[0:256]

Ciao,
Marc 'BlackJack' Rintsch
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: memcpy

2007-09-11 Thread Marc 'BlackJack' Rintsch
On Tue, 11 Sep 2007 06:22:19 -0700, Tim wrote:

 Is this what you mean? Python did not like the word c_types in front
 of POINTER. Do you know why?

Yes I know.  Read up how importing works.

 How can I re-declare a function's return type if it is declared
 somewhere else?

The return type of C functions wrapped by `ctypes` is always `int` until
you say otherwise.  How to do that is covered in the `ctypes`
tutorial/dicumentation.

 test_data = numpy.ones(1000)
 shared_memory_pointer = POINTER(c_float*256)

This binds the resulting pointer object to the name
`shared_memory_pointer`.

 shared_memory_pointer =
 windll.kernel32.MapViewOfFile(hMapObject, FILE_MAP_ALL_ACCESS,
 0, 0, TABLE_SHMEMSIZE)

And here you bind a different object to that name, so the first binding
has no effect.

Ciao,
Marc 'BlackJack' Rintsch
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Car-ac-systems

2007-09-11 Thread Marc 'BlackJack' Rintsch
On Tue, 11 Sep 2007 17:42:53 +, Zentrader wrote:

 snip
 What is it about please do not top-post that you have difficulty
 understanding? Or do MVPs feel that their time is so much more
 valuable than anyone else's that they are free to ignore the norms?
 
 Who made this the norm?

Common sense and (western) reading habits.

 In my travels through web-land, it appears to be the opposite.

You must travel strange territories.  ;-)

 Don't waste space repeating everything in every post, and it wastes
 everyone's time by have to go over the same thing again and again.

That's why you trim the quoted part to the minimum to understand what one
is answering.  Top posting and full quoting wastes time for people who
want to see the context because context and answer are way apart and in the
wrong order so one has to scroll back and forth to keep track of the
discussion.

Ciao,
Marc 'BlackJack' Rintsch
-- 
http://mail.python.org/mailman/listinfo/python-list


<    2   3   4   5   6   7   8   9   10   11   >