Re: How to debug an unfired tkinter event?

2017-10-19 Thread Peter Otten
[email protected] wrote:

> In last few days, I tried to experiment with the scrolling table
> implemented in canvas, started from this example:
> http://code.activestate.com/recipes/580793-tkinter-table-with-scrollbars/.
> Everything works fine until I moved the scrolling_area instance (which the
> canvas is in) from column=0 to column=1.

It's not clear to me what you mean with this. Did you place the table from 
the recipe elsewhere inside a window that you created or did you make 
changes in the recipe's code?
 
> The canvas has a binding:
> self.canvas.bind('', self._on_canvas_configure)
> 
> After this movement, the callback was only triggered when dragging the
> root widget to resize canvas vertically, but not horizontally.
> 
> Event seems has OS involved(it's MS Windows XP in my case). How to debug
> this kind of problem, under pdb?

I don't know, but if you can post a small example script that demonstrates 
the problem, and you are lucky, someone will see the problem.

At the very least we can check if it occurs on your oldish platform (what 
Python version?) only.


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


What happens when a __call__ function is defined in both class and object ?

2017-10-19 Thread ast

Hello, please have a look at following code snippet
(python 3.4.4)

class Test:

   a = 1

   def __init__(self):
   self.a = 2
   self.f = lambda : print("f from object")
   self.__call__ = lambda : print("__call__ from object")
 
   def __call__(self):

   print("__call__ from class Test")
 
   def f(self):

   print("f from class Test")

test=Test()


test.a

2
ok, a is defined in both the class and the object, it is read from
the object. This is the expected behavior


test.f()

f from object

ok for the same reason


test()

__call__ from class Test


Surprisingly, __call__ from the class is called, not the
one defined in the object. Why ?

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


Re: What happens when a __call__ function is defined in both class and object ?

2017-10-19 Thread Chris Angelico
On Thu, Oct 19, 2017 at 7:11 PM, ast  wrote:
> Hello, please have a look at following code snippet
> (python 3.4.4)
>
> class Test:
>
>a = 1
>
>def __init__(self):
>self.a = 2
>self.f = lambda : print("f from object")
>self.__call__ = lambda : print("__call__ from object")
> def __call__(self):
>print("__call__ from class Test")
> def f(self):
>print("f from class Test")
>
> test=Test()
>
 test.a
>
> 2
> ok, a is defined in both the class and the object, it is read from
> the object. This is the expected behavior
>
 test.f()
>
> f from object
>
> ok for the same reason
>
 test()
>
> __call__ from class Test
>
>
> Surprisingly, __call__ from the class is called, not the
> one defined in the object. Why ?

That's the way most dunder methods (those with "d"ouble "under"scores
before and after their names) work.
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: What happens when a __call__ function is defined in both class and object ?

2017-10-19 Thread Chris Angelico
On Thu, Oct 19, 2017 at 7:24 PM, Chris Angelico  wrote:
> On Thu, Oct 19, 2017 at 7:11 PM, ast  wrote:
>> Hello, please have a look at following code snippet
>> (python 3.4.4)
>>
>> class Test:
>>
>>a = 1
>>
>>def __init__(self):
>>self.a = 2
>>self.f = lambda : print("f from object")
>>self.__call__ = lambda : print("__call__ from object")
>> def __call__(self):
>>print("__call__ from class Test")
>> def f(self):
>>print("f from class Test")
>>
>> test=Test()
>>
> test.a
>>
>> 2
>> ok, a is defined in both the class and the object, it is read from
>> the object. This is the expected behavior
>>
> test.f()
>>
>> f from object
>>
>> ok for the same reason
>>
> test()
>>
>> __call__ from class Test
>>
>>
>> Surprisingly, __call__ from the class is called, not the
>> one defined in the object. Why ?
>
> That's the way most dunder methods (those with "d"ouble "under"scores
> before and after their names) work.

Oops, premature send, sorry!

Dunder methods are looked up on the class. In the extremely rare
situation where you want a per-instance customization, you can do it
yourself with something like this:

class Test:
def __call__(self):
if hasattr(self, "spam_handler"):
return self.spam_handler()
return generic_spam_handler()

It's more efficient if, most of the time, the instance dict is skipped
and the search starts with the class.

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


Re: How to debug an unfired tkinter event?

2017-10-19 Thread jfong
Peter Otten at 2017-10-19 UTC+8 PM 3:24:30 wrote:
> It's not clear to me what you mean with this. Did you place the table from 
> the recipe elsewhere inside a window that you created or did you make 
> changes in the recipe's code?

Thank you, Peter. I am using Python 3.4.4 under WinXP. 

When running the file directly from download, I get a table scrolling 
vertically only. If its Table class's __init__ parameter "scroll_horizontally" 
changed to True, it can be scrolled horizontally also. But there is a problem 
when scrolled horizontally, the header field will not align with data field 
anymore. I make some modifications to make it does. So far so good.

Later, I want to make the table also has a vertical header. The first step I 
had taken was to move all 2nd top-level widgets(not much, just four) to right 
one column further to make room for this new widget. I though it's a simple 
work, just increase their grid column value by 1. But Murphy's Law comes, the 
xscrollbar even don't show up when the table was squeezed horizontally.

> > The canvas has a binding:
> > self.canvas.bind('', self._on_canvas_configure)
> > 
> > After this movement, the callback was only triggered when dragging the
> > root widget to resize canvas vertically, but not horizontally.
> > 
> > Event seems has OS involved(it's MS Windows XP in my case). How to debug
> > this kind of problem, under pdb?
> 
> I don't know, but if you can post a small example script that demonstrates 
> the problem, and you are lucky, someone will see the problem.

This is a ~5xx lines file and I think it's not fare to ask forum members to 
look through it. So I decide to ask for help on how to debug it, instead of the 
solution.

I try to change the binding to
 self.bind_all('', self._on_canvas_configure)
and add a line into the callback
 print(event.widget)

I got some info below each time when I squeeze the table:
.
.5006
.5006.50712528
.5006.50712496
.5006.50712464
.5006.50712144
.5006.50712528.50712560.50782256
.5006.50712528.50712560.50782256.50783024
.5006.50712528.50712560.50782256
.5006.50712528.50712560.50782256.50783024

How to change these number(is it a widget ID?) to a meaning name?

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


Re: How to debug an unfired tkinter event?

2017-10-19 Thread Peter Otten
[email protected] wrote:

> Peter Otten at 2017-10-19 UTC+8 PM 3:24:30 wrote:
>> It's not clear to me what you mean with this. Did you place the table
>> from the recipe elsewhere inside a window that you created or did you
>> make changes in the recipe's code?
> 
> Thank you, Peter. I am using Python 3.4.4 under WinXP.
> 
> When running the file directly from download, I get a table scrolling
> vertically only. If its Table class's __init__ parameter
> "scroll_horizontally" changed to True, it can be scrolled horizontally
> also. But there is a problem when scrolled horizontally, the header field
> will not align with data field anymore. I make some modifications to make
> it does. So far so good.
> 
> Later, I want to make the table also has a vertical header. The first step
> I had taken was to move all 2nd top-level widgets(not much, just four) to
> right one column further to make room for this new widget. I though it's a
> simple work, just increase their grid column value by 1. But Murphy's Law
> comes, the xscrollbar even don't show up when the table was squeezed
> horizontally.

Thank you for the clarification; I understand your problem much better now.

> 
>> > The canvas has a binding:
>> > self.canvas.bind('', self._on_canvas_configure)
>> > 
>> > After this movement, the callback was only triggered when dragging the
>> > root widget to resize canvas vertically, but not horizontally.
>> > 
>> > Event seems has OS involved(it's MS Windows XP in my case). How to
>> > debug this kind of problem, under pdb?
>> 
>> I don't know, but if you can post a small example script that
>> demonstrates the problem, and you are lucky, someone will see the
>> problem.
> 
> This is a ~5xx lines file and I think it's not fare to ask forum members
> to look through it. So I decide to ask for help on how to debug it,
> instead of the solution.
> 
> I try to change the binding to
>  self.bind_all('', self._on_canvas_configure)
> and add a line into the callback
>  print(event.widget)
> 
> I got some info below each time when I squeeze the table:
> .
> .5006
> .5006.50712528
> .5006.50712496
> .5006.50712464
> .5006.50712144
> .5006.50712528.50712560.50782256
> .5006.50712528.50712560.50782256.50783024
> .5006.50712528.50712560.50782256
> .5006.50712528.50712560.50782256.50783024
> 
> How to change these number(is it a widget ID?) to a meaning name?

That's the name of the widget in the underlying tcl/tk. You can specify it 
in Python:

>>> import tkinter as tk
>>> print(tk.Button())
.139817813335904
>>> print(tk.Button(name="mybutton"))
.mybutton

You have to ensure that names are unique.

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


Re: How to debug an unfired tkinter event?

2017-10-19 Thread jfong
Peter Otten於 2017年10月19日星期四 UTC+8下午6時04分39秒寫道:
> [email protected] wrote:
> 
> > Peter Otten at 2017-10-19 UTC+8 PM 3:24:30 wrote:
> >> It's not clear to me what you mean with this. Did you place the table
> >> from the recipe elsewhere inside a window that you created or did you
> >> make changes in the recipe's code?
> > 
> > Thank you, Peter. I am using Python 3.4.4 under WinXP.
> > 
> > When running the file directly from download, I get a table scrolling
> > vertically only. If its Table class's __init__ parameter
> > "scroll_horizontally" changed to True, it can be scrolled horizontally
> > also. But there is a problem when scrolled horizontally, the header field
> > will not align with data field anymore. I make some modifications to make
> > it does. So far so good.
> > 
> > Later, I want to make the table also has a vertical header. The first step
> > I had taken was to move all 2nd top-level widgets(not much, just four) to
> > right one column further to make room for this new widget. I though it's a
> > simple work, just increase their grid column value by 1. But Murphy's Law
> > comes, the xscrollbar even don't show up when the table was squeezed
> > horizontally.
> 
> Thank you for the clarification; I understand your problem much better now.
> 
> > 
> >> > The canvas has a binding:
> >> > self.canvas.bind('', self._on_canvas_configure)
> >> > 
> >> > After this movement, the callback was only triggered when dragging the
> >> > root widget to resize canvas vertically, but not horizontally.
> >> > 
> >> > Event seems has OS involved(it's MS Windows XP in my case). How to
> >> > debug this kind of problem, under pdb?
> >> 
> >> I don't know, but if you can post a small example script that
> >> demonstrates the problem, and you are lucky, someone will see the
> >> problem.
> > 
> > This is a ~5xx lines file and I think it's not fare to ask forum members
> > to look through it. So I decide to ask for help on how to debug it,
> > instead of the solution.
> > 
> > I try to change the binding to
> >  self.bind_all('', self._on_canvas_configure)
> > and add a line into the callback
> >  print(event.widget)
> > 
> > I got some info below each time when I squeeze the table:
> > .
> > .5006
> > .5006.50712528
> > .5006.50712496
> > .5006.50712464
> > .5006.50712144
> > .5006.50712528.50712560.50782256
> > .5006.50712528.50712560.50782256.50783024
> > .5006.50712528.50712560.50782256
> > .5006.50712528.50712560.50782256.50783024
> > 
> > How to change these number(is it a widget ID?) to a meaning name?
> 
> That's the name of the widget in the underlying tcl/tk. You can specify it 
> in Python:
> 
> >>> import tkinter as tk
> >>> print(tk.Button())
> .139817813335904
> >>> print(tk.Button(name="mybutton"))
> .mybutton
> 
> You have to ensure that names are unique.

Thank you. It's very helpful information.

By the way, I just found this problem can be easily seen by not modifing the 
original file too much with the following steps:

1. copy and get the file from the link.
2. change the "scroll_horizontally" parameter in the "Table" class's __init__ 
method to "True"

Running it you will see it perform correctly.

3. Change the "column=x" at line 268, 282, 288, 292, 303 to "column=x+1"

Now it has the problem.

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


efficient way to get a sufficient set of identifying attributes

2017-10-19 Thread Robin Becker
Given a list of objects with attributes a0, a1, a2,an-1 is there an 
efficient way to find sets of attributes which can be used to distinguish 
members of the list?


As example a list of people might have

firstName, lastName, nationality, postcode, phonenumber,

as attributes. The probe items may have some of these attributes, but which 
should be used to test. Presumably the information in any attribute is highest 
if the number of distinct occurrences is the the same as the list length and 
pairs of attributes are more likely to be unique, but is there some proper way 
to go about determining what tests to use?


A particular problem might be dynamic in that the list may be being constructed 
from the probes.

--
Robin Becker

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


Re: What happens when a __call__ function is defined in both class and object ?

2017-10-19 Thread Thomas Jollans
On 2017-10-19 10:11, ast wrote:
> Surprisingly, __call__ from the class is called, not the
> one defined in the object. Why ?

That's just how dunder methods like __call__ work.

See https://docs.python.org/3/reference/datamodel.html#special-method-names

To quote the docs:

> For instance, if a class defines a method named __getitem__(), and x is an 
> instance of this class, then x[i] is roughly equivalent to 
> type(x).__getitem__(x, i). 

This is also true for __call__. In your case, calling test is
more-or-less equivalent to getattr(type(test), '__call__')(test), NOT
getattr(test, '__call__')().


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


Re: efficient way to get a sufficient set of identifying attributes

2017-10-19 Thread Robin Becker

On 19/10/2017 16:42, Stefan Ram wrote:

Robin Becker  writes:

Presumably the information in any attribute is highest
if the number of distinct occurrences is the the same as the list length and
pairs of attributes are more likely to be unique, but is there some proper way
to go about determining what tests to use?


   When there is a list

|>>> list = [ 'b', 'b', 'c', 'd', 'c', 'b' ]
|>>> l = len( list )

   , the length of its set can be obtained:

|>>> s = len( set( list ))

   . The entries are unique if the length of the set is the
   length of the list

|>>> l == s
|False

   And the ratio between the length of the set and the length
   of the list can be used to quantify the amount of repetiton.

|>>> s / l
|0.5

...
this sort of makes sense for single attributes, but ignores the possibility of 
combining the attributes to make the checks more discerning.

--
Robin Becker

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


Re: Is there a function of ipaddress to get the subnet only from input like 192.168.1.129/25

2017-10-19 Thread Daniel Flick
The problem was with the validation code.  Within the python section of the 
template, the class IPv4Interface will throw an exception due to the invalid 
value during the validation process. Therefore, the server rejects the form 
data and the template is not created.

Solution: It would work if you add an error handling to the python section of 
the template.

<%!
## python module-level code
import ipaddress
%>
<%def name="get_address(ip_string)">
<%
try:
return ipaddress.IPv4Interface(ip_string).ip
except Exception:
return "FAIL_OR_EMPTY"
%>

! Variable Input: ${LAN_IP}
${get_address(LAN_IP)}
Explanation: During the server-side validation of the HTML form, the 
configuration template is rendered with a dummy parameter set to verify the 
syntax (see file app/forms.py, class ConfigTemplateForm). Your config template 
is validated with the following parameter set during the form validation:

{
"hostname": "test",
"LAN_IP": "test"
}

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


Re: right list for SIGABRT python binary question ?

2017-10-19 Thread Karsten Hilbert
On Wed, Oct 18, 2017 at 02:07:46PM +0200, Thomas Jollans wrote:

> > When run under a debug build it sayeth right away (simulated
> > as a minimal working example):
> > 
> > root@hermes:~/bin# python2.7-dbg
> > Python 2.7.14 (default, Sep 17 2017, 18:50:44)
> > [GCC 7.2.0] on linux2
> > Type "help", "copyright", "credits" or "license" for more 
> > information.
> > >>> import mx.DateTime
> > *** You don't have the (right) mxDateTime binaries installed !
> > Traceback (most recent call last):
> >   File "", line 1, in 
> >   File "/usr/lib/python2.7/dist-packages/mx/DateTime/__init__.py", 
> > line 8, in 
> > from DateTime import *
> >   File "/usr/lib/python2.7/dist-packages/mx/DateTime/DateTime.py", 
> > line 9, in 
> > from mxDateTime import *
> >   File 
> > "/usr/lib/python2.7/dist-packages/mx/DateTime/mxDateTime/__init__.py", line 
> > 13, in 
> > raise ImportError, why
> > ImportError: 
> > /usr/lib/python2.7/dist-packages/mx/DateTime/mxDateTime/mxDateTime.so: 
> > undefined symbol: Py_InitModule4
> 
> I don't really what exactly is going on here, but in general extension
> modules compiled for non-debug builds won't work with debug builds.

OK, that helps. I found -dbg builds of mx.DateTime in Debian.

> > For good measure I have filed a bug with Debian asking for
> > recompilation of python-egenix-mxdatetime.
> 
> Even if the maintainers do that, it won't help. Check that the module
> works on its own with the regular Python build and then close the bug if
> the maintainers don't beat you to it.

Done.

> > When run under "normal" py2.7 it runs all the way through but
> > upon shutdown (*after* sys.exit(0)) faulthandler shows a
> > problem (and returns 134 which made me think of SIGABRT):
> 
> We still don't know what "it" is.
> 
> Strip down your script as much as possible. It looks like you're using a
> lot of extension modules, and any one of them could (in theory) be
> causing problems.

I know. However, stripping down isn't quite so easy (it never
is, which is a rather lame excuse for not doing so yet :-)

The script itself is but a meager 1843 lines (but using lots
of Python modules, both stdlib and my own). What it does is
take a large number of SQL files (roughly a thousand) and
replaying them against PostgreSQL thereby bootstrapping a
database layout from scratch up to the current version (21).
The abort-on-exit only happens if "enough" SQL scripts have
been run (sounds like a resource leak) and the bootstrapper
script exits (having encountered a database problem or not
makes no difference).  Running various parts of the whole
procedure does not make a difference as to whether the fault
occurs when exiting, if only "enough" SQL scripts are run.

I am currently running the bootstrapper with mxdatetime as a
dbg build to see what gives. The only other C extension I am
aware of that is in use is psycopg2.

However, none of these two (nor any other modules) have been
added to the bootstrapper (or updated) recently (in fact,
this setup has been in use for, what, a decade?) so it is
quite unclear why they should be at fault now.

The one thing that did change is PostgreSQL going from 9.6 to
10, effecting a libpq5 recompiled against the newer PG
client. I wonder whether psycopg2 needs a recompile against
that libpq5 (despite there not having been changes advertised
by PG).

I guess I'll have to downgrade libpq5 to 9.6 and see what
happens.

I'll report what I find.

(oh, and it's all available on github)

Thanks,
Karsten
-- 
GPG key ID E4071346 @ eu.pool.sks-keyservers.net
E167 67FD A291 2BEA 73BD  4537 78B9 A9F9 E407 1346
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: efficient way to get a sufficient set of identifying attributes

2017-10-19 Thread Chris Angelico
On Fri, Oct 20, 2017 at 4:21 AM, Stefan Ram  wrote:
> Dennis Lee Bieber  writes:
>>Interesting -- that is coming out to be 2^size - 1, which will sure speed
>>up calculation for larger sets rather than doing all the factorial stuff.
>
>   A set of size n has 2^n subsets.
>
>   We exclude the empty set for our purpose, so we end at 2^n-1.

Why is this? Simple. In each subset, each element is either there or
not-there, independently of the others. So each one is 1 bit of
information.

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


Efficient counting of results

2017-10-19 Thread Israel Brewster
I am working on developing a report that groups data into a two-dimensional 
array based on date and time. More specifically, date is grouped into 
categories:

day, week-to-date, month-to-date, and year-to-date

Then, for each of those categories, I need to get a count of records that fall 
into the following categories:

0 minutes late, 1-5 minutes late, and 6-15 minutes late

where minutes late will be calculated based on a known scheduled time and the 
time in the record. To further complicate things, there are actually two times 
in each record, so under the day, week-to-date, month-to-date etc groups, there 
will be two sets of "late" bins, one for each time. In table form it would look 
 something like this:

| day  |  week-to-date | month-to-date |  year-to-date  |

t1 0min| 
t1 1-5 min| ...
t1 6-15 min  | ...
t2 0min| ...
t2 1-5 min| ...
t2 6-15 min  | ...

So in the extreme scenario of a record that is for the current day, it will be 
counted into 8 bins: once each for day, week-to-date, month-to-date and 
year-to-date under the proper "late" bin for the first time in the record, and 
once each into each of the time groups under the proper "late" bin for the 
second time in the record. An older record may only be counted twice, under the 
year-to-date group. A record with no matching schedule is discarded, as is any 
record that is "late" by more than 15 minutes (those are gathered into a 
separate report)

My initial approach was to simply make dictionaries for each "row" in the 
table, like so:

t10 = {'daily': 0, 'WTD': 0, 'MTD': 0, 'YTD': 0,}
t15 = {'daily': 0, 'WTD': 0, 'MTD': 0, 'YTD': 0,}
.
.
t25 = {'daily': 0, 'WTD': 0, 'MTD': 0, 'YTD': 0,}
t215 = {'daily': 0, 'WTD': 0, 'MTD': 0, 'YTD': 0,}

then loop through the records, find the schedule for that record (if any, if 
not move on as mentioned earlier), compare t1 and t2 against the schedule, and 
increment the appropriate bin counts using a bunch of if statements. 
Functional, if ugly. But then I got to thinking: I keep hearing about all these 
fancy numerical analysis tools for python like pandas and numpy - could 
something like that help? Might there be a way to simply set up a table with 
"rules" for the columns and rows, and drop my records into the table, having 
them automatically counted into the proper bins or something? Or am I over 
thinking this, and the "simple", if ugly approach is best?

---
Israel Brewster
Systems Analyst II
Ravn Alaska
5245 Airport Industrial Rd
Fairbanks, AK 99709
(907) 450-7293
---




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


Re: right list for SIGABRT python binary question ?

2017-10-19 Thread Karsten Hilbert
On Thu, Oct 19, 2017 at 07:27:45PM +0200, Karsten Hilbert wrote:

> I am currently running the bootstrapper with mxdatetime as a
> dbg build to see what gives. The only other C extension I am
> aware of that is in use is psycopg2.

So here's the final console output of that:

==> bootstrapping "v20_fixups-pre_v21" ...
==> dropping pre-existing target database [gnumed_v21] ...
==> cloning [gnumed_v20] (72 MB) as target database [gnumed_v21] ...
==> reindexing target database (can take a while) ...
==> transferring users ...
==> bootstrapping "v20-v21-static" ...
==> bootstrapping "v20-v21-dynamic" ...
==> bootstrapping "v21-fixups" ...
==> setting up auditing ...
==> setting up encounter/episode FKs and IDXs ...
==> setting up encounter/episode FK sanity check triggers ...
==> setting up generic notifications ...
==> upgrading reference data sets ...
==> verifying target database schema ...
==> checking migrated data for plausibility ...
Done bootstrapping GNUmed database: We very likely succeeded.
log: 
/home/ncq/Projekte/gm-git/gnumed/gnumed/server/bootstrap/bootstrap-latest.log
Debug memory block at address p=0x717b7c: API ''
0 bytes originally requested
The 3 pad bytes at p-3 are not all FORBIDDENBYTE (0xfb):
at p-3: 0x03 *** OUCH
at p-2: 0x4e *** OUCH
at p-1: 0x00 *** OUCH
Because memory is corrupted at the start, the count of bytes 
requested
   may be bogus, and checking the trailing pad bytes may segfault.
The 4 pad bytes at tail=0x717b7c are not all FORBIDDENBYTE (0xfb):
at tail+0: 0x00 *** OUCH
at tail+1: 0x00 *** OUCH
at tail+2: 0x00 *** OUCH
at tail+3: 0x00 *** OUCH
The block was made by call #0 to debug malloc/realloc.
Fatal Python error: bad ID: Allocated using API '', verified using API 
'o'
./bootstrap-latest.sh: Zeile 80: 28023 Abgebrochen 
./bootstrap_gm_db_system.py --log-file=${LOG} --conf-file=${CONF} --${QUIET}
Bootstrapping "gnumed_v21" did not finish successfully (134). Aborting.

Note there's not faulthandler output here because I don't yet
know how to get a debug build of *that* (Debian does not seem
to have one and neither does pypi to my knowledge).

That'd be needed because of:

root@hermes:~/bin# python2.7-dbg
Python 2.7.14 (default, Sep 17 2017, 18:50:44)
[GCC 7.2.0] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import faulthandler
Traceback (most recent call last):
  File "", line 1, in 
ImportError: 
/usr/lib/python2.7/dist-packages/faulthandler.i386-linux-gnu.so: undefined 
symbol: Py_InitModule4
[45316 refs]
>>>

Can anyone give more guidance on what the above python debug
output might vaguely point to ?

Thanks,
Karsten
-- 
GPG key ID E4071346 @ eu.pool.sks-keyservers.net
E167 67FD A291 2BEA 73BD  4537 78B9 A9F9 E407 1346
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Efficient counting of results

2017-10-19 Thread Israel Brewster

> On Oct 19, 2017, at 9:40 AM, Israel Brewster  wrote:
> 
> I am working on developing a report that groups data into a two-dimensional 
> array based on date and time. More specifically, date is grouped into 
> categories:
> 
> day, week-to-date, month-to-date, and year-to-date
> 
> Then, for each of those categories, I need to get a count of records that 
> fall into the following categories:
> 
> 0 minutes late, 1-5 minutes late, and 6-15 minutes late
> 
> where minutes late will be calculated based on a known scheduled time and the 
> time in the record. To further complicate things, there are actually two 
> times in each record, so under the day, week-to-date, month-to-date etc 
> groups, there will be two sets of "late" bins, one for each time. In table 
> form it would look  something like this:
> 
>| day  |  week-to-date | month-to-date |  year-to-date  |
> 
> t1 0min| 
> t1 1-5 min| ...
> t1 6-15 min  | ...
> t2 0min| ...
> t2 1-5 min| ...
> t2 6-15 min  | ...
> 
> So in the extreme scenario of a record that is for the current day, it will 
> be counted into 8 bins: once each for day, week-to-date, month-to-date and 
> year-to-date under the proper "late" bin for the first time in the record, 
> and once each into each of the time groups under the proper "late" bin for 
> the second time in the record. An older record may only be counted twice, 
> under the year-to-date group. A record with no matching schedule is 
> discarded, as is any record that is "late" by more than 15 minutes (those are 
> gathered into a separate report)
> 
> My initial approach was to simply make dictionaries for each "row" in the 
> table, like so:
> 
> t10 = {'daily': 0, 'WTD': 0, 'MTD': 0, 'YTD': 0,}
> t15 = {'daily': 0, 'WTD': 0, 'MTD': 0, 'YTD': 0,}
> .
> .
> t25 = {'daily': 0, 'WTD': 0, 'MTD': 0, 'YTD': 0,}
> t215 = {'daily': 0, 'WTD': 0, 'MTD': 0, 'YTD': 0,}
> 
> then loop through the records, find the schedule for that record (if any, if 
> not move on as mentioned earlier), compare t1 and t2 against the schedule, 
> and increment the appropriate bin counts using a bunch of if statements. 
> Functional, if ugly. But then I got to thinking: I keep hearing about all 
> these fancy numerical analysis tools for python like pandas and numpy - could 
> something like that help? Might there be a way to simply set up a table with 
> "rules" for the columns and rows, and drop my records into the table, having 
> them automatically counted into the proper bins or something? Or am I over 
> thinking this, and the "simple", if ugly approach is best?

I suppose I should mention: my data source is the results of a psycopg2 query, 
so a "record" is a tuple or dictionary (depending on how I want to set up the 
cursor)

> 
> ---
> Israel Brewster
> Systems Analyst II
> Ravn Alaska
> 5245 Airport Industrial Rd
> Fairbanks, AK 99709
> (907) 450-7293
> ---
> 
> 
> 
> 
> -- 
> https://mail.python.org/mailman/listinfo/python-list

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


Application and package of the same name

2017-10-19 Thread Skip Montanaro
I'm not understanding something fundamental about absolute/relative
imports. Suppose I have an application, fribble.py, and it has a
corresponding package full of goodies it relies on, also named fribble.
>From the fribble package, the application wants to import the sandwich
function from the lunchtime module. At the top level it thus has an import
like this:

from fribble.lunchtime import sandwich

I might have a directory structure like this:

example
example/fribble.py
fribble
__init__.py
lunchtime.py

If I run inside the example directory with PYTHONPATH=.. I can't
find/import the fribble package, because the main application directory is
prepended to sys.path. Consequently, the import machinery never gets any
further down sys.path. It stumbles on the fribble application and tries to
find the bits it's interested in, to no avail.

This is in Python 2.7, FWIW. What am I missing?

Thx,

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


make test on Centos 7.4 fails

2017-10-19 Thread Decker, Ryan C.
Hey Python guys,

test_socket doesn't seem to be passing on a clean CentOS 7.4 install.

strace -s 128 -e trace=%network -o trace ./python -m test -v test_socket -m
test_sha256
== CPython 3.6.3 (default, Oct 19 2017, 14:12:01) [GCC 7.1.1 20170526 (Red
Hat 7.1.1-2)]
== Linux-3.10.0-693.2.2.el7.x86_64-x86_64-with-centos-7.4.1708-Core
little-endian
== cwd: /tmp/Python-3.6.3/build/test_python_20478
== CPU count: 8
== encodings: locale=UTF-8, FS=utf-8
Run tests sequentially
0:00:00 load avg: 1.03 [1/1] test_socket
test_sha256 (test.test_socket.LinuxKernelCryptoAPI) ... ERROR

*==*
*ERROR: test_sha256 (test.test_socket.LinuxKernelCryptoAPI)*
*--*
*Traceback (most recent call last):*
*  File "/tmp/Python-3.6.3/Lib/test/test_socket.py", line 5424, in
test_sha256*
*op.sendall(b"abc")*
*OSError: [Errno 126] Required key not available*

--
Ran 1 test in 0.002s

FAILED (errors=1)
test test_socket failed
test_socket failed

1 test failed:
test_socket

Total duration: 42 ms
Tests result: FAILURE

Here is the strace:

socket(AF_INET6, SOCK_STREAM|SOCK_CLOEXEC, IPPROTO_IP) = 3
bind(3, {sa_family=AF_INET6, sin6_port=htons(0), inet_pton(AF_INET6, "::1",
&sin6_addr), sin6_flowinfo=htonl(0), sin6_scope_id=0}, 28) = 0
--- SIGCHLD {si_signo=SIGCHLD, si_code=CLD_EXITED, si_pid=20479,
si_uid=1000, si_status=0, si_utime=0, si_stime=0} ---
socket(AF_CAN, SOCK_RAW|SOCK_CLOEXEC, 1) = 3
socket(AF_RDS, SOCK_SEQPACKET|SOCK_CLOEXEC, 0) = -1 EAFNOSUPPORT (Address
family not supported by protocol)
socket(AF_ALG, SOCK_SEQPACKET|SOCK_CLOEXEC, 0) = 3
socket(AF_INET6, SOCK_DGRAM|SOCK_CLOEXEC, IPPROTO_IP) = 3
socket(AF_INET6, SOCK_DGRAM|SOCK_CLOEXEC, IPPROTO_IP) = 3
socket(AF_INET6, SOCK_DGRAM|SOCK_CLOEXEC, IPPROTO_IP) = 3
socket(AF_INET6, SOCK_DGRAM|SOCK_CLOEXEC, IPPROTO_IP) = 3
socket(AF_INET6, SOCK_DGRAM|SOCK_CLOEXEC, IPPROTO_IP) = 3
socket(AF_INET, SOCK_STREAM|SOCK_CLOEXEC, IPPROTO_SCTP) = 3
socket(AF_INET, SOCK_STREAM|SOCK_CLOEXEC, IPPROTO_SCTP) = 3
socket(AF_INET, SOCK_STREAM|SOCK_CLOEXEC, IPPROTO_SCTP) = 3
socket(AF_ALG, SOCK_SEQPACKET|SOCK_CLOEXEC, 0) = 3
bind(3, {sa_family=AF_ALG,
sa_data="hash\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0sha256\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0"},
88) = 0
accept4(3, NULL, NULL, SOCK_CLOEXEC)= 4
sendto(4, "abc", 3, 0, NULL, 0) = -1 ENOKEY (Required key not
available)
+++ exited with 2 +++


Any ideas?


-- 
Ryan C. Decker '08
Principle Network & Systems Eng.
Siena College ITS
515 Loudon Road
Loudonville, NY 12211
[email protected]

CONFIDENTIALITY NOTICE: This e-mail, including any attachments, is for the
sole use of the intended recipient(s) and may contain confidential and
privileged information. Any unauthorized review, use, disclosure, or
distribution is prohibited. If you received this e-mail and are not the
intended recipient, please inform the sender by e-mail reply and destroy
all copies of the original message.
-- 
https://mail.python.org/mailman/listinfo/python-list


Vim, ctags jump to python standard library source

2017-10-19 Thread Matt Schepers
I prefer to use vim and ctags when developing python, but I'm having
trouble getting ctags to index the standard library. Sometimes I would like
to see an object's constructor etc...

Does anyone know how to do this?

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


Re: Efficient counting of results

2017-10-19 Thread Israel Brewster

> On Oct 19, 2017, at 10:02 AM, Stefan Ram  wrote:
> 
> Israel Brewster  writes:
>> t10 = {'daily': 0, 'WTD': 0, 'MTD': 0, 'YTD': 0,}
>> increment the appropriate bin counts using a bunch of if statements.
> 
>  I can't really completely comprehend your requirements
>  specification, you might have perfectly described it all and
>  it's just too complicated for me to comprehend, but I just
>  would like to add that there are several ways to implement a
>  "two-dimensional" matrix. You can also imagine your
>  dictionary like this:
> 
> example =
> { 'd10': 0, 'd15': 0, 'd20': 0, 'd215': 0,
>  'w10': 0, 'w15': 0, 'w20': 0, 'w215': 0,
>  'm10': 0, 'm15': 0, 'm20': 0, 'm215': 0,
>  'y10': 0, 'y15': 0, 'y20': 0, 'y215': 0 }
> 
>  Then, when the categories are already in two variables, say,
>  »a« (»d«, »w«, »m«, or »y«) and »b« (»10«, »15«, »20«, or
>  »215«), you can address the appropriate bin as

Oh, I probably was a bit weak on the explanation somewhere. I'm still wrapping 
*my* head around some of the details. That's what makes it fun :-) If it helps, 
my data would look something like this:

[ (date, key, t1, t2), 
 (date, key, t1, t2)
.
.
]

Where the date and the key are what is used to determine what "on-time" is for 
the record, and thus which "late" bin to put it in. So if the date of the first 
record was today, t1 was on-time, and t2 was 5 minutes late, then I would need 
to increment ALL of the following (using your data structure from above):

d10, w10, m10, y10, d25, w25, m25 AND y25

Since this record counts not just for the current day, but also for 
week-to-date, month-to-date and year-to-date. Basically, as the time categories 
get larger, the percentage of the total records included in that date group 
also gets larger. The year-to-date group will include all records, grouped by 
lateness, the daily group will only include todays records.

Maybe that will help clear things up. Or not. :-)
> 
> example[ a + b ]+= 1

Not quite following the logic here. Sorry.

> 
>  . (And to not have to initialized the entries to zero,
>  class collections.defaultdict might come in handy.)

Yep, those are handy in many places. Thanks for the suggestion.

---
Israel Brewster
Systems Analyst II
Ravn Alaska
5245 Airport Industrial Rd
Fairbanks, AK 99709
(907) 450-7293
---

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

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


Re: Application and package of the same name

2017-10-19 Thread Paul Moore
On 19 October 2017 at 19:18, Skip Montanaro  wrote:
> I'm not understanding something fundamental about absolute/relative
> imports. Suppose I have an application, fribble.py, and it has a
> corresponding package full of goodies it relies on, also named fribble.
> From the fribble package, the application wants to import the sandwich
> function from the lunchtime module. At the top level it thus has an import
> like this:
>
> from fribble.lunchtime import sandwich
>
> I might have a directory structure like this:
>
> example
> example/fribble.py
> fribble
> __init__.py
> lunchtime.py
>
> If I run inside the example directory with PYTHONPATH=.. I can't
> find/import the fribble package, because the main application directory is
> prepended to sys.path. Consequently, the import machinery never gets any
> further down sys.path. It stumbles on the fribble application and tries to
> find the bits it's interested in, to no avail.
>
> This is in Python 2.7, FWIW. What am I missing?

My immediate reaction is "you shouldn't name your main program and
your package the same". It's not a pattern I've seen commonly used.

However, the approaches I've seen used (a __main__.py inside the
package, so you can execute it via `python -m fribble`, or a setup.py
entry point to generate a script wrapper for the application) may be
more common among people focused more on library development than on
application development.

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


Re: Efficient counting of results

2017-10-19 Thread Peter Otten
Israel Brewster wrote:

> 
>> On Oct 19, 2017, at 10:02 AM, Stefan Ram  wrote:
>> 
>> Israel Brewster  writes:
>>> t10 = {'daily': 0, 'WTD': 0, 'MTD': 0, 'YTD': 0,}
>>> increment the appropriate bin counts using a bunch of if statements.
>> 
>>  I can't really completely comprehend your requirements
>>  specification, you might have perfectly described it all and
>>  it's just too complicated for me to comprehend, but I just
>>  would like to add that there are several ways to implement a
>>  "two-dimensional" matrix. You can also imagine your
>>  dictionary like this:
>> 
>> example =
>> { 'd10': 0, 'd15': 0, 'd20': 0, 'd215': 0,
>>  'w10': 0, 'w15': 0, 'w20': 0, 'w215': 0,
>>  'm10': 0, 'm15': 0, 'm20': 0, 'm215': 0,
>>  'y10': 0, 'y15': 0, 'y20': 0, 'y215': 0 }
>> 
>>  Then, when the categories are already in two variables, say,
>>  »a« (»d«, »w«, »m«, or »y«) and »b« (»10«, »15«, »20«, or
>>  »215«), you can address the appropriate bin as
> 
> Oh, I probably was a bit weak on the explanation somewhere. I'm still
> wrapping *my* head around some of the details. That's what makes it fun
> :-) If it helps, my data would look something like this:
> 
> [ (date, key, t1, t2),
>  (date, key, t1, t2)
> .
> .
> ]
> 
> Where the date and the key are what is used to determine what "on-time" is
> for the record, and thus which "late" bin to put it in. So if the date of
> the first record was today, t1 was on-time, and t2 was 5 minutes late,
> then I would need to increment ALL of the following (using your data
> structure from above):
> 
> d10, w10, m10, y10, d25, w25, m25 AND y25

Start with simpler more generic operations. A

def group(rows):
   ...

function that expects rows of the form

(date, key, t)

can be run twice, once with 

summary1 = group((date, key, t1) for date, key, t1, t2 in rows)

and then with t2.

Then only calculate the daily sums as you can derive the weekly, monthly and 
yearly totals by summing over the respective days (you may also get the 
yearly totals by summing over the respective months, but I expect  this to 
be an optimisation with negligable effect).

> Since this record counts not just for the current day, but also for
> week-to-date, month-to-date and year-to-date. Basically, as the time
> categories get larger, the percentage of the total records included in
> that date group also gets larger. The year-to-date group will include all
> records, grouped by lateness, the daily group will only include todays
> records.
> 
> Maybe that will help clear things up. Or not. :-)
>> 
>> example[ a + b ]+= 1
> 
> Not quite following the logic here. Sorry.
> 
>> 
>>  . (And to not have to initialized the entries to zero,
>>  class collections.defaultdict might come in handy.)
> 
> Yep, those are handy in many places. Thanks for the suggestion.
> 
> ---
> Israel Brewster
> Systems Analyst II
> Ravn Alaska
> 5245 Airport Industrial Rd
> Fairbanks, AK 99709
> (907) 450-7293
> ---
> 
>> 
>> --
>> https://mail.python.org/mailman/listinfo/python-list
> 


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


Re: Efficient counting of results

2017-10-19 Thread Thomas Jollans
On 19/10/17 20:04, Israel Brewster wrote:
>> then loop through the records, find the schedule for that record (if any, if 
>> not move on as mentioned earlier), compare t1 and t2 against the schedule, 
>> and increment the appropriate bin counts using a bunch of if statements. 
>> Functional, if ugly. But then I got to thinking: I keep hearing about all 
>> these fancy numerical analysis tools for python like pandas and numpy - 
>> could something like that help? Might there be a way to simply set up a 
>> table with "rules" for the columns and rows, and drop my records into the 
>> table, having them automatically counted into the proper bins or something? 
>> Or am I over thinking this, and the "simple", if ugly approach is best?

The numerical packages can do a lot of things; in this kind of case,
mostly hiding loops in C code. You'll have to be the judge of whether
it's helpful. I'll just try to give you an idea of what you might do.

If you had two pandas/numpy series/arrays of numpy datetimes called
"scheduled" and "actual", you could get all the delays

import pandas as pd
delays = pd.Series(actual - scheduled)

then index those by the actual time

delays.index = actual

and select bits of it as you please, e.g.

from pandas.tseries.offsets import Week
today = pd.to_datetime('today')
wtd_delays = delays[today-week:]

You can construct boolean mask arrays for certain conditions

minute_delta = pd.DateOffset(minutes=1).delta
wtd_is_between_1_and_5_min = ((wtd_delays >= 1*minute_delta) &
  (wtd_delays < 5*minute_delta))

and either get all the affected datapoints

wtd_delays[wtd_is_between_1_and_5_min]

or count them

np.count_nonzero(wtd_is_between_1_and_5_min)

If you have a larger table in a pandas DataFrame with more information,
and you want to get those rows which fit a particular requirement, you
can do that too. Something like

some_table = dataframe_conjured_out_of_thin_air()
delay = some_table['actual_time'] - some_table['proper_civilised_time']
mtd = today - pd.DateOffset(months=1)

data_mtd_5min_or_later = some_table[mtd:][delay[mtd:]
  >= 5*minute_delta]

Or something like that.

If you do a lot of this kind of stuff (sifting through largish
datasets), learning how to use pandas might be an excellent idea, but it
will of course involve a fair amount of scrolling through documentation,
googling, and reading stack overflow posts.

Some pointers:
http://pandas.pydata.org/pandas-docs/stable/
http://pandas.pydata.org/pandas-docs/stable/timeseries.html
https://docs.scipy.org/doc/numpy-1.13.0/user/basics.indexing.html#boolean-or-mask-index-arrays


> 
> I suppose I should mention: my data source is the results of a psycopg2 
> query, so a "record" is a tuple or dictionary (depending on how I want to set 
> up the cursor)

In this case, if you want speed, you're far better off doing most of the
work in SQL rather than Python! If you want clarity, maybe what you're
doing now is already good enough. Or maybe using more complex SQL would
actually be clearer.

Pandas sits somewhere in between, and IMHO only gives you significant
added value (if your data is already in a database) if you want to do
some form of statistical analysis, some other kind of more complex
computation, or if you want to create plots.


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


Re: Application and package of the same name

2017-10-19 Thread Skip Montanaro
> My immediate reaction is "you shouldn't name your main program and
> your package the same". It's not a pattern I've seen commonly used.
>
> However, the approaches I've seen used (a __main__.py inside the
> package, so you can execute it via `python -m fribble`, or a setup.py
> entry point to generate a script wrapper for the application) may be
> more common among people focused more on library development than on
> application development.

Thanks Paul. The simplest course for me is to change the name of the
application,
as the package is already in use by other people, and I don't really
want to embed the
application in the package.

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


Re: How to debug an unfired tkinter event?

2017-10-19 Thread Peter Otten
[email protected] wrote:

> Peter Otten於 2017年10月19日星期四 UTC+8下午6時04分39秒寫道:
>> [email protected] wrote:
>> 
>> > Peter Otten at 2017-10-19 UTC+8 PM 3:24:30 wrote:
>> >> It's not clear to me what you mean with this. Did you place the table
>> >> from the recipe elsewhere inside a window that you created or did you
>> >> make changes in the recipe's code?
>> > 
>> > Thank you, Peter. I am using Python 3.4.4 under WinXP.
>> > 
>> > When running the file directly from download, I get a table scrolling
>> > vertically only. If its Table class's __init__ parameter
>> > "scroll_horizontally" changed to True, it can be scrolled horizontally
>> > also. But there is a problem when scrolled horizontally, the header
>> > field will not align with data field anymore. I make some modifications
>> > to make it does. So far so good.
>> > 
>> > Later, I want to make the table also has a vertical header. The first
>> > step I had taken was to move all 2nd top-level widgets(not much, just
>> > four) to right one column further to make room for this new widget. I
>> > though it's a simple work, just increase their grid column value by 1.
>> > But Murphy's Law comes, the xscrollbar even don't show up when the
>> > table was squeezed horizontally.
>> 
>> Thank you for the clarification; I understand your problem much better
>> now.
>> 
>> > 
>> >> > The canvas has a binding:
>> >> > self.canvas.bind('', self._on_canvas_configure)
>> >> > 
>> >> > After this movement, the callback was only triggered when dragging
>> >> > the root widget to resize canvas vertically, but not horizontally.
>> >> > 
>> >> > Event seems has OS involved(it's MS Windows XP in my case). How to
>> >> > debug this kind of problem, under pdb?
>> >> 
>> >> I don't know, but if you can post a small example script that
>> >> demonstrates the problem, and you are lucky, someone will see the
>> >> problem.
>> > 
>> > This is a ~5xx lines file and I think it's not fare to ask forum
>> > members to look through it. So I decide to ask for help on how to debug
>> > it, instead of the solution.
>> > 
>> > I try to change the binding to
>> >  self.bind_all('', self._on_canvas_configure)
>> > and add a line into the callback
>> >  print(event.widget)
>> > 
>> > I got some info below each time when I squeeze the table:
>> > .
>> > .5006
>> > .5006.50712528
>> > .5006.50712496
>> > .5006.50712464
>> > .5006.50712144
>> > .5006.50712528.50712560.50782256
>> > .5006.50712528.50712560.50782256.50783024
>> > .5006.50712528.50712560.50782256
>> > .5006.50712528.50712560.50782256.50783024
>> > 
>> > How to change these number(is it a widget ID?) to a meaning name?
>> 
>> That's the name of the widget in the underlying tcl/tk. You can specify
>> it in Python:
>> 
>> >>> import tkinter as tk
>> >>> print(tk.Button())
>> .139817813335904
>> >>> print(tk.Button(name="mybutton"))
>> .mybutton
>> 
>> You have to ensure that names are unique.
> 
> Thank you. It's very helpful information.
> 
> By the way, I just found this problem can be easily seen by not modifing
> the original file too much with the following steps:
> 
> 1. copy and get the file from the link.
> 2. change the "scroll_horizontally" parameter in the "Table" class's
> __init__ method to "True"
> 
> Running it you will see it perform correctly.
> 
> 3. Change the "column=x" at line 268, 282, 288, 292, 303 to "column=x+1"
> 
> Now it has the problem.

There may be other less obvious places that are affected by your 
modifications. Does changing

self.grid_columnconfigure(0, weight=1)

to

self.grid_columnconfigure(1, weight=1)

in Table.__init__() help?


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


Problem with StreamReaderWriter on 3.6.3?

2017-10-19 Thread Peter via Python-list
I came across this code in Google cpplint.py, a Python script for 
linting C++ code. I was getting funny results with Python 3.6.3, but it 
worked fine under 2.7.13


I've tracked the problem to an issue with StreamReaderWriter; the 
traceback and error never shows under 3. The _cause_ of the error is 
clear (xrange not in Py3), but I need the raised exception to show.


I'm running Python 3.6.3 32bit on Windows 10. I also get the same 
results on Python 3.5.2 on Ubuntu (under WSL)


I'm not super familiar with rebinding stderr with codecs, but I'm 
guessing they are doing it because of some Unicode issue they may have 
been having.


I have a workaround - drop the rebinding - but it seems like there might 
be an error in StreamReaderWriter.

Do other people see the same behaviour?
Is there something I'm not seeing or understanding?
Would I raise it on issue tracker?

Peter

--

import sys
import codecs

sys.stderr = codecs.StreamReaderWriter(
    sys.stderr, codecs.getreader('utf8'), codecs.getwriter('utf8'), 
'replace')


# This should work fine in Py 2, but raise an exception in Py3
# But instead Py3 "swallows" the exception and it is never seen
print(xrange(1, 10))

# Although this line doesn't show in Py 3 (as the script has silently 
crashed)

print("This line doesn't show in Py 3")

--





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


Re: Problem with StreamReaderWriter on 3.6.3?

2017-10-19 Thread MRAB

On 2017-10-19 22:46, Peter via Python-list wrote:

I came across this code in Google cpplint.py, a Python script for
linting C++ code. I was getting funny results with Python 3.6.3, but it
worked fine under 2.7.13

I've tracked the problem to an issue with StreamReaderWriter; the
traceback and error never shows under 3. The _cause_ of the error is
clear (xrange not in Py3), but I need the raised exception to show.

I'm running Python 3.6.3 32bit on Windows 10. I also get the same
results on Python 3.5.2 on Ubuntu (under WSL)

I'm not super familiar with rebinding stderr with codecs, but I'm
guessing they are doing it because of some Unicode issue they may have
been having.

I have a workaround - drop the rebinding - but it seems like there might
be an error in StreamReaderWriter.
Do other people see the same behaviour?
Is there something I'm not seeing or understanding?
Would I raise it on issue tracker?

Peter

--

import sys
import codecs

sys.stderr = codecs.StreamReaderWriter(
      sys.stderr, codecs.getreader('utf8'), codecs.getwriter('utf8'),
'replace')

# This should work fine in Py 2, but raise an exception in Py3
# But instead Py3 "swallows" the exception and it is never seen
print(xrange(1, 10))

# Although this line doesn't show in Py 3 (as the script has silently
crashed)
print("This line doesn't show in Py 3")

--

StreamReaderWriter is being passed an encoder which returns bytes 
(UTF-8), but the output stream that is being passed, to which it will be 
writing those butes, i.e. the original sys.stderr, expects str.


I'd get the underlying byte stream of stderr using .detach():

sys.stderr = codecs.StreamReaderWriter(sys.stderr.detach(), 
codecs.getreader('utf8'), codecs.getwriter('utf8'), 'replace')

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


Re: How to debug an unfired tkinter event?

2017-10-19 Thread Terry Reedy

On 10/19/2017 5:07 AM, [email protected] wrote:


I got some info below each time when I squeeze the table:
.
.5006
.5006.50712528
.5006.50712496
.5006.50712464
.5006.50712144
.5006.50712528.50712560.50782256
.5006.50712528.50712560.50782256.50783024
.5006.50712528.50712560.50782256
.5006.50712528.50712560.50782256.50783024

How to change these number(is it a widget ID?) to a meaning name?


The number strings as names are the defaults that came from tkinter, not 
tcl/tk.  In 3.6, the default names were changed to be versions of the 
class name.


>>> import tkinter as tk
>>> r = tk.Tk()
>>> b = tk.Button(r)
>>> b

>>> b2 = tk.Button(r)
>>> b2


--
Terry Jan Reedy

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


Re: Application and package of the same name

2017-10-19 Thread Chris Angelico
On Fri, Oct 20, 2017 at 7:09 AM, Skip Montanaro
 wrote:
>> My immediate reaction is "you shouldn't name your main program and
>> your package the same". It's not a pattern I've seen commonly used.
>>
>> However, the approaches I've seen used (a __main__.py inside the
>> package, so you can execute it via `python -m fribble`, or a setup.py
>> entry point to generate a script wrapper for the application) may be
>> more common among people focused more on library development than on
>> application development.
>
> Thanks Paul. The simplest course for me is to change the name of the
> application,
> as the package is already in use by other people, and I don't really
> want to embed the
> application in the package.
>

Have you toyed with "from __future__ import absolute_import" ? Not
sure if it'd help or not, but worth a try.

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


Re: Application and package of the same name

2017-10-19 Thread Skip Montanaro
Have you toyed with "from __future__ import absolute_import" ? Not
sure if it'd help or not, but worth a try.


Yeah, I did, but it didn't change anything as far as I could tell.

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


Re: Efficient counting of results

2017-10-19 Thread Steve D'Aprano
On Fri, 20 Oct 2017 05:28 am, Israel Brewster wrote:

> If it helps, my data would look something like this:
> 
> [ (date, key, t1, t2),
>  (date, key, t1, t2)
> .
> .
> ]
> 
> Where the date and the key are what is used to determine what "on-time" is
> for the record, and thus which "late" bin to put it in.

It might help if you discuss it in less generic and more concrete terms. E.g.
are these invoices? The date field could be the invoice date, the "key" field
(aren't they all keys?) might be the due date.

What t1 and t2 are, I have no idea. Your code there suggests that they are
fields in your data records, but the contents of the fields, who knows?


> So if the date of 
> the first record was today, t1 was on-time, and t2 was 5 minutes late, then
> I would need to increment ALL of the following (using your data structure
> from above):
> 
> d10, w10, m10, y10, d25, w25, m25 AND y25

Try using descriptive variable names rather than these cryptic codes.

I don't understand what is *actually* being computed here -- you say that t1
is "on time" and t2 is "5 minutes late", but that's a contradiction: how can
a single record be both on time and 5 minutes late?

It also contradicts your statement that it is *date* and *key* that determines
which late bin to use. Rather, it seems that date and key are irrelevant and
can be ignored, it is only t1 and t2 which determine which late bins to
update.

Another question: you're talking about *dates*, which implies a resolution of
1 day, but then you talk about records being "five minutes late" which
implies a resolution of at least five minutes and probably one minute, if not
seconds or milliseconds. Which is it? My guess is that you're probably
talking about *timestamps* (datetimes) rather than *dates*.


> Since this record counts not just for the current day, but also for
> week-to-date, month-to-date and year-to-date. Basically, as the time
> categories get larger, the percentage of the total records included in that
> date group also gets larger. The year-to-date group will include all
> records, grouped by lateness, the daily group will only include todays
> records.

With the proviso that I have no confidence at all that I understand your
specification or requirements, and that I'm *guessing* a spec that makes
sense to me, I'll suggest a couple of approaches.

Hypotheses: you have data with a timestamp recording when each record
becomes "active" in some sense (e.g. when an invoice becomes due for
payment), and second timestamp representing the date/time "now" (at the start
of the computation?). You want to process all your records, and decide "as of
now, how late is each record", and then report *cumulative* subtotals for a
number of arbitrary groups: not late yet, five minutes late, one day late,
one year late, etc.

Suggestion:

Start with just the "activation time" and "now", and calculate the difference.
If they are both given in seconds, you can just subtract:

lateness = now - activation_time

to determine how late that record is. If they are Datetime objects, use a
Timedelta object.

That *single* computed field, the lateness, is enough to determine which
subtotals need to be incremented. Start by dividing all of time into named
buckets, in numeric order:

lateness <= 0: on_time
0 < lateness <= five minutes: five_minutes_late
five minutes < lateness <= sixty minutes: one_hour_late
sixty minutes < lateness <= 24 hours: one_day_late
24 hours < lateness <= seven days: one_week_late

etc. Notice that the buckets don't overlap.

(The buckets refer to the *maximum* lateness, which is opposite of standard
book-keeping practice which uses the minimum. In accounting and book-keeping,
if an invoice is 45 days past the due date, it would fall into the single
bucket "30 days past due". When it reaches 60 days past the due date, it
moves into the "60 days past due" bucket, where it will stay until it gets to
90 days overdue.)

Convert the times to a common unit (seconds), and build a list for each
bucket, using the *upper bound* as key:


buckets = [(0, on_time),
   (5*60, five_minutes_late),  # i.e. *up to* five minutes late
   (60*60, one_hour_late),  # i.e. *up to* one hour late
   (24*60*60, one_day_late), 
   ...
   (float('inf'), unbelievably_late)]

The buckets might be lists, to append the record; or integer counts, which you
add 1 to, or subtotals. I'll assume that they are lists, as that is the most
flexible.

Now process your records:

for record in records:
lateness = now - record.activation_date
for end, bucket in buckets:
if lateness <= end:
bucket.append(record)
else:
break

And you're done!

If you want the *number of records* in a particular bucket, you say:

len(bucket)

If you want the total record amount, you say:

sum(record.total for record in bucket)


(assuming your records also have a "total" field, if they're invoices say).


I hope that's even vagu

Re: Efficient counting of results

2017-10-19 Thread Chris Angelico
On Fri, Oct 20, 2017 at 12:18 PM, Steve D'Aprano
 wrote:
> On Fri, 20 Oct 2017 05:28 am, Israel Brewster wrote:
>> So if the date of
>> the first record was today, t1 was on-time, and t2 was 5 minutes late, then
>> I would need to increment ALL of the following (using your data structure
>> from above):
>>
>> d10, w10, m10, y10, d25, w25, m25 AND y25
>
> Try using descriptive variable names rather than these cryptic codes.
>
> I don't understand what is *actually* being computed here -- you say that t1
> is "on time" and t2 is "5 minutes late", but that's a contradiction: how can
> a single record be both on time and 5 minutes late?

t1 and t2 are independent actions/timepoints, AIUI. So t1 could be the
time the order was put into the oven, and t2 is the time it got
delivered to the person's door. I'm assuming, here, that this database
records pizzas, because why not. An order could have been compiled and
put into the oven on time, but still delivered late; or it could be
ovened slightly late, but thanks to the new high-speed delivery
drones, it was actually at the customer's doorstep on time.

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


Re: Efficient counting of results

2017-10-19 Thread MRAB

On 2017-10-20 03:32, Chris Angelico wrote:

On Fri, Oct 20, 2017 at 12:18 PM, Steve D'Aprano
 wrote:

On Fri, 20 Oct 2017 05:28 am, Israel Brewster wrote:

So if the date of
the first record was today, t1 was on-time, and t2 was 5 minutes late, then
I would need to increment ALL of the following (using your data structure
from above):

d10, w10, m10, y10, d25, w25, m25 AND y25


Try using descriptive variable names rather than these cryptic codes.

I don't understand what is *actually* being computed here -- you say that t1
is "on time" and t2 is "5 minutes late", but that's a contradiction: how can
a single record be both on time and 5 minutes late?


t1 and t2 are independent actions/timepoints, AIUI. So t1 could be the
time the order was put into the oven, and t2 is the time it got
delivered to the person's door. I'm assuming, here, that this database
records pizzas, because why not. An order could have been compiled and
put into the oven on time, but still delivered late; or it could be
ovened slightly late, but thanks to the new high-speed delivery
drones, it was actually at the customer's doorstep on time.


Also, AIUI:

  If it was on-time today, then it was also on-time this week, this 
month, and this year.


  If it was on-time yesterday, then it was also on-time this week, this 
month, and this year, but not today.


  If it was on-time eight days ago, then it was also on-time this 
month, and this year, but not this week.


Excepting that he's dealing with calendar weeks, calendar months, etc, 
so yesterday might not be counted as being in this week (or month, or 
even year!).


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


Re: How to debug an unfired tkinter event?

2017-10-19 Thread jfong
Peter Otten於 2017年10月20日星期五 UTC+8上午4時37分10秒寫道:
> [email protected] wrote:
> 
> > Peter Otten於 2017年10月19日星期四 UTC+8下午6時04分39秒寫道:
> >> [email protected] wrote:
> >> 
> >> > Peter Otten at 2017-10-19 UTC+8 PM 3:24:30 wrote:
> >> >> It's not clear to me what you mean with this. Did you place the table
> >> >> from the recipe elsewhere inside a window that you created or did you
> >> >> make changes in the recipe's code?
> >> > 
> >> > Thank you, Peter. I am using Python 3.4.4 under WinXP.
> >> > 
> >> > When running the file directly from download, I get a table scrolling
> >> > vertically only. If its Table class's __init__ parameter
> >> > "scroll_horizontally" changed to True, it can be scrolled horizontally
> >> > also. But there is a problem when scrolled horizontally, the header
> >> > field will not align with data field anymore. I make some modifications
> >> > to make it does. So far so good.
> >> > 
> >> > Later, I want to make the table also has a vertical header. The first
> >> > step I had taken was to move all 2nd top-level widgets(not much, just
> >> > four) to right one column further to make room for this new widget. I
> >> > though it's a simple work, just increase their grid column value by 1.
> >> > But Murphy's Law comes, the xscrollbar even don't show up when the
> >> > table was squeezed horizontally.
> >> 
> >> Thank you for the clarification; I understand your problem much better
> >> now.
> >> 
> >> > 
> >> >> > The canvas has a binding:
> >> >> > self.canvas.bind('', self._on_canvas_configure)
> >> >> > 
> >> >> > After this movement, the callback was only triggered when dragging
> >> >> > the root widget to resize canvas vertically, but not horizontally.
> >> >> > 
> >> >> > Event seems has OS involved(it's MS Windows XP in my case). How to
> >> >> > debug this kind of problem, under pdb?
> >> >> 
> >> >> I don't know, but if you can post a small example script that
> >> >> demonstrates the problem, and you are lucky, someone will see the
> >> >> problem.
> >> > 
> >> > This is a ~5xx lines file and I think it's not fare to ask forum
> >> > members to look through it. So I decide to ask for help on how to debug
> >> > it, instead of the solution.
> >> > 
> >> > I try to change the binding to
> >> >  self.bind_all('', self._on_canvas_configure)
> >> > and add a line into the callback
> >> >  print(event.widget)
> >> > 
> >> > I got some info below each time when I squeeze the table:
> >> > .
> >> > .5006
> >> > .5006.50712528
> >> > .5006.50712496
> >> > .5006.50712464
> >> > .5006.50712144
> >> > .5006.50712528.50712560.50782256
> >> > .5006.50712528.50712560.50782256.50783024
> >> > .5006.50712528.50712560.50782256
> >> > .5006.50712528.50712560.50782256.50783024
> >> > 
> >> > How to change these number(is it a widget ID?) to a meaning name?
> >> 
> >> That's the name of the widget in the underlying tcl/tk. You can specify
> >> it in Python:
> >> 
> >> >>> import tkinter as tk
> >> >>> print(tk.Button())
> >> .139817813335904
> >> >>> print(tk.Button(name="mybutton"))
> >> .mybutton
> >> 
> >> You have to ensure that names are unique.
> > 
> > Thank you. It's very helpful information.
> > 
> > By the way, I just found this problem can be easily seen by not modifing
> > the original file too much with the following steps:
> > 
> > 1. copy and get the file from the link.
> > 2. change the "scroll_horizontally" parameter in the "Table" class's
> > __init__ method to "True"
> > 
> > Running it you will see it perform correctly.
> > 
> > 3. Change the "column=x" at line 268, 282, 288, 292, 303 to "column=x+1"
> > 
> > Now it has the problem.
> 
> There may be other less obvious places that are affected by your 
> modifications. Does changing
> 
> self.grid_columnconfigure(0, weight=1)
> 
> to
> 
> self.grid_columnconfigure(1, weight=1)
> 
> in Table.__init__() help?

There is description about those numbers in the "tkinter 8.5 reference manual" 
section 5.11 Window names. It can be read by w.winfo_name().

After I give every widget a name(using the same Python instance name), the 
utput looks better. Below is the output of the failed one when squeezed 
horizontally:

.table
.table.scrolling_area
.table.yscrollbar
.table.xscrollbar
.table.canvasH
.table.scrolling_area.canvas.innerframe
.table.scrolling_area.canvas.innerframe._body
.table.scrolling_area.canvas.innerframe
.table.scrolling_area.canvas.innerframe._body

Compared to the output of a good one:

.table
.table.scrolling_area
.table.yscrollbar
.table.xscrollbar
.table.canvasH
.table.scrolling_area.canvas

It shows, when the size changed, the failed one fires onto the wrong 
widgets(the innerframe and innerframe._body), not the canvas. The canvas widget 
didn't absorb the space changing!!

That's prove your intuition is correct. The problem is at the column's weight 
setting. After does the changes according to your suggestion, the problem is 
solved. La la!

Thank yo

Re: How to debug an unfired tkinter event?

2017-10-19 Thread jfong
Terry Reedy於 2017年10月20日星期五 UTC+8上午7時37分59秒寫道:
> On 10/19/2017 5:07 AM, [email protected] wrote:
> 
> > I got some info below each time when I squeeze the table:
> > .
> > .5006
> > .5006.50712528
> > .5006.50712496
> > .5006.50712464
> > .5006.50712144
> > .5006.50712528.50712560.50782256
> > .5006.50712528.50712560.50782256.50783024
> > .5006.50712528.50712560.50782256
> > .5006.50712528.50712560.50782256.50783024
> > 
> > How to change these number(is it a widget ID?) to a meaning name?
> 
> The number strings as names are the defaults that came from tkinter, not 
> tcl/tk.  In 3.6, the default names were changed to be versions of the 
> class name.
> 
>  >>> import tkinter as tk
>  >>> r = tk.Tk()
>  >>> b = tk.Button(r)
>  >>> b
> 
>  >>> b2 = tk.Button(r)
>  >>> b2
> 
> 
> -- 
> Terry Jan Reedy

It's very nice to have this improvment.

What it will be to the previous number output format, say, .5006.50712528? 
Will it become something like this:



then, that's really great:-)

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


Re: How to debug an unfired tkinter event?

2017-10-19 Thread Terry Reedy

On 10/19/2017 11:28 PM, [email protected] wrote:

Terry Reedy於 2017年10月20日星期五 UTC+8上午7時37分59秒寫道:

On 10/19/2017 5:07 AM, [email protected] wrote:


I got some info below each time when I squeeze the table:
.
.5006
.5006.50712528
.5006.50712496
.5006.50712464
.5006.50712144
.5006.50712528.50712560.50782256
.5006.50712528.50712560.50782256.50783024
.5006.50712528.50712560.50782256
.5006.50712528.50712560.50782256.50783024

How to change these number(is it a widget ID?) to a meaning name?


The number strings as names are the defaults that came from tkinter, not
tcl/tk.  In 3.6, the default names were changed to be versions of the
class name.

  >>> import tkinter as tk
  >>> r = tk.Tk()
  >>> b = tk.Button(r)
  >>> b

  >>> b2 = tk.Button(r)
  >>> b2


--
Terry Jan Reedy


It's very nice to have this improvment.

What it will be to the previous number output format, say, .5006.50712528? 
Will it become something like this:

 


I anticipate 


then, that's really great:-)



--
Terry Jan Reedy


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