Sunday, February 28, 2010

comp.lang.python - 25 new messages in 13 topics - digest

comp.lang.python
http://groups.google.com/group/comp.lang.python?hl=en

comp.lang.python@googlegroups.com

Today's topics:

* Paper Presentation Topics - 1 messages, 1 author
http://groups.google.com/group/comp.lang.python/t/4c8f12c32d56a436?hl=en
* Python dos2unix one liner - 4 messages, 4 authors
http://groups.google.com/group/comp.lang.python/t/c4b63debe91d51c7?hl=en
* help with Python installation - 1 messages, 1 author
http://groups.google.com/group/comp.lang.python/t/7b9dfd6f69a57931?hl=en
* Is this secure? - 1 messages, 1 author
http://groups.google.com/group/comp.lang.python/t/ffff2b290db4e811?hl=en
* getting rpy2 from repository - 1 messages, 1 author
http://groups.google.com/group/comp.lang.python/t/1594d955683c0539?hl=en
* scope of generators, class variables, resulting in global na - 1 messages, 1
author
http://groups.google.com/group/comp.lang.python/t/b1ff4d23cb5ec32c?hl=en
* stripping fields from xml file into a csv - 2 messages, 2 authors
http://groups.google.com/group/comp.lang.python/t/978963344b473e2d?hl=en
* Method / Functions - What are the differences? - 4 messages, 3 authors
http://groups.google.com/group/comp.lang.python/t/72ab93ba395822ed?hl=en
* problem with floats and calculations - 1 messages, 1 author
http://groups.google.com/group/comp.lang.python/t/0159b8f2f69399dc?hl=en
* cpan for python? - 2 messages, 2 authors
http://groups.google.com/group/comp.lang.python/t/ecd51ced8d24593e?hl=en
* Printing the arguments of an attribute in a class - 4 messages, 3 authors
http://groups.google.com/group/comp.lang.python/t/5809660143c135d5?hl=en
* Challenge: escape from the pysandbox - 2 messages, 2 authors
http://groups.google.com/group/comp.lang.python/t/87bf10f8acede7c3?hl=en
* Docstrings considered too complicated - 1 messages, 1 author
http://groups.google.com/group/comp.lang.python/t/dea5c94f3d058e26?hl=en

==============================================================================
TOPIC: Paper Presentation Topics
http://groups.google.com/group/comp.lang.python/t/4c8f12c32d56a436?hl=en
==============================================================================

== 1 of 1 ==
Date: Sun, Feb 28 2010 12:14 am
From: jaagar


Paper Presentation Topics.

Our database posses huge collection of topics for paper presentations
in all
areas.

Check all our paper presentation topics at

http://pptcontent.blogspot.com/

Topics for Paper Presentation :

Paper presentation topics for ece.
Paper presentation topics for cse.
Paper presentation topics for eee.
Paper presentation topics for it.
Paper presentation topics for mba.
Paper presentation topics for mechanical engineering.
Paper presentation topics for civil engineering.
Paper presentation topics for eee.

Our sample list list of paper presentation topics in all area
branches :

Hot Topics :
Lunar Solar Power,Speech Recognition,Bullet Proof Vests,Nano
Robos,Diamond Search,Brain Controlled Car

==============================================================================
TOPIC: Python dos2unix one liner
http://groups.google.com/group/comp.lang.python/t/c4b63debe91d51c7?hl=en
==============================================================================

== 1 of 4 ==
Date: Sun, Feb 28 2010 12:48 am
From: Steven D'Aprano


On Sat, 27 Feb 2010 19:37:50 -0800, staticd wrote:

>> >Amusing how long those Python toes can be. In several replies I have
>> >noticed (often clueless) opinions on Perl. When do people learn that a
>> >language is just a tool to do a job?
>>
>> When do people learn that language makes a difference?  I used to be a
>> Perl programmer; these days, you'd have to triple my not-small salary
>> to get me to even think about programming in Perl.
>
> dude, you nailed it. many times, if not _always_, the correct output is
> important. the method used to produce the output is irrelevant.

Oh really?

Then by that logic, you would consider that these two functions are both
equally good. Forget readability, forget maintainability, forget
efficiency, we have no reason for preferring one over the other since the
method is irrelevant.


def greet1(name):
"""Print 'Hello <name>' for any name."""
print "Hello", name


def greet2(name):
"""Print 'Hello <name>' for any name."""
count = 0
for i in range(0, ("Hello", name).__len__(), 1):
word = ("Hello", name).__getitem__(i)
for i in range(0, word[:].__len__(), 1):
c = word.__getitem__(i)
import sys
import string
empty = ''
maketrans = getattr.__call__(string, 'maketrans')
chars = maketrans.__call__(empty, empty)
stdout = getattr.__call__(sys, 'stdout')
write = getattr.__call__(stdout, 'write')
write.__call__(c)
count = count.__add__(1)
import operator
eq = getattr.__call__(operator, 'eq')
ne = getattr.__call__(operator, 'ne')
if eq.__call__(count, 2):
pass
elif not ne.__call__(count, 2):
continue
write.__call__(chr.__call__(32))
write.__call__(chr.__call__(10))
return None

There ought to be some kind of competition for the least efficient
solution to programming problems-ly y'rs,


--
Steven


== 2 of 4 ==
Date: Sun, Feb 28 2010 3:05 am
From: Stefan Behnel


Steven D'Aprano, 28.02.2010 09:48:
> There ought to be some kind of competition for the least efficient
> solution to programming problems

That wouldn't be very interesting. You could just write a code generator
that spits out tons of garbage code including a line that solves the
problem, and then let it execute the code afterwards. That beast would
always win.

Stefan

== 3 of 4 ==
Date: Sun, Feb 28 2010 3:51 am
From: "Martin P. Hellwig"


On 02/28/10 11:05, Stefan Behnel wrote:
> Steven D'Aprano, 28.02.2010 09:48:
>> There ought to be some kind of competition for the least efficient
>> solution to programming problems
>
> That wouldn't be very interesting. You could just write a code generator
> that spits out tons of garbage code including a line that solves the
> problem, and then let it execute the code afterwards. That beast would
> always win.
>
> Stefan
>
Well that would be an obvious rule that garbage code that does not
contribute to the end result (ie can be taken out without affecting the
end result) would not be allowed. Enforcing the rule is another beast
though, but I would leave that to the competition.

Though the idea of a code generator is solid, but instead of generating
garbage, produces a virtual machine that implements a generator that
produces a virtual machine, etc. etc.

--
mph


== 4 of 4 ==
Date: Sun, Feb 28 2010 5:48 am
From: John Bokma


Steven D'Aprano <steve@REMOVE-THIS-cybersource.com.au> writes:

> On Sat, 27 Feb 2010 11:27:04 -0600, John Bokma wrote:
>
>> When do people learn that a
>> language is just a tool to do a job?
>
> When do people learn that there are different sorts of tools? A
> professional wouldn't use a screwdriver when they need a hammer.

[...]

> Languages are not just nebulous interchangeable "tools", they're tools

A hammer is just a tool to do a job. Doesn't mean one must or should use
a hammer to paint a wall.

> for a particular job with particular strengths and weaknesses, and
> depending on what strengths you value and what weaknesses you dislike,
> some tools simply are better than other tools for certain tasks.

In short, we agree.

--
John Bokma j3b

Hacking & Hiking in Mexico - http://johnbokma.com/
http://castleamber.com/ - Perl & Python Development

==============================================================================
TOPIC: help with Python installation
http://groups.google.com/group/comp.lang.python/t/7b9dfd6f69a57931?hl=en
==============================================================================

== 1 of 1 ==
Date: Sun, Feb 28 2010 12:52 am
From: John Yeung


On Feb 28, 12:51 am, gujax <rjngrj2...@gmail.com> wrote:
> I agree with you. I have a CD of Xubuntu. I tried
> booting up with the CD and was impressed. I noticed
> few problems with screen resolution, window size etc.

Though it may be worth working out any niggling problems to switch to
Linux, I don't think you should feel pressured to switch if you are
comfortable with Win ME on that particular machine.

> And no, I am not at all attached to Win, though wouldn't
> say the same for my computer.

It sounds like you could have switched to Linux and resolved any
configuration problems in the two years you've been trying to install
Python. ;) But that said, I have used older versions of Python
without any trouble on Win ME. In fact, I still have a Win ME machine
running 2.5.2.

According to the docs, you have to have Microsoft Installer 2.0
(freely downloadable from Microsoft if necessary) to use the 2.5 MSI
distribution:

http://www.python.org/download/releases/2.5.4/

If for whatever reason you can't or don't want to do this, 2.3 still
has an EXE distribution:

http://www.python.org/download/releases/2.3.5/

John

==============================================================================
TOPIC: Is this secure?
http://groups.google.com/group/comp.lang.python/t/ffff2b290db4e811?hl=en
==============================================================================

== 1 of 1 ==
Date: Sun, Feb 28 2010 12:43 am
From: Paul Rubin


aahz@pythoncraft.com (Aahz) writes:
> Are you sure? I haven't done a lot of research, but my impression was
> that Windows didn't have anything built in.

I don't know much about the windows but there is the CAPI and then
there is all the TCPA (i.e. DRM) stuff. Maybe it can be used somehow.

==============================================================================
TOPIC: getting rpy2 from repository
http://groups.google.com/group/comp.lang.python/t/1594d955683c0539?hl=en
==============================================================================

== 1 of 1 ==
Date: Sun, Feb 28 2010 2:00 am
From: Stephen Tucker


Hi all, I have Enthought Python 4.3 installed on my OS X 10.5. When I do $ easy_install rpy2

Searching for rpy2
No matching release version found. Searching for latest development version.
Reading http://www.enthought.com/repo/epd/eggs/MacOSX/10.4_x86/
Please enter credentials to access this repository:
User Name:

Is there a way to point to the original (non-Enthought) repository, or is there a better way?

Thanks very much in advance!

Stephen

==============================================================================
TOPIC: scope of generators, class variables, resulting in global na
http://groups.google.com/group/comp.lang.python/t/b1ff4d23cb5ec32c?hl=en
==============================================================================

== 1 of 1 ==
Date: Sun, Feb 28 2010 3:45 am
From: dontspamleo


> > ...and really means this...
> > class C:
> >   x = 1
> >   def f(self,y): return T.x + y
>
> I don't understand what T is. Did you mean C?

Yes, I meant C. Thanks.

>
> If so, you are wrong. self.x is not the same as <class>.x due to
> inheritance rules. Consider one example:
>
<example snipped see thread/>

Thanks for the nice example. Sorry for my loose language. By "really
means", what I really meant was that the most appropriate construct
should be the one referring to the class variable explicitly. I would
consider it inelegant (at least) to use an instance variable with the
same name as a class variable.

>
> > 1. Has this been discussed before?
>
> Yes.
>
> > 1. What would this suggestion break?
>
> Nearly all existing code using classes, which is nearly everything.

Is it that common to have code containing a class variable with the
same name as a global variable? Are there other use cases that would
break?

>
> > 2. What are the advantages of making the scope of class variables
> > different? Maybe is it just a historical trait?
>
> See the discussion in the PEP for introducing nested scopes in the first
> place:
>
> http://www.python.org/dev/peps/pep-0227/
>

Thanks. That is really useful. I just read the PEP. I find this
paragraph very helpful:

"An alternative would have been to allow name binding in class
scope to behave exactly like name binding in function scope. This
rule would allow class attributes to be referenced either via
attribute reference or simple name. This option was ruled out
because it would have been inconsistent with all other forms of
class and instance attribute access, which always use attribute
references. Code that used simple names would have been obscure."

The point about all other access use cases requiring attribute
references is a really good one. If a language requires self.f() to
access the member f of class C, which happens to be a function, then
self.x or C.x should also be required to access attribute x. And no
one would be crazy enough to ask to have that fixed.

@Steven: Are there other snippets from the PEP you were pointing to
specifically?

Cheers,
Leo.


==============================================================================
TOPIC: stripping fields from xml file into a csv
http://groups.google.com/group/comp.lang.python/t/978963344b473e2d?hl=en
==============================================================================

== 1 of 2 ==
Date: Sun, Feb 28 2010 4:15 am
From: Stefan Behnel


Roland Mueller, 28.02.2010 13:01:
> The stylesheet is test.xsl and the insput data test.xml. The following
> Python code the applies the stylesheet on the input data and puts the output
> into foo.
>
> Python code:
> #!/usr/bin/python
> import sys
> import libxml2
> import libxslt
>
> styledoc = libxml2.parseFile("test.xsl")
> style = libxslt.parseStylesheetDoc(styledoc)
> doc = libxml2.parseFile("test.xml")
> result = style.applyStylesheet(doc, None)
> style.saveResultToFilename("foo", result, 0)
>
> BR,
> Roland
>
> *Example run in Linux:*
> roland@komputer:~/Desktop/XML/XSLT$ ./xslt_test.py

Note that the shorthand for the above is

$ xsltproc test.xsl test.xml > foo

Stefan

== 2 of 2 ==
Date: Sun, Feb 28 2010 8:41 am
From: Hai Vu


On Feb 28, 12:05 am, Stefan Behnel <stefan...@behnel.de> wrote:
> Hal Styli, 27.02.2010 21:50:
>
> > I have a sed solution to the problems below but would like to rewrite
> > in python...
>
> Note that sed (or any other line based or text based tool) is not a
> sensible way to handle XML. If you want to read XML, use an XML parser.
> They are designed to do exactly what you want in a standard compliant way,
> and they can deal with all sorts of XML formatting and encoding, for example.
>
> > I need to strip out some data from a quirky xml file into a csv:
>
> > from something like this
>
> > < ..... cust="dick" .... product="eggs" ... quantity="12" .... >
> > < .... cust="tom" .... product="milk" ... quantity="2" ...>
> > < .... cust="harry" .... product="bread" ... quantity="1" ...>
> > < .... cust="tom" .... product="eggs" ... quantity="6" ...>
> > < ..... cust="dick" .... product="eggs" ... quantity="6" .... >
>
> As others have noted, this doesn't tell much about your XML. A more
> complete example would be helpful.
>
> > to this
>
> > dick,eggs,12
> > tom,milk,2
> > harry,bread,1
> > tom,eggs,6
> > dick,eggs,6
>
> > I am new to python and xml and it would be great to see some slick
> > ways of achieving the above by using python's XML capabilities to
> > parse the original file or python's regex to achive what I did using
> > sed.
>
> It's funny how often people still think that SAX is a good way to solve XML
> problems. Here's an untested solution that uses xml.etree.ElementTree:
>
>     from xml.etree import ElementTree as ET
>
>     csv_field_order = ['cust', 'product', 'quantity']
>
>     clean_up_used_elements = None
>     for event, element in ET.iterparse("thefile.xml", events=['start']):
>         # you may want to select a specific element.tag here
>
>         # format and print the CSV line to the standard output
>         print(','.join(element.attrib.get(title, '')
>                        for title in csv_field_order))
>
>         # safe some memory (in case the XML file is very large)
>         if clean_up_used_elements is None:
>             # this assigns the clear() method of the root (first) element
>             clean_up_used_elements = element.clear
>         clean_up_used_elements()
>
> You can strip everything dealing with 'clean_up_used_elements' (basically
> the last section) if your XML file is small enough to fit into memory (a
> couple of MB is usually fine).
>
> Stefan

This solution is so beautiful and elegant. Thank you. Now I am off to
learn ElementTree.

By the way, Stefan, I am using Python 2.6. Do you know the differences
between ElementTree and cElementTree?

==============================================================================
TOPIC: Method / Functions - What are the differences?
http://groups.google.com/group/comp.lang.python/t/72ab93ba395822ed?hl=en
==============================================================================

== 1 of 4 ==
Date: Sun, Feb 28 2010 4:38 am
From: Michael Rudolf


Out of curiosity I tried this and it actually worked as expected:

>>> class T(object):
x=[]
foo=x.append
def f(self):
return self.x


>>> t=T()
>>> t.f()
[]
>>> T.foo(1)
>>> t.f()
[1]
>>>

At first I thought "hehe, always fun to play around with python. Might
be useful sometimes" - but then It really confused me what I did. I
mean: f is what we call a method, right? But was is foo? It is not a
method and not a classmethod as it accepts no self and no cls.
So that leaves staticmethod? OK, fair, as x is "static" here anyway this
reflects what it does. But then consider this:

>>> class T(object):
def __init__(self):
self.x=[]
self.foo=self.x.append
def f(self):
return self.x


>>> y=T()
>>> y.x
[]
>>> y.foo(1)
>>> y.x
[1]
>>> a=T()
>>> a.x
[]
>>> a.foo(2)
>>> a.x
[2]
>>>

Note that all I did was moving the list and foo into the instance. Still
no self and no cls, but also no static behaviour any more.

So is foo just nothing of the above and really only a class/instance
attribute which happens to be callable?

Perhaps this all does not matter, but now I am really confused about the
terminology. So: what makes a method a method? And of what type?

Regards,
Michael


== 2 of 4 ==
Date: Sun, Feb 28 2010 6:08 am
From: "Alf P. Steinbach"


* Michael Rudolf:
> Out of curiosity I tried this and it actually worked as expected:
>
> >>> class T(object):
> x=[]
> foo=x.append
> def f(self):
> return self.x
>
>
> >>> t=T()
> >>> t.f()
> []
> >>> T.foo(1)
> >>> t.f()
> [1]
> >>>
>
> At first I thought "hehe, always fun to play around with python. Might
> be useful sometimes" - but then It really confused me what I did. I
> mean: f is what we call a method, right? But was is foo?

foo is (refers to) an object that supports call notation and that forwards calls
somewhere else, in this case to append on a list.

You might call it (descriptive) a call forwarder, or (C# or general terminology)
a delegate, or (Python 2.x) a bound method.


<example>
>>> "Hello".upper
<built-in method upper of str object at 0x00BA16E0>
>>> f = "Hello".upper
>>> f
<built-in method upper of str object at 0x00BA16E0>
>>> f()
'HELLO'
>>>
>>>
>>>
>>> f.__self__
'Hello'
>>> f.__call__
<method-wrapper '__call__' of builtin_function_or_method object at 0x00BDD170>
>>> print( f.__doc__ )
S.upper() -> str

Return a copy of S converted to uppercase.
>>> _
</example>


A common use for delegates is as command handlers in a GUI application, and in
general for event notifications.


Cheers & hth.,

- Alf


== 3 of 4 ==
Date: Sun, Feb 28 2010 6:24 am
From: Rob Williscroft


Michael Rudolf wrote in news:hmdo3m$287$1@news.urz.uni-heidelberg.de in
comp.lang.python:

> Note that all I did was moving the list and foo into the instance. Still
> no self and no cls, but also no static behaviour any more.

Yes in the first case foo was an attribute of the class, and in the second
an attribute of aon instance of the class.

In both cases it was a bound method, something similar too:

lambda item : T.x.append( item )

== 4 of 4 ==
Date: Sun, Feb 28 2010 8:39 am
From: Michael Rudolf


Am 28.02.2010 15:08, schrieb Alf P. Steinbach:
> >>> "Hello".upper
> <built-in method upper of str object at 0x00BA16E0>
> >>> f = "Hello".upper
> >>> f
> <built-in method upper of str object at 0x00BA16E0>
> >>> f()
> 'HELLO'
> >>>
> >>>
> >>>
> >>> f.__self__
> 'Hello'

Holy hand grenade.
You have no Idea how enlightened I feel right now :D

Thank you, "bound method" was the term I forgot and your example...
...totally revealed the internals behind this to me. Especially the last
line I quoted.
I mean, I always knew *that* this works, but I never knew *why*.

Regards,
Michael

==============================================================================
TOPIC: problem with floats and calculations
http://groups.google.com/group/comp.lang.python/t/0159b8f2f69399dc?hl=en
==============================================================================

== 1 of 1 ==
Date: Sun, Feb 28 2010 4:43 am
From: Albert van der Horst


In article <mailman.2549.1266184509.28905.python-list@python.org>,
Dennis Lee Bieber <wlfraed@ix.netcom.com> wrote:
>On Sun, 14 Feb 2010 10:33:54 +0100, Karsten Goen
><karsten.goen@googlemail.com> declaimed the following in
>gmane.comp.python.general:
>
>> Maybe anyone can help me with this problem, I don't want to generate for
>> every possible user input a single formula. And also it should be possible
>> for a computer, my calculator at home does the same and is much smaller and
>> slower.
>>
> Most ALL calculators that I've encountered use packed BCD internally
>for numeric data. That is -- two decimal digits (0-9) and a few special
>codes (mantissa sign, exponent sign, decimal point location, and
>exponent start). A lot of them (though strangely not HP) included "guard
>digits" -- they would display, say, 8 significant digits for results,
>but kept 10 digits internally, so that rounding errors wouldn't
>accumulate as rapidly. M$ Excel/Access/VisualBasic "money" data type

What is displayed has no influence on rounding errors.
Calculator vendors prevent puzzlement by non-knowledgeable users
by having guard digits. HP apparently deals with professional users,
so I think it not strange at all.

>carries four decimal places, on the assumption that only two are
>significant, and the last two are guards.
>
> Most ALL computers are using IEEE floating point (and the exceptions
>are still a binary floating point, not a decimal representation).
>
> The practice, which /used to be/ taught, is that one does not
>compare floating numbers for equality, but rather one compares the
>difference between floating numbers to be less than some epsilon value;
>epsilon chosen depending upon the significance needed.
>
> Rather than comparing
>
> a = b
>
>one uses
>
> abs(a - b) < epsilon
>
> Since Decimal() also has "infinite" series values (1.0/3.0), an
>epsilon comparison may also be called for.

Not may be.

>--
> Wulfraed Dennis Lee Bieber KD6MOG

Groetjes Albert

--
--
Albert van der Horst, UTRECHT,THE NETHERLANDS
Economic growth -- being exponential -- ultimately falters.
albert@spe&ar&c.xs4all.nl &=n http://home.hccnet.nl/a.w.m.van.der.horst


==============================================================================
TOPIC: cpan for python?
http://groups.google.com/group/comp.lang.python/t/ecd51ced8d24593e?hl=en
==============================================================================

== 1 of 2 ==
Date: Sun, Feb 28 2010 6:31 am
From: "ssteinerX@gmail.com"

On Feb 28, 2010, at 9:28 AM, Someone Something wrote:

> Is there something like cpan for python? I like python's syntax, but I use perl because of cpan and the tremendous modules that it has. --

Please search the mailing list archives.

This subject has been discussed to absolute death.

Draw your own conclusions about what is currently and may, in the future, be available.

S

== 2 of 2 ==
Date: Sun, Feb 28 2010 8:27 am
From: Daniel Fetchinson


> Is there something like cpan for python? I like python's syntax, but I use
> perl because of cpan and the tremendous modules that it has.

It's called PyPI or Cheese Shop:

http://pypi.python.org/pypi

<OFF>
Is it only me or others also mentally read C-SPAN when somebody writes CPAN?
</OFF>

Cheers,
Daniel


--
Psss, psss, put it down! - http://www.cafepress.com/putitdown

==============================================================================
TOPIC: Printing the arguments of an attribute in a class
http://groups.google.com/group/comp.lang.python/t/5809660143c135d5?hl=en
==============================================================================

== 1 of 4 ==
Date: Sun, Feb 28 2010 6:42 am
From: vsoler


I have a class that is a wrapper:

class wrapper:
def __init__(self, object):
self.wrapped = object
def __getattr__(self, attrname):
print 'Trace: ', attrname
#print arguments to attrname, how?
return getattr(self.wrapped, attrname)

I can run it this way:

>>> x = wrapper([1,2,3])
>>> x.append(4)
Trace: append
>>> x.wrapped
[1, 2, 3, 4]

I am able to capture the attribute name to x (that is, append).
However, I do not know how to capture and print all of its arguments
(in this case number 4).

How should I proceed?

Thank you


== 2 of 4 ==
Date: Sun, Feb 28 2010 7:00 am
From: "Alf P. Steinbach"


* vsoler:
> I have a class that is a wrapper:
>
> class wrapper:
> def __init__(self, object):
> self.wrapped = object
> def __getattr__(self, attrname):
> print 'Trace: ', attrname
> #print arguments to attrname, how?
> return getattr(self.wrapped, attrname)
>
> I can run it this way:
>
>>>> x = wrapper([1,2,3])
>>>> x.append(4)
> Trace: append
>>>> x.wrapped
> [1, 2, 3, 4]
>
> I am able to capture the attribute name to x (that is, append).
> However, I do not know how to capture and print all of its arguments
> (in this case number 4).
>
> How should I proceed?

If your goal is just learning then in your __getattr__ you might return a
wrapper for the attribute instead of the attribute itself. Equip the wrapper
with a __call__ method if it is a method. And equip it with other special
methods as appropriate.

I can imagine that that approach will lead to some practical problems, but it
may be great for learning.

If your goal is tracing, then I suggest looking at the "trace" module.

If your goal is something else purely practical, like intercepting method calls
to do arbitrary things (logging, marshaling, whatever) then I suspect that it
might getspretty complicated, hairy. For specific method calls you might just
use subclassing, but for doing this in general, parameterized, you'd need to
about the same kinds of things as the trace module does. So I guess then one
idea might be to look at the source code of that module.

But if that's what you intend to do, then best check first if there is an
existing solution. ParcPlace did this thing for a number of languages and
introduced a special term for it, I can't recall but something like
"cross-whatever mumbo jumbo concerns" plus one single catchy name. There might
be an existing Python implementation.


Cheers & hth.,

- Alf


== 3 of 4 ==
Date: Sun, Feb 28 2010 7:31 am
From: vsoler


On Feb 28, 4:00 pm, "Alf P. Steinbach" <al...@start.no> wrote:
> * vsoler:
>
>
>
> > I have a class that is a wrapper:
>
> > class wrapper:
> >     def __init__(self, object):
> >         self.wrapped = object
> >     def __getattr__(self, attrname):
> >         print 'Trace: ', attrname
> >         #print arguments to attrname, how?
> >         return getattr(self.wrapped, attrname)
>
> > I can run it this way:
>
> >>>> x = wrapper([1,2,3])
> >>>> x.append(4)
> > Trace:  append
> >>>> x.wrapped
> > [1, 2, 3, 4]
>
> > I am able to capture the attribute name to x (that is, append).
> > However, I do not know how to capture and print all of its arguments
> > (in this case number 4).
>
> > How should I proceed?
>
> If your goal is just learning then in your __getattr__ you might return a
> wrapper for the attribute instead of the attribute itself. Equip the wrapper
> with a __call__ method if it is a method. And equip it with other special
> methods as appropriate.
>
> I can imagine that that approach will lead to some practical problems, but it
> may be great for learning.
>
> If your goal is tracing, then I suggest looking at the "trace" module.
>
> If your goal is something else purely practical, like intercepting method calls
> to do arbitrary things (logging, marshaling, whatever) then I suspect that it
> might getspretty complicated, hairy. For specific method calls you might just
> use subclassing, but for doing this in general, parameterized, you'd need to
> about the same kinds of things as the trace module does. So I guess then one
> idea might be to look at the source code of that module.
>
> But if that's what you intend to do, then best check first if there is an
> existing solution. ParcPlace did this thing for a number of languages and
> introduced a special term for it, I can't recall but something like
> "cross-whatever mumbo jumbo concerns" plus one single catchy name. There might
> be an existing Python implementation.
>
> Cheers & hth.,
>
> - Alf

Alf,

My goal is just learning. In the code provided in the post I just
can't think of a method to "see", "capture" or "use" the parameters. I
am going to study the __call__ method and see if I can figure out how
I can capture the parameters.

Thank you Alf


== 4 of 4 ==
Date: Sun, Feb 28 2010 9:51 am
From: Arnaud Delobelle


vsoler <vicente.soler@gmail.com> writes:

> I have a class that is a wrapper:
>
> class wrapper:
> def __init__(self, object):
> self.wrapped = object
> def __getattr__(self, attrname):
> print 'Trace: ', attrname
> #print arguments to attrname, how?
> return getattr(self.wrapped, attrname)
>
> I can run it this way:
>
>>>> x = wrapper([1,2,3])
>>>> x.append(4)
> Trace: append
>>>> x.wrapped
> [1, 2, 3, 4]
>
> I am able to capture the attribute name to x (that is, append).
> However, I do not know how to capture and print all of its arguments
> (in this case number 4).
>
> How should I proceed?
>
> Thank you

You could do something like this:

class wrapper:
def __init__(self, object):
self.wrapped = object
def __getattr__(self, attrname):
print '** get attribute: ', self.wrapped, attrname
return wrapper(getattr(self.wrapped, attrname))
def __call__(self, *args, **kwargs):
print '** call with args: ', self.wrapped, args, kwargs
return wrapper(self.wrapped(*args, **kwargs))

x = wrapper([1,2,3])
x.append(4)

I haven't thought about it too much though.

--
Arnaud

==============================================================================
TOPIC: Challenge: escape from the pysandbox
http://groups.google.com/group/comp.lang.python/t/87bf10f8acede7c3?hl=en
==============================================================================

== 1 of 2 ==
Date: Sun, Feb 28 2010 8:43 am
From: Victor Stinner


Le samedi 27 février 2010 18:37:22, Daniel Fetchinson a écrit :
> It's google's hosting solution called app engine, for python web
> applications: http://code.google.com/appengine/docs/python/gettingstarted/
>
> I guess they also have some kind of a sandbox if they let people run
> python on their machines, I'm not sure if it's open source though.

Yes, Google AppEngine has its Python sandbox and the source code is available
online. I don't know the license. I found 7 vulnerabilities in 1 hour :-) I
contacted Google security team.

To answer to your question "How is [AppEngine] different from your project?":

* pysanbox has an import whitelist, whereas AppEngine has an import blacklist
(subprocess, socket, ... builtin modules are replaced by safe versions).
Import a Python module written in C is forbidden.
* Import a module in AppEngine imports all symbols, whereas pysandbox uses
also a symbol whitelist.
* AppEngine doesn't have proxies, all objects are modifiable (eg. sys.path)

There are other differences, but I prefer to wait for the answer from Google
before telling you more :)

AppEngine sandbox and pysandbox projects are very close: most protections are
based on blacklists, whereas RestrictedPython is only based on whitelists.

--
Victor Stinner
http://www.haypocalc.com/


== 2 of 2 ==
Date: Sun, Feb 28 2010 9:55 am
From: aahz@pythoncraft.com (Aahz)


In article <mailman.330.1267292249.4577.python-list@python.org>,
Daniel Fetchinson <fetchinson@googlemail.com> wrote:
>
>I guess they also have some kind of a sandbox if they let people run
>python on their machines, I'm not sure if it's open source though.

Thing is, I'm sure that Google uses a critical backstop to any
Python-based sandbox: something like a chroot jail. The Python sandbox
is mostly there to inform you about what you can and can't do; the real
security is provided by the OS.
--
Aahz (aahz@pythoncraft.com) <*> http://www.pythoncraft.com/

"Many customs in this life persist because they ease friction and promote
productivity as a result of universal agreement, and whether they are
precisely the optimal choices is much less important." --Henry Spencer

==============================================================================
TOPIC: Docstrings considered too complicated
http://groups.google.com/group/comp.lang.python/t/dea5c94f3d058e26?hl=en
==============================================================================

== 1 of 1 ==
Date: Sun, Feb 28 2010 8:41 am
From: Mel


Gregory Ewing wrote:

> Mel wrote:
>
>> You could think of it as a not bad use of the design principle "Clear The
>> Simple Stuff Out Of The Way First". Destinations are commonly a lot
>> simpler than sources

Calculations for immediate values could be just about anything.

Mel.


==============================================================================

You received this message because you are subscribed to the Google Groups "comp.lang.python"
group.

To post to this group, visit http://groups.google.com/group/comp.lang.python?hl=en

To unsubscribe from this group, send email to comp.lang.python+unsubscribe@googlegroups.com

To change the way you get mail from this group, visit:
http://groups.google.com/group/comp.lang.python/subscribe?hl=en

To report abuse, send email explaining the problem to abuse@googlegroups.com

==============================================================================
Google Groups: http://groups.google.com/?hl=en

0 Comments:

Post a Comment

Subscribe to Post Comments [Atom]

<< Home


Real Estate