Re: Force exception on attribute write access only one object

2009-01-10 Thread Peter Otten
Thomas Guettler wrote:

> Peter Otten schrieb:
>> Thomas Guettler wrote:
>> 
>>> for debugging I want to raise an exception if an attribute is
>>> changed on an object. Since it is only for debugging I don't want
>>> to change the integer attribute to a property.

>> class A(object):
>> def __init__(self):
>> self.foo = 42
>> 
>> a = A()
>> b = A()
>> 
>> class B(A):
>> @property
>> def foo(self):
>> return self.__dict__["foo"]
>> 
>> b.__class__ = B
>> 
>> a.foo = "whatever"
>> print b.foo
>> b.foo = "whatever"

> your idea was good, but it does not work with Django ORM Models:
> 
> Traceback (most recent call last):
>   File "/localhome/modw/django/core/handlers/base.py", line 87, in
>   get_response
> response = callback(request, *callback_args, **callback_kwargs)
>   File "/localhome/modw/foo/views/filter.py", line 473, in add
> return edit(request, 'add')
>   File "/localhome/modw/foo/views/filter.py", line 493, in edit
> filter=form.save()
>   File "/localhome/modw/foo/views/filter.py", line 457, in save
> action=form.save()
>   File "/localhome/modw/django/forms/models.py", line 315, in save
> if self.instance.pk is None:
>   File "/localhome/modw/django/db/models/base.py", line 292, in
>   _get_pk_val
> return getattr(self, meta.pk.attname)
> AttributeError: 'MyAction' object has no attribute 'filter_action_ptr_id'

I can't tell what's happening from the traceback alone.
Is "filter_action_ptr_id"" your actual "foo" and "MyAction" your "B"?
Maybe the relevant setup code would help...

Peter

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


Re: Detecting open files and forcing closure

2009-01-10 Thread Steven D'Aprano
On Fri, 09 Jan 2009 22:06:03 -0500, Andrew Robert wrote:

> Is there a way to detect the open files and close them out?

You can detect open files by trying to move them and catching the 
exception when you can't. You may wish to distinguish permission errors.

As far as forcing the file to close, that depends on what OS you are 
using and what permissions your application is running as. I intuit that 
you are running under Windows, because POSIX systems allow you to copy 
open files. My knowledge of Windows isn't that great, but I imagine that 
the only way to force a close is something drastic like rebooting the 
machine.

Sounds like your real problem is a social problem, not a technical one. 
Maybe you need a "failed to log out" jar (like a swear jar), and every 
person who failed to close their open files has to put a dollar in the 
jar.


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


Re: why cannot assign to function call

2009-01-10 Thread Steven D'Aprano
On Fri, 09 Jan 2009 20:23:11 +, Mark Wooding wrote:

> Steven D'Aprano  wrote:
> 
>> I'm pretty sure that no other pure-Python coder has manipulated
>> references either. They've manipulated objects.
> 
> No: not directly.  The Python program deals solely with references;
> anything involving actual objects is mediated by the runtime.

Your claim is ambiguous: when you say "the Python program", are you 
talking about the program I write using the Python language, or the 
Python VM, or something else? If the first, then you are wrong: the 
Python program I write doesn't deal with references. It deals with 
objects.

This discussion flounders because we conflate multiple levels of 
explanation. People say "You do foo" when they mean "the Python VM does 
foo". Earlier, I responded to your claim that I was storing references by 
saying I was pretty sure I didn't store references, and gave an example 
of the line of Python code x=23. Your response was to mix explanatory 
levels:

> You bind names to locations which store immediate representations.
> Python IRs are (in the sense defined above) exclusively references.

I most certainly don't bind names to locations. When I write x=23, I 
don't know what the location of the object 23 is, so how could I bind 
that location to the name? You are conflating what the Python VM does 
with what I do. What *I* do is bind the object 23 to the name x. I don't 
know the location of 23, I don't even know if 23 has a well-defined 
location or if it is some sort of distributed virtual data structure. As 
a Python programmer, that's the level I see: names and objects.

At a lower level, what the Python VM does is store the name 'x' in a 
dictionary, bound to the object 23. No locations come into it, because at 
this level of explanation, dictionaries are an abstract mapping. There's 
no requirement that the abstract dictionary structure works by storing 
addresses. All we know is that it maps the name 'x' to the object 23 
somehow. Maybe there's no persistent storage, and the dict stores 
instructions telling the VM how to recreate the object 23 when it is 
needed. Who knows? But at this explanatory level, there are no locatives 
involved. Names and objects float as disembodied entities in the aether, 
and dicts map one to the other.

At an even lower explanatory level, the CPython implementation of 
dictionaries works by storing a pointer (or if you prefer, a reference) 
to the object in a hash table. Pointers, of course, are locatives, and so 
finally we come to the explanation you prefer. We've gone from abstract 
names-and-classes to concrete pointers-to-bytes. But this is at least two 
levels deeper than what's visible in Python code. Just about the only 
time Python coders work with locatives is when they manually calculate 
some index into a string or list, or similar.

At an even lower explanatory level, all the VM does is copy bytes. And at 
a lower level still, it doesn't even copy bytes, it just flips bits. And 
below that, we're into physics, and I won't go there.

I daresay you probably get annoyed at me when I bring up explanations at 
the level of copying bytes. You probably feel that for the average Python 
programmer, *most of the time* such explanations are more obfuscatory 
than useful. Of course, there are exceptions, such as explaining why 
repeated string concatenation is likely to be slow. There is a time and a 
place for such low level explanations, but not at the high-level overview 
needed by the average Python programmer.

And you would be right. But I argue that your explanation at the level of 
references is exactly the same: it is too low level. It relies on 
specific details which may not even be true for all implementations of 
Python. It certainly relies on details which won't be true for 
hypothetical versions of Python running on exotic hardware. One can do 
massively parallel calculations using DNA, and such "DNA computers" are 
apparently Turing complete. I have no idea how one would write a Python 
virtual machine in such a biological computer, but I'm pretty sure that 
data values won't have well-defined locations in a machine that consists 
of billions of DNA molecules floating in a liquid.

If that's too bizarre for you, think about simulating a Python VM in your 
own head. If we know one thing about the human brain, it is that thoughts 
and concepts are not stored in single, well-defined locations, so when 
you think of "x=23", there is no pointer to a location in your head.


>> That's why we should try to keep the different layers of explanation
>> separate, without conflating them. Python programmers don't actually
>> flip bits, and neither do they manipulate references. Python
>> programmers don't have access to bits, or references. What they have
>> access to is objects.
> 
> No, that's my point: Python programmers /don't/ have direct access to
> objects.  The objects themselves are kept at arm's length by the
> indirection laye

Re: why cannot assign to function call

2009-01-10 Thread Aaron Brady
On Jan 9, 9:30 am, Joe Strout  wrote:
> Aaron Brady wrote:
> > Possible compromise.  You can think of functions as mutation-only.
> > You pass the object, and it gets a new (additional) name.  The old
> > name doesn't go in.  
>
> That's correct.  The reference itself is passed in, not the variable (or
> expression) that held or generated the reference in the calling code.

This is very odd, and I see it quite a bit.
Me: "You pass the object."
Joe: "That's correct.  You pass the reference."

What was wrong with my original?

> This is true.  (Technically, instead of variable, we should say "LValue"
> here -- there are things slightly more complex than simple variables
> that can serve as the left-hand side of an assignment.  So replace
> "variable" with "lvalue" above if you prefer.)

This is a point worth making.  I want to penny-pinch every term in an
introductory text, though, so, it's a tough call.

> M2: If 'fun()' returned a reference, you might be able to mutate the
> object that refers to.
> m2: You can sometimes mutate the object it refers to.
> C2: 'fun()' returns a reference.

This is horrendous.
http://en.wikipedia.org/wiki/Formal_fallacy
http://en.wikipedia.org/wiki/Affirming_the_consequent

A question: Are you Joe and you Mark certain that the simplest
possible introductory explanation makes use of the term 'reference'?
Perhaps we can have a contest for shortest/simplest/best description
of Python's data/variable/argument model.

Lastly, I don't see any reason why we couldn't make both explanations
available.  'For those coming from Java/etc; for those coming from
C++/etc.'  They would both get read.
--
http://mail.python.org/mailman/listinfo/python-list


if-else statement

2009-01-10 Thread Gandalf
other languages like PHP or javascript  as this if-else operator like
this

myVar = checking  == 1? 'string': 'other string'

is this stuff exist in python?

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


Re: if-else statement

2009-01-10 Thread Duncan Booth
Gandalf  wrote:

> other languages like PHP or javascript  as this if-else operator like
> this
> 
> myVar = checking  == 1? 'string': 'other string'
> 
> is this stuff exist in python?
> 
See http://docs.python.org/reference/expressions.html#boolean-operations

conditional_expression ::=  or_test ["if" or_test "else" expression]

...

The expression x if C else y first evaluates C (not x); if C is true, x is 
evaluated and its value is returned; otherwise, y is evaluated and its 
value is returned.
--
http://mail.python.org/mailman/listinfo/python-list


Re: Mocking `from foo import *` functions

2009-01-10 Thread Rob Williscroft
 wrote in news:a9ed10ff-d907-46f0-8c6a-
c3d95579a...@k1g2000prb.googlegroups.com in comp.lang.python:

> To answer to Rob: yeah, sure that would work, but I always thought

Just to note: you're answering a question about testing, but I 
answered how to alter the alerter module *for* testing. given it
was the import * that was causing the OP the problem, I tried to
address that.

> mocking the imported function didn't feel right. The test then depends
> on the import method of the tested module. If you later change your
> mind and decide to use "import sender" and then "sender.sendEmails()",
> you have to change your test code.

You have to add:

  import sender
  sender.sendEmails = mock_sendEmails

to your test script.

Note that doing the above *before* any other module imports
from sender, will be sufficient in *any* case, though it won't 
help if the tested code calls reload( sender ).

No need to scan every imported module for the mocked function.  

It also handles the case where other code imports the sender
module and calls sendEmails().

For additional robustness the above can be changed to:

  ## assert sender module hasn't been imported elsewhere yet
  
  import sys
  assert 'sender' not in sys.modules  

  import sender
  sender.sendEmails = mock_sendEmails

Rob.
-- 
http://www.victim-prime.dsl.pipex.com/
--
http://mail.python.org/mailman/listinfo/python-list


are there some special about '\x1a' symbol

2009-01-10 Thread sim.sim
Hi all!

I had touch with some different python behavior: I was tried to write
into a file a string with the '\x1a' symbol, and for FreeBSD system,
it gives expected result:

>>> open("test", "w").write('before\x1aafter')
>>> open('test').read()
'before\x1aafter'


but for my WinXP box, it gives some strange:

>>> open("test", "w").write('before\x1aafter')
>>> open('test').read()
'before'

Here I can write all symbols, but not read.
I've tested it with python 2.6, 2.5 and 2.2 and WinXP SP2.

Why is it so and is it possible to fix it?

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


Re: Mocking `from foo import *` functions

2009-01-10 Thread hsoft
On Jan 10, 4:19 pm, Rob Williscroft  wrote:
> Note that doing the above *before* any other module imports
> from sender, will be sufficient in *any* case, though it won't
> help if the tested code calls reload( sender ).

Yeah, of course, but most of the time, you want to mock at the *test*
level, (you don't want your mock to affect all the tests in your
module), so you can't just mock the function before you import your
tested module. That is why I created this handy TestCase.
--
http://mail.python.org/mailman/listinfo/python-list


NorthwestPythonDay, 31 Jan 2009

2009-01-10 Thread James Thiele
Posted for Brian Dorsey


Hello everyone,

On behalf of the Seattle Python Interest Group, I'd like to invite you
to join us for an informal day of Python talks & socializing.

When: January, 31st 9am - 5pm
Where: University of Washington campus, Seattle, Washington
Price: Free!
Details and updated information: http://www.seapig.org/NorthwestPythonDay

The day will be mostly 30 minute talks and two sets of 5 minute
lightning talks. We'll also have plenty of time to chat over lunch and
dinner/drinks.

We still have room for a few more talks, please propose a talk!
(http://www.seapig.org/NorthwestPythonDay for details)

Please join us and forward this on to other Python people!
--
http://mail.python.org/mailman/listinfo/python-list


Re: why cannot assign to function call

2009-01-10 Thread Mark Wooding
[Another tome.  I hope this contains sufficient new material to continue
to be of interest to other readers.]

Steven D'Aprano  wrote:
> On Fri, 09 Jan 2009 20:23:11 +, Mark Wooding wrote:
>
> > No: not directly.  The Python program deals solely with references;
> > anything involving actual objects is mediated by the runtime.
>
> Your claim is ambiguous: when you say "the Python program", are you
> talking about the program I write using the Python language, or the
> Python VM, or something else? 

The former.

> If the first, then you are wrong: the Python program I write doesn't
> deal with references. It deals with objects.

Do you have evidence for this claim other than your vigorous assertion?
I have evidence: in particular, after the example

  l = [1, 2, 3]
  l[1] = l

the list l (abuse of terminology: I mean `the list referred to from the
location bound to l') has the structure I drew, but you snipped:

 ,--,
 v  |
  +---++---+|
   l: | *> | *-> 1  |
  +---++---+|
   | *--'
   +---+
   | *-> 3
   +---+

Can you explain, without using the concept of references (or anything
else equivalent) why this happens, and why the result is not (for
example) [1, [1, 2, 3], 3]?

> This discussion flounders because we conflate multiple levels of
> explanation.

No.  I'm sticking to a single level of explanation.  It may appear to be
a lower level than the one you're trying to promote, and therefore
contains more detail and is more complicated, but it has the benefit
that it explains observed phenomena.

> People say "You do foo" when they mean "the Python VM does foo".

Since what the programmer does is type at a keyboard, the keystrokes
being interpreted eventually as editing commands in a text editor
(having been processed by various intermediate pieces of software
probably including keyboard drivers, and maybe window systems or
terminal line disciplines), anything else is an abuse of terminology;
but a useful one.

So, the programmer types, with the objective (presumably) of
constructing a text file containing a Python program which, when
executed by an appropriate Python implementation, will behave in a
satisfactory way.

The notion of `Python VM' is a good one (though potentially open to
confusion -- alas, `virtual machine' has multiple meanings too).  So: a
Python program can be interpreted as instructions to such a virtual
machine, to behave in particular ways.  Agreed?  The question is: how do
we best describe the behaviour of such a Python VM when executing a
Python program, given its text?

It's far too easy, as we've both shown, to slip into imprecise
terminology here.  When I write

> > You bind names to locations which store immediate representations.
> > Python IRs are (in the sense defined above) exclusively references.

I mean, of course, that a Python implementation behaves is if it binds
`names to locations which store immediate representations'.

> I most certainly don't bind names to locations. When I write x=23, I 
> don't know what the location of the object 23 is, so how could I bind 
> that location to the name?

Quite.  A lapse on my part, due to sloppy writing caused by a desire not
to make this article any longer than it already was.

> You are conflating what the Python VM does with what I do.

No...

> What *I* do is bind the object 23 to the name x. 

What you do is described above.  It doesn't involve any binding at all,
or objects.  Unless, that is, you are actually implementing Python
personally (i.e., not merely instructing a computer to do so, but
actually within your own mind); in which case I claim that you must do
so as I have described, or do so wrongly.

> I don't know the location of 23, I don't even know if 23 has a
> well-defined location

I warned that I was using certain terms in a technical sense, and
attempted to define them clearly at the top of my article.  The word
`location' was one such:

:   * A /location/[1] is an area of memory suitable for storing the
: /immediate representation/ (which I shall abbreviate to /IR/) of a
: value.  (A location may be capable of storing things other than
: IRs, e.g., representations of unevaluated expressions in lazily
: evaluated languages.  Locations may vary in size, e.g., in order
: to be capable of storing different types of IRs.)

Whether 23 has a location is unimportant.  In the context of your
example `x = 23', what's important is that the name `x' is bound to a
location, and then an immediate representation of the value `23' is
stored in that location.

(This description holds for all languages I can think of, modulo details
of which occurrences of names are binding occurrences, and C++'s user-
controlled assignment and so on.)

> or if it is some sort of distributed virtual data structure. As a
> Python programmer, that's the level I see: names and 

Re: why cannot assign to function call

2009-01-10 Thread Mark Wooding
ru...@yahoo.com  wrote:

> Agreed.  I think the docs, especially those that develop
> the conceptual model of how Python work at runtime, could
> use some major attention.

If we can achieve consensus in this (still remarkably civil) discussion,
we might be able to amend the docs.

> I would be willing to bet that most of the confused posters do not
> distinguish between assignment operation (AO) and data model (DM).
> Their conceptual box is labeled "assignment behavior" and includes
> both AO and DM.  They expect that AO+DM in Python will produce the
> same results in Python as they are used to in the other languages
> they've used.  That the discrepancy comes from the DM part rather than
> the AO part is pretty irrelevant to them given that world view.

I think that we're in agreement, up to this point at least.  That's
good.

My claim is that, until they learn to distinguish these two aspects of
Python's semantics, they will remain confused.

-- [mdw]
--
http://mail.python.org/mailman/listinfo/python-list


Re: why cannot assign to function call

2009-01-10 Thread Mel
Aaron Brady wrote:

> Lastly, I don't see any reason why we couldn't make both explanations
> available.  'For those coming from Java/etc; for those coming from
> C++/etc.'  They would both get read.

That's what I was just thinking .. there are lots of others, too: "for those
coming from relational database theory..." would be a good one.

Mel.

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


Re: are there some special about '\x1a' symbol

2009-01-10 Thread Marc 'BlackJack' Rintsch
On Sat, 10 Jan 2009 07:45:53 -0800, sim.sim wrote:

> I had touch with some different python behavior: I was tried to write
> into a file a string with the '\x1a' symbol, and for FreeBSD system, it
> gives expected result:
> 
 open("test", "w").write('before\x1aafter') open('test').read()
> 'before\x1aafter'
> 
> 
> but for my WinXP box, it gives some strange:
> 
 open("test", "w").write('before\x1aafter') open('test').read()
> 'before'
> 
> Here I can write all symbols, but not read. I've tested it with python
> 2.6, 2.5 and 2.2 and WinXP SP2.
> 
> Why is it so and is it possible to fix it?

\x1a is treated as "end of text" character in text files by Windows.  So 
if you want all, unaltered data, open the file in binary mode ('rb' and 
'wb').

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


Re: are there some special about '\x1a' symbol

2009-01-10 Thread Mel
sim.sim wrote:

> Hi all!
> 
> I had touch with some different python behavior: I was tried to write
> into a file a string with the '\x1a' symbol, and for FreeBSD system,
> it gives expected result:
> 
 open("test", "w").write('before\x1aafter')
 open('test').read()
> 'before\x1aafter'
> 
> 
> but for my WinXP box, it gives some strange:
> 
 open("test", "w").write('before\x1aafter')
 open('test').read()
> 'before'
> 
> Here I can write all symbols, but not read.
> I've tested it with python 2.6, 2.5 and 2.2 and WinXP SP2.
> 
> Why is it so and is it possible to fix it?

'\x1a' is the End-of-file mark that Windows inherited from MS-DOS and CP/M. 
The underlying Windows libraries honour it for files opened in text mode.

open ('test', 'rb').read()

will read the whole file.

Mel.

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


Re: State of the art: Tkinter, Tk 8.5, Tix?

2009-01-10 Thread r
We need TK 8.5's themes. This will bring Tkinter out of the dark ages
and into the 21st Century! And improve the shine of the Python base
distro. Python could use a good boost right now!
--
http://mail.python.org/mailman/listinfo/python-list


Looking for an efficient Python script to download and save a .zip file programmatically

2009-01-10 Thread David Shi
I am looking for an efficient Python script to download and save a .zip file 
programmatically (from http or https call).
 
Regards.
 
David


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


Re: Implementing file reading in C/Python

2009-01-10 Thread Francesco Bochicchio
On Fri, 09 Jan 2009 15:34:17 +, MRAB wrote:

> Marc 'BlackJack' Rintsch wrote:
>> On Fri, 09 Jan 2009 04:04:41 +0100, Johannes Bauer wrote:
>> 
>>> As this was horribly slow (20 Minutes for a 2GB file) I coded the whole
>>> thing in C also:
>> 
>> Yours took ~37 minutes for 2 GiB here.  This "just" ~15 minutes:
>> 
>> #!/usr/bin/env python
>> from __future__ import division, with_statement
>> import os
>> import sys
>> from collections import defaultdict
>> from functools import partial
>> from itertools import imap
>> 
>> 
>> def iter_max_values(blocks, block_count):
>> for i, block in enumerate(blocks):
>> histogram = defaultdict(int)
>> for byte in block:
>> histogram[byte] += 1
>> 
>> yield max((count, byte)
>>   for value, count in histogram.iteritems())[1]
>> 
> [snip]
> Would it be faster if histogram was a list initialised to [0] * 256?

I tried it on my computer, also getting character codes with
struct.unpack, like this:

histogram = [0,]*256

for byte in struct.unpack( '%dB'%len(block), block ): 
histogram[byte] +=1 

yield max(( count, byte ) 
  for idx, count in enumerate(histogram))[1] 

and I also removed the map( ord ... ) statement in main program, since
iter_max_values mow returns character codes directly.

The result is 10 minutes against the 13 of the original 'BlackJack's code
on my PC (iMac Intel python 2.6.1).

Strangely, using histogram = array.array( 'i',  [0,]*256 ) gives again 13
minutes, even if I create the array outside the loop and then use 
  histogram[:] = zero_array to reset the values.


Ciao
-
FB






 

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


Re: Looking for an efficient Python script to download and save a .zip file programmatically

2009-01-10 Thread Albert Hopkins
On Sat, 2009-01-10 at 17:12 +, David Shi wrote:

> I am looking for an efficient Python script to download and save
> a .zip file programmatically (from http or https call).
>  
> Regards.
>  
> David



urllib?

-a

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


Re: if-else statement

2009-01-10 Thread Philip Semanchuk


On Jan 10, 2009, at 9:26 AM, Duncan Booth wrote:


Gandalf  wrote:


other languages like PHP or javascript  as this if-else operator like
this

myVar = checking  == 1? 'string': 'other string'

is this stuff exist in python?


See http://docs.python.org/reference/expressions.html#boolean-operations

conditional_expression ::=  or_test ["if" or_test "else" expression]

...

The expression x if C else y first evaluates C (not x); if C is  
true, x is

evaluated and its value is returned; otherwise, y is evaluated and its
value is returned.


Gandalf,
I'd add to the above that this expression was only added to Python in  
v2.5, so if you want your code to be compatible with versions of  
Python <= 2.4, you should not use the ternary if.


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


Re: why cannot assign to function call

2009-01-10 Thread rurpy
On Jan 9, 6:47 am, Mark Wooding  wrote:
> ru...@yahoo.com  wrote:
> > As a side comment (because it always bugs me when I read this, even
> > though I read it in very authoritative sources), ISTM that C passes
> > everything by value except arrays; they are passed by reference (by
> > passing a pointer to the array by value.)  Admittedly, the close
> > relationship between arrays and pointers makes it easy conflate them.
>
> Arrays are distinctly second-class citizens in C.
>
> Basically, in C, arrays aren't passed at all but there's some syntactic
> sugar so you can squint and con yourself that they're passed by
> reference.
>
> If you try to pass an array, the array name immediately decays to a
> pointer, and the pointer gets passed instead -- by value.  The
> corresponding function parameter must be a pointer to an approrpriate
> kind of thing, though you're allowed to write []s to confuse yourself if
> you like -- T D[] in a function parameter declaration means precisely
> the same as T *D -- to the extent that &D has type T **D and so on.

What is the observable difference between converting an
array to a reference (pointer) to that array and passing
the reference by value, and passing the array by reference?

That is, given a C-like compiler that is the same as
C except that it passes arrays by reference, how would
it differ from an ordinary C compiler?

The choice of terminology (in this case) seems to me to
be a matter of convention rather than any fundamental
observable difference.  I guess the case for pass-by-value
would be a little stronger because one has to have "passing
a pointer by value" anyway (since pointers are first-class
datatypes) and that can be used to describe passing arrays
(as you described).  Adding a second mechanism,
"passing-arrays-by-reference", is perhaps unnecessary,
but not wrong, and may be more easily understandable
to the target audience.
--
http://mail.python.org/mailman/listinfo/python-list


Re: distinction between unzipping bytes and unzipping a file

2009-01-10 Thread webcomm
On Jan 9, 6:07 pm, John Machin  wrote:
> Yup, it looks like it's encoded in utf_16_le, i.e. no BOM as
> God^H^H^HGates intended:
>
> >>> buff = open('data', 'rb').read()
> >>> buff[:100]
>
> '<\x00R\x00e\x00g\x00i\x00s\x00t\x00r\x00a\x00t\x00i\x00o\x00n\x00>
> \x00<\x00B\x0
> 0a\x00l\x00a\x00n\x00c\x00e\x00D\x00u\x00e\x00>
> \x000\x00.\x000\x000\x000\x000\x0
> 0<\x00/\x00B\x00a\x00l\x00a\x00n\x00c\x00e\x00D\x00u\x00e\x00>\x00<
> \x00S\x00t\x0
> 0a\x00t\x00'>>> buff[:100].decode('utf_16_le')

There it is.  Thanks.

> u'0.
>
>
> >  But if I return it to my browser with python+django,
> > there are bad characters every other character
>
> Please consider that we might have difficulty guessing what "return it
> to my browser with python+django" means. Show actual code.

I did stop and consider what code to show.  I tried to show only the
code that seemed relevant, as there are sometimes complaints on this
and other groups when someone shows more than the relevant code.  You
solved my problem with decode('utf_16_le').  I can't find any
description of that encoding on the WWW... and I thought *everything*
was on the WWW.  :)

I didn't know the data was utf_16_le-encoded because I'm getting it
from a service.  I don't even know if *they* know what encoding they
used.  I'm not sure how you knew what the encoding was.

> Please consider reading the Unicode HOWTO 
> athttp://docs.python.org/howto/unicode.html

Probably wouldn't hurt, though reading that HOWTO wouldn't have given
me the encoding, I don't think.

-Ryan


> Cheers,
> John

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


Re: BadZipfile "file is not a zip file"

2009-01-10 Thread webcomm
On Jan 9, 7:33 pm, John Machin  wrote:
> It is not impossible for a file with dummy data to have been
> handcrafted or otherwise produced by a process different to that used
> for a real-data file.

I knew it was produced by the same process, or I wouldn't have shared
it. : )
But you couldn't have known that.


> > Not sure if you've seen this 
> > thread...http://groups.google.com/group/comp.lang.python/browse_thread/thread/...
>
> Yeah, I've seen it ... (sigh) ... pax Steve Holden, but *please* stick
> with one thread ...

Thanks... I thought I was posting about separate issues and would
annoy people who were only interested in one of the issues if I put
them both in the same thread.  I guess all posts re: the same script
should go in one thread, even if the questions posed may be unrelated
and may be separate issues.  There are grey areas.

Problem solved in John Machin's post at
http://groups.google.com/group/comp.lang.python/browse_thread/thread/d84f42493fe81864/03b8341539d87989?hl=en&lnk=raot#03b8341539d87989

I'll post the final code when it's prettier.

-Ryan

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


Re: download timeout vs. socket timeout

2009-01-10 Thread Giampaolo Rodola'
On 10 Gen, 03:07, "p."  wrote:
> i'm using urllib2 in python 2.4
>
> wondering how people typically deal with the case in which a download
> is too slow. setting the socket timeout only covers those cases where
> there is no response in the socket for whatever the timeout period is.
> what if, however, i'm getting bits back but want simply to bail out if
> the total time to download takes too long?
>
> i'm trying to avoid creating a whole other thread if possible?

You could retrieve your file in little parts and at the same time have
a thread which checks how many bytes have been transmitted every
second.


--- Giampaolo
http://code.google.com/p/pyftpdlib
--
http://mail.python.org/mailman/listinfo/python-list


Re: why cannot assign to function call

2009-01-10 Thread Mark Wooding
ru...@yahoo.com  wrote:

> What is the observable difference between converting an
> array to a reference (pointer) to that array and passing
> the reference by value, and passing the array by reference?

For one:

#include 

static size_t foo(char v[]) { return sizeof v; }

int main(void)
{
  char v[sizeof(char *) * 10];
  
  puts(foo(v) == sizeof(char *) ? "pointer" : "reference");
  return (0);
}

For another:

static void bar(char v[]) { char ch = 0; v = &ch; }
  /* type error if arrays truly passed by reference */

> I guess the case for pass-by-value would be a little stronger because
> one has to have "passing a pointer by value" anyway (since pointers
> are first-class datatypes) and that can be used to describe passing
> arrays (as you described).

The difference is that the /callee/ function is different between the
two cases.

Also, notice that arrays in expressions turn into pointers in the same
way, so function argument passing works the same way as assignment -- a
hallmark of pass-by-value.

-- [mdw]
--
http://mail.python.org/mailman/listinfo/python-list


Re: why cannot assign to function call

2009-01-10 Thread Joe Strout

ru...@yahoo.com wrote:


What is the observable difference between converting an
array to a reference (pointer) to that array and passing
the reference by value, and passing the array by reference?


The difference is whether an assignment to the formal parameter (within 
the function) affects the actual parameter (in the calling code).  If it 
does, then that's pass by reference.  If it does not, then that's pass 
by value.



That is, given a C-like compiler that is the same as
C except that it passes arrays by reference, how would
it differ from an ordinary C compiler?


Such a compiler is available: it's called C++, and it gives the 
programmer the choice to pass by value or pass by reference (the latter 
indicated by adding "&" to the parameter in the function declaration, 
just like you would add "ByRef" in RB or VB.NET).


If the parameter is called "foo", and you pass in "bar", then

  foo = SomeNewArray();

would change bar if it were passed by reference; it would not affect bar 
at all if it were passed by value.


The two are quite distinct.

Best,
- Joe

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


Re: why cannot assign to function call

2009-01-10 Thread Joe Strout

Aaron Brady wrote:


Aaron Brady wrote:

Possible compromise.  You can think of functions as mutation-only.
You pass the object, and it gets a new (additional) name.  The old
name doesn't go in.  

That's correct.  The reference itself is passed in, not the variable (or
expression) that held or generated the reference in the calling code.


This is very odd, and I see it quite a bit.
Me: "You pass the object."
Joe: "That's correct.  You pass the reference."

What was wrong with my original?


I'm saying that I believe your idea was correct, but worded imprecisely 
(IMHO of course).  So I restated what I believed you were saying, using 
terminology which I believe to be more precise.  If nothing else, this 
gives you the opportunity to say "No, that's not what I meant at all."



This is true.  (Technically, instead of variable, we should say "LValue"
here -- there are things slightly more complex than simple variables
that can serve as the left-hand side of an assignment.  So replace
"variable" with "lvalue" above if you prefer.)


This is a point worth making.  I want to penny-pinch every term in an
introductory text, though, so, it's a tough call.


Agreed.


M2: If 'fun()' returned a reference, you might be able to mutate the
object that refers to.
m2: You can sometimes mutate the object it refers to.
C2: 'fun()' returns a reference.


This is horrendous.
http://en.wikipedia.org/wiki/Formal_fallacy
http://en.wikipedia.org/wiki/Affirming_the_consequent


I did point out that the logic was incorrect (even though the 
conclusion, in this case, happens to be true).



A question: Are you Joe and you Mark certain that the simplest
possible introductory explanation makes use of the term 'reference'?


I am.


Perhaps we can have a contest for shortest/simplest/best description
of Python's data/variable/argument model.


Sure -- but it judging it might be difficult, requiring some newbies and 
a test to check their comprehension (as you and I pondered once before).



Lastly, I don't see any reason why we couldn't make both explanations
available.  'For those coming from Java/etc; for those coming from
C++/etc.'  They would both get read.


Yes, except that the reference explanation applies equally well to 
anyone coming from Java, C/C++, RB, VB.NET, and probably others... I'm 
not sure to whom the other explanation would appeal, unless perhaps it's 
LISP programmers (just a guess, since those on that side of the aisle 
seem to invoke LISP more frequently).



Best,
- Joe


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


Re: if-else statement

2009-01-10 Thread Scott David Daniels

Duncan Booth wrote:

Gandalf  wrote:

other languages ... [have an] if-else operator like...
 myVar = checking  == 1? 'string': 'other string'

See http://docs.python.org/reference/expressions.html#boolean-operations
...
The expression x if C else y first evaluates C (not x); if C is true, x is 
evaluated and its value is returned; otherwise, y is evaluated and its 
value is returned.


While we _do_ have the form  if  else , it is
generally agreed that in most cases your code will be more clear
with explicit tests and assignments.  Your particular example
is better

 if checking:
 my_var = 'string'
 else:
 my_var = 'other string'

remember, vertical space only kills trees if printed.

--Scott David Daniels  (who still prints this too frequently).
scott.dani...@acm.org
--
http://mail.python.org/mailman/listinfo/python-list


Re: download timeout vs. socket timeout

2009-01-10 Thread Giampaolo Rodola'
I'm sorry. I realized they had already replied when it was too late.


--- Giampaolo
http://code.google.com/p/pyftpdlib
--
http://mail.python.org/mailman/listinfo/python-list


Re: if-else statement

2009-01-10 Thread bearophileHUGS
Scott David Daniels:
>       if checking:
>           my_var = 'string'
>       else:
>           my_var = 'other string'
>
> remember, vertical space only kills trees if printed.

I value clarity a lot. But this is more DRY, sometimes it's almost
equally clear, and you reduce vertical space, packing more code in the
same space, this allows you to see more logic, allowing you to have a
higher level view of the code, so allows you to understand the code
better:

my_var = 'string' if checking else 'other string'

So I think that sometimes it's useful.

Bye,
bearophile
--
http://mail.python.org/mailman/listinfo/python-list


Re: urlopen exception

2009-01-10 Thread Steve Holden
asit wrote:
> site="www.bput.org"
> payloads="alert('xss')"
> attack= urllib2.urlopen(site+payloads,80).readlines()
> 
> according to my best knowledge, the above code is correct.
> but why it throws exceptio 

what exception it throw?
-- 
Steve Holden+1 571 484 6266   +1 800 494 3119
Holden Web LLC  http://www.holdenweb.com/

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


Re: State of the art: Tkinter, Tk 8.5, Tix?

2009-01-10 Thread excord80
On Jan 10, 11:45 am, r  wrote:
> We need TK 8.5's themes. This will bring Tkinter out of the dark ages
> and into the 21st Century! And improve the shine of the Python base
> distro. Python could use a good boost right now!

Could someone please explain what Tix provides compared to what the
new stuff in Tk 8.5 provides? Is there much overlap?
--
http://mail.python.org/mailman/listinfo/python-list


Re: distinction between unzipping bytes and unzipping a file

2009-01-10 Thread John Machin
On Jan 11, 6:15 am, webcomm  wrote:
> On Jan 9, 6:07 pm, John Machin  wrote:
>
> > Yup, it looks like it's encoded in utf_16_le, i.e. no BOM as
> > God^H^H^HGates intended:
>
> > >>> buff = open('data', 'rb').read()
> > >>> buff[:100]
>
> > '<\x00R\x00e\x00g\x00i\x00s\x00t\x00r\x00a\x00t\x00i\x00o\x00n\x00>
> > \x00<\x00B\x0
> > 0a\x00l\x00a\x00n\x00c\x00e\x00D\x00u\x00e\x00>
> > \x000\x00.\x000\x000\x000\x000\x0
> > 0<\x00/\x00B\x00a\x00l\x00a\x00n\x00c\x00e\x00D\x00u\x00e\x00>\x00<
> > \x00S\x00t\x0
> > 0a\x00t\x00'
> > >>> buff[:100].decode('utf_16_le')
>
> There it is.  Thanks.
>
> > u'0.
> > >  But if I return it to my browser with python+django,
> > > there are bad characters every other character
>
> > Please consider that we might have difficulty guessing what "return it
> > to my browser with python+django" means. Show actual code.
>
> I did stop and consider what code to show.  I tried to show only the
> code that seemed relevant, as there are sometimes complaints on this
> and other groups when someone shows more than the relevant code.  You
> solved my problem with decode('utf_16_le').  I can't find any
> description of that encoding on the WWW... and I thought *everything*
> was on the WWW.  :)

Try searching using the official name UTF-16LE ... looks like a blind
spot in the approximate matching algorithm(s) used by the search engine
(s) that you tried :-(

> I didn't know the data was utf_16_le-encoded because I'm getting it
> from a service.  I don't even know if *they* know what encoding they
> used.  I'm not sure how you knew what the encoding was.

Actually looked at the raw data. Pattern appeared to be an alternation
of 1 "meaningful" byte and one zero ('\x00') byte: => UTF16*. No BOM
('\xFE\xFF' or '\xFF\xFE') at start of file: => UTF16-?E. First byte
is meaningful: => UTF16-LE.

> > Please consider reading the Unicode HOWTO at 
> > http://docs.python.org/howto/unicode.html
>
> Probably wouldn't hurt,

Definitely won't hurt. Could even help.

> though reading that HOWTO wouldn't have given
> me the encoding, I don't think.

It wasn't intended to give you the encoding. Just read it.

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


Python 2.6.1 @executable_path

2009-01-10 Thread googler . 1 . webmaster
Hi!

I asked something similar a few days ago. Is it possible to compile
Python 2.6.1 with a dynamic path?
I don't know how the configure command would look like.

This is my current configure command for the default /Library/
Frameworks/ path:

./configure --with-framework-name=Python --with-universal-archs=all --
enable-framework --enable-universalsdk=/

Can anyone help me?
Thanks.
--
http://mail.python.org/mailman/listinfo/python-list


Python 2.6.1 @executable_path

2009-01-10 Thread googler . 1 . webmaster
Hi!

I asked something similar a few days ago. Is it possible to compile
Python 2.6.1 with a dynamic path on MacOSX?
I don't know how the configure command would look like.

This is my current configure command for the default /Library/
Frameworks/ path:

./configure --with-framework-name=Python --with-universal-archs=all --
enable-framework --enable-universalsdk=/

Can anyone help me?
Thanks.
--
http://mail.python.org/mailman/listinfo/python-list


Python 2.6.1 @executable_path

2009-01-10 Thread googler . 1 . webmaster
Hi!

I asked something similar a few days ago. Is it possible to compile
Python 2.6.1 with a dynamic path on Mac OSX Leopard 10.5.x. I found
out that I could use @executable_path but I don't know how the
configure command would look like.

This is my current configure command for the default /Library/
Frameworks/ path:

./configure --with-framework-name=Python --with-universal-archs=all --
enable-framework --enable-universalsdk=/

Can anyone help me?
Thanks.
--
http://mail.python.org/mailman/listinfo/python-list


Re: are there some special about '\x1a' symbol

2009-01-10 Thread John Machin
On Jan 11, 2:45 am, "sim.sim"  wrote:
> Hi all!
>
> I had touch with some different python behavior: I was tried to write
> into a file a string with the '\x1a' symbol, and for FreeBSD system,
> it gives expected result:
>
> >>> open("test", "w").write('before\x1aafter')
> >>> open('test').read()
>
> 'before\x1aafter'
>
> but for my WinXP box, it gives some strange:
>
> >>> open("test", "w").write('before\x1aafter')
> >>> open('test').read()
>
> 'before'
>
> Here I can write all symbols, but not read.
> I've tested it with python 2.6, 2.5 and 2.2 and WinXP SP2.
>
> Why is it so and is it possible to fix it?

You've already got two good answers, but this might add a little more
explanation:

You will aware that in Windows Command Prompt, to exit the interactive
mode of Python (among others), you need to type Ctrl-Z ...

| C:\junk>python
| Python 2.5.2 (r252:60911, Feb 21 2008, 13:11:45) [MSC v.1310 32 bit
(Intel)] on
win32
| Type "help", "copyright", "credits" or "license" for more
information.
| >>> problem = '\x1a'
| >>> ord(problem)
| 26
| >>> # What is the 26th letter of the English/ASCII alphabet?
| ...
| >>> ^Z
|
| C:\junk>

HTH,
John
--
http://mail.python.org/mailman/listinfo/python-list


Re: Python 2.6.1 @executable_path

2009-01-10 Thread Martin v. Löwis
> I asked something similar a few days ago. Is it possible to compile
> Python 2.6.1 with a dynamic path?

What is a "dynamic path"?

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


Re: Python 2.6.1 @executable_path

2009-01-10 Thread googler . 1 . webmaster
I want to embedd it into another app so on the 'customers'-mac I want
to put python into a subdirectory of my app.
bye
--
http://mail.python.org/mailman/listinfo/python-list


Re: Python 2.6.1 @executable_path

2009-01-10 Thread googler . 1 . webmaster
I want to embedd it into my app so on the 'customers'-mac I want
to put python into a subdirectory of my app.

and with the configure command above, that will not work
because the library has to be on every system in /Library/Framework/
so I found out that @executable_path is replaced by the path of the
app.
bye
--
http://mail.python.org/mailman/listinfo/python-list


Re: Python 2.6.1 @executable_path

2009-01-10 Thread Ned Deily
In article 
,
 googler.1.webmas...@spamgourmet.com wrote:

> I want to embedd it into my app so on the 'customers'-mac I want
> to put python into a subdirectory of my app.
> 
> and with the configure command above, that will not work
> because the library has to be on every system in /Library/Framework/
> so I found out that @executable_path is replaced by the path of the
> app.

Have you looked at py2app yet?  It should take care of all that for you.

http://svn.pythonmac.org/py2app/py2app/trunk/doc/index.html

-- 
 Ned Deily,
 n...@acm.org

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


Re: Looking for an efficient Python script to download and save a .zip file programmatically

2009-01-10 Thread Chris Rebert
On Sat, Jan 10, 2009 at 9:12 AM, David Shi  wrote:
> I am looking for an efficient Python script to download and save a .zip file
> programmatically (from http or https call).

You want urllib.urlretrieve():
http://docs.python.org/library/urllib.html#urllib.urlretrieve

Cheers,
Chris

-- 
Follow the path of the Iguana...
http://rebertia.com
--
http://mail.python.org/mailman/listinfo/python-list


Re: Python 2.6.1 @executable_path

2009-01-10 Thread googler . 1 . webmaster
Thanks for the link but I don't want to do a make a python script as
an applicatin, I want to embedd python into a C++ app so thats the
reason why I have to compile Python.
--
http://mail.python.org/mailman/listinfo/python-list


Re: State of the art: Tkinter, Tk 8.5, Tix?

2009-01-10 Thread r
On Jan 10, 3:05 pm, excord80  wrote:
> On Jan 10, 11:45 am, r  wrote:
>
> > We need TK 8.5's themes. This will bring Tkinter out of the dark ages
> > and into the 21st Century! And improve the shine of the Python base
> > distro. Python could use a good boost right now!
>
> Could someone please explain what Tix provides compared to what the
> new stuff in Tk 8.5 provides? Is there much overlap?

TIX is just a set of compound widgets that were not included in the
base Python Tkinter distro(notebook, label entry, etc... Now, it looks
like they are standard. The new 8.5 TK includes support for OS
specific themes, so as to give a more native feel to TK apps, hence
the 21st century analogy :)
--
http://mail.python.org/mailman/listinfo/python-list


Re: urlopen exception

2009-01-10 Thread Chris Rebert
On Sat, Jan 10, 2009 at 9:56 AM, asit  wrote:
> site="www.bput.org"
> payloads="alert('xss')"
> attack= urllib2.urlopen(site+payloads,80).readlines()
>
> according to my best knowledge, the above code is correct.
> but why it throws exceptio 

Because it's not correct. It's trying to load
www.bput.orgalert('xss') which is definitely not a
valid URL.
Also, you need to specify the protocol in the URL, i.e. http://

Cheers,
Chris

-- 
Follow the path of the Iguana...
http://rebertia.com
--
http://mail.python.org/mailman/listinfo/python-list


Re: State of the art: Tkinter, Tk 8.5, Tix?

2009-01-10 Thread Kevin Walzer

excord80 wrote:



Could someone please explain what Tix provides compared to what the
new stuff in Tk 8.5 provides? Is there much overlap?


Tix is a compiled Tk extension that adds a good number of widgets to the 
base set, such as a notebook tab, treeview, combobox, and others; Python 
includes a Tix wrapper as part of the standard library. However, it has 
not undergone much development over the past several years, and its 
widgets look very outdated (they have a mid-90s feel to them).


The themed Tk widgets (ttk) that come with Tk 8.5 add a lot of the same 
things that Tix does, but they do so in a more modern way, hooking into 
platform-specific themes and API's wherever possible (XP, Vista, Mac) 
and updating the generic X11 look as well. As such, they are more 
appropriate for modern development. Tix is more of a legacy toolkit.


A Python wrapper for ttk can be found here: 
http://code.google.com/p/python-ttk/
It will be added to the standard library at some point, probably for 
Python 3.1.


Here are some screenshots:

http://code.google.com/p/python-ttk/wiki/Screenshots

HTH,
Kevin

--
Kevin Walzer
Code by Kevin
http://www.codebykevin.com
--
http://mail.python.org/mailman/listinfo/python-list


Re: State of the art: Tkinter, Tk 8.5, Tix?

2009-01-10 Thread Kevin Walzer

excord80 wrote:

On Jan 10, 11:45 am, r  wrote:

We need TK 8.5's themes. This will bring Tkinter out of the dark ages
and into the 21st Century! And improve the shine of the Python base
distro. Python could use a good boost right now!


Could someone please explain what Tix provides compared to what the
new stuff in Tk 8.5 provides? Is there much overlap?


And here are some Tix screenshots:

http://tixlibrary.sourceforge.net/screenshots/index.shtml

--
Kevin Walzer
Code by Kevin
http://www.codebykevin.com
--
http://mail.python.org/mailman/listinfo/python-list


Re: type conversion

2009-01-10 Thread Lie
On Jan 3, 10:23 am, r  wrote:
> On Jan 2, 7:46 pm, Steven D'Aprano  
> wrote:
>
> ...incessant rambling about a news reader , 101 excuses for butting
> into a thread
> [snip]

... public display of ignorance of newsgroup ethics, 101 excuses for
not knowing proper terminologies.

> Throw your newsreader in the garbage and use Google groups, less
> headache, more filling!
>

Really? I found Google Groups lacking for many thing. Google Groups'
only advantage is being browser-based.

> No need to worry about
> "hidden headers" And you may even get a star or 2 :)

Don't you realize that even Google Groups handles those hidden headers
too. And that the hidden headers aren't really hidden, even in Google
Groups.

> > If people are ignoring a thread, they won't even see your post even though 
> > you have changed the subject line.
>
> Yea NO $#Y, that makes a lot of sense, if i am ignoring something i
> course i will not see it!
> OK, Steven so you did not go off topic you simply high-jacked this
> thread. I get it now :)

"Yea NO $#Y", that doesn't make sense. Yea or NO? or $#Y?
--
http://mail.python.org/mailman/listinfo/python-list


Re: why cannot assign to function call

2009-01-10 Thread Steven D'Aprano
On Sat, 10 Jan 2009 12:52:47 -0700, Joe Strout wrote:

>> What is the observable difference between converting an array to a
>> reference (pointer) to that array and passing the reference by value,
>> and passing the array by reference?
> 
> The difference is whether an assignment to the formal parameter (within
> the function) affects the actual parameter (in the calling code).  If it
> does, then that's pass by reference.  If it does not, then that's pass
> by value.

Such a definition is incomplete. You are mischaracterising call-by-
reference as defined by a single binary state: assignment either affects 
the caller, or it doesn't. If it does, it is p-b-r, if it doesn't, it's p-
b-v. According to this definition, there are no other argument passing 
strategies possible. That's an enormously broad brush by which you sweep 
away decades of comp sci terminology. Not just Barbara Liskov and pass-by-
object, but Algol's thunk-based pass-by-name and Haskell's pass-by-need.

In other words, you have created a false dichotomy between pass-by-
reference and pass-by-value.

There are actually three fundamental characteristics of pass-by-reference:

* passing a value by reference does not lead to the value being copied, 
in contrast with pass-by-value where it does;

* modifications to the value passed by reference are visible to the 
caller;

* assignments to the value passed by reference are visible to the caller.

Pascal VAR parameters have all three characteristics. Pascal non-VAR 
parameters have none of them. Python parameters have two of the three. C 
parameters (call-by-value) have none of them, except for arrays, where 
they have all three, making arrays in C behave just like Pascal pass-by-
reference VAR parameters.



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


Re: urlopen exception

2009-01-10 Thread Paul Rubin
asit  writes:
> site="www.bput.org"
> payloads="alert('xss')"
> attack= urllib2.urlopen(site+payloads,80).readlines()
> 
> according to my best knowledge, the above code is correct.
> but why it throws exceptio 

The code is incorrect.  Look at the string ou are sending into
urlopen.  What on earth are you trying to do?
--
http://mail.python.org/mailman/listinfo/python-list


Re: Looking for an efficient Python script to download and save a .zip file programmatically

2009-01-10 Thread MrJean1
Here are some examples using urllib.urlretrieve():





/Jean Brouwers


On Jan 10, 2:23 pm, Chris Rebert  wrote:
> On Sat, Jan 10, 2009 at 9:12 AM, David Shi  wrote:
> > I am looking for an efficient Python script to download and save a .zip file
> > programmatically (from http or https call).
>
> You want 
> urllib.urlretrieve():http://docs.python.org/library/urllib.html#urllib.urlretrieve
>
> Cheers,
> Chris
>
> --
> Follow the path of the Iguana...http://rebertia.com

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


Pickling classes (not class instances)

2009-01-10 Thread Nicolas M . Thiéry
Dear python developers,

Purpose of this e-mail:
---

How to customize how a class (not an instance of a class!!!) is
pickled?


Example:
==
class metaclass(type):
def __new__(mcs, name, bases, dict):
print " running metaclass.__new__"
return type.__new__(mcs, name, bases, dict)
#
def __reduce_class__(self):
print "reducing a class"
# do the real work

c = metaclass("foo", (object,), dict())

import copy_reg
copy_reg.pickle(metaclass, metaclass.__reduce_class__)

pickle.dumps(c)
---
PicklingError Traceback (most recent call
last)
...
PicklingError: Can't pickle : it's not found as
__main__.foo
==


Context:


I am working on the Sage project (www.sagemath.org), and more
precisely on the category infrastructure. The code is building lots of
classes on the fly by composing preexisting classes by inheritance
(for the curious, see the doc of the class Category in
http://sage.math.washington.edu:2144/file/1567cea09170/categories-nt.patch).

Since those classes are built on the fly, they cannot be pickled with
the default mechanism of name lookup. A proper pickling would be to
rebuild the class anew. Nothing problematic, except for the question
above.


Discussion:
---

It sounds like copy_reg would be the natural way to go (as in the
example above). However, its documentation suggests that it explicitly
is not intended for pickling classes, e.g. first paragraph of:

http://docs.python.org/library/copy_reg.html#module-copy_reg

is:

  The copy_reg module provides support for the pickle and cPickle
  modules. The copy module is likely to use this in the future as
  well.  It provides configuration information about object
  constructors which are not classes. Such constructors may be factory
  functions or class instances.

And indeed, looking up at the code of pickle (l. 289-299 of pickle.py)
(and similarly in cpickle), the copy-reg dispatch is explicit bypassed
for metaclasses:


# Check for a class with a custom metaclass; treat as regular
class
try:
issc = issubclass(t, TypeType)
except TypeError: # t is not a class (old Boost; see SF
#502085)
issc = 0
if issc:
self.save_global(obj)
return

# Check copy_reg.dispatch_table
reduce = dispatch_table.get(t)

Which causes the failure above.


Is there a specific reason for this restriction?

Would it be thinkable to move up the copy reg dispatch before the
metaclass treatment in pickle and cPickle? I did it locally, and it
fixed my problem.

If not, what would be the right way to achieve this?

Many thanks in advance!

Best regards,
Nicolas
--
Nicolas M. Thiéry "Isil" 
http://Nicolas.Thiery.name/
--
http://mail.python.org/mailman/listinfo/python-list


Re: why cannot assign to function call

2009-01-10 Thread Rhodri James

On Sat, 10 Jan 2009 18:44:37 -,  wrote:


What is the observable difference between converting an
array to a reference (pointer) to that array and passing
the reference by value, and passing the array by reference?


This is a red herring, though.  From either viewpoint, C
arrays are anomalous in comparison with other C data types.

--
Rhodri James *-* Wildebeeste Herder to the Masses
--
http://mail.python.org/mailman/listinfo/python-list


Regex for unicode letter characters

2009-01-10 Thread schickb
I need a regex that will match strings containing only unicode letter
characters (not including numeric or the _ character). I was surprised
to find the 're' module does not include a special character class for
this already (python 2.6). Or did I miss something?

It seems like this would be a very common need. Is the following the
only option to generate the character class (based on an old post by
Martin v. Löwis )?

import unicodedata, sys

def letters():
start = end = None
result = []
for index in xrange(sys.maxunicode + 1):
c = unichr(index)
if unicodedata.category(c)[0] == 'L':
if start is None:
start = end = c
else:
end = c
elif start:
if start == end:
result.append(start)
else:
result.append(start + "-" + end)
start = None
return u'[' + u''.join(result) + u']'

Seems rather cumbersome.

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


import relative (with a directory)

2009-01-10 Thread rocky
Import relative?

Recently for fun I've been working on a large Python program. It has
many files/modules spread over several directories/submodules.

Each module has some "demo" code at the end that I can use to run or
experiment with that module. Of course, modules often refer to others;
depending on which one, more of the code may get pulled in or not.

In this situation it has been very convenient to have do "import
relative" which is like import, but I get to specify where to start
searching from. Of course I can set sys.path, but I find that hokey.
(When I write systems administration scripts, I also generally prefer
specifying a full path name to a program, e.g. /bin/mount rather than
using PATH. Strikes me as the same issue here).

The other thing about the Python import mechanism that sometimes gets
in the way is that if there is a module imported (from the wrong place
for this context), it tacitly gets used. Within such a project I want
to specify when I want the code in this project, and if it can't find
that, then that's a failure.

I'm curious if other have the same problem and how other they deal
with this? Reading "Expert Python Programming", I see that one
solution is to install into a sandbox. But it strikes me as a little
less agile. Just as I don't enjoy issuing compile and link commands
(even if run via a Makefile), I don't want to have to issue install-
into-sandbox commands.

For concreteness here's the import code I've been using is:
http://code.google.com/p/pyimport-relative/

It is far from perfect because I just wanted to get this done and I
don't fully understand the Python modules and imp module, but for now
it gets what I want done. I will probably add some ability to top-
level package name to avoid collisions from other packages. However if
I do this, I still will probably have the program figure out the
intermediate compound names. So if I am in directory ...my-project/a/b/
c and I can say import_relative("x", "myproject") and have it import
that as "myproject.a.b.c.x". That way, if files move around and get
renamed I don't have to change any code. Also, I can probably store
somewhere in a variable "myproject".

Code that uses this can be found at http://code.google.com/p/pydbg/

Suggestions on how to improve the code are welcome; but again I don't
represent that this is done that well or that I've mastered this.

Oh, also meant to mention that in looking at say the knee code (which
looks like a translation into Python of its C code) and the C code, a
very deliberate decision was made *not* to allow filenames. So I
probably am treading where others have been and have decided not to
go. If folks are aware of threads, and discussions on this I'd be
grateful for pointers on this discussion.

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


Re: import relative (with a directory)

2009-01-10 Thread Chris Rebert
On Sat, Jan 10, 2009 at 6:11 PM, rocky  wrote:
> Import relative?
>
> Recently for fun I've been working on a large Python program. It has
> many files/modules spread over several directories/submodules.
>
> Each module has some "demo" code at the end that I can use to run or
> experiment with that module. Of course, modules often refer to others;
> depending on which one, more of the code may get pulled in or not.
>
> In this situation it has been very convenient to have do "import
> relative" which is like import, but I get to specify where to start
> searching from. Of course I can set sys.path, but I find that hokey.
> (When I write systems administration scripts, I also generally prefer
> specifying a full path name to a program, e.g. /bin/mount rather than
> using PATH. Strikes me as the same issue here).
>
> The other thing about the Python import mechanism that sometimes gets
> in the way is that if there is a module imported (from the wrong place
> for this context), it tacitly gets used. Within such a project I want
> to specify when I want the code in this project, and if it can't find
> that, then that's a failure.
>
> I'm curious if other have the same problem and how other they deal
> with this? Reading "Expert Python Programming", I see that one
> solution is to install into a sandbox. But it strikes me as a little
> less agile. Just as I don't enjoy issuing compile and link commands
> (even if run via a Makefile), I don't want to have to issue install-
> into-sandbox commands.
>
> For concreteness here's the import code I've been using is:
>http://code.google.com/p/pyimport-relative/
>
> It is far from perfect because I just wanted to get this done and I
> don't fully understand the Python modules and imp module, but for now
> it gets what I want done. I will probably add some ability to top-
> level package name to avoid collisions from other packages. However if
> I do this, I still will probably have the program figure out the
> intermediate compound names. So if I am in directory ...my-project/a/b/
> c and I can say import_relative("x", "myproject") and have it import
> that as "myproject.a.b.c.x". That way, if files move around and get
> renamed I don't have to change any code. Also, I can probably store
> somewhere in a variable "myproject".

You should probably check out the relative import syntax introduced in
PEP 328: http://www.python.org/dev/peps/pep-0328/
It should be able to do exactly what you want.

Cheers,
Chris

-- 
Follow the path of the Iguana...
http://rebertia.com
--
http://mail.python.org/mailman/listinfo/python-list


Re: Regex for unicode letter characters

2009-01-10 Thread MRAB

schickb wrote:

I need a regex that will match strings containing only unicode letter
characters (not including numeric or the _ character). I was surprised
to find the 're' module does not include a special character class for
this already (python 2.6). Or did I miss something?

It seems like this would be a very common need. Is the following the
only option to generate the character class (based on an old post by
Martin v. Löwis )?


[snip]
Basically, yes.

The re module was last worked on in 2003 (remember it's all voluntary!). 
Such omissions should be addressed in Python 2.7.

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


Re: Python 2.6.1 @executable_path

2009-01-10 Thread googler . 1 . webmaster
hmm.. very strange. Is it so complicated to compile python that I can
move the framework to the app folder?
hmm. thats really strange :-(


/myapp.app
/subfolder/Python.framework

any suggestions? Thank you very muc.
--
http://mail.python.org/mailman/listinfo/python-list


Re: why cannot assign to function call

2009-01-10 Thread Mark Wooding
Steven D'Aprano  wrote:

> There are actually three fundamental characteristics of pass-by-reference:
> 
> * passing a value by reference does not lead to the value being copied, 
> in contrast with pass-by-value where it does;
> 
> * modifications to the value passed by reference are visible to the 
> caller;
> 
> * assignments to the value passed by reference are visible to the
>   caller.

I've given an extensive definition of pass-by-reference.  Distilling and
paraphrasing, the main characteristic is that (where possible) new
variables are not created: rather, the parameter names are bound to the
caller's variables.  All three of your above characteristics are
consequences of this one.

But your definition is also flawed: it doesn't distinguish pass-by-
reference from pass-by-value/return (where the caller's variable is
updated from the function parameter's final value when the function
returns).  The difference is detectable if you use global variables,
however:

  variable g = 1

  function foo(x):
x = 2
print g

  foo(g)
  print g

prints 1 and 1 if you use pass-by-value, 2 and 2 if you use pass-by-
reference, and 1 and 2 if you use pass-by-value/result.

(Within the framework I presented elsewhere, pass-by-value/result is
like pass-by-value, except that we update:

  s = s'''[s'''(e''(n))/l]

where s''' is the store just prior to the return, s is the store
just after the return, n is the parameter name, e'' is the function's
environment, and l is the location designated by the argument
expression, i.e., (l, s') = loc(x, e, s).  If there is no such location,
then the language may either fail to compile the call, or omit the
update operation.)

> Pascal VAR parameters have all three characteristics.

That's because they work as I've suggested.

> Pascal non-VAR parameters have none of them.

Indeed.

> Python parameters have two of the three. C parameters (call-by-value)
> have none of them, except for arrays, where they have all three,
> making arrays in C behave just like Pascal pass-by- reference VAR
> parameters.

Rubbish.  In C:

  void foo(char v[42]) { v = 0; }

Calling this function has no effect on the caller whatsoever.  I've
already explained C arrays, with reference to the ISO standard,
exhaustively: there should be no excuse for continued misunderstanding
of this point.

-- [mdw]
--
http://mail.python.org/mailman/listinfo/python-list


Re: why cannot assign to function call

2009-01-10 Thread Steven D'Aprano
On Sun, 11 Jan 2009 01:22:48 +, Rhodri James wrote:

> On Sat, 10 Jan 2009 18:44:37 -,  wrote:
> 
>> What is the observable difference between converting an array to a
>> reference (pointer) to that array and passing the reference by value,
>> and passing the array by reference?
> 
> This is a red herring, though.  From either viewpoint, C arrays are
> anomalous in comparison with other C data types.

I don't believe it is a red-herring. As I understand it, Mark and Joe 
insist that C is pass-by-value *even in the case of arrays*, despite the 
semantics of array passing being identical to the semantics of pass-by-
reference in (say) Pascal. While Mark is willing to admit that arrays are 
"bizarre" (his term) in C, I don't think he accepts that passing arrays 
in C is anything but pass-by-value.

I think this gets very close to the bone of the debate. It demonstrates 
that "pass-by-value" as Mark and Joe understand it is such a broad 
concept that it can describe any argument passing behaviour at all and 
therefore is meaningless.


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


Re: urlopen exception

2009-01-10 Thread Steve Holden
Paul Rubin wrote:
> asit  writes:
>> site="www.bput.org"
>> payloads="alert('xss')"
>> attack= urllib2.urlopen(site+payloads,80).readlines()
>>
>> according to my best knowledge, the above code is correct.
>> but why it throws exceptio 
> 
> The code is incorrect.  Look at the string ou are sending into
> urlopen.  What on earth are you trying to do?

He's investigating potential cross-site scripting vulnerabilities.

regards
 Steve
-- 
Steve Holden+1 571 484 6266   +1 800 494 3119
Holden Web LLC  http://www.holdenweb.com/

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


Re: Regex for unicode letter characters

2009-01-10 Thread Steve Holden
MRAB wrote:
> schickb wrote:
>> I need a regex that will match strings containing only unicode letter
>> characters (not including numeric or the _ character). I was surprised
>> to find the 're' module does not include a special character class for
>> this already (python 2.6). Or did I miss something?
>>
>> It seems like this would be a very common need. Is the following the
>> only option to generate the character class (based on an old post by
>> Martin v. Löwis )?
>>
> [snip]
> Basically, yes.
> 
> The re module was last worked on in 2003 (remember it's all voluntary!).
> Such omissions should be addressed in Python 2.7.

By "should be" do you mean "ought to be (but I have no intention of
helping)", "are expected to be (but someone else will be doing the
work", "it's on my list and I am expecting to get finished in time for
2.7 integration" or something else?

regards
 Steve
-- 
Steve Holden+1 571 484 6266   +1 800 494 3119
Holden Web LLC  http://www.holdenweb.com/

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


Re: why cannot assign to function call

2009-01-10 Thread Mark Wooding
Steven D'Aprano  wrote:

> I don't believe it is a red-herring. As I understand it, Mark and Joe 
> insist that C is pass-by-value *even in the case of arrays*, despite the 
> semantics of array passing being identical to the semantics of pass-by-
> reference in (say) Pascal. 

But they aren't.  I've provided several examples to show this.  Most
tellingly, the parameter types are different in the two cases.

> While Mark is willing to admit that arrays are "bizarre" (his term) in
> C, I don't think he accepts that passing arrays in C is anything but
> pass-by-value.

For the purpose of clearing this up once and for all: arrays, in C, are
`don't-pass-at-all'.  There is no way -- none whatever -- of declaring a
function parameter as having array type.

Furthermore, if (say) an argument expression consists only of an
identifier bound to an object of array type, the resulting argument
expression has pointer type, and is fully evaluated prior to the
sequence point before the function call.  I provided chapter and verse
elsewhere, and I'm getting rather fed up of repeating myself.  Certainly
a dissenting opinion should include references to at least a (specific)
draft of the ISO C standard.

> I think this gets very close to the bone of the debate. It demonstrates 
> that "pass-by-value" as Mark and Joe understand it is such a broad 
> concept that it can describe any argument passing behaviour at all and 
> therefore is meaningless.

I've proven (not quite rigorously, but sketched a proof, and provided
definitions which are now sufficiently formal that you can fill in the
details) that my definition of pass-by-value excludes certain
behaviours.  But, most significantly, you cannot implement `swap' given
only pass-by-value.

-- [mdw], wondering whether Hanlon's razor is getting blunt.
--
http://mail.python.org/mailman/listinfo/python-list


Re: State of the art: Tkinter, Tk 8.5, Tix?

2009-01-10 Thread Дамјан Георгиевски
> The themed Tk widgets (ttk) that come with Tk 8.5 add a lot of the
> same things that Tix does, but they do so in a more modern way,
> hooking into platform-specific themes and API's wherever possible (XP,
> Vista, Mac) and updating the generic X11 look as well. As such, they
> are more appropriate for modern development. Tix is more of a legacy
> toolkit.

Interesting... so to summarize, what do I get from Python/TK on *Linux* 
with tkinter beeing dynamically linked to the system tk 8.5.6 ?

Especially I'd like to know if it will support fontconfig/TTF/antialiased fonts?


-- 
дамјан ( http://softver.org.mk/damjan/ )

When you do things right, people won't be sure if you did anything at all.
--
http://mail.python.org/mailman/listinfo/python-list


Re: Regex for unicode letter characters

2009-01-10 Thread MRAB

Steve Holden wrote:

MRAB wrote:

schickb wrote:

I need a regex that will match strings containing only unicode letter
characters (not including numeric or the _ character). I was surprised
to find the 're' module does not include a special character class for
this already (python 2.6). Or did I miss something?

It seems like this would be a very common need. Is the following the
only option to generate the character class (based on an old post by
Martin v. Löwis )?


[snip]
Basically, yes.

The re module was last worked on in 2003 (remember it's all voluntary!).
Such omissions should be addressed in Python 2.7.


By "should be" do you mean "ought to be (but I have no intention of
helping)", "are expected to be (but someone else will be doing the
work", "it's on my list and I am expecting to get finished in time for
2.7 integration" or something else?


The third one.
--
http://mail.python.org/mailman/listinfo/python-list


Re: why cannot assign to function call

2009-01-10 Thread rurpy
Mark Wooding wrote:
> ru...@yahoo.com  wrote:
>
>> What is the observable difference between converting an
>> array to a reference (pointer) to that array and passing
>> the reference by value, and passing the array by reference?
>
> For one:
>
> #include 
> static size_t foo(char v[]) { return sizeof v; }
> int main(void) {
>   char v[sizeof(char *) * 10];
>   puts(foo(v) == sizeof(char *) ? "pointer" : "reference");
>   return (0);
> }

You are saying that because the size of the argument
(10) is not available in the function, it cannot be
call-by-reference?

I think fortran is accepted as the archetypal call-by-
reference language and it does not automatically
supply argument size information to functions.  In
fortran, if the size of the argument is known at compile
time, the programmer explicitly declares the parameter
with the same size in the function.

  integer k(3)
  call mysub (k)
  write (unit=*, fmt=*) k(1),k(2),k(3)
  end

  subroutine mysub (x)
  integer x(3)
  do 100, i=1,3
  100   x(i) = i
  return
  end

If not, he passes the size explicitly as an argument:

  integer k(3)
  call mysub (k, 3)
  write (unit=*, fmt=*) k(1),k(2),k(3)
  end

  subroutine mysub(x, n)
  integer x(0)
  do 100, i=1,3
  100   x(i) = i
  return
  end

Obviously both these idioms translate directly into
C.  Here is the second:

 #include 
 void mysub (int k[], int n) {
int i;
for (i=0; i<3; i++) {
k[i] = i; }
return; }

  main() {
int k[3];
mysub (k, 3);
printf ("%i,%i,%i\n", k[0],k[1],k[2]); }

So if fortran can be call-by-reference without the
compiler passing size information, I don't see why
the above C code can't be as well.

> For another:
>
> static void bar(char v[]) { char ch = 0; v = &ch; }
>   /* type error if arrays truly passed by reference */

v can be used as an array reference, e.g. "v[1] = 23"
exactly as in the pass-by-reference fortran example.  And
v can also be used as a local variable and reassigned.
If the first option was the only one, would you not
say C was definitely pass-by-reference (for arrays)?
If one could only use v as a pointer (i.e. access
the argument array as "*(v+1) = 23", then I would
say that arrays are not passed at all, only pointers
by-value.
That both these options exist causes me to conclude
that, for arrays, parameter passing can be viewed
as either arrays by-reference or pointers by-value.

I don't understand what relevance type checking
has.  Since you are choosing to use v as a pointer,
one would not expect a type error, yes?

>> I guess the case for pass-by-value would be a little stronger because
>> one has to have "passing a pointer by value" anyway (since pointers
>> are first-class datatypes) and that can be used to describe passing
>> arrays (as you described).
>
> The difference is that the /callee/ function is different between the
> two cases.
>
> Also, notice that arrays in expressions turn into pointers in the same
> way, so function argument passing works the same way as assignment -- a
> hallmark of pass-by-value.

Not in all expressions as you yourself mentioned:
 int a[10], *ap;
 sizeof a;
 sizeof ap;
--
http://mail.python.org/mailman/listinfo/python-list


Re: "python -3" not working as expected

2009-01-10 Thread Benjamin
On Jan 9, 10:19 pm, John Machin  wrote:
> On Jan 10, 2:55 pm, Benjamin  wrote:
>
> > We'll need good documentation. Unfortunately, as you
> > note below, this isn't exactly the case yet.
>
> So is there a plot to remedy this? Where do we sign up?

Feel free to contribute to the wiki page: 
http://wiki.python.org/moin/PortingToPy3k

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


Python strings and coding conventions

2009-01-10 Thread koranthala
Hi,
   Python Coding Convention (PEP 8) suggests :
  Maximum Line Length

Limit all lines to a maximum of 79 characters.

  I have a string which is ~110 char long. It is a string which I am
going to print in a text file as a single string.
  i.e. in that text file, each line is taken as a different record -
so it has to be in a single line.

  Now, how can I write this code - while following PEP 8?
  I tried blockstrings, but as shown in the example below:
>>> s = r'''
... abcd
... efgh
... '''
>>> s
'\nabcd\nefgh\n'
   it has "\n" inserted - which will disallow printing it to a single
line.

   I thought about other options like concatenating strings etc, but
it seems very kludgy - esp since the whole string has a single meaning
and cannot be easily split to many logically. Then I thought of
creating a blockstring and then removing "\n", but it seemed
kludgier...

   I am sure this is a very usual issue and I am missing some very
very simple solution. But I cannot think of it at all...
--
http://mail.python.org/mailman/listinfo/python-list


Re: Python strings and coding conventions

2009-01-10 Thread Robert Kern

koranth...@gmail.com wrote:

Hi,
   Python Coding Convention (PEP 8) suggests :
  Maximum Line Length

Limit all lines to a maximum of 79 characters.

  I have a string which is ~110 char long. It is a string which I am
going to print in a text file as a single string.
  i.e. in that text file, each line is taken as a different record -
so it has to be in a single line.

  Now, how can I write this code - while following PEP 8?
  I tried blockstrings, but as shown in the example below:

s = r'''

... abcd
... efgh
... '''

s

'\nabcd\nefgh\n'
   it has "\n" inserted - which will disallow printing it to a single
line.

   I thought about other options like concatenating strings etc, but
it seems very kludgy - esp since the whole string has a single meaning
and cannot be easily split to many logically. Then I thought of
creating a blockstring and then removing "\n", but it seemed
kludgier...


I usually use implicit concatenation:

s = ('some long text that '
 'needs to be split')

--
Robert Kern

"I have come to believe that the whole world is an enigma, a harmless enigma
 that is made terrible by our own mad attempt to interpret it as though it had
 an underlying truth."
  -- Umberto Eco

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


Re: Python strings and coding conventions

2009-01-10 Thread Mensanator
On Jan 10, 10:26�pm, Robert Kern  wrote:
> koranth...@gmail.com wrote:
> > Hi,
> > � �Python Coding Convention (PEP 8) suggests :
> > � Maximum Line Length
>
> > � � Limit all lines to a maximum of 79 characters.
>
> > � I have a string which is ~110 char long. It is a string which I am
> > going to print in a text file as a single string.
> > � i.e. in that text file, each line is taken as a different record -
> > so it has to be in a single line.
>
> > � Now, how can I write this code - while following PEP 8?
> > � I tried blockstrings, but as shown in the example below:
>  s = r'''
> > ... abcd
> > ... efgh
> > ... '''
>  s
> > '\nabcd\nefgh\n'
> > � �it has "\n" inserted - which will disallow printing it to a single
> > line.
>
> > � �I thought about other options like concatenating strings etc, but
> > it seems very kludgy - esp since the whole string has a single meaning
> > and cannot be easily split to many logically. Then I thought of
> > creating a blockstring and then removing "\n", but it seemed
> > kludgier...
>
> I usually use implicit concatenation:
>
> s = ('some long text that '
> � � � 'needs to be split')

Damn! I didn't know you could do that! And if I
saw it in a program listing, such would never occur
to me. I was going to suggest the stupid way:

>>> ga = ['four score and seven years ago ', \
  'our fathers brought forth ', \
  'on this continent a new nation ', \
  'conceived in liberty and dedicated ', \
  'to the proposition that all men ', \
  'are created equal']
>>> ''.join(ga)
'four score and seven years ago our fathers brought forth on this
continent a new nation conceived in liberty and dedicated to the
proposition that all men are created equal'


>
> --
> Robert Kern
>
> "I have come to believe that the whole world is an enigma, a harmless enigma
> � that is made terrible by our own mad attempt to interpret it as though it 
> had
> � an underlying truth."
> � �-- Umberto Eco- Hide quoted text -
>
> - Show quoted text -

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


Re: import relative (with a directory)

2009-01-10 Thread Kay Schluehr
On 11 Jan., 03:27, Chris Rebert  wrote:

> You should probably check out the relative import syntax introduced in
> PEP 328:http://www.python.org/dev/peps/pep-0328/
> It should be able to do exactly what you want.

This should exactly lead to exceptions in all of his demo code because
the modules are not "scripts" anymore but "modules in a package" - a
semantical difference I wasn't even aware of until relative imports
were introduced in Python 2.5.

I'd rather avoid the sour grapes and use absolute imports.

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


Re: Python 2.6.1 @executable_path

2009-01-10 Thread Michael Torrie
googler.1.webmas...@spamgourmet.com wrote:
> Thanks for the link but I don't want to do a make a python script as
> an applicatin, I want to embedd python into a C++ app so thats the
> reason why I have to compile Python.

If you are embedding python, then all you have to do is stick the python
 modules you need in somewhere in the resources folder, along with the
dynamic link library that you built.  Then you fix up your executable to
be able to find the dynamic link library (been a while since I made app
bundles in general... followed the Qt docs if I recall).  When your app
goes to initialize python, just use python api calls to set the python
path that it will use to find the import modules.  You should be able to
programmatically determine this (see apple docs I guess).

In short, you can do it all with the python C api, programmatically in
your exe, once you've taken care of the initial link against the python
dynlib.

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


Re: urlopen exception

2009-01-10 Thread alex goretoy
I would try:

site="http://www.bput.org/";
payloads="alert('xss')"
attack= urllib2.urlopen(site+payloads,80).readlines()


-Alex Goretoy
http://www.alexgoretoy.com
somebodywhoca...@gmail.com


On Sun, Jan 11, 2009 at 2:49 AM, Steve Holden  wrote:

> Paul Rubin wrote:
> > asit  writes:
> >> site="www.bput.org"
> >> payloads="alert('xss')"
> >> attack= urllib2.urlopen(site+payloads,80).readlines()
> >>
> >> according to my best knowledge, the above code is correct.
> >> but why it throws exceptio 
> >
> > The code is incorrect.  Look at the string ou are sending into
> > urlopen.  What on earth are you trying to do?
>
> He's investigating potential cross-site scripting vulnerabilities.
>
> regards
>  Steve
> --
> Steve Holden+1 571 484 6266   +1 800 494 3119
> Holden Web LLC  http://www.holdenweb.com/
>
> --
> http://mail.python.org/mailman/listinfo/python-list
>
--
http://mail.python.org/mailman/listinfo/python-list


Re: urlopen exception

2009-01-10 Thread alex goretoy
 oops, remove the ,80 since port is not needed. Well, in my case it wasn't
working with port. notice it gives me 404, but this with my domain

>>> att=urllib2.urlopen(site+payload,80).readlines()
Traceback (most recent call last):
  File "", line 1, in 
  File "/usr/local/lib/python2.6/urllib2.py", line 124, in urlopen
return _opener.open(url, data, timeout)
  File "/usr/local/lib/python2.6/urllib2.py", line 381, in open
req = meth(req)
  File "/usr/local/lib/python2.6/urllib2.py", line 1057, in do_request_
'Content-length', '%d' % len(data))
TypeError: object of type 'int' has no len()

>>> att=urllib2.urlopen(site+payload).readlines()
Traceback (most recent call last):
  File "", line 1, in 
  File "/usr/local/lib/python2.6/urllib2.py", line 124, in urlopen
return _opener.open(url, data, timeout)
  File "/usr/local/lib/python2.6/urllib2.py", line 389, in open
response = meth(req, response)
  File "/usr/local/lib/python2.6/urllib2.py", line 502, in http_response
'http', request, response, code, msg, hdrs)
  File "/usr/local/lib/python2.6/urllib2.py", line 427, in error
return self._call_chain(*args)
  File "/usr/local/lib/python2.6/urllib2.py", line 361, in _call_chain
result = func(*args)
  File "/usr/local/lib/python2.6/urllib2.py", line 510, in
http_error_default
raise HTTPError(req.get_full_url(), code, msg, hdrs, fp)
urllib2.HTTPError: HTTP Error 404: Not Found

-Alex Goretoy
http://www.alexgoretoy.com
somebodywhoca...@gmail.com


On Sun, Jan 11, 2009 at 5:58 AM, alex goretoy
wrote:

> I would try:
>
> site="http://www.bput.org/";
> payloads="alert('xss')"
> attack= urllib2.urlopen(site+payloads,80).readlines()
>
>
> -Alex Goretoy
> http://www.alexgoretoy.com
> somebodywhoca...@gmail.com
>
>
>
> On Sun, Jan 11, 2009 at 2:49 AM, Steve Holden  wrote:
>
>> Paul Rubin wrote:
>> > asit  writes:
>> >> site="www.bput.org"
>> >> payloads="alert('xss')"
>> >> attack= urllib2.urlopen(site+payloads,80).readlines()
>> >>
>> >> according to my best knowledge, the above code is correct.
>> >> but why it throws exceptio 
>> >
>> > The code is incorrect.  Look at the string ou are sending into
>> > urlopen.  What on earth are you trying to do?
>>
>> He's investigating potential cross-site scripting vulnerabilities.
>>
>> regards
>>  Steve
>> --
>> Steve Holden+1 571 484 6266   +1 800 494 3119
>> Holden Web LLC  http://www.holdenweb.com/
>>
>> --
>> http://mail.python.org/mailman/listinfo/python-list
>>
>
>
--
http://mail.python.org/mailman/listinfo/python-list


parsing javascript generated files...

2009-01-10 Thread bruce
Hi.

Looking to parse some web pages that have javascript (jquery) embedded/used
in the pages. I'm trying to get a better understanding of exactly how the
page is generated, and displayed in the browser.

I've seen various references to python-spidermonkey, as well as
watir/firewatir. Is there a way to accomplish fetching text from javascript
generated pages?

It appears that the ability to "call" firefox using "jssh" could allow me to
return the complete page of the displayed app. I'm not sure if this is
pythonic!!

I suspect that I would have to somehow invoke the page, using firefox/jssh,
(or spidermonkey) or some other javascript engine, and then somehow invoke
the javascript function, that would fill in the 'div' within the page...

Is this even doable???

It would be great if there was someway of calling an external browser/app
that one could pass the targeted url, and get back the resulting html that's
displayed by the browser!!

A target site is http:://web-app.usc.edu/soc/term_20091.html where the
'dept' list is completely generated by javascript functions...

When i researched this awhile ago, there didn't appear to be a really good
solution to this situation. I'm curious if someone knows of a solution to
this issue that's now available and that works!

Thanks for any thoughts/comments in this issue...

-bruce

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


Re: Python strings and coding conventions

2009-01-10 Thread koranthala
On Jan 11, 9:26 am, Robert Kern  wrote:
> koranth...@gmail.com wrote:
> > Hi,
> >    Python Coding Convention (PEP 8) suggests :
> >   Maximum Line Length
>
> >     Limit all lines to a maximum of 79 characters.
>
> >   I have a string which is ~110 char long. It is a string which I am
> > going to print in a text file as a single string.
> >   i.e. in that text file, each line is taken as a different record -
> > so it has to be in a single line.
>
> >   Now, how can I write this code - while following PEP 8?
> >   I tried blockstrings, but as shown in the example below:
>  s = r'''
> > ... abcd
> > ... efgh
> > ... '''
>  s
> > '\nabcd\nefgh\n'
> >    it has "\n" inserted - which will disallow printing it to a single
> > line.
>
> >    I thought about other options like concatenating strings etc, but
> > it seems very kludgy - esp since the whole string has a single meaning
> > and cannot be easily split to many logically. Then I thought of
> > creating a blockstring and then removing "\n", but it seemed
> > kludgier...
>
> I usually use implicit concatenation:
>
> s = ('some long text that '
>       'needs to be split')
>
> --
> Robert Kern
>
> "I have come to believe that the whole world is an enigma, a harmless enigma
>   that is made terrible by our own mad attempt to interpret it as though it 
> had
>   an underlying truth."
>    -- Umberto Eco

This is a very good method.
I found another method too - on further investigation
>>> s = "abc\
... efg"
>>> s
'abcefg'
Only problem being that it doesnt support indentation.
So, implicit concatenation it is...
--
http://mail.python.org/mailman/listinfo/python-list