Re: Class-level variables - a scoping issue

2010-10-09 Thread Steven D'Aprano
On Sat, 09 Oct 2010 22:30:43 -0700, John Nagle wrote:

> Here's an obscure bit of Python semantics which is close to being a bug:

"Obscure"? It's possibly the most fundamental aspect of Python's object 
model. Setting instance.attr assigns to the instance attribute, creating 
it if it doesn't exist. Getting instance.attr retrieves the instance 
attribute, or the class attribute, or a superclass attribute, whichever 
is found first.

As for it being a bug, or close to being a bug, can you tell me which 
specified behaviour it fails to match?


[...]
>  Notice what happened here.  Within "fn1", the first
> reference to "self.classvar" references the class-level version of
> "classvar".  The assignment overrides that and creates an object-level
> instance of "self.classvar". Further references to "self.classvar" in f1
> then reference the object-level "classvar"

I'm sorry, I don't quite understand what you mean by "object-level". 
They're *all* objects. The ints 1 and 2 are objects. The instances t1 and 
t2 are objects. The class t is an object. The global namespace is an 
object. Built-ins are objects. Which of these plethora of objects do you 
mean by "object-level"?

I'm going to take a guess that you probably mean to distinguish between 
class attributes and instance attributes. From the context, that seems 
likely. Am I right?



>  Creating another instance of t makes it clear that the
> class-level variable never changes.   To change it, it has to be
> referenced as "t.classvar".

Yes.


>  Python protects global variables from similar confusion
> by making them read-only when referenced from an inner scope without a
> "global" statement.  But that protection isn't applied to class-level
> variables referenced through 'self'. Perhaps it should be.

The protection to class attributes is applied. When writing to self.attr, 
you can't accidentally change a class attribute ("global to the class"). 
To change it for all instances of the class, you have to specifically 
assign to class.attr instead.

I maintain that the most common use for class attributes (other than 
methods, of course) is for default values which are over-ridden at the 
instance level:

class Page(object):
unit = 'cm'
width = 20.9
height = 29.7
direction = 'portrait'

p1 = Page()
p2 = Page()
p2.direction = 'landscape'


it would be disastrous to have every Page instance change to landscape 
just because you changed one, and it would be inconvenient to need 
special syntax to allow writing to the instance attribute just because a 
class attribute exists. The existing behaviour is, in my opinion, ideal.



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


Re: if the else short form

2010-10-09 Thread saeed.gnu
>>> True == 1
True
>>> False == 0
True
>>> int(True)
1
>>> int(False)
0
>>> bool(1)
True
>>> bool(0)
False


‌But:
str(fill==True)+','
is simpler than:
("False,", "True,")[fill==True]
-- 
http://mail.python.org/mailman/listinfo/python-list


Excuse me!! Would you stop for a moment?

2010-10-09 Thread ثلوج الشتاء
Excuse me!!
Would you stop for a moment?!
Haven't you thought-one day- about yourself ?
Who has made it?
Have you seen a design which hasn't a designer ?!
Have you seen a wonderful,delicate work without a worker ?!
It's you and the whole universe!..
Who has made them all ?!!
You know who ?.. It's "ALLAH",prise be to him.
Just think for a moment.
How are you going to be after death ?!
Can you believe that this exact system of the universe and all of
these great creation will end in nothing...just after death!
Have you thought, for a second, How to save your soul from Allah's
punishment?!
Haven't you thought about what is the right religion?!
Here you will get the answer
http://www.islam-guide.com
http://www.sultan.org
-- 
http://mail.python.org/mailman/listinfo/python-list


Class-level variables - a scoping issue

2010-10-09 Thread John Nagle

   Here's an obscure bit of Python semantics which
is close to being a bug:

>>> class t(object) :
... classvar = 1
...
... def fn1(self) :
... print("fn1: classvar = %d" % (self.classvar,))
... self.classvar = 2
... print("fn1: classvar = %d" % (self.classvar,))
...
...
>>> t1 = t()
>>> t2 = t()
>>> t1.fn1()
fn1: classvar = 1
fn1: classvar = 2

>>> t1.fn1()
fn1: classvar = 2
fn1: classvar = 2

>>> t2.fn1()
fn1: classvar = 1
fn1: classvar = 2

Notice what happened here.  Within "fn1", the first
reference to "self.classvar" references the class-level
version of "classvar".  The assignment overrides that
and creates an object-level instance of "self.classvar".
Further references to "self.classvar" in f1 then reference the
object-level "classvar"

Creating another instance of t makes it clear that the
class-level variable never changes.   To change it, it
has to be referenced as "t.classvar".

Python protects global variables from similar confusion
by making them read-only when referenced from an inner scope
without a "global" statement.  But that protection isn't
applied to class-level variables referenced through 'self'.
Perhaps it should be.   

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


Re: reference vs. name space question

2010-10-09 Thread Ben Finney
chad  writes:

> Maybe I'm being a bit dense, but how something like
>
> [cdal...@localhost oakland]$ python
> Python 2.6.2 (r262:71600, May  3 2009, 17:04:44)
> [GCC 4.1.1 20061011 (Red Hat 4.1.1-30)] on linux2
> Type "help", "copyright", "credits" or "license" for more information.
> >>> spam
> Traceback (most recent call last):
>   File "", line 1, in 
> NameError: name 'spam' is not defined
> >>>
>
> Generate an error

Because code that uses ‘spam’ as a reference was executed.

> but something like
> >>> def foo(x, y):
> ...   pass
> ...
> >>>
>
> Doesn't?

Because no names were used as references.

> I mean, in the first case, 'spam' isn't bound to anything.

Right, and yet you asked the Python interpreter to resolve it as a
reference. Hence the NameError.

> Likewise, in the second case, both 'x' and 'y' aren't bound to
> anything.

And nothing has yet asked the Python interpreter to resolve them as
references. Instead, you defined a function object and bound that object
to the name ‘foo’.

> I don't see why the interpreter doesn't complain about 'x' and 'y' not
> being defined.

I'd advise that you need to *do* (not just read, but actually perform)
the whole Python tutorial from start to finish, to get a good grounding
in concepts in a sensible order http://docs.python.org/tutorial/>.

-- 
 \   Moriarty: “Forty thousand million billion dollars? That money |
  `\must be worth a fortune!” —The Goon Show, _The Sale of |
_o__)   Manhattan_ |
Ben Finney
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Eclipse/PyDev - BOM Lexical Error

2010-10-09 Thread Lawrence D'Oliveiro
In message , Ethan 
Furman wrote:

> Lawrence D'Oliveiro wrote:
>
>> But they can only recognize it as a BOM if they assume UTF-8 encoding to
>> begin with. Otherwise it could be interpreted as some other coding.
> 
> Not so.  The first three bytes are the flag.

But this is just a text file. All parts of its contents are text, there is 
no “flag”.

If you think otherwise, then tell us what are these three “flag” bytes for a 
Windows-1252-encoded text file?
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: open file on mac

2010-10-09 Thread Lawrence D'Oliveiro
In message
, 
tinauser wrote:

> now,the file will be opened only if i give the full path, not if i
> give only the name of the file, although the folder is in the path.
> what am I missing?

The fact that sys.path is not used for that.
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: how to handle network failures

2010-10-09 Thread Lawrence D'Oliveiro
In message , Diez B. Roggisch wrote:

> for n in xrange(max_number_of_retries):
> try:
> f=urllib.urlopen(self.url)
> data = f.read()
> break # exist the loop if all
> except IOError:
> pass

Is it worth delaying before retrying? In case of a transient routing error, 
that kind of thing.
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: script in Linux vs Windows

2010-10-09 Thread Lawrence D'Oliveiro
In message , Dennis 
Lee Bieber wrote:

> On Windows, the I/O system for text files converts  into a
>  on input, and on output converts  to .

Is it Windows doing that, or is it some Visual Studio library?
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Light on Dark screen for Spe or Pyscripter

2010-10-09 Thread flebber
On Oct 9, 3:54 pm, flebber  wrote:
> I was hoping someone knew how to setup pyscripter or Spe ide's with
> light on dark windows. I start to have trouble reading the nomal dark
> on light screens after any lengthy period.
>
> I have seen several screenshot showing emacs with python setup with
> awesome light on dark terminals but seems you need a degree in itself
> to get emacs and python working on windows with full features.

Pyscripter does have a selectable theme option in its options menu.
However seems to only relate to syntax colours and highlighting,
doesn't seem to allow changing theme to light on dark.

Anyhow still a good ide.
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Re: Re: Re: How to save a binary file?

2010-10-09 Thread hidura
Finally i had problems to save the files what are encoded i can't encode  
the string to save the file, any ideas?


On Oct 6, 2010 3:15pm, hid...@gmail.com wrote:
Ppl thanyou, for all your help finally i did it! thanks, another  
thing to who i can send a propose code, i fixed the little problem of the  
wsig.input in Python 3 i will tested in the next months but i want to  
share the code with the community, how i can do that?



On Oct 6, 2010 3:13pm, hid...@gmail.com wrote:
> Ppl thanyou, for all your help finally i did it! thanks, another  
thing to i have to send a propouse code, i can fixed the litle problem of  
the wsig.input in Python 3 i will tested in the next months but i want to  
share the code with the comunnity, how i can do that?

>
> On Oct 6, 2010 1:45pm, MRAB pyt...@mrabarnett.plus.com> wrote:
> > On 06/10/2010 15:25, hid...@gmail.com wrote:
> >
> >
> > When you put the 'wb' extension you have to pass a Encode the string
> >
> > Python does not accept a string on a wb file, Python3
> >
> >
> >
> >
> > [snip]
> >
> > You are using Python 3 and type(str) returns ""?
> >
> >
> >
> > Binary data in Python 3 should be an instance of the 'bytes' class,  
not

> >
> > an instance of the 'str' class.
> >
> >
> >
> > If you can't fix that, you could turn the string into bytes using:
> >
> >
> >
> > data = bytes(ord(c) for c in str)
> >
> >
> >
> > or by carefully choosing an encoding which would give the same result:
> >
> >
> >
> > data = str.encode('latin-1')
> >
> >
> >
> > Then you can save it:
> >
> >
> >
> > s = open('/home/hidura/test.jpeg', 'wb')
> >
> > s.write(data)
> >
> > s.close()
> >
> >
> >
> > I asked you to look at the result of repr so that you could see more
> >
> > clearly what the data actually looked like, an instance of str or an
> >
> > instance of bytes.
> >
> > --
> >
> > http://mail.python.org/mailman/listinfo/python-list
> >
-- 
http://mail.python.org/mailman/listinfo/python-list


Removing unneccessary files from Windows Tkinter tcl folder

2010-10-09 Thread python
We are using Python 2.7 for Windows (32-bit) for a bunch of small
scripts that use simple Tkinter interfaces to prompt for user
input and display information. We're thinking of freezing these
scripts (py2exe or pyinstaller), and we would like to remove some
of the optional/unnecessary files from the tcl folder that must
be distributed with frozen Tkinter apps.

I've looked at the files in the tcl directory tree and wonder if
the following folders can be deleted?

- tcl\tcl8.5\encoding 1415K
- tcl\tk8.5\demos  685K
- tcl\tcl8.5\tzdata 1450K

Any other tips on how we can reduce the size of our frozen exe
when using Tkinter would be grately appreciated.

Thank you,
Malcolm
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Re: unicode problem?

2010-10-09 Thread hidura
I had a similar problem but i can 't encode a byte to a file what has been  
uploaded, without damage the data if i used utf-8 to encode the file  
duplicates the size, and i try to change the codec to raw_unicode_escape  
and this barely give me the correct size but still damage the file, i used  
Python 3 and i have to encode the file again.


On Oct 9, 2010 11:39pm, Chris Rebert  wrote:

On Sat, Oct 9, 2010 at 4:59 PM, Brian Blais bbl...@bryant.edu> wrote:


> This may be a stemming from my complete ignorance of unicode, but when  
I do this (Python 2.6):



>



> s='\xc2\xa9 2008 \r\n'



>


> and I want the ascii version of it, ignoring any non-ascii chars, I  
thought I could do:



>



> s.encode('ascii','ignore')



>



> but it gives the error:



>



> In [20]:s.encode('ascii','ignore')


>  




> UnicodeDecodeError Traceback (most recent call last)



>



> /Users/bblais/python/doit100810a.py in ()



> > 1



> 2



> 3



> 4



> 5



>


> UnicodeDecodeError: 'ascii' codec can't decode byte 0xc2 in position 0:  
ordinal not in range(128)



>



> am I doing something stupid here?





In addition to Benjamin's explanation:





Unicode strings in Python are of type `unicode` and written with a



leading "u"; eg u"A unicode string for ¥500". Byte strings lack the



leading "u"; eg "A plain byte string". Note that "Unicode string"



does not refer to strings which have been encoded using a Unicode



encoding (eg UTF-8); such strings are still byte strings, for



encodings emit bytes.





As to why you got the /exact/ error you did:



As a backward compatibility hack, in order to satisfy your nonsensical



encoding request, Python implicitly tried to decode the byte string



`s` using ASCII as a default (the choice of ASCII here has nothing to



do with the fact that you specified ASCII in your encoding request),



so that it could then try and encode the resulting unicode string;



hence why you got a Unicode*De*codeError as opposed to a



Unicode*En*codeError, despite the fact you called *en*code().





Highly suggested further reading:



"The Absolute Minimum Every Software Developer Absolutely, Positively



Must Know About Unicode and Character Sets (No Excuses!)"



http://www.joelonsoftware.com/articles/Unicode.html





Cheers,



Chris



--



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


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


Re: reference vs. name space question

2010-10-09 Thread chad
On Oct 9, 5:52 pm, Steven D'Aprano  wrote:
> On Sat, 09 Oct 2010 12:44:29 -0700, chad wrote:
> > Given the following...
>
> > [cdal...@localhost oakland]$ python
> > Python 2.6.2 (r262:71600, May  3 2009, 17:04:44) [GCC 4.1.1 20061011
> > (Red Hat 4.1.1-30)] on linux2 Type "help", "copyright", "credits" or
> > "license" for more information.
>  class foo:
> > ...   x = 1
> > ...   y = 2
> > ...
>  one = foo()
>  two = foo()
>  print one
> > <__main__.foo instance at 0xb7f3a2ec>
>  print two
> > <__main__.foo instance at 0xb7f3a16c>
>  one.x
> > 1
>
> > Is 'one' a reference or a name space?  Also, in 'one.x'. would 'one'
> > be the name space?
>
> 'one' is a name. Since it is a bound name, it naturally refers to some
> object (in this case an instance of foo), which also makes it a reference.
>
> The object that 'one' is bound to is the namespace. The name itself is
> not -- the name itself comes *from* a namespace (in this case the global
> namespace).
>
> However, since people are lazy, and 98% of the time it makes no
> difference, and it is long and tedious to say "the object which the name
> 'one' is bound to is a namespace", people (including me) will often
> shorten that to "'one' is a namespace". But remember that when people use
> a name sometimes they're talking about the name itself and sometimes the
> object it is bound to:
>
>
>
> >>> x = 123  # x applies to the name 'x'.
> >>> print x  # x applies to the object the name is bound to
> 123
> >>> del x  # x applies to the name 'x'
>
> Not all names are references:
>
> >>> spam
>
> Traceback (most recent call last):
>   File "", line 1, in ?
> NameError: name 'spam' is not defined
>
> Since the name 'spam' is not bound to any object, it is not a reference.
> Likewise, given:
>
> def func(x, y):
>     pass
>
> the name 'func' is a name which is bound to a function object. The
> function object includes two names 'x' and 'y'. Since they're not bound
> to anything, they are not references *yet*, but when you call the
> function, they become (temporarily) bound.
>
> Hope this helps.

Maybe I'm being a bit dense, but how something like

[cdal...@localhost oakland]$ python
Python 2.6.2 (r262:71600, May  3 2009, 17:04:44)
[GCC 4.1.1 20061011 (Red Hat 4.1.1-30)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> spam
Traceback (most recent call last):
  File "", line 1, in 
NameError: name 'spam' is not defined
>>>

Generate an error, but something like
>>> def foo(x, y):
...   pass
...
>>>

Doesn't? I mean, in the first case, 'spam' isn't bound to anything.
Likewise, in the second case, both 'x' and 'y' aren't bound to
anything. I don't see why the interpreter doesn't complain about 'x'
and 'y' not being defined.

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


Re: unicode problem?

2010-10-09 Thread Chris Rebert
On Sat, Oct 9, 2010 at 4:59 PM, Brian Blais  wrote:
> This may be a stemming from my complete ignorance of unicode, but when I do 
> this (Python 2.6):
>
> s='\xc2\xa9 2008 \r\n'
>
> and I want the ascii version of it, ignoring any non-ascii chars, I thought I 
> could do:
>
> s.encode('ascii','ignore')
>
> but it gives the error:
>
> In [20]:s.encode('ascii','ignore')
> 
> UnicodeDecodeError                        Traceback (most recent call last)
>
> /Users/bblais/python/doit100810a.py in ()
> > 1
>      2
>      3
>      4
>      5
>
> UnicodeDecodeError: 'ascii' codec can't decode byte 0xc2 in position 0: 
> ordinal not in range(128)
>
> am I doing something stupid here?

In addition to Benjamin's explanation:

Unicode strings in Python are of type `unicode` and written with a
leading "u"; e.g. u"A unicode string for ¥500". Byte strings lack the
leading "u"; e.g. "A plain byte string". Note that "Unicode string"
does not refer to strings which have been encoded using a Unicode
encoding (e.g. UTF-8); such strings are still byte strings, for
encodings emit bytes.

As to why you got the /exact/ error you did:
As a backward compatibility hack, in order to satisfy your nonsensical
encoding request, Python implicitly tried to decode the byte string
`s` using ASCII as a default (the choice of ASCII here has nothing to
do with the fact that you specified ASCII in your encoding request),
so that it could then try and encode the resulting unicode string;
hence why you got a Unicode*De*codeError as opposed to a
Unicode*En*codeError, despite the fact you called *en*code().

Highly suggested further reading:
"The Absolute Minimum Every Software Developer Absolutely, Positively
Must Know About Unicode and Character Sets (No Excuses!)"
http://www.joelonsoftware.com/articles/Unicode.html

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


Re: unicode problem?

2010-10-09 Thread Benjamin Kaplan
On Sat, Oct 9, 2010 at 7:59 PM, Brian Blais  wrote:
> This may be a stemming from my complete ignorance of unicode, but when I do 
> this (Python 2.6):
>
> s='\xc2\xa9 2008 \r\n'
>
> and I want the ascii version of it, ignoring any non-ascii chars, I thought I 
> could do:
>
> s.encode('ascii','ignore')
>
> but it gives the error:
>
> In [20]:s.encode('ascii','ignore')
> 
> UnicodeDecodeError                        Traceback (most recent call last)
>
> /Users/bblais/python/doit100810a.py in ()
> > 1
>      2
>      3
>      4
>      5
>
> UnicodeDecodeError: 'ascii' codec can't decode byte 0xc2 in position 0: 
> ordinal not in range(128)
>
> am I doing something stupid here?
>
> of course, as a workaround, I can do: ''.join([c for c in s if ord(c)<128])
>
> but I thought the encode call should work.
>
>                thanks,
>                        bb
>

Encode takes a Unicode string (made up of code points) and turns it
into a byte string (a sequence of bytes). In your case, you don't have
a Unicode string. You have a byte string. In order to encode that
sequence of bytes into a different encoding, you have to first figure
out what those bytes mean (decode it). Python has no way of knowing
that your strings are UTF-8 so it just tries ascii as the default.

You can either decode the byte string explicitly or (if it's actually
a literal in your code) just specify it as a Unicode string.
s = u'\u00a9 2008'
s.encode('ascii','ignore')

The encode vs. decode confusion was removed in Python 3: byte strings
don't have an encode method and unicode strings don't have a decode
method.

> --
> Brian Blais
> bbl...@bryant.edu
> http://web.bryant.edu/~bblais
> http://bblais.blogspot.com/
>
>
>
> --
> http://mail.python.org/mailman/listinfo/python-list
>
-- 
http://mail.python.org/mailman/listinfo/python-list


unicode problem?

2010-10-09 Thread Brian Blais
This may be a stemming from my complete ignorance of unicode, but when I do 
this (Python 2.6):

s='\xc2\xa9 2008 \r\n'

and I want the ascii version of it, ignoring any non-ascii chars, I thought I 
could do:

s.encode('ascii','ignore')

but it gives the error:

In [20]:s.encode('ascii','ignore')

UnicodeDecodeErrorTraceback (most recent call last)

/Users/bblais/python/doit100810a.py in ()
> 1 
  2 
  3 
  4 
  5 

UnicodeDecodeError: 'ascii' codec can't decode byte 0xc2 in position 0: ordinal 
not in range(128)

am I doing something stupid here?

of course, as a workaround, I can do: ''.join([c for c in s if ord(c)<128])

but I thought the encode call should work.

thanks,
bb

-- 
Brian Blais
bbl...@bryant.edu
http://web.bryant.edu/~bblais
http://bblais.blogspot.com/



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


Re: Many newbie questions regarding python

2010-10-09 Thread Ethan Furman

Steven D'Aprano wrote:
And how often do you have an list that you are creating where you don't 
know what items you have to initialise the list with?


[snip]

You are right to point out that the third case is a Python gotcha: [[]]*n 
doesn't behave as expected by the naive or inexperienced Python 
programmer. I should have mentioned it, and pointed out that in that case 
you do want a list comp [[] for i in range(n)].


But that doesn't mean that the list comp is the general purpose solution. 
Consider the obvious use of the idiom:


def func(arg, count):
# Initialise the list.
L = [arg for i in range(count)]
# Do something with it.
process(L, some_function)

def process(L, f):
# Do something with each element.
for item in enumerate(L):
f(item)

Looks good, right? But it isn't, because it will suffer the exact same 
surprising behaviour if f modifies the items in place. Using a list comp 
doesn't save you if you don't know what the object is.


I've only been using Python for a couple years on a part-time basis, so 
I am not aquainted with this obvious use -- could you give a more 
concrete example?  Also, I do not see what the list comp has to do with 
the problem in process() -- the list has already been created at that 
point, so how is it the list comp's fault?


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


MealWell

2010-10-09 Thread Matt Tibbals
MealWell supports Matt Tibbals, pedophile. He is working to legalize
sex with children over the age of 2.
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Unicode Support in Ruby, Perl, Python, Emacs Lisp

2010-10-09 Thread Steven D'Aprano
On Sat, 09 Oct 2010 13:06:32 -0700, Bigos wrote:
[...]
> Maybe you have checked wrong version. There two versions of Ruby out
> there one does support unicode and the other doesn't.

Please don't feed the trolls. Xah Lee is a known troll who cross-posts to 
irrelevant newsgroups with his blatherings. He is not interested in 
learning anything which challenges his opinions, and rarely if every 
engages in dialog with those who respond.

Since your reply has little or nothing to do with the newsgroups you have 
sent it to, it is also spamming. While we're all extremely impressed by 
your assertion that Lisp is the bestest programming language evar, please 
keep your fan-boy gushing to comp.lang.lisp and don't cross-post again.

Followups to /dev/null.


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


Re: frozendict (v0.1)

2010-10-09 Thread John Nagle

On 10/7/2010 2:39 PM, kj wrote:

Following a suggestion from MRAB, I attempted to implement a
frozendict class.


   That really should be built into the language.  "dict" is the
last built-in type that doesn't have an immutable form.

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


Re: reference vs. name space question

2010-10-09 Thread Steven D'Aprano
On Sat, 09 Oct 2010 12:44:29 -0700, chad wrote:

> Given the following...
> 
> [cdal...@localhost oakland]$ python
> Python 2.6.2 (r262:71600, May  3 2009, 17:04:44) [GCC 4.1.1 20061011
> (Red Hat 4.1.1-30)] on linux2 Type "help", "copyright", "credits" or
> "license" for more information.
 class foo:
> ...   x = 1
> ...   y = 2
> ...
 one = foo()
 two = foo()
 print one
> <__main__.foo instance at 0xb7f3a2ec>
 print two
> <__main__.foo instance at 0xb7f3a16c>
 one.x
> 1
> 
> 
> Is 'one' a reference or a name space?  Also, in 'one.x'. would 'one'
> be the name space?

'one' is a name. Since it is a bound name, it naturally refers to some 
object (in this case an instance of foo), which also makes it a reference.

The object that 'one' is bound to is the namespace. The name itself is 
not -- the name itself comes *from* a namespace (in this case the global 
namespace).

However, since people are lazy, and 98% of the time it makes no 
difference, and it is long and tedious to say "the object which the name 
'one' is bound to is a namespace", people (including me) will often 
shorten that to "'one' is a namespace". But remember that when people use 
a name sometimes they're talking about the name itself and sometimes the 
object it is bound to:

>>> x = 123  # x applies to the name 'x'.
>>> print x  # x applies to the object the name is bound to
123
>>> del x  # x applies to the name 'x'


Not all names are references:

>>> spam
Traceback (most recent call last):
  File "", line 1, in ?
NameError: name 'spam' is not defined

Since the name 'spam' is not bound to any object, it is not a reference. 
Likewise, given:

def func(x, y):
pass


the name 'func' is a name which is bound to a function object. The 
function object includes two names 'x' and 'y'. Since they're not bound 
to anything, they are not references *yet*, but when you call the 
function, they become (temporarily) bound.


Hope this helps.



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


Re: Unicode Support in Ruby, Perl, Python, Emacs Lisp

2010-10-09 Thread Xah Lee
2010-10-09

On Oct 9, 3:45 pm, Sean McAfee  wrote:
> Xah Lee  writes:
> > Perl's exceedingly lousy unicode support hack is well known. In fact
> > it is the primary reason i “switched” to python for my scripting needs
> > in 2005. (See: Unicode in Perl and Python)
>
> I think your assessment is antiquated.  I've been doing Unicode
> programming with Perl for about three years, and it's generally quite
> wonderfully transparent.

you are probably right. The last period i did serious perl is 1998 to
2004. Since, have pretty much lost contact with perl community.

i have like 5 years of 8 hours day experience with perl... the app we
wrote is probably the largest perl web app at the time, say within the
top 10 largest perl web apps, during the dot com days.

spend 2 years with python about 2005, 2006, but mostly just personal
dabbling.

my dilema is this... i am really tired of perl, so i thougth python is
my solution. Comparing the syntax, semantics, etc, i really do find
python better, but to know python as well as i know perl, or, to know
a lang really as a expert (e.g. intimately familiar with all the ins
and outs of constructs, idioms, their speeds, libraries out there,
their nature, which are used, their bugs etc), takes years. So,
whenever i have this psychological urge to totally ditch perl and hug
python 100% ... but it takes a huge amount of time to dig into a lang
well again, so sometimes i thought of sticking with my perl due to my
existing knowledge and forthwith stop wasting valuable time, but then,
whenever i work in perl with its hack nature and crooked community
(all those mongers fuck), especially the syntax for nested list/hash
that's more than 3 levels (and my code almost always rely on nested
list/hash to do things since am a functional programer), and compare
to python's syntax on nested structure, i ask my self again, is this
shit really what i want to keep on at?

and python 3 comes in, and over the years i learned, that Guido really
hates functional programing (he understands it nil), and python is
moving more innto oop mumbo jumbo with more special syntaxes and
special semantics. (and perl is trivially far more capable at
functional programing than python) So, this puts a damnation in my
mental struggle for python.

in the end i really haven't decided on anything, as usual... it's not
really concrete, answerable question anyway, it's just psy struggle on
some fuzzy ideal about efficiency and perfect lang.

and there's ruby... (among others) and because i'm such a douchbag for
langs, now and then i suppose i waste my time to venture and read
about ruby, the unconcious execuse is that maybe ruby will turn out to
simply solve all my life's problems, but nagging in the back of my
mind is the reality that, yeah, go spend 3 years 8 hours a day on
ruby, then possibly it'll be practically useful to me as i do with
perl already, and, no, it won't bring you anything extra as far as
lang goes, for that you go to OCaml/F#, erlang, Mathematica ... and
who knows what kinda hidden needle in the eye i'll discover on my road
in ruby.

btw, this is all just a geek's mental disorder, common with many who's
into lang design and beauty etc type of shit. (high percentage of this
crowd hang in newsgroups) But the reality is that, this psychological
problem really don't have much practical justification ... it's just
fret, fret, fret. Fret, fret, fret. Years of fretting, while others
have written great apps all over the web.

in practice, i do not even have a need for perl or python in my work
since about 2006, except a few find/replace scripts for text
processing that i've written in the past. And, since about 2007, i've
been increasingly writing lots and lots more in elisp. (and this emacs
beast, is really a true love more than anything) So these days, almost
all of my scripts are in elisp. (and my job these days is mainly just
text processing programing)

• 〈Xah on Programing Languages〉
http://xahlee.org/Periodic_dosage_dir/comp_lang.html

> On the programmers' web site stackoverflow.com, I flag questions with
> the "unicode" tag, and of questions that mention a specific language,
> Python and C++ seem to come up the most often.
>
> > I'll have to say, as far as text processing goes, the most beautiful
> > lang with respect to unicode is emacs lisp. In elisp code (e.g.
> > Generate a Web Links Report with Emacs Lisp ), i don't have to declare
> > none of the unicode or encoding stuff. I simply write code to process
> > string or buffer text, without even having to know what encoding it
> > is. Emacs the environment takes care of all that.
>
> It's not quite perfect, though.  I recently discovered that if I enter a
> Chinese character using my Mac's Chinese input method, and then enter
> the same character using a Japanese input method, Emacs regards them as
> different characters, even though they have the same Unicode code point.
> For example, from describe-char:
>
>   character: 一 (43323, #o124473, #xa

Re: hashkey/digest for a complex object

2010-10-09 Thread Steven D'Aprano
On Sat, 09 Oct 2010 21:39:51 +0100, Arnaud Delobelle wrote:

> 1. hash() is an idempotent function, i.e. hash(hash(x)) == hash(x) hold
> for any hashable x (this is a simple consequence of the fact that
> hash(x) == x for any int x (by 'int' I mean 2.X int)).

It's a beautiful theory, but, alas, it is not the case.

>>> hash(-1) == -1
False
>>> hash(2**64) == 2**64
False

to give only two of an infinite number of counter-examples.

Aside: what do you mean by '2.x int'? Do you mean an int in 2.x versions 
before, or after, ints and longs were partially integrated?

[st...@sylar ~]$ python2.1
Python 2.1.3 (#1, Aug 12 2010, 01:53:57)
[GCC 4.1.2 20070925 (Red Hat 4.1.2-27)] on linux2
Type "copyright", "credits" or "license" for more information.
>>> 2**64
Traceback (most recent call last):
  File "", line 1, in ?
OverflowError: integer exponentiation
>>>


People keep forgetting that 2.2 introduced nearly as many far-reaching 
changes as 3.0.


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


Re: Unicode Support in Ruby, Perl, Python, Emacs Lisp

2010-10-09 Thread Sean McAfee
Xah Lee  writes:
> Perl's exceedingly lousy unicode support hack is well known. In fact
> it is the primary reason i “switched” to python for my scripting needs
> in 2005. (See: Unicode in Perl and Python)

I think your assessment is antiquated.  I've been doing Unicode
programming with Perl for about three years, and it's generally quite
wonderfully transparent.

On the programmers' web site stackoverflow.com, I flag questions with
the "unicode" tag, and of questions that mention a specific language,
Python and C++ seem to come up the most often.

> I'll have to say, as far as text processing goes, the most beautiful
> lang with respect to unicode is emacs lisp. In elisp code (e.g.
> Generate a Web Links Report with Emacs Lisp ), i don't have to declare
> none of the unicode or encoding stuff. I simply write code to process
> string or buffer text, without even having to know what encoding it
> is. Emacs the environment takes care of all that.

It's not quite perfect, though.  I recently discovered that if I enter a
Chinese character using my Mac's Chinese input method, and then enter
the same character using a Japanese input method, Emacs regards them as
different characters, even though they have the same Unicode code point.
For example, from describe-char:

  character: 一 (43323, #o124473, #xa93b, U+4E00)
  character: 一 (55404, #o154154, #xd86c, U+4E00)

On saving and reverting a file containing such text, the characters are
"normalized" to the Japanese version.
  
I suppose this might conceivably be the correct behavior, but it sure
was a surprise that (equal "一" "一") can be nil.
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: reference vs. name space question

2010-10-09 Thread Ben Finney
chad  writes:

> >>> print one
> <__main__.foo instance at 0xb7f3a2ec>
> >>> print two
> <__main__.foo instance at 0xb7f3a16c>
> >>> one.x
> 1
>
>
> Is 'one' a reference or a name space?

Yes.

It's a reference to an instance of the ‘foo’ type.

That instance is also a namespace; in other words, its attributes are in
a distinct namespace from all other instances.

This is complicated by the fact that it *also* gives access to the
attributes of ‘foo’, shared by all instances of that type. But that's
part of what it means to be an instance of the ‘foo’ type.

> Also, in 'one.x'. would 'one' be the name space?

It's *a* namespace. Since ‘one.x’ also presumably has attributes, then
it, too, is a namespace.


Learn more about classes and namespaces in Python's documentation
http://docs.python.org/tutorial/classes.html>.

In fact, you should work through the entire tutorial from start to
finish, experimenting manually with each example until you unsderstand
before moving on. That will give you a very solid grounding in Python.

-- 
 \“I got fired from my job the other day. They said my |
  `\  personality was weird. … That's okay, I have four more.” |
_o__)   —Bug-Eyed Earl, _Red Meat_ |
Ben Finney
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: hashkey/digest for a complex object

2010-10-09 Thread Arnaud Delobelle
kj  writes:

> In <87y6a9lqnj@gmail.com> Arnaud Delobelle  writes:
>
>>You could do something like this:
>
>>deep_methods = {   
>>list: lambda f, l: tuple(map(f, l)),
>>dict: lambda f, d: frozenset((k, f(v)) for k, v in d.items()),
>>set: lambda f, s: frozenset(map(f, s)),
>># Add more if needed
>>}
>
>>def apply_method(f, obj):
>>try:
>>method = deep_methods[type(obj)]
>>except KeyError:
>>return obj
>>return method(f, obj)
>
>>def deepfreeze(obj):
>>"""Return a 'hashable version' of an object
>>return apply_method(deepfreeze, obj)
>
>>def deephash(obj):
>>"""Return hash(deepfreeze(obj)) without deepfreezing"""
>>return hash(apply_method(deephash, obj))
>
>># Example of deepfreezable object:
>>obj = [1, "foo", {(2, 4): {7, 5, 4}, "bar": "baz"}]
>^   ^
>|   |
>`---`--- what's this?

This is set literal notation, introduced in Python 3!
In 2.X, you would write set([7, 5, 4])

>
>
> deepfreeze(obj)
>>(1, 'foo', frozenset({('bar', 'baz'), ((2, 4), frozenset({4, 5, 7}))}))
> deephash(obj)
>>1341422540
> hash(deepfreeze(obj))
>>1341422540
>
>
> After fixing the missing """ in deepfreeze this code works as
> advertised, 

Oops, I must admit I added the docstrings after pasting the code :)

> but I'm mystified by the identity between hash(deepfreeze(...))  and
> deephash(...).  Without some knowledge of the Python internals, I
> don't see how this follows.

OK.  I haven't actually proved it, but it follows from the following
facts:

1. hash() is an idempotent function, i.e. hash(hash(x)) == hash(x) hold
for any hashable x (this is a simple consequence of the fact that
hash(x) == x for any int x (by 'int' I mean 2.X int)).

2. Container hashable objects compute their hash from the hash of their
elements.

I don't think either of these two facts is documented, but it would be quite
easy to verify them in the Python source (I must admit I have not done
it).  And it is difficult to conceive how it could be otherwise.

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


Re: Control webbrowser from Python script

2010-10-09 Thread Diez B. Roggisch
Johny  writes:

> On Oct 9, 5:17 pm, Tim Harig  wrote:
>> On 2010-10-09, Johny  wrote:
>>
>> > Is it possible to control any webbrowser from Python ? For example to
>> > issue http POST and GET  command
>>
>> The most reliable way to interact with a webserver is through the urllib
>> and httplib modules.  This is effective for 99% of cases.  I do understand
>> that some sites heavy in javascript can be difficult to figure out how to
>> make valid requests.  On win32 you can automate Internet Explorer through
>> its InternetExplorer.Application COM interface which is fully documented at
>> the MSDN site.  On other operating systems, Firefox can be accessed using
>> its XPCOM interface.
>
> Thanks ALL who replied.I wanted to use directly  httplib modul and I
> started with it.But webserver uses SSL communication.
> Of course, I can use https protocol but the communication between
> server and  client( browser) is so complicated that I was unable to
> simulate that with python script.So I think I could do that with
> webbrowser directly ( control webbrowser from a python script)

What has HTTPS to do with this? 

My tip for this problem: use FireBug and HTTP Live Headers to get an
idea what is really going on between Server & Browser.

Then model that with Python + urllib2.

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


Re: Unicode Support in Ruby, Perl, Python, Emacs Lisp

2010-10-09 Thread Bigos
On Oct 7, 7:13 pm, Xah Lee  wrote:
> here's my experiences dealing with unicode in various langs.
>
> Unicode Support in Ruby, Perl, Python, Emacs Lisp
>
> Xah Lee, 2010-10-07
>
> I looked at Ruby 2 years ago. One problem i found is that it does not
> support Unicode well. I just checked today, it still doesn't. Just do
> a web search on blog and forums on “ruby unicode”. e.g.: Source,
> Source, Source, Source.
>
> Perl's exceedingly lousy unicode support hack is well known. In fact
> it is the primary reason i “switched” to python for my scripting needs
> in 2005. (See: Unicode in Perl and Python)
>
> Python 2.x's unicode support is also not ideal. You have to declare
> your source code with header like 「#-*- coding: utf-8 -*-」, and you
> have to declare your string as unicode with “u”, e.g. 「u"林花謝了春紅"」. In
> regex, you have to use unicode flag such as 「re.search(r'\.html
> $',child,re.U)」. And when processing files, you have to read in with
> 「unicode(inF.read(),'utf-8')」, and printing out unicode you have to
> do「outF.write(outtext.encode('utf-8'))」. If you are processing lots of
> files, and if one of the file contains a bad char or doesn't use
> encoding you expected, your python script chokes dead in the middle,
> you don't even know which file it is or which line unless your code
> print file names.
>
> Also, if the output shell doesn't support unicode or doesn't match
> with the encoding specified in your python print, you get gibberish.
> It is often a headache to figure out the locale settings, what
> encoding the terminal support or is configured to handle, the encoding
> of your file, the which encoding the “print” is using. It gets more
> complex if you are going thru a network, such as ssh. (most shells,
> terminals, as of 2010-10, in practice, still have problems dealing
> with unicode. (e.g. Windows Console, PuTTY. Exception being Mac's
> Apple Terminal.))
>
> Python 3 supposedly fixed the unicode problem, but i haven't used it.
> Last time i looked into whether i should adopt python 3, but
> apparently it isn't used much. (See: Python 3 Adoption) (and i'm quite
> pissed that Python is going more and more into OOP mumbo jumbo with
> lots ad hoc syntax (e.g. “views”, “iterators”, “list comprehension”.))
>
> I'll have to say, as far as text processing goes, the most beautiful
> lang with respect to unicode is emacs lisp. In elisp code (e.g.
> Generate a Web Links Report with Emacs Lisp ), i don't have to declare
> none of the unicode or encoding stuff. I simply write code to process
> string or buffer text, without even having to know what encoding it
> is. Emacs the environment takes care of all that.
>
> It seems that javascript and PHP also support unicode well, but i
> don't have extensive experience with them. I suppose that elisp, php,
> javascript, all support unicode well because these langs have to deal
> with unicode in practical day-to-day situations.
>
> --
> for links, 
> seehttp://xahlee.blogspot.com/2010/10/unicode-support-in-ruby-perl-pytho...
>
>  Xah ∑ xahlee.org ☄

Maybe you have checked wrong version. There two versions of Ruby out
there one does support unicode and the other doesn't. Latest version
ie. 1.9.x branch has made some progress in that regard. Please check
the following links to see if the solve your problem.

http://nuclearsquid.com/writings/ruby-1-9-encodings.html
http://loopkid.net/articles/2008/07/07/ruby-1-9-utf-8-mostly-works
http://stackoverflow.com/questions/1627767/rubys-stringgsub-unicode-and-non-word-characters

I think latest recommended version of Ruby is ruby 1.9.2p0, please try
it to see if it works for you. Of course it is not as good as Lisp,
and in Rails code you see people writing the same sequences of
characters over and over again, but some people like it because it is
better than other languages they used before. If it's a stepping stone
towards Lisp then it is a good thing imho.
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: hashkey/digest for a complex object

2010-10-09 Thread kj
In <87y6a9lqnj@gmail.com> Arnaud Delobelle  writes:

>You could do something like this:

>deep_methods = {   
>list: lambda f, l: tuple(map(f, l)),
>dict: lambda f, d: frozenset((k, f(v)) for k, v in d.items()),
>set: lambda f, s: frozenset(map(f, s)),
># Add more if needed
>}

>def apply_method(f, obj):
>try:
>method = deep_methods[type(obj)]
>except KeyError:
>return obj
>return method(f, obj)

>def deepfreeze(obj):
>"""Return a 'hashable version' of an object
>return apply_method(deepfreeze, obj)

>def deephash(obj):
>"""Return hash(deepfreeze(obj)) without deepfreezing"""
>return hash(apply_method(deephash, obj))

># Example of deepfreezable object:
>obj = [1, "foo", {(2, 4): {7, 5, 4}, "bar": "baz"}]
   ^   ^
   |   |
   `---`--- what's this?


 deepfreeze(obj)
>(1, 'foo', frozenset({('bar', 'baz'), ((2, 4), frozenset({4, 5, 7}))}))
 deephash(obj)
>1341422540
 hash(deepfreeze(obj))
>1341422540


After fixing the missing """ in deepfreeze this code works as
advertised, but I'm mystified by the identity between hash(deepfreeze(...))
and deephash(...).  Without some knowledge of the Python internals,
I don't see how this follows.

More specifically, it is not obvious to me that, for example,

hash(frozenset((,)))

would be identical to

hash(frozenset((hash(),)))

but this identity has held every time I've checked it.  Similarly
for other more complicated variations on this theme.

Anyway, thanks for the code.  It's very useful.

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


reference vs. name space question

2010-10-09 Thread chad
Given the following...

[cdal...@localhost oakland]$ python
Python 2.6.2 (r262:71600, May  3 2009, 17:04:44)
[GCC 4.1.1 20061011 (Red Hat 4.1.1-30)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> class foo:
...   x = 1
...   y = 2
...
>>> one = foo()
>>> two = foo()
>>> print one
<__main__.foo instance at 0xb7f3a2ec>
>>> print two
<__main__.foo instance at 0xb7f3a16c>
>>> one.x
1


Is 'one' a reference or a name space?  Also, in 'one.x'. would 'one'
be the name space?

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


Re: Testing for changes on a web page (was: how to find difference in number of characters)

2010-10-09 Thread D'Arcy J.M. Cain
On 09 Oct 2010 17:47:56 GMT
Seebs  wrote:
> In other words, your problem here is that you haven't actually described
> what you want.  Slow down.  Think!  Describe what you want clearly enough
> that any other person who reads your description can always come up with
> the same answer you would for a given set of inputs.

Better yet, write the unit tests for us.

-- 
D'Arcy J.M. Cain  |  Democracy is three wolves
http://www.druid.net/darcy/|  and a sheep voting on
+1 416 425 1212 (DoD#0082)(eNTP)   |  what's for dinner.
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: how to find difference in number of characters

2010-10-09 Thread Diez B. Roggisch
harryos  writes:

> On Oct 9, 4:52 pm, Peter Otten <__pete...@web.de> wrote:
>
>>
>> You might get more/better answers if you tell us more about the context of
>> the problem and add some details that may be relevant.
>>
>> Peter
>
> I am trying to determine if a wep page is updated by x number of
> characters..Mozilla firefox plugin 'update scanner' has a similar
> functionality ..A user can specify the x ..I think this would be done
> by reading from the same url at two different times and finding the
> change in body text..I was wondering if difflib could offer something
> in the way of determining the size of delta..

If you normalize the data, this might be worth trying.

Make all tags appear on one single line, possibly re-order attributes so
that they are in alphabetical order. Each text child git's also
normalized, by replacing all whitespace with a single space.

Then run difflib over these, and count the number of diffrences.


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


Re: Re: [Web-SIG] Propouse code

2010-10-09 Thread hidura
What type of codec is the best to encode binary files what are upload to an  
app write in Python 3?


On Oct 7, 2010 1:44pm, Tres Seaver  wrote:

-BEGIN PGP SIGNED MESSAGE-



Hash: SHA1





On 10/07/2010 12:17 PM, hid...@gmail.com



wrote:




> Hello list, i am develop an web app in Python 3 and i was faced the  
problem


> of the wsgi.input, at the first i follow all the manuals and always  
give me



> an error but after i create my own code, and with all the modest of the



> world i want to know how i can share with the python community when i



> finish the test?





I suggest the following:





- - Host the code's repository on the DVCS-friendly server of your choice



(bitbucket, github, launchpad). You might even post the link to the



repository to this list before you are ready for a formal release:



some brave souls might try it out, and give you patches or comments.





- - Manage your application libraries as a distitis project (eg,



installable via 'python setup.py install').





- - Ideally, use either 'setuptools' or 'distirbute' extensions to



the distutils, which will make releasing the code to the Python



Package Index as easy as; 'python setup.py sdist register upload'.





- - Announce your release(s) on this list.







Trse.



- --



===



Tres Seaver +1 540-429-0999 tsea...@palladion.com



Palladion Software "Excellence by Design" http://palladion.com



-BEGIN PGP SIGNATURE-



Version: GnuPG v1.4.10 (GNU/Linux)



Comment: Using GnuPG with Mozilla - http://enigmail.mozdev.org/





iEYEARECAAYFAkyuBuUACgkQ+gerLs4ltQ43nwCfbNkVEhxbpJ3xKwmuosLaIUpg



JxMAn2SfnXhoJuhZQEVikCz1FaAEKNXJ



=ZNeA



-END PGP SIGNATURE-





___



Web-SIG mailing list



web-...@python.org



Web SIG: http://www.python.org/sigs/web-sig


Unsubscribe:  
http://mail.python.org/mailman/options/web-sig/hidura%40gmail.com


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


Testing for changes on a web page (was: how to find difference in number of characters)

2010-10-09 Thread geremy condra
On Sat, Oct 9, 2010 at 10:47 AM, Seebs  wrote:
> On 2010-10-09, harryos  wrote:
>> What I meant by number of characters was the number of edits happened
>> between the two versions..
>
> Consider two strings:
>
> Hello, world!
>
> Yo, there.
>
> What is the "number of edits happened between the two versions"?  It could
> be:
>
> * Zero.  I just typed them both from scratch, no editing occurred between
>  them.
> * Two.  Two words are different.
> * Ten or so -- counting changed characters.
> * Three.  Two words and a punctuation mark are different.
>
> In other words, your problem here is that you haven't actually described
> what you want.  Slow down.  Think!  Describe what you want clearly enough
> that any other person who reads your description can always come up with
> the same answer you would for a given set of inputs.
>
> -s

He mentioned L distance earlier, I'm sure he means 'number of edits'
in that context...

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


Re: Testing for changes on a web page (was: how to find difference in number of characters)

2010-10-09 Thread Seebs
On 2010-10-09, harryos  wrote:
> What I meant by number of characters was the number of edits happened
> between the two versions..

Consider two strings:

Hello, world!

Yo, there.

What is the "number of edits happened between the two versions"?  It could
be:

* Zero.  I just typed them both from scratch, no editing occurred between
  them.
* Two.  Two words are different.
* Ten or so -- counting changed characters.
* Three.  Two words and a punctuation mark are different.

In other words, your problem here is that you haven't actually described
what you want.  Slow down.  Think!  Describe what you want clearly enough
that any other person who reads your description can always come up with
the same answer you would for a given set of inputs.

-s
-- 
Copyright 2010, all wrongs reversed.  Peter Seebach / usenet-nos...@seebs.net
http://www.seebs.net/log/ <-- lawsuits, religion, and funny pictures
http://en.wikipedia.org/wiki/Fair_Game_(Scientology) <-- get educated!
I am not speaking for my employer, although they do rent some of my opinions.
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Control webbrowser from Python script

2010-10-09 Thread Johny
On Oct 9, 5:17 pm, Tim Harig  wrote:
> On 2010-10-09, Johny  wrote:
>
> > Is it possible to control any webbrowser from Python ? For example to
> > issue http POST and GET  command
>
> The most reliable way to interact with a webserver is through the urllib
> and httplib modules.  This is effective for 99% of cases.  I do understand
> that some sites heavy in javascript can be difficult to figure out how to
> make valid requests.  On win32 you can automate Internet Explorer through
> its InternetExplorer.Application COM interface which is fully documented at
> the MSDN site.  On other operating systems, Firefox can be accessed using
> its XPCOM interface.

Thanks ALL who replied.I wanted to use directly  httplib modul and I
started with it.But webserver uses SSL communication.
Of course, I can use https protocol but the communication between
server and  client( browser) is so complicated that I was unable to
simulate that with python script.So I think I could do that with
webbrowser directly ( control webbrowser from a python script)
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: script in Linux vs Windows

2010-10-09 Thread aurfalien

On Oct 8, 2010, at 9:33 PM, Dennis Lee Bieber wrote:


On Fri, 8 Oct 2010 14:52:33 -0700, aurfal...@gmail.com declaimed the
following in gmane.comp.python.general:


Hi all,

Unsure how to deal with what appears to be \n vs \r issues.

The following code works in Linux;

o = open("axenfs.reg")
n = open("axenfs2.reg", "a")


Why are you opening in "a"ppend? If you want to create a new file
containing the edited contents of the input file, just open "w".



But in Windows, its one continues line with a bunch of squares in it.


In Windows WHAT? Notepad, Wordpad, Word, a command console using
"type".


Geez man, relax.  I thought it was obvious by the code that I was  
opening it using Python.


I changed a to w and added a b, so all is well now, ie:  "wb"

Still don't know why Windows treats text files as binary files but  
using wb worked well.


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


Re: Control webbrowser from Python script

2010-10-09 Thread Tim Harig
On 2010-10-09, Johny  wrote:
> Is it possible to control any webbrowser from Python ? For example to
> issue http POST and GET  command

The most reliable way to interact with a webserver is through the urllib
and httplib modules.  This is effective for 99% of cases.  I do understand
that some sites heavy in javascript can be difficult to figure out how to
make valid requests.  On win32 you can automate Internet Explorer through
its InternetExplorer.Application COM interface which is fully documented at
the MSDN site.  On other operating systems, Firefox can be accessed using
its XPCOM interface.
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Testing for changes on a web page (was: how to find difference in number of characters)

2010-10-09 Thread Emmanuel Surleau
> On Oct 9, 5:41 pm, Stefan Behnel  wrote:
> > "Number of characters" sounds like a rather useless measure here.
> 
> What I meant by number of characters was the number of edits happened
> between the two versions..Levenshtein distance may be one way for
> this..but I was wondering if difflib could do this
> regards

As pointed out above, you also need to consider how the structure of the web 
page has changed. If you are only looking at plain text, the Levenshtein 
distance measures the number of edit operations (insertion, deletion or 
substition) necessary to transform string A into string B.

Cheers,

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


Re: Control webbrowser from Python script

2010-10-09 Thread Emmanuel Surleau
> Is it possible to control any webbrowser from Python ? For example to
> issue http POST and GET  command
> Thanks
> Johny

http://docs.python.org/library/webbrowser.html

The control you get is rather limited, though. If your aim is interacting with 
a website, though, you can try urllib/urllib2 for simple things, and Mechanize 
( http://wwwsearch.sourceforge.net/mechanize/ ) or Twill ( 
http://twill.idyll.org/ ) for more complex operations.

Cheers,

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


Re: if the else short form

2010-10-09 Thread NevilleDNZ
On Oct 9, 6:55 pm, Lawrence D'Oliveiro  wrote:
> In message , BartC wrote:
>
> > "NevilleDNZ"  wrote in message
> >news:ad9841df-49a1-4c1b-95d0-e76b72df6...@w9g2000prc.googlegroups.com...
>
> >> In Algol68 this would be:
> >> x:=(i|"One","Two","Three"|"None Of The Above")
>
> > The point is, the construction works well when the syntax fully supports
> > it.
>
> But note that Algol68 doesn’t support explicit labels on the alternatives
> in a case expression or statement. That idea came later.

Good point... I do ponder why (given that linked lists can easily be
created in Algol68) useful types like LIST and DICT were left out of
the standard prelude.  I do not recall any conversation about this in
the "ALGOL Bulletins" /* I can reasonably guess why C elected to
excluded these types */

The nearest to "explicit labels" in Algol68 would be:
STRING x:=(i=0|"Zero" |:i=1|"One" |:i=2|"Two" |:i=3|"Three" |"None Of
The Above");

Basically a compound IF statement... effectively a read only, compile-
time dictionary.

N


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


Re: Testing for changes on a web page (was: how to find difference in number of characters)

2010-10-09 Thread harryos
On Oct 9, 5:41 pm, Stefan Behnel  wrote:

> "Number of characters" sounds like a rather useless measure here.

What I meant by number of characters was the number of edits happened
between the two versions..Levenshtein distance may be one way for
this..but I was wondering if difflib could do this
regards

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


Testing for changes on a web page (was: how to find difference in number of characters)

2010-10-09 Thread Stefan Behnel

harryos, 09.10.2010 14:24:

I am trying to determine if a wep page is updated by x number of
characters..Mozilla firefox plugin 'update scanner' has a similar
functionality ..A user can specify the x ..I think this would be done
by reading from the same url at two different times and finding the
change in body text.


"Number of characters" sounds like a rather useless measure here. I'd 
rather apply an XPath, CSS selector or PyQuery expression to the parsed 
page and check if the interesting subtree of it has changed at all or not, 
potentially disregarding any structural changes by stripping all tags and 
normalising the resulting text to ignore whitespace and case differences.


Stefan

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


Re: how to find difference in number of characters

2010-10-09 Thread harryos
On Oct 9, 4:52 pm, Peter Otten <__pete...@web.de> wrote:

>
> You might get more/better answers if you tell us more about the context of
> the problem and add some details that may be relevant.
>
> Peter

I am trying to determine if a wep page is updated by x number of
characters..Mozilla firefox plugin 'update scanner' has a similar
functionality ..A user can specify the x ..I think this would be done
by reading from the same url at two different times and finding the
change in body text..I was wondering if difflib could offer something
in the way of determining the size of delta..
Thanks again for the reply..
harry
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Simple database explorer

2010-10-09 Thread dusans
"Its the right answer for a program that needs to be used with many
different RDBMSes,
especially if you use its metadata access procedures."


U got it right :)
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: how to find difference in number of characters

2010-10-09 Thread Peter Otten
harryos wrote:

> but is there a way I can use difflib module to do this job?

I'm afraid I can't help you with that. 

You might get more/better answers if you tell us more about the context of 
the problem and add some details that may be relevant.

Peter

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


Re: Control webbrowser from Python script

2010-10-09 Thread Matteo Landi
Well, if you need to issue http POST/GET commands, you can take a look
at urllib/urllib2 modules. Instead if you want to take control of the
web-browser I've heard about selenium, but I've never used it.

Best regards,
Matteo

On Sat, Oct 9, 2010 at 11:39 AM, Johny  wrote:
> Is it possible to control any webbrowser from Python ? For example to
> issue http POST and GET  command
> Thanks
> Johny
> --
> http://mail.python.org/mailman/listinfo/python-list
>



-- 
Matteo Landi
http://www.matteolandi.net/
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Many newbie questions regarding python

2010-10-09 Thread Hrvoje Niksic
alex23  writes:

> If anything, I feel like the list comp version is the correct solution
> because of its reliability, whereas the multiplication form feels like
> either a lucky naive approach or relies on the reader to know the type
> of the initialising value and its mutability.

Other than list comp being slower, I'd like to point out that in some
cases the multiplication is far from being naive.  Consider this
pattern:

def in_groups_of(n, iterable):
"""Yield items of iterable packed in tuples of size n."""
return itertools.izip(*[iter(iterable)] * n)

>>> for a, b, c in in_groups_of(3, xrange(9)):
...   print a, b, c
... 
0 1 2
3 4 5
6 7 8

In the call to itertools.izip we are actually counting on list
repetition to refer to the same object.  Rewriting it as the list
comprehension would break it:

def in_groups_of(n, iterable):
return itertools.izip(*[iter(iterable) for _ in xrange(n)])

>>> for a, b, c in in_groups_of(3, xrange(9)):
...   print a, b, c
... 
0 0 0
1 1 1
2 2 2
3 3 3
4 4 4
5 5 5
6 6 6
7 7 7
8 8 8

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


Re: how to find difference in number of characters

2010-10-09 Thread harryos
On Oct 9, 2:45 pm, Peter Otten <__pete...@web.de> wrote:

>
> What would be an acceptable time?
>

Thanks for the reply Peter,
I was using python functions I came across the net..not cpython
implementations..Probably my low config machine is also to blame..(I
am no expert at judging algorithm performance either),but is there a
way I can use difflib module to do this job?Even though I went through
the docs I couldn't make out how..

regards
harry

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


Re: how to find difference in number of characters

2010-10-09 Thread Peter Otten
harryos wrote:

> I am trying to write a compare method which takes two strings and find
> how many characters have changed.
> 
> 
> def compare_strings(s1,s2):
> pass
> 
> 
> text1="goat milk"
> text2="cow milk"
> print compare_strings(text1,text2)
> 
> This must give 3 ,since 3 characters are changed between strings.I was
> advised to use levenshtein algorithm ..but then the matrix ops take a
> long time for nontrivial strings of say 2 characters ..Can this

I tried it with a string of 2 chars and the python-levenshtein package 
that comes with ubuntu. It took about one second to calculate the distance:

import functools
import random
import string
import time

from Levenshtein import distance

def make_strings(n, delete, insert, swap, replace, 
charset=string.ascii_letters):
def gen_chars():
while True:
yield random.choice(charset)
chars = gen_chars()
a = [next(chars) for i in xrange(n)]
s = "".join(a)
for i in range(delete):
del a[random.randrange(len(a))]
for i in range(insert):
a.insert(random.randrange(len(a)+1), next(chars))
for i in range(swap):
p = random.randrange(len(a)-1)
a[p], a[p+1] = a[p+1], a[p]
for i in range(replace):
a[random.randrange(len(a))] = next(chars)
t = "".join(a)
return s, t

N = 2
M = 100
ms = functools.partial(make_strings, N, M, M, M, M)

def measure(f, *args):
start = time.time()
try:
return f(*args)
finally:
print time.time() - start

if __name__ == "__main__":
import sys
args = sys.argv[1:]
if args:
N, M = map(int, args)

s, t = make_strings(N, M, M, M, M)
print measure(distance, s, t)

$ python levenshtein_demo.py 1 1000
0.225363969803
3644
$ python levenshtein_demo.py 2 1000
1.05217313766
4197
$ python levenshtein_demo.py 3 1000
2.38736391068
4390
$ python levenshtein_demo.py 4 1000
4.1686527729
4558

What would be an acceptable time?

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


Control webbrowser from Python script

2010-10-09 Thread Johny
Is it possible to control any webbrowser from Python ? For example to
issue http POST and GET  command
Thanks
Johny
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: how to handle network failures

2010-10-09 Thread Diez B. Roggisch
harryos  writes:

> hi
> I  am trying to write a DataGrabber which reads some data from given
> url..I made DataGrabber as a Thread and want to wait for some interval
> of time in case there is a network failure that prevents read().
> I am not very sure how to implement this
>
> class DataGrabber(threading.Thread):
> def __init__(self,url):
> threading.Thread.__init__(self)
> self.url=url
> def run(self):
> data=self.get_page_data()
> process_data(data)
>
> def get_page_data():
> try:
> f=urllib.urlopen(self.url)
> data=f.read(1024)
> except IOError:
> #wait for some time and try again
> time.sleep(120)
> data=self.get_page_data()
> return data
>
> Is this the way to  implement the part where the thread waits and
> reads the  data again? Will this handle network failures?Can somebody
> please help?

This only works if your page is always 1024 bytes long. Which I
doubt. So don't pass the 1024 to read.

Also, you need a loop to re-read the data. Like this:


for n in xrange(max_number_of_retries):
try:
f=urllib.urlopen(self.url)
data = f.read()
break # exist the loop if all
except IOError:
pass


self.process_data(data)


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


Re: if the else short form

2010-10-09 Thread Lawrence D'Oliveiro
In message , BartC wrote:

> "NevilleDNZ"  wrote in message
> news:ad9841df-49a1-4c1b-95d0-e76b72df6...@w9g2000prc.googlegroups.com...
>
>> In Algol68 this would be:
>> x:=(i|"One","Two","Three"|"None Of The Above")
> 
> The point is, the construction works well when the syntax fully supports
> it.

But note that Algol 68 doesn’t support explicit labels on the alternatives 
in a case expression or statement. That idea came later.
-- 
http://mail.python.org/mailman/listinfo/python-list


how to find difference in number of characters

2010-10-09 Thread harryos
hi
I am trying to write a compare method which takes two strings and find
how many characters have changed.


def compare_strings(s1,s2):
pass


text1="goat milk"
text2="cow milk"
print compare_strings(text1,text2)

This must give 3 ,since 3 characters are changed between strings.I was
advised to use levenshtein algorithm ..but then the matrix ops take a
long time for nontrivial strings of say 2 characters ..Can this
comparison be implemented using difflib module?..I am at a loss as to
how to implement this using difflib  .Is there some way I can get the
difference as a number ?
Can somebody help?
thanks
harry
-- 
http://mail.python.org/mailman/listinfo/python-list