Re: Inplace shuffle function returns none

2016-10-19 Thread Sayth Renshaw
Ok i think i do understand it. I searched the python document for in-place 
functions but couldn't find a specific reference. 

Is there a particular part in docs or blog that covers it? Or is it fundamental 
to all so not explicitly treated in one particular page?

Thanks

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


Re: function call questions

2016-10-19 Thread Peter Otten
chenyong20...@gmail.com wrote:

> 在 2016年10月19日星期三 UTC+8上午11:46:28,MRAB写道:
>> On 2016-10-19 03:15, chenyong20...@gmail.com wrote:
>> > Thanks Peter and Anssi for your kind help. Now I'm ok with the first
>> > question. But the second question still confused me. Why "it seems that
>> > after root = root.setdefault(ch,{}) tree['a'] and root are the same
>> > object" and follows tree['a']['b']? Thanks.
>> >
>> You call the .setdefault method with a key and a default value.
>> 
>> The .setdefault method does this: if the key is in the dict, it returns
>> the associated value, else it puts the key and the default into the dict
>> and then returns the default.
> 
> thanks for the reply. I understand this. I'm now confused on why tree got
> its magic result.

Perhaps it is easier to understand if you rewrite the function without 
setdefault()?

def add_to_tree(root, value_string):
print "root is %s, value_string is %s" % (root, value_string)
for ch in value_string:
print "ch is %s" % ch
if ch not in root: # always true if the root arg is an empty dict
root[ch] = {}
root = root[ch] # inside the function the inner dict becomes the new
# root
print "root is", root
print "tree is", tree


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


Re: Inplace shuffle function returns none

2016-10-19 Thread Peter Otten
Steve D'Aprano wrote:

> On Wed, 19 Oct 2016 07:25 am, Sayth Renshaw wrote:
> 
>> So why can't i assign the result slice to a variable b?
>> 
>> It just keeps getting none.
> 
> Of course you can assign the result slice to b. You just have to do it the
> right way.
> 
> You keep getting None because you do it the wrong way. Unfortunately you
> aren't showing us your code, so we have no idea what you are doing wrong.
> My guess is that you are doing something like this:
> 
> 
> a = [1, 2, 3, 4, 5, 6, 7, 8]
> b = random.shuffle(a)[0:3]
> 
> That's wrong -- shuffle() modifies the list you pass, and returns None.
> You cannot take a slice of None. Try this:
> 
> a = [1, 2, 3, 4, 5, 6, 7, 8]
> random.shuffle(a)
> b = a[0:3]
> print(b)

But once you understand how it works consider

>>> a = [1, 2, 3, 4, 5, 6, 7, 8]
>>> random.sample(a, 3)
[1, 5, 2]

instead. This should be more efficient for "small" samples and leaves `a` 
intact:

>>> a
[1, 2, 3, 4, 5, 6, 7, 8]


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


Re: Inplace shuffle function returns none

2016-10-19 Thread Ben Finney
Sayth Renshaw  writes:

> Ok i think i do understand it. I searched the python document for
> in-place functions but couldn't find a specific reference. 

They aren't a separate kind of function.

A function can do anything Python code can do; indeed, most Python
programs do just about everything they do inside functions.

A function also, if it returns, returns a value. (Sometimes that is the
value ‘None’.)

There's no special distinction between functions that return ‘None’
versus those that don't. There's no special distinction between
functions that have other effects versus those that don't.

> Is there a particular part in docs or blog that covers it? Or is it
> fundamental to all so not explicitly treated in one particular page?

I don't really understand the question, but I hope that addresses it.

-- 
 \   “Prayer must never be answered: if it is, it ceases to be |
  `\   prayer and becomes correspondence.” —Oscar Wilde, _The Epigrams |
_o__)of Oscar Wilde_, 1952 |
Ben Finney

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


Re: Inplace shuffle function returns none

2016-10-19 Thread Chris Angelico
On Wed, Oct 19, 2016 at 6:01 PM, Sayth Renshaw  wrote:
> Ok i think i do understand it. I searched the python document for in-place 
> functions but couldn't find a specific reference.
>
> Is there a particular part in docs or blog that covers it? Or is it 
> fundamental to all so not explicitly treated in one particular page?

It's not a rule, it's a design principle. So the best way to find out
about it is either to look at hundreds or thousands of Python standard
library functions and recognize a pattern... or ask on a mailing list
and be told :)

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


How can I copy one excel file data to another excel file by excluding hidden rows content using Python

2016-10-19 Thread Kishore JK
I need to copy one excel file data into another excel file by excluding rows 
which were hidden in source excel file.

https://i.stack.imgur.com/NPUK6.png

As shown in the image, from the source excel file, I need to copy the data of 
row numbers 116,135 and 139 and exclude all the remaining rows which were 
hidden because of not matching the criteria.

I have tried below code, but this is copying entire data into new excel sheet.
wb = openpyxl.load_workbook('sourcefile.xlsx')
sheet = wb.active
sheet.title = 'Sheet1'
wb.save('destinationfile.xlsx')
-- 
https://mail.python.org/mailman/listinfo/python-list


Python GUI application embedding a web browser - Options?

2016-10-19 Thread Paul Moore
I'm looking to write a GUI application in Python (to run on Windows, using 
Python 3.5). The application is just a relatively thin wrapper around a browser 
- it's presenting an existing web application, just in its own window rather 
than in a standard browser window. I'm looking for advice on a good GUI toolkit 
to use.

I've not done much GUI programming in Python, so I don't have a "preferred 
toolkit" as such. A bit of Google searching found an embedded browser widget in 
PyQt, but the examples I tried didn't work - it looks like the QWebView class 
is deprecated and has been removed in the current version of PyQt. I haven't 
really found any other examples (there's a "embedding a web page in Tkinter" 
example I found, but it looks like it's just doing rendering, not embedding a 
full browser - which I need as the site I want to access uses CSS/JavaScript).

Is there any good option for this, or would I be better looking elsewhere for a 
solution? I could probably work out how to knock something up using .NET, or a 
HTA file, for example, I'm just more comfortable coding in Python.

Thanks,
Paul
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Python GUI application embedding a web browser - Options?

2016-10-19 Thread Phil Thompson
On 19 Oct 2016, at 11:07 am, Paul Moore  wrote:
> 
> I'm looking to write a GUI application in Python (to run on Windows, using 
> Python 3.5). The application is just a relatively thin wrapper around a 
> browser - it's presenting an existing web application, just in its own window 
> rather than in a standard browser window. I'm looking for advice on a good 
> GUI toolkit to use.
> 
> I've not done much GUI programming in Python, so I don't have a "preferred 
> toolkit" as such. A bit of Google searching found an embedded browser widget 
> in PyQt, but the examples I tried didn't work - it looks like the QWebView 
> class is deprecated and has been removed in the current version of PyQt. I 
> haven't really found any other examples (there's a "embedding a web page in 
> Tkinter" example I found, but it looks like it's just doing rendering, not 
> embedding a full browser - which I need as the site I want to access uses 
> CSS/JavaScript).
> 
> Is there any good option for this, or would I be better looking elsewhere for 
> a solution? I could probably work out how to knock something up using .NET, 
> or a HTA file, for example, I'm just more comfortable coding in Python.

The Chrome-based QWebEngineView (http://doc.qt.io/qt-5/qwebengineview.html) is 
fully supported by PyQt.

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


Re: Python GUI application embedding a web browser - Options?

2016-10-19 Thread Vincent Vande Vyvre

Le 19/10/2016 à 12:07, Paul Moore a écrit :

I'm looking to write a GUI application in Python (to run on Windows, using 
Python 3.5). The application is just a relatively thin wrapper around a browser 
- it's presenting an existing web application, just in its own window rather 
than in a standard browser window. I'm looking for advice on a good GUI toolkit 
to use.

I've not done much GUI programming in Python, so I don't have a "preferred toolkit" as 
such. A bit of Google searching found an embedded browser widget in PyQt, but the examples I tried 
didn't work - it looks like the QWebView class is deprecated and has been removed in the current 
version of PyQt. I haven't really found any other examples (there's a "embedding a web page in 
Tkinter" example I found, but it looks like it's just doing rendering, not embedding a full 
browser - which I need as the site I want to access uses CSS/JavaScript).

Is there any good option for this, or would I be better looking elsewhere for a 
solution? I could probably work out how to knock something up using .NET, or a 
HTA file, for example, I'm just more comfortable coding in Python.

Thanks,
Paul


Hi,

PyQt is a good choice to embed a web viewer in a GUI application.

There's no great differences between Qt4 and Qt5.

An example with PyQt4:
http://bazaar.launchpad.net/~vincent-vandevyvre/oqapy/2.0/files/head:/oqapy-2.0/g9n/

The same with PyQt5:
http://bazaar.launchpad.net/~vincent-vandevyvre/oqapy/3.0/files/head:/g9n/

--
Vincent V.V.
Oqapy  . python3-exiv2 
 . Qarte 
 . PaQager 

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


Re: How can I copy one excel file data to another excel file by excluding hidden rows content using Python

2016-10-19 Thread Karim

On 19/10/2016 11:30, Kishore JK wrote:

I need to copy one excel file data into another excel file by excluding rows 
which were hidden in source excel file.

https://i.stack.imgur.com/NPUK6.png

As shown in the image, from the source excel file, I need to copy the data of 
row numbers 116,135 and 139 and exclude all the remaining rows which were 
hidden because of not matching the criteria.

I have tried below code, but this is copying entire data into new excel sheet.
wb = openpyxl.load_workbook('sourcefile.xlsx')
sheet = wb.active
sheet.title = 'Sheet1'
wb.save('destinationfile.xlsx')


Hello,

I use xlrd external module. This is only samples codes from one  of my 
class. You  can filter the row you want with an conditional statement.

You must have equivalent attribute in openpyxl module.

Import xlrd

Import csv

 self._book= xlrd.open_workbook(xlfilename)

 self._selected_sheets = [self._book.sheet_by_name(sheetname) for 
sheetname in sheets]


  writer = csv.writer(open(xlfilename, 'wb'), 
delimiter=self.file_format.delimiter)


  rows   = izip(*(sheet.col_values(col) for col in selected_columns))

  for row in rows:

   # NOTE!!!: could have UnicodeEncodeError exception raised here!

   try:

  writer.writerow(row)

   except UnicodeEncodeError, e:

   print("\nWarning: {0}. Row '{1}' will not be 
extracted!".format(e, row))



Regards

Karim


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


Re: Python GUI application embedding a web browser - Options?

2016-10-19 Thread Paul Moore
On 19 October 2016 at 12:10, Phil Thompson  wrote:
> The Chrome-based QWebEngineView (http://doc.qt.io/qt-5/qwebengineview.html) 
> is fully supported by PyQt.

Nice. Thanks for the pointer. Looks like the various bits of advice I
found on the web are a little out of date, is all.

Cheers,
Paul
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: [FAQ] "Best" GUI toolkit for python

2016-10-19 Thread Mark Summerfield
On Tuesday, October 18, 2016 at 9:09:46 PM UTC+1, Demosthenes Koptsis wrote:
> My favorite GUIs are PyQt and wxPython.
> 
> I prefer PyQt than PySide because PySide seem to me like an abandoned 
> project.
[snip]

It does seem that PySide 1 isn't making any visible progress.

However, PySide 2 for Qt 5 is indeed progressing, and is being funded by The Qt 
Company: https://wiki.qt.io/PySide2
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Python GUI application embedding a web browser - Options?

2016-10-19 Thread Mark Summerfield
On Wednesday, October 19, 2016 at 11:08:15 AM UTC+1, Paul  Moore wrote:
> I'm looking to write a GUI application in Python (to run on Windows, using 
> Python 3.5). The application is just a relatively thin wrapper around a 
> browser - it's presenting an existing web application, just in its own window 
> rather than in a standard browser window. I'm looking for advice on a good 
> GUI toolkit to use.
> 
> I've not done much GUI programming in Python, so I don't have a "preferred 
> toolkit" as such. A bit of Google searching found an embedded browser widget 
> in PyQt, but the examples I tried didn't work - it looks like the QWebView 
> class is deprecated and has been removed in the current version of PyQt. I 
> haven't really found any other examples (there's a "embedding a web page in 
> Tkinter" example I found, but it looks like it's just doing rendering, not 
> embedding a full browser - which I need as the site I want to access uses 
> CSS/JavaScript).
> 
> Is there any good option for this, or would I be better looking elsewhere for 
> a solution? I could probably work out how to knock something up using .NET, 
> or a HTA file, for example, I'm just more comfortable coding in Python.
> 
> Thanks,
> Paul

Since the application is a web app have you looked at:
https://github.com/r0x0r/pywebview + https://github.com/dddomodossola/remi
or at
https://flexx.readthedocs.io/en/stable/
These basically wrap the platform's web engine so you can write stand alone 
desktop apps but coding in Python (which gets translated into HTML/JS).
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Python GUI application embedding a web browser - Options?

2016-10-19 Thread Paul Moore
On Wednesday, 19 October 2016 13:54:09 UTC+1, Mark Summerfield  wrote:
>
> Since the application is a web app have you looked at:
> https://github.com/r0x0r/pywebview + https://github.com/dddomodossola/remi
> or at
> https://flexx.readthedocs.io/en/stable/
> These basically wrap the platform's web engine so you can write stand alone 
> desktop apps but coding in Python (which gets translated into HTML/JS).

I think that's the opposite of what I want (which is a standalone desktop app 
coded in Python, than embeds an active webpage as a widget in the app). But 
I'll take a closer look - it may be that I'm not clear on what they are doing 
(pretty certain, I've barely any experience with what's available for Python 
outside of command line applications).

Thanks,
Paul
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Python-based monads essay part 2

2016-10-19 Thread Chris Angelico
On Wed, Oct 19, 2016 at 9:26 AM, Gregory Ewing
 wrote:
> The things being reasoned about -- the actions -- are
> not themselves functions, but that doesn't mean there's
> any cheating going on. Would you say it was cheating
> to perform algebraic manipulations on an expression
> involving numbers or strings? Why should it be any
> different when the things being manipulated represent
> actions that affect the world?

Okay. Let me pick up two of functional programming's favourite idioms,
and you can help me understand how I/O monads fit into them.

1) Lazy evaluation. In an imperative language, you can't say "make me
a list of prime numbers, then take the 73rd element"; but functional
languages are perfectly happy to have an infinite-length as an object,
as long as you never actually ask for the last element or anything.

2) Related to the above: Dead code elimination. If the 73rd prime ends
up being multiplied by zero, Haskell is entirely justified in not
calculating it.

Okay. Now let's suppose that, instead of "73" in the first step, you
have "ask the user for an integer". Are you allowed to eliminate this
prompt, since the result of it cannot possibly affect anything? And if
not, why not? After all, eliminating terms that have been multiplied
by zero is one form of algebra. If numbers and strings and I/O monads
are all the same, there's no difference between "length of this
string" (an operation yielding an integer) and "ask the user for an
integer".

If you consider that the world changes state as a result of asking the
user for input, then you've just eliminated all notion of functional
purity. You have side effects, plain and simple, and you're using
imperative code. At what point can you deem that the algebra is safe?
At what boundary can any of your code manipulations still be done?

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


Re: how to evaluate a ast tree and change Add to Multiply ?

2016-10-19 Thread Jerry Hill
On Wed, Oct 12, 2016 at 5:55 AM, meInvent bbird  wrote:
> i just expect to
> rewrite + become multiply
> by edit the example in the link provided

This seems to work.  You need to define visit_BinOp instead of
visit_Num (since you want to mess with the binary operations, not the
numbers).  Then,in visit_BinOp, we just replace the ast.Add node with
an ast.Mult node.

import ast

class ChangeAddToMultiply(ast.NodeTransformer):
"""Wraps all integers in a call to Integer()"""
def visit_BinOp(self, node):
if isinstance(node.op, ast.Add):
node.op = ast.Mult()
return node

code = 'print(2+5)'
tree = ast.parse(code)
tree = ChangeAddToMultiply().visit(tree)
ast.fix_missing_locations(tree)
co = compile(tree, '', "exec")

exec(code)
exec(co)

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


Re: Python-based monads essay part 2

2016-10-19 Thread Marko Rauhamaa
Chris Angelico :
> Okay. Now let's suppose that, instead of "73" in the first step, you
> have "ask the user for an integer". Are you allowed to eliminate this
> prompt, since the result of it cannot possibly affect anything? And if
> not, why not?

I would guess yes; that's how Python works as well:

>>> 7 or input()
7


However, if we think of the I/O interaction (aka *the process*) to be
the return value of a function, every bead in the I/O chain counts.

> After all, eliminating terms that have been multiplied by zero is one
> form of algebra.

I wonder if the word "algebra" brings anything to this discussion. It
doesn't make Haskell any more or less functional.

> If you consider that the world changes state as a result of asking the
> user for input, then you've just eliminated all notion of functional
> purity.

Not necessarily. Nothing changes the world. Rather you have different
worlds: the world of the past and the world of the future. The world of
the past is in the past of the world of the future:

def print(world, text, cont):
return cont(World(past=world, offset=text))

def print_x_then_y(world, x, y, cont):
return print(world, x, lambda world2: print(world2, y, cont))

> You have side effects, plain and simple, and you're using imperative
> code.

Technically, no. Thought-model-wise, yes.

Of course, recursive functions can simulate Turing machines and vice
versa. You can write purely functional code in purely imperative style.

My example above is purely functional as:

 * Every object is immutable.

 * The order of evaluation does not change the end result.

The end result is an ordered sequence of events. The topological order
is a causal order (you need the past world to construct the future
world), and causal order generates a temporal order.

Now, *if* the Haskell VM chooses to *realize* I/O events *as soon as* it
becomes aware of them, you get traditional, imperative side effects.


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


Re: Button Widget and Key Binding Problem

2016-10-19 Thread Wildman via Python-list
On Wed, 19 Oct 2016 04:39:03 +0100, MRAB wrote:

> The 'bind' method passes an 'event' object when it calls; the 'command' 
> callback doesn't.
> 
> You don't care about the 'event' object anyway, so you can just define a 
> single method with a default argument that you ignore:
> 
>  def load_image(self, event=None):
>  # code to load an image

That works perfectly.  Thank you.

-- 
 GNU/Linux user #557453
The cow died so I don't need your bull!
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Python-based monads essay part 2

2016-10-19 Thread Chris Angelico
On Thu, Oct 20, 2016 at 3:51 AM, Marko Rauhamaa  wrote:
> Chris Angelico :
>> Okay. Now let's suppose that, instead of "73" in the first step, you
>> have "ask the user for an integer". Are you allowed to eliminate this
>> prompt, since the result of it cannot possibly affect anything? And if
>> not, why not?
>
> I would guess yes; that's how Python works as well:
>
> >>> 7 or input()
> 7
>

That's not quite the same. That's equivalent to:

if 7:
7
else:
input()

Short-circuiting depends only on what's known *prior* to that
evaluation. Dead code elimination removes the notion of time:

int(input("Enter something:")) * 0 + 5

This can simply return 5, because (barring an exception being thrown)
the value entered cannot affect the result. Python does not do this.
And any system that has optimization flags (PostgreSQL, Pike, etc),
this would be marked "has side effects", and therefore is not
considered dead code. But if you (maybe mistakenly) mark input() as a
pure function, then yes, this could indeed be optimized out.

> However, if we think of the I/O interaction (aka *the process*) to be
> the return value of a function, every bead in the I/O chain counts.

And that's what I mean about making function purity meaningless. Here,
look - this is a pure function!

magic_numbers = [1, 2, 4, 8]
def do_magic(x):
magic_numbers[x % 4] += x / 4
return magic_numbers[(x + 1) % 4]

Since the previous state of magic_numbers can be considered an input,
and the new state can be considered an output, this is a pure
function! Right?

>> If you consider that the world changes state as a result of asking the
>> user for input, then you've just eliminated all notion of functional
>> purity.
>
> Not necessarily. Nothing changes the world. Rather you have different
> worlds: the world of the past and the world of the future. The world of
> the past is in the past of the world of the future:
>
> def print(world, text, cont):
> return cont(World(past=world, offset=text))
>
> def print_x_then_y(world, x, y, cont):
> return print(world, x, lambda world2: print(world2, y, cont))
>
>> You have side effects, plain and simple, and you're using imperative
>> code.
>
> Technically, no. Thought-model-wise, yes.

Technical means nothing if you're destroying the meaning of functional
purity in order to squeeze I/O into it.

> My example above is purely functional as:
>
>  * Every object is immutable.
>
>  * The order of evaluation does not change the end result.
>
> The end result is an ordered sequence of events. The topological order
> is a causal order (you need the past world to construct the future
> world), and causal order generates a temporal order.

Yeah, nice. You just made a mutable object with significant order of
evaluation and then added enough external information to it that you
can pretend it's immutable and can be evaluated in any order. I could
make that same transformation with literally any function, simply by
taking "the world" as an input and an output. It makes the entire
concept utterly meaningless.

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


Re: [FAQ] "Best" GUI toolkit for python

2016-10-19 Thread Demosthenes Koptsis

I thought PyQt was supported by Qt company...

There is also an official book by Prentice Hall:

Rapid GUI Programming with Python and Qt (Prentice Hall Open Source 
Software Development)


https://www.amazon.com/Programming-Python-Prentice-Software-Development/dp/0132354187/ref=sr_1_1?ie=UTF8&qid=1476901015&sr=8-1&keywords=rapid+qt+python

Inside the book the apps are been developed in PyQt

Regards,
Dim

On 10/19/2016 03:49 PM, Mark Summerfield wrote:

On Tuesday, October 18, 2016 at 9:09:46 PM UTC+1, Demosthenes Koptsis wrote:

My favorite GUIs are PyQt and wxPython.

I prefer PyQt than PySide because PySide seem to me like an abandoned
project.

[snip]

It does seem that PySide 1 isn't making any visible progress.

However, PySide 2 for Qt 5 is indeed progressing, and is being funded by The Qt 
Company: https://wiki.qt.io/PySide2


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


Re: Python-based monads essay part 2

2016-10-19 Thread Marko Rauhamaa
Chris Angelico :

> On Thu, Oct 20, 2016 at 3:51 AM, Marko Rauhamaa  wrote:
>>
>> def print(world, text, cont):
>> return cont(World(past=world, offset=text))
>>
>> def print_x_then_y(world, x, y, cont):
>> return print(world, x, lambda world2: print(world2, y, cont))
>>
> [...]
>>
>> The end result is an ordered sequence of events. The topological order
>> is a causal order (you need the past world to construct the future
>> world), and causal order generates a temporal order.
>
> Yeah, nice. You just made a mutable object with significant order of
> evaluation and then added enough external information to it that you
> can pretend it's immutable and can be evaluated in any order.

Please point out what is mutable in my code above. It doesn't have a
single mutable object. The world is recreated with each interaction.

(Parenthetically, that's how quantum mechanics explains life, universe
and everything. Nothing ever changes. Immutable particles/systems simply
get destroyed and created with every interaction. In fact, particles can
be abstracted out as figments of imagination -- the existence is a
network of interactions.)

> I could make that same transformation with literally any function,
> simply by taking "the world" as an input and an output. It makes the
> entire concept utterly meaningless.

Whether this mental gymnastics is useful, everybody can form their own
opinion.

A funny, semirelated anecdote about Scheme. Scheme does not have a
try/finally construct. It can't -- Scheme is too powerful. That's
because in Scheme, nothing is [guaranteed to be] final. After you leave
an execution context, a continuation may leak out that enables a
system-wide jump in the middle of the context.

Python could have similar continuations if it added a "resume" keyword:

def work(f):
...
message_from_grave = raise ValueError()
if message_from_grave == "CarryOn!":
...

def guard_it():
f = open(...)
try:
work(f)
finally:
f.close()

def main():
try:
guard_it()
except ValueError:
# I'm having none of it!
resume "CarryOn!"

The problem is, how can the file f unclose itself when work resumes?
Well, maybe we'd need another keyword:

def guard_it():
f = open(...)
try:
work(f)
finally:
f.close()
nevermind:
f.reopen()


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


Re: [FAQ] "Best" GUI toolkit for python

2016-10-19 Thread Michael Torrie
On 10/19/2016 12:18 PM, Demosthenes Koptsis wrote:
> I thought PyQt was supported by Qt company...

I don't think so.  PyQt is a commercial product of Riverbank Computing.
It's dual-licensed under the GPL and a proprietary license you can buy.
Riverbank may have had a relationship with Trolltech back in the day,
but I'm not aware of any current business relationship between Riverbank
and the Qt project or the Qt company.  Riverbank is obviously happy to
be a part of the Qt open source community.

PySide was created by Nokia when they owned Qt. Basically they wanted a
python toolkit that they could use without being beholden to Riverbank.
It's clear that Nokia never wanted to monetize Qt like TrollTech did,
but rather use it to develop their own phone operating system.  That
didn't pan out and Nokia spun off Qt to it's own organization/company,
and PySide is part of that Qt Project.

PySide development is ongoing but the pace is not as rapid as PyQt.
Riverbank still develops and sells PyQt and support to companies and has
stable Qt5 support now.  I wondered if PySide would ultimately kill off
PyQt, but so far that has not been the case.

> There is also an official book by Prentice Hall:
> 
> Rapid GUI Programming with Python and Qt (Prentice Hall Open Source 
> Software Development)
> 
> https://www.amazon.com/Programming-Python-Prentice-Software-Development/dp/0132354187/ref=sr_1_1?ie=UTF8&qid=1476901015&sr=8-1&keywords=rapid+qt+python
> 
> Inside the book the apps are been developed in PyQt

Not sure what makes this an "official book." It has nothing to do with
the Qt Project or the Qt Company.

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


Re: [FAQ] "Best" GUI toolkit for python

2016-10-19 Thread Demosthenes Koptsis

Thanks Michael,

now i have a clear look about Py GUIs.

Did you suggest PySide than PyQt...?

I want to start with a Python Qt Framework.


On 10/19/2016 09:43 PM, Michael Torrie wrote:

On 10/19/2016 12:18 PM, Demosthenes Koptsis wrote:

I thought PyQt was supported by Qt company...

I don't think so.  PyQt is a commercial product of Riverbank Computing.
It's dual-licensed under the GPL and a proprietary license you can buy.
Riverbank may have had a relationship with Trolltech back in the day,
but I'm not aware of any current business relationship between Riverbank
and the Qt project or the Qt company.  Riverbank is obviously happy to
be a part of the Qt open source community.

PySide was created by Nokia when they owned Qt. Basically they wanted a
python toolkit that they could use without being beholden to Riverbank.
It's clear that Nokia never wanted to monetize Qt like TrollTech did,
but rather use it to develop their own phone operating system.  That
didn't pan out and Nokia spun off Qt to it's own organization/company,
and PySide is part of that Qt Project.

PySide development is ongoing but the pace is not as rapid as PyQt.
Riverbank still develops and sells PyQt and support to companies and has
stable Qt5 support now.  I wondered if PySide would ultimately kill off
PyQt, but so far that has not been the case.


There is also an official book by Prentice Hall:

Rapid GUI Programming with Python and Qt (Prentice Hall Open Source
Software Development)

https://www.amazon.com/Programming-Python-Prentice-Software-Development/dp/0132354187/ref=sr_1_1?ie=UTF8&qid=1476901015&sr=8-1&keywords=rapid+qt+python

Inside the book the apps are been developed in PyQt

Not sure what makes this an "official book." It has nothing to do with
the Qt Project or the Qt Company.



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


Re: Inplace shuffle function returns none

2016-10-19 Thread Sayth Renshaw
Hi Chris

I read this last night and thought i may have woken with a frightfully witty 
response.

I didnt however.

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


Pyro4 now with remote iterators

2016-10-19 Thread Irmen de Jong
Hi,

I have just released Pyro4 version 4.49; https://pypi.python.org/pypi/Pyro4

(super short description: it is a library that allows you to transparently call 
methods
on objects that are running on other machines, as if they were local)

Pyro now also supports remote iterators. This means you can loop over a remote 
iterator
(or generator) that is running on another machine, and get the items on demand 
as they
are consumed by your client code.

Server:

class RemoteObject:
@Pyro4.expose
def numbers(self):
i = 0
while i<10:
yield i
i += 1

Client:

r = Pyro4.Proxy(".")
for number in r.numbers():
print(number)


When used correctly this feature can avoid having to build large data 
structures and
returning those all at once from a remote call. Instead, you now can return them
piecemeal and only that which is actually used by the client.

The feature is also supported by the Pyrolite client library for Java and 
.NET/C#.
I think it is pretty nifty and interesting enough to post about it here :)


Regards
Irmen de Jong
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: [FAQ] "Best" GUI toolkit for python

2016-10-19 Thread Michael Torrie
On 10/19/2016 01:13 PM, Demosthenes Koptsis wrote:
> Did you suggest PySide than PyQt...?

Only that I'm using it right now, but I'm making sure my code will run
with PyQt.  I don't see a huge benefit of PySide except for the source
code license (LGPL so you can use it without fee in a proprietary project).

My opinion is that if you're fine with the GPL license of PyQt for open
source projects, it's the way to go.

If not, then with a little bit more effort PySide works pretty well.
They are close enough to each other to be more or less compatible.

> I want to start with a Python Qt Framework.

I'd say start with PyQt then.  Most examples on the net assume PyQt
anyway.  That book you mentioned is pretty good.  For a while a couple
of universities were offering it for download as a pdf to support their
classes.  Might be possible to find it in a search.
-- 
https://mail.python.org/mailman/listinfo/python-list


Converting the keys of a dictionary from numeric to string

2016-10-19 Thread pozz

I have a dictionary where the keys are numbers:

mydict = { 1: 1000, 2: 1500, 3: 100 }

I would like to convert keys from number to string representation:

mydict = { "apples": 1000, "nuts": 1500, "tables": 100 }

Of course, somewhere I have the association between key-numbers and 
key-strings, maybe in another dictionary:


keydict = { 1: "apples", 2: "nuts", 3; "tables" }

How could I make the conversion? My solution:

keydict = { 1: "Joseph", 2: "Michael", 3: "John" }
mydict = { 1: 1000, 2: 2000, 3: 3000 }
newdict = {}
for k in mydict.keys():
newdict.update({keydict[k]: mydict[k]})
print(newdict)

I tried changing just mydict (without creating newdict), without success.
--
https://mail.python.org/mailman/listinfo/python-list


Re: Converting the keys of a dictionary from numeric to string

2016-10-19 Thread Paul Rubin
pozz  writes:
> I have a dictionary where the keys are numbers: ...

Python 2.7.5:

>>> mydict = { 1: 1000, 2: 1500, 3: 100 }
>>> keydict = { 1: "apples", 2: "nuts", 3: "tables" }
>>> newdict = dict((keydict[k],v) for k,v in mydict.items())
>>> print newdict
{'tables': 100, 'nuts': 1500, 'apples': 1000}
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: [FAQ] "Best" GUI toolkit for python

2016-10-19 Thread Demosthenes Koptsis


I have the book already.

Thank you Michael


Regards,

Dim


On 10/20/2016 01:30 AM, Michael Torrie wrote:

On 10/19/2016 01:13 PM, Demosthenes Koptsis wrote:

Did you suggest PySide than PyQt...?

Only that I'm using it right now, but I'm making sure my code will run
with PyQt.  I don't see a huge benefit of PySide except for the source
code license (LGPL so you can use it without fee in a proprietary project).

My opinion is that if you're fine with the GPL license of PyQt for open
source projects, it's the way to go.

If not, then with a little bit more effort PySide works pretty well.
They are close enough to each other to be more or less compatible.


I want to start with a Python Qt Framework.

I'd say start with PyQt then.  Most examples on the net assume PyQt
anyway.  That book you mentioned is pretty good.  For a while a couple
of universities were offering it for download as a pdf to support their
classes.  Might be possible to find it in a search.


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


Re: function call questions

2016-10-19 Thread chenyong20000
在 2016年10月19日星期三 UTC+8下午3:17:18,Peter Otten写道:
> chenyong20...@gmail.com wrote:
> 
> > 在 2016年10月19日星期三 UTC+8上午11:46:28,MRAB写道:
> >> On 2016-10-19 03:15, chenyong20...@gmail.com wrote:
> >> > Thanks Peter and Anssi for your kind help. Now I'm ok with the first
> >> > question. But the second question still confused me. Why "it seems that
> >> > after root = root.setdefault(ch,{}) tree['a'] and root are the same
> >> > object" and follows tree['a']['b']? Thanks.
> >> >
> >> You call the .setdefault method with a key and a default value.
> >> 
> >> The .setdefault method does this: if the key is in the dict, it returns
> >> the associated value, else it puts the key and the default into the dict
> >> and then returns the default.
> > 
> > thanks for the reply. I understand this. I'm now confused on why tree got
> > its magic result.
> 
> Perhaps it is easier to understand if you rewrite the function without 
> setdefault()?
> 
> def add_to_tree(root, value_string):
> print "root is %s, value_string is %s" % (root, value_string)
> for ch in value_string:
> print "ch is %s" % ch
> if ch not in root: # always true if the root arg is an empty dict
> root[ch] = {}
> root = root[ch] # inside the function the inner dict becomes the new
> # root
> print "root is", root
> print "tree is", tree

please forgive my stupid. I still can't follow this.
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: function call questions

2016-10-19 Thread Frank Millman
wrote in message 
news:5506e4d8-bd1d-4e56-8d1b-f71fa8293...@googlegroups.com...


在 2016年10月19日星期三 UTC+8下午3:17:18,Peter Otten写道:

chenyong20...@gmail.com wrote:

> 在 2016年10月19日星期三 UTC+8上午11:46:28,MRAB写道:
>> On 2016-10-19 03:15, chenyong20...@gmail.com wrote:
>> > Thanks Peter and Anssi for your kind help. Now I'm ok with the first
>> > question. But the second question still confused me. Why "it seems 
>> > that

>> > after root = root.setdefault(ch,{}) tree['a'] and root are the same
>> > object" and follows tree['a']['b']? Thanks.
>> >



please forgive my stupid. I still can't follow this.


Let's see if I can explain. I am using 't' and 'r' instead of 'tree' and 
'root', but otherwise it is the same as your original example.



t = {}
r = t
id(t)

2542235910088

id(r)

2542235910088

At this point, t and r are both references to the same empty dictionary.


r = r.setdefault('a', {})


This has done two things.

It has inserted the key 'a' into the dictionary, and set its value to {}.


t

{'a': {}}

id(t)

2542235910088

It has also rebound 'r' so that it now references the new empty dictionary 
that has been inserted.



r

{}

id(r)

2542234429896

t['a']

{}

id(t['a'])

2542234429896

Now continue this process with r = r.setdefault('b', {}), and watch what 
happens.


Hopefully this will help you to understand. Feel free to ask further if not 
sure.


Frank Millman


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


Re: [FAQ] "Best" GUI toolkit for python

2016-10-19 Thread Paul Rubin
pozz  writes:
> Is there a visual GUI builder for Tkinter?

Not that I know of.  I haven't felt I needed one.  I generally draw my
intended UI on paper and then use the Tk grid layout gadget.

> Could you explain better what do you mean with "industrial-looking
> UI"? 

Something that doesn't have the slickness of consumer software.  That
said I've only used it with Python 2.  Python 3 supposedly has a larger
widget collection and can use OS native widgets.  

> What can I do and what *can't* I do with Tkinter?

I look at the very carefully tuned and sometimes animated UI's in fancy
desktop applications like Gimp and don't see a way to do similar things
in Tkinter.  Tkinter is ok for simple basic GUIs with text labels,
buttons, an event loop that lets you catch mouse clicks, etc.  Gimp uses
Gtk (I think) and does all kinds of fancy effects that seem difficult
with tkinter, though now that I think about it, they might be doable
with enough effort.

If your Python distro comes with IDLE, give it a try.  IDLE uses Tkinter
and might give you an idea of what to expect.

>> Kivy 
> It seems nice.  Does it use the native look&feel on Windows?

I don't know.  I want to try it sometime, but haven't yet.
-- 
https://mail.python.org/mailman/listinfo/python-list