Re: How can I verify if the content of a variable is a list or a string?

2012-02-01 Thread Rainer Grimm
You can do it more concise.

 def isListOrString(p):
...return any((isinstance(p,list),isinstance(p,str)))
...
 listOrString(string)
True
 listOrString([1,2,3])
True
 listOrString(2)
False
 listOrString(False)
False

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


Re: Scope of variable inside list comprehensions?

2011-12-05 Thread Rainer Grimm
Hello,

 try:
 songs = [Song(id) for id in song_ids]
 except Song.DoesNotExist:
 print unknown song id (%d) % id
that's is a bad programming style. So it will be forbidden with python 3. The 
reason is that list comprehension is a construct from the functional world. 
It's only syntactic sugar for the functions map and filter. So functions have 
to be pure functions. To say it in other words, they have to be side-effect 
free. But the python construct from above pollutes the namespace with name id.

Greetings from Rottenburg,
Rainer
-- 
http://mail.python.org/mailman/listinfo/python-list


Aw: Functional style programming in python: what will you talk about if you have an hour on this topic?

2011-07-16 Thread Rainer Grimm
Hello,
  (My post did not appear in the mailing list, so this is my second try. 
  Apology if it ends up posted twice)
  
  Hi, all,
  
  If you have read my previous posts to the group, you probably have some 
  idea why I asked this question.
  
  I am giving a few presentations on python to my colleagues who are mainly 
  java developers and starting to pick up python at work.
I think it's relatively difficult to get a feeling what a are the key
points behind functional programming. So I think you should start
explaining the concepts behind functional programming. A few ideas.
- higher order functions
- first class functions
- currying
- pure functions
- list processing
- lambda functions
- recursion instead of iteration
- pattern matching
And that should end with the three guys (map, filter and reduce ) as
building blocks of functional programming. And that is a good point to 
introduce list comprehension. Because it's only syntactic sugar for map 
filter. But what kind of sugar. It's very impressive. Thats my typical way
to introduce it in seminars.
Now it's a good time to explain, what's special in python with
functional programming. (A lot if you compare it with haskell)
I wrote an article about your topic. You have only to learn german.
About the concepts:
http://www.linux-magazin.de/Online-Artikel/Funktionale-Programmierung-1-Grundzuege?category=0
What's special about Python:
http://www.linux-magazin.de/NEWS/Online-Artikel-Funktionale-Programmierung-in-Python?category=0


Greetings from Rottenburg,
Rainer
-- 
http://mail.python.org/mailman/listinfo/python-list


Aw: Re: Aw: Functional style programming in python: what will you talk about if you have an hour on this topic?

2011-07-16 Thread Rainer Grimm
Am Samstag, 16. Juli 2011 08:46:42 UTC+2 schrieb Steven D#39;Aprano:
 Rainer Grimm wrote:
 
  I think it's relatively difficult to get a feeling what a are the key
  points behind functional programming. So I think you should start
  explaining the concepts behind functional programming. A few ideas.
  - higher order functions
  - first class functions
  - currying
  - pure functions
  - list processing
  - lambda functions
  - recursion instead of iteration
  - pattern matching
 
 Avoiding global state. 
Thats pure functions.
 Lazy data processing, including infinite data streams.
You are right. I forget to mention lazy versus eager evaluation.
 Decorators.
 Iterators.
 Using iterators as a form of pipelining.
Thats special about python adapting functional ideas.
 
 Some of these might not count as strictly functional in the lambda calculus
 sense, but they're closely related and less academic and more practical.
 
 
  And that should end with the three guys (map, filter and reduce ) as
  building blocks of functional programming. And that is a good point to
  introduce list comprehension. Because it's only syntactic sugar for map
  filter. But what kind of sugar. It's very impressive. Thats my typical way
  to introduce it in seminars.
 
 If it were me, I'd probably take a less pure approach and concentrate on
 concrete examples. As far as Python goes, the use of list comps/generator
 expressions, iterators and decorators are the main functional idioms, not
 so much map, filter and especially reduce, which only avoided being removed
 from Python 3 by the narrowest margin.
That is not my point. Before you talk about functional, your audience have to 
have a feeling, what you are talking about. The problem with functional is, 
that this expression is very difficult to grab. Like 15 years ago with OO or 
Design Pattern 10 years ago. I think the ideas behind functional programming 
are so great, that they a worth to reason a little bit about. Especially for 
people, that think, each problem can be solved in a OO manner.

But to be honest. I gave a many python seminars in the last years, talking 3
 hours about functional programming in Python and use no pure approach. 
Sorry I answered to fast in my first mail. He asked for functional style 
programming, not functional programming in Python and doing the whole in one 
hour. 

Maybe the hour is a good starting point for further investigations.

Greeting from Rottenburg,
Rainer
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: What is a list compression in Python?

2010-01-19 Thread Rainer Grimm
Hallo,
you can also look at list comprehension as syntactic sugar for the
functions map and filter. The two functions from the functional world
can be expressed in a comprehensive way with list comprehension.
 [x**2 for x in range(10) ] == map ( lambda x: x*x, range(10))
True
 [ x for x in range(10) if x%2 == 0 ] == filter ( lambda x: x%2 == 0 , 
 range(10))
True

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


Re: multivariable assignment

2010-01-01 Thread Rainer Grimm
On Dec 31 2009, 5:13 pm, davidj411 davidj...@gmail.com wrote:
 I am not sure why this behavior is this way.
 at beginning of script, i want to create a bunch of empty lists and
 use each one for its own purpose.
 however, updating one list seems to update the others.

  a = b = c = []
  a.append('1')
  a.append('1')
  a.append('1')
  c
 ['1', '1', '1']
  a
 ['1', '1', '1']
  b

 ['1', '1', '1']
a, b and c are the same objects.
You can check the object identity by
 a = b = c = []
 print id(a),id(b),id(c)
3083044140 3083044140 3083044140
 a is b is c
True

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


Re: How do I begin debugging a python memory leak?

2009-09-18 Thread Rainer Grimm
On 17 Sep., 02:10, Matthew Wilson m...@tplus1.com wrote:
 I have a web app based on TurboGears 1.0.  In the last few days, as
 traffic and usage has picked up, I noticed that the app went from using
 4% of my total memory all the way up to 50%.

 I suspect I'm loading data from the database and somehow preventing
 garbage collection.

 Are there any tools that might help me out with this?

If have one of the following plattforms
X86/Linux, AMD64/Linux, PPC32/Linux, PPC64/Linux and X86/Darwin (Mac
OS X),
have a look at valgrind.
http://valgrind.org/
I used of often for searching the memory leaks in c++. It's a great
tool to analyse your memory behaviour.

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


Re: How do I begin debugging a python memory leak?

2009-09-18 Thread Rainer Grimm
On Sep 18, 5:42 pm, Paul Rubin http://phr...@nospam.invalid wrote:
 Rainer Grimm r.gr...@science-computing.de writes:
  have a look at valgrind.

 Have you actually used that on Python?
Not with python as far I can remember. But often with C++ executables,
as i mentioned it.
I you look at
http://valgrind.org/info/
there stands:
Valgrind has been used on programs written partly or entirely in C, C+
+, Java, Perl, Python, assembly code, Fortran, Ada, and many others.

It exists also a extension for python
http://blogs.gnome.org/jamesh/2008/03/24/python-valgrind/ , to get rid
of the false positiv errors.
So it should be worth a try.
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: invoke method on many instances

2009-07-19 Thread Rainer Grimm
Hallo Alan,
 def apply2(itr, methodname, *args, **kwargs):
     f = operator.methodcaller(methodname, *args, **kwargs)
     for item in itr:
         f(item)
you can do it in a functional way.

 class A(object):
...   def hello(self): return hello:  + str
( self.__class__.__name__ )
...
 class B(A):pass
...
 class C(A):pass
...
 a=A()
 b=B()
 c=C()
 a.hello()
'hello: A'
 b.hello()
'hello: B'
 c.hello()
'hello: C'

 map( (lambda obj : getattr(obj,hello)()),(a,b,c))
['hello: A', 'hello: B', 'hello: C']
 [ getattr(obj,hello)() for obj in (a,b,c)]
['hello: A', 'hello: B', 'hello: C']

Greetings from Rottenburg,
Rainer
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Beginner question: module organisation

2007-05-19 Thread Rainer Grimm
[EMAIL PROTECTED] wrote:
 Hello :)
 
 I am new to python and I don't have much expirience in object-oriented
 technologies neither.
 
 The problem is the following: I have to create a simple python
 template script that will always follow the same algorithm, let's say:
 - read a mesh
 - transform the mesh (let's say, refine)
 
 The last step should be a kind of a black box:
 - the same input data format
 - some algorithme inside
 - the same output data format
 
 A number of different refine methods should be implemented. The end-
 user must be able to write easily a new method and call it from the
 base script without any major change.
 
 Something like this would be a solution (no classes created, no OO
 programming):
 - a module defining REFINE1(mesh), REFINE2(mesh), ...
 - in the script:
   from MODULE import REFINE2 as REFINE
   REFINE(mesh)
 
 Is it a proper solution for this kind of problem? How would you
 implement this kind of task?
 
Hello.

Have a look at the classical GangOfFour design pattern book. You can 
especially with the template methode design the processing of your data. 
The strategy pattern will help you to vary the algroithm in your processing.

To be concret, in your base class you define the processing of the data. 
  There are two distinct methods to do it.
delegate the variation of the algorithmns to other objects = strategy 
pattern
override the steps of the processing in subclasses = template method

Regards,
Rainer
-- 
  _creating IT solutions
Rainer Grimm
scVENUS Schulungsleiter science + computing ag
phone   +49(0)7071 9457-253 Hagellocher Weg 73
fax +49(0)7071 9457-511 D-72070 Tuebingen, Germany
[EMAIL PROTECTED]www.science-computing.de
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: a better solution for GUI in python

2007-03-17 Thread Rainer Grimm
ce wrote:
 Hi,
 
 My company is using python currently for our website. We need to
 develop a GUI front-end for our ERP that would be portable (Windows
 and Linux).
 
 My question is which solution would be better for the GUI (and easier
 to implement)? I knew there are something like wxidgets, QT and pyGTK?
 actually we will need some complicated stuff in the GUI and yet I
 don't know much about GUI programming.
 
 Any recommendation guys?
 
Hallo.

Hava a look at http://www.awaretek.com/toolkits.html .

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