[Tutor] QT Python: How to re-use the return value of fileDialog.openFileName() ?

2013-05-26 Thread Sunitha Misra
tutor@python.org

Hello,
I am trying to write a UI for my utility, using QT-Python. I first launch a
GUI and click on a toolButton to open up the directory structure, from
which I select a particular file.

self.fileDialog = QtGui.QFileDialog()

QtCore.QObject.connect(self.toolButton,
QtCore.SIGNAL(_fromUtf8("clicked()")), self.fileDialog.getOpenFileName)

So far so good.

How do I store the return value of getOpenFileName?

For example, if I do the following, it gives error:
QtCore.QObject.connect(self.toolButton,
QtCore.SIGNAL(_fromUtf8("clicked()")),
(filepath=self.fileDialog.getOpenFileName))

The reason I want to store the return value is because I want to capture
the filename/path that getOpenFilename returned in a string so I can copy
it to a lineEdit space as below:

self.lineEdit.setText(filepath)

text = self.lineEdit.text()

(If I do the following, it will re-lauch the directory list), which I don't
want.
self.lineEdit.setText(self.fileDialog.getOpenFileName())

Thanks in advance.
SM
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] making a string

2013-05-26 Thread Steven D'Aprano

On 27/05/13 10:43, Jim Mooney wrote:

On 26 May 2013 17:38, eryksun  wrote:

On Sun, May 26, 2013 at 8:20 PM, Steven D'Aprano  wrote:

On 27/05/13 07:40, Jim Mooney wrote:


Good to know that compile doesn't check syntax, since I erroneously
thought it did.


compile does check syntax.


Attempting to iterate an integer is a runtime TypeError, not a
compile-time SyntaxError.


Ah, that clears it up.



Like most dynamic languages, Python's compiler checks very few things at 
compile time. In general, there is little that Python *can* check, not without 
actually running the code, so even when when Python *could* detect an error at 
compile time, it usually doesn't bother because the programming effort is too 
high for the benefit.



--
Steven
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] keyboard interrupt

2013-05-26 Thread Jim Mooney
>> Bad programming advice is bad enough, but putting up bad advice to
>> edit the Windows registry is Really, Really bad.
>
> Did you leave a comment with the correction?

Good point. I wasn't registered on that board but I should go find it.
Not that the mistype in that case could possibly cause a registry
crash. It just didn't work since a backslash in the path to
windows\shellnew was missing, probably due to typing too fast.

-- 
Jim Mooney

There are those who see.
Those who see when they are shown.
And those who do not see.
 -- Leonardo da Vinci
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] making a string

2013-05-26 Thread Jim Mooney
On 26 May 2013 17:38, eryksun  wrote:
> On Sun, May 26, 2013 at 8:20 PM, Steven D'Aprano  wrote:
>> On 27/05/13 07:40, Jim Mooney wrote:
>>
>>> Good to know that compile doesn't check syntax, since I erroneously
>>> thought it did.
>>
>> compile does check syntax.
>
> Attempting to iterate an integer is a runtime TypeError, not a
> compile-time SyntaxError.

Ah, that clears it up.

Jim
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] making a string

2013-05-26 Thread eryksun
On Sun, May 26, 2013 at 8:20 PM, Steven D'Aprano  wrote:
> On 27/05/13 07:40, Jim Mooney wrote:
>
>> Good to know that compile doesn't check syntax, since I erroneously
>> thought it did.
>
> compile does check syntax.

Attempting to iterate an integer is a runtime TypeError, not a
compile-time SyntaxError.
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] making a string

2013-05-26 Thread Jim Mooney
On 26 May 2013 17:20, Steven D'Aprano  wrote:
> On 27/05/13 07:40, Jim Mooney wrote:
>
>> Good to know that compile doesn't check syntax, since I erroneously
>> thought it did.
>
>
> compile does check syntax.

I'm unclear on something. The code below creates bytecode and I don't
see an error message unless I also tack exec code on the end of it. It
has the dumb error of trying to iterate an integer.

from dis import dis

code = compile(
'''a, b = 5, 8
print a
print b
for x in a:
print x
''','', 'exec'
)

dis(code)

Result below - bytecode but no error message
Python 2.7.5 (default, May 15 2013, 22:43:36) [MSC v.1500 32 bit (Intel)]
Type "help", "copyright", "credits" or "license" for more information.
[evaluate derek.py]
  1   0 LOAD_CONST   3 ((5, 8))
  3 UNPACK_SEQUENCE  2
  6 STORE_NAME   0 (a)
  9 STORE_NAME   1 (b)

  2  12 LOAD_NAME0 (a)
 15 PRINT_ITEM
 16 PRINT_NEWLINE

  3  17 LOAD_NAME1 (b)
 20 PRINT_ITEM
 21 PRINT_NEWLINE

  4  22 SETUP_LOOP  19 (to 44)
 25 LOAD_NAME0 (a)
 28 GET_ITER
>>   29 FOR_ITER11 (to 43)
 32 STORE_NAME   2 (x)

  5  35 LOAD_NAME2 (x)
 38 PRINT_ITEM
 39 PRINT_NEWLINE
 40 JUMP_ABSOLUTE   29
>>   43 POP_BLOCK
>>   44 LOAD_CONST   2 (None)
 47 RETURN_VALUE

If I addexec codeto the end of that program I Do get an error,
after it prints a and b, but that's separate from the compile
statement:

exec code
Traceback (most recent call last):
  File "C:\Program Files (x86)\Wing IDE 101
4.1\src\debug\tserver\_sandbox.py", line 13, in 
  File "C:\Program Files (x86)\Wing IDE 101
4.1\src\debug\tserver\_sandbox.py", line 4, in 
#print 'Exiting sandbox process'
TypeError: 'int' object is not iterable
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] keyboard interrupt

2013-05-26 Thread eryksun
On Sun, May 26, 2013 at 8:25 PM, Jim Mooney  wrote:
>
> StackOverflow may be good but I just had an unpleasant experience
> wanting to add New .py file to my Windows context menu. The first
> advice I saw was missing a backslash and had me adding the string to
> the wrong key. Thankfully, it didn't work and I figured out what was
> wrong, so I now have New .py file ;')
>
> Bad programming advice is bad enough, but putting up bad advice to
> edit the Windows registry is Really, Really bad.

Did you leave a comment with the correction?
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] keyboard interrupt

2013-05-26 Thread Jim Mooney
On 26 May 2013 15:33, Marc Tompkins  wrote:
> On Sun, May 26, 2013 at 6:17 AM, eryksun  wrote:

StackOverflow may be good but I just had an unpleasant experience
wanting to add New .py file to my Windows context menu. The first
advice I saw was missing a backslash and had me adding the string to
the wrong key. Thankfully, it didn't work and I figured out what was
wrong, so I now have New .py file ;')

Bad programming advice is bad enough, but putting up bad advice to
edit the Windows registry is Really, Really bad.
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] making a string

2013-05-26 Thread Steven D'Aprano

On 27/05/13 07:40, Jim Mooney wrote:


Good to know that compile doesn't check syntax, since I erroneously
thought it did.


compile does check syntax.


py> compile("23 = 43", "", "exec")
Traceback (most recent call last):
  File "", line 1, in 
  File "", line 1
SyntaxError: can't assign to literal


The compile built-in function does the same as the Python compiler that 
compiles your code before you run it. It has three modes:

- "exec", which operates in the same way that Python compiles a module;

- "single", which operates in the same way that Python compiles a single 
statement in the interactive interpreter; and

- "eval", which tells Python that the code must be a single expression and not 
a statement or a block of code.



--
Steven
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] keyboard interrupt

2013-05-26 Thread Marc Tompkins
On Sun, May 26, 2013 at 6:17 AM, eryksun  wrote:

> On Sun, May 26, 2013 at 4:34 AM, Steven D'Aprano 
> wrote:
> >
> > So these edits aren't default-deny, but default-accept? Worse and worse.
>
> It shows who made the edit and when they edited it, which links to the
> revision history. When a question is closed it shows who voted to
> close it. Even retagging shows up in the revision history. Also, since
> it requires a rep of 2000 to edit, generally the system isn't abused.
> Lower-rep users can suggest an edit, but that goes through a review
> process.
>
> You're notified when someone edits your answer, and you can roll it
> back to a previous version. However, too many edits causes your post
> to become community wiki (I think 10 edits by the owner or edits by 5
> different users). An answer marked community wiki will no longer earn
> reputation from up votes, and users with a rep of only 100 have edit
> privileges.
>

Once again: I think that the end result (high-quality answers to relevant
questions) justifies a LOT of unpleasantness along the way; probably eight
times ought of ten that I Google a programming question, the best (and
often the first) answer ends up being on StackOverflow.  In general, the
process leads to good quality.  It just, unfortunately, lends itself to
reputation-gaming, and to useless users appearing far more prominent than
they should.  The actual damage that those parasites are able to do to the
ecosystem is limited, however... except that otherwise-motivated users
sometimes get irritated and drop out.
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] making a string

2013-05-26 Thread Mark Lawrence

On 26/05/2013 22:40, Jim Mooney wrote:

On 26 May 2013 02:51, Mark Lawrence  wrote:

Basically no.  Python 2.7 is guaranteed to be backward compatible with
Python 2.6.  New or improved functionality will be listed in the "What's New
for Python 2.7".  In fact if you look at the "What's New for Python 3.3"
you'll find all of the "What's New" going back to Python 2.0.  See this
http://docs.python.org/3/whatsnew/index.html.


That's a relief. I started with Py 3.3, realized a lot of stuff wasn't
there for it yet, regressed to 2.7, but still write "input" instead of
"raw_input" now and then, producing an error I think is mine until I
see what I did ;')  "raw_input" is such an awkwardness for a very
common use, that I'm surprised it was there in the first place.

Incidentally, I was figuring how to use compile for multi line
statements since the example I saw was a single line, compiled a small
multi-line routine nicely, and realized I had just compiled a  bad
syntax error. I was trying to iterate an integer.

Good to know that compile doesn't check syntax, since I erroneously
thought it did.



You might like to know that it's possible to write code that's usable 
with both Python 2 and Python 3, see this for starters 
https://pypi.python.org/pypi/six/1.3.0.


--
If you're using GoogleCrap™ please read this 
http://wiki.python.org/moin/GoogleGroupsPython.


Mark Lawrence

___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] making a string

2013-05-26 Thread Jim Mooney
On 26 May 2013 02:51, Mark Lawrence  wrote:
> Basically no.  Python 2.7 is guaranteed to be backward compatible with
> Python 2.6.  New or improved functionality will be listed in the "What's New
> for Python 2.7".  In fact if you look at the "What's New for Python 3.3"
> you'll find all of the "What's New" going back to Python 2.0.  See this
> http://docs.python.org/3/whatsnew/index.html.

That's a relief. I started with Py 3.3, realized a lot of stuff wasn't
there for it yet, regressed to 2.7, but still write "input" instead of
"raw_input" now and then, producing an error I think is mine until I
see what I did ;')  "raw_input" is such an awkwardness for a very
common use, that I'm surprised it was there in the first place.

Incidentally, I was figuring how to use compile for multi line
statements since the example I saw was a single line, compiled a small
multi-line routine nicely, and realized I had just compiled a  bad
syntax error. I was trying to iterate an integer.

Good to know that compile doesn't check syntax, since I erroneously
thought it did.

-- 
Jim Mooney

There are those who see.
Those who see when they are shown.
And those who do not see.
 -- Leonardo da Vinci
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] keyboard interrupt

2013-05-26 Thread eryksun
On Sun, May 26, 2013 at 4:34 AM, Steven D'Aprano  wrote:
>
> So these edits aren't default-deny, but default-accept? Worse and worse.

It shows who made the edit and when they edited it, which links to the
revision history. When a question is closed it shows who voted to
close it. Even retagging shows up in the revision history. Also, since
it requires a rep of 2000 to edit, generally the system isn't abused.
Lower-rep users can suggest an edit, but that goes through a review
process.

You're notified when someone edits your answer, and you can roll it
back to a previous version. However, too many edits causes your post
to become community wiki (I think 10 edits by the owner or edits by 5
different users). An answer marked community wiki will no longer earn
reputation from up votes, and users with a rep of only 100 have edit
privileges.
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] making a string

2013-05-26 Thread Mark Lawrence

On 26/05/2013 05:10, Jim Mooney wrote:

On 25 May 2013 20:49, Tim Hanson  wrote:


A lot of people tend to be intimidated by Mark Lutz, and so am I, I guess.


Interesting coincidence. This is a retirement project and I just
decided on the Lutz book, which looked comprehensive, since the book
I'm using is more CompSci but PyDeficient. However, the 4th edition I
of Lutz that I got only goes up to Py 2.6.

As a general question - is there any Big difference between 2.6 and
2.7 (the Py version I'm using) that I should look out for so I don't
get tripped up?

Jim Mooney


Basically no.  Python 2.7 is guaranteed to be backward compatible with 
Python 2.6.  New or improved functionality will be listed in the "What's 
New for Python 2.7".  In fact if you look at the "What's New for Python 
3.3" you'll find all of the "What's New" going back to Python 2.0.  See 
this http://docs.python.org/3/whatsnew/index.html.


--
If you're using GoogleCrap™ please read this 
http://wiki.python.org/moin/GoogleGroupsPython.


Mark Lawrence

___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Flip the coin 10x and count heads and tails: It works now!

2013-05-26 Thread Todd Matsumoto
May I suggest running randint using 0, 1. The results can be tested like a
boolean. See what happens when you do an if test on 1 versus an if test on
0.

Also perhaps store your results in a dictionary with 'heads', 'tails' each
value set to 0.


On Sat, May 25, 2013 at 3:13 PM, Steven D'Aprano wrote:

> On 25/05/13 19:25, Rafael Knuth wrote:
>
>  flips = 0
>> heads = 0
>> tails = 0
>>
>> while flips < 10:
>>  flips = flips + 1
>>
>
> Don't do this. It's not 1971 any more, we've had for loops for forty years
> :-)
>
> Use while loops when you don't know how many loops you need. When you know
> how many loops you will have, use a for loop.
>
> for flip in range(10):
> print(flip)
>
> will print 0 1 2 3 ... 9 which makes ten loops altogether. If you want to
> start counting at 1 instead of 0, use:
>
> for flip in range(1, 11):
> print(flip)
>
> which will print 1 2 3 ... 9 10.
>
>
> There's no need to count the number of heads and tails separately, since
> every loop is guaranteed to give either head or tails. So just count the
> number of heads, then the number of tails will be ten less the number of
> heads.
>
>
>
>
> --
> Steven
>
> __**_
> Tutor maillist  -  Tutor@python.org
> To unsubscribe or change subscription options:
> http://mail.python.org/**mailman/listinfo/tutor
>



-- 
Todd Matsumoto
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] keyboard interrupt

2013-05-26 Thread Steven D'Aprano

On 26/05/13 17:57, Marc Tompkins wrote:

On Fri, May 24, 2013 at 7:19 PM, Steven D'Aprano wrote:

You can edit *other* people's questions and answers??!??!??


What. The. Hell.



The idea is to build an authoritative information resource (in particular,
the goal is that the accepted answer to any given question will become the
primary result for someone Googling that same question.)  So it makes very
good sense that both questions and answers can be edited for quality, and
the result is (mostly) good.


That's all very well and good when you're dealing with a system like Wikipedia, 
where edits belong to the entire community, not the person who merely made the 
edit. But Stackoverflow is specifically in the form of Question/Answer, where 
both questions and answers are labelled as belonging to the person who made 
them. Stackoverflow has the form of a conversation, with questions and 
*replies*. How can you judge the quality of a response when you cannot be sure 
that the question you are reading is the same question that was answered? Your 
reputation depends on the relevance of your reply. Change the question, and 
your perfectly sensible, helpful reply may look like an idiot's waffling:


Q: How do I sort a list without using any built-in functions or methods?
A: Start at this Wikipedia page, where many different sort-algorithms are 
listed.


# Question gets edited.
Q: How do I count the number of 0's, 1's and 2's in a list without using any 
built-in functions or methods?
A: Start at this Wikipedia page, where many different sort-algorithms are 
listed.


And now I look like a first degree moron. By the way, I am not making this 
scenario up. Read this thread starting here:

http://mail.python.org/pipermail/python-list/2013-May/647400.html



My objection is to people gaming the system - making imperceptible edits
like dashes-to-em-dashes - in order to juice their scores.


So these edits aren't default-deny, but default-accept? Worse and worse.




--
Steven
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Fwd: Difference between types

2013-05-26 Thread Steven D'Aprano

On 25/05/13 02:52, Citizen Kant wrote:

When I say "coding", anyone can think about what coding is
in his own daily work, but that's not my way.
I'll try to refine the concept: right now I'm learning,
if I say "coding" I refer to what I type inside
my file named learningpythoncode.py that,
believe it or not, starts with:

# """After the hash can go my comment.
That's the first thing I've learned, and it's useful since
this way I don't feel like as don't knowing any language
at all. This is part of the code that Python doesn't
compute, but this isn't less code.

Since I am using the triple quotation, I can continue:


That actually doesn't work. Because you start the first line with a hash mark # 
Python treats that line as a comment, including the triple-quote marks. That 
means that the next line will give you a SyntaxError:


py> # """This is not a triple-string
... even though it starts with a """
  File "", line 2
even though it starts with a """
  ^
SyntaxError: invalid syntax


When you use # for comments, each line must begin with a # mark.

Alternatively, you can use strings as pseudo-comments, including triple-quoted 
(multi-line) strings. Using strings is special. If the first line of code in a module, or 
a class, or a function, is a string, it gets turned into a "docstring" 
(documentation string). Python copies it into a special __doc__ attribute of the module 
(or class, or function) where it can be accessed as documentation.

Otherwise, a bare string has no effect in your code, since it's just data, 
never touched, never used, it doesn't do anything. That makes it a def facto 
comment. In fact, the standard, reference implementation of Python, (so-called) 
CPython, treats such bare strings as comments and drops them from the code when 
compiling it. (But even if it didn't, it would still be an effective comment, 
since it has no effect.)

[...]

Then I consider to code another value, like A, that looks like if
cannot be reduced by rewriting any further but I arrived
to the fact that Python doesn't consider those characters like A
as able to take part in its computations until I put them between
apostrophes or quotation marks."""


Not at all. If you actually try it, you will see that Python *can* use a 
character like A as part of its computations. It is a *name*, and when you 
refer to it, Python tries to look up the value of A and fails, so you get an 
error:

py> A
Traceback (most recent call last):
  File "", line 1, in 
NameError: name 'A' is not defined


Python even tells you *exactly* what the problem is, if you read the error 
message:

- it is an error to do with names, NameError;

- specifically the name A is not defined, so there is no value to retrieve.


So we can fix this:

py> A = 9
py> A
9


This is where the English language is ambiguous. If I talk about A, am I 
talking about the *name* A or the *value* of A (in this case, 9)? The 
convention *when writing English* is to say that referring to A alone means we 
care about the value, and if we want to refer to the name specifically, we put 
it in quotation marks, 'A'.

This is unfortunately still ambiguous, since it isn't clear whether 'A' refers 
to the name or the letter of the alphabet, or perhaps quoting somebody. But in 
context, it is usually clear.

By analogy: consider the difference between the current Queen of England, and the hereditary 
position of Queen of England. The "value" (a person) of "the Queen of England" 
is currently Her Majesty Elizabeth II, an actual person, but the position itself is more abstract, 
and has been held by many different people, such as Queen Elizabeth I and Queen Victoria, and 
sometimes not held at all.

(When the ruling monarch is a man, his wife, if any, is the Queen Consort, not 
the Queen. And the widow of a deceased King is the Dowager Queen, or in the 
case of the late mother to Queen Elizabeth, Queen Elizabeth the Queen Mother. 
And of course the rules are different in other countries. Aren't titles fun?)

If Great Britain becomes a republic and abolishes the monarchy, she would 
presumably either take the surname of her husband, and become Elizabeth 
Mountbatten, or she would take on the name of the Royal House, and become 
Elizabeth Windsor.

To put this in Python terms, we might decide that the name "queen" was to refer 
to the current Queen of England. So:


# Elizabeth ascends to the throne.
queen = "Elizabeth Alexandra Mary"

# Should she abdicate or die, there is no longer a
# ruling queen, but the position remains.
queen = None

# If Great Britain abolishes the monarchy,
# even the position goes away.
liz_mountbatten = queen
del queen


So we can talk about the *name* (variable) "queen", or the *value* queen.




'A' # """ I put A between apostrophes and it suddenly turned to something
that cannot be reduced by rewriting any further,
or unless that seems to me.
I thought: if one can turn to value anything by putting it between
apostrophes or quotation 

Re: [Tutor] keyboard interrupt

2013-05-26 Thread Marc Tompkins
On Fri, May 24, 2013 at 7:19 PM, Steven D'Aprano wrote:

You can edit *other* people's questions and answers??!??!??
>
> What. The. Hell.


The idea is to build an authoritative information resource (in particular,
the goal is that the accepted answer to any given question will become the
primary result for someone Googling that same question.)  So it makes very
good sense that both questions and answers can be edited for quality, and
the result is (mostly) good.

My objection is to people gaming the system - making imperceptible edits
like dashes-to-em-dashes - in order to juice their scores.
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor