Re: new extension generator for C++

2010-05-04 Thread Stefan Behnel

Rouslan Korneychuk, 03.05.2010 22:44:

The only issue is
it will not use keyword arguments for overloaded functions (I don't know
if that can even be done reliably *and* efficiently. I would need to
give it more thought).


You should look at the argument unpacking code that Cython generates. It 
has been subject to serious benchmarking and optimisations.


Stefan

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


How to get xml.etree.ElementTree not bomb on invalid characters in XML file ?

2010-05-04 Thread Barak, Ron
Hi,

I'm parsing XML files using ElementTree from xml.etree (see code below (and 
attached xml_parse_example.py)).

However, I'm coming across input XML files (attached an example: tmp.xml) which 
include invalid characters, that produce the following traceback:

$ python xml_parse_example.py
Traceback (most recent call last):
  File "xml_parse_example.py", line 63, in 
tree = xml2dict.open_and_parse_xml_file()
  File "xml_parse_example.py", line 14, in open_and_parse_xml_file
tree = ElementTree.parse(f)
  File "c:\Python26\lib\xml\etree\ElementTree.py", line 862, in parse
tree.parse(source, parser)
  File "c:\Python26\lib\xml\etree\ElementTree.py", line 586, in parse
parser.feed(data)
  File "c:\Python26\lib\xml\etree\ElementTree.py", line 1245, in feed
self._parser.Parse(data, 0)
xml.parsers.expat.ExpatError: not well-formed (invalid token): line 6, column 34

I read the documentation for xml.etree.ElementTree and see that it may take an 
optional parser parameter, but I don't know what this parser should be - to 
ignore the invalid characters.

Could you suggest a way to call ElementTree, so it won't bomb on these invalid 
characters ?

Thanks,
Ron.



#!/usr/bin/env python

from xml.etree import ElementTree
import pprint

compute_tail = False

class XmlFileToDict():
def __init__(self, xml_file_path):
self.xml_file_path = xml_file_path

def open_and_parse_xml_file(self):
with open(self.xml_file_path, 'rt') as f:
tree = ElementTree.parse(f)
return tree

def dict_list(self, node):
res = {}
res[node.tag] = []
self.xml_to_dict(node,res[node.tag])
reply = {}
if compute_tail:
reply[node.tag] = 
{'value':res[node.tag],'attribs':node.attrib,'tail':node.tail}
else:
reply[node.tag] = {'value':res[node.tag],'attribs':node.attrib}

return reply

def xml_to_dict(self, node, res):
rep = {}

if len(node):
#n = 0
for n in list(node):
rep[node.tag] = []
value = self.xml_to_dict(n,rep[node.tag])
if len(n):
if compute_tail:
value = 
{'value':rep[node.tag],'attributes':n.attrib,'tail':n.tail}
else:
value = 
{'value':rep[node.tag],'attributes':n.attrib}
res.append({n.tag:value})
else :

res.append(rep[node.tag][0])

else:


value = {}
if compute_tail:
value = 
{'value':node.text,'attributes':node.attrib,'tail':node.tail}
else:
value = {'value':node.text,'attributes':node.attrib}

res.append({node.tag:value})

return

if __name__ == '__main__' :
xml_file_path ='tmp.xml'
xml2dict = XmlFileToDict(xml_file_path)
tree = xml2dict.open_and_parse_xml_file()
xml_dict = xml2dict.dict_list(tree.getroot())
pprint.pprint(xml_dict)





tmp.xml
Description: tmp.xml


xml_parse_example.py
Description: xml_parse_example.py
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: new extension generator for C++

2010-05-04 Thread Samuel Williams
Dear Rouslan,

It looks interesting. I say go for it. You will learn something and might make 
some improvements on existing ideas.

I recommend putting the code on www.github.com

Kind regards,
Samuel

On 4/05/2010, at 8:44 AM, Rouslan Korneychuk wrote:

> Hi, I'm new here. I'm working on a program that exposes C++ declarations to 
> Python and I was wondering if there is any interest in it.
> 
> It's a hobby project. I was originally using Boost.Python on another project 
> but found a couple of things I didn't like. Eg: you can't really have private 
> constructors. If you want a Python object created, you either have to expose 
> the constructor in Python, or create an instance of the object somewhere else 
> in memory, and have it either copied to the PyObject or have the PyObject 
> hold a reference to it. I was working on a rudimentary 3D game engine that 
> was a Python extension. It was important to avoid copying potentially huge 
> amounts of data, but to have almost every PyObject just be a pointer to 
> another object (and the object already is just a few or even one pointer) 
> seemed like an pointless compromise, considering that I was writing code that 
> was specifically designed to be a Python extension (There was also another 
> issue I came across but don't remember now).
> 
> So I looked for other solutions and noticed that Py++ (which simply generates 
> Boost.Python code for you) was based on a seperate program called GCCXML. I 
> figured I could use GCCXML and generate code however I wanted. So I did.
> 
> My program generates human-readable code (it even uses proper indentation). 
> The program still has a lot of work to be done on it, but it can already 
> generate working Python extensions.
> 
> It takes an input file like this:
> 
> 
>module doc string
> 
>
>class doc string
>
>
>
>
>
> 
> 
> You can probably figure out what the resulting code does. The goal is to 
> generate code that is as close to hand-written code as possible. It even 
> forgoes using PyArg_ParseTupleAndKeywords and has the argument checks 
> hard-wired. It also generates a header file that contains a PyObject 
> implementation of MyClass called obj_AClass, with every constructor fowarded 
> and with the new and delete operators overloaded to play nice with Python's 
> memory handler:
> ...
> extern PyTypeObject obj_AClassType;
> 
> struct obj_AClass {
>PyObject_HEAD
>MyClass base;
>bool initialized;
> 
>void *operator new(size_t s) {
>void *ptr = PyMem_Malloc(s);
>if(!ptr) throw std::bad_alloc();
>return ptr;
>}
> 
>void operator delete(void *ptr) {
>PyMem_Free(ptr);
>}
> 
>obj_AClass(MyClass const & _0) : base(_0) {
>PyObject_Init(reinterpret_cast(this),&obj_AClassType);
>initialized = true;
>}
> 
>obj_AClass(unsigned int _0) : base(_0) {
>PyObject_Init(reinterpret_cast(this),&obj_AClassType);
>initialized = true;
>}
> 
> };
> ...
> 
> If you want to expose one overload of a constructor or function but not the 
> others, you can add overload="list,of,args" to  or . Default 
> values for arguments are handled automatically. When handling overloaded 
> functions, it checks the types of the arguments to pick the best overload, 
> taking into consideration polymorphism and the fact that basic types like 
> ints can be converted from one-another. It will automatically detect if two 
> overloads match to the same set of Python arguments (eg: void func(float) vs 
> void func(double)). The only issue is it will not use keyword arguments for 
> overloaded functions (I don't know if that can even be done reliably *and* 
> efficiently. I would need to give it more thought).
> 
> If there is enough interest I can put this project up on SourceForge or 
> Google Code.
> -- 
> http://mail.python.org/mailman/listinfo/python-list

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


Next Melbourne PUG meeting 6:30pm Monday 10th of May @ Horse Bazaar

2010-05-04 Thread Richard Jones
Meeting details, location and talks list are at:

http://wiki.python.org/moin/MelbournePUG

It looks like we've got a few cool talks lined up:

15 minute talks
- None yet... suggest one!

5 minute talks
- Load-balancing xmlrpclib/jsonrpclib for robust distributed
applications (Andreux Fort)

... please feel free to suggest a topic - anything cool you've
discovered lately.

And I'm sure there'll be some talk about PyCon Australia as well!


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


Re: How to get xml.etree.ElementTree not bomb on invalid characters in XML file ?

2010-05-04 Thread Stefan Behnel

Barak, Ron, 04.05.2010 09:01:

 I'm parsing XML files using ElementTree from xml.etree (see code below
(and attached xml_parse_example.py)).

However, I'm coming across input XML files (attached an example:
tmp.xml) which include invalid characters, that produce the following
traceback:

$ python xml_parse_example.py
Traceback (most recent call last):
xml.parsers.expat.ExpatError: not well-formed (invalid token): line 6, column 34


I hope you are aware that this means that the input you are parsing is not 
XML. It's best to reject the file and tell the producers that they are 
writing broken output files. You should always fix the source, instead of 
trying to make sense out of broken input in fragile ways.




I read the documentation for xml.etree.ElementTree and see that it may
take an optional parser parameter, but I don't know what this parser
should be - to ignore the invalid characters.

Could you suggest a way to call ElementTree, so it won't bomb on these
invalid characters ?


No. The parser in lxml.etree has a 'recover' option that lets it try to 
recover from input errors, but in general, XML parsers are required to 
reject non well-formed input.


Stefan

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


Re: Sphinx hosting

2010-05-04 Thread Michele Simionato
On May 4, 8:37 am, "Martin v. Loewis"  wrote:
> > Do you know of recent improvements on the PyPI side about docs
> > hosting?
>
> Yes; go to your package's pkg_edit page, i.e.
>
> http://pypi.python.org/pypi?%3Aaction=pkg_edit&name=decorator
>
> and provide a zip file at Upload Documentation.
>
> Regards,
> Martin

Cool, that's good to know. I am still accepting recommendations for
non-Python projects ;)
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Django as exemplary design

2010-05-04 Thread Bruno Desthuilliers

TomF a écrit :
I'm interested in improving my python design by studying a large, 
well-designed codebase.  Someone (not a python programmer) suggested 
Django.  I realize that Django is popular, but can someone comment on 
whether its code is well-designed and worth studying?




Carl makes some valid points in his answer, and there are indeed a 
couple dark corners in this area - bit it's just a part of the whole 
framework. There are still things worth studying in Django IHMO, 
specially if you're interested in seeing metaclasses and descriptors at 
work.


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


Re: design question

2010-05-04 Thread Bruno Desthuilliers

Alf P. Steinbach a écrit :
(snip)
Re efficiency it seems to be a complete non-issue, but correctness is 
much more important: is there any way that the config details can be 
(inadvertently) changed while the build is going on?


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


Re: [OT] strange interaction between open and cwd

2010-05-04 Thread Gregory Ewing

Charles wrote:

In the OP's case, references to the directory have been removed from the 
file
system, but his process still has the current working directory reference to 
it,
so it has not actually been deleted. When he opens "../abc.txt", the OS 
searches

the current directory for ".." and finds the inode for /home/baz/tmp,


This doesn't seem to be quite correct. An experiment I just did
reveals that the link count on the parent directory goes down by
one when the current directory is deleted, suggesting that the ..
link has actually been removed... yet it still works!

I think what must be happening is that the kernel is maintaining
an in-memory reference to the parent directory, and treating ".."
as a special case when looking up a name.

(This probably shouldn't be too surprising, because ".." is special
in another way as well -- at the root of a mounted file system, it
leads to the parent of the mount point, even though the actual ".."
link on disk just points back to the same directory. Probably it
simplifies the name lookup logic to always treat it specially.)

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


Re: Sphinx hosting

2010-05-04 Thread James Mills
On Tue, May 4, 2010 at 5:27 PM, Michele Simionato
 wrote:
> Cool, that's good to know. I am still accepting recommendations for
> non-Python projects ;)

bitbucket (1) also provide static file hosting through the wiki. From
what I understand (tested)
you simply clone the wiki repository (which is it's own repository)
and commit a bunch of .html
files.

cheers
James

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


Re: strange interaction between open and cwd

2010-05-04 Thread Gregory Ewing

Grant Edwards wrote:


except that Python objects can form a generalized graph, and Unix
filesystems are constrained to be a tree.


Actually I believe that root is allowed to create arbitrary
hard links to directories in Unix, so it's possible to turn
the file system in to a general graph. It's highly
unrecommended, though, because it confuses the heck out of
programs that recursively traverse directories (which is
why only root is allowed to do it).

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


Re: strange interaction between open and cwd

2010-05-04 Thread Gregory Ewing

Grant Edwards wrote:


In your example, it's simply not possible to determine the file's
absolute path within the filesystem given the relative path you
provided.


Actually, I think it *is* theoretically possible to find an
absolute path for the file in this case.

I suspect that what realpath() is doing for a relative path is
something like:

1. Use getcwd() to find an absolute path for the current
   directory.
2. Chop off a trailing pathname component for each ".."
   on the front of the original path.
3. Tack the filename on the end of what's left.

Step 1 fails because the current directory no longer has
an absolute pathname -- specifically, it has no name in
what used to be its parent directory.

What realpath() is failing to realise is that it doesn't
actually need to know the full path of the current directory,
only of its parent directory, which is still reachable via
".." (if it weren't, the file wouldn't be reachable either,
and we wouldn't be having this discussion).

A smarter version of realpath() wouldn't try to find the
path of the *current* directory, but would follow the
".." links until it got to a directory that it did need to
know an absolute path for, and start with that.

Unfortunately, there is no C stdlib routine that does the
equivalent of getcwd() for an arbitrary directory, so
this would require realpath() to duplicate much of
getcwd()'s functionality, which is probably why it's
done the way it is.

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


[PATCH] add functional matrix-returning methods to Pycairo

2010-05-04 Thread Lawrence D'Oliveiro
I find the matrix methods in Pycairo to be an annoying hodge-podge of
ones that overwrite the Matrix object in-place (init_rotate, invert)
versus ones that concatenate additional transformations (rotate, scale,
translate) versus ones that return new matrices without modifying
the originals (multiply).

Myself, I prefer methods that always return new matrices. This allows
for a more functional style of programming, e.g. given

m = cairo.Matrix()

then

m2 = m.translation(-10, -10) * m.rotation(math.pi / 4) * m.translation(10, 
10)

concisely expresses rotation by 90° about the centre (10, 10).

Herewith a patch to add such methods to the cairo.Matrix class. Note
that the names (inverse, rotation, scaling, translation) are nouns,
to reflect the fact that they don't perform the actions, but they
return Matrix objects that do.

---
 src/matrix.c |   75 ++
 1 files changed, 75 insertions(+), 0 deletions(-)

diff --git a/src/matrix.c b/src/matrix.c
index eefeab9..d1709b9 100644
--- a/src/matrix.c
+++ b/src/matrix.c
@@ -206,6 +206,76 @@ matrix_translate (PycairoMatrix *o, PyObject *args) {
 }
 
 static PyObject *
+matrix_translation(PycairoMatrix *unused, PyObject *args)
+  {
+PyObject * result = NULL;
+double tx, ty;
+cairo_matrix_t result_matrix;
+do /*once*/
+  {
+if (!PyArg_ParseTuple(args, "dd:Matrix.translation", &tx, &ty))
+break;
+cairo_matrix_init_identity(&result_matrix);
+cairo_matrix_translate(&result_matrix, tx, ty);
+result = PycairoMatrix_FromMatrix(&result_matrix);
+  }
+while (0);
+return result;
+  } /*matrix_translation*/
+
+static PyObject *
+matrix_scaling(PycairoMatrix *unused, PyObject *args)
+  {
+PyObject * result = NULL;
+double sx, sy;
+cairo_matrix_t result_matrix;
+do /*once*/
+  {
+if (!PyArg_ParseTuple(args, "dd:Matrix.scaling", &sx, &sy))
+break;
+cairo_matrix_init_identity(&result_matrix);
+cairo_matrix_scale(&result_matrix, sx, sy);
+result = PycairoMatrix_FromMatrix(&result_matrix);
+  }
+while (0);
+return result;
+  } /*matrix_scaling*/
+
+static PyObject *
+matrix_rotation(PycairoMatrix *unused, PyObject *args)
+  {
+PyObject * result = NULL;
+double radians;
+cairo_matrix_t result_matrix;
+do /*once*/
+  {
+if (!PyArg_ParseTuple(args, "d:Matrix.rotation", &radians))
+break;
+cairo_matrix_init_identity(&result_matrix);
+cairo_matrix_rotate(&result_matrix, radians);
+result = PycairoMatrix_FromMatrix(&result_matrix);
+  }
+while (0);
+return result;
+  } /*matrix_rotation*/
+
+static PyObject *
+matrix_inverse(PycairoMatrix *self, PyObject *args_unused)
+  {
+PyObject * result = NULL;
+cairo_matrix_t result_matrix;
+do /*once*/
+  {
+result_matrix = self->matrix;
+if (Pycairo_Check_Status(cairo_matrix_invert(&result_matrix)))
+break;
+result = PycairoMatrix_FromMatrix(&result_matrix);
+  }
+while (0);
+return result;
+  } /*matrix_inverse*/
+
+static PyObject *
 matrix_item (PycairoMatrix *o, Py_ssize_t i) {
   switch (i) {
   case 0:
@@ -297,6 +367,11 @@ static PyMethodDef matrix_methods[] = {
   {"transform_distance",(PyCFunction)matrix_transform_distance, METH_VARARGS },
   {"transform_point", (PyCFunction)matrix_transform_point,   METH_VARARGS },
   {"translate",   (PyCFunction)matrix_translate, METH_VARARGS },
+  /* functional methods: */
+  {"translation", (PyCFunction)matrix_translation, METH_VARARGS | METH_STATIC 
},
+  {"scaling", (PyCFunction)matrix_scaling, METH_VARARGS | METH_STATIC },
+  {"rotation", (PyCFunction)matrix_rotation, METH_VARARGS | METH_STATIC },
+  {"inverse", (PyCFunction)matrix_inverse, METH_VARARGS },
   {NULL, NULL, 0, NULL},
 };
 
-- 
1.7.0

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


Re: win32 - catch events(wmi?)

2010-05-04 Thread Tim Golden

On 03/05/2010 23:53, Giampaolo Rodolà wrote:

Just out of curiosity, is WMI able to list the TCP and UDP connections
opened by a process or by the OS?
We'll have to do this for psutil (http://code.google.com/p/psutil) and
we guess it's not gonna be easy.


Not as far as I know. WMI doesn't tend to deal with things at a process
level. (Altho' I'm always being surprised at just what *is* out there
in WMI-land).

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


Re: Teaching Programming

2010-05-04 Thread Jean-Michel Pichavant

André wrote:

To Samuel Williams:(and other interested ;-)

If you want to consider Python in education, I would encourage you
have a look at http://www.python.org/community/sigs/current/edu-sig/

I think you will find that there are quite a few resources available -
perhaps more than you are aware of.

And, I don't think that because "some people do not like the
indentation strategy" is a valid reason not to consider that Python's
syntax is concise and simple.   Actually, I would almost argue for the
contrary.  Indentation indicate programming structure/logic very
clearly, without the need for arbitrary keywords and other punctuation
symbols.   There are very few keywords in the language.

You indicate that Python programs are readable.  They are also known
to be short  (much shorter than some other languages).
André
  
Python indentation has been already discussed many times around, I 
remember someone saying something like "How is it possible not to like 
indentation while any decent programmer will use it no matter the 
language, including all those which feature statements/keywords for blocks".


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


Re: Teaching Programming

2010-05-04 Thread Samuel Williams
I personally like indentation.

I just wonder whether it is an issue that some people will dislike.

But anyway, I updated the language comparison to remove this critique.

Kind regards,
Samuel

On 4/05/2010, at 9:22 PM, Jean-Michel Pichavant wrote:

> André wrote:
>> To Samuel Williams:(and other interested ;-)
>> 
>> If you want to consider Python in education, I would encourage you
>> have a look at http://www.python.org/community/sigs/current/edu-sig/
>> 
>> I think you will find that there are quite a few resources available -
>> perhaps more than you are aware of.
>> 
>> And, I don't think that because "some people do not like the
>> indentation strategy" is a valid reason not to consider that Python's
>> syntax is concise and simple.   Actually, I would almost argue for the
>> contrary.  Indentation indicate programming structure/logic very
>> clearly, without the need for arbitrary keywords and other punctuation
>> symbols.   There are very few keywords in the language.
>> 
>> You indicate that Python programs are readable.  They are also known
>> to be short  (much shorter than some other languages).
>> André
>> 
> Python indentation has been already discussed many times around, I remember 
> someone saying something like "How is it possible not to like indentation 
> while any decent programmer will use it no matter the language, including all 
> those which feature statements/keywords for blocks".
> 
> JM
> -- 
> http://mail.python.org/mailman/listinfo/python-list

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


itertools: problem with nested groupby, list()

2010-05-04 Thread Nico Schlömer
Hi,

I ran into a bit of an unexpected issue here with itertools, and I
need to say that I discovered itertools only recently, so maybe my way
of approaching the problem is "not what I want to do".

Anyway, the problem is the following:
I have a list of dictionaries, something like

[ { "a": 1, "b": 1, "c": 3 },
  { "a": 1, "b": 1, "c": 4 },
  ...
]

and I'd like to iterate through all items with, e.g., "a":1. What I do
is sort and then groupby,

my_list.sort( key=operator.itemgetter('a') )
my_list_grouped = itertools.groupby( my_list, operator.itemgetter('a') )

and then just very simply iterate over my_list_grouped,

for my_item in my_list_grouped:
# do something with my_item[0], my_item[1]

Now, inside this loop I'd like to again iterate over all items with
the same 'b'-value -- no problem, just do the above inside the loop:

for my_item in my_list_grouped:
# group by keyword "b"
my_list2 = list( my_item[1] )
my_list2.sort( key=operator.itemgetter('b') )
my_list_grouped = itertools.groupby( my_list2,
operator.itemgetter('b') )
for e in my_list_grouped:
# do something with e[0], e[1]

That seems to work all right.

Now, the problem occurs when this all is wrapped into an outer loop, such as

for k in [ 'first pass', 'second pass' ]:
for my_item in my_list_grouped:
# bla, the above

To be able to iterate more than once through my_list_grouped, I have
to convert it into a list first, so outside all loops, I go like

my_list.sort( key=operator.itemgetter('a') )
my_list_grouped = itertools.groupby( my_list, operator.itemgetter('a') )
my_list_grouped = list( my_list_grouped )

This, however, makes it impossible to do the inner sort and
groupby-operation; you just get the very first element, and that's it.

An example file is attached.

Hints, anyone?

Cheers,
Nico
#! /usr/bin/env python
# -*- coding: utf-8 -*-

import operator, itertools

my_list = [ { "a": 1, "b": 1, "c": 3 },
{ "a": 1, "b": 1, "c": 4 },
{ "a": 1, "b": 11, "c": 3 },
{ "a": 1, "b": 11, "c": 4 },
{ "a": 2, "b": 1, "c": 3 },
{ "a": 2, "b": 1, "c": 4 },
{ "a": 2, "b": 11, "c": 3 },
{ "a": 2, "b": 11, "c": 4 }  ]

# group my_list by "a"
my_list.sort( key=operator.itemgetter('a') )
my_list_grouped = itertools.groupby( my_list, operator.itemgetter('a') )
my_list_grouped = list( my_list_grouped )

for k in [ 'first pass', 'second pass' ]:
print k

for my_item in my_list_grouped:
print "\t" + str( my_item[0] )

# group by keyword "b"; need to get list first to be able to use 'sort'
my_list2 = list( my_item[1] )
my_list2.sort( key=operator.itemgetter('b') )
my_list_grouped = itertools.groupby( my_list2, operator.itemgetter('b') )

for e in my_list_grouped:
print "\t\t" + str( e[0] )
lll = list( e[1] )
for ee in lll:
print "\t\t\t" + str( ee )-- 
http://mail.python.org/mailman/listinfo/python-list


Re: print executed query

2010-05-04 Thread someone
On 4 Mai, 07:01, Fred C  wrote:
> On Apr 29, 2010, at 9:49 AM, Philip Semanchuk wrote:
>
>
>
>
>
> > On Apr 29, 2010, at 12:01 PM, someone wrote:
>
> >> Hello!
>
> >> Is there a way to print a query for logging purpose as it was or will
> >> be sent to database, if I don't escape values of query by myself?
>
> >> cursor.execute(query, [id, somestring])
>
> >> I could print query and values separate, but it would be great, if I
> >> could see how query is constructed and can the also copy it and
> >> execute in console.
>
> >> Im using psycopg2, btw
>
> > If you can fiddle with the Postgres server settings, the server has options 
> > for logging lots of things, including the queries it executes.
>
> > Hope this helps
> > Philip
>
> > --
> >http://mail.python.org/mailman/listinfo/python-list
>
>  smime.p7s
> 4KAnzeigenHerunterladen


Hello! Thanks for help! I've found much more simpler solution ;)

http://initd.org/psycopg/docs/cursor.html

query
Read-only attribute containing the body of the last query sent to the
backend (including bound arguments). None if no query has been
executed yet:

>>> cur.execute("INSERT INTO test (num, data) VALUES (%s, %s)", (42, 'bar'))
>>> cur.query
"INSERT INTO test (num, data) VALUES (42, E'bar')"

DB API extension The query attribute is a Psycopg extension to the DB
API 2.0.
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Teaching Programming

2010-05-04 Thread superpollo

Samuel Williams ha scritto:

I personally like indentation.

I just wonder whether it is an issue that some people will dislike.


i think there is an issue if you -- say -- produce python code, from 
within another programming environment, to be executed on the fly, at 
least in some instances. there might be problems if for example you 
generate code from a one-line template.


i use a special template system for my job, which goes like this:

...
%%SCHEMA:
...

$A, $B, $C being "loop" control variables reserved to the template 
system. upon parsing, the system generates the corresponding code (say 
"print 12*2**3") and stores the output for further use.


due to design restrictions, i cannot write a code-template which spans 
multiple template-lines, and that is a problem with python, because for 
instance i cannot use conditionals or for loops. if it was C or java 
there wuold be no problem since the source is free-form, so an entire 
program can "live" on a single source line.


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


Re: Teaching Programming

2010-05-04 Thread Stefan Behnel

superpollo, 04.05.2010 12:28:

i think there is an issue if you -- say -- produce python code, from
within another programming environment, to be executed on the fly, at
least in some instances. there might be problems if for example you
generate code from a one-line template.


There are a couple of code generation tools available that you can find on 
PyPI.


However, the main reason why this problem doesn't hurt much in Python is 
that Python is a dynamic language that can get you extremely far without 
generating code. It's simply not necessary in most cases, so people don't 
run into problems with it.


Stefan

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


Re: itertools: problem with nested groupby, list()

2010-05-04 Thread Ulrich Eckhardt
Nico Schlömer wrote:
> I ran into a bit of an unexpected issue here with itertools, and I
> need to say that I discovered itertools only recently, so maybe my way
> of approaching the problem is "not what I want to do".
> 
> Anyway, the problem is the following:
> I have a list of dictionaries, something like
> 
> [ { "a": 1, "b": 1, "c": 3 },
>   { "a": 1, "b": 1, "c": 4 },
>   ...
> ]
> 
> and I'd like to iterate through all items with, e.g., "a":1. What I do
> is sort and then groupby,
> 
> my_list.sort( key=operator.itemgetter('a') )
> my_list_grouped = itertools.groupby( my_list, operator.itemgetter('a') )
> 
> and then just very simply iterate over my_list_grouped,
> 
> for my_item in my_list_grouped:
> # do something with my_item[0], my_item[1]

I'd try to avoid copying the list and instead just iterate over it:


def iterate_by_key(l, key):
for d in l:
try:
yield l[key]
except:
continue

Note that you could also ask the dictionary first if it has the key, but I'm
told this way is even faster since it only requires a single lookup
attempt.


> Now, inside this loop I'd like to again iterate over all items with
> the same 'b'-value -- no problem, just do the above inside the loop:
> 
> for my_item in my_list_grouped:
> # group by keyword "b"
> my_list2 = list( my_item[1] )
> my_list2.sort( key=operator.itemgetter('b') )
> my_list_grouped = itertools.groupby( my_list2,
> operator.itemgetter('b') )
> for e in my_list_grouped:
> # do something with e[0], e[1]
> 
> That seems to work all right.

Since your operation not only iterates over a list but first sorts it, it
requires a modification which must not happen while iterating. You work
around this by copying the list first.

> Now, the problem occurs when this all is wrapped into an outer loop, such
> as
> 
> for k in [ 'first pass', 'second pass' ]:
> for my_item in my_list_grouped:
> # bla, the above
> 
> To be able to iterate more than once through my_list_grouped, I have
> to convert it into a list first, so outside all loops, I go like
> 
> my_list.sort( key=operator.itemgetter('a') )
> my_list_grouped = itertools.groupby( my_list, operator.itemgetter('a') )
> my_list_grouped = list( my_list_grouped )
> 
> This, however, makes it impossible to do the inner sort and
> groupby-operation; you just get the very first element, and that's it.

I believe that you are doing a modifying operation inside the the iteration,
which is a no-no. Create a custom iterator function (IIRC they are
called "generators") and you should be fine. Note that this should also
perform better since copying and sorting are not exactly for free, though
you may not notice that with small numbers of objects.

Uli

-- 
Sator Laser GmbH
Geschäftsführer: Thorsten Föcking, Amtsgericht Hamburg HR B62 932

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


Re: itertools: problem with nested groupby, list()

2010-05-04 Thread Jon Clements
On 4 May, 11:10, Nico Schlömer  wrote:
> Hi,
>
> I ran into a bit of an unexpected issue here with itertools, and I
> need to say that I discovered itertools only recently, so maybe my way
> of approaching the problem is "not what I want to do".
>
> Anyway, the problem is the following:
> I have a list of dictionaries, something like
>
> [ { "a": 1, "b": 1, "c": 3 },
>   { "a": 1, "b": 1, "c": 4 },
>   ...
> ]
>
> and I'd like to iterate through all items with, e.g., "a":1. What I do
> is sort and then groupby,
>
> my_list.sort( key=operator.itemgetter('a') )
> my_list_grouped = itertools.groupby( my_list, operator.itemgetter('a') )
>
> and then just very simply iterate over my_list_grouped,
>
> for my_item in my_list_grouped:
>     # do something with my_item[0], my_item[1]
>
> Now, inside this loop I'd like to again iterate over all items with
> the same 'b'-value -- no problem, just do the above inside the loop:
>
> for my_item in my_list_grouped:
>         # group by keyword "b"
>         my_list2 = list( my_item[1] )
>         my_list2.sort( key=operator.itemgetter('b') )
>         my_list_grouped = itertools.groupby( my_list2,
> operator.itemgetter('b') )
>         for e in my_list_grouped:
>             # do something with e[0], e[1]
>
> That seems to work all right.
>
> Now, the problem occurs when this all is wrapped into an outer loop, such as
>
> for k in [ 'first pass', 'second pass' ]:
>     for my_item in my_list_grouped:
>     # bla, the above
>
> To be able to iterate more than once through my_list_grouped, I have
> to convert it into a list first, so outside all loops, I go like
>
> my_list.sort( key=operator.itemgetter('a') )
> my_list_grouped = itertools.groupby( my_list, operator.itemgetter('a') )
> my_list_grouped = list( my_list_grouped )
>
> This, however, makes it impossible to do the inner sort and
> groupby-operation; you just get the very first element, and that's it.
>
> An example file is attached.
>
> Hints, anyone?
>
> Cheers,
> Nico

Does this example help at all?

my_list.sort( key=itemgetter('a','b','c') )
for a, a_iter in groupby(my_list, itemgetter('a')):
print 'New A', a
for b, b_iter in groupby(a_iter, itemgetter('b')):
print '\t', 'New B', b
for c, c_iter in groupby(b_iter, itemgetter('c')):
print '\t'*2, 'New C', c
for c_data in c_iter:
print '\t'*3, a, b, c, c_data
print '\t'*2, 'End C', c
print '\t', 'End B', b
print 'End A', a

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


Re: modifying open office spreadsheet (with OO installed)

2010-05-04 Thread Chris Withers

Jim Byrnes wrote:

News123 wrote:

Mumbling to myself, perhaps somebody else is interested.


Yes I am.


News123 wrote:

Hi,


I wanted to know who can recommend a good module/library, that allows to
modify an Open Office spreadsheet.

One can assume, that Open Office is installed on the host.



Following url gives a small introduction about using the PyUno-bridge to
open-read-modify-save a spread sheet
http://stuvel.eu/ooo-python


If .xls files are an option for you, xlutils.copy is your friend:

https://secure.simplistix.co.uk/svn/xlutils/trunk/xlutils/docs/copy.txt

cheers,

Chris

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


Re: ooolib, reading writing a spread sheet and keep formatting

2010-05-04 Thread Chris Withers

News123 wrote:


from xlrd import open_workbook
from xlutils.copy import copy

rb =  open_workbook('doc1.xls')


open_workbook('doc1.xls',formatting_info=True)


print "WB with %d sheets" % rb.nsheets
wb = copy(rb)
wb.save("doc2.xls") # file is created, but ALL formattng is lost and
formulas are now diplayed as text


cheers,

Chris

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


Re: Teaching Programming

2010-05-04 Thread superpollo

Stefan Behnel ha scritto:

superpollo, 04.05.2010 12:28:

i think there is an issue if you -- say -- produce python code, from
within another programming environment, to be executed on the fly, at
least in some instances. there might be problems if for example you
generate code from a one-line template.


There are a couple of code generation tools available that you can find 
on PyPI.


However, the main reason why this problem doesn't hurt much in Python is 
that Python is a dynamic language that can get you extremely far without 
generating code. It's simply not necessary in most cases, so people 
don't run into problems with it.


Stefan



Python 2.5.4 (r254:67916, Feb 17 2009, 20:16:45)
[GCC 4.3.3] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> A,B=2,3
>>> if A>B:
... print A+B
... else:
... print A**B-B**2
...
-1
>>> A,B=3,2
>>> if A>B:
... print A+B
... else:
... print A**B-B**2
...
5
>>>

tell me please: how can generate the same output (depending on A and B) 
without control structure? i mean in a natural "pythonic" way...


bye

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


Re: Teaching Programming

2010-05-04 Thread Martin P. Hellwig

On 05/04/10 11:28, superpollo wrote:

Samuel Williams ha scritto:

I personally like indentation.

I just wonder whether it is an issue that some people will dislike.



there might be problems if for example you
generate code from a one-line template.


Well a one-line template code generator are great and such, but if the 
output should be human readable it is necessary to have at least some 
form of mark-up control on it. Like newlines and indentations.


On the other hand if it is only meant to generate executable code, 
generating an interpreted code might not be the best solution for the 
actual problem.


For the corner cases (I can think of a couple) it is good to know you 
can use ';' most of the time.


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


python gui

2010-05-04 Thread a
where's the best online resource for teaching about GUI building?

Thanks

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


Re: itertools: problem with nested groupby, list()

2010-05-04 Thread Nico Schlömer
> Does this example help at all?

Thanks, that clarified things a lot!

To make it easier, let's just look at 'a' and 'b':


> my_list.sort( key=itemgetter('a','b','c') )
> for a, a_iter in groupby(my_list, itemgetter('a')):
>print 'New A', a
>for b, b_iter in groupby(a_iter, itemgetter('b')):
>print '\t', 'New B', b
>for b_data in b_iter:
>print '\t'*3, a, b, b_data
>print '\t', 'End B', b
>print 'End A', a

That works well, and I can wrap the outer loop in another loop without
problems. What's *not* working, though, is having more than one pass
on the inner loop, as in

=== *snip* ===
my_list.sort( key=itemgetter('a','b','c') )
for a, a_iter in groupby(my_list, itemgetter('a')):
   print 'New A', a
   for pass in ['first pass', 'second pass']:
   for b, b_iter in groupby(a_iter, itemgetter('b')):
   print '\t', 'New B', b
   for b_data in b_iter:
   print '\t'*3, a, b, b_data
   print '\t', 'End B', b
   print 'End A', a
=== *snap* ===

I tried working around this by

=== *snip* ===
my_list.sort( key=itemgetter('a','b','c') )
for a, a_iter in groupby(my_list, itemgetter('a')):
   print 'New A', a
   inner_list =  list( groupby(a_iter, itemgetter('b')) )
   for pass in ['first pass', 'second pass']:
   for b, b_iter in inner_list:
   print '\t', 'New B', b
   for b_data in b_iter:
   print '\t'*3, a, b, b_data
   print '\t', 'End B', b
   print 'End A', a
=== *snap* ===

which don't work either, and I don't understand why. -- I'll look at
Uli's comments.

Cheers,
Nico



On Tue, May 4, 2010 at 1:08 PM, Jon Clements  wrote:
> On 4 May, 11:10, Nico Schlömer  wrote:
>> Hi,
>>
>> I ran into a bit of an unexpected issue here with itertools, and I
>> need to say that I discovered itertools only recently, so maybe my way
>> of approaching the problem is "not what I want to do".
>>
>> Anyway, the problem is the following:
>> I have a list of dictionaries, something like
>>
>> [ { "a": 1, "b": 1, "c": 3 },
>>   { "a": 1, "b": 1, "c": 4 },
>>   ...
>> ]
>>
>> and I'd like to iterate through all items with, e.g., "a":1. What I do
>> is sort and then groupby,
>>
>> my_list.sort( key=operator.itemgetter('a') )
>> my_list_grouped = itertools.groupby( my_list, operator.itemgetter('a') )
>>
>> and then just very simply iterate over my_list_grouped,
>>
>> for my_item in my_list_grouped:
>>     # do something with my_item[0], my_item[1]
>>
>> Now, inside this loop I'd like to again iterate over all items with
>> the same 'b'-value -- no problem, just do the above inside the loop:
>>
>> for my_item in my_list_grouped:
>>         # group by keyword "b"
>>         my_list2 = list( my_item[1] )
>>         my_list2.sort( key=operator.itemgetter('b') )
>>         my_list_grouped = itertools.groupby( my_list2,
>> operator.itemgetter('b') )
>>         for e in my_list_grouped:
>>             # do something with e[0], e[1]
>>
>> That seems to work all right.
>>
>> Now, the problem occurs when this all is wrapped into an outer loop, such as
>>
>> for k in [ 'first pass', 'second pass' ]:
>>     for my_item in my_list_grouped:
>>     # bla, the above
>>
>> To be able to iterate more than once through my_list_grouped, I have
>> to convert it into a list first, so outside all loops, I go like
>>
>> my_list.sort( key=operator.itemgetter('a') )
>> my_list_grouped = itertools.groupby( my_list, operator.itemgetter('a') )
>> my_list_grouped = list( my_list_grouped )
>>
>> This, however, makes it impossible to do the inner sort and
>> groupby-operation; you just get the very first element, and that's it.
>>
>> An example file is attached.
>>
>> Hints, anyone?
>>
>> Cheers,
>> Nico
>
> Does this example help at all?
>
> my_list.sort( key=itemgetter('a','b','c') )
> for a, a_iter in groupby(my_list, itemgetter('a')):
>    print 'New A', a
>    for b, b_iter in groupby(a_iter, itemgetter('b')):
>        print '\t', 'New B', b
>        for c, c_iter in groupby(b_iter, itemgetter('c')):
>            print '\t'*2, 'New C', c
>            for c_data in c_iter:
>                print '\t'*3, a, b, c, c_data
>            print '\t'*2, 'End C', c
>        print '\t', 'End B', b
>    print 'End A', a
>
> Jon.
> --
> http://mail.python.org/mailman/listinfo/python-list
>
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: itertools: problem with nested groupby, list()

2010-05-04 Thread Nico Schlömer
> I'd try to avoid copying the list and instead just iterate over it:
>
>
>def iterate_by_key(l, key):
>for d in l:
>try:
>yield l[key]
>except:
>continue

Hm, that won't work for me b/c I don't know all the keys beforehand. I
could certainly do a unique(list.keys()) or something like that
beforehand, but I guess this does away with the speed advantage.

> Since your operation not only iterates over a list but first sorts it, it
> requires a modification which must not happen while iterating. You work
> around this by copying the list first.

So when I go like

for item in list:
item[1].sort()

I actually modify *list*? I didn't realize that; I thought it'd just
be a copy of it. Anyway, I could just try

for item in list:
newitem = sorted( item[1] )

in that case.

> which is a no-no. Create a custom iterator function (IIRC they are
> called "generators") and you should be fine.

I'll look into this, thanks for the hint.

Cheers,
Nico


On Tue, May 4, 2010 at 12:46 PM, Ulrich Eckhardt
 wrote:
> Nico Schlömer wrote:
>> I ran into a bit of an unexpected issue here with itertools, and I
>> need to say that I discovered itertools only recently, so maybe my way
>> of approaching the problem is "not what I want to do".
>>
>> Anyway, the problem is the following:
>> I have a list of dictionaries, something like
>>
>> [ { "a": 1, "b": 1, "c": 3 },
>>   { "a": 1, "b": 1, "c": 4 },
>>   ...
>> ]
>>
>> and I'd like to iterate through all items with, e.g., "a":1. What I do
>> is sort and then groupby,
>>
>> my_list.sort( key=operator.itemgetter('a') )
>> my_list_grouped = itertools.groupby( my_list, operator.itemgetter('a') )
>>
>> and then just very simply iterate over my_list_grouped,
>>
>> for my_item in my_list_grouped:
>>     # do something with my_item[0], my_item[1]
>
> I'd try to avoid copying the list and instead just iterate over it:
>
>
>    def iterate_by_key(l, key):
>        for d in l:
>            try:
>                yield l[key]
>            except:
>                continue
>
> Note that you could also ask the dictionary first if it has the key, but I'm
> told this way is even faster since it only requires a single lookup
> attempt.
>
>
>> Now, inside this loop I'd like to again iterate over all items with
>> the same 'b'-value -- no problem, just do the above inside the loop:
>>
>> for my_item in my_list_grouped:
>>         # group by keyword "b"
>>         my_list2 = list( my_item[1] )
>>         my_list2.sort( key=operator.itemgetter('b') )
>>         my_list_grouped = itertools.groupby( my_list2,
>> operator.itemgetter('b') )
>>         for e in my_list_grouped:
>>             # do something with e[0], e[1]
>>
>> That seems to work all right.
>
> Since your operation not only iterates over a list but first sorts it, it
> requires a modification which must not happen while iterating. You work
> around this by copying the list first.
>
>> Now, the problem occurs when this all is wrapped into an outer loop, such
>> as
>>
>> for k in [ 'first pass', 'second pass' ]:
>>     for my_item in my_list_grouped:
>>     # bla, the above
>>
>> To be able to iterate more than once through my_list_grouped, I have
>> to convert it into a list first, so outside all loops, I go like
>>
>> my_list.sort( key=operator.itemgetter('a') )
>> my_list_grouped = itertools.groupby( my_list, operator.itemgetter('a') )
>> my_list_grouped = list( my_list_grouped )
>>
>> This, however, makes it impossible to do the inner sort and
>> groupby-operation; you just get the very first element, and that's it.
>
> I believe that you are doing a modifying operation inside the the iteration,
> which is a no-no. Create a custom iterator function (IIRC they are
> called "generators") and you should be fine. Note that this should also
> perform better since copying and sorting are not exactly for free, though
> you may not notice that with small numbers of objects.
>
> Uli
>
> --
> Sator Laser GmbH
> Geschäftsführer: Thorsten Föcking, Amtsgericht Hamburg HR B62 932
>
> --
> http://mail.python.org/mailman/listinfo/python-list
>
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Teaching Programming

2010-05-04 Thread Stefan Behnel

superpollo, 04.05.2010 13:23:

Stefan Behnel ha scritto:

the main reason why this problem doesn't hurt much in Python
is that Python is a dynamic language that can get you extremely far
without generating code. It's simply not necessary in most cases, so
people don't run into problems with it.


Python 2.5.4 (r254:67916, Feb 17 2009, 20:16:45)
[GCC 4.3.3] on linux2
Type "help", "copyright", "credits" or "license" for more information.
 >>> A,B=2,3
 >>> if A>B:
... print A+B
... else:
... print A**B-B**2
...
-1
 >>> A,B=3,2
 >>> if A>B:
... print A+B
... else:
... print A**B-B**2
...
5

tell me please: how can generate the same output (depending on A and B)
without control structure? i mean in a natural "pythonic" way...


The question is: why do you have to generate the above code in the first 
place? Isn't a function enough that does the above?


Stefan

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


Re: itertools: problem with nested groupby, list()

2010-05-04 Thread Jon Clements
On 4 May, 12:36, Nico Schlömer  wrote:
> > Does this example help at all?
>
> Thanks, that clarified things a lot!
>
> To make it easier, let's just look at 'a' and 'b':
>
> > my_list.sort( key=itemgetter('a','b','c') )
> > for a, a_iter in groupby(my_list, itemgetter('a')):
> >    print 'New A', a
> >    for b, b_iter in groupby(a_iter, itemgetter('b')):
> >        print '\t', 'New B', b
> >        for b_data in b_iter:
> >            print '\t'*3, a, b, b_data
> >        print '\t', 'End B', b
> >    print 'End A', a
>
> That works well, and I can wrap the outer loop in another loop without
> problems. What's *not* working, though, is having more than one pass
> on the inner loop, as in
>
> === *snip* ===
> my_list.sort( key=itemgetter('a','b','c') )
> for a, a_iter in groupby(my_list, itemgetter('a')):
>    print 'New A', a
>    for pass in ['first pass', 'second pass']:
>        for b, b_iter in groupby(a_iter, itemgetter('b')):
>            print '\t', 'New B', b
>            for b_data in b_iter:
>                print '\t'*3, a, b, b_data
>            print '\t', 'End B', b
>        print 'End A', a
> === *snap* ===
>
> I tried working around this by
>
> === *snip* ===
> my_list.sort( key=itemgetter('a','b','c') )
> for a, a_iter in groupby(my_list, itemgetter('a')):
>    print 'New A', a
>    inner_list =  list( groupby(a_iter, itemgetter('b')) )
>    for pass in ['first pass', 'second pass']:
>        for b, b_iter in inner_list:
>            print '\t', 'New B', b
>            for b_data in b_iter:
>                print '\t'*3, a, b, b_data
>            print '\t', 'End B', b
>        print 'End A', a
> === *snap* ===
>
> which don't work either, and I don't understand why. -- I'll look at
> Uli's comments.
>
> Cheers,
> Nico
>
> On Tue, May 4, 2010 at 1:08 PM, Jon Clements  wrote:
> > On 4 May, 11:10, Nico Schlömer  wrote:
> >> Hi,
>
> >> I ran into a bit of an unexpected issue here with itertools, and I
> >> need to say that I discovered itertools only recently, so maybe my way
> >> of approaching the problem is "not what I want to do".
>
> >> Anyway, the problem is the following:
> >> I have a list of dictionaries, something like
>
> >> [ { "a": 1, "b": 1, "c": 3 },
> >>   { "a": 1, "b": 1, "c": 4 },
> >>   ...
> >> ]
>
> >> and I'd like to iterate through all items with, e.g., "a":1. What I do
> >> is sort and then groupby,
>
> >> my_list.sort( key=operator.itemgetter('a') )
> >> my_list_grouped = itertools.groupby( my_list, operator.itemgetter('a') )
>
> >> and then just very simply iterate over my_list_grouped,
>
> >> for my_item in my_list_grouped:
> >>     # do something with my_item[0], my_item[1]
>
> >> Now, inside this loop I'd like to again iterate over all items with
> >> the same 'b'-value -- no problem, just do the above inside the loop:
>
> >> for my_item in my_list_grouped:
> >>         # group by keyword "b"
> >>         my_list2 = list( my_item[1] )
> >>         my_list2.sort( key=operator.itemgetter('b') )
> >>         my_list_grouped = itertools.groupby( my_list2,
> >> operator.itemgetter('b') )
> >>         for e in my_list_grouped:
> >>             # do something with e[0], e[1]
>
> >> That seems to work all right.
>
> >> Now, the problem occurs when this all is wrapped into an outer loop, such 
> >> as
>
> >> for k in [ 'first pass', 'second pass' ]:
> >>     for my_item in my_list_grouped:
> >>     # bla, the above
>
> >> To be able to iterate more than once through my_list_grouped, I have
> >> to convert it into a list first, so outside all loops, I go like
>
> >> my_list.sort( key=operator.itemgetter('a') )
> >> my_list_grouped = itertools.groupby( my_list, operator.itemgetter('a') )
> >> my_list_grouped = list( my_list_grouped )
>
> >> This, however, makes it impossible to do the inner sort and
> >> groupby-operation; you just get the very first element, and that's it.
>
> >> An example file is attached.
>
> >> Hints, anyone?
>
> >> Cheers,
> >> Nico
>
> > Does this example help at all?
>
> > my_list.sort( key=itemgetter('a','b','c') )
> > for a, a_iter in groupby(my_list, itemgetter('a')):
> >    print 'New A', a
> >    for b, b_iter in groupby(a_iter, itemgetter('b')):
> >        print '\t', 'New B', b
> >        for c, c_iter in groupby(b_iter, itemgetter('c')):
> >            print '\t'*2, 'New C', c
> >            for c_data in c_iter:
> >                print '\t'*3, a, b, c, c_data
> >            print '\t'*2, 'End C', c
> >        print '\t', 'End B', b
> >    print 'End A', a
>
> > Jon.
> > --
> >http://mail.python.org/mailman/listinfo/python-list
>
>

Are you basically after this, then?

for a, a_iter in groupby(my_list, itemgetter('a')):
print 'New A', a
for b, b_iter in groupby(a_iter, itemgetter('b')):
b_list = list(b_i

Re: Teaching Programming

2010-05-04 Thread superpollo

Stefan Behnel ha scritto:

superpollo, 04.05.2010 13:23:

Stefan Behnel ha scritto:

the main reason why this problem doesn't hurt much in Python
is that Python is a dynamic language that can get you extremely far
without generating code. It's simply not necessary in most cases, so
people don't run into problems with it.


Python 2.5.4 (r254:67916, Feb 17 2009, 20:16:45)
[GCC 4.3.3] on linux2
Type "help", "copyright", "credits" or "license" for more information.
 >>> A,B=2,3
 >>> if A>B:
... print A+B
... else:
... print A**B-B**2
...
-1
 >>> A,B=3,2
 >>> if A>B:
... print A+B
... else:
... print A**B-B**2
...
5

tell me please: how can generate the same output (depending on A and B)
without control structure? i mean in a natural "pythonic" way...


The question is: why do you have to generate the above code in the first 
place? Isn't a function enough that does the above?


of course! *but* if i must generate on-the-fly python code that defines 
a function i am back again to the problem:


def fun():


ecc...

how can i put *that* on a oneliner?
--
http://mail.python.org/mailman/listinfo/python-list


Re: Teaching Programming

2010-05-04 Thread superpollo

Martin P. Hellwig ha scritto:

On 05/04/10 11:28, superpollo wrote:

Samuel Williams ha scritto:

I personally like indentation.

I just wonder whether it is an issue that some people will dislike.



there might be problems if for example you
generate code from a one-line template.


Well a one-line template code generator are great and such, but if the 
output should be human readable it is necessary to have at least some 
form of mark-up control on it. Like newlines and indentations.


or latex markup, in my case...

On the other hand if it is only meant to generate executable code, 
generating an interpreted code might not be the best solution for the 
actual problem.


yes, maybe generating code for a *compiler* might be faster but you must 
consider overhead due to compilation time, especially if code snippets 
are sprinkled by the dozen all over the template file.


For the corner cases (I can think of a couple) it is good to know you 
can use ';' most of the time.




most but not always as i noted (think about loops or function definition)

bye

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


Re: Ann: Validating Emails and HTTP URLs in Python

2010-05-04 Thread livibetter
First, it's good to see a library has URL and email validator.

But I found there might be a problem in your validator, the problems I
found are these URLs:

  http://example.com/path
  http://example.com/path)
  http://example.com/path]
  http://example.com/path}

By my understanding from RFCs, only first two are valid.

  >>> from lepl.apps.rfc3696 import *
  >>> v = HttpUrl()
  >>> v('http://example.com/')
  True
  >>> v('http://example.com/path')
  True
  >>> v('http://example.com/path)')
  True
  >>> v('http://example.com/path]')
  True
  >>> v('http://example.com/path}')
  True

You use RFC 3969 [1] to write your code (I read your source code,
lepl.apps.rfc3696._HttpUrl()), I think your code should only return
True for first case, but all return True. Maybe I use it incorrectly?

And I think that has a slight issue because RFC 3969 was written based
on RFC 2396 [2], which is obsoleted by RFC 3986 [3]. I never really
read RFC 3969, I am not sure if there is problem.

But in RFC 3969, it writes

   The following characters are reserved in many URIs -- they must be
   used for either their URI-intended purpose or must be encoded.
Some
   particular schemes may either broaden or relax these restrictions
   (see the following sections for URLs applicable to "web pages" and
   electronic mail), or apply them only to particular URI component
   parts.

  ; / ? : @ & = + $ , ?

However in RFC 2396 (the obsoleted RFC), "3.3. Path Component,"

   The path component contains data, specific to the authority (or the
   scheme if there is no authority component), identifying the
resource
   within the scope of that scheme and authority.

  path  = [ abs_path | opaque_part ]

  path_segments = segment *( "/" segment )
  segment   = *pchar *( ";" param )
  param = *pchar

  pchar = unreserved | escaped |
  ":" | "@" | "&" | "=" | "+" | "$" | ","

Here is unreserved of pchar:

  unreserved  = alphanum | mark

  mark= "-" | "_" | "." | "!" | "~" | "*" | "'" | "(" |
")"

In RFC 3986, they are a bit different, but my point here is "(" and
")".

The Uri from 4Suite return the results I expect:

  >>> import Ft.Lib.Uri as U
  >>> U.MatchesUriSyntax('http://example.com/path')
  True
  >>> U.MatchesUriSyntax('http://example.com/path)')
  True
  >>> U.MatchesUriSyntax('http://example.com/path}')
  False
  >>> U.MatchesUriSyntax('http://example.com/path]')
  False

I think you should use (read) RFC 3986 not RFC 3696 for URL
validation.

One more thing, HttpUrl()'s docstring should s/email/url/.

[1]: http://tools.ietf.org/html/rfc3696
[2]: http://tools.ietf.org/html/rfc2396
[3]: http://tools.ietf.org/html/rfc3986
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Teaching Programming

2010-05-04 Thread James Mills
On Tue, May 4, 2010 at 9:43 PM, Stefan Behnel  wrote:
>> Python 2.5.4 (r254:67916, Feb 17 2009, 20:16:45)
>> [GCC 4.3.3] on linux2
>> Type "help", "copyright", "credits" or "license" for more information.
>>  >>> A,B=2,3
>>  >>> if A>B:
>> ... print A+B
>> ... else:
>> ... print A**B-B**2
>> ...
>> -1
>>  >>> A,B=3,2
>>  >>> if A>B:
>> ... print A+B
>> ... else:
>> ... print A**B-B**2
>> ...
>> 5
>>
>> tell me please: how can generate the same output (depending on A and B)
>> without control structure? i mean in a natural "pythonic" way...

>>> def quadratic(a, b):
... return a + b if a > b else a**b - b**2
...
>>> a, b = 2, 3
>>> print quadratic(a, b)
-1
>>> a, b = 3, 2
>>> print quadratic(a, b)
5
>>>

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


Re: Teaching Programming

2010-05-04 Thread James Mills
On Tue, May 4, 2010 at 9:56 PM, superpollo  wrote:
> of course! *but* if i must generate on-the-fly python code that defines a
> function i am back again to the problem:

One-liner:

$ python
Python 2.6.5 (r265:79063, Apr 27 2010, 18:26:49)
[GCC 4.4.1 (CRUX)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> a, b = 2, 3
>>> print a + b if a > b else a**b - b**2
-1
>>> a, b = 3, 2
>>> print a + b if a > b else a**b - b**2
5
>>>

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


Re: itertools: problem with nested groupby, list()

2010-05-04 Thread Ulrich Eckhardt
Nico Schlömer wrote:
> So when I go like
> 
> for item in list:
> item[1].sort()
> 
> I actually modify *list*? I didn't realize that; I thought it'd just
> be a copy of it.

No, I misunderstood your code there. Modifying the objects inside the list
is fine, but I don't thing you do that, provided the items in the list
don't contain references to the list itself.

Good luck!

Uli

-- 
Sator Laser GmbH
Geschäftsführer: Thorsten Föcking, Amtsgericht Hamburg HR B62 932

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


Re: Teaching Programming

2010-05-04 Thread Ed Keith
--- On Tue, 5/4/10, Stefan Behnel  wrote:

> From: Stefan Behnel 
> Subject: Re: Teaching Programming
> To: [email protected]
> Date: Tuesday, May 4, 2010, 7:43 AM
> superpollo, 04.05.2010 13:23:
> > Stefan Behnel ha scritto:
> >> the main reason why this problem doesn't hurt much
> in Python
> >> is that Python is a dynamic language that can get
> you extremely far
> >> without generating code. It's simply not necessary
> in most cases, so
> >> people don't run into problems with it.
> >
> > Python 2.5.4 (r254:67916, Feb 17 2009, 20:16:45)
> > [GCC 4.3.3] on linux2
> > Type "help", "copyright", "credits" or "license" for
> more information.
> >  >>> A,B=2,3
> >  >>> if A>B:
> > ... print A+B
> > ... else:
> > ... print A**B-B**2
> > ...
> > -1
> >  >>> A,B=3,2
> >  >>> if A>B:
> > ... print A+B
> > ... else:
> > ... print A**B-B**2
> > ...
> > 5
> >
> > tell me please: how can generate the same output
> (depending on A and B)
> > without control structure? i mean in a natural
> "pythonic" way...
> 
> The question is: why do you have to generate the above code
> in the first 
> place? Isn't a function enough that does the above?
> 
> Stefan
> 
> -- 
> http://mail.python.org/mailman/listinfo/python-list
> 

Just thought I'd add my $0.02 here.

I wrote AsciiLitProg (http://asciilitprog.berlios.de/) in Python. It is a 
literate programming tool. It generates code from a document. It can generate 
code in any language the author wants. It would have been a LOT easier to write 
if it did not generate Python code.

Python is a great language to write in (although I do wish it did a better job 
with closures). But it is a PITA to generate code for!

   -EdK

Ed Keith
[email protected]

Blog: edkeith.blogspot.com




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


Re: Teaching Programming

2010-05-04 Thread Benjamin Kaplan
On Tue, May 4, 2010 at 7:23 AM, superpollo  wrote:
> Stefan Behnel ha scritto:
>>
>> superpollo, 04.05.2010 12:28:
>>>
>>> i think there is an issue if you -- say -- produce python code, from
>>> within another programming environment, to be executed on the fly, at
>>> least in some instances. there might be problems if for example you
>>> generate code from a one-line template.
>>
>> There are a couple of code generation tools available that you can find on
>> PyPI.
>>
>> However, the main reason why this problem doesn't hurt much in Python is
>> that Python is a dynamic language that can get you extremely far without
>> generating code. It's simply not necessary in most cases, so people don't
>> run into problems with it.
>>
>> Stefan
>>
>
> Python 2.5.4 (r254:67916, Feb 17 2009, 20:16:45)
> [GCC 4.3.3] on linux2
> Type "help", "copyright", "credits" or "license" for more information.
 A,B=2,3
 if A>B:
> ...     print A+B
> ... else:
> ...     print A**B-B**2
> ...
> -1
 A,B=3,2
 if A>B:
> ...     print A+B
> ... else:
> ...     print A**B-B**2
> ...
> 5

>
> tell me please: how can generate the same output (depending on A and B)
> without control structure? i mean in a natural "pythonic" way...
>
> bye

Well, it requires 2.6 or 3

Python 2.6.5 (r265:79063, Mar 21 2010, 22:38:52)
[GCC 4.2.1 (Apple Inc. build 5646) (dot 1)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> from __future__ import print_function
>>> A,B=2,3;print(A+B if A > B else A**B - B**2)
-1
>>> A,B=3,2;print(A+B if A > B else A**B - B**2)
5

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


Re: python gui

2010-05-04 Thread James Mills
On Tue, May 4, 2010 at 9:26 PM, a  wrote:
> where's the best online resource for teaching about GUI building?

There are many many resources available on the topic.
If you simply Google (tm) some of the keywords in your post
you'll be presented with a whole smorgasbord of useful resources.

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


Re: Teaching Programming

2010-05-04 Thread superpollo

James Mills ha scritto:

On Tue, May 4, 2010 at 9:56 PM, superpollo  wrote:

of course! *but* if i must generate on-the-fly python code that defines a
function i am back again to the problem:


One-liner:

$ python
Python 2.6.5 (r265:79063, Apr 27 2010, 18:26:49)
[GCC 4.4.1 (CRUX)] on linux2
Type "help", "copyright", "credits" or "license" for more information.

a, b = 2, 3
print a + b if a > b else a**b - b**2

-1

a, b = 3, 2
print a + b if a > b else a**b - b**2

5

--James


much obliged.

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


Re: Teaching Programming

2010-05-04 Thread Stefan Behnel

superpollo, 04.05.2010 13:56:

Stefan Behnel ha scritto:

The question is: why do you have to generate the above code in the
first place? Isn't a function enough that does the above?


of course! *but* if i must generate on-the-fly python code that defines
a function  [...]


Well, could you provide a use case where you have to generate Python code, 
and where normally written code with some kind of parametrisation won't work?


The only thing that currently comes to my mind is a template compiler that 
translates a user provided template into an executable Python program. But 
that should be pretty trivial to do as well. I just remembered this little 
thing on Fredrik's effbot site, which should help here:


http://effbot.org/zone/python-code-generator.htm

Stefan

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


Re: itertools: problem with nested groupby, list()

2010-05-04 Thread Nico Schlömer
> Are you basically after this, then?
>
> for a, a_iter in groupby(my_list, itemgetter('a')):
>print 'New A', a
>for b, b_iter in groupby(a_iter, itemgetter('b')):
>b_list = list(b_iter)
>for p in ['first', 'second']:
>for b_data in b_list:
>#whatever...

Yes. Moving the 'first', 'second' operation to the innermost loop
works all right, and I guess that's what I'll do.

> Cos that looks like it could be simplified to (untested)
> for (a, b), data_iter in groupby(my_list, itemgetter('a','b')):
>   data = list(data) # take copy
>   for pass_ in ['first', 'second']:
>  # do something with data

Potentially yes, but for now I actually need to do something at "print
'New A', a", so I can't just skip this.

Anyway, the above suggestion works well for now. Thanks!

--Nico






On Tue, May 4, 2010 at 1:52 PM, Jon Clements  wrote:
> On 4 May, 12:36, Nico Schlömer  wrote:
>> > Does this example help at all?
>>
>> Thanks, that clarified things a lot!
>>
>> To make it easier, let's just look at 'a' and 'b':
>>
>> > my_list.sort( key=itemgetter('a','b','c') )
>> > for a, a_iter in groupby(my_list, itemgetter('a')):
>> >    print 'New A', a
>> >    for b, b_iter in groupby(a_iter, itemgetter('b')):
>> >        print '\t', 'New B', b
>> >        for b_data in b_iter:
>> >            print '\t'*3, a, b, b_data
>> >        print '\t', 'End B', b
>> >    print 'End A', a
>>
>> That works well, and I can wrap the outer loop in another loop without
>> problems. What's *not* working, though, is having more than one pass
>> on the inner loop, as in
>>
>> === *snip* ===
>> my_list.sort( key=itemgetter('a','b','c') )
>> for a, a_iter in groupby(my_list, itemgetter('a')):
>>    print 'New A', a
>>    for pass in ['first pass', 'second pass']:
>>        for b, b_iter in groupby(a_iter, itemgetter('b')):
>>            print '\t', 'New B', b
>>            for b_data in b_iter:
>>                print '\t'*3, a, b, b_data
>>            print '\t', 'End B', b
>>        print 'End A', a
>> === *snap* ===
>>
>> I tried working around this by
>>
>> === *snip* ===
>> my_list.sort( key=itemgetter('a','b','c') )
>> for a, a_iter in groupby(my_list, itemgetter('a')):
>>    print 'New A', a
>>    inner_list =  list( groupby(a_iter, itemgetter('b')) )
>>    for pass in ['first pass', 'second pass']:
>>        for b, b_iter in inner_list:
>>            print '\t', 'New B', b
>>            for b_data in b_iter:
>>                print '\t'*3, a, b, b_data
>>            print '\t', 'End B', b
>>        print 'End A', a
>> === *snap* ===
>>
>> which don't work either, and I don't understand why. -- I'll look at
>> Uli's comments.
>>
>> Cheers,
>> Nico
>>
>> On Tue, May 4, 2010 at 1:08 PM, Jon Clements  wrote:
>> > On 4 May, 11:10, Nico Schlömer  wrote:
>> >> Hi,
>>
>> >> I ran into a bit of an unexpected issue here with itertools, and I
>> >> need to say that I discovered itertools only recently, so maybe my way
>> >> of approaching the problem is "not what I want to do".
>>
>> >> Anyway, the problem is the following:
>> >> I have a list of dictionaries, something like
>>
>> >> [ { "a": 1, "b": 1, "c": 3 },
>> >>   { "a": 1, "b": 1, "c": 4 },
>> >>   ...
>> >> ]
>>
>> >> and I'd like to iterate through all items with, e.g., "a":1. What I do
>> >> is sort and then groupby,
>>
>> >> my_list.sort( key=operator.itemgetter('a') )
>> >> my_list_grouped = itertools.groupby( my_list, operator.itemgetter('a') )
>>
>> >> and then just very simply iterate over my_list_grouped,
>>
>> >> for my_item in my_list_grouped:
>> >>     # do something with my_item[0], my_item[1]
>>
>> >> Now, inside this loop I'd like to again iterate over all items with
>> >> the same 'b'-value -- no problem, just do the above inside the loop:
>>
>> >> for my_item in my_list_grouped:
>> >>         # group by keyword "b"
>> >>         my_list2 = list( my_item[1] )
>> >>         my_list2.sort( key=operator.itemgetter('b') )
>> >>         my_list_grouped = itertools.groupby( my_list2,
>> >> operator.itemgetter('b') )
>> >>         for e in my_list_grouped:
>> >>             # do something with e[0], e[1]
>>
>> >> That seems to work all right.
>>
>> >> Now, the problem occurs when this all is wrapped into an outer loop, such 
>> >> as
>>
>> >> for k in [ 'first pass', 'second pass' ]:
>> >>     for my_item in my_list_grouped:
>> >>     # bla, the above
>>
>> >> To be able to iterate more than once through my_list_grouped, I have
>> >> to convert it into a list first, so outside all loops, I go like
>>
>> >> my_list.sort( key=operator.itemgetter('a') )
>> >> my_list_grouped = itertools.groupby( my_list, operator.itemgetter('a') )
>> >> my_list_grouped = list( my_list_grouped )
>>
>> >> This, however, makes it impo

Re: Teaching Programming

2010-05-04 Thread Stefan Behnel

Ed Keith, 04.05.2010 14:15:

I wrote AsciiLitProg (http://asciilitprog.berlios.de/) in Python. It is
a literate programming tool. It generates code from a document. It can
generate code in any language the author wants. It would have been a LOT
easier to write if it did not generate Python code.

Python is a great language to write in (although I do wish it did a
better job with closures). But it is a PITA to generate code for!


Interesting. Could you elaborate a bit? Could you give a short example of 
what kind of document text you translate into what kind of Python code, and 
what the problems were that you faced in doing so?


Stefan

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


Re: Teaching Programming

2010-05-04 Thread superpollo

Stefan Behnel ha scritto:

superpollo, 04.05.2010 13:56:

Stefan Behnel ha scritto:

The question is: why do you have to generate the above code in the
first place? Isn't a function enough that does the above?


of course! *but* if i must generate on-the-fly python code that defines
a function  [...]


Well, could you provide a use case where you have to generate Python 
code, and where normally written code with some kind of parametrisation 
won't work?


i see your point, and mr mills gave me hints as to how to attack some of 
my problems in such a fashion.


The only thing that currently comes to my mind is a template compiler 
that translates a user provided template into an executable Python 
program. But that should be pretty trivial to do as well. I just 
remembered this little thing on Fredrik's effbot site, which should help 
here:


http://effbot.org/zone/python-code-generator.htm


very good. i will most certainly give it a try.

but i do not think i can use it myself, since my template system wants 
the input to generate the code to stay on a single line ( don't ask :-( )


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


Re: strange interaction between open and cwd

2010-05-04 Thread Baz Walter

On 04/05/10 02:12, Ben Finney wrote:

Baz Walter  writes:


On 03/05/10 18:41, Grant Edwards wrote:

Firstly, a file may have any number of paths (including 0).


yes, of course. i forgot about hard links


Rather, you forgot that *every* entry that references a file is a hard
link.


i'm not a frequent poster on this list, but i'm aware of it's reputation 
for pointless pedantry ;-)


but what the heck, when in rome...

note that i said hard links (plural) - i think a more generous reader 
would assume i was referring to additional hard links.


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


Re: strange interaction between open and cwd

2010-05-04 Thread Baz Walter

On 04/05/10 03:19, Grant Edwards wrote:

On 2010-05-03, Baz Walter  wrote:

On 03/05/10 19:12, Grant Edwards wrote:

Even though the user provided a legal and openable path?


that sounds like an operational definition to me: what's the
difference between "legal" and "openable"?


Legal as in meets the syntactic requirements for a path (not sure if
there really are any requirements other than it being a
null-terminated string).  Openable meaning that it denotes a path file
that exists and for which the caller has read permissions on the file
and execute premissions on the directories within the path.


openable is not the same as accessible. a file can still openable, even 
though a user may not have permission to access it.


a better definition of "legal path" might be whether any useful 
information can be gained from a stat() call on it.

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


Re: [OT] strange interaction between open and cwd

2010-05-04 Thread Charles

"Gregory Ewing"  wrote in message 
news:[email protected]...
> Charles wrote:
>
>> In the OP's case, references to the directory have been removed
>> from the file system, but his process still has the current working
>> directory reference to it, so it has not actually been deleted.
>> When he opens "../abc.txt", the OS  searches the current directory
>> for ".." and finds the inode for /home/baz/tmp,
>
> This doesn't seem to be quite correct. An experiment I just did
> reveals that the link count on the parent directory goes down by
> one when the current directory is deleted, suggesting that the ..
> link has actually been removed... yet it still works!
>
> I think what must be happening is that the kernel is maintaining
> an in-memory reference to the parent directory, and treating ".."
> as a special case when looking up a name.
>
> (This probably shouldn't be too surprising, because ".." is special
> in another way as well -- at the root of a mounted file system, it
> leads to the parent of the mount point, even though the actual ".."
> link on disk just points back to the same directory. Probably it
> simplifies the name lookup logic to always treat it specially.)

I am by no means an expert in this area, but what I think
happens (and I may well be wrong) is that the directory
is deleted on the file system. The link from the parent
is removed, and the parent's link count is decremented,
as you observed, but the directory itself is still intact with
it's original contents, including the "." and ".." names and
associated inode numbers. Unix does not normally zero
out files on deletion - the file's blocks usually retain their
contents, and I would not expect directories to be
a special case.

The blocks from the directory will be re-cycled
when the memory reference (the process's current
working directory) disappears, but until then the directory
and its contents are still accessible via the process's current
directory. This is all supposition, and based on distant
memories form the mid 1980s, I could very well be
wrong.


Charles



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


Re: Teaching Programming

2010-05-04 Thread Stefan Behnel

superpollo, 04.05.2010 14:46:

my template system wants
the input to generate the code to stay on a single line ( don't ask :-( )


I hope you don't mind if I still ask. What are you generating and for what 
templating system?


Stefan

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


Re: Teaching Programming

2010-05-04 Thread Ed Keith
--- On Tue, 5/4/10, Stefan Behnel  wrote:

> From: Stefan Behnel 
> Subject: Re: Teaching Programming
> To: [email protected]
> Date: Tuesday, May 4, 2010, 8:40 AM
> Ed Keith, 04.05.2010 14:15:
> > I wrote AsciiLitProg (http://asciilitprog.berlios.de/) in Python. It is
> > a literate programming tool. It generates code from a
> document. It can
> > generate code in any language the author wants. It
> would have been a LOT
> > easier to write if it did not generate Python code.
> > 
> > Python is a great language to write in (although I do
> wish it did a
> > better job with closures). But it is a PITA to
> generate code for!
> 
> Interesting. Could you elaborate a bit? Could you give a
> short example of what kind of document text you translate
> into what kind of Python code, and what the problems were
> that you faced in doing so?
> 
> Stefan
> 
> -- http://mail.python.org/mailman/listinfo/python-list
> 

The program is written using itself. If you click on the link above you will 
see an HTML file that fully describes the program. That HTML is generated from 
an AcsiiDoc (http://www.methods.co.nz/asciidoc/) document. The same document is 
used to generate the python code that it describes.
The source document, and the generated HTML and Python code are all avalable at 
BerliOS (http://developer.berlios.de/projects/asciilitprog/).

For more information on Literate Programming in general see the following links.

http://www.literateprogramming.com/
http://en.wikipedia.org/wiki/Literate_programming
http://en.literateprograms.org/LiteratePrograms:Welcome


   -EdK

Ed Keith
[email protected]

Blog: edkeith.blogspot.com




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


Re: Teaching Programming

2010-05-04 Thread superpollo

Stefan Behnel ha scritto:

superpollo, 04.05.2010 14:46:

my template system wants
the input to generate the code to stay on a single line ( don't ask :-( )


I hope you don't mind if I still ask. What are you generating and for 
what templating system?


ok, since you asked for it, prepare yourself for a bit of a horror story ;-)

i will answer in my next post

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


Re: strange interaction between open and cwd

2010-05-04 Thread Ben Finney
Baz Walter  writes:

> On 04/05/10 02:12, Ben Finney wrote:
> > Baz Walter  writes:
> >> yes, of course. i forgot about hard links
> >
> > Rather, you forgot that *every* entry that references a file is a
> > hard link.
>
> i'm not a frequent poster on this list, but i'm aware of it's
> reputation for pointless pedantry ;-)

Only pointless if you view this as a conversation entirely for the
benefit of you and I. I, on the other hand, am trying to make this
useful to whoever may read it now and in the future.

> note that i said hard links (plural) - i think a more generous reader
> would assume i was referring to additional hard links.

The point, though, was that this is normal operation, rather than
exceptional. Files have zero or more hard links, and “this file has
exactly one hard link” is merely a common case among that spectrum.

I'm glad you already knew this, and hope you can appreciate that it's
better explicit than implicit.

-- 
 \“The difference between religions and cults is determined by |
  `\  how much real estate is owned.” —Frank Zappa |
_o__)  |
Ben Finney
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: [OT] strange interaction between open and cwd

2010-05-04 Thread Nobody
On Tue, 04 May 2010 23:02:29 +1000, Charles wrote:

> I am by no means an expert in this area, but what I think happens (and I
> may well be wrong) is that the directory is deleted on the file system.
> The link from the parent is removed, and the parent's link count is
> decremented, as you observed, but the directory itself is still intact
> with it's original contents, including the "." and ".." names and
> associated inode numbers. Unix does not normally zero out files on
> deletion - the file's blocks usually retain their contents, and I would
> not expect directories to be a special case.

You are correct.

System calls don't "delete" inodes (files, directories, etc), they
"unlink" them. Deletion occurs when the inode's link count reaches zero
and no process holds a reference to the inode (a reference could be a
descriptor, or the process' cwd, chroot directory, or an mmap()d file, etc).

IOW, reference-counted garbage collection.

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


Re: strange interaction between open and cwd

2010-05-04 Thread Baz Walter

On 04/05/10 09:23, Gregory Ewing wrote:

Grant Edwards wrote:


In your example, it's simply not possible to determine the file's
absolute path within the filesystem given the relative path you
provided.


Actually, I think it *is* theoretically possible to find an
absolute path for the file in this case.

I suspect that what realpath() is doing for a relative path is
something like:

1. Use getcwd() to find an absolute path for the current
directory.
2. Chop off a trailing pathname component for each ".."
on the front of the original path.
3. Tack the filename on the end of what's left.

Step 1 fails because the current directory no longer has
an absolute pathname -- specifically, it has no name in
what used to be its parent directory.

What realpath() is failing to realise is that it doesn't
actually need to know the full path of the current directory,
only of its parent directory, which is still reachable via
".." (if it weren't, the file wouldn't be reachable either,
and we wouldn't be having this discussion).

A smarter version of realpath() wouldn't try to find the
path of the *current* directory, but would follow the
".." links until it got to a directory that it did need to
know an absolute path for, and start with that.

Unfortunately, there is no C stdlib routine that does the
equivalent of getcwd() for an arbitrary directory, so
this would require realpath() to duplicate much of
getcwd()'s functionality, which is probably why it's
done the way it is.


actually, this part of the problem can be achieved using pure python. 
given the basename of a file, all you have to do is use os.stat and 
os.listdir to recursively climb up the tree and build a dirpath for it. 
start by doing os.stat(basename) to make sure you have a legal file in 
the current directory; then use os.stat('..') to get the parent 
directory inode, and stat each of the items in os.listdir('../..') to 
find a name matching that inode etc. (note that the possibility of 
hard-linked directories doesn't really spoil this - for relative paths, 
we don't care exactly which absolute path is found).


this will work so long as the file is in a part of the filesystem that 
can be traversed from the current directory to the root. what i'm not 
sure about is whether it's possible to cross filesystem boundaries using 
this kind of technique.


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


Re: strange interaction between open and cwd

2010-05-04 Thread Nobody
On Tue, 04 May 2010 20:08:36 +1200, Gregory Ewing wrote:

>> except that Python objects can form a generalized graph, and Unix
>> filesystems are constrained to be a tree.
> 
> Actually I believe that root is allowed to create arbitrary hard links to
> directories in Unix, so it's possible to turn the file system in to a
> general graph. It's highly unrecommended, though, because it confuses the
> heck out of programs that recursively traverse directories (which is why
> only root is allowed to do it).

The 1980s are over already ;)

I don't know of any modern Unix which allows creating additional hard
links to directories (even for root). It creates too many problems, and
isn't necessary if you have symlinks.

If you want to turn the filesystem into a cyclic graph, you typically need
to bypass the kernel's filesystem interface and modify the block device
directly (e.g. with debugfs). If fsck finds out, it's likely to fix it.

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


Re: design question

2010-05-04 Thread Tim Arnold
On May 4, 3:39 am, Bruno Desthuilliers  wrote:
> Alf P. Steinbach a écrit :
> (snip)
>
> > Re efficiency it seems to be a complete non-issue, but correctness is
> > much more important: is there any way that the config details can be
> > (inadvertently) changed while the build is going on?
>
> +1

Thanks everyone, I'm new at this type of development so it helps to
get input from folks who know what they're talking about. I'm just
been coding and doing what is right in my own eyes...

There is almost no way for the configuration to change during a build,
so it looks like I was just worrying about something that doesn't need
worry.

Now on to the things that do!

thanks again,
--Tim
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: strange interaction between open and cwd

2010-05-04 Thread Baz Walter

On 04/05/10 09:08, Gregory Ewing wrote:

Grant Edwards wrote:


except that Python objects can form a generalized graph, and Unix
filesystems are constrained to be a tree.


Actually I believe that root is allowed to create arbitrary
hard links to directories in Unix, so it's possible to turn
the file system in to a general graph. It's highly
unrecommended, though, because it confuses the heck out of
programs that recursively traverse directories (which is
why only root is allowed to do it).


i think there are versions of mac osx that use hard-linked directories 
in their backup systems.

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


Re: strange interaction between open and cwd

2010-05-04 Thread Nobody
On Mon, 03 May 2010 06:18:55 -0700, Chris Rebert wrote:

>> but how can python determine the
>> parent directory of a directory that no longer exists?
> 
> Whether or not /home/baz/tmp/xxx/ exists, we know from the very structure
> and properties of directory paths that its parent directory is, *by
> definition*, /home/baz/tmp/ (just chop off everything after the
> second-to-last slash).

Not necessarily.

There are two common interpretations for the concept of a "parent
directory". One is the directory referenced by removing the last component
from the (normalised) path, the other is the directory referenced by
appending ".." to the path.

The two don't necessarily refer to the same directory if the last
component is a symlink (or an additional hard link, on platforms which
allow creating additional hard links to directories).

For the current directory[1], or a directory referenced by a descriptor
rather than a path, the latter definition has to be used, as there is no
path to manipulate.

[1] And also for e.g. "./../..".

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


Re: Teaching Programming

2010-05-04 Thread alex23
Ed Keith  wrote:
> For more information on Literate Programming in general see the following 
> links.

None of which address the question of what you found problematic about
generating Python code. Was it issues with indentation?


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


RE: How to get xml.etree.ElementTree not bomb on invalid characters in XML file ?

2010-05-04 Thread Barak, Ron
 
> -Original Message-
> From: Stefan Behnel [mailto:[email protected]] 
> Sent: Tuesday, May 04, 2010 10:24 AM
> To: [email protected]
> Subject: Re: How to get xml.etree.ElementTree not bomb on 
> invalid characters in XML file ?
> 
> Barak, Ron, 04.05.2010 09:01:
> >  I'm parsing XML files using ElementTree from xml.etree (see code 
> > below (and attached xml_parse_example.py)).
> >
> > However, I'm coming across input XML files (attached an example:
> > tmp.xml) which include invalid characters, that produce the 
> following
> > traceback:
> >
> > $ python xml_parse_example.py
> > Traceback (most recent call last):
> > xml.parsers.expat.ExpatError: not well-formed (invalid 
> token): line 6, 
> > column 34
> 
> I hope you are aware that this means that the input you are 
> parsing is not XML. It's best to reject the file and tell the 
> producers that they are writing broken output files. You 
> should always fix the source, instead of trying to make sense 
> out of broken input in fragile ways.
> 
> 
> > I read the documentation for xml.etree.ElementTree and see 
> that it may 
> > take an optional parser parameter, but I don't know what 
> this parser 
> > should be - to ignore the invalid characters.
> >
> > Could you suggest a way to call ElementTree, so it won't 
> bomb on these 
> > invalid characters ?
> 
> No. The parser in lxml.etree has a 'recover' option that lets 
> it try to recover from input errors, but in general, XML 
> parsers are required to reject non well-formed input.
> 
> Stefan
> 
> 
> 

Hi Stefan,
The XML file seems to be valid XML (all XML viewers I tried were able to read 
it). 
You can verify this by trying to read the XML example I attached to the 
original message (attached again here).
Actually, when trying to view the file with an XML viewer, these offensive 
characters are not shown.
It's just that some of the fields include characters that the parser used by 
ElementTree seems to chock on.
Bye,
Ron.

tmp_small.xml
Description: tmp_small.xml
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Django as exemplary design

2010-05-04 Thread alex23
TomF  wrote:
> I'm interested in improving my python design by studying a large,
> well-designed codebase.  Someone (not a python programmer) suggested
> Django.  I realize that Django is popular, but can someone comment on
> whether its code is well-designed and worth studying?

Here's a viewpoint that says no: 
http://mockit.blogspot.com/2010/04/mess-djangos-in.html

There's a lot of good counterpoint in the comments too.

(I also think there's value to be gained in studying _bad_ code,
too...)
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: itertools: problem with nested groupby, list()

2010-05-04 Thread Peter Otten
Nico Schlömer wrote:

> Hi,
> 
> I ran into a bit of an unexpected issue here with itertools, and I
> need to say that I discovered itertools only recently, so maybe my way
> of approaching the problem is "not what I want to do".
> 
> Anyway, the problem is the following:
> I have a list of dictionaries, something like
> 
> [ { "a": 1, "b": 1, "c": 3 },
>   { "a": 1, "b": 1, "c": 4 },
>   ...
> ]
> 
> and I'd like to iterate through all items with, e.g., "a":1. What I do
> is sort and then groupby,
> 
> my_list.sort( key=operator.itemgetter('a') )
> my_list_grouped = itertools.groupby( my_list, operator.itemgetter('a') )
> 
> and then just very simply iterate over my_list_grouped,
> 
> for my_item in my_list_grouped:
> # do something with my_item[0], my_item[1]
> 
> Now, inside this loop I'd like to again iterate over all items with
> the same 'b'-value -- no problem, just do the above inside the loop:
> 
> for my_item in my_list_grouped:
> # group by keyword "b"
> my_list2 = list( my_item[1] )
> my_list2.sort( key=operator.itemgetter('b') )
> my_list_grouped = itertools.groupby( my_list2,
> operator.itemgetter('b') )
> for e in my_list_grouped:
> # do something with e[0], e[1]
> 
> That seems to work all right.
> 
> Now, the problem occurs when this all is wrapped into an outer loop, such
> as
> 
> for k in [ 'first pass', 'second pass' ]:
> for my_item in my_list_grouped:
> # bla, the above
> 
> To be able to iterate more than once through my_list_grouped, I have
> to convert it into a list first, so outside all loops, I go like
> 
> my_list.sort( key=operator.itemgetter('a') )
> my_list_grouped = itertools.groupby( my_list, operator.itemgetter('a') )
> my_list_grouped = list( my_list_grouped )
> 
> This, however, makes it impossible to do the inner sort and
> groupby-operation; you just get the very first element, and that's it.
> 
> An example file is attached.
> 
> Hints, anyone?

If you want a reusable copy of a groupby(...) it is not enough to convert it 
to a list as a whole:

>>> from itertools import groupby
>>> from operator import itemgetter
>>> items = [(1,1), (1,2), (1,3), (2,1), (2,2)]
>>> grouped_items = list(groupby(items, key=itemgetter(0))) # WRONG
>>> for run in 1, 2:
... print "run", run
... for k, g in grouped_items:
... print k, list(g)
...
run 1
1 []
2 [(2, 2)]
run 2
1 []
2 []

Instead, you have to process the groups, too:

>>> grouped_items = [(k, list(g)) for k, g in groupby(items, 
key=itemgetter(0))]
>>> for run in 1, 2:
... print "run", run
... for k, g in grouped_items:
... print k, list(g)
...
run 1
1 [(1, 1), (1, 2), (1, 3)]
2 [(2, 1), (2, 2)]
run 2
1 [(1, 1), (1, 2), (1, 3)]
2 [(2, 1), (2, 2)]

But usually you don't bother and just run groupby() twice:

>>> for run in 1, 2:
... print "run", run
... for k, g in groupby(items, key=itemgetter(0)):
... print k, list(g)
...
run 1
1 [(1, 1), (1, 2), (1, 3)]
2 [(2, 1), (2, 2)]
run 2
1 [(1, 1), (1, 2), (1, 3)]
2 [(2, 1), (2, 2)]

The only caveat then is that list(items) == list(items) must hold.

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


py3 tkinter acceps bytes. why?

2010-05-04 Thread Matthias Kievernagel
From: Matthias Kievernagel 
Subject: py3 tkinter acceps bytes. why?
Newsgroups: comp.lang.python
Summary: 
Keywords: 

In a recent thread named "py3 tkinter Text accepts what bytes?"
(google groups link:
http://groups.google.com/group/comp.lang.python/browse_thread/thread/b75ed69f4e81b202/e2aff9ddd62d210c?lnk=raot)
I asked what kinds of bytes are accepted as tkinter parameters.
I still wonder why they are accepted at all.
Does anyone know a reason for this
or has a link to some relevant discussion?

Thanks for any hint,
Matthias Kievernagel


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


Re: Teaching Programming

2010-05-04 Thread Ed Keith
--- On Tue, 5/4/10, alex23  wrote:

> From: alex23 
> Subject: Re: Teaching Programming
> To: [email protected]
> Date: Tuesday, May 4, 2010, 10:06 AM
> Ed Keith 
> wrote:
> > For more information on Literate Programming in
> general see the following links.
> 
> None of which address the question of what you found
> problematic about
> generating Python code. Was it issues with indentation?
> 
> 
> -- 
> http://mail.python.org/mailman/listinfo/python-list
> 

Yes, the problem was indentation.

To deal with indentation I had to 

   1) keep track of indentation of all chunks of code embedded in the 
  document and indent inserted chunks to the sum of all the 
  indentation of the enclosing chunks.

and

   2) expand all tabls to spaces and allow the user to set the tab 
  expansion size from the command line. Fortunately Python provides 
  good support for this.

Neither of these problems exist in languages that are not indention sensitive.

Tabs are always a problem when writing Python. I get around this problem by 
setting my text editor to expand all tabs with spaces when editing Python, but 
I have had problems when coworkers have not done this.

Don't get me wrong, I love working in Python. It is one of my favorite 
languages, but it, like all other languages, is not perfect.

-EdK

Ed Keith
[email protected]

Blog: edkeith.blogspot.com





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


RE: Django as exemplary design

2010-05-04 Thread Michael . Coll-Barth

> From: alex23

> (I also think there's value to be gained in studying _bad_ code,
> too...)

Oh, very true.  And not just true for python.  But, only if an 'expoert'
points out why it is bad and provides an alternative.  And saying things
like, "it isn't pyhonic" or that such and such is a more "pythonic way"
is NOT helpful.







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


Re: Teaching Programming

2010-05-04 Thread James Mills
On Wed, May 5, 2010 at 12:21 AM, Ed Keith  wrote:
> To deal with indentation I had to
>
>   1) keep track of indentation of all chunks of code embedded in the
>      document and indent inserted chunks to the sum of all the
>      indentation of the enclosing chunks.

In my experience of non-indentation sensitive languages
such as C-class (curly braces) it's just as hard to keep track
of opening and closing braces.

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


Re: [OT] strange interaction between open and cwd

2010-05-04 Thread Baz Walter

On 04/05/10 03:25, Grant Edwards wrote:

On 2010-05-04, Charles  wrote:


I don't see how it's inelegant at all.  Perhaps it's
counter-intuitive if you don't understand how a Unix filesystem
works, but the underlying filesystem model is very simple, regular,
and elegant.


but probably makes some bit of the OS's job slightly easier and is
usually good enough in practice. Pragmatism is a bitch sometimes. :-)




I agree that the Unix file system is quite elegant, but can be
counter-intuitive for people who are used to the "one file, one name"
paradigm.


I guess I've been using Unix for too long (almost 30 years).  I don't
think I was consciously aware of a "one file, one name" paradigm.  Is
that a characteristic of Dos, Windows or Mac filesystems?


[...]



In the OP's case, references to the directory have been removed from
the file system, but his process still has the current working
directory reference to it, so it has not actually been deleted. When
he opens "../abc.txt", the OS searches the current directory for ".."
and finds the inode for /home/baz/tmp, then searches that directory
(/home/baz/tmp) for abc.txt and finds it.


Exactly.  I probably should have taken the time to explain that as
well as you did.  One forgets that there are a log of new Unix users
who've never been taught how the filesystem works.


actually, what i failed to grok is that whatever '..' refers to is all 
that is needed to find the file 'abc.txt' *even if the current directory 
has been deleted*. an absolute path just isn't needed. this has really 
got nothing to do with how unix filesystems work per se or "one file, 
one name", but more to do with simple reference counting. when i 
mentioned in my original post how windows handles attempts to delete the 
cwd differently, i should have put two and two together and figured this 
all out for myself. but i don't think it's immediately obvious what the 
consequences of (re)moving the cwd are, even if you've been using unix 
filesystems for a while. in know for a fact that i have used several 
linux programs in the past that didn't handle this possiblity 
gracefully, so it's not just new users that can be caught out by this.

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


Re: strange interaction between open and cwd

2010-05-04 Thread Grant Edwards
On 2010-05-04, Gregory Ewing  wrote:
> Grant Edwards wrote:
>
>> except that Python objects can form a generalized graph, and Unix
>> filesystems are constrained to be a tree.
>
> Actually I believe that root is allowed to create arbitrary
> hard links to directories in Unix,

I know that used to be the case in SunOS (as I found out the hard
way), but I've been told by Solaris experts that it is no longer
allowed.  I was also under the impression that it wasn't allowed in
Linux, but I've never tried it.

According the the Linux ln (1) man page:

   -d, -F, --directory

  allow the superuser to attempt to hard link directories (note:
  will probably fail due to system restrictions, even for the
  superuser)
  
> so it's possible to turn the file system in to a general graph. It's
> highly unrecommended, though, because it confuses the heck out of
> programs that recursively traverse directories (which is why only
> root is allowed to do it).

It sure caused trouble when I did it on a SunOS system back in the day
(1990 or thereabouts).

-- 
Grant Edwards   grant.b.edwardsYow! I feel like I'm
  at   in a Toilet Bowl with a
  gmail.comthumbtack in my forehead!!
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Teaching Programming

2010-05-04 Thread Andre Engels
On Tue, May 4, 2010 at 4:35 PM, James Mills
 wrote:
> On Wed, May 5, 2010 at 12:21 AM, Ed Keith  wrote:
>> To deal with indentation I had to
>>
>>   1) keep track of indentation of all chunks of code embedded in the
>>      document and indent inserted chunks to the sum of all the
>>      indentation of the enclosing chunks.
>
> In my experience of non-indentation sensitive languages
> such as C-class (curly braces) it's just as hard to keep track
> of opening and closing braces.

Although I have little or no experience with this, I still dare to say
that I don't agree. The difference is that in C you do not _need_ to
know where in the braces-defined hierarchy you are. You just embed or
change a piece of code at the right location. In Python however you
_do_ need to know how far your code is to be indented.


-- 
André Engels, [email protected]
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Teaching Programming

2010-05-04 Thread Ed Keith
--- On Tue, 5/4/10, James Mills  wrote:

> From: James Mills 
> Subject: Re: Teaching Programming
> To: "python list" 
> Date: Tuesday, May 4, 2010, 10:35 AM
> On Wed, May 5, 2010 at 12:21 AM, Ed
> Keith 
> wrote:
> > To deal with indentation I had to
> >
> >   1) keep track of indentation of all chunks of code
> embedded in the
> >      document and indent inserted chunks to the sum
> of all the
> >      indentation of the enclosing chunks.
> 
> In my experience of non-indentation sensitive languages
> such as C-class (curly braces) it's just as hard to keep
> track
> of opening and closing braces.
> 
> --James
> -- 
> http://mail.python.org/mailman/listinfo/python-list
> 

Not in this case, because the user is required to include all text of the 
program in the chunks, the tools does not generate any text, It only needs to 
generate white space if the language is white space sensitive. 

I can see how it could be a problem in other cases.

In the interest of completeness, I should mention that I had to take care not 
to introduce extraneous line breaks for languages that are sensitive to them. 
It is much easier to generate code for languages that are completely white 
space agnostic. 

I actually find the fact that I need to think about where I can, and where I 
can not, break a line to be a bigger problem when write code, than keeping 
track of indentation level. And Python is not the only language that has this 
problem.

-EdK

Ed Keith
[email protected]

Blog: edkeith.blogspot.com




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


Re: Teaching Programming

2010-05-04 Thread Ed Keith
--- On Tue, 5/4/10, Andre Engels  wrote:

> From: Andre Engels 
> Subject: Re: Teaching Programming
> To: "James Mills" 
> Cc: "python list" 
> Date: Tuesday, May 4, 2010, 11:00 AM
> On Tue, May 4, 2010 at 4:35 PM, James
> Mills
> 
> wrote:
> > On Wed, May 5, 2010 at 12:21 AM, Ed Keith 
> wrote:
> >> To deal with indentation I had to
> >>
> >>   1) keep track of indentation of all chunks of
> code embedded in the
> >>      document and indent inserted chunks to the
> sum of all the
> >>      indentation of the enclosing chunks.
> >
> > In my experience of non-indentation sensitive
> languages
> > such as C-class (curly braces) it's just as hard to
> keep track
> > of opening and closing braces.
> 
> Although I have little or no experience with this, I still
> dare to say
> that I don't agree. The difference is that in C you do not
> _need_ to
> know where in the braces-defined hierarchy you are. You
> just embed or
> change a piece of code at the right location. In Python
> however you
> _do_ need to know how far your code is to be indented.
> 
> 

For a programmer, it is harder to keep track of braced.

For a code generator, it is harder to keep track of indentation.

It is a matter of which you are more interested in catering to. Python is 
easier to write, C is easier to generate.

-EdK

Ed Keith
[email protected]

Blog: edkeith.blogspot.com





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


Re: Teaching Programming

2010-05-04 Thread Ethan Furman

Andre Engels wrote:

On Tue, May 4, 2010 at 4:35 PM, James Mills
 wrote:

On Wed, May 5, 2010 at 12:21 AM, Ed Keith  wrote:

To deal with indentation I had to

  1) keep track of indentation of all chunks of code embedded in the
 document and indent inserted chunks to the sum of all the
 indentation of the enclosing chunks.

In my experience of non-indentation sensitive languages
such as C-class (curly braces) it's just as hard to keep track
of opening and closing braces.


Although I have little or no experience with this, I still dare to say
that I don't agree. The difference is that in C you do not _need_ to
know where in the braces-defined hierarchy you are. You just embed or
change a piece of code at the right location. In Python however you
_do_ need to know how far your code is to be indented.


And how do you know where the right location is?  You have to figure it 
out from the different (nested) braces at the 'right' location.  For me, 
at least, it's much easier to get that information from the already 
indented Python code, as opposed to indenting (and double-checking the 
indents) on the braces language.


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


Re: HTTP server + SQLite?

2010-05-04 Thread Gilles Ganault
On Mon, 3 May 2010 23:07:08 -0700 (PDT), Bryan
 wrote:
>I love SQLite because it solves problems I actually have. For the vast
>majority of code I write, "lite" is a good thing, and lite as it is,
>SQLite can handle several transactions per second. I give SQLite a
>file path and in a split second I have a relational, transactional
>database. Great. I did not want to configure a server and I sure did
>not want to inflict complexity upon my users.

Exactly. I need a safe way to share an SQLite database among a few
years, but sharing a drive isn't safe but I also don't need a
full-fledged DBMS like MySQL.

At this point, as an alternative to a Python-based solution, it seems
like Mongoose + Lua + SQLite could do the trick.
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Teaching Programming

2010-05-04 Thread Stefan Behnel

Ed Keith, 04.05.2010 15:19:

--- On Tue, 5/4/10, Stefan Behnel wrote:

Ed Keith, 04.05.2010 14:15:

Python is a great language to write in (although I do
wish it did a better job with closures). But it is a PITA to
generate code for!


Interesting. Could you elaborate a bit? Could you give a
short example of what kind of document text you translate
into what kind of Python code, and what the problems were
that you faced in doing so?


The program is written using itself. If you click on the link above you
will see an HTML file that fully describes the program.


I did. I find it a bit hard to read due to the block splitting (basically 
like 'include' based spaghetti programming), but so far, the actual code 
that does the code merging looks pretty simple and I can't figure out where 
the "PITA" bit is on that page. That's why I asked.


Stefan

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


Re: How to get xml.etree.ElementTree not bomb on invalid characters in XML file ?

2010-05-04 Thread Stefan Behnel

Barak, Ron, 04.05.2010 16:11:

  I'm parsing XML files using ElementTree from xml.etree (see code
below (and attached xml_parse_example.py)).

However, I'm coming across input XML files (attached an example:
tmp.xml) which include invalid characters, that produce the
following traceback:

$ python xml_parse_example.py
Traceback (most recent call last):
xml.parsers.expat.ExpatError: not well-formed (invalid
token): line 6, column 34


I hope you are aware that this means that the input you are
parsing is not XML. It's best to reject the file and tell the
producers that they are writing broken output files. You
should always fix the source, instead of trying to make sense
out of broken input in fragile ways.


The XML file seems to be valid XML (all XML viewers I tried were able to read 
it).


This is what xmllint gives me:

---
$ xmllint /home/sbehnel/tmp.xml
tmp.xml:6: parser error : Char 0x0 out of allowed range
  "MainStorage_snap
  ^
tmp.xml:6: parser error : Premature end of data in tag m_sanApiName1 line 6
  "MainStorage_snap
  ^
tmp.xml:6: parser error : Premature end of data in tag DbHbaGroup line 5
  "MainStorage_snap
  ^
tmp.xml:6: parser error : Premature end of data in tag database line 4
  "MainStorage_snap
  ^
---

The file contains 0-bytes - clearly not XML.

Stefan

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


Re: Teaching Programming

2010-05-04 Thread Ed Keith
--- On Tue, 5/4/10, Stefan Behnel  wrote:

> From: Stefan Behnel 
> Subject: Re: Teaching Programming
> To: [email protected]
> Date: Tuesday, May 4, 2010, 11:33 AM
> Ed Keith, 04.05.2010 15:19:
> > --- On Tue, 5/4/10, Stefan Behnel wrote:
> >> Ed Keith, 04.05.2010 14:15:
> >>> Python is a great language to write in
> (although I do
> >>> wish it did a better job with closures). But
> it is a PITA to
> >>> generate code for!
> >> 
> >> Interesting. Could you elaborate a bit? Could you
> give a
> >> short example of what kind of document text you
> translate
> >> into what kind of Python code, and what the
> problems were
> >> that you faced in doing so?
> > 
> > The program is written using itself. If you click on
> the link above you
> > will see an HTML file that fully describes the
> program.
> 
> I did. I find it a bit hard to read due to the block
> splitting (basically like 'include' based spaghetti
> programming), but so far, the actual code that does the code
> merging looks pretty simple and I can't figure out where the
> "PITA" bit is on that page. That's why I asked.
> 
> Stefan
> 
> -- http://mail.python.org/mailman/listinfo/python-list
> 

The PITA is having to keep track of the indentation of each embedded chunk and 
summing it for each level of indentation. This requires a fair amount of 
bookkeeping that would not otherwise be necessary. 

The original prototype simply replaced each embedded chunk with the text from 
the chunk definition, all indenting information was lost. It worked for most 
languages, but not Python.

In testing the program I used two languages, Python and J. I figured if I could 
make both of them work I would not have any problem with anything else.

-EdK

Ed Keith
[email protected]

Blog: edkeith.blogspot.com



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


Re: Teaching Programming

2010-05-04 Thread D'Arcy J.M. Cain
On Wed, 5 May 2010 00:35:18 +1000
James Mills  wrote:
> In my experience of non-indentation sensitive languages
> such as C-class (curly braces) it's just as hard to keep track
> of opening and closing braces.

Harder.  That was the big "Aha!" for me with Python.  My first
programming language was Fortran in 1969 so when I saw indentation as
syntax I also recoiled in horror for about 0.5 seconds.  However, I
immediately realized that from now on I could be sure that if it looked
right then it was right.

for (x = 0; x++; x < 10);
printf("Current number is %d\n", x);

Or...

for (x = 0; x++; x < 10);
{
printf("Current number is %d\n", x);
}

Oops.  Looks right but isn't.  Try to make that mistake in Python.

Note untested - I wasn't about to fire up an editor, create a C
program, add the includes, compile and run the a.out just to test
this.  Another way that Python excels.  However, I am pretty sure that
the above would compile.

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


Re: Teaching Programming

2010-05-04 Thread alex23
Ed Keith  wrote:
> Tabs are always a problem when writing Python. I get
> around this problem by setting my text editor to expand
> all tabs with spaces when editing Python, but I have had
> problems when coworkers have not done this.

It's best not to trust others to do the right thing. I do trust Tools/
scripts/reindent.py more though :)

> Don't get me wrong, I love working in Python. It is one
> of my favorite languages, but it, like all other languages,
> is not perfect.

I was genuinely interested in the problems you hit as I have some hazy
projects in mind that I was planning on trying to handle via code
generation. Context managers seem the best place to first try dealing
with it. If you're interested I can keep you posted when I finally
have some working code.

Cheers for the clarification.
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Teaching Programming

2010-05-04 Thread Stefan Behnel

Ed Keith, 04.05.2010 17:43:

The PITA is having to keep track of the indentation of each embedded
chunk and summing it for each level of indentation. This requires a fair
amount of bookkeeping that would not otherwise be necessary.

The original prototype simply replaced each embedded chunk with the text
from the chunk definition, all indenting information was lost. It worked
for most languages, but not Python.

In testing the program I used two languages, Python and J.


Well, then both of the language generators have benefited from your effort 
because the generated complete code is properly indented and therefore much 
more readable during debugging. I'd say it was worth it.


Stefan

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


Re: Teaching Programming

2010-05-04 Thread superpollo

superpollo ha scritto:

Stefan Behnel ha scritto:

superpollo, 04.05.2010 14:46:

my template system wants
the input to generate the code to stay on a single line ( don't ask 
:-( )


I hope you don't mind if I still ask. What are you generating and for 
what templating system?


ok, since you asked for it, prepare yourself for a bit of a horror story 
;-)


i will answer in my next post


ok here it is.

i am not a programmer or it-guy in the first place; my job is as a high 
school teacher in a field only remotely connected with computers (math 
and physics).


since i have some kind of computer literacy (as opposed to most of my 
colleagues), some years ago i was kindly asked to try and solve a 
"simple" particular problem, that is to write a program that generates 
math exercises (q+a) from an example taken from the textbook. for 
instance, this:


%%TITLE:Sample worksheet
%%
%%SCHEMA:\lim_{x \to }
%%SCHEMA:\frac
%%SCHEMA:{x^3-x^2-x}
%%SCHEMA:{x^3-x^2+x-}\\
%%
%%ANS:FRAC
%%ANSNUM:
%%ANSDEN:
%%
%%AMIN:1
%%AINC:1
%%AMAX:2
%%BMIN:3
%%BINC:1
%%BMAX:4
%%CMIN:2
%%CINC:1
%%CMAX:3

should generate this latex source document:

\documentclass[a4paper,10pt,twocolumn,fleqn]{article}
\title{Sample worksheet}
\pagestyle{empty}
\usepackage[italian]{babel}
\usepackage{amsmath}
\usepackage{amssymb}
\usepackage{cancel}
\usepackage{mathrsfs}
\usepackage[dvips]{graphicx}
\usepackage{eurosym}
\usepackage{pstricks}
\usepackage{pst-eucl}
\usepackage{pst-poly}
\usepackage{pst-plot}
\frenchspacing
\begin{document}
\section*{\center{\framebox{Sample worksheet}}}
\noindent
\begin{enumerate}
\item
\begin{multline*}
\lim_{x \to 1}
\frac
{x^3+3x^2-4x}
{x^3-x^2+2x-2}\\
\end{multline*}
\item
\begin{multline*}
\lim_{x \to 2}
\frac
{x^3+x^2-6x}
{x^3-2x^2+2x-4}\\
\end{multline*}
\item
\begin{multline*}
\lim_{x \to 2}
\frac
{x^3+2x^2-8x}
{x^3-2x^2+2x-4}\\
\end{multline*}
\item
\begin{multline*}
\lim_{x \to 1}
\frac
{x^3+2x^2-3x}
{x^3-x^2+2x-2}\\
\end{multline*}
\item
\begin{multline*}
\lim_{x \to 1}
\frac
{x^3+2x^2-3x}
{x^3-x^2+3x-3}\\
\end{multline*}
\item
\begin{multline*}
\lim_{x \to 1}
\frac
{x^3+3x^2-4x}
{x^3-x^2+3x-3}\\
\end{multline*}
\item
\begin{multline*}
\lim_{x \to 2}
\frac
{x^3+x^2-6x}
{x^3-2x^2+3x-6}\\
\end{multline*}
\item
\begin{multline*}
\lim_{x \to 2}
\frac
{x^3+2x^2-8x}
{x^3-2x^2+3x-6}\\
\end{multline*}
\end{enumerate}
\subsection*{\center{Answers}}
\begin{enumerate}
\item
\begin{displaymath}
\frac{5}{3}
\end{displaymath}
\item
\begin{displaymath}
\frac{5}{3}
\end{displaymath}
\item
\begin{displaymath}
2
\end{displaymath}
\item
\begin{displaymath}
\frac{4}{3}
\end{displaymath}
\item
\begin{displaymath}
1
\end{displaymath}
\item
\begin{displaymath}
\frac{5}{4}
\end{displaymath}
\item
\begin{displaymath}
\frac{10}{7}
\end{displaymath}
\item
\begin{displaymath}
\frac{12}{7}
\end{displaymath}
\end{enumerate}
\end{document}

which in turn can be used to generate the following pdf file:

http://www.datafilehost.com/download-cc88a19e.html

fine huh? now, for the horror part.

when i began putting down some code i thought that maybe it would take a 
couple of evenings to put up a working prototype, and i was w-r-o-n-g:


1) as i said i am not a professional
2) i decided to use bash shell as a core language for the 
parser/generator ( *sigh* )

3) feautiritis soon crept in

if some of you knows a bit about math and especially algebra, you will 
understand that some of the biggest problems in generating standard math 
notation in to cope with the various layers of traditions and innuendos: 
for example i could generate the string "2x" which is fine; but what 
about "1x" or "-1x" or "0x"? and what about plus signs at the start of 
an expression (normally omitted)? when does a subexpression start? etc 
... there are plenty of icompatible rules and as much as exceptions ...


as you can see i had many problems to solve and i am not trained to cope 
with such issues from a programmatic standpoint.


another thing is that for some features i intended to include i found 
convenient to use python (since it's the language i feel i am more at 
ease with), so i had to cope with template lines like this:


%%SCHEMA:

or even worse:

%%ANSFRCEMBPYC:"~longmapsto~" , $C**2*(3*$A+3*$B+$C) , "~mathrm{(max~loc.)}">\\


to make a long story short: the whole program is now 4775 lines of bash 
code, written by an unqualified amateur under time pressure; sometimes i 
have to hand-modify it to get certain outputs as i expect them to be; 
this in turn breaks other subsystems.


i am ashamed to post the code, and it is a true design nightmare. also 
it is offtopic (being bash).


*but* it was and it is a wonderful experience to learn to program, to 
acknowledge weaknesses end strongnesses of various approaches, to better 
understand the structure of math notation, problem solving end teaching 
techniques.


it is still work in progress though, and i hope i will never see the day 
i have to refurbish it. i hope -- instead -- that the next time

Re: Teaching Programming

2010-05-04 Thread Terry Reedy

On 5/3/2010 7:46 PM, cjw wrote:


Nobody likes indentation at first,


Speak for yourself, please. For two decades before I met Python, I 
indented code nicely whenever it was allowed. That option was one of the 
great advancements of Fortran77 over FortranIV. Coming from C, I was 
immediately glad to be done with those darn braces.


tjr

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


Re: Teaching Programming

2010-05-04 Thread D'Arcy J.M. Cain
On Tue, 4 May 2010 17:00:11 +0200
Andre Engels  wrote:
> Although I have little or no experience with this, I still dare to say
> that I don't agree. The difference is that in C you do not _need_ to
> know where in the braces-defined hierarchy you are. You just embed or
> change a piece of code at the right location. In Python however you
> _do_ need to know how far your code is to be indented.

Well, I'm afraid your lack of experience shows.  Experienced C coders
will tell you that one of the most annoying things is counting braces
to make sure that you have the right number in the right places.  In
fact, they add indentation so that they can use the visual layout to
check the brace layout.  Braces are the added step.

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


Re: Teaching Programming

2010-05-04 Thread Chris Rebert
On Tue, May 4, 2010 at 8:49 AM, D'Arcy J.M. Cain  wrote:
> On Wed, 5 May 2010 00:35:18 +1000
> James Mills  wrote:
>> In my experience of non-indentation sensitive languages
>> such as C-class (curly braces) it's just as hard to keep track
>> of opening and closing braces.
>
> Harder.  That was the big "Aha!" for me with Python.  My first
> programming language was Fortran in 1969 so when I saw indentation as
> syntax I also recoiled in horror for about 0.5 seconds.

The amount of mental scaring that Fortran has caused regarding
indentation is astounding.
Maybe the PSF should run re-education camps for Fortran programmers... :P

> However, I
> immediately realized that from now on I could be sure that if it looked
> right then it was right.
>
>    for (x = 0; x++; x < 10);
>        printf("Current number is %d\n", x);
>
> Or...
>
>    for (x = 0; x++; x < 10);
>    {
>        printf("Current number is %d\n", x);
>    }
>
> Oops.  Looks right but isn't.  Try to make that mistake in Python.

Technically, that pitfall /could/ be eliminated if curly-braces
languages simply always required the curly-braces for bodies and
stopped special-casing the null body case, in which case both your
examples would be syntax errors. Removing the special-casing of
single-line bodies too would remove a further class of errors.
However, I've yet to encounter a language that takes such an approach.
Quite a pity.

Cheers,
Chris
--
I've thought a lot about this.
http://blog.rebertia.com/2010/01/24/of-braces-and-semicolons/
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Teaching Programming

2010-05-04 Thread Chris Rebert
On Tue, May 4, 2010 at 8:52 AM, Stefan Behnel  wrote:
> Ed Keith, 04.05.2010 17:43:
>> The PITA is having to keep track of the indentation of each embedded
>> chunk and summing it for each level of indentation. This requires a fair
>> amount of bookkeeping that would not otherwise be necessary.
>>
>> The original prototype simply replaced each embedded chunk with the text
>> from the chunk definition, all indenting information was lost. It worked
>> for most languages, but not Python.
>>
>> In testing the program I used two languages, Python and J.
>
> Well, then both of the language generators have benefited from your effort
> because the generated complete code is properly indented and therefore much
> more readable during debugging. I'd say it was worth it.

Though there are auto-re-indenting programs for curly-brace languages you know.

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


Re: Teaching Programming

2010-05-04 Thread Terry Reedy

On 5/4/2010 8:46 AM, superpollo wrote:


but i do not think i can use it myself, since my template system wants
the input to generate the code to stay on a single line ( don't ask :-( )


I think we can agree that Python (unlike C, for instance) is not good 
for writing non-humanly-readable one-unbounded-length-line code. That is 
contrary to its design goal. But I would think that you would have 
similar problems with other manditorily multiline languages, like 
Fortran and Basic.


Terry Jan Reedy

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


Re: Teaching Programming

2010-05-04 Thread Dave Angel

Ethan Furman wrote:
Andre 
Engels wrote:

On Tue, May 4, 2010 at 4:35 PM, James Mills
 wrote:

On Wed, May 5, 2010 at 12:21 AM, Ed Keith  wrote:

To deal with indentation I had to

  1) keep track of indentation of all chunks of code embedded in the
 document and indent inserted chunks to the sum of all the
 indentation of the enclosing chunks.

In my experience of non-indentation sensitive languages
such as C-class (curly braces) it's just as hard to keep track
of opening and closing braces.


Although I have little or no experience with this, I still dare to say
that I don't agree. The difference is that in C you do not _need_ to
know where in the braces-defined hierarchy you are. You just embed or
change a piece of code at the right location. In Python however you
_do_ need to know how far your code is to be indented.


And how do you know where the right location is?  You have to figure 
it out from the different (nested) braces at the 'right' location.  
For me, at least, it's much easier to get that information from the 
already indented Python code, as opposed to indenting (and 
double-checking the indents) on the braces language.


~Ethan~



Much more important to be able to read code reliably than write it quickly.

When I was heavily doing C++ I found myself wishing that incorrect 
indentation would be an error or at least a warning.  And I hadn't ever 
heard of Python.  When I did, I said "About time!"


Of course, I was surrounded by many programmers who ignored any warning 
that the compiler produced, while I cranked up warnings  to max, with 
pedantic.


On one project I generated C++ code, about 20k lines.  And it was all 
indented and commented.  Most of that code went through the code 
generator twice.  The "source" was a header file from outside my 
organization.  The generator used that to create a new header, more 
detailed.  Then the generator used that to create source & headers that 
were actually used by the project.


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


Re: Teaching Programming

2010-05-04 Thread Gary Herron

Terry Reedy wrote:

On 5/3/2010 7:46 PM, cjw wrote:


Nobody likes indentation at first,


Speak for yourself, please. For two decades before I met Python, I 
indented code nicely whenever it was allowed. That option was one of 
the great advancements of Fortran77 over FortranIV. Coming from C, I 
was immediately glad to be done with those darn braces.


tjr



Right.  Somewhere in the 80's I read a paper in sigplan which 
demonstrated that indentation-only was sufficient to communicate the 
structure of a program.I immediately *knew* that was the right way 
to go.  Sadly, I had to wait a decade or so before discovering Python in 
the mid 90's, but I never forgot that paper nor lost my eager 
anticipation waiting for language design to catch up with that idea.


Gary Herron

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


Re: Teaching Programming

2010-05-04 Thread Stefan Behnel

superpollo, 04.05.2010 17:55:

since i have some kind of computer literacy (as opposed to most of my
colleagues), some years ago i was kindly asked to try and solve a
"simple" particular problem, that is to write a program that generates
math exercises (q+a) from an example taken from the textbook. for
instance, this:

%%TITLE:Sample worksheet
%%
%%SCHEMA:\lim_{x \to }
%%SCHEMA:\frac
%%SCHEMA:{x^3-x^2-x}
%%SCHEMA:{x^3-x^2+x-}\\
%%
%%ANS:FRAC
%%ANSNUM:
%%ANSDEN:
%%
%%AMIN:1
%%AINC:1
%%AMAX:2
%%BMIN:3
%%BINC:1
%%BMAX:4
%%CMIN:2
%%CINC:1
%%CMAX:3

should generate this latex source document:

\documentclass[a4paper,10pt,twocolumn,fleqn]{article}
\title{Sample worksheet}
\pagestyle{empty}
\usepackage[italian]{babel}
\usepackage{amsmath}
\usepackage{amssymb}
\usepackage{cancel}
\usepackage{mathrsfs}
\usepackage[dvips]{graphicx}
\usepackage{eurosym}
\usepackage{pstricks}
\usepackage{pst-eucl}
\usepackage{pst-poly}
\usepackage{pst-plot}
\frenchspacing
\begin{document}
\section*{\center{\framebox{Sample worksheet}}}
\noindent
\begin{enumerate}
\item
\begin{multline*}
\lim_{x \to 1}
\frac
{x^3+3x^2-4x}
{x^3-x^2+2x-2}\\
\end{multline*}
\item
\begin{multline*}
\lim_{x \to 2}
\frac
{x^3+x^2-6x}
{x^3-2x^2+2x-4}\\
\end{multline*}
\item
\begin{multline*}
\lim_{x \to 2}
\frac
{x^3+2x^2-8x}
{x^3-2x^2+2x-4}\\
\end{multline*}
\item
\begin{multline*}
\lim_{x \to 1}
\frac
{x^3+2x^2-3x}
{x^3-x^2+2x-2}\\
\end{multline*}
\item
\begin{multline*}
\lim_{x \to 1}
\frac
{x^3+2x^2-3x}
{x^3-x^2+3x-3}\\
\end{multline*}
\item
\begin{multline*}
\lim_{x \to 1}
\frac
{x^3+3x^2-4x}
{x^3-x^2+3x-3}\\
\end{multline*}
\item
\begin{multline*}
\lim_{x \to 2}
\frac
{x^3+x^2-6x}
{x^3-2x^2+3x-6}\\
\end{multline*}
\item
\begin{multline*}
\lim_{x \to 2}
\frac
{x^3+2x^2-8x}
{x^3-2x^2+3x-6}\\
\end{multline*}
\end{enumerate}
\subsection*{\center{Answers}}
\begin{enumerate}
\item
\begin{displaymath}
\frac{5}{3}
\end{displaymath}
\item
\begin{displaymath}
\frac{5}{3}

> [...]

I'm not exactly sure I understand the mapping between the two formats, but 
it seems to me that you'll need a proper math expression parser (with a 
strong emphasis on *parser*) for this. Math expressions are not exactly 
trivial (recursion, prefix/infix/postfix notations, functions), which is 
why 'real' programmers don't write parsers for them but download tested, 
working code for that from somewhere.




another thing is that for some features i intended to include i found
convenient to use python (since it's the language i feel i am more at
ease with), so i had to cope with template lines like this:

%%SCHEMA:


You should assure that the math module is always imported, and otherwise 
restrict the expressiveness to expressions (terms that result in a value), 
not arbitrary statements (executable commands that do things without 
returning anything).




to make a long story short: the whole program is now 4775 lines of bash
code,


Argh!



*but* it was and it is a wonderful experience to learn to program


It does sound like you almost completely skipped over two very important 
programming lessons, though: reading code is harder than writing it (i.e. 
maintenance cost matters), and the best code is the code that you don't 
need to write (and debug and maintain).


Stefan

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


Re: HTTP server + SQLite?

2010-05-04 Thread Terry Reedy

On 5/4/2010 2:07 AM, Bryan wrote:


The SQLite developers state the situation brilliantly at
http://www.sqlite.org/whentouse.html:


For future reference, that link does not work with Thunderbird. This one 
does.


http://www.sqlite.org/whentouse.html

When posting links, best to put them on a line by themselves, with NO 
punctuation, even if 'proper' English seems to require it. A space 
before the colon probably would have worked too, if its complete absence 
bothers one too much ;=).


Terry Jan Reedy


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


Re: long int computations

2010-05-04 Thread Mensanator
On May 3, 10:17 am, [email protected] (Victor Eijkhout) wrote:
> I have two long ints, both too long to convert to float, but their ratio
> is something reasonable. How can I compute that? The obvious "(1.*x)/y"
> does not work.

You could try using the gmpy module. It supports arbitrary precision
floats, so converting long to float is no problem.

YY = 3**10684
float(YY)
Traceback (most recent call last)
  File "pyshell#78>", line 1, in 
float(YY)
OverflowError: Python int too large to convert to C double

import gmpy
gmpy.mpf(YY)
mpf('3.6600365709...0197681e5097',16936)



>
> Victor.
>
> --
> Victor Eijkhout -- eijkhout at tacc utexas edu

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


Re: Recursive functions not returning lists as expected

2010-05-04 Thread Terry Reedy

On 5/4/2010 1:45 AM, rickhg12hs wrote:

On May 4, 1:34 am, Cameron Simpson  wrote:

On 03May2010 22:02, rickhg12hs  wrote:
| Would a kind soul explain something basic to a python noob?
|
| Why doesn't this function always return a list?
|
| def recur_trace(x,y):
|   print x,y
|   if not x:
| return y
|   recur_trace(x[1:], y + [x[0]])

You need:
 return recur_trace(x[1:], y + [x[0]])

Otherwise the function returns None.


Ah, an explicit "return" is required.  Thanks!
[To bad there's no tail recursion optimization.]  8-(


That issue is much more complicated than you probably imagine. Here is 
part of it, using your toy example of computing y +x. [Code snippets 
below are untested and might have typos.]


Write your tail recursive function with the recursive branch first:

def t_r(x,y):
  if x:
return t_r(x[1:], y+[x[0]])
  else:
return y

and the conversion ('optimization') to while form is trivial:

def t_w(x,y):
  while x:
x,y = x[1:], y+[x[0]]
  else:
return y

Of course, most would omit the 'else:', which I left to emphasize the 
paralellism. More importantly, 'while' loops are relatively rare in 
Python because scanning an iterable (a typical use of tail recursion) 
can be and usually is done (generically) with for loops instead:


def t_f(x,y):
  for o in x:
y = y + [o]
  return y

Of course, this, like your original code, wastefully creates numerous 
temporary lists (two per iteration) when only one new list is needed for 
the entire process. This is easily fixed in the loop version:


def t_f2(x,y):
  r = y[:]
  for o in x:
r.append(o)
  return r

Making that important fix in the recursive version is harder since the 
copying should be done exactly once, and not with every recursive call. 
I leave it to you to try either of the two main approaches.


Terry Jan Reedy


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


scrolledtext download location

2010-05-04 Thread Robin
Does anyone know where I can download the ScrolledText tkintewr
widget, looked all over for it and had no luck,
Thanks,
-Robin
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: long int computations

2010-05-04 Thread Victor Eijkhout
Mensanator  wrote:

> You could try using the gmpy module. It supports arbitrary precision
> floats, so converting long to float is no problem.

I fear I may actually have to go symbolic. I'm now having to use the
12th root of 2, and I would like the twelfth power of that to be exactly
2.

Victor.
-- 
Victor Eijkhout -- eijkhout at tacc utexas edu
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: How to get xml.etree.ElementTree not bomb on invalid characters in XML file ?

2010-05-04 Thread Terry Reedy

On 5/4/2010 11:37 AM, Stefan Behnel wrote:

Barak, Ron, 04.05.2010 16:11:



The XML file seems to be valid XML (all XML viewers I tried were able
to read it).


From Internet Explorer:

The XML page cannot be displayed
Cannot view XML input using XSL style sheet. Please correct the error 
and then click the Refresh button, or try again later.





An invalid character was found in text content. Error processing 
resource 'file:///C:/Documents and Settings...


 "BROLB21



This is what xmllint gives me:

---
$ xmllint /home/sbehnel/tmp.xml
tmp.xml:6: parser error : Char 0x0 out of allowed range
"MainStorage_snap
^
tmp.xml:6: parser error : Premature end of data in tag m_sanApiName1 line 6
"MainStorage_snap
^
tmp.xml:6: parser error : Premature end of data in tag DbHbaGroup line 5
"MainStorage_snap
^
tmp.xml:6: parser error : Premature end of data in tag database line 4
"MainStorage_snap
^
---

The file contains 0-bytes - clearly not XML.


IE agrees.


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


Re: new extension generator for C++

2010-05-04 Thread Rouslan Korneychuk

On 05/04/2010 03:06 AM, Samuel Williams wrote:

Dear Rouslan,

It looks interesting. I say go for it. You will learn something and might make 
some improvements on existing ideas.

I recommend putting the code on www.github.com

Kind regards,
Samuel


Thanks for the suggestion. I think I'll do just that. The "social" 
aspect sounds interesting, since up until now, this project has been a 
solitary exercise.



On 05/04/2010 01:51 AM, Stefan Behnel wrote:
> Last I heard, that was basically what PyBindGen does (and probably some
> other existing binding generators). You should take a look at them
> before investing too much time into a duplicated effort.
>
> Also, if you're interested in performance, you should take a look at
> Cython, which is an optimising compiler for (basically) Python code that
> can talk to C, C++ and Fortran code. It's been used a lot for writing
> performance critical library wrappers.
>
> Stefan
>

I took a brief look at PyBindGen. I noticed a couple of things in the 
generated code of a test module. The PyObject definitions held a pointer 
to the contained datatype, which is one of the things I was trying to 
steer away from. It would generate a std::map for each new type, 
apparently to allow the contained type to be matched back to the 
PyObject when needed. I intend to use the same method that Boost.Python 
uses, sneak the pointer into the destruct functor of a shared_ptr.


And when parsing arguments from an overloaded set, PyBindGen appears to 
generate complete separate functions for each overload and then try one 
after the other. The reason mine doesn't support overloaded named 
arguments is because it instead uses a bunch of nested if-statements to 
match the overload with as few checks as possible. Mine also orders the 
checks so that a base-class is never checked before a derived-class and 
that numeric types and string types are matched to their best fit (given 
func(double x) and func(int x), a python float value will always call 
func(double)). I'm not certain that PyBindGen doesn't generate code that 
takes this into consideration I haven't studied its code.


I'll definitely take a look at how Cython handles arguments some time.


It's also interesting to note that while PyBindGen will parse C++ types 
itself, to interpret arguments and return types, mine uses gccxml to do 
all the work. When given a specific overload, it will generate a dummy 
function with the same set of arguments in the file given to gccxml, 
along with typedefs for all built-in types. That way, you can specify 
types either explicitly, or with typedefs regardless of how the 
interface you want to expose does it, and my code doesn't have to do any 
extra work.



The only question I have now is what about licensing? Is that something 
I need to worry about? Should I go with LGPL, MIT, or something else? 
And should the license be mentioned in the code or be in a single 
separate file at the top-level directory?

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


Re: scrolledtext download location

2010-05-04 Thread Terry Reedy

On 5/4/2010 1:32 PM, Robin wrote:

Does anyone know where I can download the ScrolledText tkintewr
widget, looked all over for it and had no luck,


Since this is a module included with tkinter which is included with 
Python, (at least on Windows) I am puzzled. Perhaps you need to supply 
more info as to your problem.


(3.1) Lib ref 24.4:
"tkinter.scrolledtext module provides a class of the same name which 
implements a basic text widget which has a vertical scroll ..."



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


Re: Teaching Programming

2010-05-04 Thread superpollo

James Mills ha scritto:

On Tue, May 4, 2010 at 9:56 PM, superpollo  wrote:

of course! *but* if i must generate on-the-fly python code that defines a
function i am back again to the problem:


One-liner:

$ python
Python 2.6.5 (r265:79063, Apr 27 2010, 18:26:49)
[GCC 4.4.1 (CRUX)] on linux2
Type "help", "copyright", "credits" or "license" for more information.

a, b = 2, 3
print a + b if a > b else a**b - b**2

-1

a, b = 3, 2
print a + b if a > b else a**b - b**2

5

--James


what if i want an elif clause?

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


Re: py3 tkinter acceps bytes. why?

2010-05-04 Thread Terry Reedy

On 5/4/2010 10:17 AM, Matthias Kievernagel wrote:

From: Matthias Kievernagel
Subject: py3 tkinter acceps bytes. why?
Newsgroups: comp.lang.python
Summary:
Keywords:

In a recent thread named "py3 tkinter Text accepts what bytes?"
(google groups link:
http://groups.google.com/group/comp.lang.python/browse_thread/thread/b75ed69f4e81b202/e2aff9ddd62d210c?lnk=raot)
I asked what kinds of bytes are accepted as tkinter parameters.
I still wonder why they are accepted at all.
Does anyone know a reason for this
or has a link to some relevant discussion?


I do not remember any particular public discussion of tkinter on the dev 
lists. I suspect the reason is a) avoid breaking old code and b) tkinter 
(tk inter-face) passes params on to tk which expects byte strings. I 
would not be surprised if tkinter has to encode py3 (unicode) strings 
before passing them on.


The Py3 changes were greatly complicated by the need to interface with 
the older bytes world.


Terry Jan Reedy

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


Re: Teaching Programming

2010-05-04 Thread superpollo

superpollo ha scritto:

James Mills ha scritto:

On Tue, May 4, 2010 at 9:56 PM, superpollo  wrote:
of course! *but* if i must generate on-the-fly python code that 
defines a

function i am back again to the problem:


One-liner:

$ python
Python 2.6.5 (r265:79063, Apr 27 2010, 18:26:49)
[GCC 4.4.1 (CRUX)] on linux2
Type "help", "copyright", "credits" or "license" for more information.

a, b = 2, 3
print a + b if a > b else a**b - b**2

-1

a, b = 3, 2
print a + b if a > b else a**b - b**2

5

--James


what if i want an elif clause?



my first try obviously falied:

>>> print a + b if a > b elif a=b "WOW" else a**b - b**2
  File "", line 1
print a + b if a > b elif a=b "WOW" else a**b - b**2
^
SyntaxError: invalid syntax

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


Re: Teaching Programming

2010-05-04 Thread Terry Reedy

On 5/4/2010 1:44 PM, Dennis Lee Bieber wrote:

On Tue, 04 May 2010 12:06:10 -0400, Terry Reedy
declaimed the following in gmane.comp.python.general:


Speak for yourself, please. For two decades before I met Python, I
indented code nicely whenever it was allowed. That option was one of the
great advancements of Fortran77 over FortranIV. Coming from C, I was
immediately glad to be done with those darn braces.


What kept you from indenting FORTRAN-IV?


There was a several year gap with learning of 'structured programming' 
in between. Perhaps ignorance of both the possibility (no one did it, 
that I know of) and desirability. Glass screens made consistent 
indentation a lot easier that with cards and teletypes. The new 
structured block constructs made indentation more useful, too.




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


  1   2   >