Philosophical Python: 2*b or not 2*b

2011-10-27 Thread Andreas Neudecker
Not the answers I expected:
;-)

>>> b = True
>>> 2*b or not 2*b
2

>>> b = False
>>> 2*b or not 2*b
True


It all becomes clear when you look at:

>>> b = False
>>> 2*b
0

>>> b = True
>>> 2*b
2

No surprise there really. But fun anyway.

Any more philsophical Python code out there?

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


Genehmigt vom Ministerium für alberne Gangarten

2011-10-27 Thread Andreas Neudecker
http://blog.stuttgarter-zeitung.de/fundstucke/2011/10/27/genehmigt-vom-ministerium-fur-alberne-gangarten/

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


Problem using execvp

2011-10-27 Thread faucheuse
Hi,

I'm trying to launch my python program with another process name than
"python.exe".
In order to do that I'm trying to use the os.execvp function :

os.execvp("./Launch.py", ["ProcessName"])

Launch.py is the file that Launch the program and ProcessName is
the ... Process Name ^^

I get this error : OSError : [Errno 8] Exec format error.

I searched and found many solutions like : os.execvp("./Launch.py",
["./Launch.py","ProcessName"]), but nothing worked so far.

Can you help me plz ?
-- 
http://mail.python.org/mailman/listinfo/python-list


Presenting recursive dict (json_diff)

2011-10-27 Thread mcepl
Hi,

I have here a simple script (https://gitorious.org/json_diff/mainline)
which makes a diff between two JSON files. So for JSON objects

{
"a": 1,
"b": 2,
"son": {
"name": "Janošek"
}
}

and

{
"a": 2,
"c": 3,
"daughter": {
"name": "Maruška"
}
}

it generates

{
"append": {
"c": 3,
"daughter": {
"name": "Maruška"
}
},
"remove": {
"b": 2,
"son": {
"name": "Janošek"
}
},
"update": {
"a": 2
}
}

(obvious problems with name conflicts between internal keys and the
investigated keys will be somehow addressed later; any simple Pythonic
suggestions how?)

Now, I would like to create a script (or class) to present such diff
object in HTML (and mind you the diffs might be large, hopefully not
too much deeply nested, by with many many keys).

Any suggestions how to do it? Any libraries to use? I would love to
use some templating language, but I am not sure which simple ones
(e.g., I like pystache) could do recursive templating. So currently I
am tending towards generating strings.

I am currently tending towards something like (not cleaned to be CSS-
only yet):


  
 
'c' = 3
  
  
'b' = 2
--
  
  
'a' = 2
  
  
children
  
  
*
son = 'Ivánek'
  
  
*
daughter =
'Maruška'
  


but I cannot say I like it. Any suggestions?

Thank,

Matěj
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Presenting recursive dict (json_diff)

2011-10-27 Thread mcepl
On 27 říj, 10:50, mcepl  wrote:
> Hi,
>
> I have here a simple script (https://gitorious.org/json_diff/mainline)
> which makes a diff between two JSON files. So for JSON objects

and I have completely burried to lead on this. The point is that the
resulting object can be endlessly recursively nested. On each level I
can have not only append/remove/update subobjects, but also number of
subtrees. Something like this would be better picture:

{
"append": {
"c": 3
},
"remove": {
"b": 2
},
"update": {
"a": 2,
"children": {
"update": {
"son": "Ivánek"
},
"append": {
"daughter": "Maruška"
}
}
}
}
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Problem using execvp

2011-10-27 Thread Hans Mulder

On 27/10/11 10:57:55, faucheuse wrote:

I'm trying to launch my python program with another process name than
"python.exe".


Which version of Python are you using?
Which version of which operating system?


In order to do that I'm trying to use the os.execvp function :

os.execvp("./Launch.py", ["ProcessName"])

Launch.py is the file that Launch the program and ProcessName is
the ... Process Name ^^

I get this error : OSError : [Errno 8] Exec format error.


Maybe ./Lauch.py is not executable.

Can you run ./Launch.py from the command line?
Does it have a valid shebang line?
Is the 'x' bit set?
What does "file ./Launch.py" report?


I searched and found many solutions like : os.execvp("./Launch.py",
["./Launch.py","ProcessName"]), but nothing worked so far.


This works for me:

os.execvp("./Launch.py", ["ProcessName"]])


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


python logging multiple processes to one file (via socket server)

2011-10-27 Thread Gelonida N
Hi,

I have a rather 'simple' problem.
Logging from multiple processes to the same file AND be sure, that no
log message is lost,




1.) Log multiple processes to one file:
--

I have a python program, which I want to log, but which forks several times.

Due to the forking logging to files with the default logging.FileHandler
seems out of question.


It seems, that I could use a SocketHandler, which collects data from all
different processes and logs then to one file.


Does anybody have a working example.


2.) Ensure, that no log message is lost.
--
If I understood the doc of the SocketHandler, then
it will drop messages if the socket handler is not available.


However for my current activity I would prefer, that it aborts if it
cannot  connect to the socket and that it blocks if the log server
doesn't handle the sent data fast enough.

Is this possible.

Thanlks a lot in advance.


What I found so far:
http://docs.python.org/howto/logging-cookbook.html#logging-to-a-single-file-from-multiple-processes

It states:
"The following section documents this approach in more detail and
includes a working socket receiver which can be used as a starting point
for you to adapt in your own applications."

Somehow I have a mental block though and fail to see the 'following
section'.


I also found http://code.google.com/p/python-loggingserver/ and ran
first tests.

However it seems, that this server stops logging my application after
about 128 log entries. (the number varies and is not necessarily exactly
128), whereas the console loggier continues logging.

I'm not really show why and would prefer a simpler example first.


Thanks in advance for any code example, idea, link, comment.


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


__dict__ attribute for built-in types

2011-10-27 Thread candide
I realize that built-in types objects don't provide a __dict__ attribute 
and thereby i can't set an attribute to a such object, for instance



>>> a=[42,421]
>>> a.foo="bar"
Traceback (most recent call last):
  File "", line 1, in 
AttributeError: 'list' object has no attribute 'foo'
>>> a.__dict__
Traceback (most recent call last):
  File "", line 1, in 
AttributeError: 'list' object has no attribute '__dict__'
>>>


So, i was wondering :

-- why this behaviour ?
-- where the official documentation refers to this point ?


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


Re: spawnl issues with Win 7 access rights

2011-10-27 Thread Propad
Hello Gentelmen,

the suggestion to add the optional second parameter fixed the problem,
spawnl now works on the Win 7 computer I'm responsible for (with
Python 2.2). So the suggested cause seems to be right.

Thank you for the great help!

Cheers,

Nenad



On 26 Okt., 21:20, Terry Reedy  wrote:
> On 10/26/2011 10:38 AM, Tim Golden wrote:
>
> > On 26/10/2011 02:11, Terry Reedy wrote:
> >> OP reports 2.6 with XP works.
>
> > Where do you see that, Terry? (Or was there an offlist email?)
>
> The first message ofhttp://bugs.python.org/issue8036
> "Python 2.6 is however happy and just reports invalid arg."
>
> --
> Terry Jan Reedy

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


Re: spawnl issues with Win 7 access rights

2011-10-27 Thread Tim Golden

On 27/10/2011 11:27, Propad wrote:

the suggestion to add the optional second parameter fixed the problem,
spawnl now works on the Win 7 computer I'm responsible for (with
Python 2.2). So the suggested cause seems to be right.


FWIW, although it's not obvious, the args parameter to spawnl
is intended to become the sys.args (in Python terms) of the
newly-spawned process. Which is why the first element is expected
to be the name of the process. It took me some time to realise
this myself :)

Anyway, glad we could be of help.

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


Re: Problem using execvp

2011-10-27 Thread James Housden
On Oct 27, 11:27 am, Hans Mulder  wrote:
> On 27/10/11 10:57:55, faucheuse wrote:
>
> > I'm trying to launch my python program with another process name than
> > "python.exe".
>
> Which version of Python are you using?
> Which version of which operating system?
>
> > In order to do that I'm trying to use the os.execvp function :
>
> > os.execvp("./Launch.py", ["ProcessName"])
>
> > Launch.py is the file that Launch the program and ProcessName is
> > the ... Process Name ^^
>
> > I get this error : OSError : [Errno 8] Exec format error.
>
> Maybe ./Lauch.py is not executable.
>
> Can you run ./Launch.py from the command line?
> Does it have a valid shebang line?
> Is the 'x' bit set?
> What does "file ./Launch.py" report?
>
> > I searched and found many solutions like : os.execvp("./Launch.py",
> > ["./Launch.py","ProcessName"]), but nothing worked so far.
>
> This works for me:
>
>      os.execvp("./Launch.py", ["ProcessName"]])
>
> -- HansM

Hello,

This worked for me:
os.execvp("./Launch.py", ["python", "ProcessName"])

Best regards,
James
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: __dict__ attribute for built-in types

2011-10-27 Thread Arnaud Delobelle
On 27 October 2011 11:08, candide  wrote:
> I realize that built-in types objects don't provide a __dict__ attribute and
> thereby i can't set an attribute to a such object, for instance
>
>
 a=[42,421]
 a.foo="bar"
> Traceback (most recent call last):
>  File "", line 1, in 
> AttributeError: 'list' object has no attribute 'foo'
 a.__dict__
> Traceback (most recent call last):
>  File "", line 1, in 
> AttributeError: 'list' object has no attribute '__dict__'


Some built in types have a __dict__:

>>> def foo(): pass
...
>>> foo.__dict__
{}
>>> import random
>>> len(random.__dict__)
57

>
> So, i was wondering :
>
> -- why this behaviour ?

Performance reasons I guess.

> -- where the official documentation refers to this point ?

I don't know this one :)

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


Re: __dict__ attribute for built-in types

2011-10-27 Thread Duncan Booth
candide  wrote:

> I realize that built-in types objects don't provide a __dict__ 
attribute 
> and thereby i can't set an attribute to a such object, for instance
> 
> 
> >>> a=[42,421]
> >>> a.foo="bar"
> Traceback (most recent call last):
>File "", line 1, in 
> AttributeError: 'list' object has no attribute 'foo'
> >>> a.__dict__
> Traceback (most recent call last):
>File "", line 1, in 
> AttributeError: 'list' object has no attribute '__dict__'
> >>>
> 
> 
> So, i was wondering :
> 
> -- why this behaviour ?

Types without a __dict__ use less memory. Also, if you couldn't have a 
type that didn't have a `__dict__` then any `dict` would also need its 
own `__dict__` which would either result in infinite memory use or 
recursive dictionaries.

It isn't just built-in types, you can choose for any type you define 
whether or not to have a '__dict__' attribute

>>> class Fixed(object):
__slots__ = ('foo', 'bar')
readonly = 42


>>> f = Fixed()
>>> f.foo, f.bar = 1, 2
>>> f.foo, f.bar, f.readonly
(1, 2, 42)
>>> f.readonly = 24
Traceback (most recent call last):
  File "", line 1, in 
f.readonly = 24
AttributeError: 'Fixed' object attribute 'readonly' is read-only
>>> f.baz = 'whatever'
Traceback (most recent call last):
  File "", line 1, in 
f.baz = 'whatever'
AttributeError: 'Fixed' object has no attribute 'baz'

> -- where the official documentation refers to this point ?
> 
See http://docs.python.org/reference/datamodel.html for the docs about 
__slots__

There is also the API documentation which describes at a low level how 
to control whether or not instances have a dict:
 http://docs.python.org/c-api/typeobj.html#tp_dictoffset

I'm not sure though where you find a higher level statement of which 
builtin types have a __dict__.

-- 
Duncan Booth http://kupuguy.blogspot.com
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Forking simplejson

2011-10-27 Thread Paul Kölle

Am 26.10.2011 19:34, schrieb Nathan Rice:

Since this happily went off to the wrong recipient the first time...

The python json module/simpljson are badly in need of an architecture
update.  The fact that you can't override the encode method of
JSONEncoder and have it work reliably without monkey patching the pure
python encoder is a sign that something is horribly wrong.

+1

I wonder why the builtin json didn't implemented the __json__ hook. Now 
you need to write encoders for possibly arbitrary (imported/third party) 
objects Looks like it's not so hard to extend json.JSONEncoder to 
look for __json__ though:


>>> import json
>>> from functools import partial
>>> from types import MethodType
>>> class Encoder(json.JSONEncoder):
... def default(self, obj):
fn = getattr(obj, '__json__', None)
... if fn and type(fn) == MethodType:
... return obj.__json__()
... return json.JSONEncoder.default(self, obj)
...
>>> class T(object):
... def __json__(self):
... return 'foo'
...
>>> t = T()
>>> dumps = partial(json.dumps, cls=Encoder)
>>> dumps(dict([(1,1), (2,2), ('test',t)]))
'{"test": "foo", "1": 1, "2": 2}'
>>>

cheers
 Paul

[snip]

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


Re: __dict__ attribute for built-in types

2011-10-27 Thread Chris Angelico
On Thu, Oct 27, 2011 at 10:03 PM, Duncan Booth
 wrote:
> Types without a __dict__ use less memory. Also, if you couldn't have a
> type that didn't have a `__dict__` then any `dict` would also need its
> own `__dict__` which would either result in infinite memory use or
> recursive dictionaries.
>

Easy, just self-reference.
a = {}
a.__dict__ is a  --> True

Yeah, it's recursion, but no different from types:

>>> type(type) is type
True

If you want this behavior, you can do it easily enough.

>>> class dictdict(dict):
def __init__(self):
self.__dict__=self

>>> a=dictdict()
>>> a.__dict__ is a
True

However, the more compelling argument is that a __slots__ object can
be WAY more efficient.

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


Re: Forking simplejson

2011-10-27 Thread Amirouche Boubekki
> +---+---+
>> | Python protocol | JSON |
>> | or special case | |
>> +===+=**==+
>> | (ø) __json__ | see (ø) |
>> +---+-**--|
>>  | map   | object|
>>
>
> I am curious what you mean by the 'map' protocol.


I mean dictionnary-like objects
-- 
http://mail.python.org/mailman/listinfo/python-list


[ANN] pyKook 0.6.0 - task automation tool for Python, similar to Rake or Ant

2011-10-27 Thread Makoto Kuwata
Hi,

I have released pyKook 0.6.0.
http://pypi.python.org/pypi/Kook/0.6.0
http://www.kuwata-lab.com/kook/
http://www.kuwata-lab.com/kook/pykook-users-guide.html

In this release, a lot of enhancements are introduced.


pyKook Overview
---

pyKook is a task automation tool for Python, similar to Rake or Ant.

(Kookbook.py):

kookbook.default = 'build'

@recipe(None, ['hello'])
def build(c):
"""build all"""
pass

@recipe('hello', ['hello.o'])
def file_hello(c):
"""build 'hello' from 'hello.o'"""
system(c%'gcc -o $(product) $(ingred)')

@recipe('*.o', ['$(1).c', '$(1).h'])
def file_o(c):
system(c%'gcc -c $(ingred)')


Command-line:

bash> kk # or pykook
$ gcc -c hello.c
### *** hello.o (recipe=file_o)
$ gcc -c hello.c
### ** hello (recipe=file_hello)
$ gcc -o hello hello.o
### * build (recipe=build)

See http://www.kuwata-lab.com/kook/pykook-users-guide.html for details.


Enhancements in this release


* 'kookbook' variable is available in your cookbook to specify
  materials or default product.
* Recipe meta-programming support.
  You can manipulate recipe objects directly.
* Load other cookbooks by kookbook.load().
  This enables you to split your Kookbook.py into several files.
* Support some useful task recipes: clean, sweep, and all.
* Namespace is now supported. It is called as 'Category' in Kook.
* Concatenation supported.
  You can concatenate your cookbook and pyKook libraries into a file.
  Using concatenated file, user doesn't need to install pyKook at all.
* Argument description is available.
* Private spice option is available.
* New command 'pushd()' provided.

See http://www.kuwata-lab.com/kook/pykook-CHANGES.txt for details.


Have fun!

--
regards,
makoto kuwata
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: __dict__ attribute for built-in types

2011-10-27 Thread candide

Le 27/10/2011 13:03, Duncan Booth a écrit :




-- where the official documentation refers to this point ?


See http://docs.python.org/reference/datamodel.html for the docs about
__slots__

There is also the API documentation which describes at a low level how
to control whether or not instances have a dict:
  http://docs.python.org/c-api/typeobj.html#tp_dictoffset

I'm not sure though where you find a higher level statement of which
builtin types have a __dict__.




OK, thanks for the information abouts the slots. Nevertheless, this 
cannot answer completely my question. Some builtin types like string, 
lists, integer, float, dictionaries, etc have the property that 
instances of those types don't provide a __dict__ attribute. I can't 
imagine the documentation lets pass silently this point.


But beside this, how to recognise classes whose object doesn't have a 
__dict__ attribute ?

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


Re: Forking simplejson

2011-10-27 Thread Chris Rebert
On Thu, Oct 27, 2011 at 6:55 AM, Amirouche Boubekki
 wrote:
>
>>> +---+---+
>>> | Python protocol | JSON |
>>> | or special case | |
>>> +===+===+
>>> | (ø) __json__ | see (ø) |
>>> +---+---|
>>>      | map               | object        |
>>
>> I am curious what you mean by the 'map' protocol.
>
> I mean dictionnary-like objects

How do you propose to detect such objects? isinstance(x, collections.Mapping) ?

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


Re: Forking simplejson

2011-10-27 Thread Chris Rebert
On Wed, Oct 26, 2011 at 2:14 AM, Amirouche Boubekki
 wrote:
> Héllo,
>
> I would like to fork simplejson [1] and implement serialization rules based
> on protocols instead of types [2], plus special cases for protocol free
> objects, that breaks compatibility. The benefit will be a better API for
> json serialization of custom classes and in the case of iterable it will
> avoid a calls like:
>
 simplejson.dumps(list(my_iterable))
>
> The serialization of custom objects is documented in the class instead of
> the ``default`` function of current simplejson implementation [3].
>
> The encoding algorithm works with a priority list that is summarized in the
> next table:
>
> +---+---+
> | Python protocol   | JSON  |
> |  or special case  |   |
> +===+===+

> | (§) unicode   | see (§)   |

> (§) if the algorithm arrives here, call unicode (with proper encoding rule)
> on the object and use the result as json serialization

I would prefer a TypeError in such cases, for the same reason
str.join() doesn't do an implicit str() on its operands:
- Explicit is better than implicit.
- (Likely) errors should never pass silently.
- In the face of ambiguity, refuse the temptation to guess.

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


Re: inserting \ in regular expressions

2011-10-27 Thread John Roth
On Oct 26, 2:47 pm, Dave Angel  wrote:
> On 10/26/2011 03:48 PM, Ross Boylan wrote:
>
>
>
>
>
>
>
> > I want to replace every \ and " (the two characters for backslash and
> > double quotes) with a \ and the same character, i.e.,
> > \ ->  \\
> > " ->  \"
>
> > I have not been able to figure out how to do that.  The documentation
> > for re.sub says "repl can be a string or a function; if it is a string,
> > any backslash escapes in it are processed.That is, \n is converted to a
> > single newline character, \r is converted to a carriage return, and so
> > forth. Unknown escapes such as \j are left alone."
>
> > \\ is apparently unknown, and so is left as is. So I'm unable to get a
> > single \.
>
> > Here are some tries in Python 2.5.2.  The document suggested the result
> > of a function might not be subject to the same problem, but it seems to
> > be.
>  def f(m):
> > ...    return "\\"+m.group(1)
> > ...
>  re.sub(r"([\\\"])", f, 'Silly " quote')
> > 'Silly \\" quote'
> > 
> >>> re.sub(r"([\\\"])", "\\1", 'Silly " quote')
> > 'Silly \\" quote'
>
> > Or perhaps I'm confused about what the displayed results mean.  If a
> > string has a literal \, does it get shown as \\?
>
> > I'd appreciate it if you cc me on the reply.
>
> > Thanks.
> > Ross Boylan
>
> I can't really help on the regex aspect of your code, but I can tell you
> a little about backslashes, quote literals, the interpreter, and python.
>
>
>   Now, one way to cheat on the string if you know you'll want to put
> actual backslashes is to use the raw string. That works quite well
> unless you want the string to end with a backslash.  There isn't a way
> to enter that as a single raw literal.  You'd have to do something
> string like
>       a = r"strange\literal\with\some\stuff" + "\\"
>
> My understanding is that no valid regex ends with a backslash, so this
> may not affect you.
>
> --
>
> DaveA

Dave's answer is excellent background. I've snipped everything except
the part I want to emphasize, which is to use raw strings. They were
put into Python specifically for your problem: that is, how to avoid
the double and triple backslashes while writing regexes.

John Roth

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


Re: __dict__ attribute for built-in types

2011-10-27 Thread Duncan Booth
Chris Angelico  wrote:

> On Thu, Oct 27, 2011 at 10:03 PM, Duncan Booth
> wrote:
>> Types without a __dict__ use less memory. Also, if you couldn't have a
>> type that didn't have a `__dict__` then any `dict` would also need its
>> own `__dict__` which would either result in infinite memory use or
>> recursive dictionaries.
>>
> 
> Easy, just self-reference.
> a = {}
> a.__dict__ is a  --> True
> 
> Yeah, it's recursion, but no different from types:
> 

Try thinking that one through. Imagine you could set up a dictionary the 
way you describe:

>>> class DictWithDict(dict):
def __init__(self, *args, **kw):
dict.__init__(self, *args, **kw)
self.__dict__ = self


>>> d = DictWithDict()
>>> d.keys()
dict_keys([])
>>> d = DictWithDict({'a': 42})
>>> d.keys()
dict_keys(['a'])
>>> d['keys'] = lambda: 'oops'
>>> d.keys()
'oops'
>>> 

A dict with itself as its own __dict__ becomes like a javascript object 
where subscripting and attribute access are mostly interchangeable.


-- 
Duncan Booth http://kupuguy.blogspot.com
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Forking simplejson

2011-10-27 Thread Amirouche Boubekki
2011/10/27 Chris Rebert 

> On Wed, Oct 26, 2011 at 2:14 AM, Amirouche Boubekki
>  wrote:
> > Héllo,
> >
> > I would like to fork simplejson [1] and implement serialization rules
> based
> > on protocols instead of types [2], plus special cases for protocol free
> > objects, that breaks compatibility. The benefit will be a better API for
> > json serialization of custom classes and in the case of iterable it will
> > avoid a calls like:
> >
>  simplejson.dumps(list(my_iterable))
> >
> > The serialization of custom objects is documented in the class instead of
> > the ``default`` function of current simplejson implementation [3].
> >
> > The encoding algorithm works with a priority list that is summarized in
> the
> > next table:
> >
> > +---+---+
> > | Python protocol   | JSON  |
> > |  or special case  |   |
> > +===+===+
> 
> > | (§) unicode   | see (§)   |
> 
> > (§) if the algorithm arrives here, call unicode (with proper encoding
> rule)
> > on the object and use the result as json serialization
>
> I would prefer a TypeError in such cases, for the same reason
> str.join() doesn't do an implicit str() on its operands:
> - Explicit is better than implicit.
> - (Likely) errors should never pass silently.
> - In the face of ambiguity, refuse the temptation to guess.
>

granted it's better.
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Philosophical Python: 2*b or not 2*b

2011-10-27 Thread John Ladasky
On Oct 27, 1:11 am, Andreas Neudecker  wrote:

> Any more philsophical Python code out there?

That is the question.
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: __dict__ attribute for built-in types

2011-10-27 Thread Amirouche Boubekki
2011/10/27 candide 

> I realize that built-in types objects don't provide a __dict__ attribute
> and thereby i can't set an attribute to a such object, for instance
>
>
> >>> a=[42,421]
> >>> a.foo="bar"
> Traceback (most recent call last):
>  File "", line 1, in 
> AttributeError: 'list' object has no attribute 'foo'
> >>> a.__dict__
> Traceback (most recent call last):
>  File "", line 1, in 
> AttributeError: 'list' object has no attribute '__dict__'
> >>>
>
>
> So, i was wondering :
>
> -- why this behaviour ?
>

performance


> -- where the official documentation refers to this point ?
>

it's here and there in python documentation. I did not find specific
documentation about the __dict__ property.

Have a look at :

- naming conventions in http://www.python.org/dev/peps/pep-0008/
- http://docs.python.org/library/stdtypes.html#modules

__dict__ is similar to other __*__ properties and has a function that
actually use it to do something usefull aka. dir
http://docs.python.org/library/functions.html#dir

The way I understand it is that it's for internal use but it's exposed for
debugging (and learning ?) purpose.
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: __dict__ attribute for built-in types

2011-10-27 Thread Amirouche Boubekki
> But beside this, how to recognise classes whose object doesn't have a
> __dict__ attribute ?
>

Why do you need to access __dict__ ? maybe dir is enough see the other
message
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: python logging multiple processes to one file (via socket server)

2011-10-27 Thread Vinay Sajip
On Oct 27, 11:09 am, Gelonida N  wrote:
> "The following section documents this approach in more detail and
> includes a working socket receiver which can be used as a starting point
> for you to adapt in your own applications."
>
> Somehow I have a mental block though and fail to see the 'following
> section'.

You're right, the link got lost in a reorganisation of the
documentation. Working example is here:

http://docs.python.org/howto/logging-cookbook.html#sending-and-receiving-logging-events-across-a-network

Regards,

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


xmlrpclib threadsafe?

2011-10-27 Thread quarterlife
I need to talk to an xmlrpc server.  I open one connection and then
create a pile of threads communicating to that server.  Based on some
limited testing, it seems to work really well.  The next step is to
turn it into a pool.  But before I continue, the question is: Does
anyone know if the xmlrpclib is really threadsafe?
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: python logging multiple processes to one file (via socket server)

2011-10-27 Thread bobicanprogram
On Oct 27, 6:09 am, Gelonida N  wrote:
> Hi,
>
> I have a rather 'simple' problem.
> Logging from multiple processes to the same file AND be sure, that no
> log message is lost,
>
> 1.) Log multiple processes to one file:
> --
>
> I have a python program, which I want to log, but which forks several times.
>
> Due to the forking logging to files with the default logging.FileHandler
> seems out of question.
>
> It seems, that I could use a SocketHandler, which collects data from all
> different processes and logs then to one file.
>
> Does anybody have a working example.
>
> 2.) Ensure, that no log message is lost.
> --
> If I understood the doc of the SocketHandler, then
> it will drop messages if the socket handler is not available.
>
> However for my current activity I would prefer, that it aborts if it
> cannot  connect to the socket and that it blocks if the log server
> doesn't handle the sent data fast enough.
>
> Is this possible.
>
> Thanlks a lot in advance.
>
> What I found so 
> far:http://docs.python.org/howto/logging-cookbook.html#logging-to-a-singl...
>
> It states:
> "The following section documents this approach in more detail and
> includes a working socket receiver which can be used as a starting point
> for you to adapt in your own applications."
>
> Somehow I have a mental block though and fail to see the 'following
> section'.
>
> I also foundhttp://code.google.com/p/python-loggingserver/and ran
> first tests.
>
> However it seems, that this server stops logging my application after
> about 128 log entries. (the number varies and is not necessarily exactly
> 128), whereas the console loggier continues logging.
>
> I'm not really show why and would prefer a simpler example first.
>
> Thanks in advance for any code example, idea, link, comment.


You might want to check out the SIMPL toolkit (http://
www.icanprogram.com/06py/main.html).   A SIMPL receiver will
"naturally" queue and serialize messages from multiple senders.

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


Re: python logging multiple processes to one file (via socket server)

2011-10-27 Thread 88888 Dihedral
Well, please check  the byte code compiled results. This is useful. I know that 
 a lot people are  working on increasing the speed of execution scripts written 
in python, say, psyco, pyrex for packages released! 
-- 
http://mail.python.org/mailman/listinfo/python-list


Need Windows user / developer to help with Pynguin

2011-10-27 Thread Lee Harr

I develop the free python-based turtle graphics application pynguin.

http://pynguin.googlecode.com/


Lately I have been getting a lot of positive comments from people
who use the program, but I am also getting a lot of feedback from
people on Windows (mostly beginners) who are having trouble
getting the program running.

I don't use Windows myself, though I have found access occasionally
to fix bugs. I just don't know enough about Windows culture to be
able to make a reliable method for installing or running the program.


The method that I wrote for Linux also works on Windows, but it has
to be run from the command prompt.


I am hoping someone can look at what is there and come up with a
reliable method or a simple set of steps that people can follow to get
up and running. Hopefully without having to resort to the command
prompt.

I started a wiki page here:
http://code.google.com/p/pynguin/wiki/InstallingPynguinOnWindows
but I can't even test if it actually works


Thanks for any help.

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


Re: Need Windows user / developer to help with Pynguin

2011-10-27 Thread Andrew Berg
On 10/27/2011 2:38 PM, Lee Harr wrote:
> I am hoping someone can look at what is there and come up with a
> reliable method or a simple set of steps that people can follow to get
> up and running. Hopefully without having to resort to the command
> prompt.
> 
> I started a wiki page here:
> http://code.google.com/p/pynguin/wiki/InstallingPynguinOnWindows
> but I can't even test if it actually works
> 
> 
> Thanks for any help.
Apparently, the US is a forbidden country and Google won't let me
download. Otherwise, I'd test it for you.

-- 
CPython 3.2.2 | Windows NT 6.1.7601.17640 | Thunderbird 7.0
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Presenting recursive dict (json_diff)

2011-10-27 Thread Terry Reedy

On 10/27/2011 4:50 AM, mcepl wrote:

Hi,

I have here a simple script (https://gitorious.org/json_diff/mainline)
which makes a diff between two JSON files. So for JSON objects

{
 "a": 1,
 "b": 2,
 "son": {
 "name": "Janošek"
 }
}

and

{
 "a": 2,
 "c": 3,
 "daughter": {
 "name": "Maruška"
 }
}

it generates

{
 "append": {
 "c": 3,
 "daughter": {
 "name": "Maruška"
 }
 },
 "remove": {
 "b": 2,
 "son": {
 "name": "Janošek"
 }
 },
 "update": {
 "a": 2
 }
}

(obvious problems with name conflicts between internal keys and the
investigated keys will be somehow addressed later; any simple Pythonic
suggestions how?)


Use '_append', etc, much like namedtuple does, for the same reason.

--
Terry Jan Reedy


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


Re: spawnl issues with Win 7 access rights

2011-10-27 Thread Terry Reedy

On 10/27/2011 6:36 AM, Tim Golden wrote:

On 27/10/2011 11:27, Propad wrote:

the suggestion to add the optional second parameter fixed the problem,
spawnl now works on the Win 7 computer I'm responsible for (with
Python 2.2). So the suggested cause seems to be right.


FWIW, although it's not obvious, the args parameter to spawnl
is intended to become the sys.args (in Python terms) of the
newly-spawned process. Which is why the first element is expected
to be the name of the process. It took me some time to realise
this myself :)

Anyway, glad we could be of help.


Can we make this fix automatic for Win7 to fix #8036?

--
Terry Jan Reedy

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


Dynamically creating properties?

2011-10-27 Thread Andy Dingley
I have some XML, with a variable and somewhat unknown structure. I'd
like to encapsulate this in a Python class and expose the text of the
elements within as properties.

How can I dynamically generate properties (or methods) and add them to
my class?  I can easily produce a dictionary of the required element
names and their text values, but how do I create new properties at run
time?

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


Re: Dynamically creating properties?

2011-10-27 Thread John Gordon
In  Andy 
Dingley  writes:

> How can I dynamically generate properties (or methods) and add them to
> my class?  I can easily produce a dictionary of the required element
> names and their text values, but how do I create new properties at run
> time?

You can set dynamic attributes on class objects without any special
processing at all.  Just do it, like so:

  class X(object):
pass

  myx = X()

  myx.color = 'red'
  myx.food = 'french fries'
  myx.lucky_number = 7

Or, if you don't know the attribute name beforehand:

  setattr(myx, 'occupation', 'programmer') 

For methods, use an existing method name (without the trailing parentheses)
as the attribute value, like so:

  myx.method = float # assigns the built-in method float()

-- 
John Gordon   A is for Amy, who fell down the stairs
gor...@panix.com  B is for Basil, assaulted by bears
-- Edward Gorey, "The Gashlycrumb Tinies"

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


Assigning generator expressions to ctype arrays

2011-10-27 Thread Patrick Maupin
Bug or misunderstanding?

Python 2.7.1+ (r271:86832, Apr 11 2011, 18:13:53)
[GCC 4.5.2] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> x = 32 * [0]
>>> x[:] = (x for x in xrange(32))
>>> from ctypes import c_uint
>>> x = (32 * c_uint)()
>>> x[:] = xrange(32)
>>> x[:] = (x for x in xrange(32))
Traceback (most recent call last):
  File "", line 1, in 
ValueError: Can only assign sequence of same size
>>>

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


Re: Presenting recursive dict (json_diff)

2011-10-27 Thread Matej Cepl

Dne 27.10.2011 21:49, Terry Reedy napsal(a):

Use '_append', etc, much like namedtuple does, for the same reason.


Right, done. What about the presentation issue? Any ideas?

Best,

Matěj
--
http://mail.python.org/mailman/listinfo/python-list


Re: Dynamically creating properties?

2011-10-27 Thread DevPlayer
On Oct 27, 3:59 pm, Andy Dingley  wrote:
> I have some XML, with a variable and somewhat unknown structure. I'd
> like to encapsulate this in a Python class and expose the text of the
> elements within as properties.
>
> How can I dynamically generate properties (or methods) and add them to
> my class?  I can easily produce a dictionary of the required element
> names and their text values, but how do I create new properties at run
> time?
>
> Thanks,

class MyX(object):
pass
myx = myx()

xml_tag = parse( file.readline() )

# should be a valid python named-reference syntax,
# although any object that can be a valid dict key is allowed.
# generally valid python named reference would be the answer to
your question
attribute = validate( xml_tag )

# dynamicly named property
setattr( myx, attribute, property(get_func, set_func, del_func,
attr_doc) )

# "dynamicly named method"
# really should be a valid python named-reference syntax
myfunc_name = validate(myfunc_name)

def somefunc(x):
return x+x
# or
somefunc = lambda x: x + x

setattr( myx, myfunc_name, somefunc )


So beaware of:
# \
setattr(myx, '1', 'one')

myx.1
File "", line 1
x.1
  ^
SyntaxError: invalid syntax

# \
x.'1'
  File "", line 1
x.'1'
^
SyntaxError: invalid syntax

# \
x.__dict__['1']   # returns
'one'

x.__dict__# returns
{'1': 'one'}

So you should validate your variable names if you are getting them
from somewhere.

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


Re: __dict__ attribute for built-in types

2011-10-27 Thread Steven D'Aprano
On Thu, 27 Oct 2011 16:01:25 +0200, candide wrote:

> OK, thanks for the information abouts the slots. Nevertheless, this
> cannot answer completely my question. Some builtin types like string,
> lists, integer, float, dictionaries, etc have the property that
> instances of those types don't provide a __dict__ attribute. I can't
> imagine the documentation lets pass silently this point.

What, you think it goes against the laws of physics that nobody thought 
to mention it in the docs? 

> But beside this, how to recognise classes whose object doesn't have a
> __dict__ attribute ?

The same way as you would test for any other attribute.

>>> hasattr(42, '__dict__')
False


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


Re: Assigning generator expressions to ctype arrays

2011-10-27 Thread Steven D'Aprano
On Thu, 27 Oct 2011 13:34:28 -0700, Patrick Maupin wrote:

> Bug or misunderstanding?
> 
> Python 2.7.1+ (r271:86832, Apr 11 2011, 18:13:53) [GCC 4.5.2] on linux2
> Type "help", "copyright", "credits" or "license" for more information.
 x = 32 * [0]
 x[:] = (x for x in xrange(32))
 from ctypes import c_uint
 x = (32 * c_uint)()
 x[:] = xrange(32)
 x[:] = (x for x in xrange(32))
> Traceback (most recent call last):
>   File "", line 1, in 
> ValueError: Can only assign sequence of same size

>From the outside, you can't tell how big a generator expression is. It 
has no length:

>>> g = (x for x in xrange(32))
>>> len(g)
Traceback (most recent call last):
  File "", line 1, in 
TypeError: object of type 'generator' has no len()

Since the array object has no way of telling whether the generator will 
have the correct size, it refuses to guess. I would argue that it should 
raise a TypeError with a less misleading error message, rather than a 
ValueError, so "bug".

The simple solution is to use a list comp instead of a generator 
expression. If you have an arbitrary generator passed to you from the 
outside, and you don't know how long it is yourself, you can use 
itertools.islice to extract just the number of elements you want. Given g 
some generator expression, rather than doing this:

# risky, if g is huge, the temporary list will also be huge
x[:] = list(g)[:32]

do this instead:

# use lazy slices guaranteed not to be unexpectedly huge
x[:] = list(itertools.islice(g, 32))


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


Re: __dict__ attribute for built-in types

2011-10-27 Thread Chris Angelico
On Fri, Oct 28, 2011 at 1:36 AM, Duncan Booth
 wrote:
> Try thinking that one through. Imagine you could set up a dictionary the
> way you describe
>
> A dict with itself as its own __dict__ becomes like a javascript object
> where subscripting and attribute access are mostly interchangeable.

Yeah; I never said it was a good thing, just that it's possible.
"Everything is permissible" - but not everything is beneficial.
"Everything is permissible" - but not everything is constructive. (1
Corinthians 10:23, NIV translation.)

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


Re: __dict__ attribute for built-in types

2011-10-27 Thread candide

Le 28/10/2011 00:19, Steven D'Aprano a écrit :


What, you think it goes against the laws of physics that nobody thought
to mention it in the docs?



No but I'm expecting from Python documentation to mention the laws of 
Python ...






But beside this, how to recognise classes whose object doesn't have a
__dict__ attribute ?


The same way as you would test for any other attribute.


hasattr(42, '__dict__')

False





OK but I'm talking about classes, not instances  : 42 has no __dict__ 
attribute but, may be, 43 _has_ such attribute, who knows in advance ? ;)


Let'have a try :

>>> hasattr(43, '__dict__')
False
>>>


so we have proved by induction that no integer instance has a 
dictionnary attribute ;)





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


Re: __dict__ attribute for built-in types

2011-10-27 Thread Hrvoje Niksic
candide  writes:

> But beside this, how to recognise classes whose object doesn't have a
> __dict__ attribute ?

str, list and others aren't classes, they are types.  While all
(new-style) classes are types, not all types are classes.  It's
instances of classes (types created by executing the "class" statement
or its equivalent) that automatically get a __dict__, unless __slots__
was used at class definition time to suppress it.  Built-in and
extension types can choose whether to implement __dict__.

(Mechanics of defining built-in and extension types are of course
implementation-specific.  CPython allows adding __dict__ to any
extension type by setting the tp_dictoffset member of the type
definition struct to the appropriate offset into the instance struct.)
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Dynamically creating properties?

2011-10-27 Thread DevPlayer
Personally I like to use this function instead of a "try: except:"
because try-except will allow names like __metaclass__.

Remember, setattr(obj, attr_name, value) allows attr_name to be any
valid str().
For example: '!@kdafk11', or '1_1', '1e-20', '0.0', '*one', '\n%%',
etc.

def isvalid_named_reference( astring ):
# "varible name" is really a named_reference
# import string   # would be cleaner

valid_first_char =
'_abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ'
valid_rest =
'_0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ'

# I think it's ok here for the rare type-check
# as unicode named-references are not allowed
if type(astring) is not str: return False

if len(astring) == 0: return False

if astring[0] not in valid_first_char: return False

for c in astring[1:]:
if c not in valid_rest: return False

# Python keywords not allowed as named references (variable names)
for astr in ['and', 'assert', 'break', 'class', 'continue',
'def', 'del', 'elif', 'else', 'except', 'exec',
'finally', 'for', 'from', 'global', 'if',
'import', 'in', 'is', 'lambda', 'not', 'or',
'pass', 'print', 'raise', 'return', 'try',
'while', 'yield',]:
if astring == astr: return False

# valid names but bad idea
if astring == '__builtins__': return None
if astring == '__metaclass__': return None
for astr in dir(__builtins__):
if astring == astr: return None # use None as a warning

# there might be more like __slots__, and other
# module level effecting special names like '__metaclass__'

return True

Also when using dynamically created "varible names" to check if your
objects have an attribute with that name already.
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Dynamically creating properties?

2011-10-27 Thread DevPlayer

At least one error:
change:
> for astr in dir(__builtins__):
to:
for astr in __builtins__.__dict__:
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: __dict__ attribute for built-in types

2011-10-27 Thread candide

Le 28/10/2011 00:57, Hrvoje Niksic a écrit :


was used at class definition time to suppress it.  Built-in and
extension types can choose whether to implement __dict__.



Is it possible in the CPython implementation to write something like this :

"foo".bar = 42

without raising an attribute error ?
--
http://mail.python.org/mailman/listinfo/python-list


Re: __dict__ attribute for built-in types

2011-10-27 Thread MRAB

On 28/10/2011 00:36, candide wrote:

Le 28/10/2011 00:57, Hrvoje Niksic a écrit :


was used at class definition time to suppress it. Built-in and
extension types can choose whether to implement __dict__.



Is it possible in the CPython implementation to write something like this :

"foo".bar = 42

without raising an attribute error ?


No, built-in classes written in C have certain limitations, but why
would you want to do that anyway?
--
http://mail.python.org/mailman/listinfo/python-list


Re: Assigning generator expressions to ctype arrays

2011-10-27 Thread Patrick Maupin
On Oct 27, 5:31 pm, Steven D'Aprano  wrote:
> From the outside, you can't tell how big a generator expression is. It has no 
> length:

I understand that.

> Since the array object has no way of telling whether the generator will have 
> the correct size, it refuses to guess.

It doesn't have to guess.  It can assume that I, the programmer, know
what the heck I am doing, and then validate that assumption -- trust,
but verify.  It merely needs to fill the slice and then ask for one
more and check that StopIteration is raised.

> I would argue that it should raise a TypeError
> with a less misleading error message, rather
> than a ValueError, so "bug".

And I would argue that it should simply work, unless someone can
present a more compelling reason why not.

> The simple solution is to use a list comp
> instead of a generator expression.

I know how to work around the issue.  I'm not sure I should have to.
It violates the principle of least surprise for the ctypes array to
not be able to interoperate with the iterator protocol in this
fashion.

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


Re: Dynamically creating properties?

2011-10-27 Thread DevPlayer
Second error

def isvalid_named_reference( astring ):
# "varible name" is really a named_reference
import __builtin__# add line


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


NLTK and package structure

2011-10-27 Thread Steven Bird
The Natural Language Toolkit (NLTK) is a suite of open source Python
packages for natural language processing, available at
http://nltk.org/, together with an O'Reilly book which is available
online for free.  Development is now hosted at http://github.com/nltk
-- get it here: g...@github.com:nltk/nltk.git

I am seeking advice on how to speed up our import process.  The
contents of several sub-packages are made available at the top level,
for the convenience of programmers and so that the examples published
in the book are more concise.  This has been done by having lots of
"from subpackage import *" in the top-level __init__.py.  Some of
these imports are lazy.  Unfortunately, any import of nltk leads to
cascading imports which pull in most of the library, unacceptably
slowing down the load time.

https://github.com/nltk/nltk/blob/master/nltk/__init__.py

I am looking for a solution that meets the following requirements:
1) import nltk is as fast as possible
2) published code examples are not broken
(or are easily fixed by calling nltk.load_subpackages() before the
rest of the code)
3) popular subpackage names are available at the top level
(e.g. nltk.probability.ConditionalFreqDist as nltk.ConditionalFreqDist)

The existing discussion of this issue amongst our developers is posted here:
http://code.google.com/p/nltk/issues/detail?id=378

Our practice in structuring subpackages is described here:
http://code.google.com/p/nltk/wiki/PackageStructure

Thanks for any advice.

-Steven Bird (NLTK Project coordinator)
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Problem using execvp

2011-10-27 Thread Nobody
On Thu, 27 Oct 2011 01:57:55 -0700, faucheuse wrote:

> I get this error : OSError : [Errno 8] Exec format error.

The most likely reason for this error is a missing or invalid
shebang, e.g.:

#!/usr/bin/python
or:
#!/usr/bin/env python

The "#!" must be the first two bytes in the file. Beware of text editors
adding a Unicode BOM (byte order mark) at the beginning of the file.

Forgetting to add execute permission would result in EPERM (errno=13,
"Permission denied"). An invalid interpreter results in ENOENT (errno=2,
"No such file or directory").

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


Re: __dict__ attribute for built-in types

2011-10-27 Thread Terry Reedy

On 10/27/2011 6:52 PM, candide wrote:


No but I'm expecting from Python documentation to mention the laws of
Python ...


The details of CPython builtin classes are not laws of Python. It *is* a 
'law of Python' that classes can use 'slots = ' to restrict the 
attributes of instances. By implication, builtin classes in any 
implementation do not have to allow attribute assignment. I do not 
believe it would be a violation if some implementation did so.


None of this is to say that we could not say something on the subject at 
the beginning of the 'built-in types' chapter of the lib manual.



OK but I'm talking about classes, not instances :


Yes you are. The class determines whether its instances have assignable 
new attributes.


> 42 has no __dict__ > attribute but,
> may be, 43 _has_ such attribute, who knows in advance ? ;)

True, in a sense, but if the class allowed a user to execute
"42.__dict__ = {}" then you could safely assume that "43.xxx = z" should 
work also.


--
Terry Jan Reedy

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


Re: __dict__ attribute for built-in types

2011-10-27 Thread candide

Le 28/10/2011 02:02, MRAB a écrit :



No, built-in classes written in C have certain limitations, but why
would you want to do that anyway?





Mainly for learning purpose and Python better understanding.

Actually, I have a class of mine for drawing graphs with the Graphviz 
software. The nodes of the graph to be represented was supposed to have 
2 attributes, say title and shortName. Now, I want to plot a graph whose 
nodes are pure string. So to fit the class interface, I was trying to 
add title and shortName attribute to every string node.


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


Re: __dict__ attribute for built-in types

2011-10-27 Thread alex23
On Oct 28, 8:52 am, candide  wrote:
> No but I'm expecting from Python documentation to mention the laws of
> Python ...

It's not a "law", it's an _implementation detail_. The docs don't tend
to mention every such decision made because that's what the source is
for.

> But beside this, how to recognise classes whose object doesn't have a
> __dict__ attribute ?

The better question is: why do you need to be able to?

> Is it possible in the CPython implementation to write something like this :
> "foo".bar = 42
> without raising an attribute error ?

Why are you trying to modify an immutible object?

If you really want to assign attributes to string objects, subclass
str.
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: __dict__ attribute for built-in types

2011-10-27 Thread Patrick Maupin
On Oct 27, 9:46 pm, candide  wrote:
> Le 28/10/2011 02:02, MRAB a crit :
>
>
>
> > No, built-in classes written in C have certain limitations, but why
> > would you want to do that anyway?
>
> Mainly for learning purpose and Python better understanding.
>
> Actually, I have a class of mine for drawing graphs with the Graphviz
> software. The nodes of the graph to be represented was supposed to have
> 2 attributes, say title and shortName. Now, I want to plot a graph whose
> nodes are pure string. So to fit the class interface, I was trying to
> add title and shortName attribute to every string node.

You can easily do that by subclassing a string:

class AnnotatedStr(str):
pass

x = AnnotatedStr('Node1')
x.title = 'Title for node 1'

etc.

The fact that you subclass it (unless your subclass uses __slots__)
will give it a dict.
-- 
http://mail.python.org/mailman/listinfo/python-list


Unicode literals and byte string interpretation.

2011-10-27 Thread Fletcher Johnson
If I create a new Unicode object u'\x82\xb1\x82\xea\x82\xcd' how does
this creation process interpret the bytes in the byte string? Does it
assume the string represents a utf-16 encoding, at utf-8 encoding,
etc...?

For reference the string is これは in the 'shift-jis' encoding.
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Assigning generator expressions to ctype arrays

2011-10-27 Thread Terry Reedy

On 10/27/2011 8:09 PM, Patrick Maupin wrote:

> x[:] = (x for x in xrange(32))

This translates to

s.__setitem__(slice(None,None), generator_object)

where 'generator_object' is completely opaque, except that it will yield 
0 to infinity objects in response to next() before raising StopIteration.


Given that a cytpe_array is a *fixed-length* array, *unlike* Python's 
extensible lists and arrays, failure is a possibility due to mis-matched 
lengths. So ctype_array can either look first, as it does, by calling 
len(value_object), or leap first and create a temporary array, see if it 
fills up exactly right, and if it does, copy it over.



I know how to work around the issue.  I'm not sure I should have to.


I do not think everyone else should suffer substantial increase in space 
and run time to avoid surprising you.



It violates the principle of least surprise


for ctypes to do what is most efficient in 99.9% of uses?


for the ctypes array to
not be able to interoperate with the iterator protocol in this
fashion.


It could, but at some cost. Remember, people use ctypes for efficiency, 
so the temp array path would have to be conditional. When you have a 
patch, open a feature request on the tracker.


--
Terry Jan Reedy

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


Re: Unicode literals and byte string interpretation.

2011-10-27 Thread David Riley
On Oct 27, 2011, at 11:05 PM, Fletcher Johnson wrote:

> If I create a new Unicode object u'\x82\xb1\x82\xea\x82\xcd' how does
> this creation process interpret the bytes in the byte string? Does it
> assume the string represents a utf-16 encoding, at utf-8 encoding,
> etc...?
> 
> For reference the string is これは in the 'shift-jis' encoding.

Try it and see!  One test case is worth a thousand words.  And Python has an 
interactive interpreter. :-)


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


Re: Unicode literals and byte string interpretation.

2011-10-27 Thread Chris Angelico
On Fri, Oct 28, 2011 at 2:05 PM, Fletcher Johnson  wrote:
> If I create a new Unicode object u'\x82\xb1\x82\xea\x82\xcd' how does
> this creation process interpret the bytes in the byte string? Does it
> assume the string represents a utf-16 encoding, at utf-8 encoding,
> etc...?
>
> For reference the string is これは in the 'shift-jis' encoding.

Encodings define how characters are represented in bytes. I think
probably what you're looking for is a byte string with those hex
values in it, which you can then turn into a Unicode string:

>>> a=b'\x82\xb1\x82\xea\x82\xcd'
>>> unicode(a,"shift-jis")# use 'str' instead of 'unicode' in Python 3
u'\u3053\u308c\u306f'

The u'' notation is for Unicode strings, which are not encoded in
any way. The last line of the above is a valid way of entering that
string in your source code, identifying Unicode characters by their
codepoints.

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


Re: Dynamically creating properties?

2011-10-27 Thread Lie Ryan

On 10/28/2011 08:48 AM, DevPlayer wrote:

On Oct 27, 3:59 pm, Andy Dingley  wrote:

I have some XML, with a variable and somewhat unknown structure. I'd
like to encapsulate this in a Python class and expose the text of the
elements within as properties.

How can I dynamically generate properties (or methods) and add them to
my class?  I can easily produce a dictionary of the required element
names and their text values, but how do I create new properties at run
time?

Thanks,


 class MyX(object):
 pass
 myx = myx()

 xml_tag = parse( file.readline() )

 # should be a valid python named-reference syntax,
 # although any object that can be a valid dict key is allowed.
 # generally valid python named reference would be the answer to
your question
 attribute = validate( xml_tag )

 # dynamicly named property
 setattr( myx, attribute, property(get_func, set_func, del_func,
attr_doc) )

 # "dynamicly named method"
 # really should be a valid python named-reference syntax
 myfunc_name = validate(myfunc_name)

 def somefunc(x):
 return x+x
 # or
 somefunc = lambda x: x + x

 setattr( myx, myfunc_name, somefunc )


So beaware of:
 # \
 setattr(myx, '1', 'one')

 myx.1
 File "", line 1
 x.1
   ^
 SyntaxError: invalid syntax

 # \
 x.'1'
   File "", line 1
 x.'1'
 ^
 SyntaxError: invalid syntax

 # \
 x.__dict__['1']   # returns
 'one'

 x.__dict__# returns
 {'1': 'one'}

So you should validate your variable names if you are getting them
from somewhere.


XML does not allow attribute names to start with a number, so I doubt 
you need to worry about that. In addition, if you also need to 
dynamically access attributes and you have zero control of the name, you 
can use getattr().


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


Re: Need Windows user / developer to help with Pynguin

2011-10-27 Thread Terry Reedy

On 10/27/2011 3:38 PM, Lee Harr wrote:


I develop the free python-based turtle graphics application pynguin.

http://pynguin.googlecode.com/


Lately I have been getting a lot of positive comments from people
who use the program, but I am also getting a lot of feedback from
people on Windows (mostly beginners) who are having trouble
getting the program running.

I don't use Windows myself, though I have found access occasionally
to fix bugs. I just don't know enough about Windows culture to be
able to make a reliable method for installing or running the program.

The method that I wrote for Linux also works on Windows, but it has
to be run from the command prompt.

I am hoping someone can look at what is there and come up with a
reliable method or a simple set of steps that people can follow to get
up and running. Hopefully without having to resort to the command
prompt.

I started a wiki page here:
http://code.google.com/p/pynguin/wiki/InstallingPynguinOnWindows
but I can't even test if it actually works


In my experience, double clicking on x.zip should open the zip in the 
zip program (xp) but not actually extract. Win7, with zip built in, just 
treats x.zip as a directory in Explorer, with no external program required.


You pynguin.zip contains one top level file -- a directory called 
pynguin that contains multiple files*. Extracting pynguin.zip to a 
pynguin directory in the same directory as pynguin.zip, the default 
behavior with Win7 at least, creates a new pynguin directory that 
contains the extracted pynguin directory. So one has to run 
pynguin/pynguin/setup.py. In other words, there is one level too many. 
People can erase 'pynguin' from the target list to avoid this. (I am not 
sure 7zip on xp has the same default.)


*You would make things easier, at least for Windows users, if you added 
the proper extensions.

pynguin => pynguin.py
README => README.txt
etc for other text files.

If you are using setup.py, I do not know of any alternative to using a 
command prompt. But do tell people to find it with Start/All 
Programs/Accessories/Command Prompt. Then two choices that I know of.


1."cd dir", where dir is the path to the directory containing setup.py. 
Of course, for "python setup.py install" to work, people must have the 
Python installation path python-dir in the PATH environmental variable, 
which it will not be unless they put it there. Without that, 
"python-dir\python setup.py install" is required.


2. "cd python-dir" followed by "python pynguin-dir\setup.py install"

I may have missed something, but I do not actually run many Python apps. 
One I have used (renpy) makes separate installers for Windows, *nix, and 
Mac. As I remember, is does a standard Windows installation with one of 
the free installers, asking where to put the files and if Start menu 
icons are wanted. I ran it with a desktop shortcup to the main program.


The new disutiles2/distribute/packaging package plus a new python 
launcher for Windows should make things easier within a year, by the 
time 3.3 is out.


For the occasion library package, I prefer to directly extract to 
site-packages.


--
Terry Jan Reedy

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


Re: Presenting recursive dict (json_diff)

2011-10-27 Thread Terry Reedy

On 10/27/2011 4:58 PM, Matej Cepl wrote:

Dne 27.10.2011 21:49, Terry Reedy napsal(a):

Use '_append', etc, much like namedtuple does, for the same reason.


Right, done. What about the presentation issue? Any ideas?


No.

--
Terry Jan Reedy

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


Re: __dict__ attribute for built-in types

2011-10-27 Thread Nobody
On Thu, 27 Oct 2011 12:08:45 +0200, candide wrote:

> I realize that built-in types objects don't provide a __dict__ attribute 
> and thereby i can't set an attribute to a such object, for instance

Note that possession or absence of a __dict__ attribute doesn't
necessarily mean that you can or can't set attributes, as the class can
override __setattr__().

Ultimately, there is no guaranteed mechanism to determine in advance
whether or not an attempt to set an attribute will succeed.

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


Re: __dict__ attribute for built-in types

2011-10-27 Thread Steven D'Aprano
On Fri, 28 Oct 2011 01:36:41 +0200, candide wrote:

> Le 28/10/2011 00:57, Hrvoje Niksic a écrit :
> 
>> was used at class definition time to suppress it.  Built-in and
>> extension types can choose whether to implement __dict__.
>>
>>
> Is it possible in the CPython implementation to write something like
> this :
> 
> "foo".bar = 42
> 
> without raising an attribute error ?

No, because built-in strings don't have a __dict__ and so you cannot add 
new attributes that didn't already exist.

But you can subclass str and do whatever you like.

class Str(str):
pass

Str("foo").bar = 42



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