A way to re-organize a list

2007-07-19 Thread beginner
Hi Everyone,

I have a simple list reconstruction problem, but I don't really know
how to do it.

I have a list that looks like this:

l=[ ("A", "a", 1), ("A", "a", 2), ("A", "a", 3), ("A", "b", 1), ("A",
"b", 2), ("B", "a", 1), ("B", "b", 1)]

What I want to do is to reorganize it in groups, first by the middle
element of the tuple, and then by the first element. I'd like the
output look like this:

out=[
   [#group by first element "A"
  [("A", "a", 1), ("A", "a", 2), ("A", "a", 3)], #group by
second element "a"
  [ ("A", "b", 1), ("A", "b", 2)], #group by second element
"b"
   ],
   [   #group by first element "B"
  [("B", "a", 1)],
  [("B", "b", 1)]
   ]
]


All the solutions I came up with are difficult to read and even harder
to go back and change, and I just feel are too complicated for such a
simple problem. I am wondering if anyone here has some insight.

If there is a 'functional' way to do this, it would be even greater.


Thanks,
Geoffrey

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


Flatten a list/tuple and Call a function with tuples

2007-07-25 Thread beginner
Hi,

I am wondering how do I 'flatten' a list or a tuple? For example, I'd
like to transform[1, 2, (3,4)] or [1,2,[3,4]] to  [1,2,3,4].

Another question is how do I pass a tuple or list of all the
aurgements of a function to the function. For example, I have all the
arguments of a function in a tuple a=(1,2,3). Then I want to pass each
item in the tuple to a function f so that I make a function call
f(1,2,3). In perl it is a given, but in python, I haven't figured out
a way to do it. (Maybe apply? but it is deprecated?)

Thanks,
cg

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


Re: Flatten a list/tuple and Call a function with tuples

2007-07-25 Thread beginner
On Jul 25, 10:19 am, Stargaming <[EMAIL PROTECTED]> wrote:
> On Wed, 25 Jul 2007 14:50:18 +, beginner wrote:
> > Hi,
>
> > I am wondering how do I 'flatten' a list or a tuple? For example, I'd
> > like to transform[1, 2, (3,4)] or [1,2,[3,4]] to  [1,2,3,4].
>
> A recursive function, always yielding the first element of the list,
> could do the job. See the ASPN Python Cookbook for a few 
> implementations.http://aspn.activestate.com/ASPN/search?
> query=flatten§ion=PYTHONCKBK&type=Subsection
>
> > Another question is how do I pass a tuple or list of all the aurgements
> > of a function to the function. For example, I have all the arguments of
> > a function in a tuple a=(1,2,3). Then I want to pass each item in the
> > tuple to a function f so that I make a function call f(1,2,3). In perl
> > it is a given, but in python, I haven't figured out a way to do it.
> > (Maybe apply? but it is deprecated?)
> >>> def foo(a, b, c): print a, b, c
> ...
> >>> t = (1, 2, 3)
> >>> foo(*t)
>
> 1 2 3
>
> Have a look at the official tutorial, 4.7.4http://www.python.org/doc/
> current/tut/node6.html#SECTION00674
>
> > Thanks,
> > cg
>
> HTH,
> Stargaming

Hi Stargaming,

I know the * operator. However, a 'partial unpack' does not seem to
work.

def g():
  return (1,2)

def f(a,b,c):
  return a+b+c

f(*g(),10) will return an error.

Do you know how to get that to work?

Thanks,
cg


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


Re: Flatten a list/tuple and Call a function with tuples

2007-07-25 Thread beginner
On Jul 25, 11:00 am, [EMAIL PROTECTED] wrote:
> On Jul 25, 10:46 am, beginner <[EMAIL PROTECTED]> wrote:
>
>
>
>
>
> > On Jul 25, 10:19 am, Stargaming <[EMAIL PROTECTED]> wrote:
>
> > > On Wed, 25 Jul 2007 14:50:18 +, beginner wrote:
> > > > Hi,
>
> > > > I am wondering how do I 'flatten' a list or a tuple? For example, I'd
> > > > like to transform[1, 2, (3,4)] or [1,2,[3,4]] to  [1,2,3,4].
>
> > > A recursive function, always yielding the first element of the list,
> > > could do the job. See the ASPN Python Cookbook for a few 
> > > implementations.http://aspn.activestate.com/ASPN/search?
> > > query=flatten§ion=PYTHONCKBK&type=Subsection
>
> > > > Another question is how do I pass a tuple or list of all the aurgements
> > > > of a function to the function. For example, I have all the arguments of
> > > > a function in a tuple a=(1,2,3). Then I want to pass each item in the
> > > > tuple to a function f so that I make a function call f(1,2,3). In perl
> > > > it is a given, but in python, I haven't figured out a way to do it.
> > > > (Maybe apply? but it is deprecated?)
> > > >>> def foo(a, b, c): print a, b, c
> > > ...
> > > >>> t = (1, 2, 3)
> > > >>> foo(*t)
>
> > > 1 2 3
>
> > > Have a look at the official tutorial, 4.7.4http://www.python.org/doc/
> > > current/tut/node6.html#SECTION00674
>
> > > > Thanks,
> > > > cg
>
> > > HTH,
> > > Stargaming
>
> > Hi Stargaming,
>
> > I know the * operator. However, a 'partial unpack' does not seem to
> > work.
>
> > def g():
> >   return (1,2)
>
> > def f(a,b,c):
> >   return a+b+c
>
> > f(*g(),10) will return an error.
>
> > Do you know how to get that to work?
>
> > Thanks,
> > cg
>
> As I mentioned, you can access the elements individually using square
> brackets. The following works:
>
> f(g()[0], g()[1], 10)
>
> But it's not clear. Unfortunately, I'm not seeing much else for tuple
> unpacking except the obvious:
>
> a,b=g()
> f(a,b,10)
>
> Mike- Hide quoted text -
>
> - Show quoted text -

Unfortunately f(g()[0], g()[1], 10) is calling g() twice. Sometimes
this is not a good idea.

> a,b=g()
> f(a,b,10)

would work until you want it to be an expression.

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


Re: Flatten a list/tuple and Call a function with tuples

2007-07-25 Thread beginner

> Not the most beautiful solution, but it works.
>
> Diez- Hide quoted text -
>
> - Show quoted text -

Yeah it works! Thanks.

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


Re: Flatten a list/tuple and Call a function with tuples

2007-07-25 Thread beginner
> Also, this has not been suggested:
>
> py> def g():
> ...   return (1,2)
> ...
> py> def f(a,b,c):
> ...   return a+b+c
> ...
> py> f(c=10, *g())
> 13
>
> James- Hide quoted text -
>
> - Show quoted text -

Great idea.

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


Re: Flatten a list/tuple and Call a function with tuples

2007-07-25 Thread beginner
On Jul 25, 11:33 am, Paul Rubin <http://[EMAIL PROTECTED]> wrote:
> beginner <[EMAIL PROTECTED]> writes:
> > I know the * operator. However, a 'partial unpack' does not seem to work.
>
> A few other posters have mentioned ways around this, but you might ask
> yourself what coding situation makes you want to do this stuff in the
> first place.  I won't say there's never a reason for it, but a lot of
> times, a list containing a mixture of scalars and lists/tuples is a
> sign that your underlying data representation is contorted.  Things
> are logically single values or they are logically lists of values, and
> that mixed representation is often a sign that the item logically
> should be a list, and you're hairing up the program with special
> treatment of the case where the list has exactly one element.
>
> I.e. instead of [[1,2,], 3, [5,6,]] maybe you really want
> [[1,2,], [3,], [5,6]] without the special treatment and flattening.

Very good question. It is well possible that the problem is my
programming style. I am new to python and am still developing a style
that works for me. A lot of things that work in perl does not seem to
work. Unpacking and flattening are just two examples.

I need nested lists to represent nested records in a script. Since the
structure of the underlying data is nested, I think it is probably
reasonable to represent them as nested lists. For example, if I have
the below structure:

Big Record
   Small Record Type A
   Many Small Record Type B
   Small Record Type C

It is pretty natural to use lists, although after a while it is
difficult to figure out the meaning of the fields in the lists. If
only there were a way to 'attach' names to members of the list.

For the unpacking question, I encountered it when working with list
comprehensions. For example:

[ f(*x,1,2) for x in list] is difficult to do if I don't want to
expand *x to x[0]..x[n]. There are usually 7-10 items in the list and
it is very tedious and error prone.

The second problem is from a nested list comprehension. I just needed
something to flatten the list at the moment.

I am still forming my way to do things in python via trial and error.
It is well possible that this is not the natural way to do things.


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


Re: Flatten a list/tuple and Call a function with tuples

2007-07-25 Thread beginner

> Well, there are several ways to solve this. You could either invoke f(*g
> () + (10,)). Might be a bit nasty and unreadable, though. Or you could
> just change your function f to accept them in reversed order (f(10, *g)
> should work) or convert g() to return a dictionary like {'b': 1, 'c': 2}
> and use f(10, **g). But if your function f's hugest use case is being
> called with g(), changing f to accept something and g's result (tuple) --
> unpacking it inside f -- might be better.- Hide quoted text -
>
> - Show quoted text -

These all work. Thanks.

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


Another C API Question

2007-07-26 Thread beginner
Hi,

I run into another C API question. What is the simplest way to convert
an PyObject into a double?

For example, I have

 PyObject *obj;

I know obj is a number, but I do not know the exact type. How can I
convert it to double without writing a giant switch() that exhausts
every single type of number?

Thanks,
beginner

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


C API -- Two questions

2007-07-26 Thread beginner
Hi Everyone,

I am writing a C extension and encountered two problems.

1) How can I include a description of the arguments? I can include a
description string. No problem. But whenever I say help(my_func) it
shows the arguments are "... ". However, if a function is defined in
python, I will definitely be able to see the parameters when I type
help(my_func).

2) How can I make the arguments less picky without writing a lot of
type conversion code? My function really needs a tuple as its
argument. For example, f( (1,2,3) ) would work. However, in order to
make it easy to use, I am thinking that it should be able to take a
list too. In other words, I want f( [1,2,3] ) to work also. I can
certainly check for the types in the code and deal with each
situation. But remember this is tedious to do in C. Is there any
better way to handle this?

Thanks,
cg

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


Re: Another C API Question

2007-07-27 Thread beginner
Hi Farshid,

On Jul 26, 8:18 pm, Farshid Lashkari <[EMAIL PROTECTED]> wrote:
> beginner wrote:
> > I know obj is a number, but I do not know the exact type. How can I
> > convert it to double without writing a giant switch() that exhausts
> > every single type of number?
>
> Try using the PyFloat_AsDouble(...) function, it should be able to
> convert an object to a double, as long as the object implements the
> __float__ method.

This works with PyFloat only. It does not work with integers.

Thanks,
b

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


Re: Another C API Question

2007-07-27 Thread beginner
Hi Robert,

On Jul 26, 8:16 pm, Robert Kern <[EMAIL PROTECTED]> wrote:
> beginner wrote:
> > Hi,
>
> > I run into another C API question. What is the simplest way to convert
> > an PyObject into a double?
>
> > For example, I have
>
> >  PyObject *obj;
>
> > I know obj is a number, but I do not know the exact type. How can I
> > convert it to double without writing a giant switch() that exhausts
> > every single type of number?
>
> Convert it to a Python float using PyNumber_Float(), then use 
> PyFloat_AsDouble()
> to get the C double value from it.
>

Thanks a lot for your help.

Best regards,
beginner


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


Re: Another C API Question

2007-07-27 Thread beginner
On Jul 27, 11:37 am, Farshid Lashkari <[EMAIL PROTECTED]> wrote:
> beginner wrote:
> > This works with PyFloat only. It does not work with integers.
>
> Did you try it out? I have used it on ints, bools, and objects that
> implement the __float__ method. It does not work on strings though.

I did and it did not seem to work. I ended up doing the following.
Verbose, isn't it?
If I do d=PyFloat_AsDouble(oDiscount); in the third "if", I get an
error. Maybe I missed something obvious.


if(oDiscount==Py_None)
{
}
else if(PyFloat_Check(oDiscount))
{
d=PyFloat_AsDouble(oDiscount);
}
else if(PyNumber_Check(oDiscount))
{
PyObject *pf=PyNumber_Float(oDiscount);
if(pfDiscount)
{
d=PyFloat_AsDouble(pfDiscount);
 Py_DECREF(pfDiscount);
}
}
else ...

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


Re: C API -- Two questions

2007-07-27 Thread beginner
On Jul 27, 1:23 am, Marc 'BlackJack' Rintsch <[EMAIL PROTECTED]> wrote:
> On Fri, 27 Jul 2007 00:34:22 +, beginner wrote:
> > 2) How can I make the arguments less picky without writing a lot of
> > type conversion code? My function really needs a tuple as its
> > argument. For example, f( (1,2,3) ) would work. However, in order to
> > make it easy to use, I am thinking that it should be able to take a
> > list too. In other words, I want f( [1,2,3] ) to work also. I can
> > certainly check for the types in the code and deal with each
> > situation. But remember this is tedious to do in C. Is there any
> > better way to handle this?
>
> The same way as you would do in Python: convert the argument into a tuple
> if you *really* need a tuple, or just use it as sequence or via iterator.
> And pay attention to errors of course.
>
> Ciao,
> Marc 'BlackJack' Rintsch

That makes sense. Thanks.

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


Tkinter program with a usable interpreter console

2007-07-27 Thread beginner
Hi everyone,

I have a question about Tkinter programs. What I want is a GUI window
along with an interpreter window, so that I can do things by clicking
on the GUI or typing commands in the interpreter console. For example,
I wish I can type in the console:

>>> modify_my_key_data()
>>> update_gui()

and my gui is modified for me.

The problem is that the Tkinter program ends with a .mainloop() call
and it is not going to give back control to the command prompt. I feel
it is almost like I need to implement the python shell myself. Is
there any better way of doing this?

Thanks,
beginner

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


Tkinter -- Show Data in an Excel like Read-Only Grid

2007-07-27 Thread beginner
Hi All,

I am really new to Tk and Tkinter. I googled the web but it was not
mentioned how to build a data grid with Tkinter.

Basically, I want to show an excel like data grid with fixed column
and row headers and sortable columns. But the grids can be read-only.

Can anyone give some hint on implementing this?


Thanks,
beginner

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


Re: Another C API Question

2007-07-27 Thread beginner
On Jul 27, 4:50 pm, Farshid Lashkari <[EMAIL PROTECTED]> wrote:
> beginner wrote:
> > I did and it did not seem to work. I ended up doing the following.
> > Verbose, isn't it?
> > If I do d=PyFloat_AsDouble(oDiscount); in the third "if", I get an
> > error. Maybe I missed something obvious.
>
> That's strange. I just tried the following code:
>
> fprintf(stdout,"True = %lf\n",PyFloat_AsDouble(Py_True));
> fprintf(stdout,"False = %lf\n",PyFloat_AsDouble(Py_False));
> fprintf(stdout,"5 = %lf\n",PyFloat_AsDouble(PyInt_FromLong(5)));
>
> And it printed the following:
>
> True = 1.00
> False = 0.00
> 5 = 5.00
>
> What version of Python are you using?

Interesting. Let me try it again. I definitely want to remove the
intermediate step that creates a temporary Py object. Well, Python is
probably doing it internally anyway, but at least I don't have to see
it. :-)

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


Re: Another C API Question

2007-07-27 Thread beginner
On Jul 27, 4:50 pm, Farshid Lashkari <[EMAIL PROTECTED]> wrote:
> beginner wrote:
> > I did and it did not seem to work. I ended up doing the following.
> > Verbose, isn't it?
> > If I do d=PyFloat_AsDouble(oDiscount); in the third "if", I get an
> > error. Maybe I missed something obvious.
>
> That's strange. I just tried the following code:
>
> fprintf(stdout,"True = %lf\n",PyFloat_AsDouble(Py_True));
> fprintf(stdout,"False = %lf\n",PyFloat_AsDouble(Py_False));
> fprintf(stdout,"5 = %lf\n",PyFloat_AsDouble(PyInt_FromLong(5)));
>
> And it printed the following:
>
> True = 1.00
> False = 0.00
> 5 = 5.00
>
> What version of Python are you using?

I am using 2.5.1 and Windows XP. Thanks for your help.

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


Re: Tkinter -- Show Data in an Excel like Read-Only Grid

2007-07-28 Thread beginner
On Jul 27, 11:08 pm, Zentrader <[EMAIL PROTECTED]> wrote:
> On Jul 27, 2:56 pm, beginner <[EMAIL PROTECTED]> wrote:
>
> > Hi All,
>
> > I am really new to Tk and Tkinter. I googled the web but it was not
> > mentioned how to build a data grid with Tkinter.
>
> > Basically, I want to show an excel like data grid with fixed column
> > and row headers and sortable columns. But the grids can be read-only.
>
> > Can anyone give some hint on implementing this?
>
> > Thanks,
> > beginner
>
> See if tkTable will helphttp://tkinter.unpythonic.net/wiki/TkTable

Thanks for your help.

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


Re: Tkinter program with a usable interpreter console

2007-07-28 Thread beginner
On Jul 27, 6:17 pm, Ivan Johansen <[EMAIL PROTECTED]> wrote:
> beginner wrote:
> > The problem is that the Tkinter program ends with a .mainloop() call
> > and it is not going to give back control to the command prompt. I feel
> > it is almost like I need to implement the python shell myself. Is
> > there any better way of doing this?
>
> Take a look at this:http://lfw.org/python/Console.py
>
> I haven't really used it myself yet, but it looks really great.
>
> Ivan Johansen

It looks interesting. Let me take a close look.

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


What is the "functional" way of doing this?

2007-07-30 Thread beginner
Hi,

If I have a number n and want to generate a list based on like the
following:

def f(n):
 l=[]
 while n>0:
 l.append(n%26)
 n /=26
return l

I am wondering what is the 'functional' way to do the same.

Thanks,
beginner

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


Re: What is the "functional" way of doing this?

2007-07-31 Thread beginner
On Jul 30, 5:48 pm, beginner <[EMAIL PROTECTED]> wrote:
> Hi,
>
> If I have a number n and want to generate a list based on like the
> following:
>
> def f(n):
>  l=[]
>  while n>0:
>  l.append(n%26)
>  n /=26
> return l
>
> I am wondering what is the 'functional' way to do the same.
>
> Thanks,
> beginner

I see. It is interesting (and not surprisingly) that recursion or
yield are required. Thanks for everyone's help.

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


standalone process to interact with the web

2007-07-31 Thread beginner
Hi Everyone,

I am looking for a way to allow a standalone python process to easily
interactive with a few web pages. It has to be able to easily receive
requests from the web and post data to the web.

I am thinking about implementing a standalone soap server, but I am
not sure which library is good.

Any suggestions?

Thanks a lot,
Geoffrey

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


Re: standalone process to interact with the web

2007-07-31 Thread beginner
Hi Steve,

On Jul 31, 11:42 am, Steve Holden <[EMAIL PROTECTED]> wrote:
> beginner wrote:
> > Hi Everyone,
>
> > I am looking for a way to allow a standalone python process to easily
> > interactive with a few web pages. It has to be able to easily receive
> > requests from the web and post data to the web.
>
> > I am thinking about implementing a standalone soap server, but I am
> > not sure which library is good.
>
> > Any suggestions?
>
> > Thanks a lot,
> > Geoffrey
>
> Look nor further than mechanize -
>
>http://wwwsearch.sourceforge.net/mechanize/
>
> With mechanize and its partner ClientForm you can rule the web world ;-)
>
> regards
>   Steve
> --
> Steve Holden+1 571 484 6266   +1 800 494 3119
> Holden Web LLC/Ltd  http://www.holdenweb.com
> Skype: holdenweb  http://del.icio.us/steve.holden
> --- Asciimercial --
> Get on the web: Blog, lens and tag the Internet
> Many services currently offer free registration
> --- Thank You for Reading -

This seems to be an HTTP client library. It is very interesting, but
is not what I need. I am looking for something that can provide
service to web pages. For example, when a browser requests a web page,
the web page is going to send a few requests to my server. My server
then is going to respond, and the web page takes the response and
format it in human readable form.




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


Re: standalone process to interact with the web

2007-07-31 Thread beginner
On Jul 31, 1:11 pm, Steve Holden <[EMAIL PROTECTED]> wrote:
> beginner wrote:
> > Hi Steve,
>
> > On Jul 31, 11:42 am, Steve Holden <[EMAIL PROTECTED]> wrote:
> >> beginner wrote:
> >>> Hi Everyone,
> >>> I am looking for a way to allow a standalone python process to easily
> >>> interactive with a few web pages. It has to be able to easily receive
> >>> requests from the web and post data to the web.
> >>> I am thinking about implementing a standalone soap server, but I am
> >>> not sure which library is good.
> >>> Any suggestions?
> >>> Thanks a lot,
> >>> Geoffrey
> >> Look nor further than mechanize -
>
> >>http://wwwsearch.sourceforge.net/mechanize/
>
> >> With mechanize and its partner ClientForm you can rule the web world ;-)
>
> >> regards
> >>   Steve
> >> --
> >> Steve Holden+1 571 484 6266   +1 800 494 3119
> >> Holden Web LLC/Ltd  http://www.holdenweb.com
> >> Skype: holdenweb  http://del.icio.us/steve.holden
> >> --- Asciimercial --
> >> Get on the web: Blog, lens and tag the Internet
> >> Many services currently offer free registration
> >> --- Thank You for Reading -
>
> > This seems to be an HTTP client library. It is very interesting, but
> > is not what I need. I am looking for something that can provide
> > service to web pages. For example, when a browser requests a web page,
> > the web page is going to send a few requests to my server. My server
> > then is going to respond, and the web page takes the response and
> > format it in human readable form.
>
> Well, surely in that case you can use any protocol you like. It's up to
> the web server and your "web server server" to agree how to communicate.
> Take a look at the SocketServer library, for example.
>
> regartds
>   Steve
> --
> Steve Holden+1 571 484 6266   +1 800 494 3119
> Holden Web LLC/Ltd  http://www.holdenweb.com
> Skype: holdenweb  http://del.icio.us/steve.holden
> --- Asciimercial --
> Get on the web: Blog, lens and tag the Internet
> Many services currently offer free registration
> --- Thank You for Reading -- Hide quoted text -
>
> - Show quoted text -

Yes exactly. I just don't want to reinvent the wheel as I imagine
there are already tons of libraries and frameworks that support RPC or
the like functions.

Thanks,
Geoffrey

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


Re: A way to re-organize a list

2007-07-31 Thread beginner
On Jul 19, 10:05 am, beginner <[EMAIL PROTECTED]> wrote:
> Hi Everyone,
>
> I have a simple list reconstruction problem, but I don't really know
> how to do it.
>
> I have a list that looks like this:
>
> l=[ ("A", "a", 1), ("A", "a", 2), ("A", "a", 3), ("A", "b", 1), ("A",
> "b", 2), ("B", "a", 1), ("B", "b", 1)]
>
> What I want to do is to reorganize it in groups, first by the middle
> element of the tuple, and then by the first element. I'd like the
> output look like this:
>
> out=[
>[#group by first element "A"
>   [("A", "a", 1), ("A", "a", 2), ("A", "a", 3)], #group by
> second element "a"
>   [ ("A", "b", 1), ("A", "b", 2)], #group by second element
> "b"
>],
>[   #group by first element "B"
>   [("B", "a", 1)],
>   [("B", "b", 1)]
>]
> ]
>
> All the solutions I came up with are difficult to read and even harder
> to go back and change, and I just feel are too complicated for such a
> simple problem. I am wondering if anyone here has some insight.
>
> If there is a 'functional' way to do this, it would be even greater.
>
> Thanks,
> Geoffrey

I guess I still don't quite get functional programming. Here is my
imperitive way to do it in O(n).

"""Put a sorted hirerchical structure into nested groups"""

def group_items(source_list, f_list, target=[]):
"""Group_items: Group a list based on the grouping functions.

source_list is a list or iterator that produces a stream of source
records.

f_list is a list of functions. Each function takes the form
f(a,b), where
a and b are two records. If the two records should be in the same
group, it returns
True, otherwise it returns False.

If target is not provided, a new list will be created. Otherwise,
the records are
appended to the provided list.
"""

last_item=None

for new_item in source_list:
level=len(f_list)
t=target
for fn in f_list:
if  t==[] or last_item==None or not fn(last_item,
new_item):
ng=new_item
for i in range(level): ng=[ng]
t.append(ng)
break
else:
t=t[-1]
level -= 1
else:
t.append(new_item)
last_item=new_item

return target


if __name__=="__main__":
import pprint
def f(a,b):
return a[0]==b[0]
def g(a,b):
return a[1]==b[1]

mydata=[[1,2,3,4],[1,2,4,5],[1,2,"A","B"],[2,2,"A","C"]]
t=group_items(mydata, [f,g])
pprint.pprint(t)



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


Python end of file marker similar to perl's __END__

2007-07-31 Thread beginner
Hi All,

This is just a very simple question about a python trick.

In perl, I can write __END__ in a file and the perl interpreter will
ignore everything below that line. This is very handy when testing my
program. Does python have something similar?

Thanks,
Geoffrey

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


Re: Python end of file marker similar to perl's __END__

2007-08-01 Thread beginner
On Jul 31, 10:53 pm, beginner <[EMAIL PROTECTED]> wrote:
> Hi All,
>
> This is just a very simple question about a python trick.
>
> In perl, I can write __END__ in a file and the perl interpreter will
> ignore everything below that line. This is very handy when testing my
> program. Does python have something similar?
>
> Thanks,
> Geoffrey

Thanks everyone for responding. It doesn't look like python has it. I
would definitely miss it. As Steve said, the nice thing about __END__
is that things below __END__ do not have to have legit syntax. That
let me focus on the lines of code I am debugging and do not have to
worry about some bad syntax down the line. This feature is especially
handy if I am, saying, replacing modoules or changing data structures.


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


Assertion in list comprehension

2007-08-01 Thread beginner
Hi,

Does anyone know how to put an assertion in list comprehension? I have
the following list comprehension, but I want to use an assertion to
check the contents of rec_stdl. I ended up using another loop which
essentially duplicates the functions of list comprehension. It just
look like a waste of coding and computer time to me.

I just wish I could put the assertions into list comprehensions.

x=[(rec_stdl[0].st/1.0,
rec_stdl[0].cl,
rec_stdl[0].bb,
rec_stdl[0].bo,
rec_stdl[1].bb,
rec_stdl[1].bo,
rec_stdl[0].ex
   )
   for rec_stdl in rec_by_ex if len(rec_stdl)==2
]

#duplicated loop
if __debug__:
for rec_stdl in rec_by_ex:
l=len(rec_stdl)
assert(l<=2 and l>0)
if l==2:
assert(rec_stdl[0].c=="C" and rec_stdl[1].c=="P")
assert(rec_stdl[0].ex==rec_stdl[1].ex)
assert(rec_stdl[0].st==rec_stdl[1].st)
assert(rec_stdl[0].cp==rec_stdl[1].cp)

Thanks,
Geoffrey

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


Awkward format string

2007-08-01 Thread beginner
Hi,

In order to print out the contents of a list, sometimes I have to use
very awkward constructions. For example, I have to convert the
datetime.datetime type to string first, construct a new list, and then
send it to print. The following is an example.

x=(e[0].strftime("%Y-%m-%d"), e[1].strftime("%Y-%m-%d"))+e[2:]
print  >>f, "%s\t%s\t%d\t%f\t%f\t%f\t%d" % x

e is a tuple. x is my new tuple.

Does anyone know better ways of handling this?

Thanks,
Geoffrey

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


Re: Awkward format string

2007-08-01 Thread beginner
On Aug 1, 11:31 am, "Chris Mellon" <[EMAIL PROTECTED]> wrote:
> On 8/1/07, beginner <[EMAIL PROTECTED]> wrote:
>
> > Hi,
>
> > In order to print out the contents of a list, sometimes I have to use
> > very awkward constructions. For example, I have to convert the
> > datetime.datetime type to string first, construct a new list, and then
> > send it to print. The following is an example.
>
> > x=(e[0].strftime("%Y-%m-%d"), e[1].strftime("%Y-%m-%d"))+e[2:]
> > print  >>f, "%s\t%s\t%d\t%f\t%f\t%f\t%d" % x
>
> > e is a tuple. x is my new tuple.
>
> > Does anyone know better ways of handling this?
>
> You seem to be doing quite complicated things with your magical e
> tuple. Do you have some specific aversion to classes?

e is not complicated. It is a record that have 7 fields. In my program
a function outputs a list of tuples, each is of type e, and now I just
need to send them to a text file.

I have no problem using classes and I do use them everywhere. But
using classes does not solve my problem here. I will probably find
myself doing:

print  >>f, "%s\t%s\t%d\t%f\t%f\t%f\t%d" % (x.field1..strftime("%Y-%m-
%d"), x.field2..strftime("%Y-%m-%d"), x.field3, x.field4, x.field5,
x.field.6, x.field7)

This is also tedious and error-prone.



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


Re: Assertion in list comprehension

2007-08-01 Thread beginner
On Aug 1, 11:09 am, "[EMAIL PROTECTED]" <[EMAIL PROTECTED]>
wrote:
> On Aug 1, 9:37 am, beginner <[EMAIL PROTECTED]> wrote:
>
>
>
>
>
> > Hi,
>
> > Does anyone know how to put an assertion in list comprehension? I have
> > the following list comprehension, but I want to use an assertion to
> > check the contents of rec_stdl. I ended up using another loop which
> > essentially duplicates the functions of list comprehension. It just
> > look like a waste of coding and computer time to me.
>
> > I just wish I could put the assertions into list comprehensions.
>
> > x=[(rec_stdl[0].st/1.0,
> > rec_stdl[0].cl,
> > rec_stdl[0].bb,
> > rec_stdl[0].bo,
> > rec_stdl[1].bb,
> > rec_stdl[1].bo,
> > rec_stdl[0].ex
> >)
> >for rec_stdl in rec_by_ex if len(rec_stdl)==2
> > ]
>
> > #duplicated loop
> > if __debug__:
> > for rec_stdl in rec_by_ex:
> > l=len(rec_stdl)
> > assert(l<=2 and l>0)
> > if l==2:
> > assert(rec_stdl[0].c=="C" and rec_stdl[1].c=="P")
> > assert(rec_stdl[0].ex==rec_stdl[1].ex)
> > assert(rec_stdl[0].st==rec_stdl[1].st)
> > assert(rec_stdl[0].cp==rec_stdl[1].cp)
>
> > Thanks,
> > Geoffrey
>
> Can't you just call a function from within your list comprehension and
> do whatever you want for each item? Something like this (not tested):
>
> def checker(item):
> assert(len(item) <= 2 and len(item) > 0)
> if len(item) == 2:
> assert(item[0].c == "C" and item[1].c == "P"
>
> return len(item) == 2
>
> x = [whatever for item in all_items if checker(item = item)]- Hide quoted 
> text -
>
> - Show quoted text -

Good idea! Thank you!

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


Re: Assertion in list comprehension

2007-08-01 Thread beginner
On Aug 1, 11:28 am, "Chris Mellon" <[EMAIL PROTECTED]> wrote:
> On 8/1/07, beginner <[EMAIL PROTECTED]> wrote:
>
>
>
>
>
> > Hi,
>
> > Does anyone know how to put an assertion in list comprehension? I have
> > the following list comprehension, but I want to use an assertion to
> > check the contents of rec_stdl. I ended up using another loop which
> > essentially duplicates the functions of list comprehension. It just
> > look like a waste of coding and computer time to me.
>
> > I just wish I could put the assertions into list comprehensions.
>
> > x=[(rec_stdl[0].st/1.0,
> > rec_stdl[0].cl,
> > rec_stdl[0].bb,
> > rec_stdl[0].bo,
> > rec_stdl[1].bb,
> > rec_stdl[1].bo,
> > rec_stdl[0].ex
> >)
> >for rec_stdl in rec_by_ex if len(rec_stdl)==2
> > ]
>
> > #duplicated loop
> > if __debug__:
> > for rec_stdl in rec_by_ex:
> > l=len(rec_stdl)
> > assert(l<=2 and l>0)
> > if l==2:
> > assert(rec_stdl[0].c=="C" and rec_stdl[1].c=="P")
> > assert(rec_stdl[0].ex==rec_stdl[1].ex)
> > assert(rec_stdl[0].st==rec_stdl[1].st)
> > assert(rec_stdl[0].cp==rec_stdl[1].cp)
>
> First: All your asserts are wrong. Assert is a statement, not a
> function. These specific ones will behave as expected, but it's easy
> to accidentally write ones that always pass this way.


Do you mean I should not use the parentheses?


> Secondly: This is a waste of code, because if __debug__ is not defined
> asserts will be skipped by the compiler. You could use the same loop
> block for both branches.

I know. My original question was how. Dan suggested to write a checker
function.


> Thirdly: This sort of testing is precisely what unit tests and/or
> doctests are for.

Agreed.

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


Re: Assertion in list comprehension

2007-08-01 Thread beginner
On Aug 1, 12:35 pm, "Chris Mellon" <[EMAIL PROTECTED]> wrote:
> On 8/1/07, beginner <[EMAIL PROTECTED]> wrote:
>
>
>
>
>
> > On Aug 1, 11:28 am, "Chris Mellon" <[EMAIL PROTECTED]> wrote:
> > > On 8/1/07, beginner <[EMAIL PROTECTED]> wrote:
>
> > > > Hi,
>
> > > > Does anyone know how to put an assertion in list comprehension? I have
> > > > the following list comprehension, but I want to use an assertion to
> > > > check the contents of rec_stdl. I ended up using another loop which
> > > > essentially duplicates the functions of list comprehension. It just
> > > > look like a waste of coding and computer time to me.
>
> > > > I just wish I could put the assertions into list comprehensions.
>
> > > > x=[(rec_stdl[0].st/1.0,
> > > > rec_stdl[0].cl,
> > > > rec_stdl[0].bb,
> > > > rec_stdl[0].bo,
> > > > rec_stdl[1].bb,
> > > > rec_stdl[1].bo,
> > > > rec_stdl[0].ex
> > > >)
> > > >for rec_stdl in rec_by_ex if len(rec_stdl)==2
> > > > ]
>
> > > > #duplicated loop
> > > > if __debug__:
> > > > for rec_stdl in rec_by_ex:
> > > > l=len(rec_stdl)
> > > > assert(l<=2 and l>0)
> > > > if l==2:
> > > > assert(rec_stdl[0].c=="C" and rec_stdl[1].c=="P")
> > > > assert(rec_stdl[0].ex==rec_stdl[1].ex)
> > > > assert(rec_stdl[0].st==rec_stdl[1].st)
> > > > assert(rec_stdl[0].cp==rec_stdl[1].cp)
>
> > > First: All your asserts are wrong. Assert is a statement, not a
> > > function. These specific ones will behave as expected, but it's easy
> > > to accidentally write ones that always pass this way.
>
> > Do you mean I should not use the parentheses?
>
> Yes.
>
>
>
> > > Secondly: This is a waste of code, because if __debug__ is not defined
> > > asserts will be skipped by the compiler. You could use the same loop
> > > block for both branches.
>
> > I know. My original question was how. Dan suggested to write a checker
> > function.
>
> Use the second block in all cases. In any situation where "if
> __debug__" is False, the asserts are a noop and in fact won't even be
> present in the bytecode.
>
>
>
>
>
> > > Thirdly: This sort of testing is precisely what unit tests and/or
> > > doctests are for.
>
> > Agreed.
>
> > --
> >http://mail.python.org/mailman/listinfo/python-list- Hide quoted text -
>
> - Show quoted text -- Hide quoted text -
>
> - Show quoted text -

I see. In fact I want to whole block surrounded by __debug__ to be
optimized away in non-debug runs. If the logic of my program is
correct, the asserts are guaranteed to be true, no matter what the
input is. It is not input checking.

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


Re: Assertion in list comprehension

2007-08-01 Thread beginner
On Aug 1, 12:38 pm, "Chris Mellon" <[EMAIL PROTECTED]> wrote:
> On 01 Aug 2007 16:55:53 GMT, Stargaming <[EMAIL PROTECTED]> wrote:
>
>
>
>
>
> > On Wed, 01 Aug 2007 11:28:48 -0500, Chris Mellon wrote:
>
> > > On 8/1/07, beginner <[EMAIL PROTECTED]> wrote:
> > >> Hi,
>
> > >> Does anyone know how to put an assertion in list comprehension? I have
> > >> the following list comprehension, but I want to use an assertion to
> > >> check the contents of rec_stdl. I ended up using another loop which
> > >> essentially duplicates the functions of list comprehension. It just
> > >> look like a waste of coding and computer time to me.
>
> > >> I just wish I could put the assertions into list comprehensions.
>
> > >> x=[(rec_stdl[0].st/1.0,
> > >> rec_stdl[0].cl,
> > >> rec_stdl[0].bb,
> > >> rec_stdl[0].bo,
> > >> rec_stdl[1].bb,
> > >> rec_stdl[1].bo,
> > >> rec_stdl[0].ex
> > >>)
> > >>for rec_stdl in rec_by_ex if len(rec_stdl)==2
> > >> ]
>
> > >> #duplicated loop
> > >> if __debug__:
> > >> for rec_stdl in rec_by_ex:
> > >> l=len(rec_stdl)
> > >> assert(l<=2 and l>0)
> > >> if l==2:
> > >> assert(rec_stdl[0].c=="C" and rec_stdl[1].c=="P")
> > >> assert(rec_stdl[0].ex==rec_stdl[1].ex)
> > >> assert(rec_stdl[0].st==rec_stdl[1].st)
> > >> assert(rec_stdl[0].cp==rec_stdl[1].cp)
>
> > > First: All your asserts are wrong. Assert is a statement, not a
> > > function. These specific ones will behave as expected, but it's easy to
> > > accidentally write ones that always pass this way.
>
> > Could you come up with an example? I can only think of accidentally
> > injecting a comma, what would create a (true, in a boolean context) tuple.
>
> > And, well, if you're only using () for readabilty, this might sometimes
> > look messy when calling assert with the extended syntax::
>
> >   assert(False), "error text"
>
> It's very easy to write this as assert(False, "error text") if you're
> in the habit of thinking that assert is a function.
>
> > Where one could expect the construction of a tuple.
>
> > > Secondly: This is a waste of code, because if __debug__ is not defined
> > > asserts will be skipped by the compiler. You could use the same loop
> > > block for both branches.
>
> > Well, the `assert` isn't there for no reason, but if you're serious about
> > it, `raise` could be better.
>
> > > Thirdly: This sort of testing is precisely what unit tests and/or
> > > doctests are for.
>
> > Huh? What beginner is doing there seems more like input validation than
> > testing. Unit or doctests are meant for testing (and in case of doctests,
> > showing) whether a function works as expected.
>
> Not in a big __debug__ block it isn't.


No I was trying to test the logic of my code not validating the
contents of the data. Thanks.

>
>
> > --
> >http://mail.python.org/mailman/listinfo/python-list- Hide quoted text -
>
> - Show quoted text -- Hide quoted text -
>
> - Show quoted text -


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


Re: A way to re-organize a list

2007-08-01 Thread beginner
On Aug 1, 1:31 am, Marc 'BlackJack' Rintsch <[EMAIL PROTECTED]> wrote:
> On Wed, 01 Aug 2007 01:33:49 +, beginner wrote:
> > On Jul 19, 10:05 am, beginner <[EMAIL PROTECTED]> wrote:
> >> Hi Everyone,
>
> >> I have a simple list reconstruction problem, but I don't really know
> >> how to do it.
>
> >> I have a list that looks like this:
>
> >> l=[ ("A", "a", 1), ("A", "a", 2), ("A", "a", 3), ("A", "b", 1), ("A",
> >> "b", 2), ("B", "a", 1), ("B", "b", 1)]
>
> >> What I want to do is to reorganize it in groups, first by the middle
> >> element of the tuple, and then by the first element. I'd like the
> >> output look like this:
>
> >> out=[
> >>[#group by first element "A"
> >>   [("A", "a", 1), ("A", "a", 2), ("A", "a", 3)], #group by
> >> second element "a"
> >>   [ ("A", "b", 1), ("A", "b", 2)], #group by second element
> >> "b"
> >>],
> >>[   #group by first element "B"
> >>   [("B", "a", 1)],
> >>   [("B", "b", 1)]
> >>]
> >> ]
>
> >> All the solutions I came up with are difficult to read and even harder
> >> to go back and change, and I just feel are too complicated for such a
> >> simple problem. I am wondering if anyone here has some insight.
>
> >> If there is a 'functional' way to do this, it would be even greater.
>
> >> Thanks,
> >> Geoffrey
>
> > I guess I still don't quite get functional programming. Here is my
> > imperitive way to do it in O(n).
>
> I didn't try to follow it but are you sure about O(n)?  With the inner
> loop going from 0 to `level` it looks suspiciously quadratic to me.  You
> really should try to grasp the `itertools.groupby()` solution.
>
> And the result of that script looks strange:
>
> 1, 2, 3, 4], [1, 2, 4, 5], [1, 2, 'A', 'B']]], [[[2, 2, 'A', 'C'
>
> Aren't the two top level elements wrapped in one list too much?
> Furthermore the test data doesn't contain an example where the first item
> is the same but the second item is different.
>
> > def group_items(source_list, f_list, target=[]):
>
> Default arguments are evaluated *once* when the ``def`` is executed.  So
> all calls to this function share the same list object bound to `target`.
> Call this function twice without an explicit `target` and you'll see the
> problem.
>
> Ciao,
> Marc 'BlackJack' Rintsch- Hide quoted text -
>
> - Show quoted text -


Wow. Thanks for the life-saving response. It uncovers a serious bug
about the default argument! Thank you Marc!

It is actually O(n) because it scans the list of records once and
exactly once. The inner loop is trying to make multi-level grouping.
In f_list, you define each level of grouping. For example,
f_list=[f,g] in the above code means 1) you want to put records into
sub-lists so that within each sub-list, the first element of the
records are the same. then 2) within each group, you want to further
group them by the second element of the records. That is why the
results look like the following:

[
   [ #first level grouping
  [[1, 2, 3, 4], [1, 2, 4, 5], [1, 2, 'A', 'B']] #second level
grouping
   ],
   [ #first level grouping
  [[2, 2, 'A', 'C']] #second level grouping
   ]
]

In the example, the inner loop is executed twice. So it loops a total
of 2*n times and it is O(n). If you want to make m level grouping, the
execution time would be O(m*n).

I looked at itertools. It is really cool. But if I just want to make
multiple nested groups, reusing the above function is going to take
less code than using itertools.groupby every time. Also, in
itertools.groupby, I think you have to scan the list of records
multiple times to make nested groups. Although it is still O(m*n),
each operation of groupby is going to be slower.

The one thing I still haven't figured out, is to make nested
generators of multiple nested grouping, so that I can use a multi-loop
to traverse them. A loop like below:

for i in big_group:
  for j in i:
for k in j:
  .

i, j, k are generators.

I think short of changing my function to be recursive and the outer
function 'yield' to the inner function, which calls yield to make an
inner generator, it will be difficult to do.

Thanks again for pointing out the default value problem.

Geoffrey

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


Re: standalone process to interact with the web

2007-08-02 Thread beginner
On Aug 1, 3:50 am, "Diez B. Roggisch" <[EMAIL PROTECTED]> wrote:
> beginner wrote:
> > Hi Everyone,
>
> > I am looking for a way to allow a standalone python process to easily
> > interactive with a few web pages. It has to be able to easily receive
> > requests from the web and post data to the web.
>
> > I am thinking about implementing a standalone soap server, but I am
> > not sure which library is good.
>
> > Any suggestions?
>
> Forget SOAP. Use XMLRPC. SOAP is bloated, not to interoperable and the last
> time I checked (has been a while though) the python-implementations were
> troublesome.
>
> http://wanderingbarque.com/nonintersecting/2006/11/15/the-s-stands-fo...
>
> Diez

Thanks for the sgguestion. XML RPC is definitely a good candidate.

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


Re: standalone process to interact with the web

2007-08-02 Thread beginner
On Aug 1, 5:04 am, Bruno Desthuilliers  wrote:
> beginner a écrit :
> (snip)
>
> > Yes exactly. I just don't want to reinvent the wheel as I imagine
> > there are already tons of libraries and frameworks that support RPC or
> > the like functions.
>
> Why go thru the pain of RPC, SOAP or such bloated horrors ? Why not just
> use plain old HTTP with a RESTful API ? Then you just need to make your
> app wsgi compliant.

I am not familiar with RESTful API. I will look into it.

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

Re: Awkward format string

2007-08-02 Thread beginner
On Aug 2, 3:32 am, Bruno Desthuilliers  wrote:
> beginner a écrit :
>
> > Hi,
>
> > In order to print out the contents of a list, sometimes I have to use
> > very awkward constructions. For example, I have to convert the
> > datetime.datetime type to string first, construct a new list,
>
> s/list/tuple/
>
> > and then
> > send it to print. The following is an example.
>
> >x=(e[0].strftime("%Y-%m-%d"), e[1].strftime("%Y-%m-%d"))+e[2:]
> >print  >>f, "%s\t%s\t%d\t%f\t%f\t%f\t%d" % x
>
> > e is a tuple. x is my new tuple.
>
> > Does anyone know better ways of handling this?
>
>  >>> from datetime import datetime
>  >>> dt = datetime(2007,8,2)
>  >>> dt
> datetime.datetime(2007, 8, 2, 0, 0)
>  >>> str(dt)
> '2007-08-02 00:00:00'
>  >>> "%s" % dt
> '2007-08-02 00:00:00'
>  >>> dt.date()
> datetime.date(2007, 8, 2)
>  >>> str(dt.date())
> '2007-08-02'
>
> Do you really need datetime objects ? If not, using date objects instead
> would JustWork(tm) - at least until someone ask you to use another date
> format !-)
>
> Else, and since you seem to have a taste for functional programming:
>
> from datetime import datetime
> from functools import partial
>
> def iformat(e):
>  fake = lambda obj, dummy: obj
>  for item in e:
>  yield getattr(item, 'strftime', partial(fake, item))('%Y-%m-%d')
>
> e = (datetime(2007,8,1),datetime(2007,8,2) ,42, 0.1, 0.2, 0.3, 1138)
> print tuple(iformat(e))
> print "%s\t%s\t%d\t%f\t%f\t%f\t%d" % tuple(iformat(e))

Thanks.

The 'functional' taste is still under development. It hasn't reached
production quality yet. :-)

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

Re: Efficient Rank Ordering of Nested Lists

2007-08-02 Thread beginner
On Aug 2, 8:20 pm, "[EMAIL PROTECTED]"
<[EMAIL PROTECTED]> wrote:
> A naive approach to rank ordering (handling ties as well) of nested
> lists may be accomplished via:
>
>def rankLists(nestedList):
>   def rankList(singleList):
>   sortedList = list(singleList)
>   sortedList.sort()
>   return map(sortedList.index, singleList)
>   return map(rankList, nestedList)
>
>>>> unranked = [ [ 1, 2, 3, 4, 5 ], [ 3, 1, 5, 2, 4 ], [ -1.1, 2.2,
> 0, -1.1, 13 ] ]
>>>> print rankLists(unranked)
>
>[[0, 1, 2, 3, 4], [2, 0, 4, 1, 3], [0, 3, 2, 0, 4]]
>
> This works nicely when the dimensions of the nested list are small.
> It is slow when they are big.  Can someone suggest a clever way to
> speed it up?

Indexing the sorted list with a dictionary will speed it up a little.

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


Re: Help me!!

2007-08-16 Thread beginner
> Some one help me so that
> If ab = [a,b,c,d]
> and cd = [a,c]
> my global list file should be [A,b,C,d]


If there is a lot of entries in the list, I would consider using an
indexed data structure such as dict.

>>> ab=['a','b','c','d']
>>> cd=['a','c']
>>> common=[x.upper() for x in ab if x in cd]
>>> ab_minus_cd=[x for x in ab if x not in cd]
>>> cd_minus_ab=[x for x in ab if x not in cd]
>>> requested_list = common+ab_minus_cd+cd_minus_ab

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


Re: Help me!!

2007-08-16 Thread beginner
On Aug 16, 2:26 pm, beginner <[EMAIL PROTECTED]> wrote:
> > Some one help me so that
> > If ab = [a,b,c,d]
> > and cd = [a,c]
> > my global list file should be [A,b,C,d]

If there is a lot of entries in the list, I would consider using an
indexed data structure such as dict.


ab=['a','b','c','d']
cd=['a','c']
common=[x.upper() for x in ab if x in cd]
ab_minus_cd=[x for x in ab if x not in cd]
cd_minus_ab=[x for x in ab if x not in cd]
requested_list = common+ab_minus_cd+cd_minus_ab- Hide quoted text -

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


How to say $a=$b->{"A"} ||={} in Python?

2007-08-16 Thread beginner
Hi All.

I'd like to do the following in more succint code:

if k in b:
a=b[k]
else:
a={}
b[k]=a

a['A']=1


In perl it is just one line: $a=$b->{"A"} ||={}.

Thanks,
Geoffrey

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


Re: How to say $a=$b->{"A"} ||={} in Python?

2007-08-16 Thread beginner
On Aug 16, 5:43 pm, [EMAIL PROTECTED] (Lawrence Oluyede) wrote:
> beginner <[EMAIL PROTECTED]> wrote:
> > I'd like to do the following in more succint code:
>
> > if k in b:
> > a=b[k]
> > else:
> > a={}
> > b[k]=a
>
> b.setdefault(k, a)
>
> --
> Lawrence, oluyede.org - neropercaso.it
> "It is difficult to get a man to understand
> something when his salary depends on not
> understanding it" - Upton Sinclair

I am afraid it is not the same. b.setdefault(k, {}) will always create
an empty dict, even if k is in b, as demonstrated in the below code.

b={}
def f(i):
print "I am evaluated %d" % i
return i

b.setdefault('A', f(1))
b.setdefault('A', f(2))
b


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


Re: How to say $a=$b->{"A"} ||={} in Python?

2007-08-16 Thread beginner
On Aug 16, 6:21 pm, James Stroud <[EMAIL PROTECTED]> wrote:
> beginner wrote:
> > Hi All.
>
> > I'd like to do the following in more succint code:
>
> > if k in b:
> > a=b[k]
> > else:
> > a={}
> > b[k]=a
>
> > a['A']=1
>
> > In perl it is just one line: $a=$b->{"A"} ||={}.
>
> I'm afraid you've asked a non sequiter:
>
> euler 40% cat test.pl
>
> $a=$b->{"A"} ||={} ;
> print "$a\n" ;
>
> $b->{"B"} = 0 ;
> $a=$b->{"B"} ||={} ;
> print "$a\n" ;
>
> $b->{"X"} = 15 ;
> $a=$b->{"X"} ||={} ;
> print "$a\n" ;
>
> euler 41% perl test.pl
> HASH(0x92662a0)
> HASH(0x926609c)
> 15
>
> James
>
> --
> James Stroud
> UCLA-DOE Institute for Genomics and Proteomics
> Box 951570
> Los Angeles, CA 90095
>
> http://www.jamesstroud.com/


It is not supposed to be used this way.
$b is supposed to be a hash-table of hash-table. If a key exists in
$b, it points to another hash table. The $a=$b->{"A"} ||={} pattern is
useful when you want to add records to the double hash table.

For example, if you have a series of records in the format of (K1, K2,
V), and you want to add them to the double hash-table, you can do
$a=$b->{K1} || ={}
$a->{K2}=V


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


Re: How to say $a=$b->{"A"} ||={} in Python?

2007-08-16 Thread beginner
On Aug 16, 9:32 pm, Carsten Haese <[EMAIL PROTECTED]> wrote:
> On Fri, 2007-08-17 at 00:53 +, beginner wrote:
> > $b is supposed to be a hash-table of hash-table. If a key exists in
> > $b, it points to another hash table. The $a=$b->{"A"} ||={} pattern is
> > useful when you want to add records to the double hash table.
>
> > For example, if you have a series of records in the format of (K1, K2,
> > V), and you want to add them to the double hash-table, you can do
> > $a=$b->{K1} || ={}
> > $a->{K2}=V
>
> What is the best solution in Perl need not be the best solution in
> Python. In Python you should just use a tuple as your dict key, i.e.
> a[k1,k2] = v, unless you have some other constraints you're not telling
> us.
>
> HTH,
>
> --
> Carsten Haesehttp://informixdb.sourceforge.net

I use tuples this way all the time. It is indeed very neat.  But it is
not a replacement for double hash-table.  If I want to retrieve
information just by K1, it is not efficient to index on (K1, K2).

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


Re: How to say $a=$b->{"A"} ||={} in Python?

2007-08-16 Thread beginner
On Aug 16, 5:35 pm, beginner <[EMAIL PROTECTED]> wrote:
> Hi All.
>
> I'd like to do the following in more succint code:
>
> if k in b:
> a=b[k]
> else:
> a={}
> b[k]=a
>
> a['A']=1
>
> In perl it is just one line: $a=$b->{"A"} ||={}.
>
> Thanks,
> Geoffrey

It looks like defaultdict is the solution for this kind of thing.
Thanks all for your help.

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


Re: How to say $a=$b->{"A"} ||={} in Python?

2007-08-17 Thread beginner
On Aug 17, 2:35 am, Sébastien Buchoux <[EMAIL PROTECTED]> wrote:
> beginner a écrit :
>
>
>
> > Hi All.
>
> > I'd like to do the following in more succint code:
>
> > if k in b:
> > a=b[k]
> > else:
> > a={}
> > b[k]=a
>
> > a['A']=1
>
> > In perl it is just one line: $a=$b->{"A"} ||={}.
>
> > Thanks,
> > Geoffrey
>
> One solution I often use in such cases:
>
> try:
> a = b[k]
> except KeyError: #or except IndexError: if b is a list/tuple and not a dict
> a = {}
> b[k] = a
>
> a['A'] = 1
>
> Indeed, exceptions are handled faster than "if/else" loops. As it was
> mentionned earlier, One neat solution in Perl may not be the perfect one
> in Python.
>
> Cheers,
>
> Sébastien- Hide quoted text -
>
> - Show quoted text -

Wow. This solution is interesting. I'll try this. Thanks.

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

Re: How to say $a=$b->{"A"} ||={} in Python?

2007-08-17 Thread beginner
On Aug 16, 11:02 pm, "Carsten Haese" <[EMAIL PROTECTED]> wrote:
> On Fri, 17 Aug 2007 03:15:10 -, beginner wrote
>
> > On Aug 16, 9:32 pm, Carsten Haese <[EMAIL PROTECTED]> wrote:
> > > What is the best solution in Perl need not be the best solution in
> > > Python. In Python you should just use a tuple as your dict key, i.e.
> > > a[k1,k2] = v, unless you have some other constraints you're not telling
> > > us.
>
> > > HTH,
>
> > > --
> > > Carsten Haesehttp://informixdb.sourceforge.net
>
> > I use tuples this way all the time. It is indeed very neat.  But it
> > is not a replacement for double hash-table.  If I want to retrieve
> > information just by K1, it is not efficient to index on (K1, K2).
>
> If you have to look up all values associates with k1 and any k2, you're right,
> that's not efficient. That would fall under "other constraints you're not
> telling us." I'm not a mind reader.
>
> -Carsten

Yeah, I should have mentioned that I actually want to group the data
by K1 and then by K2.

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


How to call module functions inside class instance functions?

2007-08-18 Thread beginner
Hi Everyone,

I have encountered a small problems.  How to call module functions
inside class instance functions? For example,  calling func1 in func2
resulted in a compiling error.

"my module here"

def func1():
 print "hello"

class MyClass:
   def func2():
 #how can I call func1 here.
 func1() #results in an error


Thanks,
Geoffrey

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


How to make a module function visible only inside the module?

2007-08-18 Thread beginner
Hi Everyone,

Is there any equivalent version of C's static function in Python. I
know I can make a class function private by starting a function name
with two underscores, but it does not work with module functions.

For exmaple, __func1 is still visible outside the module.

mymodule.py
"""my module""

def __func1():
print "Hello"



main.py
import mymodule

mymodule.__func1()

Thanks,
Geoffrey

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


Re: Parser Generator?

2007-08-18 Thread beginner
On Aug 18, 5:22 pm, "Jack" <[EMAIL PROTECTED]> wrote:
> Hi all, I need to do syntax parsing of simple naturual languages,
> for example, "weather of London" or "what is the time", simple
> things like these, with Unicode support in the syntax.
>
> In Java, there are JavaCC, Antlr, etc. I wonder what people use
> in Python? Antlr also has Python support but I'm not sure how good
> it is. Comments/hints are welcome.

Antlr seems to be able to generate python code, too.

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


"constructor" or initialization function for module

2007-08-18 Thread beginner
Hi Everyone,

An extended module (.pyd) written in C have an init function that is
called when the module is imported. Does anyone know if there is a way
to provide an init function for a module written in python?

Thanks,
Geoffrey

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


Re: How to call module functions inside class instance functions?

2007-08-18 Thread beginner
On Aug 18, 8:18 pm, Steve Holden <[EMAIL PROTECTED]> wrote:
> beginner wrote:
> > Hi Everyone,
>
> > I have encountered a small problems.  How to call module functions
> > inside class instance functions? For example,  calling func1 in func2
> > resulted in a compiling error.
>
> > "my module here"
>
> > def func1():
> >  print "hello"
>
> > class MyClass:
> >def func2():
> >  #how can I call func1 here.
> >  func1() #results in an error
>
> If you had bothered to include the error message it would have been
> obvious that the problem with your code isn't in body of the method at
> all - you have failed to include an argument to the method to pick up
> the instance on which the method is called. I am guessing that when you
> create an instance and call its func2 method you see the message
>
> Traceback (most recent call last):
>File "test07.py", line 12, in 
>  myInstance.func2()
> TypeError: func2() takes no arguments (1 given)
>
> which would have been a very useful clue. Please include the traceback
> in future! Here's a version of your program that works.
>
> [EMAIL PROTECTED] ~/Projects/Python
> $ cat test07.py
> "my module here"
>
> def func1():
>   print "hello"
>
> class MyClass:
> def func2(self):
>   #how can I call func1 here.
>   func1() #results in an error
>
> myInstance = MyClass()
> myInstance.func2()
>
> [EMAIL PROTECTED] ~/Projects/Python
> $ python test07.py
> hello
>
> regards
>   Steve
> --
> Steve Holden+1 571 484 6266   +1 800 494 3119
> Holden Web LLC/Ltd  http://www.holdenweb.com
> Skype: holdenweb  http://del.icio.us/steve.holden
> --- Asciimercial --
> Get on the web: Blog, lens and tag the Internet
> Many services currently offer free registration
> --- Thank You for Reading -- Hide quoted text -
>
> - Show quoted text -

I apologize for not posting the exact code and error message. The
missing "self" is due to a typo of mine. It is not really the problem
I am encountering.

testmodule.py
-
"""Test Module"""

def __module_level_func():
print "Hello"

class TestClass:
def class_level_func(self):
__module_level_func()


main.py
--
import testmodule

x=testmodule.TestClass()
x.class_level_func()


The error message I am encountering is: NameError: global name
'_TestClass__module_level_func' is not defined

I think it has something to do with the two underscores for
__module_level_func. Maybe it has something to do with the python
implementation of the private class level functions.

By the way, the reason I am naming it __module_level_func() is because
I'd like __module_level_func() to be private to the module, like the C
static function. If the interpreter cannot really enforce it, at least
it is some sort of naming convention for me.

Thanks,
Geoffrey

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


Re: How to call module functions inside class instance functions?

2007-08-18 Thread beginner
On Aug 18, 8:13 pm, Zentrader <[EMAIL PROTECTED]> wrote:
> On Aug 18, 5:40 pm, beginner <[EMAIL PROTECTED]> wrote:
>
>
>
>
>
> > Hi Everyone,
>
> > I have encountered a small problems.  How to call module functions
> > inside class instance functions? For example,  calling func1 in func2
> > resulted in a compiling error.
>
> > "my module here"
>
> > def func1():
> >  print "hello"
>
> > class MyClass:
> >def func2():
> >  #how can I call func1 here.
> >  func1() #results in an error
>
> > Thanks,
> > Geoffrey
>
> You might want to check one of the online tutorials about how to code
> classes.  Google or look at "Learning Python" herehttp://www.python-eggs.org/
> def func1():
>  print "hello"
>
> class MyClass:
>def func2(self):
>  #how can I call func1 here.
>  func1() #results in an error
>
> MC= MyClass()
> MC.func2()- Hide quoted text -
>
> - Show quoted text -

Thanks for your help. The missing "self" is a typo of mine. It is not
the problem I am encountering. Sorry for posting the wrong code.

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


Re: "constructor" or initialization function for module

2007-08-18 Thread beginner
On Aug 18, 8:25 pm, Steve Holden <[EMAIL PROTECTED]> wrote:
> beginner wrote:
> > Hi Everyone,
>
> > An extended module (.pyd) written in C have an init function that is
> > called when the module is imported. Does anyone know if there is a way
> > to provide an init function for a module written in python?
>
> The body of a Python module is executed the first time it is imported
> (that's how the functions and classes get defined: class and def
> statements are executable), so just put your initialization code inline.
>
> regards
>   Steve
> --
> Steve Holden+1 571 484 6266   +1 800 494 3119
> Holden Web LLC/Ltd  http://www.holdenweb.com
> Skype: holdenweb  http://del.icio.us/steve.holden
> --- Asciimercial --
> Get on the web: Blog, lens and tag the Internet
> Many services currently offer free registration
> --- Thank You for Reading -

I see. That works.

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


Re: How to make a module function visible only inside the module?

2007-08-18 Thread beginner
On Aug 18, 8:27 pm, [EMAIL PROTECTED] (Lawrence Oluyede) wrote:
> beginner <[EMAIL PROTECTED]> wrote:
> > Is there any equivalent version of C's static function in Python. I
> > know I can make a class function private by starting a function name
> > with two underscores, but it does not work with module functions.
>
> The trick for the name mangling does not work at module level. Anyway,
> if you read the PEP 8 [1] you can correctly write your code following a
> well known coding standard. A function like this:
>
> def _f():
> pass
>
> is meant to be private, you can also state it in the function's
> docstring to be more clear, if you want, but it's not necessary
>
> > For exmaple, __func1 is still visible outside the module.
>
> Yes, and _f() will also be. There's no such thing as enforcing
> encapsulation in Python, even the "__method()" trick can be easily
> bypassed if you have to.
>
> 1 - <http://www.python.org/dev/peps/pep-0008/>
>
> HTH
>
> --
> Lawrence, oluyede.org - neropercaso.it
> "It is difficult to get a man to understand
> something when his salary depends on not
> understanding it" - Upton Sinclair

Thanks a lot. I was using two underscores, __module_method() as my
static method convention, and then I had some problems calling them
from inside class methods.

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


Re: How to make a module function visible only inside the module?

2007-08-19 Thread beginner
On Aug 19, 7:45 am, Bjoern Schliessmann  wrote:
> beginner wrote:
> > Thanks a lot. I was using two underscores, __module_method() as my
> > static method convention, and then I had some problems calling
> > them from inside class methods.
>
> *Please* do yourself and other people that sometime may have to read
> your code a favor and write code at least loosely oriented to
> PEP 8.
>
> BTW, Python has no "static methods" at module level. And I suppose
> what you call "class methods" actually aren't.
>
> Regards,
>
> Björn
>
> --
> BOFH excuse #183:
>
> filesystem not big enough for Jumbo Kernel Patch

I just started learning the language. I wasn't aware of the PEP.

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

'REPL' style IDE

2007-08-20 Thread beginner
Hi Everyone,

I am using the Wing IDE. It works great when developing applications,
but the workflow is like Visual Studio -- after you execute it or
debug it, the python script ends.

What I want is an interactive interpreting environment. I want the IDE
to execute a boot script to initialize my environment and create some
basic data objects. And then I want to be able to type in command on
the command line using these objects. The IDLE that comes with Python
does this, but compared with Wing, it does not have a lot of the
convenient features.

I am wondering if there is anything more powerful than IDLE that can
do this.

Thanks,
Geoffrey

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


Re: 'REPL' style IDE

2007-08-20 Thread beginner
On Aug 20, 12:50 pm, beginner <[EMAIL PROTECTED]> wrote:
> Hi Everyone,
>
> I am using the Wing IDE. It works great when developing applications,
> but the workflow is like Visual Studio -- after you execute it or
> debug it, the python script ends.
>
> What I want is an interactive interpreting environment. I want the IDE
> to execute a boot script to initialize my environment and create some
> basic data objects. And then I want to be able to type in command on
> the command line using these objects. The IDLE that comes with Python
> does this, but compared with Wing, it does not have a lot of the
> convenient features.
>
> I am wondering if there is anything more powerful than IDLE that can
> do this.
>
> Thanks,
> Geoffrey

If Wing could load my init script into a python session ONCE and then
run my code in another files MANY times, with or without the debugger,
without starting a new python session, that would be great.

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


Re: str().join() isn't working

2007-08-20 Thread beginner
On Aug 20, 1:16 pm, "Robert Dailey" <[EMAIL PROTECTED]> wrote:
> here is a more realized example of the lists I'm trying to join:
>
> _user_includes = [
> "../src",
> "../resource",
> "../inc",
> "../src",
> "../data",
> "../gui",
> "../script",
> "../script/actions",
> "../gui/dispatch",
> "../gui/factories",
> "../gui/frames",
> "../gui/getters",
> "../gui/localization",
> "../gui/player",
> "../gui/setters",
> "../gui/utilities",
> "../sis",
> "../player",
> "../platform/ngi",
> "../../engine",
> "../../engine/audio/NGI",
> "../../engine/io",
> "../../engine/io\NGI",
> "../../engine/math",
> "../../engine/math/fixed",
> "../../engine/path/NGI",
> "../../engine/text/NGI",
> "../../engine/mem",
> "../../engine/text",
> "../../engine/observer",
> "../../sdk/tiny_xml",
> "../../sdk/zlib",
> "../../sdk/lpng",
> "../../sdk/IFDLib/Source/Precompile",
> "../../sdk/IFDLib/Source/CoreLib",
> "../../sdk/IFDLib/inc",
> "../../sdk/IFDLib/Source/UtilLib",
> "../../sdk/IFDLib/Source/GameLib",
> "../../sdk/IFDlib/Source/OSLib/_NGI",
> "../../sdk/stl-port/NGI",
> "../../sdk/mini-boost/NGI",
> "../../sdk/mini-boost/COMMON",
> ]
>
> _system_includes = [
> "../../../../../../Symbian/9.1/NGAGE_SDK_1.1/EPOC32/include",
> 
> "../../../../../../Symbian/9.1/NGAGE_SDK_1.1/EPOC32/include/osextensions/rg­a",
> 
> "../../../../../../Symbian/9.1/NGAGE_SDK_1.1/EPOC32/include/osextensions/st­dapis",
> 
> "../../../../../../Symbian/9.1/NGAGE_SDK_1.1/EPOC32/include/osextensions/st­dapis/stlport",
> "../../../../../../Symbian/9.1/NGAGE_SDK_1.1/epoc32/include/variant"
> ]
>
> On 8/20/07, Robert Dailey <[EMAIL PROTECTED]> wrote:
>
>
>
> > Hi,
>
> > First have a look at the following code:
>
> > In main.py:
> > ---­
> > space = " "
>
> > includes = space.join ( system._user_includes ) + " " + space.join(
> > system._system_includes )
>
> > In system.py:
> > ---­
> > _user_includes = [
> > ]
>
> > _system_includes = [
> > ]
>
> > The above does not work. The interpreter states: "TypeError: sequence item
> > 0: expected string, list found". I'm not sure what this means. Can anyone
> > help me figure out what I'm doing wrong? Thanks.
>
> > PS: I've also tried putting strings in the lists above just to make sure
> > that them being empty wasn't the problem. I got no different results.- Hide 
> > quoted text -
>
> - Show quoted text -


I have no system.py, but when I run

includes = space.join (_user_includes ) + " " +
space.join(_system_includes )


with the lists given in your post, I get no error. I am using v2.5.1
on Windows XP.


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

Re: 'REPL' style IDE

2007-08-20 Thread beginner
On Aug 20, 2:51 pm, JoeSox <[EMAIL PROTECTED]> wrote:
> On 8/20/07, beginner <[EMAIL PROTECTED]> wrote:
>
> > Hi Everyone,
>
> > I am using the Wing IDE. It works great when developing applications,
> > but the workflow is like Visual Studio -- after you execute it or
> > debug it, the python script ends.
>
> > What I want is an interactive interpreting environment. I want the IDE
> > to execute a boot script to initialize my environment and create some
> > basic data objects. And then I want to be able to type in command on
> > the command line using these objects. The IDLE that comes with Python
> > does this, but compared with Wing, it does not have a lot of the
> > convenient features.
>
> > I am wondering if there is anything more powerful than IDLE that can
> > do this.
>
> I use Wing IDE.
> Place something like this on the bottom of your module you are debuging.
> Place a stop point on the line you want then start your debug! Hope that 
> helps.
>
> def test():
> c=MyClass
> c.do_foobar()
>
> if __name__ == '__main__':
>   test()
> --
> Later, Joe

Thanks for your help.

Yes, this is what I always do. However, I want more than debugging
interactively. I am trying to use the python interperter as a
programmable application. Once I run my boot script, the python
interpreter should have all the right objects and libraries, and the
end user (who obviously knows python) can simply type in a few
commands, calling my functions, to achieve his purposes
interactively.

What I am doing right now is almost exactly what you prescribed. I put
a 'pass' statement at the end of my boot script and set a breakpoint
on it, so that once the debugger stops on the breakpoint, the user can
start typing in commands in the Debug Probe window. But this is by no
means a nice set up.

Thanks,
Geoffrey

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


Re: 'REPL' style IDE

2007-08-20 Thread beginner
On Aug 20, 2:39 pm, Jeff <[EMAIL PROTECTED]> wrote:
> python-mode in Emacs.

Yeah, but I don't know anything about Emacs and as far as I know it is
pretty complicated.

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


List Comprehension Question: One to Many Mapping?

2007-08-23 Thread beginner
Hi All,

How do I map a list to two lists with list comprehension?

For example, if I have x=[ [1,2], [3,4] ]

What I want is a new list of list that has four sub-lists:

[[1,2], [f(1), f(2)], [3,4], [f(3), f(4)]]

[1,2] is mapped to [1,2] and [f(1), f(2)] and [3,4] is mapped to
[3,4], [f(3), f(4)].

I just can't find any way to do that with list comprension. I ended up
using a loop (untested code based on real code):

l=[]
for y in x:
   l.append(y)
   l.append([f(z) for z in y])

Thanks,
Geoffrey

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


Re: List Comprehension Question: One to Many Mapping?

2007-08-23 Thread beginner
On Aug 24, 12:41 am, Davo <[EMAIL PROTECTED]> wrote:
> On Aug 23, 9:24 pm, beginner <[EMAIL PROTECTED]> wrote:
>
>
>
>
>
> > Hi All,
>
> > How do I map a list to two lists with list comprehension?
>
> > For example, if I have x=[ [1,2], [3,4] ]
>
> > What I want is a new list of list that has four sub-lists:
>
> > [[1,2], [f(1), f(2)], [3,4], [f(3), f(4)]]
>
> > [1,2] is mapped to [1,2] and [f(1), f(2)] and [3,4] is mapped to
> > [3,4], [f(3), f(4)].
>
> > I just can't find any way to do that with list comprension. I ended up
> > using a loop (untested code based on real code):
>
> > l=[]
> > for y in x:
> >l.append(y)
> >l.append([f(z) for z in y])
>
> > Thanks,
> > Geoffrey
>
> This may be what you want:
>
> l = [[y, [f(z) for z in y]] for y in x]
>
> But It's a bit dense.  How about:
> l=[]
> for y in x:
> Fy = [f(z) for z in y]
> l.extend([y, Fy])
>
> -- David- Hide quoted text -
>
> - Show quoted text -

This is exactly what I was looking for. Thanks.

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


Re: List Comprehension Question: One to Many Mapping?

2007-08-24 Thread beginner
On Aug 24, 12:44 am, beginner <[EMAIL PROTECTED]> wrote:
> On Aug 24, 12:41 am, Davo <[EMAIL PROTECTED]> wrote:
>
>
>
>
>
> > On Aug 23, 9:24 pm, beginner <[EMAIL PROTECTED]> wrote:
>
> > > Hi All,
>
> > > How do I map a list to two lists with list comprehension?
>
> > > For example, if I have x=[ [1,2], [3,4] ]
>
> > > What I want is a new list of list that has four sub-lists:
>
> > > [[1,2], [f(1), f(2)], [3,4], [f(3), f(4)]]
>
> > > [1,2] is mapped to [1,2] and [f(1), f(2)] and [3,4] is mapped to
> > > [3,4], [f(3), f(4)].
>
> > > I just can't find any way to do that with list comprension. I ended up
> > > using a loop (untested code based on real code):
>
> > > l=[]
> > > for y in x:
> > >l.append(y)
> > >l.append([f(z) for z in y])
>
> > > Thanks,
> > > Geoffrey
>
> > This may be what you want:
>
> > l = [[y, [f(z) for z in y]] for y in x]
>
> > But It's a bit dense.  How about:
> > l=[]
> > for y in x:
> > Fy = [f(z) for z in y]
> > l.extend([y, Fy])
>
> > -- David- Hide quoted text -
>
> > - Show quoted text -
>
> This is exactly what I was looking for. Thanks.- Hide quoted text -
>
> - Show quoted text -

On second thought, that will generate one additional level. So it is
not what I am looking for.

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


Re: List Comprehension Question: One to Many Mapping?

2007-08-24 Thread beginner
On Aug 24, 5:47 am, Paul Rubin  wrote:
> Boris Borcic <[EMAIL PROTECTED]> writes:
> > >> For example, if I have x=[ [1,2], [3,4] ]
>
> > >> What I want is a new list of list that has four sub-lists:
>
> > >> [[1,2], [f(1), f(2)], [3,4], [f(3), f(4)]]
> > > [[a, map(f,a)] for a in x]
>
> > no, that one will be [[[1,2], [f(1), f(2)]], [[3,4], [f(3), f(4)]]]
> > eg two sublists instead of four.
>
> Oh ugh, I misread the original request and thought the extra brackets
> were there.  I think the following works and is reasonably intuitive:
>
> from itertools import chain
> y = list(chain(*([a, map(f,a)] for a in x)))
>
> But the original request itself seems a bit weird.

Not so werid. :-) Just to put this into context, I have a list of list
of objects x=[[o1, o2, o3, o4], [o5,o6]]

I was trying to plot them with matplotlib. Each sub-list is a line. So
I would have to re-organize the list to be like the following:

v=[[o1.x, o2.x, o3.x, o4.x], [o1.y, o2.y, o3.y, o4.y], [o5.x, o6.x],
[o5.y, o6.y]]

So that I can pass it to plot:

plot(*tuple(v))

I like the chain and map solutions.

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


Re: Joining Big Files

2007-08-25 Thread beginner
On Aug 25, 4:57 am, mosscliffe <[EMAIL PROTECTED]> wrote:
> I have 4 text files each approx 50mb.
>
> I need to join these into one large text file.
>
> I only need to do this very occasionally, as the problem has occurred
> because of upload limitations.
>
> Bearing in mind filesize and memory useage, would I be better reading
> every line in every file and writing each line to the output file or
> is there someway I could execute some shell command.
>
> I am working on a hosted website with no direct telnet or similar
> access.
>
> I would appreciate any tips, best code, as I do not want to break
> anything on the website.
>
> Richard

I would probably open the files in binary mode and copy 128 KB - 512
KB at a time. I wouldn't copy a line at a time as it is possible that
one of the files contains a very long line.


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


Re: List Comprehension Question: One to Many Mapping?

2007-09-14 Thread beginner
On Aug 24, 5:35 am, Boris Borcic <[EMAIL PROTECTED]> wrote:
> Paul Rubin wrote:
> > beginner <[EMAIL PROTECTED]> writes:
> >> For example, if I have x=[ [1,2], [3,4] ]
>
> >> What I want is a new list of list that has four sub-lists:
>
> >> [[1,2], [f(1), f(2)], [3,4], [f(3), f(4)]]
>
> > [[a, map(f,a)] for a in x]
>
> no, that one will be [[[1,2], [f(1), f(2)]], [[3,4], [f(3), f(4)]]]
> eg two sublists instead of four.
>
> [map(g,a) for a in x for g in [None,f]]
>
> will do it.
>
> ...a bit too cleverly, but there's worse :
>
> list((yield a) or map(f,a) for a in x)
>
> Cheers, BB

Really cool!

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


Speed of Nested Functions & Lambda Expressions

2007-10-23 Thread beginner
Hi All,

It is really convenient to use nested functions and lambda
expressions. What I'd like to know is if Python compiles fn_inner()
only once and change the binding of v every time fn_outer() is called
or if Python compile and generate a new function object every time. If
it is the latter, will there be a huge performance hit? Would someone
give some hint about how exactly Python does this internally?

def fn_outer(v):
a=v*2
def fn_inner():
print "V:%d,%d" % (v,a)

fn_inner()

Thanks,
Geoffrey

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


Life-time of temporary variables in list comprehensions

2007-10-23 Thread beginner
Hi All,

If I have a list comprehension:

ab=["A","B"]
c = "ABC"
[1.0 if c=='A' else c='B' for c in ab]
print c

>>"B"

My test shows that if c is not defined before the list comprehension,
it will be created in the list comprehension; if it is defined before
the list comprehension, the value will be overwritten. In other words,
temp variables are not local to list comprehensions.

My question is why is this and is there any way to make c local to
list comp?

Thanks,
Geoffrey

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


Re: Life-time of temporary variables in list comprehensions

2007-10-23 Thread beginner
On Oct 23, 12:02 pm, beginner <[EMAIL PROTECTED]> wrote:
> Hi All,
>
> If I have a list comprehension:
>
> ab=["A","B"]
> c = "ABC"
> [1.0 if c=='A' else c='B' for c in ab]
> print c
>
> >>"B"
>
> My test shows that if c is not defined before the list comprehension,
> it will be created in the list comprehension; if it is defined before
> the list comprehension, the value will be overwritten. In other words,
> temp variables are not local to list comprehensions.
>
> My question is why is this and is there any way to make c local to
> list comp?
>
> Thanks,
> Geoffrey

I see.

Thanks for everyone's help!

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


Re: Speed of Nested Functions & Lambda Expressions

2007-10-23 Thread beginner
On Oct 23, 11:06 am, Gary Herron <[EMAIL PROTECTED]> wrote:
> beginner wrote:
> > Hi All,
>
> > It is really convenient to use nested functions and lambda
> > expressions. What I'd like to know is if Python compiles fn_inner()
> > only once and change the binding of v every time fn_outer() is called
> > or if Python compile and generate a new function object every time. If
> > it is the latter, will there be a huge performance hit? Would someone
> > give some hint about how exactly Python does this internally?
>
> > def fn_outer(v):
> > a=v*2
> > def fn_inner():
> > print "V:%d,%d" % (v,a)
>
> > fn_inner()
>
> > Thanks,
> > Geoffrey
>
> The code is compiled only once when the file is initially read in.
> During execution of fn_outer, v will be bound to a value, then a, then
> fn_inner will be bound (to an already compiled code object) and so on.
>
> Really, from the point of view of Python while executing fn_outer, the
> def of fn_inner looks just like an assignment with fn_inner as the
> variable name and a code object as the value.
>
> Gary Herron- Hide quoted text -
>
> - Show quoted text -

I see. Thanks Gary!

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


Re: Speed of Nested Functions & Lambda Expressions

2007-10-24 Thread beginner
On Oct 24, 2:52 am, Duncan Booth <[EMAIL PROTECTED]> wrote:
> beginner <[EMAIL PROTECTED]> wrote:
> > It is really convenient to use nested functions and lambda
> > expressions. What I'd like to know is if Python compiles fn_inner()
> > only once and change the binding of v every time fn_outer() is called
> > or if Python compile and generate a new function object every time. If
> > it is the latter, will there be a huge performance hit? Would someone
> > give some hint about how exactly Python does this internally?
>
> You can use Python's bytecode disassembler to see what actually gets
> executed here:
>
> >>> def fn_outer(v):
>
> a=v*2
> def fn_inner():
> print "V:%d,%d" % (v,a)
>
> fn_inner()
>
> >>> import dis
> >>> dis.dis(fn_outer)
>
>   2   0 LOAD_DEREF   1 (v)
>   3 LOAD_CONST   1 (2)
>   6 BINARY_MULTIPLY
>   7 STORE_DEREF  0 (a)
>
>   3  10 LOAD_CLOSURE 0 (a)
>  13 LOAD_CLOSURE 1 (v)
>  16 BUILD_TUPLE  2
>  19 LOAD_CONST   2 ( 01177218, file "", line 3>)
>  22 MAKE_CLOSURE 0
>  25 STORE_FAST   1 (fn_inner)
>
>   6  28 LOAD_FAST1 (fn_inner)
>  31 CALL_FUNCTION0
>  34 POP_TOP
>  35 LOAD_CONST   0 (None)
>  38 RETURN_VALUE
>
>
>
> When you execute the 'def' statement, the two scoped variables a and v
> are built into a tuple on the stack, the compiled code object for the
> inner function is also pushed onto the stack and then the function is
> created by the 'MAKE_CLOSURE' instruction. This is then stored in a
> local variable (STORE_FAST) which is then loaded and called.
>
> So the function definition is pretty fast, BUT notice how fn_inner is
> referenced by STORE_FAST/LOAD_FAST whereas a and v are referenced by
> LOAD_DEREF/STORE_DEREF and LOAD_CLOSURE.
>
> The code for fn_inner also uses LOAD_DEREF to get at the scoped
> variables:
>
>   4   0 LOAD_CONST   1 ('V:%d,%d')
>   3 LOAD_DEREF   1 (v)
>   6 LOAD_DEREF   0 (a)
>   9 BUILD_TUPLE  2
>  12 BINARY_MODULO  
>  13 PRINT_ITEM  
>  14 PRINT_NEWLINE  
>  15 LOAD_CONST   0 (None)
>  18 RETURN_VALUE
>
> (its a bit harder to disassemble that one, I stuck a call to dis.dis
> inside fn_outer to get that)
>
> If you do some timings you'll find that LOAD_DEREF/STORE_DEREF are
> rather slower than LOAD_FAST/STORE_FAST, so while the overhead for
> creating the function is minimal you could find that if you access the
> variables a lot (even in fn_outer) there may be a measurable slow-down.
>
> If timings show that it is a code hotspot then you might find it better
> to nest the function but pass any required values in as parameters (but
> if you don't have evidence for this just write whatever is clearest).


Thanks for the detailed analysis, Duncan. Also thanks for showing how
the disassembler can be used to figure this out. I was just looking
for a tool like this. This is great. Thanks again.

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


Re: Better writing in python

2007-10-24 Thread beginner
On Oct 24, 9:04 am, "A.T.Hofkamp" <[EMAIL PROTECTED]> wrote:
> > On 2007-10-24, Alexandre Badez <[EMAIL PROTECTED]> wrote:
> > I'm just wondering, if I could write a in a "better" way this
> > code
>
> > lMandatory = []
> > lOptional = []
> > for arg in cls.dArguments:
> >   if arg is True:
> > lMandatory.append(arg)
> >   else:
> > lOptional.append(arg)
> > return (lMandatory, lOptional)
>
> > I think there is a better way, but I can't see how...
>
> You can do it shorter, not sure that it also qualifies as better
>
> d = { True : [] , False : [] }
> for arg in cls.arguments:
>   d[arg == True].append(arg)
>
> return d[True], d[False]
>
> One potential problem here is that 'arg == True' may not be the same as 'if
> arg:'. I couldn't come up with a better equivalent expression, maybe one of 
> the
> other readers knows more about this?
>
> Albert

d[bool(arg)].append(arg) resolves your concern?

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


Handle Exceptions Inside List Comprehension

2007-10-29 Thread beginner
Hi All,

I am wondering if there is any way to handle exceptions inside list
comprehension. For example,

[f(x) for x in xs]

I want to skip the point if f(x) raises an exception. How can I do
that without totally removing the list comprehension?

Thanks,
Geoffrey

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


Inefficient summing

2008-10-08 Thread beginner
Hi All,

I have a list of records like below:

rec=[{"F1":1, "F2":2}, {"F1":3, "F2":4} ]

Now I want to write code to find out the ratio of the sums of the two
fields.

One thing I can do is:

sum(r["F1"] for r in rec)/sum(r["F2"] for r in rec)

But this is slow because I have to iterate through the list twice.
Also, in the case where rec is an iterator, it does not work.

I can also do this:

sum1, sum2= reduce(lambda x, y: (x[0]+y[0], x[1]+y[1]), ((r["F1"],
r["F2"]) for r in rec))
sum1/sum2

This loops through the list only once, and is probably more efficient,
but it is less readable.

I can of course use an old-fashioned loop. This is more readable, but
also more verbose.

What is the best way, I wonder?


-a new python programmer
--
http://mail.python.org/mailman/listinfo/python-list


Re: split a list based on a predicate

2008-10-09 Thread beginner
Hi,

On Oct 8, 6:36 pm, "Rajanikanth Jammalamadaka" <[EMAIL PROTECTED]>
wrote:
> Hi!
>
> Is there a functional way to do this?
>
> I have an array [0,1,2,3,0,1,2,2,3] and I want the first chunk of
> non-decreasing values from this array (eg: In this case I want
> [0,1,2,3])
>
> Thanks,
>
> Rajanikanth

Here is an idea. It is not the most efficient code!

def combine(l, a):
if not l or l[-1]http://mail.python.org/mailman/listinfo/python-list


Re: Inefficient summing

2008-10-09 Thread beginner
On Oct 9, 3:53 pm, Alexander Schmolck <[EMAIL PROTECTED]> wrote:
> beginner <[EMAIL PROTECTED]> writes:
> > Hi All,
>
> > I have a list of records like below:
>
> > rec=[{"F1":1, "F2":2}, {"F1":3, "F2":4} ]
>
> > Now I want to write code to find out the ratio of the sums of the two
> > fields.
>
> > One thing I can do is:
>
> > sum(r["F1"] for r in rec)/sum(r["F2"] for r in rec)
>
> > But this is slow because I have to iterate through the list twice.
> > Also, in the case where rec is an iterator, it does not work.
>
> how about:
>
> ratio = (lambda c: c.real/c.imag)(sum(complex(r["F1"], r["F2"] for r in rec)))
>
> ?
>
> :)- Hide quoted text -
>
> - Show quoted text -

Neat, but I will have a problem if I am dealing with three fields,
right?
--
http://mail.python.org/mailman/listinfo/python-list


How to write simple code to match strings?

2009-12-29 Thread beginner
Hi All,

I run into a problem.  I have a string s that can be a number of
possible things. I use a regular expression code like below to match
and parse it. But it looks very ugly. Also, the strings are literally
matched twice -- once for matching and once for extraction -- which
seems to be very slow. Is there any better way to handle this?


  def convert_data_item(s):
if re.match('^\$?([-+]?[0-9,]*\.?[0-9,]+)$',s):
g=re.match('^\$?([-+]?[0-9,]*\.?[0-9,]+)$',s)
v=float(g.group(1).replace(',',''))
elif re.match('^\(\$?([-+]?[0-9,]*\.?[0-9,]+)\)$',s):
g=re.match('^\(\$?([-+]?[0-9,]*\.?[0-9,]+)\)$',s)
v=-float(g.group(1).replace(',',''))
elif re.match('^\d{1,2}-\w+-\d{1,2}$',s):
v=dateutil.parser.parse(s, dayfirst=True)
elif s=='-':
v=None
else:
print "Unrecognized format %s" % s
v=s
return v

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


Re: How to write simple code to match strings?

2009-12-29 Thread beginner
Hi Steve,

On Dec 30, 12:01 am, Steven D'Aprano  wrote:
> On Tue, 29 Dec 2009 21:01:05 -0800, beginner wrote:
> > Hi All,
>
> > I run into a problem.  I have a string s that can be a number of
> > possible things. I use a regular expression code like below to match and
> > parse it. But it looks very ugly. Also, the strings are literally
> > matched twice -- once for matching and once for extraction -- which
> > seems to be very slow. Is there any better way to handle this?
>
> The most important thing you should do is to put the regular expressions
> into named variables, rather than typing them out twice. The names
> should, preferably, describe what they represent.
>
> Oh, and you should use raw strings for regexes. In this particular
> example, I don't think it makes a difference, but if you ever modify the
> strings, it will!
>
> You should get rid of the unnecessary double calls to match. That's just
> wasteful. Also, since re.match tests the start of the string, you don't
> need the leading ^ regex (but you do need the $ to match the end of the
> string).
>
> You should also fix the syntax error, where you have "elif s=='-'"
> instead of "elif s='-'".
>
> You should consider putting the cheapest test(s) first, or even moving
> the expensive tests into a separate function.
>
> And don't be so stingy with spaces in your source code, it helps
> readability by reducing the density of characters.
>
> So, here's my version:
>
> def _re_match_items(s):
>     # Setup some regular expressions.
>     COMMON_RE = r'\$?([-+]?[0-9,]*\.?[0-9,]+)'
>     FLOAT_RE = COMMON_RE + '$'
>     BRACKETED_FLOAT_RE = r'\(' + COMMON_RE + r'\)$'
>     DATE_RE = r'\d{1,2}-\w+-\d{1,2}$'
>     mo = re.match(FLOAT_RE, s)  # "mo" short for "match object"
>     if mo:
>         return float(mo.group(1).replace(',', ''))
>     # Otherwise mo will be None and we go on to the next test.
>     mo = re.match(BRACKETED_FLOAT_RE, s)
>     if mo:
>         return -float(mo.group(1).replace(',', ''))
>     if re.match(DATE_RE, s):
>         return dateutil.parser.parse(s, dayfirst=True)
>     raise ValueError("bad string can't be matched")
>
> def convert_data_item(s):
>     if s = '-':
>         return None
>     else:
>         try:
>             return _re_match_items(s)
>         except ValueError:
>             print "Unrecognized format %s" % s
>             return s
>
> Hope this helps.
>
> --
> Steven

This definitely helps.

I don't know if it should be s=='-' or s='-'. I thought == means equal
and = means assignment?

Thanks again,
G



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


Question

2005-08-28 Thread Beginner/Not Yet Programmer
I've never programmed before, so I thought I'd try and learn a bit by
using some Python tutorials.  I started using the tutorial at
http://www.honors.montana.edu/~jjc/easytut/easytut/node3.html.  It
mentioned different forms of Python, specifically Command Line and
IDLE.  Being inexperienced, I'm not sure how to change from Command
Line to IDLE, and I'm not sure which one I'm in when I start up the
program.  In my Python folder, the only applications I have are
python.exe and pythonw.exe.  Pythonw.exe won't run.  So, I run
python.exe, and I'm not sure whether it is IDLE, Command Line, or
neither.  Also, I'm unsure of how to save programs, considering when I
run python.exe, it opens up a window which does not have the little bar
with the "File", "Edit", "View" and "Help" buttons on it.  If you can
help me out at all, thank you.

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