Re: Need help with Python to C code compiler

2011-08-09 Thread Stefan Behnel

Vijay Anantha Murthy, 09.08.2011 07:37:

Is there any compiler which will help me convert my python code to proper C
code?
In my python code I am using the XML.dom.minidom module to parse an xml and
process the results obtained by ElementsByTagName.
I don't know of any such compiler which will help me convert this kind of
python code to C code.


It will certainly help much more to migrate your code to ElementTree first. 
See the xml.etree.cElementTree package, and also the external lxml.etree 
package. MiniDOM is known to have severe performance problems, including a 
huge memory overhead.




My intention is to convert python code to a readable C code which can later
be compile to an .exe file.


Have a look at py2exe then.



Cython will not be of much use to me as it is in the end writing c
extensions which will later be used in python.


So? You can just bundle CPython with it. Cython can directly generate the 
necessary code for embedding the runtime.




However, I wish to use this as a proper readable C code which can later be
shared with users - the c source code as well as the exe along with its
corresponding .py file.


Cython will mostly give you that. It may not produce the most obvious C 
code in some cases, because it is an optimising compiler, but it's readable 
(especially in the annotated HTML version), and it allows you to ship your 
C code with your .py file, thus avoiding a user dependency on Cython.




My main impediment here is writing out the C code manually myself, my C
skills are quite poor and it would require a huge effort to sharpening my
C skills before writing the code myself, I can not afford that luxury of
time.


That perfectly hits one of the more important use cases Cython is made for.



I was surfing and came across shedskin, but that might not just support the
xml minidom module for my purposes.


Shedskin produces very fast code and generates stand-alone modules. 
However, it's not meant to compile Python code at all, rather a statically 
typed subset of the language. That implies that most Python code won't 
compile unchanged with it. Cython is much closer to Python semantics and 
capable of compiling a huge amount of existing Python code. Plus, it 
generates fast code and provides a very straight forward path for 
optimising the compiled code.


There are other compilers listed here, although none of them has actual 
advantages over Cython and Shedskin:


http://wiki.python.org/moin/PythonImplementations#Compilers

Stefan

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


Re: Need help with Python to C code compiler

2011-08-09 Thread Chris Angelico
On Tue, Aug 9, 2011 at 6:37 AM, Vijay Anantha Murthy
 wrote:
> My main impediment here is writing out the C code manually myself, my C
> skills are quite poor and it would require a huge effort to sharpening my
> C skills before writing the code myself, I can not afford that luxury of
> time.

Writing code is a skill. You'll seldom get as readable code out of an
automated conversion utility than you would get by having an expert
hand-write the code; properly-rewritten code will look at the intent
behind the code, not just the current code.

I recommend either learning C, or accepting Cython or Shedskin.

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


Re: WxPython and TK

2011-08-09 Thread gb
azrael writes:

> OK, now. Isn't it maybe time to throw out TK once and for all?

no, because Tk has a clear advantage over many other UI tolkits
Tk _was designed_ and it was designed by very competent people [1]

good luck with smurfs' hunting [2], ciao
 gb

[1]:  1986-91 ACM's Grace Murray Hopper Awards
  1986 William N. Joy,  BSD Unix, Sun;
  1987 John Ousterhout, Tcl/TK;
  1988 Guy L. Steele,   Scheme, Java;
  1989 W. Daniel Hillis Thinking Machines;
  1990 Richard Stallman Emacs, Gcc, FSF;
  1991 Feng-hsiung Hsu  Deep Blue;

[2]   http://www.smurf.com/smurf.php/www/who/en/azrael
-- 
http://mail.python.org/mailman/listinfo/python-list


How to solve this problem

2011-08-09 Thread Johny
I have a client that  is a part of a local network.This client has a
local address( not public).Is there a way how I can connect to this
client from outside world?
What software must I install so that I can connect and control that
client from outside?

Thanks

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


Re: How to solve this problem

2011-08-09 Thread Katriel Cohn-Gordon
Unless you have the router configured to allow connections through to your
local client, you will need the client to connect *outwards*, either to you
or to an intermediate server.

On Tue, Aug 9, 2011 at 12:10 PM, Johny  wrote:

> I have a client that  is a part of a local network.This client has a
> local address( not public).Is there a way how I can connect to this
> client from outside world?
> What software must I install so that I can connect and control that
> client from outside?
>
> Thanks
>
> B
> --
> http://mail.python.org/mailman/listinfo/python-list
>
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: [Swig-user] Segmentation Fault on exit

2011-08-09 Thread Stefan Zager
On Sat, Aug 6, 2011 at 3:35 AM, Vipul Raheja  wrote:
> Hi,
>
> I have wrapped a library from C++ to Python using SWIG. But when I
> import it in Python, I am able to work fine with it, but it gives a
> segmentation fault while exiting. Following is the log:
>
> vipul@vipul-laptop:~/ossim-svn/src/pyossim/swig$ python
> Python 2.6.6 (r266:84292, Sep 15 2010, 15:52:39)
> [GCC 4.4.5] on linux2
> Type "help", "copyright", "credits" or "license" for more information.
 import pyossim

> * Do some stuff *
 exit()
> Segmentation fault
> vipul@vipul-laptop:~/ossim-svn/src/pyossim/swig$

I'm sorry, but that's not enough information to debug the problem.
Please provide a minimal complete test case which demonstrates the
problem.

Thanks,

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


Re: os.path.dirname(sys.argv[0]) always returns nothing

2011-08-09 Thread WaffleSouffle
On Aug 2, 12:57 am, Gregory Ewing  wrote:
> Thijs Engels wrote:
> > argv[0] returns the name of the current file (string), but no path
> > information if I recall correct.
>
> It's the path that was used to specify the script by whatever
> launched it, so it could be either absolute or relative to
> the current directory.
>
> --
> Greg

>From the docs there are a couple of special cases: "If the command was
executed using the -c command line option to the interpreter, argv[0]
is set to the string '-c'. If no script name was passed to the Python
interpreter, argv[0] is the empty string."

So if you're running in a python interpreter compiled into an
executable which doesn't initialise argv, that's another case where
things won't go as expected.

That's probably not relevant in this case, but I've certainly been
bitten by it in the past.
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: How to solve this problem

2011-08-09 Thread John Gordon
In <81a681e7-0be1-4edc-9e27-b492e8b63...@a17g2000yqk.googlegroups.com> Johny 
 writes:

> I have a client that  is a part of a local network.This client has a
> local address( not public).Is there a way how I can connect to this
> client from outside world?

When you say the client's address is "not public", what do you mean
exactly?  Can the client initiate connections to the outside network?
Is the client behind a firewall?

> What software must I install so that I can connect and control that
> client from outside?

Perhaps you should ask your network administrators.

And what does this question have to do with python?

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

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


Re: How to solve this problem

2011-08-09 Thread Ulrich Eckhardt
Johny wrote:
> I have a client that  is a part of a local network.This client has a
> local address( not public).Is there a way how I can connect to this
> client from outside world?
> What software must I install so that I can connect and control that
> client from outside?

How is that a Python problem? Ask your network admin to configure something 
like port forwarding.

Also, take a look at Eric S. Raymond's essay on asking smart questions, 
there are at least three things detailed there which you are doing wrong and 
where you are standing in your own way.

Good luck!

Uli

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

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


__all__

2011-08-09 Thread Ethan Furman

Greetings!

Does anyone know/recall the original purpose of __all__?

I had thought it was primarily to specify what would be imported when 
`from ... import *` was executed, such as for tk;  today, it seems it is 
also used to specify the API for the module, and so the help() subsystem 
will only provide details for those items found in __all__.


The issue I'm having with this is that there are roughly a dozen items I 
would like to make available via the `import *` mechanism in my dbf 
module, and another dozen that, while part of the public API, don't need 
to be available via an `import *`.


History lessons, as well as ideas, welcomed!

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


Re: __all__

2011-08-09 Thread Steven D'Aprano
Ethan Furman wrote:

> Greetings!
> 
> Does anyone know/recall the original purpose of __all__?

To customise the names available for `from ... import *`:

http://docs.python.org/whatsnew/2.1.html#other-changes-and-fixes


> I had thought it was primarily to specify what would be imported when
> `from ... import *` was executed, such as for tk;  

Yes, that was the original use. If __all__ is not defined, Python will
import everything that doesn't start with an underscore.


> today, it seems it is 
> also used to specify the API for the module, and so the help() subsystem
> will only provide details for those items found in __all__.

The two meanings are assumed to be synonymous: names in the public API
should be importable with *, and importable names should be in the public
API. You can't specify "this can be imported with *, but isn't public"
or "this is public, but not importable with *".

http://docs.python.org/reference/simple_stmts.html#index-1090


Also, the behaviour of __all__ with packages may be slightly different:

http://docs.python.org/tutorial/modules.html#index-1134

 
> The issue I'm having with this is that there are roughly a dozen items I
> would like to make available via the `import *` mechanism in my dbf
> module, and another dozen that, while part of the public API, don't need
> to be available via an `import *`.

You have to choose both, or neither, but you can't have just one.



-- 
Steven

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


numpy/scipy: calculate definite integral of sampled data

2011-08-09 Thread Manuel Graune
Hi everyone,

to calculate the definite integral of a function or an array of sampled
data scipy provides (among others) the quad and trapz functions.
So it is possible to compute e. g. the definite integral of cos(t) over
some range by doing

definite_integral= scipy.integrate.quad(cos,lower_limit,upper_limit) 

or

definite_integral= scipy.integrate.trapz(some_array).

Now, if I want to plot cos(t) and  the integral of cos(t) from 0 to t in
a graph, the necessary array can be calculated by:

@numpy.vectorize
def intfunc(fnc,upper_limit):
return scipy.integrate.quad(fnc,0.0,upper_limit)

definite_inegral= intfunc(cos,t)

which seems (whithout knowing the actual code) a bit wasteful and slow
but is relatively concise.

Now for my question: scipy provides e. g. the trapz-function to
calculate definite integral of a complete array of sampled data.
However, I have no idea how to get achieve the same as above for
sampled data (apart from manually iterating in a for-loop). Is there
a function somewhere which delivers an array of the definite integrals
for each of the data-points in an array? 


Regards,

Manuel


-- 
A hundred men did the rational thing. The sum of those rational choices was
called panic. Neal Stephenson -- System of the world
http://www.graune.org/GnuPG_pubkey.asc
Key fingerprint = 1E44 9CBD DEE4 9E07 5E0A  5828 5476 7E92 2DB4 3C99
-- 
http://mail.python.org/mailman/listinfo/python-list


Passing every element of a list as argument to a function

2011-08-09 Thread Antonio Vera
Hi!,
I have a very simple syntax question. I want to evaluate a library
function f receiving an arbitrary number of arguments (like
itertools.product), on the elements of a list l. This means that I
want to compute f(l[0],l[1],...,l[len(l)-1]).

Is there any operation "op" such that f(op(l)) will give the sequence
of elements of l as arguments to f?

Thanks for your time.
Best,
Antonio
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Passing every element of a list as argument to a function

2011-08-09 Thread Chris Angelico
On Tue, Aug 9, 2011 at 6:02 PM, Antonio Vera  wrote:
> Hi!,
> I have a very simple syntax question. I want to evaluate a library
> function f receiving an arbitrary number of arguments (like
> itertools.product), on the elements of a list l. This means that I
> want to compute f(l[0],l[1],...,l[len(l)-1]).
>
> Is there any operation "op" such that f(op(l)) will give the sequence
> of elements of l as arguments to f?

Yep!

f(*l)

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


Re: Object Diffs

2011-08-09 Thread Irmen de Jong

On 08-08-11 21:50, Croepha wrote:

Hello Python list:

I am doing research into doing network based propagation of python
objects.  In order to maintain network efficiency. I wan't to just
send the differences of python objects, I was wondering if there
was/is any other research or development in this area? I was thinking
that I could look at how pickle works an implement a diff system
there, or I could actually create a pickle of the object and then use
difflib to compare the flattened text...

Ideas, or comments welcome



No Python code, but you may want to have a look at the DeltaCompressor 
of the Kryo serializer (written for Java).

http://code.google.com/p/kryo/

(disclaimer: I haven't used it, just read a little bit about it)

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


RE: Passing every element of a list as argument to a function

2011-08-09 Thread Prasad, Ramit
-Original Message-
From: python-list-bounces+ramit.prasad=jpmorgan@python.org 
[mailto:python-list-bounces+ramit.prasad=jpmorgan@python.org] On Behalf Of 
Antonio Vera
Sent: Tuesday, August 09, 2011 12:02 PM
To: python-list@python.org
Subject: Passing every element of a list as argument to a function

Hi!,
I have a very simple syntax question. I want to evaluate a library
function f receiving an arbitrary number of arguments (like
itertools.product), on the elements of a list l. This means that I
want to compute f(l[0],l[1],...,l[len(l)-1]).

Is there any operation "op" such that f(op(l)) will give the sequence
of elements of l as arguments to f?

Thanks for your time.
Best,
Antonio
-- 
http://mail.python.org/mailman/listinfo/python-list

op(*l) for a list (or positional arguments).

If you are trying to pass named keyword arguments then you must pass it a 
dictionary { 'keywordName' : 'value' }
Example:
>>>def F(name=None):
  pass
>>>F(**{'name':'boo'})


Ramit


Ramit Prasad | JPMorgan Chase Investment Bank | Currencies Technology
712 Main Street | Houston, TX 77002
work phone: 713 - 216 - 5423


This email is confidential and subject to important disclaimers and
conditions including on offers for the purchase or sale of
securities, accuracy and completeness of information, viruses,
confidentiality, legal privilege, and legal entity disclaimers,
available at http://www.jpmorgan.com/pages/disclosures/email.  
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: __all__

2011-08-09 Thread Paul Woolcock
The gurus will have to correct me if this is not an accepted practice, but I
know some projects (Fabric is the one that comes to mind) will define a
submodule specifically for the 'from blah import *' situation. The submodule
would be called "api", or something like that, so you can do: 'from
mymodule.api import *' to separate the items you want to include in your
public api, while still keeping other names importable by 'import blah'
On Aug 9, 2011 1:00 PM, "Steven D'Aprano" <
steve+comp.lang.pyt...@pearwood.info> wrote:
> Ethan Furman wrote:
>
>> Greetings!
>>
>> Does anyone know/recall the original purpose of __all__?
>
> To customise the names available for `from ... import *`:
>
> http://docs.python.org/whatsnew/2.1.html#other-changes-and-fixes
>
>
>> I had thought it was primarily to specify what would be imported when
>> `from ... import *` was executed, such as for tk;
>
> Yes, that was the original use. If __all__ is not defined, Python will
> import everything that doesn't start with an underscore.
>
>
>> today, it seems it is
>> also used to specify the API for the module, and so the help() subsystem
>> will only provide details for those items found in __all__.
>
> The two meanings are assumed to be synonymous: names in the public API
> should be importable with *, and importable names should be in the public
> API. You can't specify "this can be imported with *, but isn't public"
> or "this is public, but not importable with *".
>
> http://docs.python.org/reference/simple_stmts.html#index-1090
>
>
> Also, the behaviour of __all__ with packages may be slightly different:
>
> http://docs.python.org/tutorial/modules.html#index-1134
>
>
>> The issue I'm having with this is that there are roughly a dozen items I
>> would like to make available via the `import *` mechanism in my dbf
>> module, and another dozen that, while part of the public API, don't need
>> to be available via an `import *`.
>
> You have to choose both, or neither, but you can't have just one.
>
>
>
> --
> Steven
>
> --
> http://mail.python.org/mailman/listinfo/python-list
-- 
http://mail.python.org/mailman/listinfo/python-list


just for fun: make a class (not its instances) iterable

2011-08-09 Thread Gelonida N
Hi,

I am just curious. There  is no real use case:

If I have a class and I want that its instances are iterable I can just
add a class metohod named __iter__()


example:

class MyClass(object):
def __iter__(self):
for val in range(10):
yield val


this allows me to do:
myobj = MyClass()
for val in myobj:
print val


Now I wondered whether there is any way to  implement a class such, that
I can write


for val in MyClass:
print val

I know I can implement  a classmethod (e.g myclassmethod() ) and write:

for val in MyClass.myclassmethod():
print val

but I wondered whether classed can be made iterable

Thanks in advance for your answers.

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


Re: What's in a name?

2011-08-09 Thread Ron
On Aug 1, 10:11 am, Andrew Berg  wrote:
> Hmm
> How about Rainbow Video Encoder Wrapper (Rainbow View for short - RView
> is taken, possibly multiple times)?
> I added an arbitrary word to a generic name, and the result doesn't seem
> to be taken by anything software-related. It wraps more than just video
> encoders (in fact, x264 will likely be the only one it wraps until it's
> matured quite a bit :P ), but I didn't want the name to get too long.
> The module itself will likely be called just rainbow.py.
>
> --
> CPython 3.2.1 | Windows NT 6.1.7601.17592 | Thunderbird 5.0
> PGP/GPG Public Key ID: 0xF88E034060A78FCB

Andrew

How about SuperMux

with a command line interface called Cyclops
and a GUI fornt end called VisualMux
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: just for fun: make a class (not its instances) iterable

2011-08-09 Thread Chris Kaynor
You can using metaclasses (untested):

>>>class MyMetaClass(type):
>>>def __iter__(self):
>>>return [1, 2, 3, 4]

>>>class MyClass(object):
>>>__metaclass__ = MyMetaClass

>>>for value in MyClass:
>>>print value
1
2
3
4

Chris


On Tue, Aug 9, 2011 at 2:43 PM, Gelonida N  wrote:

> Hi,
>
> I am just curious. There  is no real use case:
>
> If I have a class and I want that its instances are iterable I can just
> add a class metohod named __iter__()
>
>
> example:
>
> class MyClass(object):
>def __iter__(self):
>for val in range(10):
>yield val
>
>
> this allows me to do:
> myobj = MyClass()
> for val in myobj:
>print val
>
>
> Now I wondered whether there is any way to  implement a class such, that
> I can write
>
>
> for val in MyClass:
>print val
>
> I know I can implement  a classmethod (e.g myclassmethod() ) and write:
>
> for val in MyClass.myclassmethod():
>print val
>
> but I wondered whether classed can be made iterable
>
> Thanks in advance for your answers.
>
> --
> http://mail.python.org/mailman/listinfo/python-list
>
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Complex sort on big files

2011-08-09 Thread John Nagle

On 8/6/2011 10:53 AM, sturlamolden wrote:

On Aug 1, 5:33 pm, aliman  wrote:


I've read the recipe at [1] and understand that the way to sort a
large file is to break it into chunks, sort each chunk and write
sorted chunks to disk, then use heapq.merge to combine the chunks as
you read them.


Or just memory map the file (mmap.mmap) and do an inline .sort() on
the bytearray (Python 3.2). With Python 2.7, use e.g. numpy.memmap
instead. If the file is large, use 64-bit Python. You don't have to
process the file in chunks as the operating system will take care of
those details.

Sturla


   No, no, no.  If the file is too big to fit in memory, trying to
page it will just cause thrashing as the file pages in and out from
disk.

   The UNIX sort program is probably good enough.  There are better
approaches, if you have many gigabytes to sort, (see Syncsort, which
is a commercial product) but few people need them.

John Nagle

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


ANNOUNCING GOZERBOT 0.9.3 BETA1

2011-08-09 Thread Bart Thate
Hi world and every living thing on it !

I just want to give a headsup to the fact that i released the first
beta of GOZERBOT 0.9.3. This release brings in a lot of bug fixes, so
please try it out ;]

Download is at http://gozerbot.googlecode.com, please if you find
problems with the bot file a ticket on 
http://code.google.com/p/gozerbot/issues/list.
Otherwise email me at bth...@gmail.com or check channel #dunkbots on
irc.freenode.net, thnx ! feedback is very much appreciated ;]

I also want to say sorry to my korean brothers and sisters, i have no
evil intend with GOZERBOT even if you might read that into my
announcements, i'll give the reason for the name below .. it is taken
from the dutch language. Peace !


GOZERBOT name origin


23:37 < dunker> !tr nl en 1) Bargoense kerel 2) Bink 3) Gast 4) Goser
5)
Joch 6) Jongen 7) Ker 8) Kerel 9) Kerel (bargoens) 10) Knaap 11)
Knakker 12)
Knul 13) Man 14) Persoonsbenaming 15) Vent

23:37 < jsonbot> 1) Bargoens guy 2) Bink 3) Guest 4) Goser 5) Jochberg
6)
Girl 7) Ker 8) Guy 9) Dude (slang) 10) Knaap 11) bloke 12) Lad 13) F
14)
Name Person 15) Vent


친구
1) Bargoens 사람 2) 빙크 3) 게스트 4) Goser 5) Jochberg 6) 소녀 7) 케르 8)
가이 9) 친구 (속 어) 10) Knaap 11) 던 12) 래드 13) F 14) 이름 인 15) 벤트

About GOZERBOT:

GOZERBOT is a channel bot that aids with conversation in irc channels
and jabber conference rooms. its mainly used to send notifications
(RSS, nagios, etc.) and to have custom commands made for the channel.
More then just a channel bot GOZERBOT aims to provide a platform for
the user to program his own bot and make it into something thats
usefull. This is done with a plugin structure that makes it easy to
program your own plugins. But GOZERBOT comes with some batteries
included, there are now over 100 plugins already written and ready for
use.

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


Re: just for fun: make a class (not its instances) iterable

2011-08-09 Thread Steven D'Aprano
Gelonida N wrote:


> Now I wondered whether there is any way to  implement a class such, that
> I can write
> 
> 
> for val in MyClass:
> print val


One way to make an object iterable (there are others) is to add an __iter__
method to the object's class.

Unlike in some languages, classes in Python are themselves objects, which
means that classes themselves have a class, "type". The class of a class is
called the metaclass. So to make the class object (the instance) iterable,
use a metaclass.


>>> class MetaIter(type):
... def __iter__(self):
... for name in 'abcd':
... yield getattr(self, name)
...
>>> class MyClass(object):
... __metaclass__ = MetaIter
... # In Python 3, write as class MyClass(object, metaclass=MetaIter)
... a, b, c, d, = 42, 23, -99, "Surprise!"
...
>>>
>>> for obj in MyClass:
... print obj
...
42
23
-99
Surprise!




-- 
Steven

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


Re: just for fun: make a class (not its instances) iterable

2011-08-09 Thread Terry Reedy

On 8/9/2011 5:43 PM, Gelonida N wrote:

Now I wondered whether there is any way to  implement a class such, that
I can write

for val in MyClass:
print val


And what are the items in a class that you expect that to produce?

I am pretty sure you can already do
for val in MyClass.__dict__

(or __dict__.items())

--
Terry Jan Reedy

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


Re: numpy/scipy: calculate definite integral of sampled data

2011-08-09 Thread Terry Reedy

numpy/scipy questions are best asked on the numpy/scipy user lists.

--
Terry Jan Reedy

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


Re: just for fun: make a class (not its instances) iterable

2011-08-09 Thread Tim Chase

On 08/09/2011 07:11 PM, Terry Reedy wrote:

On 8/9/2011 5:43 PM, Gelonida N wrote:

Now I wondered whether there is any way to  implement a class such, that
I can write

for val in MyClass:
print val


And what are the items in a class that you expect that to produce?


I can see doing something like

  class MyClass:
instances = []
def __init__(self, *args, **kwargs):
  self.instances.append(self)
  # ...

  a = MyClass(...)
  b = MyClass(...)
  for instance in MyClass:
do_something(instance)

I was curious/surprised to find that

  class MyClass:
instances = []
@classmethod
def __iter__(cls):
  for i in cls.instances:
yield i
def __init__(self):
  self.instances.append(self)

didn't work as I expected since MyClass then has an __iter__ 
property that should know about the class.


-tkc



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


Re: just for fun: make a class (not its instances) iterable

2011-08-09 Thread Terry Reedy

On 8/9/2011 8:29 PM, Tim Chase wrote:

On 08/09/2011 07:11 PM, Terry Reedy wrote:

On 8/9/2011 5:43 PM, Gelonida N wrote:

Now I wondered whether there is any way to implement a class such, that
I can write

for val in MyClass:
print val


And what are the items in a class that you expect that to produce?


I can see doing something like

class MyClass:
instances = []
def __init__(self, *args, **kwargs):
self.instances.append(self)
# ...

a = MyClass(...)
b = MyClass(...)
for instance in MyClass:


for instance in MyClass.instances:


do_something(instance)

I was curious/surprised to find that

class MyClass:
instances = []
@classmethod
def __iter__(cls):
for i in cls.instances:
yield i
def __init__(self):
self.instances.append(self)

didn't work as I expected since MyClass then has an __iter__ property
that should know about the class.


I strongly suspect that iter(ob) starts the lookup for __iter__ with 
type(ob). I would also expect

  for instance in MyClass.__iter__()
to work, but that is a waste since instances is already iterable.

--
Terry Jan Reedy

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


input file format

2011-08-09 Thread Etay
Hi all,

I am trying to write a Python script which will read the output file
of a numerical model, and then copy some of that information into a
new file in a new format. The output file of the numerical model is
basically

54321  # number of points
1234  # number of time steps

__a whole bunch of lines with info I don't need__

[TIMESTAMP] 001
1  10  10
2  10  11
3  11  11
etc... in the format: Point#  X  Y for all 54321 points
[TIMESTAMP] 002
1  13  13
2  14  13
etc... for all 54321 points

and so for each of the 1234 time steps

I would like to be able to save the number of points and time steps,
then move all the time stamped data into a new file, but it needs to
be written there as [ Pt#  Y  X ].

Is there some simple way to skip over the lines I don't need, and then
inform Python about the format of the lines with the data I do need
within a loop?

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


Re: input file format

2011-08-09 Thread Steven D'Aprano
On Wed, 10 Aug 2011 01:00 pm Etay wrote:

> Is there some simple way to skip over the lines I don't need, and then
> inform Python about the format of the lines with the data I do need
> within a loop?

Yes. It's called "programming" :)

f = open('some file')
for line in f:
if some_condition(line):
continue  # skip the line
process(line)
f.close()

some_condition can be anything you like. You just have to write the code to
make it so.


-- 
Steven

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


multiprocessing timing issue

2011-08-09 Thread Tim Arnold

Hi, I'm having problems with an empty Queue using multiprocessing.

The task:
I have a bunch of chapters that I want to gather data on individually 
and then update a report database with the results.

I'm using multiprocessing to do the data-gathering simultaneously.

Each chapter report gets put on a Queue in their separate processes. 
Then each report gets picked off the queue and the report database is 
updated with the results.


My problem is that sometimes the Queue is empty and I guess it's
because the get_data() method takes a lot of time.

I've used multiprocessing before, but never with a Queue like this.
Any notes or suggestions are very welcome.

The task starts off with:
Reporter(chapters).report()

thanks,
--Tim Arnold

from Queue import Empty
from multiprocessing import Process, Queue

def run_mp(objects,fn):
q = Queue()
procs = dict()
for obj in objects:
procs[obj['name']] = Process(target=fn, args=(obj,q))
procs[obj['name']].start()

return q

class Reporter(object):
def __init__(self, chapters):
self.chapters = chapters

def report(self):
q = run_mp(self.chapters, self.get_data)

for i in range(len(self.chapters)):
try:
data = q.get(timeout=30)
except Empty:
print 'Report queue empty at %s' % (i)
else:
self.update_report(data)

def get_data(self, chapter, q):
data = expensive_calculations()
q.put(data)

def update_report(self, data):
db connection, etc.
--
http://mail.python.org/mailman/listinfo/python-list


allow line break at operators

2011-08-09 Thread Yingjie Lan
Hi all,

When writing a long expresion, one usually would like to break it into multiple 
lines. Currently, you may use a '\' to do so, but it looks a little awkward 
(more like machine-oriented thing). Therefore I start wondering why not allow 
line breaking at an operator, which is the standard way of breaking a long 
expression in publication? Here is an example:

#the old way

x = 1+2+3+4+\
  1+2+3+4

#the new way
x = 1+2+3+4+ #line continues as it is clearly unfinished

  1+2+3+4

Of course, the dot operator is also included, which may facilitate method 
chaining:

x = svg.append( 'circle' ).
  r(2).cx(1).xy(1).
  foreground('black').bkground('white')

Thoughts?

Yingjie

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


Re: allow line break at operators

2011-08-09 Thread Chris Rebert
On Tue, Aug 9, 2011 at 9:42 PM, Yingjie Lan  wrote:
> Hi all,
>
> When writing a long expresion, one usually would like to break it into 
> multiple lines. Currently, you may use a '\' to do so, but it looks a little 
> awkward (more like machine-oriented thing). Therefore I start wondering why 
> not allow line breaking at an operator, which is the standard way of breaking 
> a long expression in publication? Here is an example:
>
> #the old way
>
> x = 1+2+3+4+\
>   1+2+3+4
>
> #the new way
> x = 1+2+3+4+ #line continues as it is clearly unfinished
>
>   1+2+3+4

# the currently allowed way
 x = (1+2+3+4+
1+2+3+4)
# note the parentheses

I think this is sufficient.

> Of course, the dot operator is also included, which may facilitate method 
> chaining:
>
> x = svg.append( 'circle' ).
>   r(2).cx(1).xy(1).
>   foreground('black').bkground('white')

Python does not particularly endorse method chaining; it's why
list.sort(), list.append(), and similar methods of built-in types
return None rather than self.
Also, I dislike this for the dot operator especially, as it can
obscure whether a method call or a function call is taking place.

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


Re: allow line break at operators

2011-08-09 Thread Yingjie Lan
On Tue, Aug 9, 2011 at 9:42 PM, Yingjie Lan  wrote:

> Hi all,
>
> When writing a long expresion, one usually would like to break it into 
> multiple lines. Currently, you may use a '\' to do so, but it looks a little 
> awkward (more like machine-oriented thing). Therefore I start wondering why 
> not allow line breaking at an operator, which is the standard way of breaking 
> a long expression in publication? Here is an example:
>
> #the old way
>
> x = 1+2+3+4+\
>   1+2+3+4
>
> #the new way
> x = 1+2+3+4+ #line continues as it is clearly unfinished
>
>   1+2+3+4

:# the currently allowed way
:x = (1+2+3+4+
:    1+2+3+4)
:# note the parentheses
:
:I think this is sufficient.

That works, but not in the most natural way--the way people are customed 
to...why require a pair of parenthis when we can do without them? Also, the new 
way does not affect the old ways of doing things at all, it is fully backward 
compatible. So this just offers a new choice.

> Of course, the dot operator is also included, which may facilitate method 
> chaining:
>
> x = svg.append( 'circle' ).
>   r(2).cx(1).xy(1).
>   foreground('black').bkground('white')

:Also, I dislike this for the dot operator especially, as it can
:obscure whether a method call or a function call is taking place.

Again, this only offers a new choice, and does not force anybody to do it this 
way.

cheers,

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


subprocess.Popen and thread module

2011-08-09 Thread Danny Wong (dannwong)
Hi All,
   I'm trying to execute some external commands from multiple database.
I'm using threads and subprocess.Popen ( from docs, all the popen*
functions are deprecated and I was told to use subprocess.Popen) to
execute the external commands in parallel, but the commands seems to
hang. 
My question is:
Is subprocess.Popen thread safe?  If not, what other module should I use
to perform a system call? I also, want to log stdout and stderr to a
file. Thanks.
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: subprocess.Popen and thread module

2011-08-09 Thread Chris Rebert
On Tue, Aug 9, 2011 at 11:38 PM, Danny Wong (dannwong)
 wrote:
> Hi All,
>   I'm trying to execute some external commands from multiple database.
> I'm using threads and subprocess.Popen ( from docs, all the popen*
> functions are deprecated and I was told to use subprocess.Popen) to
> execute the external commands in parallel, but the commands seems to
> hang.

What's your Popen() call look like?

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


RE: subprocess.Popen and thread module

2011-08-09 Thread Danny Wong (dannwong)
Hi Chris,
Here is the code,
try:
cmd_output = subprocess.Popen(['scm', 'load', '--force', '-r', 
nickname, '-d', directory, project], stdout=subprocess.PIPE, 
stderr=subprocess.PIPE)
status = cmd_output.wait()
print "In load status is: %s" % status + "\n"
except:
print "Error Executing %s" % command + "\n"


-Original Message-
From: ch...@rebertia.com [mailto:ch...@rebertia.com] On Behalf Of Chris Rebert
Sent: Tuesday, August 09, 2011 11:47 PM
To: Danny Wong (dannwong)
Cc: python-list@python.org
Subject: Re: subprocess.Popen and thread module

On Tue, Aug 9, 2011 at 11:38 PM, Danny Wong (dannwong)
 wrote:
> Hi All,
>   I'm trying to execute some external commands from multiple database.
> I'm using threads and subprocess.Popen ( from docs, all the popen*
> functions are deprecated and I was told to use subprocess.Popen) to
> execute the external commands in parallel, but the commands seems to
> hang.

What's your Popen() call look like?

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