Re: reference vs. name space question

2010-10-10 Thread Steven D'Aprano
On Sat, 09 Oct 2010 21:00:45 -0700, chad wrote:

> 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

In this case, you have asked Python to look up the name 'spam'. Since 
spam has never been bound to any object, (no assignment spam=something 
has occurred) the lookup fails and Python raises an exception.


 def foo(x, y):
> ...   pass
> ...


> Doesn't? 

Why should it fail? You haven't attempted to look up names x or y. You 
have executed a function definition statement, which creates a new 
function object taking two arguments named x and y. The names only exist 
in the function's local namespace, they can only be referenced from 
inside the function's local namespace, and since you haven't yet called 
the function, Python doesn't make any attempt to look up the names, and 
so there is no error.

If you do call the function, one of two things will happen:

(1) You do supply arguments for x and y, in which case the names will be 
bound and looking them up will succeed; or

(2) You don't supply arguments for x and/or y, and Python will raise 
TypeError before the function code is executed.



> 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.

If it did, it would make it a bit hard to write functions if you couldn't 
refer to formal parameters.



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


Re: Class-level variables - a scoping issue

2010-10-10 Thread Jonathan Gardner
On Oct 9, 10:30 pm, John Nagle  wrote:
>     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,))
> ...
> ...

You don't quite understand the intricacies of Pythonese yet. It's
really simple, which isn't a common feature of most programming
languages.

class T(object):
classvar = 1

means: Create a class which has an attribute "classvar" initialized to
1. Assign that class to "T".

"self.classvar" means: Lookup the value for 'classvar' in 'self',
visiting its class and parent classes if you can't find it.

"self.classvar = 2" means: Create or replace the attribute 'classvar'
of 'self' with 2.

Notice how the two meanings are quite different from each other?

Fetching an attribute means something different than assigning to it.
Python doesn't remember where the attribute came from when assigning---
it simply assigns at the top level.

If you wanted to, instead, change T's classvar, then you need to be
explicit about it and write "T.classvar = 2".
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Class-level variables - a scoping issue

2010-10-10 Thread Lawrence D'Oliveiro
In message <4cb14f8c$0$1627$742ec...@news.sonic.net>, John Nagle wrote:

> 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’d say there is definitely an inconsistency with the absoutely anal 
restrictions on global variables, i.e. the well-known sequence

... reference to globalvar ...
globalvar = newvalue

which triggers the error “UnboundLocalError: local variable 'globalvar' 
referenced before assignment”.

It seems to me the same principle, that of disallowing implicit overriding 
of a name from an outer scope with that from an inner one after the former 
has already been referenced, should be applied here as well.
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: if the else short form

2010-10-10 Thread Lawrence D'Oliveiro
In message
<45368e8d-3b4f-4380-974d-bf9cd5d68...@w9g2000prc.googlegroups.com>, 
NevilleDNZ wrote:

> 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 guess a list type wasn’t seen as primitive enough. Besides, what would you 
call the fields—would you use the LISP-traditional names of “car” and “cdr”? 
POP-2, another language from that time, used “hd” and “tl”, but these were 
actually functions that looked for fields named “front” and “back”, and 
interpreted their values in special ways. This allowed for “lazy 
evaluation”, which meant that list elements only took up storage when they 
were actually referenced.

As for DICT, I think table lookups were still a sufficiently novel concept 
for people to disagree on how they should best be implemented.
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: script in Linux vs Windows

2010-10-10 Thread Dave Angel

 On 2:59 PM, Lawrence D'Oliveiro wrote:

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?


Windows OS itself does not transform any newlines at all.

However, each version of Windows includes several versions of the Visual 
Studio runtime dll's, and applications which use those libraries may 
*think* the extra transformations those libraries do is part of Windows 
itself.


For example, in my XP system I see msvcrt.dll, msvcrt20.dll, and 
msvcrt40.dll in the c:\windows\system32 directory.  These are part of 
various releases of Visual Studio (or of the C compiler in it).  There 
are many other such dll's, but I'm concentrating on the one containing 
open(), and write(), etc.


The open() call is in Visual Studio's dll, while the CreateFileA is in 
Windows itself.  The latter does no transformation, and nothing stops 
Python from calling that directly.  However, except for the "t" versus 
"b", the open() call more closely matches the Unix convention.


The rest is convention.  Many applications included with Windows create 
text files with the 0d0a convention, and expect files and streams to 
follow that convention.  That includes CMD.exe, Notepad, etc.  If you 
don't use any of these, Windows wouldn't care if you always used 0a for 
a line separator.


DaveA


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


Re: hashkey/digest for a complex object

2010-10-10 Thread Hrvoje Niksic
Steven D'Aprano  writes:

> 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

This is a counter-example for the (invalid) premise that hash(x) == x,
but not for the invariant of hash(hash(x)) == hash(x).

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

> 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?

I would take it to mean the type 2.x calls 'int', i.e. fixed-width
integer type.
-- 
http://mail.python.org/mailman/listinfo/python-list


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

2010-10-10 Thread David Kastrup
Sean McAfee  writes:

> 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.

Your headers state:

User-Agent: Gnus/5.11 (Gnus v5.11) Emacs/22.3 (darwin)

That's an old version of Emacs, more than 2 years old.  23.1 has been
released more than a year ago.  The current version is 23.2.

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


Re: hashkey/digest for a complex object

2010-10-10 Thread Arnaud Delobelle
Steven D'Aprano  writes:

> 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.

I can only see one counterexample, (-1).  2**64 is of type 'long' in 2.X
on your machine (or, to be pedantic, 2.x where x >= 2).  And, in fact,
(-1) is the only int such that hash(x) != x.

In can only guess that (-1) is a value that has a special meaning when
hashing.  Try this (Python 2.6):

>>> class A(object):
... def __hash__(self): return -1
... 
>>> a = A()
>>> hash(a)
-2


> 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?

Either.

> [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.

I didn't forget, but what bearing does it have on this particular issue?
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: "Strong typing vs. strong testing"

2010-10-10 Thread Lie Ryan
On 10/01/10 00:24, TheFlyingDutchman wrote:
> 
>>
>>> If I had to choose between "blow up" or "invalid answer" I would pick
>>> "invalid answer".
>>
>> there are some application domains where neither option would be
>> viewed as a satisfactory error handling strategy. Fly-by-wire, petro-
>> chemicals, nuclear power generation. Hell you'd expect better than
>> this from your phone!
>>
> 
> I wasn't speaking generally, just in the case of which of only two
> choices RG's code should be referred to - "blowing up" or "giving an
> invalid answer".

I can make any application never blows up. Just put this in the main()
of every application you write:

while True:
try:
... program goes here ...
except:
pass

Done. Program never halted execution, it never blows up. My program is
bug free, heck why do thousands of computer scientists can't figure out
that such a simple construct will fix all bugs once and for all?

> I think error handling in personal computer and website software has
> improved over the years but there is still some room for improvement
> as you will still get error messages that don't tell you something you
> can relay to tech support more than that an error occurred or that
> some operation can't be performed.

I think the ideal error message tells the user exactly what's wrong and
how to fix the problem. A generic error message like "This program
encountered a problem, and need to close." is as useless as an error
message could be.

> But I worked with programmers doing in-house software who were
> incredibly turned off by exception handling in C++. I thought that
> meant that they preferred to return and check error codes from
> functions as they had done in C, and for some of them it did seem to
> mean that. 

Introduce them to RAII (Resource Acquisition Is Initialization), that's
a superior alternative to exception handling in C++.

> But for others it seemed that they didn't want to
> anticipate errors at all ("that file is always gonna be there!"). I
> read a Java book by Deitel and Deitel and they pointed out what might
> have lead to that attitude - the homework and test solutions in
> college usually didn't require much if any error handling - the
> student could assume files were present, data was all there and in the
> format expected, user input was valid and complete, etc.

Personally, the amount of error message I'd design into the program
depends on the expected level of proficiency of the expected users. If
I'm developing for an in-house software, and most of the users of my
program are expected to be experts or have necessary training, I'd be
less inclined to include sophisticated error checking in favor of
simplicity (but I usually work in high-level language, where errors like
non-existent file will throw an exception instead of passing silently,
so take it with a grain of salt). If I'm designing software for a
general public though, I'd include multitudes of idiot checking.
-- 
http://mail.python.org/mailman/listinfo/python-list


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

2010-10-10 Thread Nobody
On Sat, 09 Oct 2010 15:45:42 -0700, Sean McAfee wrote:

>> 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 don't know about GNU Emacs, but XEmacs doesn't use Unicode internally,
it uses byte-strings with associated encodings. Some of us like it that
way, as converting to Unicode may not be reversible, and it's often
important to preserve exact byte sequences.

FWIW, I'd expect Ruby to have worse support for Unicode, as its creator is
Japanese. Unicode is still far more popular in locales which historically
used ASCII or "almost ASCII" (e.g. ISO-646-*, ISO-8859-*) encodings than
in locales which had to use a radically different encoding.

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


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

2010-10-10 Thread Steven D'Aprano
On Sun, 10 Oct 2010 11:34:02 +0200, David Kastrup wrote:
[unnecessary quoting removed]
> Your headers state:
> 
> User-Agent: Gnus/5.11 (Gnus v5.11) Emacs/22.3 (darwin)

Please stop spamming multiple newsgroups. I'm sure this is of great 
interest to the Emacs newsgroup, but not of Python.

Followups to /dev/null.

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


Re: Help with pointers when calling from python to C

2010-10-10 Thread Nobody
On Fri, 08 Oct 2010 18:02:15 +0200, Jonas H. wrote:

> On 10/08/2010 05:23 PM, Carolyn MacLeod wrote:
>> "How do I pass an integer by reference to a C function?"
> 
> That's impossible in pure Python. The only thing I can think of is a 
> wrapper in C.

I didn't see the original message, but if you're using ctypes, create a
c_int value and use byref():

i = c_int(42)
myfunc(byref(i))
print i

http://docs.python.org/library/ctypes.html#passing-pointers-or-passing-parameters-by-reference

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


Re: "Strong typing vs. strong testing"

2010-10-10 Thread Lie Ryan
On 10/02/10 20:04, Nick Keighley wrote:
>>> > > In a statically typed language, the of-the-wrong-type is something which
>>> > > can, by definition, be caught at compile time.
>> >
>> > Any time something is true "by definition" that is an indication that
>> > it's not a particularly useful fact.
> I'm not sure I agree. On occaision knowing something is true-by-
> definition is very useful!

Something that is true by definition is just as useful as saying: "my
program is correct, by definition, because my requirement is what my
code is doing". It's a circular argument, your program requirement, for
which the program is supposed to be tested against, is the code itself;
so whatever undesirable behavior the program might have is parts of the
requirement, so the program is, by definition, bug free and it's user
expectation that's wrong.
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: "Strong typing vs. strong testing"

2010-10-10 Thread Lie Ryan
On 10/05/10 14:36, salil wrote:
> So, the programmer who
> specifically mentions "Int" in the signature of the function, is
> basically overriding this default behavior for specific reasons
> relevant to the application, for example, for performance. I think
> Haskell's way is the right. 

I agree that in general, what Haskell did is correct, and a perfectly
reasonable design decision. In terms of *correctness* though, mandatory
infinite precision is slightly more correct, as you can't have a
clueless programmer writing Int, because he read somewhere that it's
faster, and blowing up the program. In practice, this never matters, as
we don't expect to see a lot of clueless programmers.

> It is providing "safe behavior"  as
> default and at the same time treating programmer as adults, at least
> in this case.
> 
> I think dynamic languages are attractive because they make programs
> less verbose. But, statically typed languages with type inference
> (Haskell, OCaML, Scala, F#) is a very good compromise because they
> offer both type safety and succinctness. And when we need algorithms
> that should work the same independent of types, Haskell has
> typeclasses which are pretty intuitive, unlike the horrible C++
> templates.

Agreed. Dynamic language and implicit static typing are two very good
compromises for writing succinct codes. Implicit static typing lends to
better type safety, while dynamic typing lends to better reflection and
meta-programming. Both while preserving code succinctness.


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


Re: Class-level variables - a scoping issue

2010-10-10 Thread Chris Torek
In article <4cb14f8c$0$1627$742ec...@news.sonic.net>
John Nagle   wrote:
>Here's an obscure bit of Python semantics which
>is close to being a bug:

[assigning to instance of class creates an attribute within
the instance, thus obscuring the class-level version of the
attribute]

This is sort of a feature, but one I have been reluctant to use:
you can define "default values" for instances within the class,
and only write instance-specific values into instances as needed.
This would save space in various cases, for instance.

> 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.  

It's not really clear to me how one would distinguish between
"accidental" and "deliberate" creation of these variables,
syntactically speaking.

If you want direct, guaranteed access to the class-specific variable,
using __class__ is perhaps the Best Way right now:

>>> class K:
... x = 42
... def __init__(self): pass
... 
>>> inst = K()
>>> inst.x # just to show that we're getting K.x here
42
>>> inst.x = 'hah'
>>> inst.x
'hah'
>>> inst.__class__.x
42
>>>

One could borrow the "nonlocal" keyword to mean "I know that
there is potential confusion here between instance-specific
attribute and class-level attribute", but the implication seems
backwards:

nonlocal self.foo

implies that you want self.foo to be shorthand for self.__class__.foo,
not that you know that self.__class__.foo exists but you *don't*
want to use that.

If Python had explicit local variable declarations, then:

local self.foo

would be closer to the implied semantics here.

As it is, I think Python gets this pretty much right, and if you
think this is more a bug than a feature, you can always insert
assert statements in key locations, e.g.:

assert 'foo' not in inst.__class__.__dict__, \
'overwriting class var "foo"'

(you can even make that a function using introspection, although
it could get pretty hairy).
-- 
In-Real-Life: Chris Torek, Wind River Systems
Salt Lake City, UT, USA (40°39.22'N, 111°50.29'W)  +1 801 277 2603
email: gmail (figure it out)  http://web.torek.net/torek/index.html
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: "Strong typing vs. strong testing"

2010-10-10 Thread Lie Ryan
On 10/01/10 23:56, BartC wrote:
> 
> "Pascal J. Bourguignon"  wrote in message
> news:87zkuyjawh@kuiper.lan.informatimago.com...
>> "BartC"  writes:
>>
>>> "Pascal J. Bourguignon"  wrote in message
> 
 When Intel will realize that 99% of its users are running VM
>>>
>>> Which one?
>>
>> Any implementation of a controlled environment is a virtual machine.
>> Sometimes it is explicitely defined, such as in clisp, parot or jvm, but
>> more often it is implicit, such as in sbcl, or worse, developed in an
>> ad-hoc way in applications (eg. written in C++).
> 
> But if you had to implement a VM directly in hardware, which one (of the
> several varieties) would you choose?

Virtual Machine in Hardware... isn't that a contradiction?

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


Compiling xpython: No rule to make target `runathana.py', needed by `frozen/frozen.c'.

2010-10-10 Thread est
I found an interesting project, it's a single 4.7 MB executable with
stdlib+PIL+pdf r/w+magpy+wxpython environment

But when compiling following the instructions

http://www.xpython.org/#compiling

there is an error

make: *** No rule to make target `runathana.py', needed by `frozen/
frozen.c'.  Stop.

I am using Debian 4.0 32bit machine
-- 
http://mail.python.org/mailman/listinfo/python-list


SQLObject 0.14.0

2010-10-10 Thread Oleg Broytman
Hello!

I'm pleased to announce version 0.14.0, the first stable release of branch
0.14 of SQLObject.


What is SQLObject
=

SQLObject is an object-relational mapper.  Your database tables are described
as classes, and rows are instances of those classes.  SQLObject is meant to be
easy to use and quick to get started with.

SQLObject supports a number of backends: MySQL, PostgreSQL, SQLite,
Firebird, Sybase, MSSQL and MaxDB (also known as SAPDB).


Where is SQLObject
==

Site:
http://sqlobject.org

Development:
http://sqlobject.org/devel/

Mailing list:
https://lists.sourceforge.net/mailman/listinfo/sqlobject-discuss

Archives:
http://news.gmane.org/gmane.comp.python.sqlobject

Download:
http://pypi.python.org/pypi/SQLObject/0.14.0

News and changes:
http://sqlobject.org/News.html


What's New
==

News since 0.13
---

Features & Interface


* The lists of columns/indices/joins are now sorted according to the order
  of declaration.

* ``validator2`` was added to all columns; it is inserted at the beginning
  of the list of validators, i.e. its ``from_python()`` method is called
  first, ``to_python()`` is called last, after all validators in the list.

* SQLiteConnection's parameter ``use_table_info`` became boolean with default
  value True; this means the default schema parser is now based on ``PRAGMA
  table_info()``.

* Major API change: attribute 'dirty' was moved to sqlmeta.

For a more complete list, please see the news:
http://sqlobject.org/News.html

Oleg.
-- 
 Oleg Broytmanhttp://phd.pp.ru/p...@phd.pp.ru
   Programmers don't die, they just GOSUB without RETURN.
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: how to handle network failures

2010-10-10 Thread Jorgen Grahn
On Fri, 2010-10-08, harryos wrote:
> 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?

You are using TCP sockets. When you get an error on one of those, the
TCP connection is dead (except for a few special cases like EAGAIN,
EINTR).

But you also risk *not* getting told and hanging forever, or anyway
for far longer than your application is likely to want to wait. For
example if the peer host is suddenly disconnected from the network --
TCP will keep trying, in case a connection suddenly reappears much
later.

Try provoking that situation and see what happens.

/Jorgen

-- 
  // Jorgen GrahnO  o   .
-- 
http://mail.python.org/mailman/listinfo/python-list


Institutional careers in Management work.

2010-10-10 Thread gaurav
Careers recruitments in Management work. Employments in Management
careers. http://managementjobs.webs.com/pm.htm &
http://jobscore.webs.com/executivemanager.htm

Rush for career in government and computer jobs potential revenue.
http://rojgars1.webs.com/gov.htmhttp://rojgars.webs.com/bankingjobs.htm
-- 
http://mail.python.org/mailman/listinfo/python-list


Documentation on the Tkinter files in Python's tcl folder?

2010-10-10 Thread python
Is there some sort of documentation regarding the files in
Python's tcl folder? I've been googling this topic without
finding such a source.

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


Re: hashkey/digest for a complex object

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

>Steven D'Aprano  writes:

>> 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.

(Infinite???)

>And, in fact,
>(-1) is the only int such that hash(x) != x.

Arnaud, how did you determine that -1 is the only such int?  I
can't imagine any other approach other than a brute-force check of
all ints...  When I tried this I ran into unforeseen limitations
in xrange, etc.  It's all very doable, but still, it would take at
least about 3 hours on my laptop.

>In can only guess that (-1) is a value that has a special meaning when
>hashing.  Try this (Python 2.6):

 class A(object):
>... def __hash__(self): return -1
>... 
 a = A()
 hash(a)
>-2

Very cool.

BTW, thank you for the explanation in your previous post.  It makes
a lot of sense.  I find it interesting (as Hrvoje pointed out) that
the hash function is (or appears to be) idempotent on integers
(long or not), even though it is not the identity on the integers.
Thanks to Steven for the counterexamples to show the latter.  I've
learned tons from this exchange.

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


Re: hashkey/digest for a complex object

2010-10-10 Thread Steven D'Aprano
On Sun, 10 Oct 2010 11:06:00 +0100, Arnaud Delobelle wrote:

> Steven D'Aprano  writes:
> 
>> 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.
> 
> I can only see one counterexample, (-1).  2**64 is of type 'long' in 2.X
> on your machine (or, to be pedantic, 2.x where x >= 2).  And, in fact,
> (-1) is the only int such that hash(x) != x.

Fair point. I was mistaken.

I had the impression that the integration between ints and longs since 
Python 2.2 was more extensive than it actually is. I made the above 
comments based on the idea that since Python 2.2, longs are a subclass of 
ints, e.g. that isinstance(2**64, int) would return True. Turns out that 
I'm wrong, in which case I agree with you that -1 is the only int counter-
example.

As an aside, this makes me glad that I have continued writing 
isinstance(x, (int, long)) in my code, even though I "knew" it was 
unnecessary. Not the first time, and probably not the last, that a "this 
can never happen" test saved the day.


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


Re: hashkey/digest for a complex object

2010-10-10 Thread Steven D'Aprano
On Sun, 10 Oct 2010 13:49:09 +, kj wrote:

>>> to give only two of an infinite number of counter-examples.
> 
> (Infinite???)

I was mistaken. Given Arnaud's specification that we look only at the 
Python 2.x ints, I believe that there is only one counter-example, namely 
-1.

However, in Python 3.x I would be correct. The reasoning is a simple 
application of the pigeon-hole principle. Since hash(n) is limited to 
returning a finite number of distinct results, but there are an infinite 
number of Python 3 ints, it follows that there must be an infinite number 
of collisions.


>>And, in fact,
>>(-1) is the only int such that hash(x) != x.
> 
> Arnaud, how did you determine that -1 is the only such int?  I can't
> imagine any other approach other than a brute-force check of all ints...

Reading the source code is also a good approach.

I don't read C very well, but even I can work out what this is doing:

static long
int_hash(PyIntObject *v)
{
/* XXX If this is changed, you also need to change the way
   Python's long, float and complex types are hashed. */
long x = v -> ob_ival;
if (x == -1)
x = -2;
return x;
}


(from intobject.c in Python 2.6.1). It takes a Python int object, 
extracts the value of it as a C long, returns -2 if the value is -1 
otherwise returns the value.


>  When I tried this I ran into unforeseen limitations in xrange, etc. 
> It's all very doable, but still, it would take at least about 3 hours on
> my laptop.


What are you doing? This takes less than 20 seconds to test up to 2**24:

import time
def test(n):
t = time.time()
assert hash(0) == 0
assert hash(1) == 1
assert hash(-1) == -2
for i in xrange(2, n):
assert hash(i) == i, "failed %d" % i
assert hash(-i) == -i, "failed %d" % -i
t = time.time() - t
return t


>>> test(2**24)
18.076412916183472

Since 2**31 is 2**7 = 128 times bigger, I estimate that testing 
everything up to 2**31 should take less than 45 minutes. And my PC is not 
exactly the fastest machine on the block.



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


Re: hashkey/digest for a complex object

2010-10-10 Thread Arnaud Delobelle
kj  writes:

> In <87hbgu8irb@gmail.com> Arnaud Delobelle  writes:

[...]

>>And, in fact,
>>(-1) is the only int such that hash(x) != x.
>
> Arnaud, how did you determine that -1 is the only such int?  I
> can't imagine any other approach other than a brute-force check of
> all ints...  When I tried this I ran into unforeseen limitations
> in xrange, etc.  It's all very doable, but still, it would take at
> least about 3 hours on my laptop.

I looked at the source, namely the get_hash() function in the
intobject.c file in the 2.x source, and the get_hash() function in the
longobject.c file in the 3.x source.

>>In can only guess that (-1) is a value that has a special meaning when
>>hashing.  Try this (Python 2.6):
>
> class A(object):
>>... def __hash__(self): return -1
>>... 
> a = A()
> hash(a)
>>-2
>
> Very cool.
>
> BTW, thank you for the explanation in your previous post.  It makes
> a lot of sense.  I find it interesting (as Hrvoje pointed out) that
> the hash function is (or appears to be) idempotent on integers
> (long or not), even though it is not the identity on the integers.
> Thanks to Steven for the counterexamples to show the latter.  I've
> learned tons from this exchange.

It still seems to hold that hash() is idempotent on all objects.

I have learnt too that hash(-1) is not (-1), and that it seems that a
hash value is not allowed to be (-1).  There is one thing left to find
out.  Why can't it be (-1)?  Maybe looking at the source for the hash()
builtin would yield a clue, or maybe someone who knows would be able to
tell us?

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


Re: hashkey/digest for a complex object

2010-10-10 Thread Hrvoje Niksic
Arnaud Delobelle  writes:

> I have learnt too that hash(-1) is not (-1), and that it seems that a
> hash value is not allowed to be (-1).  There is one thing left to find
> out.  Why can't it be (-1)?

Because -1 has a special meaning in the C function equivalent to
Python's hash().  PyObject_Hash returning -1 means that an exception was
raised somewhere inside the object's __hash__ method.  For that reason
hash functions that really return -1 must change that to another value,
and -2 is as good a replacement as any.

This is documented in
http://docs.python.org/c-api/object.html?highlight=pyobject_hash#PyObject_Hash
-- 
http://mail.python.org/mailman/listinfo/python-list


UTF-8 problem encoding and decoding in Python3

2010-10-10 Thread hidura
Hello everybody i am trying to encode a file string of an upload file and i  
am facing some problems with the first part of the file. When i open  
directly and try to decode the file the error is this:  
`UnicodeDecodeError: 'utf8' codec can't decode byte 0xff in position 0:  
unexpected code byte` here is the first part of the file:

`\xff\xd8\xff\xe0\x00\x10JFIF\x00\x01\x01\x00\x00\x01\x00\x01\x00\x00\xff\xdb\x00C\x00\x08\x06\x06\x07\x06\x05\x08\x07\x07\x07\t\t\x08\n\x0c\x14\r\x0c\x0b\x0b\x0c`
but when i try to encode the file in the server the encode change the parts  
of the file and the result is  
this:`\xc3\xbf\xc3\x98\xc3\xbf\xc3\xa0\x00\x10JFIF` without say that the  
file doesn 't save correctly.


Any ideas?
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Many newbie questions regarding python

2010-10-10 Thread Peter Pearson
On Sat, 09 Oct 2010 19:30:16 -0700, Ethan Furman  wrote:
> Steven D'Aprano wrote:
[snip]
>> 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?

Well, here's a worked example of Steven D's code (Python 2.5.2):

>>> def func(arg, count):
...   L = [arg for i in range(count)]
...   process(L, some_function)
... 
>>> def process(L, v):
...   for item in L:
... v(item)
... 
>>> def some_function(x):
...   x.append(1)
...   print x
... 
>>> func([], 3)
[1]
[1, 1]
[1, 1, 1]
>>> 

Is that the output you expected?  Probably not:  the unwary
reader (including me, not too long ago) expects that

  L = [arg for i in range(count)]

will be equivalent to

  L = [[], [], []]

but it's not, because the three elements in the first L are three
references to the *same* list.  Observe:

>>> arg = []
>>> L = [arg for i in range(3)]
>>> L
[[], [], []]
>>> L[0].append(1)
>>> L
[[1], [1], [1]]

... as opposed to ...

>>> L = [ [] for i in range(3)]
>>> L
[[], [], []]
>>> L[0].append(1)
>>> L
[[1], [], []]

-- 
To email me, substitute nowhere->spamcop, invalid->net.
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: UTF-8 problem encoding and decoding in Python3

2010-10-10 Thread Chris Rebert
On Sun, Oct 10, 2010 at 10:25 AM,   wrote:
> Hello everybody i am trying to encode a file string of an upload file and i
> am facing some problems with the first part of the file. When i open
> directly and try to decode the file the error is this: `UnicodeDecodeError:
> 'utf8' codec can't decode byte 0xff in position 0: unexpected code byte`
> here is the first part of the file:
> `\xff\xd8\xff\xe0\x00\x10JFIF\x00\x01\x01\x00\x00\x01\x00\x01\x00\x00\xff\xdb\x00C\x00\x08\x06\x06\x07\x06\x05\x08\x07\x07\x07\t\t\x08\n\x0c\x14\r\x0c\x0b\x0b\x0c`
> but when i try to encode the file in the server the encode change the parts
> of the file and the result is
> this:`\xc3\xbf\xc3\x98\xc3\xbf\xc3\xa0\x00\x10JFIF` without say that the
> file doesn 't save correctly.
>
> Any ideas?

Judging by the "\xff\xe0" and "JFIF", you're dealing with a JFIF file,
which is binary, and thus you shouldn't be encoding/decoding it in the
first place. Just write the byte string directly to a file (being sure
to include "b" in the `mode` argument to open() when opening the file)
without any further processing.

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


Re: Class-level variables - a scoping issue

2010-10-10 Thread John Nagle

On 10/10/2010 12:53 AM, Lawrence D'Oliveiro wrote:

In message<4cb14f8c$0$1627$742ec...@news.sonic.net>, John Nagle wrote:


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’d say there is definitely an inconsistency with the absoutely anal
restrictions on global variables, i.e. the well-known sequence

 ... reference to globalvar ...
 globalvar = newvalue

which triggers the error “UnboundLocalError: local variable 'globalvar'
referenced before assignment”.

It seems to me the same principle, that of disallowing implicit overriding
of a name from an outer scope with that from an inner one after the former
has already been referenced, should be applied here as well.


Right.  That's what I'm getting at.

I understand how the current semantics fall out of the obvious
implementation.  But I don't see those semantics as particularly
desirable.  The obvious semantics for globals are similar, but
that case is so error-prone that it was made an error.

(If you want default values for an instance, you define them
in __init__, not as class-level attributes.)

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


Re: "Strong typing vs. strong testing"

2010-10-10 Thread Martin Gregorie
On Sun, 10 Oct 2010 21:38:11 +1100, Lie Ryan wrote:

> 
> Virtual Machine in Hardware... isn't that a contradiction?
>
Nope. Several mainframes did that. 

Two that I knew well were both British - the ICL 1900 and 2900. The 
Burroughs x700 series also used hardware virtualisation. Both Burroughs 
and 2900 series of machines could even run different addressing  and 
virtual hardware architectures, e.g. word-addressing vs. byte addressing, 
in each virtual machine. 

The 1900's  'hardware' registers, PC, CCR, etc were the first few words 
in each program's address space. That was late 60s tech, so scarcely a 
new concept nowadays. 

The ICL 2900 and, by extension Multics, since the 2900 hardware's process 
protection scheme was swiped from Multics, used a different approach - 
again hardware-implemented virtual per-process registers but in addition 
'rings of protection' which meant the OS could  affect your code but you 
couldn't touch it and the OS in turn couldn't touch the hardware drivers 
etc.


-- 
martin@   | Martin Gregorie
gregorie. | Essex, UK
org   |
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: UTF-8 problem encoding and decoding in Python3

2010-10-10 Thread Almar Klein
Hi,

please tell us what you are trying to do. Encoding (with UTF-8) is a method
to convert a Unicode string to a sequence of bytes. Decoding does the
reverse.


When i open
> directly and try to decode the file the error is this: `UnicodeDecodeError:
> 'utf8' codec can't decode byte 0xff in position 0: unexpected code byte`
>

This means the series of byte that you are trying to convert to a string is
not valid UTF-8. It can't be, because it would not contain 0xff or 0xfe
bytes.


but when i try to encode the file in the server the encode change the parts
> of the file and the result is
> this:`\xc3\xbf\xc3\x98\xc3\xbf\xc3\xa0\x00\x10JFIF` without say that the
>

So here you *encode* the file, not decoding it.

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


Re: hashkey/digest for a complex object

2010-10-10 Thread kj
In <4cb1dc9a$0$29970$c3e8da3$54964...@news.astraweb.com> Steven D'Aprano 
 writes:

>Reading the source code is also a good approach.

Every time I have attempted to answer a question by looking at the
Python C source, all I've achieved was wasting time, sometimes a
*lot* of time, so by now I've developed what can only be described
as a phobia to it.  I probably need professional help at this point.

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


Re: UTF-8 problem encoding and decoding in Python3

2010-10-10 Thread Hidura
I try to encode a binary file what was upload to a server and is
extract from the wsgi.input of the environ and comes as an unicode
string.

2010/10/10, Almar Klein :
> Hi,
>
> please tell us what you are trying to do. Encoding (with UTF-8) is a method
> to convert a Unicode string to a sequence of bytes. Decoding does the
> reverse.
>
>
> When i open
>> directly and try to decode the file the error is this:
>> `UnicodeDecodeError:
>> 'utf8' codec can't decode byte 0xff in position 0: unexpected code byte`
>>
>
> This means the series of byte that you are trying to convert to a string is
> not valid UTF-8. It can't be, because it would not contain 0xff or 0xfe
> bytes.
>
>
> but when i try to encode the file in the server the encode change the parts
>> of the file and the result is
>> this:`\xc3\xbf\xc3\x98\xc3\xbf\xc3\xa0\x00\x10JFIF` without say that the
>>
>
> So here you *encode* the file, not decoding it.
>
>   Almar
>

-- 
Enviado desde mi dispositivo móvil

Diego I. Hidalgo D.
-- 
http://mail.python.org/mailman/listinfo/python-list


cp936 uses gbk codec, doesn't decode `\x80` as U+20AC EURO SIGN

2010-10-10 Thread John Machin

|>>> '\x80'.decode('cp936')
Traceback (most recent call last):
  File "", line 1, in 
UnicodeDecodeError: 'gbk' codec can't decode byte 0x80
 in position 0: incomplete multibyte sequence

However:

Retrieved 2010-10-10 from
http://www.unicode.org/Public
/MAPPINGS/VENDORS/MICSFT/WINDOWS/CP936.TXT

#Name: cp936 to Unicode table
#Unicode version: 2.0
#Table version: 2.01
#Table format:  Format A
#Date:  1/7/2000
#
#Contact:   shawn.ste...@microsoft.com
...
0x7F0x007F  #DELETE
0x800x20AC  #EURO SIGN
0x81#DBCS LEAD BYTE

Retrieved 2010-10-10 from
http://msdn.microsoft.com/en-us/goglobal/cc305153.aspx

Windows Codepage 936
[pictorial mapping; shows 80 mapping to 20AC]

Retrieved 2010-10-10 from
http://demo.icu-project.org
/icu-bin/convexp?conv=windows-936-2000&s=ALL

[pictorial mapping for converter
"windows-936-2000" with
aliases including GBK, CP936, MS936;
shows 80 mapping to 20AC]

So Microsoft appears to think that
cp936 includes the euro,
and the ICU project seem to think that GBK and cp936
both include the euro.

A couple of questions:

Is this a bug or a shrug?

Where can one find the mapping tables
from which the various CJK codecs are derived?




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


Re: Design: Module interface vs. library interface

2010-10-10 Thread Aahz
In article <4c8b8123.80...@sschwarzer.net>,
Stefan Schwarzer   wrote:
>
>Which approach would you prefer and why? Or some other approach? Would
>you use a different approach if the library-internal class was named
>`_FTPFile` instead of `FTPFile`?

If it's not a public class, it should have been named `_FTPFile`.  When
your class isn't public, it doesn't matter much exactly how you expose it
internally.  (I essentially never use __all__ myself, but I don't write
libraries for public consumption.)
-- 
Aahz (a...@pythoncraft.com)   <*> http://www.pythoncraft.com/

"If you think it's expensive to hire a professional to do the job, wait
until you hire an amateur."  --Red Adair
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: open file on mac

2010-10-10 Thread tinauser
On Oct 10, 6:54 am, Lawrence D'Oliveiro  wrote:
> 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.

ok,then could you tell me if there is a way to have the folder of the
module where the "open" command is?
this module is called from C, and user could install the python code
folder (where both the module and the file are) where he wants.

thank you in advance
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Control webbrowser from Python script

2010-10-10 Thread luca
On Oct 9, 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

I'm using funkload and it is awesome!

http://funkload.nuxeo.org/

FunkLoad is a functional and load web tester, written in Python, whose
main use cases are:

Functional testing of web projects, and thus regression testing as
well.
Performance testing: by loading the web application and monitoring
your servers it helps you to pinpoint bottlenecks, giving a detailed
report of performance measurement.
Load testing tool to expose bugs that do not surface in cursory
testing, like volume testing or longevity testing.
Stress testing tool to overwhelm the web application resources and
test the application recoverability.
Writing web agents by scripting any web repetitive task, like checking
if a site is alive.
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: open file on mac

2010-10-10 Thread Benjamin Kaplan
On Sun, Oct 10, 2010 at 5:29 PM, tinauser  wrote:
> On Oct 10, 6:54 am, Lawrence D'Oliveiro  central.gen.new_zealand> wrote:
>> 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.
>
> ok,then could you tell me if there is a way to have the folder of the
> module where the "open" command is?
> this module is called from C, and user could install the python code
> folder (where both the module and the file are) where he wants.
>

open uses the current working directory, which is available through
the os.getcwd() function in Python.
If you want to use a path relative to the current module, you can use
that module's __file__ attribute.
os.path.dirname(os.path.abspath(__file__))
will give you the absolute path to the directory with the current module in it.

So if you want to open the foo.txt file in the same directory as the
module, you use
os.path.join(os.path.dirname(__file__), 'foo.txt')

> thank you in advance
> --
> http://mail.python.org/mailman/listinfo/python-list
>
-- 
http://mail.python.org/mailman/listinfo/python-list


Reading Outlook .msg file using Python

2010-10-10 Thread John Henry
Hello all:

I have a need to read .msg files exported from Outlook.  Google search
came out with a few very old posts about the topic but nothing really
useful.  The email module in Python is no help - everything comes back
blank and it can't even see if there are attachments.  Did find a Java
library to do the job and I suppose when push come to shove, I would
have to learn Jython and see if it can invoke the Java library.  But
before I do that, can somebody point me to a Python/COM solution?

I don't need to gain access to Exchange or Outlook.  I just need to
read the .msg file and extract information + attachments from it.

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


Re: Class-level variables - a scoping issue

2010-10-10 Thread John Posner

On 10/10/2010 3:07 PM, John Nagle wrote:


I understand how the current semantics fall out of the obvious
implementation. But I don't see those semantics as particularly
desirable. The obvious semantics for globals are similar, but
that case is so error-prone that it was made an error.


Nicely stated.


(If you want default values for an instance, you define them
in __init__, not as class-level attributes.)


Since it's unlikely that the language will change, perhaps a naming 
convention would help. I'm not sure I like this myself, but ...


Class attributes are often used as "class constants", so how about 
naming them with UPPERCASE names, like other constants? When you choose 
to override one of these constants, like this:


   self.EGGS = 4

... the awkward looks of the statement serve as a hint that something 
special is happening.


#
class SpamMeal:
EGGS = 2

def __init__(self, egg_count=None):
if egg_count:
self.EGGS = egg_count

def Report(self):
print "This meal includes %d eggs." % self.EGGS

meal = SpamMeal()
meal.Report()# "This meal includes 2 eggs."

meal = SpamMeal(3)
meal.Report()# "This meal includes 3 eggs."

meal = SpamMeal()
meal.EGGS = 4
meal.Report()# "This meal includes 4 eggs."
#

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


Re: if the else short form

2010-10-10 Thread Hrvoje Niksic
Antoon Pardon  writes:

> Personaly I don't see a reason to declare in advance that someone
> who wants to treat "True" differently from non-zero numbers or
> non-empty sequences and does so by a test like:
>
>   if var == Trueorif var is True
>
> to have written incorrect code.

I wouldn't call it a priori incorrect, but breaking well-established
idioms is like a SHOULD NOT thing in RFC's; you can do that, but you'd
better have a really good reason.

In the case of the gtk code OP quoted, I highly doubt that the author
wanted to special-case True in the way you describe.  It is much more
likely that he made a mistake which happened to work because he never
tested the code with a true value that evalues != 1.
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Light on Dark screen for Spe or Pyscripter

2010-10-10 Thread PyScripter
On Oct 10, 7:48 am, flebber  wrote:
> 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.

In Pyscripter try View, Themes, EOS
Further dark theme at http://www.mytreedb.com/downloads/m-subcat/a-skins.html
(you need to download the archive and place the skin file at %APPDATA%/
pyscripter/skins)

Dark hihglighters at http://code.google.com/p/pyscripter/wiki/Customization
(download and import into PyScripter via View, Options, Import/Export)

Finally you can create your own skin using the skin editor at
http://www.silverpointdevelopment.com/skineditor/index.htm


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


Re: Class-level variables - a scoping issue

2010-10-10 Thread Steven D'Aprano
On Sun, 10 Oct 2010 18:14:33 -0400, John Posner wrote:

> Class attributes are often used as "class constants", so how about
> naming them with UPPERCASE names, like other constants? When you choose
> to override one of these constants, like this:
> 
> self.EGGS = 4

Er what?

If self.EGGS is meant as a constant, you shouldn't be re-assigning to it 
*ever*.


> ... the awkward looks of the statement serve as a hint that something
> special is happening.

I disagree that anything special is happening. The current behaviour is, 
to my mind, the correct behaviour.


> #
> class SpamMeal:
>  EGGS = 2
> 
>  def __init__(self, egg_count=None):
>  if egg_count:
>  self.EGGS = egg_count
> 
>  def Report(self):
>  print "This meal includes %d eggs." % self.EGGS
> 
> meal = SpamMeal()
> meal.Report()# "This meal includes 2 eggs."
> 
> meal = SpamMeal(3)
> meal.Report()# "This meal includes 3 eggs."
> 
> meal = SpamMeal()
> meal.EGGS = 4
> meal.Report()# "This meal includes 4 eggs."
#


Apart from the ugliness of the attribute name, this does not differ in 
the slightest from the current behaviour. How could it? Merely changing 
the attribute name "eggs" to "NAME" can't change the behaviour. Your 
SpamMeal instances still default to 2 eggs, assigning to instance.EGGS 
still creates an instance attribute while leaving SpamMeal.EGGS 
untouched, and it is still necessary to assign to SpamMeal.EGGS in order 
to globally change all instances (that don't already have their own EGGS 
attribute).

Since nothing has changed, what's the point? What exactly is the 
surprising behaviour that you are trying to flag?



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


Re: if the else short form

2010-10-10 Thread NevilleDNZ
On Oct 10, 6:02 pm, Lawrence D'Oliveiro  wrote:
> As for DICT, I think table lookups were still a sufficiently novel concept
> for people to disagree on how they should best be implemented.

I then stumbled over this paper:
Title: List processing in Algol 68 - V. J. Rayward-Smith
International Journal of Mathematical Education in Science and
Technology, 1464-5211, Volume 17, Issue 2, 1986, Pages 239 – 245
* http://www.informaworld.com/index/746867626.pdf - PayWall $30.00
> Abstract
> An Algol 68 implementation of a LISP-like list processing package
> is described. Emphasis is placed upon the importance of Algol 68
> as a teaching tool and upon the need for proving the correctness
> of the programs.

Not having LIST and DICT as part of the base language would make sense
if user contributions were encouraged.  After all that would make way
for evolving the base/primitive language by selecting the most
successful contributions.  IMHO this is where python modules have been
a big success story as it helps time proof the language by allowing
the language to embrace new technologies as they establish themselves
in the market place.

c.f. Anti Gravity: http://xkcd.com/353/

Enjoy
NevilleDNZ
--
To download Linux's Algol68 Compiler, Interpreter & Runtime:
* http://sourceforge.net/projects/algol68/files
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Light on Dark screen for Spe or Pyscripter

2010-10-10 Thread PyScripter
On Oct 11, 1:51 am, PyScripter  wrote:
> In Pyscripter try View, Themes, EOS
> Further dark theme athttp://www.mytreedb.com/downloads/m-subcat/a-skins.html
> (you need to download the archive and place the skin file at %APPDATA%/
> pyscripter/skins)
>
> Dark hihglighters athttp://code.google.com/p/pyscripter/wiki/Customization
> (download and import into PyScripter via View, Options, Import/Export)
>
> Finally you can create your own skin using the skin editor 
> athttp://www.silverpointdevelopment.com/skineditor/index.htm

See http://code.google.com/p/pyscripter/wiki/Screenshot9 for a dark
look example
-- 
http://mail.python.org/mailman/listinfo/python-list


Great career in Management

2010-10-10 Thread gaurav
Work from home genuine bulk data entry, home and government jobs
sources. http://rojgars1.webs.com/gov.htm
http://rojgars.webs.com/bankingjobs.htm

Easy Management careers for you. Get start earning at your Management
work.  Visit: http://managementjobs.webs.com/pm.htm &
http://jobscore.webs.com/executivemanager.htm
-- 
http://mail.python.org/mailman/listinfo/python-list


Can't find pythonwin

2010-10-10 Thread HP Garcia
I am working on a class project that requires us to use python. I have never 
used python before so be gentle. My question is I have python 2.5 installed 
on my pc. The instructions as me to run pythonwin but I can't find 
pythonwin. What I did find is IDLE(GUI). Any suggestions where I can find 
it. I did a search on m pc and no luck. 


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


Re: Can't find pythonwin

2010-10-10 Thread Mark Hammond

On 11/10/2010 11:24 AM, HP Garcia wrote:

I am working on a class project that requires us to use python. I have
never used python before so be gentle. My question is I have python 2.5
installed on my pc. The instructions as me to run pythonwin but I can't
find pythonwin. What I did find is IDLE(GUI). Any suggestions where I
can find it. I did a search on m pc and no luck.


pythonwin comes with the pywin32 extensions - see 
http://sf.net/projects/pywin32.


Cheers,

Mark

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


Re: Can't find pythonwin

2010-10-10 Thread Jed Smith
On Sun, Oct 10, 2010 at 8:24 PM, HP Garcia  wrote:
> I am working on a class project that requires us to use python. I have never
> used python before so be gentle. My question is I have python 2.5 installed
> on my pc. The instructions as me to run pythonwin but I can't find
> pythonwin. What I did find is IDLE(GUI). Any suggestions where I can find
> it. I did a search on m pc and no luck.

As far as I know, Pythonwin is a collection of extensions to Python
that are available on Windows (and antiquated).

If your professor means "Python on Windows" instead of "Pythonwin",
this might be of use:
http://docs.python.org/faq/windows#how-do-i-run-a-python-program-under-windows

-- 
Jed Smith
j...@jedsmith.org
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Can't find pythonwin

2010-10-10 Thread MRAB

On 11/10/2010 01:24, HP Garcia wrote:

I am working on a class project that requires us to use python. I have
never used python before so be gentle. My question is I have python 2.5
installed on my pc. The instructions as me to run pythonwin but I can't
find pythonwin. What I did find is IDLE(GUI). Any suggestions where I
can find it. I did a search on m pc and no luck.


Ask Google. :-)
--
http://mail.python.org/mailman/listinfo/python-list


Re: "Strong typing vs. strong testing"

2010-10-10 Thread Dave Angel



On 2:59 PM, Lie Ryan wrote:

On 10/01/10 23:56, BartC wrote:

"Pascal J. Bourguignon"  wrote in message
news:87zkuyjawh@kuiper.lan.informatimago.com...

"BartC"  writes:


"Pascal J. Bourguignon"  wrote in message

When Intel will realize that 99% of its users are running VM

Which one?

Any implementation of a controlled environment is a virtual machine.
Sometimes it is explicitely defined, such as in clisp, parot or jvm, but
more often it is implicit, such as in sbcl, or worse, developed in an
ad-hoc way in applications (eg. written in C++).

But if you had to implement a VM directly in hardware, which one (of the
several varieties) would you choose?

Virtual Machine in Hardware... isn't that a contradiction?


Intel's processors aren't just hardware, they already have lots of 
microcode, as has nearly every machine  built in the last 40 years or 
so.  However, the hardware is heavily biased towards the "Intel x86 
instruction" architecture.  But there are at least 3 vm's already in the 
Pentium processor:  one emulates the 8086 PC model, real mode,  another 
is 32bit, and the third is 64 bit.


Years ago, somebody microcoded the UCSD p machine, which is probably the 
logical forerunner to the byte code of both the jvm and the Python byte 
code architecture.  The machine was pretty quick, but it wasn't a 
commercial success, perhaps in part because of thermal problems, but 
primarily because it 'wasn't compatible."


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


Re: Class-level variables - a scoping issue

2010-10-10 Thread Lawrence D'Oliveiro
In message <4cb23ac9.70...@optimum.net>, John Posner wrote:

> Since it's unlikely that the language will change ...

Python 4000, anybody? :)
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: if the else short form

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

> Not having LIST and DICT as part of the base language would make sense
> if user contributions were encouraged.

Unfortunately, they neglected to include any kind of module/package system 
to make this kind of thing easy to do.

But then again, the state of the art in this respect at that time was 
FORTRAN, with its independent, not separate compilation. That meant there 
was no ability to check that the types, or even numbers, of arguments were 
consistent between the definition of a subprogram and a call to it.

This misfeature even carried over to C. C++ tried to finesse it by using 
name-mangling so you got some kind of error from the linker if arguments 
didn’t match, even if you couldn’t actually understand that’s what the error 
meant.

Languages that insisted on being able to do proper compiler-level cross 
checks between separately-compiled modules (e.g. Modula-2, Ada) never really 
became that popular. This saddened me.

> IMHO this is where python modules have been a big success story as it
> helps time proof the language by allowing the language to embrace new
> technologies as they establish themselves in the market place.

Nowadays we take it for granted that the core language should be a strong 
and compact basis to build on, rather than providing lots of built-in 
features, and all the rest should come from run-time libraries.
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Many newbie questions regarding python

2010-10-10 Thread Ethan Furman

Peter Pearson wrote:

On Sat, 09 Oct 2010 19:30:16 -0700, Ethan Furman  wrote:


Steven D'Aprano wrote:


[snip]

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?



Well, 





the unwary
reader (including me, not too long ago) expects that

  L = [arg for i in range(count)]

will be equivalent to

  L = [[], [], []]

but it's not, because the three elements in the first L are three
references to the *same* list.  Observe:




My question is more along the lines of: a mutable object was passed in 
to func()... what style of loop could be used to turn that one object 
into /n/ distinct objects?  A list comp won't do it, but neither will a 
for loop, nor a while loop.


Further, Steven stated "it will suffer the exact same surprising 
behaviour if f modifies the items in place" -- the damage has already 
been done by that point, as L has however many copies of the *same* object.


Seems to me you would have to use copy.copy or copy.deepcopy to be safe 
in such a circumstance, and the loop style is irrelevant.


If I've missed something, somebody please enlighten me.

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


Re: Reading Outlook .msg file using Python

2010-10-10 Thread Lawrence D'Oliveiro
In message
, John 
Henry wrote:

> I have a need to read .msg files exported from Outlook.

Try using EML format instead. That’s plain text.
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Compiling xpython: No rule to make target `runathana.py', needed by `frozen/frozen.c'.

2010-10-10 Thread est
On Oct 10, 7:40 pm, est  wrote:
> I found an interesting project, it's a single 4.7 MB executable with
> stdlib+PIL+pdf r/w+magpy+wxpython environment
>
> But when compiling following the instructions
>
> http://www.xpython.org/#compiling
>
> there is an error
>
> make: *** No rule to make target `runathana.py', needed by `frozen/
> frozen.c'.  Stop.
>
> I am using Debian 4.0 32bit machine

Today I am trying to compile it using mingw (GNU Make 3.81) on Win7 32
bit and got this

In file included from Python/thread.c:121:
Python/thread_pthread.h: In function 'PyThread_start_new_thread':
Python/thread_pthread.h:200: error: aggregate value used where an
integer was expected
Python/thread_pthread.h: In function 'PyThread_get_thread_ident':
Python/thread_pthread.h:222: error: aggregate value used where an
integer was expected
make[1]: *** [Python/thread.o] Error 1
make[1]: Leaving directory `e:/dev/github.com/xpython/Python-linux'
mingw32-make: *** [Python-linux/python] Error 2

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


Re: Class-level variables - a scoping issue

2010-10-10 Thread Ethan Furman

John Nagle wrote:

On 10/10/2010 12:53 AM, Lawrence D'Oliveiro wrote:


In message<4cb14f8c$0$1627$742ec...@news.sonic.net>, John Nagle wrote:

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"


As has been noted, this is the documented behavior, and completely 
rational in my mind with the way instances should interact with their 
classes.



I’d say there is definitely an inconsistency with the absoutely anal
restrictions on global variables, i.e. the well-known sequence

 ... reference to globalvar ...
 globalvar = newvalue

which triggers the error “UnboundLocalError: local variable 'globalvar'
referenced before assignment”.

It seems to me the same principle, that of disallowing implicit 
overriding
of a name from an outer scope with that from an inner one after the 
former

has already been referenced, should be applied here as well.


I completely disagree.  Inner and outer scopes are working in the 
procedural (/functional?) paradigm, whereas classes and instances are 
working in the OO paradigm.


You don't make every auto driver wear helmets because motorcycle drivers 
have to*.



(If you want default values for an instance, you define them
in __init__, not as class-level attributes.)


If you want non-class mutable attributes, you assign them in __init__. 
There is nothing incorrect about assigning default immutable attributes 
at the class level.


~Ethan~

--
* Obviously, this statement only makes sense in those locales that 
require helmets on motorcycle riders, but not on everyday auto drivers. 
 (Yes, I'm thinking of race-car drivers (at least in my locale.))

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


Re: Reading Outlook .msg file using Python

2010-10-10 Thread John Henry
On Oct 10, 8:27 pm, Lawrence D'Oliveiro  wrote:
> In message
> , John
>
> Henry wrote:
> > I have a need to read .msg files exported from Outlook.
>
> Try using EML format instead. That’s plain text.

Thanks for the reply.  I would have to check to see if my client's
Outlook can export in EML format directly.  I don't want to use a
converter.
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: "Strong typing vs. strong testing"

2010-10-10 Thread Rob Warnock
Martin Gregorie   wrote:
+---
| Lie Ryan wrote:
| > Virtual Machine in Hardware... isn't that a contradiction?
|
| Nope. Several mainframes did that. 
| 
| Two that I knew well were both British - the ICL 1900 and 2900.
| The Burroughs x700 series also used hardware virtualisation.
+---

Circa 1965, the IBM 1410 did hardware emulation of the earlier IBM 1401.
There was a front panel switch you could flip before you rebooted to
set the mode. We needed it at Emory Univ. since the program that drove
the Calcomp X-Y plotter only ean on the 1401. So we ran our Fortran
programs in 1410 mode with output to a mag tape, then rebooted into
1401 mode to run the program that plotted the data on the tape, then
rebooted back into 1410 mode for the next guy who needed the machine.


-Rob

-
Rob Warnock 
627 26th Avenue http://rpw3.org/>
San Mateo, CA 94403 (650)572-2607

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


os.stat and strange st_ctime

2010-10-10 Thread Mark Jones
I've read the part about these being variable

Note


The exact meaning and resolution of the st_atime, st_mtime, andst_ctime members
depends on the operating system and the file system. For example, on Windows
systems using the FAT or FAT32 file systems, st_mtimehas 2-second
resolution, and st_atime has only 1-day resolution. See your operating
system documentation for details.


However, given the fact that NTFS supports 100ns resolution, the results
here just look wrong.  st_ctime doesn't show a different time until there is
a ~15 second gap in the creation times (then however is shows the full time
gap)  Notice

Linux doesn't do this, the times look normal.  Seems strange to break cross
platform compatibility like this...


Here is the test code:

import os
import time

def runtest(sleepSecs):
print "Sleeping: " + str(sleepSecs)

fA = open("A", "w")
fA.close();

if sleepSecs:
time.sleep(sleepSecs)

fB = open("B", "w")
fB.close();
 info = os.stat("A")
print "A:",info.st_ctime, info.st_mtime, info.st_atime
 info = os.stat("B")
print "B:",info.st_ctime, info.st_mtime, info.st_atime

os.remove("A")
os.remove("B")


When run under Win7 on NTFS

for i in range(1,20):
  runtest(i)

>>> for i in range(0,20):
...   stattest.runtest(i)
...
Sleeping: 0
A: 1286770829.44 1286770829.44 1286770829.44
B: 1286770829.44 1286770829.44 1286770829.44
Sleeping: 1
A: 1286770829.44 1286770829.44 1286770829.44
B: 1286770829.44 1286770830.44 1286770830.44
Sleeping: 2
A: 1286770829.44 1286770830.45 1286770830.45
B: 1286770829.44 1286770832.45 1286770832.45
Sleeping: 3
A: 1286770829.44 1286770832.46 1286770832.46
B: 1286770829.44 1286770835.46 1286770835.46
Sleeping: 4
A: 1286770829.44 1286770835.47 1286770835.47
B: 1286770829.44 1286770839.47 1286770839.47
Sleeping: 5
A: 1286770829.44 1286770839.48 1286770839.48
B: 1286770829.44 1286770844.48 1286770844.48
Sleeping: 6
A: 1286770829.44 1286770844.49 1286770844.49
B: 1286770829.44 1286770850.49 1286770850.49
Sleeping: 7
A: 1286770829.44 1286770850.5 1286770850.5
B: 1286770829.44 1286770857.5 1286770857.5
Sleeping: 8
A: 1286770829.44 1286770857.51 1286770857.51
B: 1286770829.44 1286770865.51 1286770865.51
Sleeping: 9
A: 1286770829.44 1286770865.52 1286770865.52
B: 1286770829.44 1286770874.52 1286770874.52
Sleeping: 10
A: 1286770829.44 1286770874.52 1286770874.52
B: 1286770829.44 1286770884.53 1286770884.53
Sleeping: 11
A: 1286770829.44 1286770884.56 1286770884.56
B: 1286770829.44 1286770895.56 1286770895.56
Sleeping: 12
A: 1286770829.44 1286770895.57 1286770895.57
B: 1286770829.44 1286770907.57 1286770907.57
Sleeping: 13
A: 1286770829.44 1286770907.58 1286770907.58
B: 1286770829.44 1286770920.58 1286770920.58
Sleeping: 14
A: 1286770829.44 1286770920.59 1286770920.59
B: 1286770829.44 1286770934.59 1286770934.59
*Sleeping: 15*
*A: 1286770829.44 1286770934.6 1286770934.6*
*B: 1286770949.6 1286770949.6 1286770949.6*
Sleeping: 16
A: 1286770829.44 1286770949.61 1286770949.61
B: 1286770965.61 1286770965.61 1286770965.61
Sleeping: 17
A: 1286770829.44 1286770965.62 1286770965.62
B: 1286770982.62 1286770982.62 1286770982.62
Sleeping: 18
A: 1286770829.44 1286770982.63 1286770982.63
B: 1286771000.63 1286771000.63 1286771000.63
Sleeping: 19
A: 1286770829.44 1286771000.63 1286771000.63
B: 1286771019.63 1286771019.63 1286771019.63
-- 
http://mail.python.org/mailman/listinfo/python-list


WE R HOT COUPLES AND SEARCHING FOR HOT MAN, WOMAN OR COUPLES FOR REAL FUN

2010-10-10 Thread COUPLES FOR FUN
WE R HOT COUPLES AND SEARCHING FOR HOT MAN, WOMAN OR COUPLES FOR REAL
FUN
JUST CLICK  no promlem, just click

http://adultfriendfinder.com/go/page/reg_form_video_04?lang=english&pid=g1264377-ppc

http://adultfriendfinder.com/go/page/reg_form_video_04?lang=english&pid=g1264377-ppc

http://adultfriendfinder.com/go/page/reg_form_video_03?pid=g1250650-ppc

 http://alt.com/go/g1250650-ppc


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


Re: Eclipse/PyDev - BOM Lexical Error

2010-10-10 Thread Ethan Furman

Lawrence D'Oliveiro wrote:
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?


MS treats those first three bytes as a flag -- if they equal the BOM, MS 
treats it as UTF-8, if they equal anything else, MS does not treat it as 
UTF-8.


If you think otherwise, hop on an MS machine and test it out.

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


Re: open file on mac

2010-10-10 Thread tinauser
On Oct 10, 11:44 pm, Benjamin Kaplan  wrote:
> On Sun, Oct 10, 2010 at 5:29 PM, tinauser  wrote:
> > On Oct 10, 6:54 am, Lawrence D'Oliveiro  > central.gen.new_zealand> wrote:
> >> 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.
>
> > ok,then could you tell me if there is a way to have the folder of the
> > module where the "open" command is?
> > this module is called from C, and user could install the python code
> > folder (where both the module and the file are) where he wants.
>
> open uses the current working directory, which is available through
> the os.getcwd() function in Python.
> If you want to use a path relative to the current module, you can use
> that module's __file__ attribute.
> os.path.dirname(os.path.abspath(__file__))
> will give you the absolute path to the directory with the current module in 
> it.
>
> So if you want to open the foo.txt file in the same directory as the
> module, you use
> os.path.join(os.path.dirname(__file__), 'foo.txt')
>
> > thank you in advance
> > --
> >http://mail.python.org/mailman/listinfo/python-list
>
>

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


socket problem and html problem

2010-10-10 Thread bussiere bussiere
here is my code and two questions :
why it says to me that i can't bind the socket ?
normally it had closed it and kill it :/
and why it returns me plain text and not html ?

Regards and a pack of m&m's to the one who will help me on this two
questions.


import socket
import sys
# Create a TCP/IP socket



class Serverhttp:
def __init__(self):
sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
server_address = ('localhost', 2)
print >>sys.stderr, 'starting up on %s port %s' % server_address
sock.bind(server_address)
# Listen for incoming connections
sock.listen(1)
off = 2
self.message = ""
while True:
# Wait for a connection
print >>sys.stderr, 'waiting for a connection'
if off == 2 or off == 1:
connection, client_address = sock.accept()
try:
print >>sys.stderr, 'connection from', client_address

# Receive the data in small chunks and retransmit it
while True:
if off == 1:
sock = socket.socket(socket.AF_INET,
socket.SOCK_STREAM)
server_address = ('localhost', 17000)
sock.bind(server_address)
connection, client_address = sock.accept()
off = 0
data = connection.recv(1024)
print >>sys.stderr, 'received "%s"' % data
if data:
self.message = self.traitement(data)
connection.sendall(self.message)
connection.close()
sock.close()
del(sock)
off = 1
else:
print >>sys.stderr, 'no more data from',
client_address
break

finally:
# Clean up the connection
connection.close()


def traitement(self,string):
return
"Content-type:text/html;charset=utf8\n\ntest"
if __name__ == "__main__":
s = Serverhttp()

Google Fan boy
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Class-level variables - a scoping issue

2010-10-10 Thread Gregory Ewing

Lawrence D'Oliveiro wrote:

It seems to me the same principle, that of disallowing implicit overriding 
of a name from an outer scope with that from an inner one after the former 
has already been referenced, should be applied here as well.


How would you intend to enforce such a restriction?

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