Re: Issues installing Python 2.7

2014-11-27 Thread Steven D'Aprano
On Wed, 26 Nov 2014 09:09:30 -0800, billyfurlong wrote:

> ./configure --prefix=/opt/python2.7 --enable-shared

Do you need to install to /opt instead of the default of 
/usr/local/bin? I have multiple versions of Python installed on a Red Hat 
system (Centos, but RH should be the same) using the default prefix of 
/usr/local/bin and don't have any trouble.


I recommend you try again with just:


./configure
make
make altinstall


then add an alias to your .bashrc 

alias python=python2.7


and see how that works for you.


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


Re: I love assert

2014-11-27 Thread TP
On Tue, Nov 11, 2014 at 11:40 AM, Peter Cacioppi 
wrote:

> I get the impression that most Pythonistas aren't as habituated with
> assert statements as I am. Is that just a misimpression on my part? If not,
> is there a good reason to assert less with Python than other languages?
>
> As far as I can tell, Python supports assert perfectly well. When run with
> the optimization flagging, the asserts are truly removed.
>
> I think one needs to take care with some basic assert coding - it's not a
> substitute for unit tests, it doesn't absolve you of normal exception
> responsibilities, and, most of all, it should be used for passive
> inspection and not action. But given these guidelines, I still find it very
> useful as "active comments".
>

In "Why Most Unit Testing is Waste" by James O Coplien [1] he says on page
10:

"However, as with most unit tests, it's better to make this an assertion
than to pepper your test framework with such checks."

page 15:

"When I look at most unit tests -- especially those written with JUnit --
they are assertions in disguise. When I write a great piece of software I
sprinkle it with assertions that describe promises that I expect the
callers of my functions to live up to, as well as promises that function
makes to its clients. Those assertions evolve in the same artefact as the
rest of my code."

page 16:

"An even more professional approach is to leave the assertions in the code
when you ship, and to automatically file a bug report on behalf of the end
user and perhaps to try to re-start the application every time an assertion
fails."

"Turn unit tests into assertions. Use them to feed your fault-tolerance
architecture on high-availability systems. This solves the problem of
maintaining a lot of extra software modules that assess execution and check
for correct behavior; that's one half of a unit test. The other half is the
driver that executes the code: count on your stress tests, integration
tests, and system tests to do that."

And in "Seque" by James O Coplien [2] he is even more emphatic on page 17:

"This argumentation, of course, is just an alternative justification for
the use of assertions in the code, as we already introduced in Chapter 1.
Assertions are powerful unit-level guards that beat most unit tests in two
ways. First, each one can do the job of a large number (conceivably,
infinite) of scenario-based unit tests that compare computational results
to an oracle. Second, they extend the run time of the test over a much
wider range of contexts and detailed scenario variants by extending the
test into the lifetime of the product."

[1] http://www.rbcs-us.com/documents/Why-Most-Unit-Testing-is-Waste.pdf

[2] http://www.rbcs-us.com/documents/Segue.pdf
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: I love assert

2014-11-27 Thread alister
On Thu, 27 Nov 2014 01:22:37 -0800, TP wrote:

> On Tue, Nov 11, 2014 at 11:40 AM, Peter Cacioppi
> 
> wrote:
> 
>> I get the impression that most Pythonistas aren't as habituated with
>> assert statements as I am. Is that just a misimpression on my part? If
>> not,
>> is there a good reason to assert less with Python than other languages?
>>
>> As far as I can tell, Python supports assert perfectly well. When run
>> with the optimization flagging, the asserts are truly removed.
>>
>> I think one needs to take care with some basic assert coding - it's not
>> a substitute for unit tests, it doesn't absolve you of normal exception
>> responsibilities, and, most of all, it should be used for passive
>> inspection and not action. But given these guidelines, I still find it
>> very useful as "active comments".
>>
>>
> In "Why Most Unit Testing is Waste" by James O Coplien [1] he says on
> page 10:
> 
> "However, as with most unit tests, it's better to make this an assertion
> than to pepper your test framework with such checks."
> 
> page 15:
> 
> "When I look at most unit tests -- especially those written with JUnit
> --
> they are assertions in disguise. When I write a great piece of software
> I sprinkle it with assertions that describe promises that I expect the
> callers of my functions to live up to, as well as promises that function
> makes to its clients. Those assertions evolve in the same artefact as
> the rest of my code."
> 
> page 16:
> 
> "An even more professional approach is to leave the assertions in the
> code when you ship, and to automatically file a bug report on behalf of
> the end user and perhaps to try to re-start the application every time
> an assertion fails."
> 
> "Turn unit tests into assertions. Use them to feed your fault-tolerance
> architecture on high-availability systems. This solves the problem of
> maintaining a lot of extra software modules that assess execution and
> check for correct behavior; that's one half of a unit test. The other
> half is the driver that executes the code: count on your stress tests,
> integration tests, and system tests to do that."
> 
> And in "Seque" by James O Coplien [2] he is even more emphatic on page
> 17:
> 
> "This argumentation, of course, is just an alternative justification for
> the use of assertions in the code, as we already introduced in Chapter
> 1.
> Assertions are powerful unit-level guards that beat most unit tests in
> two ways. First, each one can do the job of a large number (conceivably,
> infinite) of scenario-based unit tests that compare computational
> results to an oracle. Second, they extend the run time of the test over
> a much wider range of contexts and detailed scenario variants by
> extending the test into the lifetime of the product."
> 
And as may wiser people than me have already highlighted Assertions can 
be switched off in python which means they cannot be relied upon in 
production code invalidating the authors suggestion that they can 
automate bug reports & "Extend testing into the lifetime of the product"
 

-- 
The state of some commercial Un*x is more unsecure than any Linux box
without a root password...
-- Bernd Eckenfels
-- 
https://mail.python.org/mailman/listinfo/python-list


Db transactions and locking

2014-11-27 Thread Frank Millman
Hi all

I just learned something about database transactions, locking, and DB-API 
2.0.

I wondered why a PostgreSQL statement was hanging. On investigation, it was 
waiting to acquire a lock. On further investigation, the lock was held by a 
simple SELECT statement. This surprised me.

I got a clue from a message on the PostgreSQL mailing list -

"PostgreSQL by default commits between each statement unless you explicitly 
start a transaction."

All Python database adaptors that I have used start a transaction when you 
open a cursor. I have just re-read DB-API 2.0, and I cannot see anything 
that specifies this behaviour, but AFAICT this is what happens.

I started to monitor the locks that PostgreSQL holds while I was running my 
application, and sure enough, the lock table grew and grew, even though I 
was only issuing SELECTs. I use a connection pool, so I never actually close 
the connections. If I did I am fairly sure the locks would be released.

I changed my application to call conn.commit() every time I return a 
connection back to the pool, and PostgreSQL shows all the locks being 
released straight away, so I think this has solved the problem.

I don't know if Sql Server and sqlite3 behave the same, but I don't think it 
can do any harm, so I let it apply across the board.

Can anyone see any problem with this?

Frank Millman



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


Re: Db transactions and locking

2014-11-27 Thread Chris Angelico
On Thu, Nov 27, 2014 at 9:24 PM, Frank Millman  wrote:
> "PostgreSQL by default commits between each statement unless you explicitly
> start a transaction."
>
> All Python database adaptors that I have used start a transaction when you
> open a cursor. I have just re-read DB-API 2.0, and I cannot see anything
> that specifies this behaviour, but AFAICT this is what happens.

The default is probably for backward compatibility, but automatically
opening a transaction is the better way. (When I used DB2, back in the
90s, that was the only way anything was ever done. There was no such
thing as autocommit.) Assume that everything you do is inside a
transaction; that's how it's going to be on the back-end anyway. For
example, PostgreSQL's MVCC is controlled by transaction numbers; any
row with a transaction number greater than the viewing transaction's
is invisible. That means that even a SELECT statement must, by
definition, be in a transaction.

One easy way to use psycopg2 - though I don't know if this is the best way - is:

with conn, conn.cursor() as cur:
cur.execute(...)

Exit the with block and you close off the transaction and release
cursor resources.

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


Re: Fwd: Python Signal/Slot + QThred code analysis

2014-11-27 Thread Juan Christian
On Thu Nov 27 2014 at 2:12:40 AM Michael Torrie  wrote:
Hmm, I hit a wall. There's no main.ui file. Can you rework your code
so that you have a single file (plus a separate ui file that's okay),
that simulates the url request, that I can execute easily.

As you asked, here it's, everything on one module and without network
calls, http://pastebin.com/ibS9FSSd

The main.ui is a XML, http://pastebin.com/Eq9hNMBX , just save it as
main.ui and it will work. I think now you have everything in order to test
the app.

What I have in mind is simple (I think), have the Outpost get the data
using QThread, and when it got everything needed emit a signal. So when I
have something like 'var = Outpost('123')', when I get the signal I'll know
that I can call 'var.trades, var.posts' and it won't give me NullErrors or
something like that.
-- 
https://mail.python.org/mailman/listinfo/python-list


Asyncio problem, looking for advice.

2014-11-27 Thread Benjamin Risher
Hello all,

I'm working on a project to learn asyncio and network programming.  What I'm 
trying to do is forward a connection from myself to another machine.  Kind of 
like an asynchronous python implementation of fpipe.  

In a nutshell:

1 --> start a server listening on localhost
2 --> connect to server 
3 --> server connects to a listening service (ssh for instance)
4 --> server handles both connections to pass traffic back and forth through 
each

What I have now *kind of* works.  It sends data back and forth, but when I ssh 
to localhost -p 12345, i never get the password prompt.  It looks like one 
packet hangs out and doesn't get sent from what I saw in tcpdump.  

Any help would be greatly appreciated.  


Here's a link to the same code as below, just with syntax highlighting etc...
http://pastebin.com/iLE4GZH3


import asyncio
import logging


class MyServer:
def __init__(self, loop):
self.server = None
self.loop = loop
self.clients = dict()
self.log = logging.getLogger(__name__)

def _accept_client(self, client_reader, client_writer):
""" Client initially drops in here """
task = asyncio.Task(self._handle_client(client_reader, client_writer))
self.clients[task] = (client_reader, client_writer)

def client_done(task):
self.clients[task][1].close()  # closes the StreamWriter that was 
part of the task
del self.clients[task]

self.log.info("New connection.")
task.add_done_callback(client_done)

@asyncio.coroutine
def _handle_client(self, client_reader, client_writer):
""" Try to connect to port 22 and broker between 2 connections.

:param client_reader: StreamReader object
:param client_writer: StreamWriter object
"""
reader, writer = yield from 
asyncio.async(asyncio.open_connection('localhost', 22))

while True:
client_data = yield from 
asyncio.async(self.read_data(client_reader))
if client_data:
writer.write(client_data)
yield from writer.drain()

server_data = yield from asyncio.async(self.read_data(reader))
if server_data:
client_writer.write(server_data)
yield from client_writer.drain()


@asyncio.coroutine
def read_data(self, reader):
data = None

while True:
print('top of while')
data = yield from asyncio.Task(reader.read(2048))
return data

def start(self):
""" start the server listening on 12345 """

self.server = self.loop.run_until_complete(
asyncio.streams.start_server(
self._accept_client, # client_connected_callback
'localhost', # host
12345,   # port
loop=self.loop   # loop
)
)

def stop(self):
if self.server:
self.server.close()
self.loop.run_until_complete(self.server.wait_closed())
self.server = None

if __name__ == '__main__':
loop = asyncio.get_event_loop()
my_server = MyServer(loop)
my_server.start()
loop.run_forever()
my_server.stop()
-- 
https://mail.python.org/mailman/listinfo/python-list


Where is path.py?

2014-11-27 Thread David Aldrich
Hi

I am running Python 2.7 on Windows 8.1. Today I installed path.py using 
easy_install:

easy_install path.py

The example code that I've seen imports path.py as follows:

from path import path

I am fairly new to Python and have a few questions about this:

1) Why is 'from path' required here?

2) I am using Microsoft's PTVS IDE to develop my code. It is not displaying 
code completion for path.py (it works for the standard libraries). How would I 
tell PTVS about path.py? 

Perhaps I just need to know where path.py is as I can't find it in my 
filesystem.

Best regards

David

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


Re: [ANN] Pylint 1.4 released

2014-11-27 Thread Ned Batchelder

On 11/23/14 4:45 AM, Claudiu Popa wrote:

Hello!

On behalf of the Pylint development team, I'm happy to announce that
Pylint 1.4 has been released.

This release has a lot of improvements over the last one. One of the
main differences is that support for Python versions < 2.7 has been
droped, which allows us to support Python 2.7 and 3.3+ from a single
codebase.


Other important changes:


BTW: it would be great to include a link to the full list of changes, 
wherever they might be.




  * A new spelling checker (disabled by default).

If you find any bugs, don't hesitate to open a new issue on our issue tracker.


I found a problem with the new spell feature, but the issue tracker 
(http://www.bytebucket.org/logilab/pylint/issues) seems broken: 
everything I tried ended at a 403 CSRF validation failure page.


Is there another way to report problems?

--
Ned Batchelder, http://nedbatchelder.com

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


Re: Where is path.py?

2014-11-27 Thread MRAB

On 2014-11-27 14:10, David Aldrich wrote:

Hi

I am running Python 2.7 on Windows 8.1. Today I installed path.py
using easy_install:

easy_install path.py

The example code that I've seen imports path.py as follows:

from path import path

I am fairly new to Python and have a few questions about this:

1) Why is 'from path' required here?


The syntax:

from  import 

will import the name  from the module , so:

from path import path

will import the name 'path' (a class) from the module 'path'.


2) I am using Microsoft's PTVS IDE to develop my code. It is not
displaying code completion for path.py (it works for the standard
libraries). How would I tell PTVS about path.py?

Perhaps I just need to know where path.py is as I can't find it in my
filesystem.


Try:

import path
print path.__file__
--
https://mail.python.org/mailman/listinfo/python-list


Re: Where is path.py?

2014-11-27 Thread Dave Angel

On 11/27/2014 09:10 AM, David Aldrich wrote:

Hi

I am running Python 2.7 on Windows 8.1. Today I installed path.py using 
easy_install:

easy_install path.py

The example code that I've seen imports path.py as follows:

from path import path

I am fairly new to Python and have a few questions about this:

1) Why is 'from path' required here?



The module called "path" evidently has a global symbol "path" in it. 
That's unfortunate and very confusing, but it's pretty common.  (See the 
time function in the time module, standard library.)  I don't know 
anything about the module, but I'll make a wild guess that path is a 
function.  It doesn't matter, but it'll let me make explicit examples.


So the obvious approach to dealing with a module is to import it.

import path

At that point, to call the above mentioned function, you would need to use:

result =  path.path(arguments)

That gets confusing, so you might want a shortcut:

pathfunc = path.path

and now you can use

result =  pathfunc(arguments)

The "from" logic facilitates that, by combining the import with the 
assignment:  The line:


from path import path
is equivalent to
import path;  path = path.path


I don't know where path.py is, and it might be called something else, 
such as path.pyc, or path.pyd, or ...  But you can probably search for 
it, if you like, with something like


dir /s  c:\path.p*

I am assuming you're running on Windows, because you mention a Microsoft 
IDE.  If you were on Linux or Unix or Mac, you'd presumably use the find 
program.


>
> 2) I am using Microsoft's PTVS IDE to develop my code. It is not 
displaying code completion for path.py (it works for the standard 
libraries). How would I tell PTVS about path.py?



No clue about Microsoft's PTVS IDE;  I hadn't heard of it before.  But 
if somebody else doesn't jump in, you should be able to use the Python
  dir() and help() built-in functions.  Then only work if the path 
module is well written, of course.


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


RE: Where is path.py?

2014-11-27 Thread David Aldrich
> The syntax:

>from  import 

> will import the name  from the module , so:

> from path import path

> will import the name 'path' (a class) from the module 'path'.

Thanks.  But I don't quite understand.  If I use sys:

import sys

args = sys.argv[1:]

I don't need to use 'from'.  What is different with using path?

>> Perhaps I just need to know where path.py is as I can't find it in my
>> filesystem.

>Try:

>import path
>print path.__file__

c:\Python27\lib\site-packages\path.py-7.0-py2.7.egg\path.pyc

Where would I typically put that path in an IDE?

Thanks for your help.

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


Re: Where is path.py?

2014-11-27 Thread Chris Angelico
On Fri, Nov 28, 2014 at 2:30 AM, David Aldrich
 wrote:
> Thanks.  But I don't quite understand.  If I use sys:
>
> import sys
>
> args = sys.argv[1:]
>
> I don't need to use 'from'.  What is different with using path?

You could instead say:

from sys import argv
args = argv[1:]

The only confusing bit here is that instead of "sys" and "argv", you
have "path" and "path", the same name twice. But it's the same thing
happening.

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


RE: Where is path.py?

2014-11-27 Thread David Aldrich
> The only confusing bit here is that instead of "sys" and "argv", you have
> "path" and "path", the same name twice. But it's the same thing happening.

Thanks very much for all replies I received for my question. It's clearer now.

By the way, for Windows users, I do recommend Microsoft's PTVS (Python Tools 
for Visual Studio). It's an excellent IDE and free if you use the Express 
Edition of Visual Studio.

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


Re: [Pylint-dev] [ANN] Pylint 1.4 released

2014-11-27 Thread Claudiu Popa
On Thu, Nov 27, 2014 at 5:19 PM, Ned Batchelder  wrote:
> On 11/23/14 4:45 AM, Claudiu Popa wrote:
>>
>> Hello!
>>
>> On behalf of the Pylint development team, I'm happy to announce that
>> Pylint 1.4 has been released.
>>
>> This release has a lot of improvements over the last one. One of the
>> main differences is that support for Python versions < 2.7 has been
>> droped, which allows us to support Python 2.7 and 3.3+ from a single
>> codebase.
>>
>>
>> Other important changes:
>
>
> BTW: it would be great to include a link to the full list of changes,
> wherever they might be.

Indeed. We don't have right now a full list of changes, except the
changelog for 1.4:
https://bitbucket.org/logilab/pylint/src/ede9e9ebc2557f768e129f29aeb365608ae96ea2/ChangeLog?at=default#cl-4

>
>>
>>   * A new spelling checker (disabled by default).
>>
>> If you find any bugs, don't hesitate to open a new issue on our issue
>> tracker.
>
>
> I found a problem with the new spell feature, but the issue tracker
> (http://www.bytebucket.org/logilab/pylint/issues) seems broken: everything I
> tried ended at a 403 CSRF validation failure page.
>
> Is there another way to report problems?
>

Hm, isn't it bitbucket.org? Never encountered bytebucket.org until now.
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: [code-quality] [ANN] Pylint 1.4 released

2014-11-27 Thread Julien Cristau
On Thu, Nov 27, 2014 at 10:19:28 -0500, Ned Batchelder wrote:

> I found a problem with the new spell feature, but the issue tracker
> (http://www.bytebucket.org/logilab/pylint/issues) seems broken:
> everything I tried ended at a 403 CSRF validation failure page.
> 
Not sure where that url came from, afaik the tracker is
https://bitbucket.org/logilab/pylint/issues

Cheers,
Julien
-- 
Julien Cristau  
Logilab http://www.logilab.fr/
Informatique scientifique & gestion de connaissances
-- 
https://mail.python.org/mailman/listinfo/python-list


How is max supposed to work, especially key.

2014-11-27 Thread Albert van der Horst
In the Rosetta code I come across this part of
LU-decomposition.

def pivotize(m):
"""Creates the pivoting matrix for m."""
n = len(m)
ID = [[float(i == j) for i in xrange(n)] for j in xrange(n)]
for j in xrange(n):
row = max(xrange(j, n), key=lambda i: abs(m[i][j]))
if j != row:
ID[j], ID[row] = ID[row], ID[j]
return ID

That it's using a cast from boolean to float and using
at the other moment a float as a boolean, suggest that this
code is a bit too clever for its own good, but anyway.

My problem is with the max. I never saw a max with a key.

In my python help(max) doesn't explain the key. It says that
max can handle an iterator (I didn't know that), and you can
pass and optional "key=func", but that's all.

I expect it to be something like
  elements in the iterator are taken into account only if the
  key applied to the iterator evaluates to a True value.

However that doesn't pan out:
"
max(xrange(100,200), key=lambda i: i%17==0 )
102
"

I expect the maximum number that is divisible by 17 in the
range, not the minimum.

Can anyone shed light on this?

Groetjes Albert
-- 
Albert van der Horst, UTRECHT,THE NETHERLANDS
Economic growth -- being exponential -- ultimately falters.
albert@spe&ar&c.xs4all.nl &=n http://home.hccnet.nl/a.w.m.van.der.horst

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


Re: [ANN] PyUBL

2014-11-27 Thread Burak Arslan

On 11/26/14 08:53, dieter wrote:
> Burak Arslan  writes:
>> We've gone through the grunt work of researching and integrating
>> XMLDSIG, XAdES and UBL schemas and its various extensions and
>> dependencies and wrote a bunch of scripts that map these documents to
>> python objects.
> In this context, I would like to mention "PyXB"
> ("https://pypi.python.org/pypi/PyXB";). It is a general mapper
> for XML-Schema.

Well, Spyne is a general-purpose object mapper. It maps python objects
to xml documents and back using a given xml schema as well as to json,
yaml, etc. etc. and also to relational databases (via sqlalchemy).

I didn't know PyXB and I'm sure its way better than Spyne at parsing
esoteric Xml Schema documents, but in this case Spyne seems to be doing
a good enough job.

Best,
Burak

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


Re: How is max supposed to work, especially key.

2014-11-27 Thread Peter Otten
Albert van der Horst wrote:

> In the Rosetta code I come across this part of
> LU-decomposition.
> 
> def pivotize(m):
> """Creates the pivoting matrix for m."""
> n = len(m)
> ID = [[float(i == j) for i in xrange(n)] for j in xrange(n)]
> for j in xrange(n):
> row = max(xrange(j, n), key=lambda i: abs(m[i][j]))
> if j != row:
> ID[j], ID[row] = ID[row], ID[j]
> return ID
> 
> That it's using a cast from boolean to float and using
> at the other moment a float as a boolean, suggest that this
> code is a bit too clever for its own good, but anyway.
> 
> My problem is with the max. I never saw a max with a key.
> 
> In my python help(max) doesn't explain the key. It says that
> max can handle an iterator (I didn't know that), and you can
> pass and optional "key=func", but that's all.
> 
> I expect it to be something like
>   elements in the iterator are taken into account only if the
>   key applied to the iterator evaluates to a True value.
> 
> However that doesn't pan out:
> "
> max(xrange(100,200), key=lambda i: i%17==0 )
> 102
> "
> 
> I expect the maximum number that is divisible by 17 in the
> range, not the minimum.
> 
> Can anyone shed light on this?

Given a function f() max(items, key=f) returns the element of the `items` 
sequence with the greatest f(element), e. g. for

max(["a", "bcd", "ef"], key=len)

the values 1, 3, 2 are calculated and the longest string in the list is 
returned:

>>> max(["a", "bcd", "ef"], key=len)
'bcd'

If there is more than one item with the maximum calculated the first is 
given, so for your attempt

max(xrange(100,200), key=lambda i: i%17==0 )

the values False, False, True, False, ... are calculated and because

>>> True > False
True

the first one with a True result is returned.

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


Re: Db transactions and locking

2014-11-27 Thread Ian Kelly
On Nov 27, 2014 4:26 AM, "Frank Millman"  wrote:
> All Python database adaptors that I have used start a transaction when you
> open a cursor. I have just re-read DB-API 2.0, and I cannot see anything
> that specifies this behaviour, but AFAICT this is what happens.

I don't know how others work, but cx_Oracle starts a transaction when you
execute a statement, not when you open a cursor.
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: [Pylint-dev] [code-quality] [ANN] Pylint 1.4 released

2014-11-27 Thread Michal Nowikowski
Hello,

Just submitted pull request with a fix:
https://bitbucket.org/logilab/pylint/pull-request/205/fixed-reading-list-of-ignored-words-for/diff

Regards,
Godfryd

On Thu, Nov 27, 2014 at 7:00 PM, Ned Batchelder 
wrote:

> On 11/27/14 11:59 AM, Julien Cristau wrote:
>
>> On Thu, Nov 27, 2014 at 10:19:28 -0500, Ned Batchelder wrote:
>>
>>  I found a problem with the new spell feature, but the issue tracker
>>> (http://www.bytebucket.org/logilab/pylint/issues) seems broken:
>>> everything I tried ended at a 403 CSRF validation failure page.
>>>
>>>  Not sure where that url came from, afaik the tracker is
>> https://bitbucket.org/logilab/pylint/issues
>>
>>
> Hmm, not sure where bytebucket came from either! Sorry for the noise...
>
>  Cheers,
>> Julien
>>
>>
>
> --
> Ned Batchelder, http://nedbatchelder.com
>
> ___
> Pylint-dev mailing list
> pylint-...@lists.logilab.org
> http://lists.logilab.org/mailman/listinfo/pylint-dev
>
-- 
https://mail.python.org/mailman/listinfo/python-list


Logging with unittest

2014-11-27 Thread Jared Windover
I've been working with unittest for a while and just started using logging, and 
my question is: is it possible to use logging to display information about the 
tests you're running, but still have it be compatible with the --buffer option 
so that you only see it if a test fails? It seems like it would be a useful 
(and more standard than print statements) way to include stuff like parameter 
values near where a test may fail. 

What I currently have happening is that the logging output is shown regardless 
of whether or not the -b (or --buffer) flag is present. Any thoughts?
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Db transactions and locking

2014-11-27 Thread Chris Angelico
On Fri, Nov 28, 2014 at 5:02 AM, Ian Kelly  wrote:
> On Nov 27, 2014 4:26 AM, "Frank Millman"  wrote:
>> All Python database adaptors that I have used start a transaction when you
>> open a cursor. I have just re-read DB-API 2.0, and I cannot see anything
>> that specifies this behaviour, but AFAICT this is what happens.
>
> I don't know how others work, but cx_Oracle starts a transaction when you
> execute a statement, not when you open a cursor.

Is there any material difference here? I mean, sure, you have a
transaction, it'll show up in logs and stuff, but without any locks,
it's unlikely to have much impact on the system. Unless you're
creating and destroying a bunch of unnecessary cursors all the time,
of course, but that'd be wasteful for other reasons.

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


Re: Issues installing Python 2.7

2014-11-27 Thread Chris Angelico
On Fri, Nov 28, 2014 at 4:40 AM, Dennis Lee Bieber
 wrote:
> On Thu, 27 Nov 2014 11:53:10 +1100, Steven D'Aprano
>  declaimed the following:
>
>>billyfurl...@gmail.com wrote:
>
>>
>>> Add this to the bashrc
>>> export PATH=$PATH:/opt/python2.7/bin/
>>
>>I'm not so sure about that, but I don't have time to investigate right now.
>>
> If this is for the user login (and where it won't conflict with the
> system startup processes) I'd have swapped the $PATH and /opt... order --
> putting the 2.7 bin directory ahead of the system bin directory.

I wouldn't. Do you know, for absolute certain, which programs you run
that end up calling on Python, perhaps via "#!/usr/bin/env python", or
perhaps by being shell scripts and running "python my_main_module.py",
or something? You'd break those. Unless, of course, you're making
absolutely sure there's no "python" command in the /opt/... directory,
in which case the PATH order won't have any semantic difference, and
directory caching will make sure there's virtually no performance
difference, so the current form is just as good.

That said, I have never compiled a Python 2, ever. I've always
compiled Py3, and let that happily take over the name "python3",
leaving the system Python 2.x on the name "python" and "python2".

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


Re: Fwd: Python Signal/Slot + QThred code analysis

2014-11-27 Thread Michael Torrie
On 11/27/2014 04:58 AM, Juan Christian wrote:
> What I have in mind is simple (I think), have the Outpost get the data
> using QThread, and when it got everything needed emit a signal. So when I
> have something like 'var = Outpost('123')', when I get the signal I'll know
> that I can call 'var.trades, var.posts' and it won't give me NullErrors or
> something like that.

So there are a number of logic problems in your code.  The main one is
that the call to create your worker class returns immediately.  It's not
part of the thread itself.  So your signal logic will never work.

This was a good excuse for me to dive into Qt a bit more.  So it turns
out that QThread objects can have their own event loops. This turns out
to be quite ideal for what you want.  So here's a Worker class does
everything you want, but with only one instance, and uses signals properly:

class Worker(QThread):

# This is the signal that we'll emit every time
# we load a new set of values from the url
new_value = Signal(str,str)

def __init__(self, url, *args, **kwargs):
super(Worker, self).__init__(*args, **kwargs)
self.url = url
self.start()

def run(self):
# Since we're going to run a mini event loop in the thread
# we can create and use a timer here
self.timer = QTimer()
self.timer.start(5000)
self.timer.timeout.connect(self.fetch_url)
# Start the thread's event loop
self.exec_()

def fetch_url(self):
# Doesn't matter how long this takes because it only
# blocks the thread's event loop, not the GUI
time.sleep(random.random() * 2)
trades = str(random.random() * 25)
posts = str(random.random() * 100)

# communicate the values via the signal
self.new_value.emit(trades, posts)

So with this, you can instantiate it once, then tie the new_value signal
to a callback in the main loop.  So no need for the Outlook object, and
no intermediate callbacks.  The thread signals directly.  And the
callback receives the data directly, so no check for NODATA values and
such:

def create_topic(trades, posts):
box = QGroupBox()
grid = QGridLayout()
nickname = QTextEdit()

box.setFixedHeight(200)
nickname.setText("%s : %s" % (trades, posts))

grid.addWidget(nickname)
box.setLayout(grid)

# Access global variable MainWindow, defined below
MainWindow.vlay.addWidget(box)

Hope this helps.  Here's complete working code, minus the .ui file:
http://pastebin.com/VhmSFX2t


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


Re: Fwd: Python Signal/Slot + QThred code analysis

2014-11-27 Thread Juan Christian
On Thu Nov 27 2014 at 8:53:16 PM Michael Torrie  wrote:
Hope this helps. Here's complete working code, minus the .ui file:
http://pastebin.com/VhmSFX2t

Thanks, I'll just repost the code on pastebin with a NEVER expire time and
UNLISTED, so that it can be of some help for others here in the mailist:
http://pastebin.com/9Jrmw4VL

I'll read the code thoroughly and reply to you if I find something strange,
many thanks!
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Fwd: Python Signal/Slot + QThred code analysis

2014-11-27 Thread Juan Christian
On Thu Nov 27 2014 at 9:16:38 PM Juan Christian 
wrote:
I'll read the code thoroughly and reply to you if I find something strange,
many thanks!


So, instantly I found one issue, you said that this code won't block the
GUI, only the thread event loop, but if we keep moving the window while
it's open, every time new info is printed the GUI freezes for like 1-2
seconds.

And why this approach of a single instance is better? I mean, I'll have to
call Outpost every time I get new data from my other API, because there
will be different users, so I need to create different instances so I call
the site with a new ID, don't I?

Maybe the answer for that question is that you using a timer that is ALWAYS
active and ALWAYS calling the the outpost site, so I'll have something like:

var = Outpost('12345')
var.posts -> 100
var.setID('67893')
var.posts -> 35

Is that right? But I don't think that always calling the site this way is
good for them and for me, sometimes I may not get new users for like 10~15
minutes, so I would have wasted a lot of calls for nothing. That's why I
only instantiate/call the site when I get new users.
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Fwd: Python Signal/Slot + QThred code analysis

2014-11-27 Thread Michael Torrie
On 11/27/2014 04:29 PM, Juan Christian wrote:
> So, instantly I found one issue, you said that this code won't block the
> GUI, only the thread event loop, but if we keep moving the window while
> it's open, every time new info is printed the GUI freezes for like 1-2
> seconds.

Correct.  The thread does not block the GUI.  I cannot replicate your
problem on my machine here. Maybe we're finding a problem with Windows
and Qt.  I can drag the window around and it keeps on a going.

> And why this approach of a single instance is better? I mean, I'll have to
> call Outpost every time I get new data from my other API, because there
> will be different users, so I need to create different instances so I call
> the site with a new ID, don't I?

Just seems wasteful, that's all.  Why not instantiate Outpost from the
worker thread?  Especially if your thread only does one thing every 5
seconds.  Starting up a new thread and destroying seems like a waste.
But it certainly can be done that way.

> Maybe the answer for that question is that you using a timer that is ALWAYS
> active and ALWAYS calling the the outpost site, so I'll have something like:
> 
> var = Outpost('12345')
> var.posts -> 100
> var.setID('67893')
> var.posts -> 35
> 
> Is that right? But I don't think that always calling the site this way is
> good for them and for me, sometimes I may not get new users for like 10~15
> minutes, so I would have wasted a lot of calls for nothing. That's why I
> only instantiate/call the site when I get new users.

I was simply using the logic you originally provided. I have no idea
what Outpost does or how you use it.  Your original code just grabbed
some data every 5 seconds, so that's what I got it to do in the most
efficient way.  You'll have to adapt it to your real needs.

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


Re: Fwd: Python Signal/Slot + QThred code analysis

2014-11-27 Thread Michael Torrie
On 11/27/2014 04:29 PM, Juan Christian wrote:
> Is that right? But I don't think that always calling the site this way is
> good for them and for me, sometimes I may not get new users for like 10~15
> minutes, so I would have wasted a lot of calls for nothing. That's why I
> only instantiate/call the site when I get new users.

So the real problem with instantiating a worker thread for every request
is that you have to hold onto a reference to the worker long enough for
the work to be done.  Your original code was not doing this, so had it
worked, the thread would have been aborted before it even started its
work, as soon as the worker instance went out of scope.

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


Can you use self in __str__

2014-11-27 Thread Seymore4Head
def __str__(self):
s = "Hand contains "
for x in self.hand:
s = s + str(x) + " "
return s

This is part of a Hand class.  I need a hand for the dealer and a hand
for the player.
dealer=Hand()
player=Hand()
This prints out 'Hand contains " foo bar 
for both the dealer's hand and the player's hand.

Is there a way to include "self" in the __string__ so it reads
Dealer hand contains foo bar
Player hand contains foo bar

I tried:
s  = self + "Hand contains "  and that doesn't work.
s = self + s + str(x) + " "  doesn't work either
Line 60: TypeError: cannot concatenate 'str' and 'Hand' objects

I tried:
s = self.hand + s + str(x) + " "
Line 60: TypeError: can only concatenate list to list
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Can you use self in __str__

2014-11-27 Thread Dave Angel

On 11/27/2014 08:26 PM, Seymore4Head wrote:

 def __str__(self):
 s = "Hand contains "
 for x in self.hand:
 s = s + str(x) + " "
 return s

This is part of a Hand class.  I need a hand for the dealer and a hand
for the player.
dealer=Hand()
player=Hand()


Good so far. But you neglect to tell us Python version.


This


What's "this" ?  Please include real code, not just vague suggestions.


prints out 'Hand contains " foo bar
for both the dealer's hand and the player's hand.

Is there a way to include "self" in the __string__ so it reads
Dealer hand contains foo bar
Player hand contains foo bar


You don't show us the __init__ method.  That's where you should 
establish a "name" for the particular instance of Hand.


class Hand(object):
def __init__(self, name):
self.name = name
self.hand =   something ...

def __str__(self):
  s = self.name + " contains "
  for x in self.hand:
  s = s + str(x) + " "
  return s


When you quote an error, you should be including the whole thing, as 
well as showing the code that triggers it.  In this message you're 
giving us only disjoint fragments, and one line summary of the traceback.


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


Re: Can you use self in __str__

2014-11-27 Thread Shiyao Ma
2014-11-28 9:26 GMT+08:00 Seymore4Head :
> def __str__(self):
> s = "Hand contains "
> for x in self.hand:
> s = s + str(x) + " "
> return s
>
> This is part of a Hand class.  I need a hand for the dealer and a hand
> for the player.
> dealer=Hand()
> player=Hand()
> This prints out 'Hand contains " foo bar
> for both the dealer's hand and the player's hand.
>
> Is there a way to include "self" in the __string__ so it reads
> Dealer hand contains foo bar
> Player hand contains foo bar

I bet you want the object name (aka, dealer, player) be included in
the string 's'.
To that end, you need to access the namespace where 'self' is in.
But I dunno where the namespace 'self' resides in.
Does PyObject has a field to store the namespace of an object?
Appreciated if anyone could
inform me on this.

Now, make a little assumption that the instance lives in the module
level. Then we can do
this:

#!/usr/bin/env python

class Hand(object):
def __init__(self):
self.hand = [1, 2, 3, 4]

def __str__(self):
s = self._myname + " hand contains "
for x in self.hand:
s = s + str(x) + " "
return s

@property
def _myname(self):
# get the module
mod = self.__module__
import sys
ns = vars(sys.modules[mod])
# NB only works the instance is at the module level
for name, obj in ns.iteritems():
if id(obj) == id(self):
break
else:
#nothing found
return ""
return name

John = Hand()
print(John)

# this prints
# John hand contains 1 2 3 4

bad indentation with my wasavi plugin, see paste:

https://bpaste.net/show/f5b86957295f


What if it's in the local namespace of a function or method? IDK, try
to get that thing first.


Regards






--

吾輩は猫である。ホームーページはhttp://introo.me。
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Can you use self in __str__

2014-11-27 Thread Seymore4Head
On Thu, 27 Nov 2014 21:49:29 -0500, Dave Angel 
wrote:

class Hand:
def __init__(self):
self.hand = []
# create Hand object

def __str__(self):
s = 'Hand contains '
for x in self.hand:
s = s + str(x) + " "
return s

I am using 2.7 (Codeskulptor).  This is working code.  It starts with
an empty list that gets appended from a full deck of shuffled cards.
dealer=Hand()
player=Hand()
I don't really know how to post working code without posting a lot.  I
am not being too successful in trying to post enough code to have it
work without posting the entire code.
Here is the link if you want to run it.
http://www.codeskulptor.org/#user38_Kka7mh2v9u_9.py
The print out looks like this:
Hand contains H4 DQ.

I can (and am) currently printing the hand like this:
print "Player's",player
print "Dealer's",dealer

My question is can you add (self) in the __str__ so when you issue the
command "print player" the "player" part is included in the __str__.
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Can you use self in __str__

2014-11-27 Thread Seymore4Head
On Fri, 28 Nov 2014 11:04:26 +0800, Shiyao Ma  wrote:

>2014-11-28 9:26 GMT+08:00 Seymore4Head :
>> def __str__(self):
>> s = "Hand contains "
>> for x in self.hand:
>> s = s + str(x) + " "
>> return s
>>
>> This is part of a Hand class.  I need a hand for the dealer and a hand
>> for the player.
>> dealer=Hand()
>> player=Hand()
>> This prints out 'Hand contains " foo bar
>> for both the dealer's hand and the player's hand.
>>
>> Is there a way to include "self" in the __string__ so it reads
>> Dealer hand contains foo bar
>> Player hand contains foo bar
>
>I bet you want the object name (aka, dealer, player) be included in
>the string 's'.

That is exactly what I want, but your explanation is too complicated
for my feeble mind to get just yet.

>To that end, you need to access the namespace where 'self' is in.
>But I dunno where the namespace 'self' resides in.
>Does PyObject has a field to store the namespace of an object?
>Appreciated if anyone could
>inform me on this.
>
>Now, make a little assumption that the instance lives in the module
>level. Then we can do
>this:
>
>#!/usr/bin/env python
>
>class Hand(object):
>def __init__(self):
>self.hand = [1, 2, 3, 4]
>
>def __str__(self):
>s = self._myname + " hand contains "
>for x in self.hand:
>s = s + str(x) + " "
>return s
>
>@property
>def _myname(self):
># get the module
>mod = self.__module__
>import sys
>ns = vars(sys.modules[mod])
># NB only works the instance is at the module level
>for name, obj in ns.iteritems():
>if id(obj) == id(self):
>break
>else:
>#nothing found
>return ""
>return name
>
>John = Hand()
>print(John)
>
># this prints
># John hand contains 1 2 3 4
>
>bad indentation with my wasavi plugin, see paste:
>
>https://bpaste.net/show/f5b86957295f
>
>
>What if it's in the local namespace of a function or method? IDK, try
>to get that thing first.
>
>
>Regards
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Fwd: Python Signal/Slot + QThred code analysis

2014-11-27 Thread Michael Torrie
On 11/27/2014 04:29 PM, Juan Christian wrote:
> Is that right? But I don't think that always calling the site this way is
> good for them and for me, sometimes I may not get new users for like 10~15
> minutes, so I would have wasted a lot of calls for nothing. That's why I
> only instantiate/call the site when I get new users.

Okay, here's a reworking of the code that invokes a new QThread instance
each time.  Note the QThread instance has to be bound to the MainWindow
so that it won't be destroyed when it goes out of scope.  Also the
Worker thread sends a signal with the data, so there's no need to check
worker.trades and risk it being invalid data.

Perhaps this would be more what you had in mind.

You may want to look into asynchronous I/O as well.  That does not
require threads at all.  Fire off a request to load a url, and then get
a callback when it's done.

Anyway, here's the code (too lazy to post it to pastebin):

import time
import random
from PySide.QtCore import QObject, QThread, Signal, QFile, QTimer, Qt
from PySide.QtGui import QGroupBox, QGridLayout, QTextEdit, QApplication
from PySide.QtUiTools import QUiLoader


class Worker(QThread):

# This is the signal that we'll emit every time
# we load a new set of values from the url
new_value = Signal(QThread, str,str)

def __init__(self, url, *args, **kwargs):
super(Worker, self).__init__(*args, **kwargs)
self.url = url
self.start()

def run(self):
# Doesn't matter how long this takes because it only
# blocks the thread's event loop, not the GUI
print ('Starting work...')
time.sleep(random.random() * 2)
trades = str(random.random() * 25)
posts = str(random.random() * 100)
print ('Finished work.')

# communicate the values via the signal
# we have to pass self so that the callback
# can properly clean up after the thread.
self.new_value.emit(self, trades, posts)

def loadui(file_name):
loader = QUiLoader()
uifile = QFile(file_name)
uifile.open(QFile.ReadOnly)
ui = loader.load(uifile)
uifile.close()
return ui



if __name__ == "__main__":
import sys

# Putting this in here because it requires access to MainWindow
# which only exists if this part of the code runs.
def create_topic(worker, trades, posts):
box = QGroupBox()
grid = QGridLayout()
nickname = QTextEdit()

box.setFixedHeight(200)
nickname.setText("%s : %s" % (trades, posts))

grid.addWidget(nickname)
box.setLayout(grid)

# Access global variable MainWindow, defined below
MainWindow.vlay.addWidget(box)

# clean up the worker to prevent resource leak.
worker.quit()
worker.wait()

def output_slot():
# in order for the thread to stay running, we have to bind it to
the
# Main Window, which we
worker = Worker('http://www.tf2outpost.com/user /' + id64,
MainWindow)
worker.new_value.connect(create_topic)

app = QApplication(sys.argv)

MainWindow = loadui("main.ui")
MainWindow.vlay.setAlignment(Qt.AlignTop)
timer = QTimer()
timer.start(5000)
timer.timeout.connect(output_slot)
id64 = '123'

MainWindow.show()
app.exec_()


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


Why is Smiling a Donation?

2014-11-27 Thread bv4bv4bv4
Why is Smiling a Donation?
 
(And smiling to your brother is donation). This is what the greatest Prophet 
(All Prayers and Peace of Allah be upon him) said and this is what the latest 
researches are discovering, so let's read 
Researchers have studied about smiling influence on others. They have found 
that smiling contains strong information that can influence the human 
subconscious mind!
They found that everyone has his particular smile that no one can share with 
him. Moreover, every smile contains special effect, too. They photographed 
these smiles and showed it slowly, hence, they noticed some specific movements. 
Likewise, a person can have more than one kind of smiles, regarding his mental 
status, what is he speaking about and the person he is speaking to...
 
Some of the most important information of such researches, researches are 
talking about what you can give to others through the smile, because it is more 
than giving a material thing for the following reasons:
When smiling you can transmit joy to others, which is a sort of donation and 
which can be the most important. Studies showed that sometimes man may need joy 
and gladness more than food and drink. Also, that joy can treat many diseases 
starting by heart problems.
Through the smile you can transmit information to others very easily, because a 
word with a smile has more influence on the brain. Magnetic resonance imaging 
has shown that expression influence changes too much with a smile. The 
expression is the same , however, the influenced brain parts differ according 
to smile type accompanying this information or this expression.
You can calm an atmosphere stain of a given situation with a soft smile. This 
cannot be done with money. Hence, smile is worthier and more important than 
money. So, the less you can give to others is smile donation.
Smile and recovery: Many doctors have noticed smile influence on recovery. 
Therefore, some researches declare that the doctor's smile is a part of the 
treatment! Then, giving a smile to your friend, your wife or your neighbor, you 
are giving them a free remedy prescription without feeling, and this is a kind 
of donation.
For these reasons and others, smile is considered as a sort of donation, giving 
and generosity. And now dear reader, do you realize why did the prophet of 
compassion (All Prayers and Peace of Allah be upon him)  say: (and smiling to 
your brother is donation)!!

By: Abduldaem Al-Kaheel
www.kaheel7.com/eng
http://www.kaheel7.com/eng/index.php/legislative-miracles/193-why-is-smiling-a-donation-
 
Source:
·  Smile -- And The World Can Hear You, Even If You Hide, 
www.sciencedaily.com, Jan. 16, 2008.
 
Thank you
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Can you use self in __str__

2014-11-27 Thread Chris Angelico
On Fri, Nov 28, 2014 at 2:04 PM, Shiyao Ma  wrote:
> What if it's in the local namespace of a function or method? IDK, try
> to get that thing first.

What if it's in multiple namespaces? What if it's not in any at all?
Your solution is not going to work in the general case, AND it's a bad
idea. Please don't do this in any code that I'll ever have to
maintain.

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


Re: html page mail link to webmail program

2014-11-27 Thread Akira Li
Ethan Furman  writes:

> On 11/11/2014 05:08 PM, Ben Finney wrote:
>> Ethan Furman  writes:
>>
>>> My wife (using a Win7 machine) will be on a web page that has a link
>>> to mail somebody.  She clicks on it, and it opens the currently
>>> installed but unused Thunderbird.
>>>
>>> Ideally, what would happen is a new window/tab would open to gmail
>>> with a new compose window with the email address in place and the
>>> cursor in the subject line.
>>
>> What is the Python question? I can't see anywhere that you would be
>> using Python code to address this.
>
> Really?  Huh.
>
> Okay, the explicit Python question:  Clicking on a mail link in a web
> browser can start an external program.  I would like that external
> program to be a Python script that: opens a new tab in the currently
> running browser (or a new default browser window), loads up the
> default web mail client (or one specified if there is no way to
> know/have a default), navigates to the compose pane (or starts there
> if possible), enters in the email address from the link that was
> passed to it, and, if not too much more, move the cursor to the
> subject field.
>
> Surely this can be done in Python.
>
Related question:
http://stackoverflow.com/questions/14288177/interact-with-other-programs-using-python


--
Akira

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


Re: Db transactions and locking

2014-11-27 Thread Frank Millman

"Dennis Lee Bieber"  wrote in message 
news:4loe7at2ls7tfq0oe041ru9svvsm8ak...@4ax.com...
> On Thu, 27 Nov 2014 12:24:39 +0200, "Frank Millman" 
> declaimed the following:
>
>
>>All Python database adaptors that I have used start a transaction when you
>>open a cursor. I have just re-read DB-API 2.0, and I cannot see anything
>>that specifies this behaviour, but AFAICT this is what happens.
>>
>
> Really?
>

Well, I can't prove it, no, but *something* starts a transaction, even if 
you do not specify one.

Maybe the adaptor detects the first statement after opening a cursor, and 
starts a transaction at that point.

Here is my empirical 'proof' -

I start up a PostgreSQL interactive session with psql, and list the current 
locks - there are 3, which always seem to be there.

>From another psql session, I issue some sql commands. Here is a list of the 
commands, followed by the number of current locks.

SELECT * FROM mytable - 3
BEGIN - 3
SELECT * FROM mytable - 4 (a new AccessShareLock on mytable)
COMMIT - 3

This confirms what I posted earlier - "PostgreSQL by default commits between 
each statement unless you explicitly start a transaction."

Then I start a python session, set up a connection using psycopg2, and do 
the same.

cur = conn.cursor() - 3
cur.execute('SELECT * FROM mytable') - 4
cur.fetchall() - 4
cur.close() - 4
conn.commit() - 3

This seems to confirm what I thought, but then I continued, and was 
surprised at the result.

I can repeat these lines at will -

cur.execute('SELECT * FROM mytable') - 4
conn.commit() - 3

But if I do this -

cur.execute('SELECT * FROM mytable') - 4
cur.execute('commit') - 3

cur.execute('SELECT * FROM mytable') - 3
cur.execute('commit') - 3

There seems to be a difference between conn.commit() and 
cur.execute('commit'), which leaves the connection in a different state.

However, for my purposes, this is academic.

The main lesson I have learned is that you should always issue a commit 
after any logical set of SQL statements, even if they are only SELECTs, 
otherwise the locks are not released.

Frank



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


Re: Curious function argument

2014-11-27 Thread Akira Li
"ast"  writes:

> Hello
>
> I saw in a code from a previous message in this forum
> a curious function argument.
>
> def test(x=[0]):
>   print(x[0])   ## Poor man's object
>   x[0] += 1
>
 test()
> 0
 test()
> 1
 test()
> 2

>
> I understand that the author wants to implement a global
> variable x . It would be better to write 'global x' inside the
> function.
>
> At first test() function call, it prints 0, that's OK.
> But at the second call, since we dont pass any argument to test(),
> x should be equal to its default value [0] (a single item list). But
> it seems that Python keeps the original object whose content has
> been changed to 1.
>
> Is it a usual way to implement a global variable ?
>
It is not a global, a global would be easily available (visible) to
other functions. You can get it if you try but Python also allows you to
get private attributes, replace builtins and other things that you
should not normally do.

*"# Poor man's object"* in this context suggests that I'm the
author. The code is meant as a part of a short Python script that is
executed directly. Imagine the *test()* definition is put inside main()
function that is called when the script is run.

`def test(*, _x=[0]):..` attaches some state (_x list) to the function
*test*. It could be used as a way to emulate *nonlocal* keyword:

  def counter():
  count = 0
  def increment():
  nonlocal count
  count += 1
  return count
  return increment
  
  c = counter()
  print(c(), c()) # 0, 1
  c = counter()
  print(c(), c()) # 0, 1

an alternative is to create an object using class:

  class Counter:
  def __init__(self):
  self.count = 0
  def __call__(self):
  self.count += 1
  return self.count

  c = Counter()
  print(c(), c())
  
For this specific case, you could use `itertools.count`:

  c = itertools.count()
  print(next(c), next(c))

which could be implemented here as a generator:

  def count():
  count = 0
  while True:
 count += 1  
 yield count

  c = count()
  print(next(c), next(c))

"Objects are data with methods attached, closures are functions with data
attached." [1]

Sometimes you need to define a class to create objects, in other cases a
function is enough (and of course functions are objects too in Python).

[1]
http://stackoverflow.com/questions/13857/can-you-explain-closures-as-they-relate-to-python


--
Akira


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


Re: Db transactions and locking

2014-11-27 Thread Chris Angelico
On Fri, Nov 28, 2014 at 4:44 PM, Frank Millman  wrote:
> There seems to be a difference between conn.commit() and
> cur.execute('commit'), which leaves the connection in a different state.

Yes, if a connection library offers a way to commit/roll back, it's
usually best to use that.

> However, for my purposes, this is academic.
>
> The main lesson I have learned is that you should always issue a commit
> after any logical set of SQL statements, even if they are only SELECTs,
> otherwise the locks are not released.

Correct. Every time you use any database, you should think in terms of
units of work, which correspond precisely to transactions.

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


Re: Db transactions and locking

2014-11-27 Thread Frank Millman

"Frank Millman"  wrote in message 
news:m5924d$nbq$1...@ger.gmane.org...
>
>
> This seems to confirm what I thought, but then I continued, and was 
> surprised at the result.
>
> I can repeat these lines at will -
>
> cur.execute('SELECT * FROM mytable') - 4
> conn.commit() - 3
>
> But if I do this -
>
> cur.execute('SELECT * FROM mytable') - 4
> cur.execute('commit') - 3
>
> cur.execute('SELECT * FROM mytable') - 3
> cur.execute('commit') - 3
>
> There seems to be a difference between conn.commit() and 
> cur.execute('commit'), which leaves the connection in a different state.
>

On reflection, this makes sense, and also 'proves' that my initial theory of 
the adaptor starting a transaction on opening a cursor must be wrong.

My guess now is that the connection is in one of two states - a transaction 
is active, or it is not. If a command is issued, and a transaction is not 
active, then a transaction is started. If a conn.commit() or a 
conn.rollback() is issued, the command is passed up to the database, and the 
connection state is reset to not active.

cur.execute('commit') tells the database to commit the transaction, but the 
adaptor is not aware of this, so does not reset. Therefore the next command 
does not trigger starting a new transaction.

I have now learned another lesson - never use cur.execute('commit'), always 
use conn.commit()

Frank



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


Re: Db transactions and locking

2014-11-27 Thread Chris Angelico
On Fri, Nov 28, 2014 at 5:57 PM, Frank Millman  wrote:
> cur.execute('commit') tells the database to commit the transaction, but the
> adaptor is not aware of this, so does not reset. Therefore the next command
> does not trigger starting a new transaction.
>
> I have now learned another lesson - never use cur.execute('commit'), always
> use conn.commit()

Without looking in the source I can't be sure, but that seems very logical.

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


Re: Can you use self in __str__

2014-11-27 Thread Steven D'Aprano
Seymore4Head wrote:

> On Fri, 28 Nov 2014 11:04:26 +0800, Shiyao Ma  wrote:
> 
>>2014-11-28 9:26 GMT+08:00 Seymore4Head :
>>> def __str__(self):
>>> s = "Hand contains "
>>> for x in self.hand:
>>> s = s + str(x) + " "
>>> return s
>>>
>>> This is part of a Hand class.  I need a hand for the dealer and a hand
>>> for the player.
>>> dealer=Hand()
>>> player=Hand()
>>> This prints out 'Hand contains " foo bar
>>> for both the dealer's hand and the player's hand.
>>>
>>> Is there a way to include "self" in the __string__ so it reads
>>> Dealer hand contains foo bar
>>> Player hand contains foo bar
>>
>>I bet you want the object name (aka, dealer, player) be included in
>>the string 's'.
> 
> That is exactly what I want, but your explanation is too complicated
> for my feeble mind to get just yet.


Shiyao Ma's explanation is too complicated for *my* feeble mind to get, and
I've been using Python for over 15 years. Either I have no idea what Ma is
trying to say, or Ma has no idea what (s)he's trying to say.

:-)


>>To that end, you need to access the namespace where 'self' is in.

`self` is a local variable of the method. Accessing the local namespace is
automatic.


-- 
Steven

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