Re: Usage of main()

2009-09-04 Thread alex23
Sean DiZazzo  wrote:
> What are you using to test the scripts?  I could be completely wrong,
> but I find it hard to believe that the second version is much (if any)
> faster than the first.  Then again, I don't know much about the
> internals...

Sorry, Sean, unfortunately you are wrong, although it's understandable
that you've missed this.

The lookup of locally scoped references is a lot faster than that of
global ones, primarily due to the lookup order: it first checks the
local scope and then out through surrounding scopes _before_ the
global scope.

So yes, depending on the nature of your code, its quite conceivable to
find distinct performance differences between code using the __main__
idiom and code without.
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Usage of main()

2009-09-04 Thread Sean DiZazzo
On Sep 3, 11:55 pm, alex23  wrote:
> Sean DiZazzo  wrote:
> > What are you using to test the scripts?  I could be completely wrong,
> > but I find it hard to believe that the second version is much (if any)
> > faster than the first.  Then again, I don't know much about the
> > internals...
>
> Sorry, Sean, unfortunately you are wrong, although it's understandable
> that you've missed this.
>
> The lookup of locally scoped references is a lot faster than that of
> global ones, primarily due to the lookup order: it first checks the
> local scope and then out through surrounding scopes _before_ the
> global scope.
>
> So yes, depending on the nature of your code, its quite conceivable to
> find distinct performance differences between code using the __main__
> idiom and code without.

Interesting.  I guess at some point I should try to understand what is
going on under the covers.  Thanks.
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Usage of main()

2009-09-04 Thread Manuel Graune
Sean DiZazzo  writes:

> I'm trying to come up with an answer for you, but I can't...
>
> The if __name__ == "__main__": idiom *is* the standard way to write
> python programs, but it's not there to speed up programs.  It's there
> so that your program can be executed differently whether it is called
> as a runnable script from the command line, or if it is imported.


thanks for your answer. What you are explaining is exactly why I tried
it in the first place. I'm just wondering why (this is my impression,
not necessaryly the reallity) none of the recommended texts on python
put this in the first chapters. Instead - if it is mentioned at all -
it is hidden somewhere in the "advanced" sections. Even if the reason
for this is (I'm guessing...) because it is thought to be to complicated
to explain the "why" right at the beginning, it probably would not hurt
to just tell that this is the "correct" way of doing things right at the
start and add a footnote.

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


Re: match braces?

2009-09-04 Thread lallous
Hello,

Thank you all for your replies.

A simple suggestion as Chris' actually might help.

I am used to two spaces indentation since years, and apparently two
spaces won't make it clear if no visuals were present (braces, or
begin/end, ...)

Though it is not comfortable to change a style, I will play with 8
spaces indentation, it would naturally make it clearer ;)

--
Elias

On Sep 3, 11:43 am, Chris Rebert  wrote:
> On Thu, Sep 3, 2009 at 2:38 AM, lallous wrote:
> > Hello
>
> > In C/C++ you use the braces where as in Python you use the indentation
> > levels.
> > Most editors offer a Ctrl+[ to match the braces so that you can easily
> > identify the scopes (more correctly "statements blocks").
>
> > I am finding it difficult to see blocks
>
> Erm, how does the indentation itself not make it plainly and explicitly clear?
> Perhaps you need to set your tabstops wider?
>
> Cheers,
> Chris

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


Re: The future of Python immutability

2009-09-04 Thread r
"""The future of Python immutability"""

Define future:
  The future is a time period commonly understood to contain all
events that have yet to occur. It is the opposite of the past, and is
the time after the present

Define immutability:
  Not subject or susceptible to change. In object-oriented and
functional programming, an immutable object is an object whose state
cannot be modified after it is created. This is in contrast to a
mutable object, which can be modified after it is created.



hmm, applying this logic i'd have to say about the same really.

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


Re: Usage of main()

2009-09-04 Thread Carl Banks
On Sep 3, 11:55 pm, alex23  wrote:
> Sean DiZazzo  wrote:
> > What are you using to test the scripts?  I could be completely wrong,
> > but I find it hard to believe that the second version is much (if any)
> > faster than the first.  Then again, I don't know much about the
> > internals...
>
> Sorry, Sean, unfortunately you are wrong, although it's understandable
> that you've missed this.
>
> The lookup of locally scoped references is a lot faster than that of
> global ones, primarily due to the lookup order: it first checks the
> local scope and then out through surrounding scopes _before_ the
> global scope.

Sorry, alex, unfortunately you are wrong, although it's understandable
that you've missed this.

Actually, Python knows if a variable is local, nonlocal (meaning a
local from a surrounding scope), or global at compile time, so at run
time Python attempts only one kind of lookup.

The speedup comes because local lookups are much faster.  Accessing a
local is a simple index operation, and a nonlocal is a pointer deref
or two, then an indexing.  However for global variables the object is
looked up in a dictionary.


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


Re: possible attribute-oriented class

2009-09-04 Thread Peter Otten
Ken Newton wrote:

> class AttrClass(object):
> """AttrClass lets you freely add attributes in nested manner"""
> 
> def __init__(self):
> pass
> def __setitem__(self, key, value):
> return self.__dict__.__setitem__(key, value)
> def __repr__(self):
> return "%s(%s)" % (self.__class__.__name__,
> self.__dict__.__repr__())
> def __str__(self):
> ll = ['{']
> for k,v in self.__dict__.iteritems():
> ll.append("%s : %s" % (k, str(v)))
> return '\n'.join(ll) + '}'
> 
> def test():
> atr = AttrClass()
> atr.first = 1
> atr.second = 2
> atr.third = 'three'
> 
> atrsub = AttrClass()
> atrsub.aaa = 'AAA'
> atrsub.bbb = 'BBB'
> 
> atr.fourth = atrsub
> atr.fifth = 5
> 
> print atr
> print
> print repr(atr)
> print
> print atr.fourth.aaa

Just in case you didn't note: your test() function will run successfully 
even if you remove the __setitem__() method. Allowing

atr["x"] = 42

but not

print attr["x"]

may puzzle your intended audience.



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


Re: Usage of main()

2009-09-04 Thread Carl Banks
On Sep 3, 11:39 pm, Simon Brunning  wrote:
> 2009/9/4 Manuel Graune :
>
> > How come the main()-idiom is not "the standard way" of writing a
> > python-program (like e.g. in C)?
>
> Speaking for myself, it *is* the standard way to structure a script. I
> find it more readable, since I can put my main function at the very
> top where it's visible, with the classes and functions it makes use of
> following in some logical sequence.
>
> I suspect that this is the case for many real-world scripts. Perhaps
> it's mainly in books and demos where the extra stuff is left out so
> the reader can focus on what the writer is demonstrating?

Speaking for myself, I almost never put any logic at the top level in
anything other than tiny throwaway scripts.  Top level is for
importing, and defining functions, classes, and constants, and that's
it.

Even when doing things like preprocessing I'll define a function and
call it rather than putting the logic at top-level.  Sometimes I'll
throw in an if-test at top level (for the kind of stuff I might choose
an #if preprocessor statement in C for) but mostly I just put that in
functions.


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


Re: The future of Python immutability

2009-09-04 Thread Francesco Bochicchio
On Sep 3, 9:07 pm, Nigel Rantor  wrote:
>
> Right, this is where I would love to have had more experience with Haksell.
>
> Yes, as soon as you get to a situation where no thread can access shared
> state that is mutable your problems go away, you're also getting no work
> done becasue the threads, whilst they may be performing lots of
> interesting calculations have no way of allowing the rest of the
> program, or operating system, know about it.
>

Threads could communicate only with channels, message queue, or
equivalent. Is what
I do that as much as I can, to avoid the headache of sharing data
between threads. It is
less efficient than the shared data model and adds latency, but ensure
that each thread
is self-contained, making for safer programming and opening the way to
better parallelization.
AFAIK erlang Processes  and scala Actors implement a similar model at
language level.

In python, there is kamaelia that implements a similar paradigm,
although it is more concerned
with logical parallelization than with multitheading performance
issue.

I believe this kind of paradigms will bring us to the massive
multicore world easier than FP.
Consider that most FP languages have accepted a compromise and become
'unpure' (i.e. have
constructs to change variable in place). Even haskell, the only pure
FP
language I know (sort of), has things like mutable arrays.
All these features expose current FP languages at the same 'shared
resource' risks of imperative one,
although admittedly at a less level. And FP languages have their own
crop of problems - e.g how to deal
efficiently with deep recursion levels, parameters passed by copy,
huge list built in memory (if you use eager evaluation)
or build-up of thunks (if you use lazy evaluation).

But then, I'm just a programmer, so I could be easily wrong :-)

Ciao
-
FB


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


Re: The future of Python immutability

2009-09-04 Thread Ulrich Eckhardt
Nigel Rantor wrote:
> John Nagle wrote:
>> Immutability is interesting for threaded programs, because
>> immutable objects can be shared without risk.  Consider a programming
>> model where objects shared between threads must be either immutable or
>> "synchronized" in the sense that Java uses the term.
> 
> Yes, this is one of the reasons I am currently learning Haskell, I am
> not yet anywhwere near proficient but the reason I am looking into FP is
> because of some of the claims of the FP community, particularly Erlang,
> regarding the benefits of pure FP with respect to multi-threading.

Actually, I wouldn't say that FP itself changes anything there. However, FP
usually (correct me if I'm wrong) implies immutability of objects, i.e. no
variable assignment.


>  > Such programs are free of most race conditions, without much
>  > programmer effort to make them so.
> 
> I disagree. They are not free of most race conditions, and it still
> takes a lot of effort. Where did you get this idea from? Have you been
> reading some Java primer that attempts to make it sound easy?
[...]
>> With this mechanism, multi-thread programs with shared data
>> structures can be written with little or no explicit locking by
>> the programmer.  If the restrictions are made a bit stricter,
>> strict enough that threads cannot share mutable unsynchronized data,
>> removal of the "global interpreter lock" is potentially possible.
>> This is a route to improved performance on modern multi-core CPUs.
> 
> Right, this is where I would love to have had more experience with
> Haksell.
> 
> Yes, as soon as you get to a situation where no thread can access shared
> state that is mutable your problems go away, you're also getting no work
> done becasue the threads, whilst they may be performing lots of
> interesting calculations have no way of allowing the rest of the
> program, or operating system, know about it.

I think it is the combination of immutable objects and message passing
(which he omitted mentioning) that is the point. I initially encountered
this principle in Erlang, but it also applies in other languages. For
example in C++, you can create MT-program using std::auto_ptr for
mutable, exclusively owned objects and boost::shared_pointer for
shared, immutable objects.


> So, in response to your point of trying to get an immutable API so that
> Python can easily have multi-threaded programs that do not present race
> conditions I would say the following:
> 
> That is not the challenge, that's the easy part. The challenge is
> getting useful information out of a system that has only been fed
> immutable objects.

You're overly pessimistic. Immutable objects are a tool, not the holy grail.
Having them available can help you solve some problems, not all of them.
You obviously still need mutable data, but if you can restrict access to
them to just one thread, there is no need for synchronisation for them.
Lastly, for the message passing, you also need shared, mutable structures
(queues), so you can't live completely without conventional locking.

Uli

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

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


Re: The future of Python immutability

2009-09-04 Thread Paul Rubin
Ulrich Eckhardt  writes:
> Lastly, for the message passing, you also need shared, mutable structures
> (queues), so you can't live completely without conventional locking.

But that can be completely behind the scenes in the language or
library implementation.  The application programmer doesn't have to
think about those locks.
-- 
http://mail.python.org/mailman/listinfo/python-list


cross platform distribution

2009-09-04 Thread vpr
Hi All

After a couple of experiments, searching around and reading Steve
Holden's lament about bundling and ship python code, I thought I'd
direct this to to the group. I'm using Python 2.6 btw.

I've build a commercial application that I'd like to bundle and ship.
I'd like to protect some of my IP and the py2exe and cx_freeze builds
provide good enough protection for me.

I'd like to provide a build for windows and a build for linux. Windows
ironically has been easier to target and py2exe has given me a nice
build that I can ship between XP, Vista & Server on both 32 and 64
bit.

On linux I've build a build using cx_freeze which works well except
it's not really portable betweem distributions.


I've also been thinking about distributing bytcode versions but things
get tricky quickly.

Can anywone give me some pointers?

Cheers

vpr

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


Re: match braces?

2009-09-04 Thread Steven D'Aprano
On Fri, 04 Sep 2009 00:25:34 -0700, lallous wrote:

> Hello,
> 
> Thank you all for your replies.
> 
> A simple suggestion as Chris' actually might help.
> 
> I am used to two spaces indentation since years, and apparently two
> spaces won't make it clear if no visuals were present (braces, or
> begin/end, ...)

I'm not sure about that.
  A two space indent seems pretty obvious to me.
  But perhaps that's because I'm using a monospaced font.
I suppose if you use a proportional font, spaces will be much narrower, 
and it may be harder to notice the indent.

> Though it is not comfortable to change a style, I will play with 8
> spaces indentation, it would naturally make it clearer ;)

How about trying four spaces first?
Eight spaces is rather a lot, especially once you get to the third
indent level and a third of the page is whitespace.



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


Re: python module for data comparison of 2 MySQL servers

2009-09-04 Thread Kushal Kumaran
On Wed, Sep 2, 2009 at 5:30 AM, <> wrote:
> I have 2 MySQL servers in 2 different data centers.
> Between them, there is data replication setup.
>
> Is there a python tool so I can do data comparison for daily records?
>
> Basically, just access both servers and do a diff in memory and print out
> records.
>

No idea why you need a python tool, but maatkit
(http://www.maatkit.org/) has something similar.

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


Re: obscure problem using elementtree to make xhtml website

2009-09-04 Thread Richard Brodie

"Stefan Behnel"  wrote in message 
news:[email protected]...


>>Not a bug in IE (this time), which is correctly parsing the file as html.
>
> ... which is obviously not the correct thing to do when it's XHTML.

It isn't though; it's HTML with a XHTML DOCTYPE, and the
compatibility rules in Appendix C of the XHTML recommendation apply.
http://www.w3.org/TR/xhtml1/#C_3


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


Multiple inheritance - How to call method_x in InheritedBaseB from method_x in InheritedBaseA?

2009-09-04 Thread The Music Guy
I have a peculiar problem that involves multiple inheritance and method calling.

I have a bunch of classes, one of which is called MyMixin and doesn't
inherit from anything. MyMixin expects that it will be inherited along
with one of several other classes that each define certain
functionality. It defines method_x, which it assumes will also be
defined in the other class that MyMixin ends up getting inherited
with. For example,

class MyMixin(object):
def method_x(self, a, b, c):
...

class BaseA(object):
def method_x(self, a, b, c):
...

class BaseB(object):
def method_x(self, a, b, c):
...

class BaseC(object):
def method_x(self, a, b, c):
...

class FooX(MyMixin, BaseA):
...

class FooY(MyMxin, BaseB):
...

class FooZ(MyMixin, BaseC):
...


This all appears fine at first, but there is a problem: Each Foo's
method_x must call the method_x of MyMixin as well as the method_x of
each respective Foo's second base class. One cannot simply call
FooN.method_x, because that will only call MyMixin.method_x and not
that of the other base.

One might be tempted to amend MyMixin's method_x so that it calls the
parent's method_x before doing anything else:

class MyMixin(object):
def method_x(self, a, b, c):
super(MyMixin, self).method_x(a, b, c)
...

...but of course, that will fail with an AttributeError because
MyMixin's only superclass is object, which does not have a method_x.

The only way I can think to solve the problem would be to implement a
method_x for each Foo that calls the method_x for each of the bases:

class FooX(MyMixin, BaseA):
def method_x(self, a, b, c):
MyMixin.method_x(self, a, b, c)
BaseA.method_x(self, a, b, c)

class FooY(MyMxin, BaseB):
def method_x(self, a, b, c):
MyMixin.method_x(self, a, b, c)
BaseB.method_x(self, a, b, c)

class FooZ(MyMixin, BaseC):
def method_x(self, a, b, c):
MyMixin.method_x(self, a, b, c)
BaseC.method_x(self, a, b, c)


The problem with this solution is that method_x has to be explicitly
created for each Foo, even though they all do just about the same
thing, which kind of defeats the purpose of using multiple inheritance
in this situation. Besides that, I just don't like it!

So, does anyone have an idea about how to remedy this, or at least
work around it?
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Usage of main()

2009-09-04 Thread Jan Kaliszewski

So yes, depending on the nature of your code, its quite conceivable to
find distinct performance differences between code using the __main__
idiom and code without.


But -- it should be emphasized -- it's faster thanks to running code
(an doing name lookups) within a function, and *not* thanks to using
the __main__ idiom (i.e. 'if __name__ == "__main__":' condition).

Cheers,
*j

--
Jan Kaliszewski (zuo) 
--
http://mail.python.org/mailman/listinfo/python-list


Re: Why does this group have so much spam?

2009-09-04 Thread BJ Swope
And I would kindly appreciate it if you fellas wouldn't go solving
this little spam problem!  Selling Anti-Spam industry leading
appliances has managed to put me in a rather nice house and I'd hate
to lose it just because you fellas went and solved the problem! ;)



On Thu, Sep 3, 2009 at 11:24 PM, r wrote:
>
> *ahem*! You guy's do remember this thread (?at one time in history?)
> was about spam on this list, right? Not internet connection fees. ;-)
> --
> http://mail.python.org/mailman/listinfo/python-list
>



-- 
We are all slave to our own paradigm. -- Joshua Williams

If the letters PhD appear after a person's name, that person will
remain outdoors even after it's started raining. -- Jeff Kay
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Usage of main()

2009-09-04 Thread Jan Kaliszewski

04-09-2009 o 08:37:43 r  wrote:


Why use a nested function when you already *in* main?


I understand you name global scope as 'main'. But (independently
of using the __main__ idiom and so on) it is still good idea not to
place to much code in the global scope but to place your app-logic
code in functions -- because, as we noted:

* in practice it is considerably faster,

* it helps you with using functions & class browsers.

Cheers,
*j

--
Jan Kaliszewski (zuo) 
--
http://mail.python.org/mailman/listinfo/python-list


Re: The future of Python immutability

2009-09-04 Thread Hendrik van Rooyen
On Thursday 03 September 2009 21:07:21 Nigel Rantor wrote:

> That is not the challenge, that's the easy part. The challenge is
> getting useful information out of a system that has only been fed
> immutable objects.

Is it really that difficult?  (completely speculative):

class MyAnswer(object):
def __init__(self)
self.finished = False
self.Answer = None
self.collected = False

Then when you start a thread, you give it an instance:

ans = MyAnswer()
list_of_answers.append(c)

worker_thread = thread.start_new_thread(worker, (ans, immutable_stuff ))

def worker(ans):
ans.Answer = do_some_stuff()
ans.finished = True

Then to see if everybody is finished:

runbool = True
While runbool:
finished = False
runbool = False
for answer in list_of_answers:
if answer.finished and not answer.collected:
do_something(answer.Answer)
answer.collected = True
else:
runbool = True

This scheme gives only one thread the license to make a change to the answer 
instance, so how can it go wrong?

You can also extend it for long running threads by playing ping pong with the 
two booleans -  the collector sets the collected boolean and clears the 
finished, and the worker must clear the collected boolean before starting a 
new cycle. ( the worker waits for not finished, then clears collected)

I do similar stuff in assembler and I have no problems - Am I missing 
something subtle?

Of course - working with immutable objects only is a bit like working with 
read only memory, or worse, write only memory  - not easy.

- Hendrik


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


Re: python module for data comparison of 2 MySQL servers

2009-09-04 Thread Tino Wildenhain

Hi,

Am 02.09.2009 02:00, schrieb none:

I have 2 MySQL servers in 2 different data centers.
Between them, there is data replication setup.

Is there a python tool so I can do data comparison for daily records?


Why should the data differ and the replication not detect and correct
it? I frequently hear that MySQL has such a sophisticated replication...


Basically, just access both servers and do a diff in memory and print
out records.


I'd not do this in memory since any database of resonably size can
quickly have more then your work mem data in a table and in this
case you would have it twice.

You could start with connections to both databases
and select * from table order by [primary keys or all columns]
and then use difflib.SequenceMatcher on both cursors
(they should be iterable)

keep in mind that depending on your replication type
the databases could differ by the exact time you start your
comparison.

Regards
Tino



smime.p7s
Description: S/MIME Cryptographic Signature
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: obscure problem using elementtree to make xhtml website

2009-09-04 Thread Stefan Behnel
Richard Brodie wrote:
> "Stefan Behnel" wrote:
>> Lee wrote:
>>> Not a bug in IE (this time), which is correctly parsing the file as html.
>> ... which is obviously not the correct thing to do when it's XHTML.
> 
> It isn't though; it's HTML with a XHTML DOCTYPE

Not the page I look at (i.e. the link provided by the OP). It clearly has
an XHTML namespace, so it's X(HT)ML, not HTML.

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


logger module : Question about log message format

2009-09-04 Thread jorma kala
Hi,
I've created a logger like this:


LOG_FILENAME = 'test.txt'
fh=logging.FileHandler(LOG_FILENAME,'w')
logger1 = logging.getLogger('myLogger1')
logger1.addHandler(fh)
logger1.setLevel(logging.INFO)
logger1.info('message from logger1')

and was hoping to get log messages in this format in my log file:

:INFO:myLogger1:message from logger1

instead I just get a plain message like this:

message from logger1

Do you know why?

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


Re: Usage of main()

2009-09-04 Thread Mel
Manuel Graune wrote:
[ ... ]
> thanks for your answer. What you are explaining is exactly why I tried
> it in the first place. I'm just wondering why (this is my impression,
> not necessaryly the reallity) none of the recommended texts on python
> put this in the first chapters. Instead - if it is mentioned at all -
> it is hidden somewhere in the "advanced" sections. Even if the reason
> for this is (I'm guessing...) because it is thought to be to complicated
> to explain the "why" right at the beginning, it probably would not hurt
> to just tell that this is the "correct" way of doing things right at the
> start and add a footnote.

Maybe it's the "correct" way, but it isn't *the* correct way.  In my 
experience, when I import a program, it isn't because I want to run it 
straight through.  For that there's `exec`, and subprocess and what not.  
More likely I want to get at the internals -- maybe produce my own output 
from the intermediate results, for example.  For that, a monolithic `main` 
function is beside the point.

For a sizeable program you can get the same speed advantage, to within five 
9s or so, with a structure like

## ...
if __name__ == '__main__':
process_this_input()
process_that_input()
mess_with_the_collected_data()
write_the_output()



Mel.

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


Invitation to connect on LinkedIn

2009-09-04 Thread Navneet Khanna
LinkedIn


Navneet Khanna requested to add you as a connection on LinkedIn:
--

Jaime,

I'd like to add you to my professional network on LinkedIn.

- Navneet

Accept invitation from Navneet Khanna
http://www.linkedin.com/e/I2LlXdLlWUhFABKmxVOlgGLlWUhFAfhMPPF/blk/I309673816_3/6lColZJrmZznQNdhjRQnOpBtn9QfmhBt71BoSd1p65Lr6lOfPdvdz4UcPsSej0PiiZxk6VSk6VAbiYRdj8Nd3sTcjsLrCBxbOYWrSlI/EML_comm_afe/

View invitation from Navneet Khanna
http://www.linkedin.com/e/I2LlXdLlWUhFABKmxVOlgGLlWUhFAfhMPPF/blk/I309673816_3/0PnPoNe3cTdzAMcQALqnpPbOYWrSlI/svi/
 

--
DID YOU KNOW you can use your LinkedIn profile as your website? Select a vanity 
URL and then promote this address on your business cards, email signatures, 
website, etc
http://www.linkedin.com/e/ewp/inv-21/


 
--
(c) 2009, LinkedIn Corporation

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


Python SSH interface

2009-09-04 Thread Mag Gam
Is there something similar to NetSSH
(http://search.cpan.org/dist/Net-SSH-Perl/) for python?
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Python SSH interface

2009-09-04 Thread Avell Diroll

Mag Gam wrote:

Is there something similar to NetSSH
(http://search.cpan.org/dist/Net-SSH-Perl/) for python?



I don't know much about perl modules functionalities, but paramiko might 
be what you are searching for.

http://www.lag.net/paramiko/

Cheers

Ju
--
Those who do not understand Unix are condemned to reinvent it, poorly.
-Henry Spencer
--
http://mail.python.org/mailman/listinfo/python-list


Re: Python SSH interface

2009-09-04 Thread Diez B. Roggisch
Mag Gam wrote:

> Is there something similar to NetSSH
> (http://search.cpan.org/dist/Net-SSH-Perl/) for python?

Google dead today? From the > 3.000.000 answers for python + ssh, I suggest
paramiko, but there are more options.

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


File Handling Problem

2009-09-04 Thread joy99
Dear Group,

I have a file. The file has multiple lines. I want to get the line
number of any one of the strings.
Once I get that I like to increment the line number and see the string
of the immediate next line or any following line as output. The
problem as I see is nicely handled in list,

like list_one=["god","earth","sky","cloud","rain"]
  list_one[0]="god"
  list_one[2]="sky"

but can it be for string and file?

I can generally put a cursor and move it back and forth for my need
using index/find and then re. But, I was looking to use some built-in
methods. Can I use fileinput or ast? But, I was not finding any good
resources, using http://docs.python.org/library/fileinput.html#fileinput.input,
etc. did not help much.

If any one can kindly help me out.

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


Re: recursive decorator

2009-09-04 Thread Michele Simionato
On Sep 3, 6:41 pm, Ethan Furman  wrote:
>
> The original thread by Bearophile:
>    http://mail.python.org/pipermail/python-list/2009-May/711848.html

I have read the thread. What Bearophile wants can be implemented with
a bytecode hack, no
need for the decorator module. Let me call 'recur' the self-function,
like in Clojure.
You can define a decorator that makes "self-conscious" a recursive
function as follows:

# requires byteplay by Noam Raphael
# see http://byteplay.googlecode.com/svn/trunk/byteplay.py
from byteplay import Code, LOAD_GLOBAL, STORE_FAST, LOAD_FAST

def enable_recur(f):
print f.func_code.co_names
if 'recur' not in f.func_code.co_names:
return f # do nothing on non-recursive functions
c = Code.from_code(f.func_code)
c.code[1:1] = [(LOAD_GLOBAL, f.__name__), (STORE_FAST, 'recur')]
for i, (opcode, value) in enumerate(c.code[2:]):
if opcode == LOAD_GLOBAL and value == 'recur':
c.code[i+2] = (LOAD_FAST, 'recur')
f.func_code = c.to_code()
return f

## example of use

@enable_recur
def f(x):
if x == 1:
return 1
else:
return x*recur(x-1)

print f(4) # =>24


Please accept this without explanation since it would take me a lot of
time
to explain how it works. Just accept that bytecode hacks are
incredible
(and nonportable too) ;-)

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


Re: Usage of main()

2009-09-04 Thread Ben Finney
"Jan Kaliszewski"  writes:

> I understand you name global scope as 'main'. But (independently of
> using the __main__ idiom and so on) it is still good idea not to place
> to much code in the global scope but to place your app-logic code in
> functions -- because, as we noted:
>
> * in practice it is considerably faster,
>
> * it helps you with using functions & class browsers.

* having a module that can be imported without side effects helps select
  pieces of the module's functionality

* any module should be importable without side effects to make it easier
  to run unit tests for that module

-- 
 \“The number of UNIX installations has grown to 10, with more |
  `\ expected.” —Unix Programmer's Manual, 2nd Ed., 1972-06-12 |
_o__)  |
Ben Finney
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: possible attribute-oriented class

2009-09-04 Thread Jan Kaliszewski

[originally from [email protected],
 crossposted to [email protected]]

04-09-2009 o 00:46:01 Ken Newton  wrote:


I have created the following class definition with the idea of making
a clean syntax for non-programmers to created structured data within a
python environment.

I would appreciate comments on this code. First, is something like
this already done? Second, are there reasons for not doing this?  If
this seems OK, how could I clean up the string conversion to have
indented format.

The expected use would have all items in the structure be simple
python types or AttrClass types. Code written in python could walk the
structure in a simple way to locate any desired values. Code in a
C/C++ extension should also be able to walk the structure to use any
value in the structure.

class AttrClass(object):
"""AttrClass lets you freely add attributes in nested manner"""

def __init__(self):
pass
def __setitem__(self, key, value):
return self.__dict__.__setitem__(key, value)
def __repr__(self):
return "%s(%s)" % (self.__class__.__name__,  
self.__dict__.__repr__())

def __str__(self):
ll = ['{']
for k,v in self.__dict__.iteritems():
ll.append("%s : %s" % (k, str(v)))
return '\n'.join(ll) + '}'

[snip]

I find the idea interesting and close to my own needs in many
situations, if I could alter it a bit.

Of course, we always can use an empty class ('class MyStruct: pass')
or simply use a dict... But both methods are inconvinient in some
ways.

In the case of dict we are convicted -- even when we need static
access -- to mapping notation (obj['member']) which is less
convenient and (what's more important) more error-prone than
attribute dot-notation.

In the case of empty class/object we can use convenient attr
dot-notation but dynamic access is less natural...

IMHO there could be -- in collections module or even as a built-in
factory function -- something (somehow) similar to namedtuple, but
mutable and more dict-like. I'am less focused on nesting such
structures, and more on making it a namespace-like objects with
convenience-and-today-usage features. Please consider the code:


  class AttrDict(dict):  # (or maybe from OrderedDict)
  "It's only a model. (Shhh!)"

  def __getattr__(self, name):
  if name.startswith('_'):
  raise AttributeError("AttrDict's key can't "
   "start with underscore")
  else:
  return self[name]

  def __setattr__(self, name, value):
  self[name] = value

  def __delattr__(self, name):
  del self[name]

  def __repr__(self):
  return '{0}({1})'.format(self.__class__.__name__,
 dict.__repr__(self))
  def __str__(self):
  return self._as_str()

  def _gen_format(self, indwidth, indstate):
  indst = indstate * ' '
  ind = (indstate + indwidth) * ' '
  yield ('\n' + indst + '{' if indstate else '{')
  for key, val in self.items():
  valstr = (str(val) if not isinstance(val, AttrDict)
else val._as_str(indwidth, indstate + indwidth))
  yield '{ind}{key}: {valstr}'.format(ind=ind, key=key,
  valstr=valstr)
  yield indst + '}'

  def _as_str(self, indwidth=4, indstate=0):
  return '\n'.join(self._gen_format(indwidth, indstate))

  def _as_dict(self):
  return dict.copy(self)


  # Test code:
  if __name__ == '__main__':
  struct = AttrDict()
  struct.first = 1
  struct.second = 2.0
  struct.third = '3rd'
  struct.fourth = [4]
  print(struct)
  # output:
  # {
  # 'second': 2.0
  # 'fourth': [4]
  # 'third': '3rd'
  # 'first': 1
  # }

  del struct.fourth

  print(repr(struct))
  # output:
  # AttrDict({'second': 2.0, 'third': '3rd', 'first': 1})

  print(struct.first)  # (static access)
  # output:
  # 1

  for x in ('first', 'second', 'third'):
  print(struct[x])  # (dynamic access)
  # output:
  # 1
  # 2.0
  # 3rd

  struct.sub = AttrDict(a=1, b=2, c=89)
  print(struct._as_dict())
  # output:
  # {'second': 2.0, 'sub': AttrDict({'a': 1, 'c': 89, 'b': 2}),\
  #  'third': '3rd', 'first': 1}

  print(struct._as_str(8))
  # output:
  # {
  # second: 2.0
  # sub:
  # {
  # a: 1
  # c: 89
  # b: 2
  # }
  # third: 3rd
  # first: 1
  # }


What do you think about it?

Cheers,
*j

--
Jan Kaliszewski (zuo) 
--
http://mail.python.org/mailman/listinfo/python-list


Re: cross platform distribution

2009-09-04 Thread Philip Semanchuk


On Sep 4, 2009, at 4:44 AM, vpr wrote:


Hi All

After a couple of experiments, searching around and reading Steve
Holden's lament about bundling and ship python code, I thought I'd
direct this to to the group. I'm using Python 2.6 btw.

I've build a commercial application that I'd like to bundle and ship.
I'd like to protect some of my IP and the py2exe and cx_freeze builds
provide good enough protection for me.

I'd like to provide a build for windows and a build for linux. Windows
ironically has been easier to target and py2exe has given me a nice
build that I can ship between XP, Vista & Server on both 32 and 64
bit.

On linux I've build a build using cx_freeze which works well except
it's not really portable betweem distributions.


I've also been thinking about distributing bytcode versions but things
get tricky quickly.

Can anywone give me some pointers?


I don't know how much "critical" code you have, but you might want to  
look at Cython which will translate your Python into C with little  
change to your Python source. Of course, compiled C code can still be  
disassembled, but it's harder than Python bytecode.


HTH
P


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


Re: Usage of main()

2009-09-04 Thread Albert Hopkins
On Fri, 2009-09-04 at 22:55 +1000, Ben Finney wrote:
> * having a module that can be imported without side effects helps
> select
>   pieces of the module's functionality
> 
> * any module should be importable without side effects to make it
> easier
>   to run unit tests for that module
> 

+1

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


Re: cross platform distribution

2009-09-04 Thread vpr
On Sep 4, 3:19 pm, Philip Semanchuk  wrote:
> On Sep 4, 2009, at 4:44 AM, vpr wrote:
>
>
>
> > Hi All
>
> > After a couple of experiments, searching around and reading Steve
> > Holden's lament about bundling and ship python code, I thought I'd
> > direct this to to the group. I'm using Python 2.6 btw.
>
> > I've build a commercial application that I'd like to bundle and ship.
> > I'd like to protect some of my IP and the py2exe and cx_freeze builds
> > provide good enough protection for me.
>
> > I'd like to provide a build for windows and a build for linux. Windows
> > ironically has been easier to target and py2exe has given me a nice
> > build that I can ship between XP, Vista & Server on both 32 and 64
> > bit.
>
> > On linux I've build a build using cx_freeze which works well except
> > it's not really portable betweem distributions.
>
> > I've also been thinking about distributing bytcode versions but things
> > get tricky quickly.
>
> > Can anywone give me some pointers?
>
> I don't know how much "critical" code you have, but you might want to  
> look at Cython which will translate your Python into C with little  
> change to your Python source. Of course, compiled C code can still be  
> disassembled, but it's harder than Python bytecode.
>
> HTH
> P

Hi Peter

Sounds like a plan, how portable will that be between Linux systems?
Won't I run into some GLIBC problems?
Can you force it to statically link the binary?

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


Re: Video?

2009-09-04 Thread David C . Ullrich
On Thu, 03 Sep 2009 23:04:45 +0200, Avell Diroll
 wrote:

>David C Ullrich wrote:
>...
>> Is that correct? If so is there some other standard Python
>> windowing kit that does include some sort of video functionality?
>> 
>> (Talking Ubuntu Linux if it matters.)
>
>I don't know about video and wxpython, but gstreamer has some python 
>bindings (python-gst0.10 on jaunty).
>You may find a nice intro here:
>http://pygstdocs.berlios.de/

Keen, thanks. Glancing at that page it looks like it should do
just fine. Even comes with several pygtk examples, which one
could presumably use or tweak and use or adapt to wxpython.

Can't believe the guy who posted the "FAQ" there complaining
that the docs suck. Someone should send him a refumd of the money he
paid.

>
>Hope this helped

Not sure yet, but seems pretty likely.

>Ju

David C. Ullrich

"Understanding Godel isn't about following his formal proof. 
That would make a mockery of everything Godel was up to."
(John Jones, "My talk about Godel to the post-grads."
in sci.logic.)
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: [Guppy-pe-list] An iteration idiom (Was: Re: loading files containing multiple dumps)

2009-09-04 Thread Sverker Nilsson
On Thu, 2009-09-03 at 10:05 +0100, Chris Withers wrote:
> Raymond Hettinger wrote:
> > In the first case, you would write:
> >sets.extend(h.load(f))
> 
> yes, what I had was:
> 
> for s in iter(h.load(f)): sets.append(s)
> 
> ...which I mistakenly thought was working, but in in fact boils down to 
> Raymond's code.
> 
> The problem is that each item that h.load(f) returns *is* actually an 
> iterable, so either of the above just ends up the contents of each set 
> being extended onto `sets` rather than the sets themselved.

Yes that is what makes it confusing, otherwise you would get an
exception.

I hope the new loadall method as I wrote about before will resolve this.

def loadall(self,f):
''' Generates all objects from an open file f or a file named f'''
if isinstance(f,basestring):
f=open(f)
while True:
yield self.load(f)

Should we call it loadall? It is a generator so it doesn't really load
all immedietally, just lazily. Maybe call it iload? Or redefine load,
but that might break existing code so would not be good.

> It's all really rather confusing, apologies if there's interspersed rant 
> in here:
> 
>  >>> from guppy import hpy
>  >>> h = hpy()
> 
> Minor rant, why do I have to instantiate a
> 
> to do anything with heapy?
> Why doesn't heapy just expose load, dump, etc?

Basically, the need for the h=hpy() idiom is to avoid any global
variables. Heapy uses some rather big internal data structures, to cache
such things as dict ownership. I didn't want to have all those things in
global variables. Now they are all contained in the hpy() session
context. So you can get rid of them by just deleting h if h=hpy(), and
the other objects you created. Also, it allows for several parallel
invocations of Heapy.

However, I am aware of the extra initial overhead to do h=hpy(). I
discussed this in my thesis. "Section 4.7.8 Why not importing Use
directly?" page 36, 

http://guppy-pe.sourceforge.net/heapy-thesis.pdf

Maybe a module should be added that does this, especially if someone
provides a patch and/or others agree :-)


> (oh, and reading the code for guppy.heapy.Use and its ilk made me go 
> temporarily blind!) ;-)

Try sunglasses:) (Well, I am aware of this, it was a
research/experimental system and could have some refactoring :-)

>  >>> f = open('copy.hpy')
>  >>> s = h.load(f)
> 
> Less minor rant: this applies to most things to do with heapy... Having 
> __repr__ return the same as __str__ and having that be a long lump of 
> text is rather annoying. If you really must, make __str__ return the big 
> lump of text but have __repr__ return a simple, short, item containing 
> the class, the id, and maybe the number of contained objects...

I thought it was cool to not have to use print but get the result
directly at the prompt.

But if this is a problem and especially if others also complain, we
could add an option for shorter __repr__.

h=hpy(short_repr=True)

Or something else/shorter if you wish.

BTW, I think a cool thing with having everything based on a context
session, h=hpy(args), is that you could add any options there. That
would be harder/less clean if you just imported all methods from a
module. Patch some module-level variable ilk ...

> Anyway...
> 
>  >>> id(s)
> 13905272
>  >>> len(s)
> 192
>  >>> s.__class__
> 
>  >>> i = s[0]
>  >>> id(i)
> 13904112
>  >>> len(i)
> 1
>  >>> i.__class__
> 
> 
> Hmmm, I'm sure there's a good reason why an item in a set has the exact 
> same class and iterface as a whole set?

Um, perhaps no very good reason but... a subset of a set is still a set,
isn't it? This is the same structure that is used in IdentitySet
objects. Each row is still an IdentitySet, and has the same attributes.
This is also like Python strings work, there is no special character
type, a character is just a string of length 1. I thought this was
pretty cool when I first saw it in Python, compared to other languages
as C or Pascal. If we don't need a new type, we could better avoid it.

So what's the problem? :-)

> It feels like some kind of filtering, where are the docs that explain 
> all this?

Unfortunately, the docs for the Stat object have been lagging behind.
Sorry. But as people gain more interest for Heapy and send comments or
even patches, I get more motivated to look into it. :-)

Thanks and Cheers,

Sverker

-- 
Expertise in Linux, embedded systems, image processing, C, Python...
http://sncs.se


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


Re: Video?

2009-09-04 Thread David C . Ullrich
On Thu, 3 Sep 2009 16:24:44 -0700 (PDT), Che M 
wrote:

>On Sep 3, 4:11 pm, David C Ullrich  wrote:
>> Not at all important, just for fun (at least for me):
>>
>> It seems to me, looking at various docs, that wxWidgets
>> includes a "media control" that can play video files, but
>> it's not included in wxPython. (There's something in
>> wxPython with a promising name but it seems to be just audio.)
>
>It is included with wxPython.  

Sure enough - the docs I had must be out of date.

>But if you look in the overview
>in the demo, it it says:
>
>"wx.MediaCtrl is not currently available on unix systems."
>
>So there's your problem, I think.  On Win, it plays a video in
>the demo.

Oh well. 




David C. Ullrich

"Understanding Godel isn't about following his formal proof. 
That would make a mockery of everything Godel was up to."
(John Jones, "My talk about Godel to the post-grads."
in sci.logic.)
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: cross platform distribution

2009-09-04 Thread Philip Semanchuk


On Sep 4, 2009, at 9:24 AM, vpr wrote:


On Sep 4, 3:19 pm, Philip Semanchuk  wrote:

On Sep 4, 2009, at 4:44 AM, vpr wrote:




Hi All



After a couple of experiments, searching around and reading Steve
Holden's lament about bundling and ship python code, I thought I'd
direct this to to the group. I'm using Python 2.6 btw.


I've build a commercial application that I'd like to bundle and  
ship.
I'd like to protect some of my IP and the py2exe and cx_freeze  
builds

provide good enough protection for me.


I'd like to provide a build for windows and a build for linux.  
Windows

ironically has been easier to target and py2exe has given me a nice
build that I can ship between XP, Vista & Server on both 32 and 64
bit.



On linux I've build a build using cx_freeze which works well except
it's not really portable betweem distributions.


I've also been thinking about distributing bytcode versions but  
things

get tricky quickly.



Can anywone give me some pointers?


I don't know how much "critical" code you have, but you might want to
look at Cython which will translate your Python into C with little
change to your Python source. Of course, compiled C code can still be
disassembled, but it's harder than Python bytecode.

HTH
P


Hi Peter


It's Philip, actually. =)



Sounds like a plan, how portable will that be between Linux systems?


Very portable, but I should have mentioned that it requires you to  
distribute a C file that's compiled on the user's machine. That's easy  
to do via distutils but it adds a requirement to your app.



Won't I run into some GLIBC problems?
Can you force it to statically link the binary?


I don't know the answer to those questions, but it's just a regular C  
file, albeit one that's autogenerated. It comes with all of the pros  
and cons of a C file you'd written yourself.


Good luck
Philip




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


Re: using queue

2009-09-04 Thread MRAB

Tim Arnold wrote:
"Jan Kaliszewski"  wrote in message 
news:[email protected]...

06:49:13 Scott David Daniels  wrote:


Tim Arnold wrote:
(1) what's wrong with having each chapter in a separate thread? Too 
much going on for a single processor?

Many more threads than cores and you spend a lot of your CPU switching
tasks.

In fact, python threads work relatively the best with a powerful single
core; with more cores it becomes being suprisingly inefficient.

The culprit is Pythn GIL and the way it [mis]cooperates with OS
scheduling.

See: http://www.dabeaz.com/python/GIL.pdf

Yo
*j

--
Jan Kaliszewski (zuo) 


I've read about the GIL (I think I understand the problem there)--thanks. In 
my example, the actual job called for each chapter ended up being a call to 
subprocess (that called a different python program). I figured that would 
save me from the GIL problems since each subprocess would have its own GIL.


In the words of Tom Waits, " the world just keeps getting bigger when you 
get out on your own".  So I'm re-reading now, and maybe what I've been doing 
would have been better served by the multiprocessing package.


I'm running python2.6 on FreeBSD with a dual quadcore cpu. Now my questions 
are:
(1) what the heck should I be doing to get concurrent builds of the 
chapters, wait for them all to finish, and pick up processing the main job 
again? The separate chapter builds have no need for communication--they're 
autonomous.

(2) using threads with the target fn calling subprocess, a bad idea?
(3) should I study up on multiprocessing package and/or pprocessing?

thanks for your inputs,


You could adapt the threading solution I gave to multiprocessing; just
use the multiprocessing queue class instead of the threading queue
class, etc.
--
http://mail.python.org/mailman/listinfo/python-list


Re: [Guppy-pe-list] An iteration idiom (Was: Re: loading files containing multiple dumps)

2009-09-04 Thread Sverker Nilsson
On Fri, 2009-09-04 at 15:25 +0200, Sverker Nilsson wrote:

> 
> However, I am aware of the extra initial overhead to do h=hpy(). I
> discussed this in my thesis. "Section 4.7.8 Why not importing Use
> directly?" page 36, 
> 
> http://guppy-pe.sourceforge.net/heapy-thesis.pdf

Actually it is described in "4.7.4 Why session context - why not global
variables?" p. 33.

Sorry for the double post. I never seem to get things right the first
time (or so on) :-/

Sverker



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


Re: The future of Python immutability

2009-09-04 Thread Adam Skutt
On Sep 3, 2:03 pm, John Nagle  wrote:
>      Suppose, for discussion purposes, we had general "immutable objects".
> Objects inherited from "immutableobject" instead of "object" would be
> unchangeable once "__init__" had returned.  Where does this take us?

You can create this in various ways through metaclasses.  I've done
it, mostly because I was curious to see how hard it would be and if it
actually gained me anything useful.

>      With this mechanism, multi-thread programs with shared data
> structures can be written with little or no explicit locking by
> the programmer.  If the restrictions are made a bit stricter,
> strict enough that threads cannot share mutable unsynchronized data,
> removal of the "global interpreter lock" is potentially possible.
> This is a route to improved performance on modern multi-core CPUs.
Nope, preventing mutation of the objects themselves is not enough.
You also have to forbid variables from being rebound (pointed at
another object).  Consider this simple example:

-- Thread 1 -- | -- Thread 2 --
a = "Foo"
spawn Thread 2
a = "Bar"print "thread 2: %s" % a
print "thread 1: %s" % a

You could see (ignoring the fact the output isn't ordered):
"thread 1: Bar"
"thread 2: Foo"
or:
"thread 1: Bar"
"thread 2: Bar"

so the fact "Foo" and "Bar" are immutable isn't enough to solve the
problem.  The variables themselves, since they obey pointer semantics,
must also be forbidden from being reseated (i.e., they must be
references in the C++ sense or become 'T const * const' pointers).

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


Re: python daemon - compress data and load data into MySQL by pyodbc

2009-09-04 Thread MacRules

Dennis Lee Bieber wrote:

On Thu, 03 Sep 2009 14:43:40 -0400, MacRules 
declaimed the following in gmane.comp.python.general:


Oracle DB in data center 1 (LA, west coast)
MSSQL DB in data center 2 (DC, east coast)


Note that your thread subject line states MySQL... There is a big
difference between MySQL and M$ SQL-Server.

So network bandwidth is an issue, I prefer to have gzip fist and deliver 
the data.


I need 2 python daemons or a web service here in the future.
I will enter the Oracle table name, user id and password.
So the task is dump out Oracle data (Linux) and insert that to MSSQL.


One: daemon implies something running in the background,
stand-alone, and not associated with any "user" type account.  IOW,
something like a server program...

Do you really intend to create a server application that sits around
waiting for anyone to connect to it and send a properly formatted
request? What safeguards do you intend to restrict this daemon from
responding to hackers (after all, you're going to have to have an open
port to accept requests from outside).

Second: said daemon will have to be running ON the end with the
Oracle DBMS (or, at least, within its LAN which presumably has higher
bandwidth than the long distance connection).
 
I can try first with 1 daemon python. Take the Oracle data file, and let 
the daemon connects to MSSQL (with pyodbc) and load the data in.


I see three operations here:

1   dumping data from Oracle
2   transferring data from LA to DC
3   importing data to whatever DBMS you are really using.

I believe, in a later post, you mention this is a one-time task. If
so, WHY worry about network bandwidth... Just let it run overnight...

Question: does the Oracle server permit remote connections?
(dangerous unless behind a VPN type connection)

If it does, I'd suggest the initial program should just open a
connection both the Oracle server, and the local server. Request the
contents of whatever needs to be transferred, collect the data, massage
it into whatever format is needed for the local, and insert it to the
local... Anything else will require being able to log into the remote
machine and running programs ON it. In which case you could just FTP the
data from one end to the other (assuming there is an FTP server active
on one or the other end -- or a web server address you can stuff the
files into and use HTTP to fetch them)


I have 1000 servers, and 1 time each, so it is 1000 times.
What I try to say, I do not need real-time push/pull here.

I know the manual operations, just want Python does automatically for me.

I like to see good examples on how to code daemon, signal, maybe 
multi-threading.


I wrote something like that in C way back. And good at shell scripts.
--
http://mail.python.org/mailman/listinfo/python-list


How to access ODBC databases ?

2009-09-04 Thread Timothy Madden

Hello

I would like to use a database through ODCB in my python application. I 
have Slackware Linux, but I would not mind a portable solution, since 
python runs on both Unixes and Windows.


I would like a free/open-source solution and the python module for ODBC 
access that I have found is *pyodbc*, but the problem is it fails to 
connect to my database with an error like:


>>> conn = pyodbc.connect('DRIVER={PostgreSQL 
Unicode};Servername=127.0.0.1;UID=pikantBlue;Database=pikantBlue')

Traceback (most recent call last):
  File "", line 1, in 
pyodbc.Error: ('0', '[0] [nxDC (202) (SQLDriverConnectW)')


Do you know what the problem is ? My database is working and I can 
connect with *psql -U pikantBlue*, and also my ODBC installation is 
working and I can connect with *isql pikantBlue*.


From python I can also connect with the *pyodb* module, which is 
another module for ODBC access, but pyodb does not have query parameters 
and all
the functions in the DB api, it is only meant the be a simple way to 
acccess ODBC.


Do you know why my *pyodbc* module would not connect, when others will ?
Do you know other modules to use ODBC in python ?

Thank you
Timothy Madden
--
http://mail.python.org/mailman/listinfo/python-list


Re: File Handling Problem

2009-09-04 Thread Lucas Prado Melo
On Fri, Sep 4, 2009 at 9:50 AM, joy99  wrote:

> Dear Group,
>
> I have a file. The file has multiple lines. I want to get the line
> number of any one of the strings.
> Once I get that I like to increment the line number and see the string
> of the immediate next line or any following line as output. The
> problem as I see is nicely handled in list,
>

You could just grab each line and associate that line's content with its
line numbers, like this:
f = open("my_file.txt", "r")
line2pos = {}
for line_num, line in enumerate(f.readlines()):
if line not in line2pos:
line2pos[line] = []
line2pos[line].append(line_num)
(...)

This approach would be nice when you don't know which string to look for
beforehand.
In the case you already know it, there's no need for keeping the line2pos
variable, the right approach should be to iterate through the lines of the
file and, when you see the string you need, just display the next line and
exit:
def lookNextLineAfterString(s)
 f = open("my_file.txt", "r")
 found = False
 while True:
 line = f.readline()
 if line == '':
  break
 line = line[:-1] #stripping the newline
 if line == s:
 found = True
 break
 if found == True:
 nextString = f.readline()
 if nextString == '':
  print "String found but there's no next string"
 else:
  print "Next string is ", nextString[:-1]
 else:
 print "String not found"
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: possible attribute-oriented class

2009-09-04 Thread Colin J. Williams

Jan Kaliszewski wrote:

[originally from [email protected],
 crossposted to [email protected]]

04-09-2009 o 00:46:01 Ken Newton  wrote:


I have created the following class definition with the idea of making
a clean syntax for non-programmers to created structured data within a
python environment.

I would appreciate comments on this code. First, is something like
this already done? Second, are there reasons for not doing this?  If
this seems OK, how could I clean up the string conversion to have
indented format.

The expected use would have all items in the structure be simple
python types or AttrClass types. Code written in python could walk the
structure in a simple way to locate any desired values. Code in a
C/C++ extension should also be able to walk the structure to use any
value in the structure.

class AttrClass(object):
"""AttrClass lets you freely add attributes in nested manner"""

def __init__(self):
pass
def __setitem__(self, key, value):
return self.__dict__.__setitem__(key, value)
def __repr__(self):
return "%s(%s)" % (self.__class__.__name__, 
self.__dict__.__repr__())

def __str__(self):
ll = ['{']
for k,v in self.__dict__.iteritems():
ll.append("%s : %s" % (k, str(v)))
return '\n'.join(ll) + '}'

[snip]

I find the idea interesting and close to my own needs in many
situations, if I could alter it a bit.

Of course, we always can use an empty class ('class MyStruct: pass')
or simply use a dict... But both methods are inconvinient in some
ways.

In the case of dict we are convicted -- even when we need static
access -- to mapping notation (obj['member']) which is less
convenient and (what's more important) more error-prone than
attribute dot-notation.

In the case of empty class/object we can use convenient attr
dot-notation but dynamic access is less natural...

IMHO there could be -- in collections module or even as a built-in
factory function -- something (somehow) similar to namedtuple, but
mutable and more dict-like. I'am less focused on nesting such
structures, and more on making it a namespace-like objects with
convenience-and-today-usage features. Please consider the code:


  class AttrDict(dict):  # (or maybe from OrderedDict)
  "It's only a model. (Shhh!)"

  def __getattr__(self, name):
  if name.startswith('_'):
  raise AttributeError("AttrDict's key can't "
   "start with underscore")
  else:
  return self[name]

  def __setattr__(self, name, value):
  self[name] = value

  def __delattr__(self, name):
  del self[name]

  def __repr__(self):
  return '{0}({1})'.format(self.__class__.__name__,
 dict.__repr__(self))
  def __str__(self):
  return self._as_str()

  def _gen_format(self, indwidth, indstate):
  indst = indstate * ' '
  ind = (indstate + indwidth) * ' '
  yield ('\n' + indst + '{' if indstate else '{')
  for key, val in self.items():
  valstr = (str(val) if not isinstance(val, AttrDict)
else val._as_str(indwidth, indstate + indwidth))
  yield '{ind}{key}: {valstr}'.format(ind=ind, key=key,
  valstr=valstr)
  yield indst + '}'

  def _as_str(self, indwidth=4, indstate=0):
  return '\n'.join(self._gen_format(indwidth, indstate))

  def _as_dict(self):
  return dict.copy(self)


  # Test code:
  if __name__ == '__main__':
  struct = AttrDict()
  struct.first = 1
  struct.second = 2.0
  struct.third = '3rd'
  struct.fourth = [4]
  print(struct)
  # output:
  # {
  # 'second': 2.0
  # 'fourth': [4]
  # 'third': '3rd'
  # 'first': 1
  # }

  del struct.fourth

  print(repr(struct))
  # output:
  # AttrDict({'second': 2.0, 'third': '3rd', 'first': 1})

  print(struct.first)  # (static access)
  # output:
  # 1

  for x in ('first', 'second', 'third'):
  print(struct[x])  # (dynamic access)
  # output:
  # 1
  # 2.0
  # 3rd

  struct.sub = AttrDict(a=1, b=2, c=89)
  print(struct._as_dict())
  # output:
  # {'second': 2.0, 'sub': AttrDict({'a': 1, 'c': 89, 'b': 2}),\
  #  'third': '3rd', 'first': 1}

  print(struct._as_str(8))
  # output:
  # {
  # second: 2.0
  # sub:
  # {
  # a: 1
  # c: 89
  # b: 2
  # }
  # third: 3rd
  # first: 1
  # }


What do you think about it?

Cheers,
*j

I like both suggestions.  The dot notation is simpler than the dictionary one, 
in may cases.


struct is perhaps a name to avoid, as it is a standard module.

The 

Re: The future of Python immutability

2009-09-04 Thread Scott David Daniels

John Nagle wrote:

... Suppose, for discussion purposes, we had general "immutable objects".
Objects inherited from "immutableobject" instead of "object" would be
unchangeable once "__init__" had returned.  Where does this take us?


Traditionally in Python we make that, "once __new__ had returned."

--Scott David Daniels
[email protected]
--
http://mail.python.org/mailman/listinfo/python-list


Re: What python can NOT do?

2009-09-04 Thread Mike Coleman
On Aug 28, 5:37 pm, qwe rty  wrote:
> i know that an interpreted language like python can't be used to make
> an operating system or system drivers.
>
> what else can NOT be done in python? what are the limitations of the
> language?

Neither of those is strictly true.  It is true, though, that Python
cannot be used to write arbitrarily complex one-liners, though.
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: possible attribute-oriented class

2009-09-04 Thread Scott David Daniels

Ken Newton wrote: ...

I would appreciate comments on this code. First, is something like
this already done? Second, are there reasons for not doing this?  ...

class AttrClass(object):

  ...

def __repr__(self):
return "%s(%s)" % (self.__class__.__name__, self.__dict__.__repr__())
def __str__(self):
ll = ['{']
for k,v in self.__dict__.iteritems():
ll.append("%s : %s" % (k, str(v)))
return '\n'.join(ll) + '}'


Yes, I've done stuff something like this (I use setattr /
getattr rather than direct access to the __dict__).

You'd do better to sort the keys before outputting them, so
that you don't confuse the user by printing two similarly
built parts in different orders.

Personally, I'd filter the outputs to avoid names beginning
with '_', as they may contribute to clutter without adding
much information.

An equality operator would be nice as well (don't bother with
ordering though, you get lost in a twisty maze of definitions
all different).

--Scott David Daniels
[email protected]
--
http://mail.python.org/mailman/listinfo/python-list


Re: File Handling Problem

2009-09-04 Thread Rami Chowdhury

f = open("myfile.txt", "r")
list_one = f.read().splitlines()
f.close()


Or use f.readlines(), which would do the same thing IIRC?

On Fri, 04 Sep 2009 07:46:42 -0700, Stephen Fairchild  
 wrote:



joy99 wrote:


Dear Group,

I have a file. The file has multiple lines. I want to get the line
number of any one of the strings.
Once I get that I like to increment the line number and see the string
of the immediate next line or any following line as output. The
problem as I see is nicely handled in list,

like list_one=["god","earth","sky","cloud","rain"]
  list_one[0]="god"
  list_one[2]="sky"

but can it be for string and file?


It's easy to read a file into a list.

f = open("myfile.txt", "r")
list_one = f.read().splitlines()
f.close()




--
Rami Chowdhury
"Never attribute to malice that which can be attributed to stupidity" --  
Hanlon's Razor

408-597-7068 (US) / 07875-841-046 (UK) / 0189-245544 (BD)
--
http://mail.python.org/mailman/listinfo/python-list


Re: How to access ODBC databases ?

2009-09-04 Thread Martin P. Hellwig

Timothy Madden wrote:

 >>> conn = pyodbc.connect('DRIVER={PostgreSQL 
Unicode};Servername=127.0.0.1;UID=pikantBlue;Database=pikantBlue')

Traceback (most recent call last):
  File "", line 1, in 



pyodbc.Error: ('0', '[0] [nxDC (202) (SQLDriverConnectW)')


Not sure (i.e. wild guess) but that line makes me think it has something 
to do with the encoding, is it possible to try a different driver?


--
MPH
http://blog.dcuktec.com
'If consumed, best digested with added seasoning to own preference.'
--
http://mail.python.org/mailman/listinfo/python-list


Re: File Handling Problem

2009-09-04 Thread Tim Golden

Rami Chowdhury wrote:

f = open("myfile.txt", "r")
list_one = f.read().splitlines()
f.close()


Or use f.readlines(), which would do the same thing IIRC?


No: readlines () retains the "\n"s; splitlines () loses them

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


Re: File Handling Problem

2009-09-04 Thread Stephen Fairchild
joy99 wrote:

> Dear Group,
> 
> I have a file. The file has multiple lines. I want to get the line
> number of any one of the strings.
> Once I get that I like to increment the line number and see the string
> of the immediate next line or any following line as output. The
> problem as I see is nicely handled in list,
> 
> like list_one=["god","earth","sky","cloud","rain"]
>   list_one[0]="god"
>   list_one[2]="sky"
> 
> but can it be for string and file?

It's easy to read a file into a list.

f = open("myfile.txt", "r")
list_one = f.read().splitlines()
f.close()
-- 
Stephen Fairchild
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: What python can NOT do?

2009-09-04 Thread MRAB

Mike Coleman wrote:

On Aug 28, 5:37 pm, qwe rty  wrote:

i know that an interpreted language like python can't be used to make
an operating system or system drivers.

what else can NOT be done in python? what are the limitations of the
language?


Neither of those is strictly true.  It is true, though, that Python
cannot be used to write arbitrarily complex one-liners, though.


Why not? You could put the code in a string literal and then use 'exec',
all in one source line.
--
http://mail.python.org/mailman/listinfo/python-list


Re: Usage of main()

2009-09-04 Thread Scott David Daniels

Carl Banks wrote:

On Sep 3, 11:39 pm, Simon Brunning  wrote:

2009/9/4 Manuel Graune :


How come the main()-idiom is not "the standard way" of writing a
python-program (like e.g. in C)?

Speaking for myself, it *is* the standard way to structure a script. I
find it more readable, since I can put my main function at the very
top where it's visible, with the classes and functions it makes use of
following in some logical sequence.

I suspect that this is the case for many real-world scripts. Perhaps
it's mainly in books and demos where the extra stuff is left out so
the reader can focus on what the writer is demonstrating?


Speaking for myself, I almost never put any logic at the top level in
anything other than tiny throwaway scripts.  Top level is for
importing, and defining functions, classes, and constants, and that's
it.

Even when doing things like preprocessing I'll define a function and
call it rather than putting the logic at top-level.  Sometimes I'll
throw in an if-test at top level (for the kind of stuff I might choose
an #if preprocessor statement in C for) but mostly I just put that in
functions.


If you structure your programs this way, you can get another speedup
for frequently used programs.  Create a little program consisting of:

import actual_program
actual_program.main()

Your larger program will only be compiled once, and the dinky one
compiles quickly.

--Scott David Daniels
[email protected]
--
http://mail.python.org/mailman/listinfo/python-list


retrieving real time quotes from yahoo

2009-09-04 Thread ss
Hello,
ive started using python for a few weeks now, and came across a
problem that i would appreciate help solving.  im trying to create
code which can grab real time quotes from yahoo (yes ive created an
account for yahoo finance).  But im not sure how to generate an
authenticated login and how to request data.  Ive been searching the
net for over the last few hours without having found a way to do this.

any help  would be welcome

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


multiprocessing: Correct usage of pool & queue?

2009-09-04 Thread Allen Fowler
Hello,

I have a list of tasks/items that I want handed off to
threads/processes to complete.  (I would like to stick with process if
I could, since there is some CPU work here. )

Each task involves some calculations and a call to a remote server over 
urllib2/HTTP.  

The
time to complete each task varies from 1 to 20 seconds depending on a
number of factors including variable delay on the remote server.

I would like to:

1) Have a maximum of 20 "in-flight" tasks.  (thus worker processes?)

2)
Not overload the external server that each task is calling.  No more
than "3 new tasks" per second. More "waiting" tasks may be OK, i need
to test it. 

3) Certain tasks in my list must be processed in
the correct order.  (I guess the asignment logic must somehow tag those
to by done by the same worker?)


Do any of you have suggestions? Can someone point me in the direction of sample 
code for this?

Thank you,
:)


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


Re: Multiple inheritance - How to call method_x in InheritedBaseB from method_x in InheritedBaseA?

2009-09-04 Thread Scott David Daniels

The Music Guy wrote:

I have a peculiar problem that involves multiple inheritance and method calling.

I have a bunch of classes, one of which is called MyMixin and doesn't
inherit from anything. MyMixin expects that it will be inherited along
with one of several other classes that each define certain
functionality.

...  ...

This all appears fine at first, but ...
One might be tempted to amend MyMixin's method_x so that it calls the
parent's method_x before doing anything else:

class MyMixin(object):
def method_x(self, a, b, c):
super(MyMixin, self).method_x(a, b, c)
...

...but of course, that will fail with an AttributeError because
MyMixin's only superclass is object, which does not have a method_x.


Here the fix below works.


The only way I can think to solve the problem would be to implement a
method_x for each Foo that calls the method_x for each of the bases:

...

So, does anyone have an idea about how to remedy this, or at least
work around it?


The diamond inheritance stuff is meant to allow you to deal with
exactly this issue.  If you define a class, MixinBase, with do-
nothing entries for all the methods you are inventing, and you
make all of your Mixin classes (and your main class) inherit
from MixinBase, you are guaranteed that all of the Mixins you
use will be earlier on the method resolution order (mro in the
docs) than MixinBase.  If the set of actual methods is small
and pervasive, I might even be tempted rename MixinBase to
"Object":

>>> if 1:
class MixinBase(object):
'''Base for solving mixin strategy.

Also a nice common place to describe the args and meaning.
'''
def method_x(self, a, b, c):
'''Suitable docstring'''
print 'MixinBase'

class MyMixin(MixinBase):
def method_x(self, a, b, c):
super(MyMixin, self).method_x(a, b, c)
print 'Mixin'
class BaseA(MixinBase):
def method_x(self, a, b, c):
super(BaseA, self).method_x(a, b, c)
print 'BaseA'
class BaseB(MixinBase):
pass
class BaseC(MixinBase):
def method_x(self, a, b, c):
super(BaseC, self).method_x(a, b, c)
print 'BaseC'
class FooX(MyMixin, BaseA):
def method_x(self, a, b, c):
super(FooX, self).method_x(a, b, c)
print 'FooX'
class FooY(MyMixin, BaseB):
pass
class FooZ(MyMixin, BaseC):
def method_x(self, a, b, c):
super(FooZ, self).method_x(a, b, c)
print 'FooZ'


>>> FooZ().method_x(1,2,3)
MixinBase
BaseC
Mixin
FooZ
>>> FooY().method_x(1,2,3)
MixinBase
Mixin
>>> FooX().method_x(1,2,3)
MixinBase
BaseA
Mixin
FooX
>>> BaseA().method_x(1,2,3)
MixinBase
BaseA
>>>
--Scott David Daniels
[email protected]
--
http://mail.python.org/mailman/listinfo/python-list


Re: Why does this group have so much spam?

2009-09-04 Thread Ethan Furman

Steven D'Aprano wrote:

On Thu, 03 Sep 2009 16:01:26 -0700, Ethan Furman wrote:



Steven D'Aprano wrote:


On Thu, 03 Sep 2009 12:19:48 -0700, Ethan Furman wrote:




Steven D'Aprano wrote:



On Thu, 03 Sep 2009 04:01:54 -0400, Terry Reedy wrote:





ISP's price residential service based on average fixed cost and
average usage. Multiple homes using one connection push those
averages up.



Is that meant to be a problem?

When people buy more, the unit price they are paying falls, but the
total price they pay generally goes up. E.g. we've recently upgraded
our business link from AUD$150 per month for 60GB to $190 for 100GB.
The per GB price is less, but the total we pay is more -- and the ISP
doesn't have to do much extra work for that extra money.







The difference is that you *upgraded* your service and so incurred a
greater total cost.  If my neighbor lets the rest of the neighborhood
use his wireless, while I do not, yet my prices go up because on
average more usage is happening, I am paying more but not getting more.



Incorrect -- you are getting all the downloads you make yourself, plus
the warm fuzzy feeling of happiness from the knowledge that other
people are making downloads you have paid for.

Of course, if you've *unintentionally* left your wi-fi open, perhaps
"cold feelings of dread and horror" would be more appropriate, but
we're talking about the situation where folks deliberately leave their
wi-fi open for whatever reason.





Read a little closer, Steven -- *my* wi-fi is *closed*, it's my neighbor
(in theory) who has his open, and all that extra usage is making *my*
rate go up -- no warm fuzzies, only irritation.


Okay, that makes zero sense at all.


[snip]

If I'm leaching off my neighbour's open network, chances are that I'll be 
using my own account less, so the average will tend to remain about the 
same.


[more snippage]

Ah, I think that's the communication problem between us -- you're an 
optimist!  ;-)


In my experience, the folks that would use the open wi-fi do *not* have 
an account of their own, are *not* paying their fair share, and I feel 
very differently about that situation than about somebody who *is* 
paying for their *own* service, even if they use it *a lot*.


Don't you just love bold?  ;-)

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


[ANN] Athens Python User Group - Meeting September 9, 2009, 19:00.

2009-09-04 Thread Orestis Markou

== Announcing the 1st meeting of the Athens Python User Group ==

If you live near Athens, Greece and are interested in meeting fellow  
Python programmers, meet us for a friendly chat at the Eleftheroudakis  
Bookstore café, on Wednesday 9 September, 7:00pm.


If you plan to attend, please add a comment here: 
http://orestis.gr/blog/2009/09/01/athens-python-user-group/

Orestis


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


Support for Windows 7 ?

2009-09-04 Thread Pascale Mourier

Hello,

I don't know how to report presumed "bugs" to the Python development 
team.. in the past, the very few bugs I found were always fixed by a 
more recent version.


On of my students has installed Windows 7 RTM on his cherished computer, 
and claims that Python 2.6.2 doesn't support it.
The sample program had a problem with the library function 
os.listdir(dirarg) always returning the same result for different values 
of dirarg.


DO YOU KNOW HOW FAR Python has been tested for Windows 7?

Thanks for suggestions.

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


Re: File Handling Problem

2009-09-04 Thread Rami Chowdhury

No: readlines () retains the "\n"s; splitlines () loses them


Ah, thank you for the clarification!

On Fri, 04 Sep 2009 08:39:37 -0700, Tim Golden   
wrote:



Rami Chowdhury wrote:

f = open("myfile.txt", "r")
list_one = f.read().splitlines()
f.close()

 Or use f.readlines(), which would do the same thing IIRC?


No: readlines () retains the "\n"s; splitlines () loses them

TJG




--
Rami Chowdhury
"Never attribute to malice that which can be attributed to stupidity" --  
Hanlon's Razor

408-597-7068 (US) / 07875-841-046 (UK) / 0189-245544 (BD)
--
http://mail.python.org/mailman/listinfo/python-list


Re: possible attribute-oriented class

2009-09-04 Thread Ken Newton

I like this version very much. I'm ready to put this into practice to see
how it
works in practice.

A minor point: I envision this to be used in a context where all key values
are
strings (legal attribute identifiers). But constructing an AttrClass from a
dict
or setting values directly with the dict syntax can allow any valid item as
a
dict key -- specifically numbers, tuples, etc. If the user of this class
chooses
to do this, a number of the items become inaccessible to the attribute
syntax.
In my case, I think this won't be a problem since I anticipate that values
will
always be set by the attribute syntax, but it might be an issue for other
uses.


On Fri, Sep 4, 2009 at 6:01 AM, Jan Kaliszewski  wrote:

> [originally from [email protected],
>  crossposted to [email protected]]
>
[snip]

>  class AttrDict(dict):  # (or maybe from OrderedDict)
>  "It's only a model. (Shhh!)"
>
>  def __getattr__(self, name):
>  if name.startswith('_'):
>  raise AttributeError("AttrDict's key can't "
>   "start with underscore")
>  else:
>  return self[name]
>
>  def __setattr__(self, name, value):
>  self[name] = value
>
>  def __delattr__(self, name):
>  del self[name]
>
>  def __repr__(self):
>  return '{0}({1})'.format(self.__class__.__name__,
> dict.__repr__(self))
>  def __str__(self):
>  return self._as_str()
>
>  def _gen_format(self, indwidth, indstate):
>  indst = indstate * ' '
>  ind = (indstate + indwidth) * ' '
>  yield ('\n' + indst + '{' if indstate else '{')
>  for key, val in self.items():
>  valstr = (str(val) if not isinstance(val, AttrDict)
>else val._as_str(indwidth, indstate + indwidth))
>  yield '{ind}{key}: {valstr}'.format(ind=ind, key=key,
>  valstr=valstr)
>  yield indst + '}'
>
>  def _as_str(self, indwidth=4, indstate=0):
>  return '\n'.join(self._gen_format(indwidth, indstate))
>
>  def _as_dict(self):
>  return dict.copy(self)
>
>
>  # Test code:
>  if __name__ == '__main__':
>  struct = AttrDict()
>  struct.first = 1
>  struct.second = 2.0
>  struct.third = '3rd'
>  struct.fourth = [4]
>  print(struct)
>  # output:
>  # {
>  # 'second': 2.0
>  # 'fourth': [4]
>  # 'third': '3rd'
>  # 'first': 1
>  # }
>
>  del struct.fourth
>
>  print(repr(struct))
>  # output:
>  # AttrDict({'second': 2.0, 'third': '3rd', 'first': 1})
>
>  print(struct.first)  # (static access)
>  # output:
>  # 1
>
>  for x in ('first', 'second', 'third'):
>  print(struct[x])  # (dynamic access)
>  # output:
>  # 1
>  # 2.0
>  # 3rd
>
>  struct.sub = AttrDict(a=1, b=2, c=89)
>  print(struct._as_dict())
>  # output:
>  # {'second': 2.0, 'sub': AttrDict({'a': 1, 'c': 89, 'b': 2}),\
>  #  'third': '3rd', 'first': 1}
>
>  print(struct._as_str(8))
>  # output:
>  # {
>  # second: 2.0
>  # sub:
>  # {
>  # a: 1
>  # c: 89
>  # b: 2
>  # }
>  # third: 3rd
>  # first: 1
>  # }
>
>
> What do you think about it?
>
> Cheers,
> *j
>
> --
> Jan Kaliszewski (zuo) 
>
> --
> http://mail.python.org/mailman/listinfo/python-list
>
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: recursive decorator

2009-09-04 Thread Ethan Furman

Michele Simionato wrote:

On Sep 3, 6:41 pm, Ethan Furman  wrote:


The original thread by Bearophile:
  http://mail.python.org/pipermail/python-list/2009-May/711848.html



I have read the thread. What Bearophile wants can be implemented with
a bytecode hack, no
need for the decorator module. Let me call 'recur' the self-function,
like in Clojure.
You can define a decorator that makes "self-conscious" a recursive
function as follows:

# requires byteplay by Noam Raphael
# see http://byteplay.googlecode.com/svn/trunk/byteplay.py
from byteplay import Code, LOAD_GLOBAL, STORE_FAST, LOAD_FAST

def enable_recur(f):
print f.func_code.co_names
if 'recur' not in f.func_code.co_names:
return f # do nothing on non-recursive functions
c = Code.from_code(f.func_code)
c.code[1:1] = [(LOAD_GLOBAL, f.__name__), (STORE_FAST, 'recur')]
for i, (opcode, value) in enumerate(c.code[2:]):
if opcode == LOAD_GLOBAL and value == 'recur':
c.code[i+2] = (LOAD_FAST, 'recur')
f.func_code = c.to_code()
return f

## example of use

@enable_recur
def f(x):
if x == 1:
return 1
else:
return x*recur(x-1)

print f(4) # =>24


Please accept this without explanation since it would take me a lot of
time
to explain how it works. Just accept that bytecode hacks are
incredible
(and nonportable too) ;-)

Michele Simionato


No worries -- not sure I would understand the explanation at this point, 
anyway!


Just to verify, using the decorator module is portable, yes?

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


Re: logger module : Question about log message format

2009-09-04 Thread Kev Dwyer
On Fri, 04 Sep 2009 12:34:32 +0100, jorma kala wrote:

> Hi,
> I've created a logger like this:
> 
> 
> LOG_FILENAME = 'test.txt'
> fh=logging.FileHandler(LOG_FILENAME,'w') logger1 =
> logging.getLogger('myLogger1') logger1.addHandler(fh)
> logger1.setLevel(logging.INFO)
> logger1.info('message from logger1')
> 
> and was hoping to get log messages in this format in my log file:
> 
> :INFO:myLogger1:message from logger1
> 
> instead I just get a plain message like this:
> 
> message from logger1
> 
> Do you know why?
> 
> Many thanks.


See http://docs.python.org/library/logging.html#formatter-objects

Kev


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


Running Sum script

2009-09-04 Thread Jul
hello,

I have a .txt file that is in this format --

12625
17000
12000
14500
17000
12000
17000
14500
14500
12000
...and so on...

i need to create a python script that will open this file and have a
running sum until the end of file.

it sounds really simple its just for some reason i am having problem
with it.

i would really appreciate your help
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Support for Windows 7 ?

2009-09-04 Thread Martin v. Löwis
> On of my students has installed Windows 7 RTM on his cherished computer,
> and claims that Python 2.6.2 doesn't support it.
> The sample program had a problem with the library function
> os.listdir(dirarg) always returning the same result for different values
> of dirarg.
> 
> DO YOU KNOW HOW FAR Python has been tested for Windows 7?

To my knowledge, it works fine. I built and tested Python 3.1.1 on
Windows 7, and didn't notice anything strange.

In most respects, Windows 7 is compatible with Vista, so anything
that runs well on Vista (including Python) should also work on W7
just fine.

If there is a specific problem, we would need a specific test case,
to be reported to bugs.python.org.

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


Re: Running Sum script

2009-09-04 Thread David Smith
Jul wrote:
> hello,
> 
> I have a .txt file that is in this format --
> 
> 12625
> 17000
> 12000
> 14500
> 17000
> 12000
> 17000
> 14500
> 14500
> 12000
> ...and so on...
> 
> i need to create a python script that will open this file and have a
> running sum until the end of file.
> 
> it sounds really simple its just for some reason i am having problem
> with it.
> 
> i would really appreciate your help

It is really simple.  Can you post the code you've written so far?

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


Re: Support for Windows 7 ?

2009-09-04 Thread Pascale Mourier

Martin v. Löwis a écrit :


If there is a specific problem, we would need a specific test case,
to be reported to bugs.python.org.


Tks for the name above. I asked my student to prepare the bug demo package,

 but I didn't know how to send it!

Given that the problem is with reading the file system, it is likely to 
be w/ sth else


 than Windows 7, maybe some weird HD partition combination?

Rgds,

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


Re: Running Sum script

2009-09-04 Thread Stephen Fairchild
Jul wrote:

> hello,
> 
> I have a .txt file that is in this format --
> 
> 12625
> 17000
> 12000
> 14500
> 17000
> 12000
> 17000
> 14500
> 14500
> 12000
> ...and so on...
> 
> i need to create a python script that will open this file and have a
> running sum until the end of file.

Untested:

with open("numbers.txt", "r") as f:
   print sum(int(x) for x in f)
-- 
Stephen Fairchild
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Support for Windows 7 ?

2009-09-04 Thread Martin v. Löwis
> Given that the problem is with reading the file system, it is likely to
> be w/ sth else
> 
>  than Windows 7, maybe some weird HD partition combination?

Without having seen any details, I refuse to guess. Most likely, it is
a user mistake.

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


Re: Running Sum script

2009-09-04 Thread Jul
On Sep 4, 2:21 pm, Stephen Fairchild  wrote:
> Jul wrote:
> > hello,
>
> > I have a .txt file that is in this format --
>
> > 12625
> > 17000
> > 12000
> > 14500
> > 17000
> > 12000
> > 17000
> > 14500
> > 14500
> > 12000
> > ...and so on...
>
> > i need to create a python script that will open this file and have a
> > running sum until the end of file.
>
> Untested:
>
> with open("numbers.txt", "r") as f:
>print sum(int(x) for x in f)
> --
> Stephen Fairchild

thats what i have so far --

#!/usr/bin/python

import os.path

#open up the file
formisanoOpen = open("formisano_num.txt", "r")

#read in all the data into a list
readData = formisanoOpen.readLines()

#set up a sum
sum = 0;

#begin a loop
for trial in readData:

#the next line is indented (YA doesn't indent)
sum += int(trial)

#loop is over, so unindent
#report the sum
print sum


end

but it doesnt want to run for some reason
-- 
http://mail.python.org/mailman/listinfo/python-list


Application-global "switches"?

2009-09-04 Thread kj



I'm looking for the "best-practice" way to define application-global
read-only switches, settable from the command line.  The best
example I can think of of such global switch is the built-in variable
__debug__.  This variable is visible everywhere in a program, and
broadly affects its operation.

The situation that prompts this question is the task of implementing
a certain application that is supposed to run for several days
(typically 2-3 weeks).  It is important to be able to re-start this
application where it left off in case that, for some reason (e.g.
internet connection failure), it terminates prematurely.  When this
application is restarted its behavior is somewhat different from
when it is started from scratch.  (For example, when it is re-started,
it does not clear certain directories.)

Hence, I'd like to be able to have a variable, e.g. CONTINUATION_MODE,
visible everywhere in the code, that tells the application to behave
in "continuation mode", so that I can write stuff like

if not CONTINUATION_MODE:
clean_the_slate()

The only solution I can come up with is to define a "dummy module",
say _config.py, which contains only upper-case variables representing
these global switches, and is imported by all the other modules in
the application with the line "from _config import *".  During the
early stages of the run, the script inspects the command-line flags,
and if it finds a --continuing flag, it sets the variable
_config.CONTINUATION_MODE to True.  (The last point implies that
these variables are not strictly speaking read-only, since they
most be set at the beginning of the run.  But after this initial
setting, they should remain read-only.)

I'm sure this would work OK, but I wonder if there is a more Pythonic
way to do this sort of thing.  Is there a best practice for setting
such application-global switches?

TIA!

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


Re: Running Sum script

2009-09-04 Thread David Smith
Jul wrote:
> On Sep 4, 2:21 pm, Stephen Fairchild  wrote:
>> Jul wrote:
>>> hello,
>>> I have a .txt file that is in this format --
>>> 12625
>>> 17000
>>> 12000
>>> 14500
>>> 17000
>>> 12000
>>> 17000
>>> 14500
>>> 14500
>>> 12000
>>> ...and so on...
>>> i need to create a python script that will open this file and have a
>>> running sum until the end of file.
>> Untested:
>>
>> with open("numbers.txt", "r") as f:
>>print sum(int(x) for x in f)
>> --
>> Stephen Fairchild
> 
> thats what i have so far --
> 
> #!/usr/bin/python
> 
> import os.path
> 
> #open up the file
> formisanoOpen = open("formisano_num.txt", "r")
> 
> #read in all the data into a list
> readData = formisanoOpen.readLines()
> 
> #set up a sum
> sum = 0;
> 
> #begin a loop
> for trial in readData:
> 
> #the next line is indented (YA doesn't indent)
> sum += int(trial)
> 
> #loop is over, so unindent
> #report the sum
> print sum
> 
> 
> end
> 
> but it doesnt want to run for some reason

... ok ... what do you get.  If it's an error, please post the stack
trace.  Please help us help you -- provide details.

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


Re: Application-global "switches"?

2009-09-04 Thread Terry Reedy

kj wrote:



I'm looking for the "best-practice" way to define application-global
read-only switches, settable from the command line.  The best
example I can think of of such global switch is the built-in variable
__debug__.  This variable is visible everywhere in a program, and
broadly affects its operation.

The situation that prompts this question is the task of implementing
a certain application that is supposed to run for several days
(typically 2-3 weeks).  It is important to be able to re-start this
application where it left off in case that, for some reason (e.g.
internet connection failure), it terminates prematurely.  When this
application is restarted its behavior is somewhat different from
when it is started from scratch.  (For example, when it is re-started,
it does not clear certain directories.)

Hence, I'd like to be able to have a variable, e.g. CONTINUATION_MODE,
visible everywhere in the code, that tells the application to behave
in "continuation mode", so that I can write stuff like

if not CONTINUATION_MODE:
clean_the_slate()

The only solution I can come up with is to define a "dummy module",
say _config.py, which contains only upper-case variables representing
these global switches, and is imported by all the other modules in
the application with the line "from _config import *".  During the
early stages of the run, the script inspects the command-line flags,
and if it finds a --continuing flag, it sets the variable
_config.CONTINUATION_MODE to True.  (The last point implies that
these variables are not strictly speaking read-only, since they
most be set at the beginning of the run.  But after this initial
setting, they should remain read-only.)

I'm sure this would work OK, but I wonder if there is a more Pythonic
way to do this sort of thing.  Is there a best practice for setting
such application-global switches?


I believe what you describe above is more or less standard practice.

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


Re: Running Sum script

2009-09-04 Thread Rami Chowdhury

Could you let us know what kind of error you are getting?

I don't know if this is your error, but this line won't run:


readData = formisanoOpen.readLines()


Since Python is case-sensitive, you would need a lower-case 'l' in  
'readlines()' -- perhaps that would solve your problem?


On Fri, 04 Sep 2009 11:26:06 -0700, Jul  wrote:


On Sep 4, 2:21 pm, Stephen Fairchild  wrote:

Jul wrote:
> hello,

> I have a .txt file that is in this format --

> 12625
> 17000
> 12000
> 14500
> 17000
> 12000
> 17000
> 14500
> 14500
> 12000
> ...and so on...

> i need to create a python script that will open this file and have a
> running sum until the end of file.

Untested:

with open("numbers.txt", "r") as f:
   print sum(int(x) for x in f)
--
Stephen Fairchild


thats what i have so far --

#!/usr/bin/python

import os.path

#open up the file
formisanoOpen = open("formisano_num.txt", "r")

#read in all the data into a list
readData = formisanoOpen.readLines()

#set up a sum
sum = 0;

#begin a loop
for trial in readData:

#the next line is indented (YA doesn't indent)
sum += int(trial)

#loop is over, so unindent
#report the sum
print sum


end

but it doesnt want to run for some reason




--
Rami Chowdhury
"Never attribute to malice that which can be attributed to stupidity" --  
Hanlon's Razor

408-597-7068 (US) / 07875-841-046 (UK) / 0189-245544 (BD)
--
http://mail.python.org/mailman/listinfo/python-list


Re: Application-global "switches"?

2009-09-04 Thread ici
On Sep 4, 9:29 pm, kj  wrote:
> I'm looking for the "best-practice" way to define application-global
> read-only switches, settable from the command line.  The best
> example I can think of of such global switch is the built-in variable
> __debug__.  This variable is visible everywhere in a program, and
> broadly affects its operation.
>
> The situation that prompts this question is the task of implementing
> a certain application that is supposed to run for several days
> (typically 2-3 weeks).  It is important to be able to re-start this
> application where it left off in case that, for some reason (e.g.
> internet connection failure), it terminates prematurely.  When this
> application is restarted its behavior is somewhat different from
> when it is started from scratch.  (For example, when it is re-started,
> it does not clear certain directories.)
>
> Hence, I'd like to be able to have a variable, e.g. CONTINUATION_MODE,
> visible everywhere in the code, that tells the application to behave
> in "continuation mode", so that I can write stuff like
>
>     if not CONTINUATION_MODE:
>         clean_the_slate()
>
> The only solution I can come up with is to define a "dummy module",
> say _config.py, which contains only upper-case variables representing
> these global switches, and is imported by all the other modules in
> the application with the line "from _config import *".  During the
> early stages of the run, the script inspects the command-line flags,
> and if it finds a --continuing flag, it sets the variable
> _config.CONTINUATION_MODE to True.  (The last point implies that
> these variables are not strictly speaking read-only, since they
> most be set at the beginning of the run.  But after this initial
> setting, they should remain read-only.)
>
> I'm sure this would work OK, but I wonder if there is a more Pythonic
> way to do this sort of thing.  Is there a best practice for setting
> such application-global switches?
>
> TIA!
>
> kynn

while 1:
  try:
run_main_code()
cleanup()
  except:
while conn_fail():
   time.sleep(5.0)
  finally:
cleanup_all()
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: How to access ODBC databases ?

2009-09-04 Thread Timothy Madden

Martin P. Hellwig wrote:

Timothy Madden wrote:

 >>> conn = pyodbc.connect('DRIVER={PostgreSQL 
Unicode};Servername=127.0.0.1;UID=pikantBlue;Database=pikantBlue')

Traceback (most recent call last):
  File "", line 1, in 



pyodbc.Error: ('0', '[0] [nxDC (202) (SQLDriverConnectW)')


Not sure (i.e. wild guess) but that line makes me think it has something 
to do with the encoding, is it possible to try a different driver?



Thank you.

Slackware also comes with mysql, I guess I could try to install and 
register a driver for it in my unixODBC installation.


What about the encoding ? What should I be looking for in this case ?
Indeed when I try
   createdb --encoding=UTF-8 pikantBlue
I get the message that the server's /LC_TYPE settings require encoding 
to be LATIN1/. What should I do about it ? Should I have been more 
careful about the configure options when compiling postgresql ? I also 
tried pyodbc on Ubuntu with the database installed with apt-get and I 
still get the same problem.


Thank you
Timothy Madden
--
http://mail.python.org/mailman/listinfo/python-list


Re: Running Sum script

2009-09-04 Thread Juli Dolzhenko
On Sep 4, 2:52 pm, "Rami Chowdhury"  wrote:
> Could you let us know what kind of error you are getting?
>
> I don't know if this is your error, but this line won't run:
>
> > readData = formisanoOpen.readLines()
>
> Since Python is case-sensitive, you would need a lower-case 'l' in
> 'readlines()' -- perhaps that would solve your problem?
>
>
>
> On Fri, 04 Sep 2009 11:26:06 -0700, Jul  wrote:
> > On Sep 4, 2:21 pm, Stephen Fairchild  wrote:
> >> Jul wrote:
> >> > hello,
>
> >> > I have a .txt file that is in this format --
>
> >> > 12625
> >> > 17000
> >> > 12000
> >> > 14500
> >> > 17000
> >> > 12000
> >> > 17000
> >> > 14500
> >> > 14500
> >> > 12000
> >> > ...and so on...
>
> >> > i need to create a python script that will open this file and have a
> >> > running sum until the end of file.
>
> >> Untested:
>
> >> with open("numbers.txt", "r") as f:
> >>print sum(int(x) for x in f)
> >> --
> >> Stephen Fairchild
>
> > thats what i have so far --
>
> > #!/usr/bin/python
>
> > import os.path
>
> > #open up the file
> > formisanoOpen = open("formisano_num.txt", "r")
>
> > #read in all the data into a list
> > readData = formisanoOpen.readLines()
>
> > #set up a sum
> > sum = 0;
>
> > #begin a loop
> > for trial in readData:
>
> > #the next line is indented (YA doesn't indent)
> > sum += int(trial)
>
> > #loop is over, so unindent
> > #report the sum
> > print sum
>
> > end
>
> > but it doesnt want to run for some reason
>
> --
> Rami Chowdhury
> "Never attribute to malice that which can be attributed to stupidity" --
> Hanlon's Razor
> 408-597-7068 (US) / 07875-841-046 (UK) / 0189-245544 (BD)

in the terminal i get a very strange "permission denied" error that
might not have anything to do with the code. I checked permissions for
the file and they are set to "read and write" so, again, I am really
not sure what going wrong.
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: cross platform distribution

2009-09-04 Thread vpr
On Sep 4, 3:33 pm, Philip Semanchuk  wrote:
> On Sep 4, 2009, at 9:24 AM, vpr wrote:
>
>
>
> > On Sep 4, 3:19 pm, Philip Semanchuk  wrote:
> >> On Sep 4, 2009, at 4:44 AM, vpr wrote:
>
> >>> Hi All
>
> >>> After a couple of experiments, searching around and reading Steve
> >>> Holden's lament about bundling and ship python code, I thought I'd
> >>> direct this to to the group. I'm using Python 2.6 btw.
>
> >>> I've build a commercial application that I'd like to bundle and  
> >>> ship.
> >>> I'd like to protect some of my IP and the py2exe and cx_freeze  
> >>> builds
> >>> provide good enough protection for me.
>
> >>> I'd like to provide a build for windows and a build for linux.  
> >>> Windows
> >>> ironically has been easier to target and py2exe has given me a nice
> >>> build that I can ship between XP, Vista & Server on both 32 and 64
> >>> bit.
>
> >>> On linux I've build a build using cx_freeze which works well except
> >>> it's not really portable betweem distributions.
>
> >>> I've also been thinking about distributing bytcode versions but  
> >>> things
> >>> get tricky quickly.
>
> >>> Can anywone give me some pointers?
>
> >> I don't know how much "critical" code you have, but you might want to
> >> look at Cython which will translate your Python into C with little
> >> change to your Python source. Of course, compiled C code can still be
> >> disassembled, but it's harder than Python bytecode.
>
> >> HTH
> >> P
>
> > Hi Peter
>
> It's Philip, actually. =)
>
> > Sounds like a plan, how portable will that be between Linux systems?
>
> Very portable, but I should have mentioned that it requires you to  
> distribute a C file that's compiled on the user's machine. That's easy  
> to do via distutils but it adds a requirement to your app.
>
> > Won't I run into some GLIBC problems?
> > Can you force it to statically link the binary?
>
> I don't know the answer to those questions, but it's just a regular C  
> file, albeit one that's autogenerated. It comes with all of the pros  
> and cons of a C file you'd written yourself.
>
> Good luck
> Philip

Thank Philip :)
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: cross platform distribution

2009-09-04 Thread ianaré
These are all good suggestions. I just wanted to add that you can
distribute pre-built Linux packages for the most popular distros like
one for RHEL/Centos/Fedora as RPM and one for Debian/Ubuntu as DEB.
Any C code in them would be compiled.

On Sep 4, 9:33 am, Philip Semanchuk  wrote:
> On Sep 4, 2009, at 9:24 AM, vpr wrote:
>
>
>
> > On Sep 4, 3:19 pm, Philip Semanchuk  wrote:
> >> On Sep 4, 2009, at 4:44 AM, vpr wrote:
>
> >>> Hi All
>
> >>> After a couple of experiments, searching around and reading Steve
> >>> Holden's lament about bundling and ship python code, I thought I'd
> >>> direct this to to the group. I'm using Python 2.6 btw.
>
> >>> I've build a commercial application that I'd like to bundle and  
> >>> ship.
> >>> I'd like to protect some of my IP and the py2exe and cx_freeze  
> >>> builds
> >>> provide good enough protection for me.
>
> >>> I'd like to provide a build for windows and a build for linux.  
> >>> Windows
> >>> ironically has been easier to target and py2exe has given me a nice
> >>> build that I can ship between XP, Vista & Server on both 32 and 64
> >>> bit.
>
> >>> On linux I've build a build using cx_freeze which works well except
> >>> it's not really portable betweem distributions.
>
> >>> I've also been thinking about distributing bytcode versions but  
> >>> things
> >>> get tricky quickly.
>
> >>> Can anywone give me some pointers?
>
> >> I don't know how much "critical" code you have, but you might want to
> >> look at Cython which will translate your Python into C with little
> >> change to your Python source. Of course, compiled C code can still be
> >> disassembled, but it's harder than Python bytecode.
>
> >> HTH
> >> P
>
> > Hi Peter
>
> It's Philip, actually. =)
>
> > Sounds like a plan, how portable will that be between Linux systems?
>
> Very portable, but I should have mentioned that it requires you to  
> distribute a C file that's compiled on the user's machine. That's easy  
> to do via distutils but it adds a requirement to your app.
>
> > Won't I run into some GLIBC problems?
> > Can you force it to statically link the binary?
>
> I don't know the answer to those questions, but it's just a regular C  
> file, albeit one that's autogenerated. It comes with all of the pros  
> and cons of a C file you'd written yourself.
>
> Good luck
> Philip

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


Re: possible attribute-oriented class

2009-09-04 Thread Jan Kaliszewski

04-09-2009 Ken Newton  wrote:


I like this version very much. I'm ready to put this into practice to see
how it works in practice.

[snip]

Not only you (Ken) and me. :-) It appears that the idea is quite old. Nick
Coghlan replied at [email protected]:


Jan Kaliszewski wrote:

What do you think about it?


It reminds me a bit of the old (short-lived) namespaces module:

http://web.archive.org/web/20060216094030/http://namespace.python-hosting.com/

Steven's draft PEP on the topic is still available in the python-list
archives:

http://mail.python.org/pipermail/python-list/2005-February/307235.html

The problem we found with it was that the basic solutions (empty class
and now named_tuple) were good enough that it wasn't worth the hassle
involved in grabbing an extra library for it.


Named tuples (which indeed are really very nice) are read-only, but the
approach they represent could (and IMHO should) be extended to some kind
of mutable objects.

The old discussion, the above link points to, shows that such a
dot-accessible dict-like class is something that many people need and
repeatedly implemet it (more or less perfectly) for themselves.

Maybe that past proposition (to add a separate namespace module which
a number types for viewing, chaining and so on) was too sophisticated?

Most common use cases could be covered with one attr-dict-like type,
that could be placed in collections module (or even, in time, as
a built-in factory function, together with namedtuple?).


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


Re: Running Sum script

2009-09-04 Thread Tobiah


> in the terminal i get a very strange "permission denied" error that might
> not have anything to do with the code. I checked permissions for the file
> and they are set to "read and write" so, again, I am really not sure what
> going wrong.

Try:

python myfile

Or

chmod +x myfile
./myfile
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Running Sum script

2009-09-04 Thread Maggie
On Sep 4, 4:37 pm, Tobiah  wrote:
> > in the terminal i get a very strange "permission denied" error that might
> > not have anything to do with the code. I checked permissions for the file
> > and they are set to "read and write" so, again, I am really not sure what
> > going wrong.
>
> Try:
>
> python myfile
>
> Or
>
> chmod +x myfile
> ./myfile

try it where? code or terminal?

thanks so much?
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Application-global "switches"?

2009-09-04 Thread Ethan Furman

ici wrote:

On Sep 4, 9:29 pm, kj  wrote:


I'm looking for the "best-practice" way to define application-global
read-only switches, settable from the command line.  The best
example I can think of of such global switch is the built-in variable
__debug__.  This variable is visible everywhere in a program, and
broadly affects its operation.

The situation that prompts this question is the task of implementing
a certain application that is supposed to run for several days
(typically 2-3 weeks).  It is important to be able to re-start this
application where it left off in case that, for some reason (e.g.
internet connection failure), it terminates prematurely.  When this
application is restarted its behavior is somewhat different from
when it is started from scratch.  (For example, when it is re-started,
it does not clear certain directories.)

Hence, I'd like to be able to have a variable, e.g. CONTINUATION_MODE,
visible everywhere in the code, that tells the application to behave
in "continuation mode", so that I can write stuff like

   if not CONTINUATION_MODE:
   clean_the_slate()

The only solution I can come up with is to define a "dummy module",
say _config.py, which contains only upper-case variables representing
these global switches, and is imported by all the other modules in
the application with the line "from _config import *".  During the
early stages of the run, the script inspects the command-line flags,
and if it finds a --continuing flag, it sets the variable
_config.CONTINUATION_MODE to True.  (The last point implies that
these variables are not strictly speaking read-only, since they
most be set at the beginning of the run.  But after this initial
setting, they should remain read-only.)

I'm sure this would work OK, but I wonder if there is a more Pythonic
way to do this sort of thing.  Is there a best practice for setting
such application-global switches?

TIA!

kynn



while 1:
  try:
run_main_code()
cleanup()
  except:
while conn_fail():
   time.sleep(5.0)
  finally:
cleanup_all()


How will this restart the OP's process with all files still in place and 
continuation mode on?


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


Re: Running Sum script

2009-09-04 Thread Rami Chowdhury

try it where? code or terminal?


Please try these in the terminal -- the permission denied error may be due  
to your shell not being able to execute the Python script, instead of your  
Python script not being able to open the data file.


On Fri, 04 Sep 2009 13:37:10 -0700, Maggie  wrote:


On Sep 4, 4:37 pm, Tobiah  wrote:
> in the terminal i get a very strange "permission denied" error that  
might
> not have anything to do with the code. I checked permissions for the  
file
> and they are set to "read and write" so, again, I am really not sure  
what

> going wrong.

Try:

python myfile

Or

chmod +x myfile
./myfile


try it where? code or terminal?

thanks so much?




--
Rami Chowdhury
"Never attribute to malice that which can be attributed to stupidity" --  
Hanlon's Razor

408-597-7068 (US) / 07875-841-046 (UK) / 0189-245544 (BD)
--
http://mail.python.org/mailman/listinfo/python-list


Re: How to access ODBC databases ?

2009-09-04 Thread Martin P. Hellwig

Timothy Madden wrote:

Martin P. Hellwig wrote:

Timothy Madden wrote:

 >>> conn = pyodbc.connect('DRIVER={PostgreSQL 
Unicode};Servername=127.0.0.1;UID=pikantBlue;Database=pikantBlue')

Traceback (most recent call last):
  File "", line 1, in 



pyodbc.Error: ('0', '[0] [nxDC (202) (SQLDriverConnectW)')


Not sure (i.e. wild guess) but that line makes me think it has 
something to do with the encoding, is it possible to try a different 
driver?



Thank you.

Slackware also comes with mysql, I guess I could try to install and 
register a driver for it in my unixODBC installation.


What about the encoding ? What should I be looking for in this case ?
Indeed when I try
   createdb --encoding=UTF-8 pikantBlue
I get the message that the server's /LC_TYPE settings require encoding 
to be LATIN1/. What should I do about it ? Should I have been more 
careful about the configure options when compiling postgresql ? I also 
tried pyodbc on Ubuntu with the database installed with apt-get and I 
still get the same problem.


Thank you
Timothy Madden


According to this:
http://psqlodbc.projects.postgresql.org/faq.html#2.5
You could try the 'PostgreSQL ANSI' driver.

--
MPH
http://blog.dcuktec.com
'If consumed, best digested with added seasoning to own preference.'
--
http://mail.python.org/mailman/listinfo/python-list


Re: Running Sum script

2009-09-04 Thread <><><><>
On Sep 4, 4:37 pm, Maggie  wrote:
> On Sep 4, 4:37 pm, Tobiah  wrote:
>
> > > in the terminal i get a very strange "permission denied" error that might
> > > not have anything to do with the code. I checked permissions for the file
> > > and they are set to "read and write" so, again, I am really not sure what
> > > going wrong.
>
> > Try:
>
> > python myfile
>
> > Or
>
> > chmod +x myfile
> > ./myfile
>
> try it where? code or terminal?
>
> thanks so much?

fantastic...! thank you so much..now i finally have my errors - which
are the following --

 File "running_sum_formisano.py", line 18
sum+= int(trial)
  ^
IndentationError: expected an indented block

how would this be fixed?
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Support for Windows 7 ?

2009-09-04 Thread Martin P. Hellwig

Michel Claveau - MVP wrote:

Du coup, j'ai envie de déduire : 
  - Que certains étudiants d'écoles de commerce françaises préfèrent travailler avec "l'étranger" plutôt qu'avec "le français".

  - Il faudra dire à d'autres étudiants d'écoles de commerce françaises que le 
fait de ne pas arriver/savoir installer et tester Python sous Windows-7 donne 
une mauvaise image de l'école...

I'd like to conclude that it is perfectly acceptable to continue a 
thread in the language it started with, even though all parties might 
have a native language other than English.



--
MPH
http://blog.dcuktec.com
'If consumed, best digested with added seasoning to own preference.'
--
http://mail.python.org/mailman/listinfo/python-list


Re: Problem w/ mysqldump

2009-09-04 Thread Victor Subervi
I got essentially the same printout. There were the following, among many
others:
mysqldump.exe
mysqldump.pdb
What's a *.pdb file? Don't know it matters. If there were just some darn way
to know where that daggone database is, I could copy it and move it to
another machine.
TIA,
V

On Thu, Sep 3, 2009 at 1:42 PM, Dennis Lee Bieber wrote:

> On Wed, 2 Sep 2009 16:45:02 -0400, Victor Subervi
>  declaimed the following in
> gmane.comp.python.general:
>
> > I tried running it like you said, got this error:
> > 'mysqldump' is not a recognized internal or external command.
> > If I could just figure out in what file the data were stored, I could
> copy
> > it and try it in another computer. Any ideas?
>
>That tells me that the MySQL binaries are either not installed on
> the computer, or are not in the Windows (not Python) search path. (note:
> I've line split at each ; so it can be read)
>
> C:\Documents and Settings\Dennis Lee Bieber>echo %PATH%
> e:\Python25\;
> E:\GNAT\2008\bin;
> C:\WINDOWS\system32;
> C:\WINDOWS;
> C:\WINDOWS\System32\Wbem;
> C:\Program Files\SciTE;
> C:\Program Files\Java\jre1.6.0_03\bin;
> C:\Program Files\Java\jdk1.6.0_03\bin;
> C:\Program Files\Common Files\Adobe\AGL;
> C:\Tcl\bin;
> C:\Program Files\Common Files\Roxio Shared\9.0\DLLShared\;
> C:\Program Files\Common Files\Roxio Shared\DLLShared\;
> c:\Regina;
> C:\Program Files\TortoiseSVN\bin;
>
> C:\PROGRA~1\MySQL\MySQL Server 5.0\bin;
>
> E:\GNAT\GtkAda\bin;
> C:\MSSQL7\BINN;
> c:\PROGRA~1\sdb\programs\bin;
> c:\PROGRA~1\sdb\programs\pgm;
> e:\python25\scripts;
> C:\WINDOWS\system32\WindowsPowerShell\v1.0;
> C:\Program Files\Common Files\Roxio Shared\DLLShared\;
> C:\Program Files\Common Files\Roxio Shared\9.0\DLLShared\;
> c:\bin
>
>Note the isolated line in the middle above? That's how the OS finds
> all the MySQL command line tools.
>
> C:\Documents and Settings\Dennis Lee Bieber>dir "C:\PROGRA~1\MySQL\MySQL
> Server 5.0\bin"
>  Volume in drive C is System
>  Volume Serial Number is 0487-A514
>
>  Directory of C:\PROGRA~1\MySQL\MySQL Server 5.0\bin
>
> 09/16/2006  11:04 AM  .
> 09/16/2006  11:04 AM  ..
> 08/26/2006  01:14 AM 1,196,032 comp-err.exe
> 08/26/2006  01:14 AM 1,519,616 libmySQL.dll
> 08/26/2006  01:14 AM 1,433,600 myisamchk.exe
> 08/26/2006  01:14 AM 1,339,392 myisamlog.exe
> 08/26/2006  01:14 AM 1,351,680 myisampack.exe
> 08/26/2006  01:14 AM 1,314,816 myisam_ftdump.exe
> 08/26/2006  01:14 AM 1,622,016 mysql.exe
> 08/26/2006  01:14 AM 1,482,752 mysqladmin.exe
> 08/26/2006  01:14 AM 1,499,136 mysqlbinlog.exe
> 08/26/2006  01:14 AM 1,470,464 mysqlcheck.exe
> 08/26/2006  01:14 AM 6,721,536 mysqld-debug.exe
> 08/26/2006  01:14 AM 4,964,352 mysqld-max-nt.exe
> 08/26/2006  01:14 AM 4,960,256 mysqld-max.exe
> 08/26/2006  01:14 AM 4,435,968 mysqld-nt.exe
> 08/26/2006  01:14 AM 4,435,968 mysqld.exe
> 08/26/2006  01:14 AM 1,511,424 mysqldump.exe
> 08/26/2006  01:14 AM 1,470,464 mysqlimport.exe
> 04/19/2006  04:41 AM 1,561,600 MySQLInstanceConfig.exe
> 08/26/2006  01:14 AM 1,519,616 mysqlmanager.exe
> 08/26/2006  01:14 AM 1,474,560 mysqlshow.exe
> 08/26/2006  01:14 AM 1,576,960 mysqltest.exe
> 08/26/2006  01:14 AM 1,814,528 mysql_client_test.exe
> 08/26/2006  01:14 AM 1,200,128 mysql_upgrade.exe
> 08/26/2006  01:14 AM 1,191,936 my_print_defaults.exe
> 08/26/2006  01:14 AM 1,171,456 perror.exe
> 08/26/2006  01:14 AM 1,187,840 replace.exe
>  26 File(s) 55,428,096 bytes
>   2 Dir(s)  11,701,739,520 bytes free
>
>MySQLdb is just a db-api interface to a running server. But
> mysqldump is not a MySQLdb command -- it's a MySQL commandline utility.
> Anything being invoked via os.system(), or a popen() variant, is
> external to Python, and depends upon the OS to execute.
>
>
>
>
>
>
> --
>Wulfraed Dennis Lee Bieber   KD6MOG
>[email protected]   
> HTTP://wlfraed.home.netcom.com/
>
> --
> http://mail.python.org/mailman/listinfo/python-list
>
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: [Python-ideas] possible attribute-oriented class

2009-09-04 Thread George Sakkis
On Fri, Sep 4, 2009 at 4:37 PM, Jan Kaliszewski wrote:
> 04-09-2009 Ken Newton  wrote:
>
>> I like this version very much. I'm ready to put this into practice to see
>> how it works in practice.
>
> [snip]
>
> Not only you (Ken) and me. :-) It appears that the idea is quite old. Nick
> Coghlan replied at [email protected]:
>
>> Jan Kaliszewski wrote:
>>>
>>> What do you think about it?
>>
>> It reminds me a bit of the old (short-lived) namespaces module:
>>
>>
>> http://web.archive.org/web/20060216094030/http://namespace.python-hosting.com/
>>
>> Steven's draft PEP on the topic is still available in the python-list
>> archives:
>>
>> http://mail.python.org/pipermail/python-list/2005-February/307235.html
>>
>> The problem we found with it was that the basic solutions (empty class
>> and now named_tuple) were good enough that it wasn't worth the hassle
>> involved in grabbing an extra library for it.
>
> Named tuples (which indeed are really very nice) are read-only, but the
> approach they represent could (and IMHO should) be extended to some kind
> of mutable objects.

Maybe something like http://code.activestate.com/recipes/576555/ ?

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


Re: Running Sum script

2009-09-04 Thread Chris Rebert
On Fri, Sep 4, 2009 at 1:49 PM, <><><><> wrote:
> On Sep 4, 4:37 pm, Maggie  wrote:
>> On Sep 4, 4:37 pm, Tobiah  wrote:
>>
>> > > in the terminal i get a very strange "permission denied" error that might
>> > > not have anything to do with the code. I checked permissions for the file
>> > > and they are set to "read and write" so, again, I am really not sure what
>> > > going wrong.
>>
>> > Try:
>>
>> >         python myfile
>>
>> > Or
>>
>> >         chmod +x myfile
>> >         ./myfile
>>
>> try it where? code or terminal?
>>
>> thanks so much?
>
> fantastic...! thank you so much..now i finally have my errors - which
> are the following --
>
>  File "running_sum_formisano.py", line 18
>    sum+= int(trial)
>      ^
> IndentationError: expected an indented block
>
> how would this be fixed?

Have you even read the Python tutorial? Please do so if you haven't already.
Indent the line by 4 spaces.

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


Re: obscure problem using elementtree to make xhtml website

2009-09-04 Thread Nobody
On Fri, 04 Sep 2009 13:21:54 +0200, Stefan Behnel wrote:

 Not a bug in IE (this time), which is correctly parsing the file as html.
>>> ... which is obviously not the correct thing to do when it's XHTML.
>> 
>> It isn't though; it's HTML with a XHTML DOCTYPE
> 
> Not the page I look at (i.e. the link provided by the OP). It clearly has
> an XHTML namespace, so it's X(HT)ML, not HTML.

It depends upon your User-Agent header.

By default, it returns a Content-Type of application/xhtml+xml, so it
should be parsed as XML, i.e.  should be treated as
.

But if the User-Agent header indicates MSIE, it returns a Content-Type of
text/html, which should be parsed as HTML, where  won't work.

XHTML can be either HTML or XML, and it makes a difference as to whether
you parse it as HTML or XML. If you want to create a document which parses
the same way in either case, you must adhere to the compatibility
rules in Appendix C of the XHTML standard, which means (amongst other
things) not minimising tags which can have content (i.e. not EMPTY),
regardless of whether or not they do have content.

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


Re: Subclassing list and splicing

2009-09-04 Thread Nobody
On Thu, 03 Sep 2009 17:10:12 +, Kreso wrote:

> I would prefer that resulting object m belonged to myclist class.
> How to obtain such behaviour? Must I somehow implement __getslice__
> method myself?

Yes; you should also implement __getitem__(), as this is used for extended
slices.

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


Compiler.ast helper function "literal_eval" in python 2.4

2009-09-04 Thread Sean Talts
Hi,

I'm trying to parse some python with the compiler module, select a
subset of the AST returned, and then evaluate that subset, all in
python 2.4.  It seems like in python 2.6 the compiler.ast.literal_eval
function may be what I'm looking for, but unfortunately for my project
we are restricted to using 2.4.  I was wondering if there was some way
programmers performed this way back when?

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


How to download directly to a file?

2009-09-04 Thread kj



I want to send a POST request and have the returned content put
directly into a file.  Is there a way to do this easily in Python?
I've been looking at the documentation for urllib2, but I can't
see a direct way to do this, other than saving the returned contents
to an in-memory variable and writing out the variable to a file.
But this is precisely what I'd like to avoid.

Thanks!

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


Re: How to download directly to a file?

2009-09-04 Thread Diez B. Roggisch

kj schrieb:

I want to send a POST request and have the returned content put
directly into a file.  Is there a way to do this easily in Python?
I've been looking at the documentation for urllib2, but I can't
see a direct way to do this, other than saving the returned contents
to an in-memory variable and writing out the variable to a file.
But this is precisely what I'd like to avoid.


You get a file-like object, what's wrong reading that chunkwise & 
dumping that to a file? Or are 4KB blocksize to hard of a memory constraint?


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


incorrect DeprecationWarning?

2009-09-04 Thread Alan G Isaac

Python 2.6.2 (r262:71605, Apr 14 2009, 22:40:02) [MSC v.1500 32 bit (Intel)] on 
win32
Type "help", "copyright", "credits" or "license" for more information.

class MyError(Exception):

... def __init__(self, message):
... Exception.__init__(self)
... self.message = message
...

e = MyError('msg')

__main__:4: DeprecationWarning: BaseException.message has been deprecated as of 
Python 2.6


So?  Why would that mean I cannot add such an attribute
to derived classes?

Contrast:

Python 2.6.2 (r262:71605, Apr 14 2009, 22:40:02) [MSC v.1500 32 bit (Intel)] on 
win32
Type "help", "copyright", "credits" or "license" for more information.

class MyError(Exception):

... def __init__(self, message):
... Exception.__init__(self)
... self.msg = message
...

e = MyError('msg')


Beyond odd.  A bug?

Alan Isaac

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


Problems with hex-conversion functions

2009-09-04 Thread Arnon Yaari
Hello everyone.
Perhaps I'm missing something, but I see several problems with the two
hex-conversion function pairs that Python offers:
1. binascii.hexlify and binascii.unhexlify
2. bytes.fromhex and bytes.hex

Problem #1:
bytes.hex is not implemented, although it was specified in PEP 358.
This means there is no symmetrical function to accompany
bytes.fromhex.

Problem #2:
Both pairs perform the same function, although The Zen Of Python
suggests that
"There should be one-- and preferably only one --obvious way to do
it."
I do not understand why PEP 358 specified the bytes function pair
although it mentioned the binascii pair...

Problem #3:
bytes.fromhex may receive spaces in the input string, although
binascii.unhexlify may not.
I see no good reason for these two functions to have different
features.

Problem #4:
binascii.unhexlify may receive both input types: strings or bytes,
whereas bytes.fromhex raises an exception when given a bytes
parameter.
Again there is no reason for these functions to be different.

Problem #5:
binascii.hexlify returns a bytes type - although ideally, converting
to hex should always return string types and converting from hex
should always return bytes.
IMO there is no meaning of bytes as an output of hexlify, since the
output is a representation of other bytes.
This is also the suggested behavior of bytes.hex in PEP 358


Problems #4 and #5 call for a decision about the input and output of
the functions being discussed:

Option A : Strict input and output
unhexlify (and bytes.fromhex) may only receives string and may only
return bytes
hexlify (and bytes.hex) may only receives bytes and may only return
strings

Option B : Robust input and strict output
unhexlify (and bytes.fromhex) may receive bytes and strings and may
only return bytes
hexlify (and bytes.hex) may receive bytes or strings and may only
return strings

Of course we may also consider a third option, which will allow the
return type of all functions to be robust (perhaps specified in a
keyword argument), but as I wrote in the description of problem #5, I
see no sense in that.

Note that PEP 3137 describes: "... the more strict definitions of
encoding and decoding in Python 3000: encoding always takes a Unicode
string and returns a bytes sequence, and decoding always takes a bytes
sequence and returns a Unicode string." - suggesting option A.

To repeat problems #4 and #5, the current behavior does not match any
option:
* The return type of binascii.hexlify should be string, and this is
not the current behavior.
As for the input:
* Option A is not the current behavior because binascii.unhexlify may
receive both input types.
* Option B is not the current behavior because bytes.fromhex does not
allow bytes as input.


To fix these issues, three changes should be applied:
1. Deprecate bytes.fromhex. This fixes the following problems:
   #4 (go with option B and remove the function that does not allow
bytes input)
   #2 (the binascii functions will be the only way to "do it")
   #1 (bytes.hex should not be implemented)
2. In order to keep the functionality that bytes.fromhex has over
unhexlify,
   the latter function should be able to handle spaces in its input
(fix #3)
3. binascii.hexlify should return string as its return type (fix #5)


Any thoughts are appreciated.
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: incorrect DeprecationWarning?

2009-09-04 Thread Terry Reedy

Alan G Isaac wrote:
Python 2.6.2 (r262:71605, Apr 14 2009, 22:40:02) [MSC v.1500 32 bit 
(Intel)] on win32

Type "help", "copyright", "credits" or "license" for more information.

class MyError(Exception):

... def __init__(self, message):
... Exception.__init__(self)
... self.message = message
...

e = MyError('msg')
__main__:4: DeprecationWarning: BaseException.message has been 
deprecated as of Python 2.6



So?  Why would that mean I cannot add such an attribute
to derived classes?


It does not mean that. Try printing e.message and you should see 'msg'.
I believe what it does mean is the the special meaning of 
exception.message (I have forgotten what it is) is gone in Python 3.


In Py3
class MyError(Exception):
 def __init__(self, message):
 Exception.__init__(self)
 self.message = message

e = MyError('msg')
print(e.message)

# 'msg'

No warning any more.


Beyond odd.  A bug?


No. A warning about a 'future' change in behavior.

tjr

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


  1   2   >