Re: What's the best way to minimize the need of run time checks?

2016-08-12 Thread Lawrence D’Oliveiro
On Thursday, August 11, 2016 at 8:33:41 AM UTC+12, Juan Pablo Romero Méndez 
wrote:

> I've been trying to find (without success so far) an example of a situation
> where the dynamic features of a language like Python provides a clear
> advantage over languages with more than one type.

I have used, and continue to use, both static and dynamic languages.

With static languages, once a piece of code compiles without errors, you have a 
slightly higher level of confidence in its correctness than with a dynamic 
language.

On the other hand, a dynamic language allows me to be much more productive, 
because I have to write less code to begin with.

The closest I can offer for an apples-to-apples comparison is PyCairo 
 versus Qahirah 
. Both are Python bindings for the Cairo 
graphics library; the former is written in C as a Python extension module, the 
latter is done in pure Python using ctypes.

I didn’t write the former; I merely tried to rescue it from abandonment to see 
if I could fill in a few more missing features. And what I found was, it would 
be quicker to start again from scratch than to continue working on it.
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: What's the best way to minimize the need of run time checks?

2016-08-12 Thread Marko Rauhamaa
Lawrence D’Oliveiro :

> With static languages, once a piece of code compiles without errors,
> you have a slightly higher level of confidence in its correctness than
> with a dynamic language.
>
> On the other hand, a dynamic language allows me to be much more
> productive, because I have to write less code to begin with.

I believe a high-level language like Python makes it easier to write
correct code as well. That's because every line you write has a nonzero
chance of containing a bug, and Python allows you to express complicated
notions more naturally and fewer code lines.

In a word, Python makes it easier to see the forest for the trees.


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


Re: What's the best way to minimize the need of run time checks?

2016-08-12 Thread BartC

On 12/08/2016 09:38, Marko Rauhamaa wrote:

Lawrence D’Oliveiro :


With static languages, once a piece of code compiles without errors,
you have a slightly higher level of confidence in its correctness than
with a dynamic language.

On the other hand, a dynamic language allows me to be much more
productive, because I have to write less code to begin with.


You can be too dynamic. Take an example like this:

 class date:
 def __init__(self,d,m,y):
 self.day=d
 self.month=m
 self.year=y

 d=date(25,12,2015)

 d.yaer=1999

 print (d.day,d.month,d.year)

'year' has been spelled wrongly, but this error is not picked up by the 
byte-code compiler and will not immediately be detected at runtime 
either. It will be seen in my example only if someone notices the 
printout isn't what it should be.


This would never get past a static language nor some that also have 
dynamic types.


--
Bartc


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


Re: What's the best way to minimize the need of run time checks?

2016-08-12 Thread Chris Angelico
On Fri, Aug 12, 2016 at 7:38 PM, BartC  wrote:
> You can be too dynamic. Take an example like this:
>
>  class date:
>  def __init__(self,d,m,y):
>  self.day=d
>  self.month=m
>  self.year=y
>
>  d=date(25,12,2015)
>
>  d.yaer=1999
>
>  print (d.day,d.month,d.year)
>
> 'year' has been spelled wrongly, but this error is not picked up by the
> byte-code compiler and will not immediately be detected at runtime either.
> It will be seen in my example only if someone notices the printout isn't
> what it should be.
>
> This would never get past a static language nor some that also have dynamic
> types.

Nor will it get past a lot of linters. Or unit tests. There are other solutions.

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


Re: What's the best way to minimize the need of run time checks?

2016-08-12 Thread BartC

On 12/08/2016 10:45, Chris Angelico wrote:

On Fri, Aug 12, 2016 at 7:38 PM, BartC  wrote:

You can be too dynamic. Take an example like this:



 d.yaer=1999

 print (d.day,d.month,d.year)



This would never get past a static language nor some that also have dynamic
types.


Nor will it get past a lot of linters. Or unit tests. There are other solutions.


How would a linter know that you didn't /want/ to create a new attribute 
called 'yaer'?


And unit tests (which I only vaguely know about) I don't think are 
universally used. People like to use dynamic languages to code rapidly 
and informally.


--
Bartc

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


Re: What's the best way to minimize the need of run time checks?

2016-08-12 Thread Steven D'Aprano
On Fri, 12 Aug 2016 07:38 pm, BartC wrote:

> 'year' has been spelled wrongly

How do you know?

The point is that it is intentional that callers can set arbitrary
attributes on (most) objects. It is perfectly legitimate to set:

d.year
d.century
d.owner
d.extra_seconds

and, yes, even d.yaer, since it isn't the compiler's job to check your
spelling or critique your choice of variable names.

If you want to spell check your code, run a spellchecker[1], or a linter, or
have a human review it. Or all three.

There are broadly two tactics that a language can take when it comes to
attribute (or more widely, variable) names:


(1) Editing is cheap, but compiling and testing code is expensive, and the
consequences of accessing a non-existent variable are catastrophic
(overwriting arbitrary memory, accessing uninitialised memory, segmentation
faults, execution of arbitrary code, security vulnerabilities, etc).
Therefore it is important that the compiler check ahead of time that every
name referenced exists and has been declared.

(2) Compiling and testing code is cheap, and the consequences of accessing
non-existent variables are mild. Since typos are generally rare (say, one
name out of a hundred is mispelled), requiring declarations is a waste of
effort. Rare errors will be caught through human review, linters, and
testing.


The first time I ever compiled a full-sized application (not a particular
large one either, it was a text editor a little more featureful than
Notepad) it took something like nine hours to compile on a Mac SE (this was
circa 1990). How mad would I have been if, eight hours and thirty minutes
into the compilation, the compiler suddenly stopped with an error caused by
a mistyped variable name?

Today, I could probably compile the equivalent program in a minute. Do I
care if the compiler doesn't pick up the typo until 58 seconds through the
process? Not a whit. I would care if the compiler doesn't pick up the error
*at all* and I'm writing in a low level language where accessing a
non-existent variable allows Bulgarian hackers to steal the money in my
bank account, so there's still some advantage to having declarations in
low-level languages.

It *absolutely makes sense* to require variable declarations (possibly
including type information, where it cannot be inferred) when the
consequences of a typo are routinely expected to be severe. Under those
circumstances, a "bondage and discipline" language that forces you to be
absolutely pedantic to satisfy the compiler can be a good thing.

Turn it around though: one of the reasons why languages like Ruby,
Javascript, Lua, Python, Perl etc are so ridiculously productive is that
you don't have to keep stopping to declare variables just to satisfy the
compiler. Need a new variable? Just assign to it and keep going -- there's
no, or very little, mental task switching.

Another reason why such languages are so productive is the availability of
an interactive REPL where you can try out code interactively. How clumsy
and awkward the REPL would be if you had to keep declaring variables ahead
of time.

The cost of that flexibility is that I have to pay a little bit more
attention when doing a code review, I have to write a few more tests. But
the edit-compile-run cycle is so ridiculously fast that this doesn't really
matter. Programmers have invented entirely new coding paradigms, Test
Driven Development and Agile, to take advantage of this speed and
flexibility. Sure, you can take TDD and Agile into the realm of statically
typed languages, but it will always be like running a sprint wearing a suit
of armour. You can do it, but you'll be out-paced by the sprinter wearing
track shoes and Lycra.





[1] Are there programming language aware spell checkers? If not, there
should be.


-- 
Steve
“Cheer up,” they said, “things could be worse.” So I cheered up, and sure
enough, things got worse.

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


Re: What's the best way to minimize the need of run time checks?

2016-08-12 Thread Steven D'Aprano
On Fri, 12 Aug 2016 07:58 pm, BartC wrote:

> On 12/08/2016 10:45, Chris Angelico wrote:
>> On Fri, Aug 12, 2016 at 7:38 PM, BartC  wrote:
>>> You can be too dynamic. Take an example like this:
> 
>>>  d.yaer=1999
[...]
> How would a linter know that you didn't /want/ to create a new attribute
> called 'yaer'?

Of course the linter can't tell, but it is the job of the linter to complain
about things which *might* be errors. You don't have to listen to the
linter. Think of it as your mother nagging you to put a sweater on because
she's cold. It is the job of the compiler to complain about things which
*definitely are* errors. Think of compiler errors as your abusive father
threatening to beat you with a thick leather belt if you don't do *exactly*
what he says.


> And unit tests (which I only vaguely know about) I don't think are
> universally used. 

Well, of course. There are still plenty of people whose coding paradigm
is "Oh, it compiles? Quick, ship it!"


> People like to use dynamic languages to code rapidly 
> and informally.

And you can do that. And you can also code rapidly and formally with Test
Driven Development, Agile[1] and Scrum.





[1] Often misused to the point that Agile and Scrum are considered by some
to be forms of class warfare conducted by management against programmers.
Especially when combined with stack ranking, quite possible *the* most
employee-hostile management technique since keel-hauling. But Agile itself
is not to blame for the abuses it is put to.


-- 
Steve
“Cheer up,” they said, “things could be worse.” So I cheered up, and sure
enough, things got worse.

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


Re: What's the best way to minimize the need of run time checks?

2016-08-12 Thread BartC

On 12/08/2016 12:07, Steven D'Aprano wrote:

On Fri, 12 Aug 2016 07:38 pm, BartC wrote:


'year' has been spelled wrongly


How do you know?


I know because my intention was to create a RECORD, with a specific set 
of members or fields. With records, you usually don't just create 
arbitrary named members as you go along.


But in Python, the mechanism and syntax used to create such members is 
the same as used for ad-hoc attributes. This was the point I was making 
about sometimes it can be a little too dynamic.



There are broadly two tactics that a language can take when it comes to
attribute (or more widely, variable) names:


(1) Editing is cheap, but compiling and testing code is expensive


No, compiling is cheap too.


The first time I ever compiled a full-sized application (not a particular
large one either, it was a text editor a little more featureful than
Notepad) it took something like nine hours to compile on a Mac SE (this was
circa 1990).


That is completely crazy even for 1990. I was using my own tools then 
and the time to re-compile a 50,000-line application would have been 
measured in seconds. Certainly not minutes anyway (I wouldn't have had 
the patience!).



How mad would I have been if, eight hours and thirty minutes
into the compilation, the compiler suddenly stopped with an error caused by
a mistyped variable name?

Today, I could probably compile the equivalent program in a minute.


I'm actually working on a compiler now that could compile a 50Kloc 
application in about 1/16th of a second (to byte-code; perhaps 1/8th of 
a second to in-memory native code).


A pass that merely checks that names have been declared has effectively 
zero cost.



It *absolutely makes sense* to require variable declarations (possibly
including type information, where it cannot be inferred)


My example was specifically about attribute names where pre-declaring 
the set of allowed attributes would not be onerous, would be usefully 
self-documenting, and would allow more errors to be picked up.


(Of course the design of Python makes that impractical because it would 
require the byte-code compiler to see inside imported modules before 
execution is commenced.)



Turn it around though: one of the reasons why languages like Ruby,
Javascript, Lua, Python, Perl etc are so ridiculously productive is that
you don't have to keep stopping to declare variables just to satisfy the
compiler. Need a new variable? Just assign to it and keep going -- there's
no, or very little, mental task switching.

Another reason why such languages are so productive is the availability of
an interactive REPL where you can try out code interactively. How clumsy
and awkward the REPL would be if you had to keep declaring variables ahead
of time.


I agree when it comes to variables. But it /does/ allow these extra 
errors to creep in that are not detected until that particular fragment 
of code is executed.


--
Bartc


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


Re: What's the best way to minimize the need of run time checks?

2016-08-12 Thread BartC

On 12/08/2016 12:55, BartC wrote:

On 12/08/2016 12:07, Steven D'Aprano wrote:



The first time I ever compiled a full-sized application (not a particular
large one either, it was a text editor a little more featureful than
Notepad) it took something like nine hours to compile on a Mac SE
(this was
circa 1990).


That is completely crazy even for 1990.


(So crazy that you must either have added a couple of zeros or there's 
something unusual you haven't mentioned (maybe the SE was emulating 
another machine and that emulator was interpreted - badly. Or the files 
were at the other end of a 300baud connection.)


A build time of nine hours means 3 lines per second for a 100Kloc app, 
and 30 lps for a 1Mloc app.)


--
Bartc


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


Re: What's the best way to minimize the need of run time checks?

2016-08-12 Thread Michael Torrie
On 08/12/2016 05:07 AM, Steven D'Aprano wrote:
> The first time I ever compiled a full-sized application (not a particular
> large one either, it was a text editor a little more featureful than
> Notepad) it took something like nine hours to compile on a Mac SE (this was
> circa 1990). How mad would I have been if, eight hours and thirty minutes
> into the compilation, the compiler suddenly stopped with an error caused by
> a mistyped variable name?

Surely this application was built using a build system, even back then,
that would allow compilation to resume and not rebuild object files that
were already built.

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


Re: What's the best way to minimize the need of run time checks?

2016-08-12 Thread Michael Selik
On Fri, Aug 12, 2016, 7:11 AM Steven D'Aprano 
wrote:

>
> [1] Are there programming language aware spell checkers? If not, there
> should be.
>

A good autocomplete is much like a spell-checker. I have far fewer spelling
errors when using an editor with autocomplete.

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


Re: What's the best way to minimize the need of run time checks?

2016-08-12 Thread William Ray Wing

> On Aug 12, 2016, at 7:07 AM, Steven D'Aprano  
> wrote:
> 
> 

[megabyte]

> 
> 
> [1] Are there programming language aware spell checkers? If not, there
> should be.
> 
> 

There are programming language-aware editors with built-in spell checkers (and 
syntax coloring, but that’s a different matter).

-Bill

> -- 
> Steve
> “Cheer up,” they said, “things could be worse.” So I cheered up, and sure
> enough, things got worse.
> 
> -- 
> https://mail.python.org/mailman/listinfo/python-list

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


Re: A strange list concatenation result

2016-08-12 Thread Mok-Kong Shen

Am 11.08.2016 um 23:49 schrieb Gary Herron:

On 08/11/2016 03:06 PM, Mok-Kong Shen wrote:


def test(list1,list2):
  list1+=[4,5,6]
  list2=list2+[4,5,6]
  print("inside ",list1,list2)
  return

[snip]


# With

list1=[1,2,3]
list2=[1,2,3]
test(list1,list2)
print("outside",list1,list2)

# I got the following:
# inside  [1, 2, 3, 4, 5, 6] [1, 2, 3, 4, 5, 6]
# outside [1, 2, 3, 4, 5, 6] [1, 2, 3]

[snip]


In this next example, there are two separate lists:


list1 = [1,2,3]
list2 = [1,2,3]
list1 += [4,5,6]
print(list1, list2)

[1, 2, 3, 4, 5, 6] [1, 2, 3]


Does that help?


I don't yet understand why in my 2nd example list2 came out as
[1, 2, 3] outside.

M. K.

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


Meta Python Chapter 3 Available

2016-08-12 Thread Charles Ross
I’ve completed a first draft of Chapter 3: Modules (and Packages) of the Meta 
Python book. Feedback from novices and experts alike would be welcome.

https://github.com/chivalry/meta-python 


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


Re: What's the best way to minimize the need of run time checks?

2016-08-12 Thread Lawrence D’Oliveiro
On Friday, August 12, 2016 at 9:39:11 PM UTC+12, BartC wrote:

> 'year' has been spelled wrongly

That’s why Python has __slots__.
-- 
https://mail.python.org/mailman/listinfo/python-list


Error running an exe file in Windows with reportlab import

2016-08-12 Thread Ernest Bonat, Ph.D.
I have created a simple Python program including the following packages:



import numpy as np

import matplotlib.pyplot as plt

import pandas as pd

from reportlab.pdfgen import canvas



if __name__ == '__main__':

print("Hi!")



I have created the installation package (PyInstaller) using the following
command line:



pyinstaller --onedir --name=runatestapp --noupx
"C:\Users\Ernest\workspace\bars\src\run_a_test.py"



The required folders (build and dist) and the spec file are created
properly with no errors. When I run the runatestapp.exe file in the command
prompt I got the error "Failed to execute script run_a_test".



I'm using the following software versions now:



Anaconda3 4.1.1.64-bit

Python 3.5.2

PyInstaller 3.2

Windows 10 64-bit



Here is the Traceback info



Traceback (most recent call last):

  File "run_a_test.py", line 4, in 

from reportlab.pdfgen import canvas

  File "", line 969, in _find_and_load

  File "", line 958, in _find_and_load_unlocked

  File "", line 664, in _load_unlocked

  File "", line 634, in
_load_backward_compatible

  File
"C:\Users\Ernest\AppData\Local\Continuum\lib\site-packages\PyInstaller\loader\pyimo

d03_importers.py", line 389, in load_module

exec(bytecode, module.__dict__)

  File "site-packages\reportlab\pdfgen\canvas.py", line 19, in 

  File "", line 969, in _find_and_load

  File "", line 958, in _find_and_load_unlocked

  File "", line 664, in _load_unlocked

  File "", line 634, in
_load_backward_compatible

  File
"C:\Users\Ernest\AppData\Local\Continuum\lib\site-packages\PyInstaller\loader\pyimo

d03_importers.py", line 389, in load_module

exec(bytecode, module.__dict__)

  File "site-packages\reportlab\rl_config.py", line 131, in 

  File "site-packages\reportlab\rl_config.py", line 102, in _startUp

  File "site-packages\reportlab\lib\utils.py", line 695, in rl_isdir

AttributeError: 'FrozenImporter' object has no attribute '_files'

Failed to execute script run_a_test



Any help is much appreciated. Feel free to contact me at any time you need.



Thank you,



Ernest Bonat, Ph.D.

Senior Software Engineer

Senior Data Scientist
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: What's the best way to minimize the need of run time checks?

2016-08-12 Thread BartC

On 12/08/2016 23:12, Lawrence D’Oliveiro wrote:

On Friday, August 12, 2016 at 9:39:11 PM UTC+12, BartC wrote:


'year' has been spelled wrongly


That’s why Python has __slots__.


OK. So when I said:

>
> My example was specifically about attribute names where pre-declaring
> the set of allowed attributes would not be onerous, would be usefully
> self-documenting, and would allow more errors to be picked up.

it turns out someone implemented just that!

Although it doesn't sound like Python to bolt-on some random feature 
that reins in its dynamic capabilities. Most people here seem to be 
against limiting the language in any way.


> (Of course the design of Python makes that impractical because it
> would
> require the byte-code compiler to see inside imported modules before
> execution is commenced.)

And the scheme I had in mind would detect the problem at compile-time.

The '__slots__' feature can't do that (unless someone uses a very clever 
'linter'), but it will actually detect an attribute mismatch at runtime 
instead of silently creating an extraneous one.


--
Bartc


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


Re: What's the best way to minimize the need of run time checks?

2016-08-12 Thread Juan Pablo Romero Méndez
2016-08-12 1:10 GMT-07:00 Lawrence D’Oliveiro :

> On Thursday, August 11, 2016 at 8:33:41 AM UTC+12, Juan Pablo Romero
> Méndez wrote:
>
> > I've been trying to find (without success so far) an example of a
> situation
> > where the dynamic features of a language like Python provides a clear
> > advantage over languages with more than one type.
>
> I have used, and continue to use, both static and dynamic languages.
>
> With static languages, once a piece of code compiles without errors, you
> have a slightly higher level of confidence in its correctness than with a
> dynamic language.
>
> On the other hand, a dynamic language allows me to be much more
> productive, because I have to write less code to begin with.
>

From your point of view, dynamic languages are more concise?


>
> The closest I can offer for an apples-to-apples comparison is PyCairo <
> https://github.com/ldo/pycairo> versus Qahirah  qahirah>. Both are Python bindings for the Cairo graphics library; the
> former is written in C as a Python extension module, the latter is done in
> pure Python using ctypes.
>
> I didn’t write the former; I merely tried to rescue it from abandonment to
> see if I could fill in a few more missing features. And what I found was,
> it would be quicker to start again from scratch than to continue working on
> it.
> --
> https://mail.python.org/mailman/listinfo/python-list
>
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Asynchronous programming

2016-08-12 Thread Steven D'Aprano
On Fri, 12 Aug 2016 03:47 pm, Paul Rudin wrote:

> Steven D'Aprano  writes:
> 
>> Thanks to everyone who has answered, I think I'm slowly starting to get
>> it now. Let's see if we can come up with a toy example that doesn't
>> involve low-level socket programming :-)
>>
>> Let me simulate a slow function call:

[...]
> They're not separate processes or threads, just think tasks that suspend
> and restart cooperatively. You need some kind of event loop to
> orchestrate running the tasks. Asyncio provides one.
> 
> You can do your example like this:
> 
> import asyncio, random
> 
> async def work(id):
> print("starting with id", id)
> workload = random.randint(5, 15)
> for i in range(workload):
> await asyncio.sleep(0.2)  # pretend to do some real work
> print("processing id", id)  # let the user see some progress
> print("done with id", id)
> return 10 + id
> 
> loop = asyncio.get_event_loop()
> 
> pending = [1, 2, 3, 4]
> 
> jobs = [asyncio.ensure_future(work(n)) for n in pending]
>  
> loop.run_until_complete(asyncio.gather(*jobs))
> 
> loop.close()


That is *awesome*. Thank you for the example!





-- 
Steve
“Cheer up,” they said, “things could be worse.” So I cheered up, and sure
enough, things got worse.

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


Re: A strange list concatenation result

2016-08-12 Thread Steven D'Aprano
On Sat, 13 Aug 2016 06:44 am, Mok-Kong Shen wrote:

> list2 = [1,2,3]
> list1 += [4,5,6]
> print(list1, list2)
>> [1, 2, 3, 4, 5, 6] [1, 2, 3]
>>
>>
>> Does that help?
> 
> I don't yet understand why in my 2nd example list2 came out as
> [1, 2, 3] outside.

Because you assign list2 = [1, 2, 3]. What did you expect it to be?




-- 
Steve
“Cheer up,” they said, “things could be worse.” So I cheered up, and sure
enough, things got worse.

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


Re: What's the best way to minimize the need of run time checks?

2016-08-12 Thread Steven D'Aprano
On Sat, 13 Aug 2016 08:12 am, Lawrence D’Oliveiro wrote:

> On Friday, August 12, 2016 at 9:39:11 PM UTC+12, BartC wrote:
> 
>> 'year' has been spelled wrongly
> 
> That’s why Python has __slots__.

No, that is OFFICIALLY *not* why Python has __slots__.

Python has __slots__ in order to support applications where you have huge
numbers of small objects where the collective memory used by millions of
unused __dicts__ is significant.

You may choose to (ab)use __slots__ to avoid the caller adding new
attributes to your objects, but that practice is discouraged.




-- 
Steve
“Cheer up,” they said, “things could be worse.” So I cheered up, and sure
enough, things got worse.

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


Re: Asynchronous programming

2016-08-12 Thread Paul Rubin
Steven D'Aprano  writes:
>> await asyncio.sleep(0.2)  # pretend to do some real work
> That is *awesome*. Thank you for the example!

Keep in mind that the above basically takes the task off the list of
runnables for 0.2 seconds, so it sits doing nothing and doesn't
interfere with other tasks running.  In the case where you're doing
actual computation, you want to yield (await) much more often than 0.2
sec, so other tasks can stay responsive.  This is cooperative
multitasking, where each task has to be nice to the others, in contrast
with preemptive multitasking where the task switches happen behind the
scenes.
-- 
https://mail.python.org/mailman/listinfo/python-list


Why my image cannot be displayed?

2016-08-12 Thread huey . y . jiang
Hi All,

Image display drives me crazy. After I get it displayed, and want to do the job 
with a class, display failed again. Please take a look at my trial code:

from Tkinter import *

class imageDisplay:
def __init__(self, parent=None):
canvas = Canvas(width=400, height=300, bg='beige')
canvas.pack()
self.canvas = canvas
img = PhotoImage(file="xxx.gif")
self.canvas.create_image(150, 0, image=img, anchor=NW)

if __name__ == '__main__':
imageDisplay()
mainloop()

Result:  A blank canvas was displayed. No error message. 

Can somebody help? Thanks so much!


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


Re: Why my image cannot be displayed?

2016-08-12 Thread MRAB

On 2016-08-13 02:40, huey.y.ji...@gmail.com wrote:

Hi All,

Image display drives me crazy. After I get it displayed, and want to do the job 
with a class, display failed again. Please take a look at my trial code:

from Tkinter import *

class imageDisplay:
def __init__(self, parent=None):
canvas = Canvas(width=400, height=300, bg='beige')
canvas.pack()
self.canvas = canvas
img = PhotoImage(file="xxx.gif")
self.canvas.create_image(150, 0, image=img, anchor=NW)

if __name__ == '__main__':
imageDisplay()
mainloop()

Result:  A blank canvas was displayed. No error message.

Can somebody help? Thanks so much!


You need to keep a reference to the image, so add:

self.img = img

otherwise, when __init__ returns, it'll be garbage-collected.

You also need to keep a reference to the instance created by 
imageDisplay(), so:


display = imageDisplay()

This is because the internals of tkinter is actually written in Tcl 
(another programming language), so although Tcl might have a reference 
to the window you're seeing, Python itself won't. (Why Tcl maintains a 
reference to the window, but not the image that's in the window, is a 
mystery... :-))


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


Re: What's the best way to minimize the need of run time checks?

2016-08-12 Thread breamoreboy
On Friday, August 12, 2016 at 2:31:01 PM UTC+1, Michael Torrie wrote:
> On 08/12/2016 05:07 AM, Steven D'Aprano wrote:
> > The first time I ever compiled a full-sized application (not a particular
> > large one either, it was a text editor a little more featureful than
> > Notepad) it took something like nine hours to compile on a Mac SE (this was
> > circa 1990). How mad would I have been if, eight hours and thirty minutes
> > into the compilation, the compiler suddenly stopped with an error caused by
> > a mistyped variable name?
> 
> Surely this application was built using a build system, even back then,
> that would allow compilation to resume and not rebuild object files that
> were already built.

Are you one of those extremely lucky people who worked on projects in which the 
header files never, ever changed?

Kindest regards.

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


Re: What's the best way to minimize the need of run time checks?

2016-08-12 Thread Lawrence D’Oliveiro
On Saturday, August 13, 2016 at 2:33:55 PM UTC+12, bream...@gmail.com wrote:
>> Surely this application was built using a build system, even back then,
>> that would allow compilation to resume and not rebuild object files that
>> were already built.
> 
> Are you one of those extremely lucky people who worked on projects in which
> the header files never, ever changed?

Or we use languages where imports are considered a build dependency, and 
“header files” are just a bad dream...
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Win32 API in pywin32

2016-08-12 Thread Lawrence D’Oliveiro
On Friday, August 5, 2016 at 11:58:05 AM UTC+12, I wrote:
> 
> Are people still using Win32? I thought Windows went 64-bit years ago.

Here  is 
a little titbit I was looking for:

Win32 has a function for getting the size of a file. File sizes on
Windows are limited to 2^64 bytes, and so they need a 64-bit integer to
be expressed easily. But the API call to get the size of a file doesn't
give you a 64-bit value. Instead, it gives you a pair of 32-bit values
that have to be combined in a particular way. For 32-bit Windows, that's
sort of understandable; 32-bit Windows is, well, 32-bit, so you might not
expect to be able to use 64-bit integers. But if you use the same API in
64-bit Windows, it still gives you the pair of numbers, rather than just
a nice simple 64-bit number. While this made some kind of sense on 32-bit
Windows, it makes no sense at all on 64-bit Windows, since 64-bit Windows
can, by definition, use 64-bit numbers.

This is why it’s still called “Win32” and not “Win64”...
-- 
https://mail.python.org/mailman/listinfo/python-list