Re: Cycling through iterables diagonally

2016-02-26 Thread Jussi Piitulainen
Pablo Lucena writes:

> Hello,
>
> I am trying to accomplish the following:
>
> Say I have a group of 4 lists as follows:
>
> l1 = ['a1', 'a2', 'a3', 'a4']
> l2 = ['b1', 'b2', 'b3', 'b4']
> l3 = ['c1', 'c2', 'c3', 'c4']
> l4 = ['d1', 'd2', 'd3', 'd4']
>
> I would like to cycle through these lists "diagonally" in groups of
> len(list) (in this example, each list has 4 items).
>
> cycle1: a1, b2, b3, b4
> cycle2: a2, b3, c4, d1
> cycle3: a3, b4, c1, d2
> cycle4: a4, b1, c2, d3

First line should be a1, b2, c3, d4, right?

> The way I thought about doing this is as follows:
>
> from collections import deque
> from itertools import cycle

[...]

> Prior to this I was mucking around with index counting while looping,
> and popping lists out of a deque, popping an item out of the list, and
> appending the list back into the deque during each iteration.
>
> Is there a better/cleaner way to do this? I was hoping for some cool
> itertools logic =)

Here's a combination of itertools (chaining of slices for the rotations)
and builtin machinery (argument spreading, a generator expression, zip,
enumerate) that seems to have the desired effect.

from itertools import chain, islice

l1 = ['a1', 'a2', 'a3', 'a4']
l2 = ['b1', 'b2', 'b3', 'b4']
l3 = ['c1', 'c2', 'c3', 'c4']
l4 = ['d1', 'd2', 'd3', 'd4']

n = 4
chainfrom = chain.from_iterable
print(*chainfrom(zip(*(chain(islice(m, k, n),
 islice(m, 0, k))
   for k, m
   in enumerate([l1, l2, l3, l4])

# prints: a1 b2 c3 d4 a2 b3 c4 d1 a3 b4 c1 d2 a4 b1 c2 d3
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Cycling through iterables diagonally

2016-02-26 Thread Peter Otten
Pablo Lucena wrote:

> Say I have a group of 4 lists as follows:
> 
> l1 = ['a1', 'a2', 'a3', 'a4']
> l2 = ['b1', 'b2', 'b3', 'b4']
> l3 = ['c1', 'c2', 'c3', 'c4']
> l4 = ['d1', 'd2', 'd3', 'd4']
> 
> I would like to cycle through these lists "diagonally" in groups of
> len(list) (in this example, each list has 4 items).

> Prior to this I was mucking around with index counting while looping, and
> popping lists out of a deque, popping an item out of the list, and
> appending the list back into the deque during each iteration.
> 
> Is there a better/cleaner way to do this? I was hoping for some cool
> itertools logic =)

I have a weak spot for the itertools myself, but I think in terms of clarity 
it is hard to beat the conventional

def diagonals(a):
N = len(a)
for i in range(N):
for k in range(N):
yield a[k][(k+i)%N]

print(list(diagonals([l1, l2, l3, l4])))

Of course that's as uncool as it can get ;)

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


Re: Cycling through iterables diagonally

2016-02-26 Thread Mark Lawrence

On 26/02/2016 09:59, Peter Otten wrote:

Pablo Lucena wrote:


Say I have a group of 4 lists as follows:

l1 = ['a1', 'a2', 'a3', 'a4']
l2 = ['b1', 'b2', 'b3', 'b4']
l3 = ['c1', 'c2', 'c3', 'c4']
l4 = ['d1', 'd2', 'd3', 'd4']

I would like to cycle through these lists "diagonally" in groups of
len(list) (in this example, each list has 4 items).



Prior to this I was mucking around with index counting while looping, and
popping lists out of a deque, popping an item out of the list, and
appending the list back into the deque during each iteration.

Is there a better/cleaner way to do this? I was hoping for some cool
itertools logic =)


I have a weak spot for the itertools myself, but I think in terms of clarity
it is hard to beat the conventional

def diagonals(a):
 N = len(a)
 for i in range(N):
 for k in range(N):
 yield a[k][(k+i)%N]

print(list(diagonals([l1, l2, l3, l4])))

Of course that's as uncool as it can get ;)



It might be uncool, but at least I can read it, unlike the earlier Jussi 
Piitulainen answer that made my head spin :)


--
My fellow Pythonistas, ask not what our language can do for you, ask
what you can do for our language.

Mark Lawrence

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


How to remove the line numbers from the file in python

2016-02-26 Thread Ganesh Pal
what would be the easiest way to remove the lines in the leading
numbers 1.e 1 ,2, 19  from this file  using python ?


  1 import os
  2 Suite = "Test Mail"
  3
  4 def sendMail(x):
  5 text = x
  6 sendmail_location = "/home/prasad/onefs/share/sendmail" #
sendmail location
  7 p = os.popen("%s -t" % sendmail_location, "w")
  8 p.write("From: %s\n" % "[email protected]")
  9 p.write("To: %s\n" % "[email protected]")
10 #p.write("To: %s\n" % "[email protected]")
11 p.write("Subject: Suite : %s \n" % (Suite))
12 p.write("\n") # blank line separating headers from body
13 p.write("%s" %text)
14 status = p.close()
15
16 if status != 0:
17 print "Sendmail exit status", status
18
19 sendMail("Test Mail")

Regards,
Ganesh
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: How to remove the line numbers from the file in python

2016-02-26 Thread Peter Otten
Ganesh Pal wrote:

> what would be the easiest way to remove the lines in the leading
> numbers 1.e 1 ,2, 19  from this file  using python ?
> 
> 
>   1 import os
>   2 Suite = "Test Mail"
>   3
>   4 def sendMail(x):
>   5 text = x
>   6 sendmail_location = "/home/prasad/onefs/share/sendmail" #
> sendmail location
>   7 p = os.popen("%s -t" % sendmail_location, "w")
>   8 p.write("From: %s\n" % "[email protected]")
>   9 p.write("To: %s\n" % "[email protected]")
> 10 #p.write("To: %s\n" % "[email protected]")
> 11 p.write("Subject: Suite : %s \n" % (Suite))
> 12 p.write("\n") # blank line separating headers from body
> 13 p.write("%s" %text)
> 14 status = p.close()
> 15
> 16 if status != 0:
> 17 print "Sendmail exit status", status
> 18
> 19 sendMail("Test Mail")

sys.stdout.writelines(
line.lstrip().lstrip("0123456789")[1:] or "\n" 
for line in sys.stdin)

Replace stdin/out with files as needed.

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


Re: How to remove the line numbers from the file in python

2016-02-26 Thread Peter Heitzer
Ganesh Pal  wrote:
>what would be the easiest way to remove the lines in the leading
>numbers 1.e 1 ,2, 19  from this file  using python ?
import sys,re
for line in sys.stdin:
print re.sub('^\d+','',line).rstrip()

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


subprocess startup error

2016-02-26 Thread Shweta Dinnimani
hi

hello, I'm begineer to python programming.. I had installed python 3.5.1
version on my windows 7 system. I was fine earlier and now when i was
trying the programs on string i'm facing the subprocess startup error. IDLE
is not connecting. And python shell is also not opening. I tried
uninstalling and installing the python shell but Im facing the
problem.Please do help me

-- 
Thank you
with best regards

Shweta Dinnimani
+91-8762650817
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: subprocess startup error

2016-02-26 Thread Shweta Dinnimani
forgot to mention..

i saved my file as string.py since than i'm facing this error

On Fri, Feb 26, 2016 at 6:50 PM, Shweta Dinnimani 
wrote:

> hi
>
> hello, I'm begineer to python programming.. I had installed python 3.5.1
> version on my windows 7 system. I was fine earlier and now when i was
> trying the programs on string i'm facing the subprocess startup error. IDLE
> is not connecting. And python shell is also not opening. I tried
> uninstalling and installing the python shell but Im facing the
> problem.Please do help me
>
> --
> Thank you
> with best regards
>
> Shweta Dinnimani
> +91-8762650817
>



-- 
Thank you
with best regards

Shweta Dinnimani
+91-8762650817
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: subprocess startup error

2016-02-26 Thread Peter Otten
Shweta Dinnimani wrote:

> forgot to mention..
> 
> i saved my file as string.py since than i'm facing this error

Rename that file to something that does not clash with the module names in 
the standard library, mystring.py for example, and IDLE should work again.

> On Fri, Feb 26, 2016 at 6:50 PM, Shweta Dinnimani 
> wrote:
> 
>> hi
>>
>> hello, I'm begineer to python programming.. I had installed python 3.5.1
>> version on my windows 7 system. I was fine earlier and now when i was
>> trying the programs on string i'm facing the subprocess startup error.
>> IDLE is not connecting. And python shell is also not opening. I tried
>> uninstalling and installing the python shell but Im facing the
>> problem.Please do help me


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


Re: Cycling through iterables diagonally

2016-02-26 Thread marco . nawijn
On Friday, February 26, 2016 at 8:44:38 AM UTC+1, Pablo Lucena wrote:
> Hello,
> 
> I am trying to accomplish the following:
> 
> Say I have a group of 4 lists as follows:
> 
> l1 = ['a1', 'a2', 'a3', 'a4']
> l2 = ['b1', 'b2', 'b3', 'b4']
> l3 = ['c1', 'c2', 'c3', 'c4']
> l4 = ['d1', 'd2', 'd3', 'd4']
> 
> I would like to cycle through these lists "diagonally" in groups of
> len(list) (in this example, each list has 4 items).
> 
> cycle1: a1, b2, b3, b4
> cycle2: a2, b3, c4, d1
> cycle3: a3, b4, c1, d2
> cycle4: a4, b1, c2, d3
> 
> The way I thought about doing this is as follows:
> 
> from collections import deque
> from itertools import cycle
> 
> l1 = deque(['a1', 'a2', 'a3', 'a4'])
> l2 = deque(['b1', 'b2', 'b3', 'b4'])
> l3 = deque(['c1', 'c2', 'c3', 'c4'])
> l4 = deque(['d1', 'd2', 'd3', 'd4'])
> 
> l1.rotate(-0)
> l2.rotate(-1)
> l3.rotate(-2)
> l4.rotate(-3)
> 
> groups = cycle([l1, l2, l3, l4])
> 
> In [115]: for group in groups:
>.: if not group:
>.: break
>.: print(group.popleft())
>.:
> a1
> b2
> c3
> d4
> a2
> b3
> c4
> d1
> a3
> b4
> c1
> d2
> a4
> b1
> c2
> d3
> 
> Prior to this I was mucking around with index counting while looping, and
> popping lists out of a deque, popping an item out of the list, and
> appending the list back into the deque during each iteration.
> 
> Is there a better/cleaner way to do this? I was hoping for some cool
> itertools logic =)
> 
> Thanks!
> 
> 
> -- 
> *Pablo*

Hello Pablo,

If you don't mind using third-party packages you 
could this quite straighforward with numpy:
(Python 2.7)

import numpy as np

a = np.array([
  ('a1', 'a2', 'a3', 'a4'),
  ('b1', 'b2', 'b3', 'b4'),
  ('c1', 'c2', 'c3', 'c4'),
  ('d1', 'd2', 'd3', 'd4')], dtype=str)

nrows, ncols = a.shape

for i in range(ncols):
print np.diag(np.roll(a, -i, axis=1))

This prints:

['a1' 'b2' 'c3' 'd4']
['a2' 'b3' 'c4' 'd1']
['a3' 'b4' 'c1' 'd2']
['a4' 'b1' 'c2' 'd3']

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


Re: Sorry, here are the details.

2016-02-26 Thread Oscar Benjamin
Please reply to the list rather than directly to me (and then you will
get a quicker response from someone else).

On 25 February 2016 at 17:13, Giriprasadh Raghavan
 wrote:
> I open the setup file of python 3.5.1 and click then install button. Then I
> am displayed an error that says:
>
> One or more issues caused the setup to fail. Please fix the issues an then
> retry the setup. For more info see the log file.
>
> 0x80070570 – The file or directory is corrupted and unreadable.
>
> (I actually downloaded it for the 3rd time as it said the file or directory
> is corrupted and unreadable.)

This problem (0x80070570) has been reported a few times:

https://mail.python.org/pipermail/python-list/2015-September/697447.html
https://mail.python.org/pipermail/python-list/2016-January/702590.html
https://mail.python.org/pipermail/python-list/2015-December/700998.html

However I haven't seen a fix for it. It is apparently due to problems
with corrupted files or with broken hard disks or something. I don't
remember hearing of this problem before Python 3.5 and AFAIK it only
occurs with 3.5.

Could you try installing 3.4 and let us know if that works? If so it
may be a bug (I don't see any tracker issues for it yet).

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


Re: subprocess startup error

2016-02-26 Thread Oscar Benjamin
On 26 February 2016 at 13:30, Peter Otten <[email protected]> wrote:
> Shweta Dinnimani wrote:
>>>
>>> hello, I'm begineer to python programming.. I had installed python 3.5.1
>>> version on my windows 7 system. I was fine earlier and now when i was
>>> trying the programs on string i'm facing the subprocess startup error.
>>> IDLE is not connecting. And python shell is also not opening. I tried
>>> uninstalling and installing the python shell but Im facing the
>>> problem.Please do help me
>
>> forgot to mention..
>>
>> i saved my file as string.py since than i'm facing this error
>
> Rename that file to something that does not clash with the module names in
> the standard library, mystring.py for example, and IDLE should work again.

It's ridiculous that it's this easy to accidentally crash IDLE and the
Python shell.

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


Re: Looking for examples of using aiopg with aiohttp and non-async startup.

2016-02-26 Thread Ray Cote
Answer (obvious after a refreshing sleep):
Just run a separate async pool connection prior to kicking off the aiohttp
web application.
The startup now looks like:

async def connect():
return await aiopg.create_pool(…)

if __name__ == “__main__”:
loop = asyncio.get_event_loop()
pool = loop.run_until_complete(connect())
app = web.Application()
app["pool"] = pool
app.router.add_route(‘POST', '/pv/v1/', handle_v1)
web.run_app(app)


Then, in the handle_v1 code:
pool = request.app["pool"]
connection = await pool.acquire()
cursor = await connection.cursor()
—r

On Thu, Feb 25, 2016 at 5:23 PM, Ray Cote 
wrote:

> Hello:
>
> I have an aiohttp project that starts in the usual way:
>
> app = web.Application()
> app.router.add_route(‘POST”, ‘/‘, handler)
> web.run_app(app)
>
> My question is, how do I work with an aiopg.pool with aiohttp?
> There only seems to be async interfaces into aiopg — but I don’t want to
> create the pool in my handler since that requires a connection on each
> transaction.
>
> I found one example that was
>   app[“db”] = await aiopg.create_pool(dsn)
> but that doesn’t seen correct since we’re not yet in a loop.
>
> Can someone please provide an example showing the proper way to integrate
> aiopg pool into an aiohttp web application?
>
> Regards
> —Ray
>
> --
> Raymond Cote, President
> voice: +1.603.924.6079 email: [email protected] skype:
> ray.cote
>
>
>


-- 
Raymond Cote, President
voice: +1.603.924.6079 email: [email protected] skype:
ray.cote
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: subprocess startup error

2016-02-26 Thread Emile van Sebille

On 2/26/2016 6:49 AM, Oscar Benjamin wrote:

On 26 February 2016 at 13:30, Peter Otten <[email protected]> wrote:

Shweta Dinnimani wrote:

i saved my file as string.py since than i'm facing this error


Rename that file to something that does not clash with the module names in
the standard library, mystring.py for example, and IDLE should work again.


It's ridiculous that it's this easy to accidentally crash IDLE and the
Python shell.


I don't think there's a way to protect against it -- how could you work 
on string.py otherwise?


Emile



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


ANN: Wing IDE 5.1.10 released

2016-02-26 Thread Wingware

Hi,

Wingware has released version 5.1.10 of Wing IDE, our cross-platform 
integrated development environment for the Python programming language.


Wing IDE features a professional code editor with vi, emacs, visual 
studio, and other key bindings, auto-completion, call tips, 
context-sensitive auto-editing, goto-definition, find uses, refactoring, 
a powerful debugger, version control, unit testing, search, project 
management, and many other features.


This release includes the following minor improvements:

Fix issues with Django app creation
French localization updates (thanks to Jean Sanchez)
Improve analysis cache performance
Don't specify Qt dependencies in Debian packages
Fix occasional hangs when debugging multi-threaded code
Fix display of decimal instances in stack data
Don't hide the auto-completer when a debug process exits or on 
other events that cause update of menus and toolbars

Don't show duplicate entries for the same file in Compare to Repository
Fix crash triggered by some mako files
Fix problem with inspection of bound methods in Python 3
12 other bug fixes

For details see http://wingware.com/news/2016-02-25 and 
http://wingware.com/pub/wingide/5.1.10/CHANGELOG.txt


What's New in Wing 5.1:

Wing IDE 5.1 adds multi-process and child process debugging, syntax 
highlighting in the shells, support for pytest, Find Symbol in Project, 
persistent time-stamped unit test results, auto-conversion of indents on 
paste, an XCode keyboard personality, support for Flask, Django 1.7, 
1.8, and 1.9, Python 3.5 and recent Google App Engine versions, improved 
auto-completion for PyQt, recursive snippet invocation, and many other 
minor features and improvements.


Free trial: http://wingware.com/wingide/trial
Downloads: http://wingware.com/downloads
Feature list: http://wingware.com/wingide/features
Sales: http://wingware.com/store/purchase
Upgrades: https://wingware.com/store/upgrade

Questions?  Don't hesitate to email us at [email protected].

Thanks,

--

Stephan Deibel
Wingware | Python IDE

The Intelligent Development Environment for Python Programmers

wingware.com

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


Re: Pyraf problem

2016-02-26 Thread Arshpreet Singh
On Thursday, 25 February 2016 20:07:52 UTC+5:30, Sapna Mishra  wrote:
> Dear Sir/Mam, 
> 
> I am using python for my astronomy purpose, for that I want to use PyRaf, but 
> strange thing is occurring that pyraf is getting open as a root but out side 
> my root user directory when I am typing pyraf I am getting like; 
> No graphics/display possible for this session. 
> Tkinter import failed. 
> The numpy package is required by PyRAF and was not found. Please visit 
> http://numpy.scipy.org 

Hi Sapna, You can't run directly X-window system from root account, SO you are 
getting Tkinter error. 

You can try to install as follows:

sudo pip install pyraf

But befoore that you also need to install numpy as it is required for pyraf or 
scipy too.

SO you need to run 
sudo pip install numpy scipy

Above instructions will work only for Python2.X
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: subprocess startup error

2016-02-26 Thread Wolfgang Maier

On 26.02.2016 15:57, Emile van Sebille wrote:

On 2/26/2016 6:49 AM, Oscar Benjamin wrote:

On 26 February 2016 at 13:30, Peter Otten <[email protected]> wrote:

Shweta Dinnimani wrote:

i saved my file as string.py since than i'm facing this error


Rename that file to something that does not clash with the module
names in
the standard library, mystring.py for example, and IDLE should work
again.


It's ridiculous that it's this easy to accidentally crash IDLE and the
Python shell.


I don't think there's a way to protect against it -- how could you work
on string.py otherwise?



Emitting a warning message in such situations is being discussed in 
http://bugs.python.org/issue23809 though it is not a very lively discussion.


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


Re: subprocess startup error

2016-02-26 Thread Mark Lawrence

On 26/02/2016 15:08, Wolfgang Maier wrote:

On 26.02.2016 15:57, Emile van Sebille wrote:

On 2/26/2016 6:49 AM, Oscar Benjamin wrote:

On 26 February 2016 at 13:30, Peter Otten <[email protected]> wrote:

Shweta Dinnimani wrote:

i saved my file as string.py since than i'm facing this error


Rename that file to something that does not clash with the module
names in
the standard library, mystring.py for example, and IDLE should work
again.


It's ridiculous that it's this easy to accidentally crash IDLE and the
Python shell.


I don't think there's a way to protect against it -- how could you work
on string.py otherwise?



Emitting a warning message in such situations is being discussed in
http://bugs.python.org/issue23809 though it is not a very lively
discussion.



Thanks for the link.  I knew that I remembered a discussion about this 
but frustratingly couldn't find it for myself :(


--
My fellow Pythonistas, ask not what our language can do for you, ask
what you can do for our language.

Mark Lawrence

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


Reduce memory fragmentation with MALLOC_MMAP_THRESHOLD_ and MALLOC_MMAP_MAX_

2016-02-26 Thread Lorenzo Bolla
Hi all,

I've been experimenting with MALLOC_MMAP_THRESHOLD_ and MALLOC_MMAP_MAX_ env 
variables to affect memory management in a long-running Python 2 process.
See http://man7.org/linux/man-pages/man3/mallopt.3.html

I got the idea from this bug report: http://bugs.python.org/issue11849

The results I have are encouraging: memory fragmentation is reduced and the 
typical high-water mark visible in memory used by long-running processes is 
lower.

My only concern is if there are other side effects that may bite back, when 
using such low level tweaks. Does anyone have any experience in using them?

Here is an example script that shows how those variables affect RSS memory in a 
script that generate a large dictionary:
https://gist.github.com/lbolla/8e2640133032b0a6bb9c
Just run "alloc.sh" and compare the output.

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


The Real-Time Use of Python in Data Science World!

2016-02-26 Thread Anita Goyal
This course will help you to expertise the usage of Python in Data Science 
world.

Carter your Python Knowledge so that it can be utilized to get the Insights of 
Data using Methodologies and Techniques of Data Science...

Objective:
Understand the concepts of Data science and Python
You will be able to use Python in Discovering Data.
You will have an idea of Statistical and Analytical methods to deal with huge 
data sets.
You will gain an expertise on Regular Expressions, looping functions and 
concepts of Object Oriented Programming.
You will be able to create business algorithms and data models using Python and 
it's techniques.
Work on Real-life Projects will help you to get a practical experience of real 
scenarios of IT Industry.

Start learning Python for Data Science from basics to advance levels here...
https://goo.gl/070wXw
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: The Real-Time Use of Python in Data Science World!

2016-02-26 Thread Joel Goldstick
On Fri, Feb 26, 2016 at 2:35 PM, Anita Goyal 
wrote:

> This course will help you to expertise the usage of Python in Data Science
> world.
>
> Carter your Python Knowledge so that it can be utilized to get the
> Insights of Data using Methodologies and Techniques of Data Science...
>
> Objective:
> Understand the concepts of Data science and Python
> You will be able to use Python in Discovering Data.
> You will have an idea of Statistical and Analytical methods to deal with
> huge data sets.
> You will gain an expertise on Regular Expressions, looping functions and
> concepts of Object Oriented Programming.
> You will be able to create business algorithms and data models using
> Python and it's techniques.
> Work on Real-life Projects will help you to get a practical experience of
> real scenarios of IT Industry.
>
> Start learning Python for Data Science from basics to advance levels
> here...
> https://goo.gl/070wXw
> --
> https://mail.python.org/mailman/listinfo/python-list
>


Second post by this person.  Both advertising classes for a fee.  Is this
proper posting?  I tend to think it is spam
-- 
Joel Goldstick
http://joelgoldstick.com/ 
http://cc-baseballstats.info/
-- 
https://mail.python.org/mailman/listinfo/python-list


Request Help With Displaying XBM Image

2016-02-26 Thread Wildman via Python-list
As part of a program I am working on I want to display a
48x48 XBM image on the main window.  I have done a lot
of searching for code snippets and I found what appears
to be the correct way to do it using a Label.  So far I
have not been able to get it to work.  I have tried
different variations of the syntax but nothing will
display the image.  The Label will expand to size of
the image but nothing is there.  I have the background
of the Label set to white so it can be seen.

For experimentation I create a small program for the
sole purpose of displaying an XBM image.  The complete
script is pasted below.  I am thinking the problem 
might have something with the way the image object
is being created.  I'm probable making a dumb newbie
mistake.  BTW, if I run the script from a terminal
window, I do not get any errors.  Any guidance is
appreciated.


#!/usr/bin/python

from Tkinter import *
import tkFileDialog, os, Image, ImageTk

class cv():

default_dir = os.environ['HOME']

class Window(Frame):

def __init__(self, master = None):
Frame.__init__(self,master)
self.master = master
self.init_window()

def init_window(self):
self.master.title("XBM Image Test")
self.pack(fill=BOTH, expand=1)

self.openButton = Button(self,
 text="Open Image",
 width = 10,
 command=self.open_image)
self.openButton.place(x=50, y=100)

self.quitButton = Button(self,
 text="Quit",
 width=10,
 command=self.quit)
self.quitButton.place(x=50, y=140)

self.xbmImage = Label(self,
  bg="white")
self.xbmImage.place(x=80, y=25)

def open_image(self):
file_filter = [
('X BitMap', '*.xbm *.XBM'),
('all files', '*.*')
]
fileName = tkFileDialog.askopenfilename(parent=root,
initialdir=cv.default_dir,
filetypes=file_filter,
title="Open XBM Image")
if fileName:
cv.default_dir = os.path.dirname(fileName)
openImage = Image.open(fileName)
imageFile = ImageTk.BitmapImage(openImage)
self.xbmImage.config(image=imageFile)
else:
 return None

root = Tk()
root.minsize(width=200, height=180)
root.maxsize(width=200, height=180)
app = Window(root)
root.mainloop()

-- 
 GNU/Linux user #557453
May the Source be with you.
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: The Real-Time Use of Python in Data Science World!

2016-02-26 Thread Wildman via Python-list
On Fri, 26 Feb 2016 11:35:58 -0800, Anita Goyal wrote:

> This course (...)

What!?  No peas?  I like peas with spam.

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


Re: Request Help With Displaying XBM Image

2016-02-26 Thread Peter Otten
Wildman via Python-list wrote:

> As part of a program I am working on I want to display a
> 48x48 XBM image on the main window.  I have done a lot
> of searching for code snippets and I found what appears
> to be the correct way to do it using a Label.  So far I
> have not been able to get it to work.  I have tried
> different variations of the syntax but nothing will
> display the image.  The Label will expand to size of
> the image but nothing is there.  I have the background
> of the Label set to white so it can be seen.
> 
> For experimentation I create a small program for the
> sole purpose of displaying an XBM image.  The complete
> script is pasted below.  I am thinking the problem
> might have something with the way the image object
> is being created.  I'm probable making a dumb newbie
> mistake.  BTW, if I run the script from a terminal
> window, I do not get any errors.  Any guidance is
> appreciated.

It's not you, the program as you wrote it should and would show the image, 
were it not for an odd quirk in how images are handled in tkinter:

You have to keep an explicit reference of the Image to prevent it from being 
garbage-collected. Changing open_image() as follows

> def open_image(self):
> file_filter = [
> ('X BitMap', '*.xbm *.XBM'),
> ('all files', '*.*')
> ]
> fileName = tkFileDialog.askopenfilename(parent=root,
> initialdir=cv.default_dir,
> filetypes=file_filter,
> title="Open XBM Image")
> if fileName:
> cv.default_dir = os.path.dirname(fileName)
> openImage = Image.open(fileName)
  self.imageFile = ImageTk.BitmapImage(openImage)
  self.xbmImage.config(image=self.imageFile)
> else:
>  return None

should achieve that.

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


Bug in Python?

2016-02-26 Thread Sven R. Kunze

Hi everybody,

I recognized the following oddity (background story: 
http://srkunze.blogspot.com/2016/02/lets-go-down-rabbit-hole.html).


Python sometimes seems not to hop back and forth between C and Python code.

Can somebody explain this?


class MyList(list):
count = 0
def __setitem__(self, key, value):
self.count += 1
super(MyList, self).__setitem__(key, value)

# using heapq directly
from heapq import heappop
ml = MyList(range(10))
heappop(ml) # that's C
print(ml.count) # print 0


# using exact copy from heapq
from heapq import _siftup
def my_heappop(heap):
lastelt = heap.pop()
if heap:
returnitem = heap[0]
heap[0] = lastelt
_siftup(heap, 0)# that's C
return returnitem
return lastelt

ml = MyList(range(10))
my_heappop(ml)
print(ml.count) # print 6


Best,
Sven

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


Re: Bug in Python?

2016-02-26 Thread Ian Kelly
On Fri, Feb 26, 2016 at 3:08 PM, Sven R. Kunze  wrote:
> Python sometimes seems not to hop back and forth between C and Python code.

C code as a rule tends to ignore dunder methods. Those are used to
implement Python operations, not C operations.

> _siftup(heap, 0)# that's C

Your comment here appears to be incorrect.

>>> from heapq import _siftup
>>> type(_siftup)

>>> import inspect
>>> print(inspect.getsource(_siftup))
def _siftup(heap, pos):
endpos = len(heap)
startpos = pos
newitem = heap[pos]
# Bubble up the smaller child until hitting a leaf.
childpos = 2*pos + 1# leftmost child position
while childpos < endpos:
# Set childpos to index of smaller child.
rightpos = childpos + 1
if rightpos < endpos and not heap[childpos] < heap[rightpos]:
childpos = rightpos
# Move the smaller child up.
heap[pos] = heap[childpos]
pos = childpos
childpos = 2*pos + 1
# The leaf at pos is empty now.  Put newitem there, and bubble it up
# to its final resting place (by sifting its parents down).
heap[pos] = newitem
_siftdown(heap, startpos, pos)


So I would guess that the difference here is because one
implementation is entirely C, and the other implementation is entirely
Python.
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Request Help With Displaying XBM Image

2016-02-26 Thread Wildman via Python-list
On Fri, 26 Feb 2016 22:49:58 +0100, Peter Otten wrote:

> Wildman via Python-list wrote:

> It's not you, the program as you wrote it should and would show the image, 
> were it not for an odd quirk in how images are handled in tkinter:
> 
> You have to keep an explicit reference of the Image to prevent it from being 
> garbage-collected. Changing open_image() as follows
> 
>> def open_image(self):
>> file_filter = [
>> ('X BitMap', '*.xbm *.XBM'),
>> ('all files', '*.*')
>> ]
>> fileName = tkFileDialog.askopenfilename(parent=root,
>> initialdir=cv.default_dir,
>> filetypes=file_filter,
>> title="Open XBM Image")
>> if fileName:
>> cv.default_dir = os.path.dirname(fileName)
>> openImage = Image.open(fileName)
>   self.imageFile = ImageTk.BitmapImage(openImage)
>   self.xbmImage.config(image=self.imageFile)
>> else:
>>  return None
> 
> should achieve that.

You, Sir, are a scholar and a gentleman.  Your change
worked perfectly.  A thousand thank-you's.

-- 
 GNU/Linux user #557453
"Be at war with your vices, at peace with your neighbors,
and let every new year find you a better man."
  -Benjamin Franklin
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Bug in Python?

2016-02-26 Thread eryk sun
 On Fri, Feb 26, 2016 at 4:08 PM, Sven R. Kunze  wrote:
> Python sometimes seems not to hop back and forth between C and Python code.
> Can somebody explain this?

Normally a C extension would call PySequence_SetItem, which would call
the type's sq_ass_item, which for MyList is slot_sq_ass_item. The
latter function bridges the CPython and Python sides by binding and
calling the overridden __setitem__ method.  However, the _heapq
extension module uses `PyList_SET_ITEM(heap, 0, lastelt)`. This macro
expands to `((PyListObject *)(heap))->ob_item[0] = lastelt`. This
directly modifies the internal ob_item array of the list, so the
overridden __setitem__ method is never called. I presume it was
implemented like this with performance in mind, but I don't know
whether or not that justifies the loss of generality.
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Bug in Python?

2016-02-26 Thread eryk sun
On Fri, Feb 26, 2016 at 4:37 PM, Ian Kelly  wrote:
> So I would guess that the difference here is because one
> implementation is entirely C, and the other implementation is entirely
> Python.

Exactly, the C implementation of siftup is only called internally. So
there's no need to export it as a function. Calling the C heappop
calls this internal siftup implementation, which uses the
_PyList_ITEMS macro to reference the list's internal ob_item array.
Thus all of the assignment operations party directly on the array
instead of using the abstract API.
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Bug in Python?

2016-02-26 Thread mentificium
On Friday, February 26, 2016 at 2:09:07 PM UTC-8, Sven R. Kunze wrote:
> Hi everybody,
> 
> I recognized the following oddity (background story: 
> http://srkunze.blogspot.com/2016/02/lets-go-down-rabbit-hole.html).
> 
> Python sometimes seems not to hop back and forth between C and Python code.
> [...]

http://srkunze.blogspot.com/2016/02/lets-go-down-rabbit-hole.html ?

If there are too many bugs in Python, 
you could switch to Perl (Perl6 just came out :-)
especially for artificial intelligence (Strong AI) 
and for webservers running a "Ghost" AI Mind. See 

http://www.sourcecodeonline.com/details/ghost_perl_webserver_strong_ai.html 

or you could use "AI Steps" to code Strong AI in Python.

Arthur
-- 
http://ai.neocities.org/AiSteps.html 
http://www.amazon.com/dp/B00FKJY1WY 
http://mind.sourceforge.net/python.html 
http://www.sourcecodeonline.com/details/ghost_perl_webserver_strong_ai.html 
-- 
https://mail.python.org/mailman/listinfo/python-list