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

2016-08-14 Thread BartC

On 14/08/2016 05:13, Steven D'Aprano wrote:

On Sun, 14 Aug 2016 06:59 am, BartC wrote:



* The fields in my record are known at compile time



There's nothing like that in Python. But the difference between compile-time
and run-time is not that significant in practice (as opposed to theory,
where it is critical).


But this is, sort of, the subject of the thread. I gave record fields as 
one area where they could be made static by pre-declaring, without too 
many objections.





* (My record fields have other features: aliases for any field; the
ability to access by index, doing len() etc; fast conversions to and
from a list; also the ability to create packed records that exactly
match C structs, and so on.)


I'm not sure what you mean by aliases, or how you would use them. Got an
example?


(
record date =
var day, month, year
 var giorno @day
end

So 'giorno' is a synonym for 'day'. Or sometimes a field is used for 
different purposes in different contexts, and a more apt name might be 
useful:


var text
 var window_caption @ text
 var button_text@ text

(This can be done also with the more formal 'union' syntax, but I only 
allow that for packed structs. It might look like this within a record def:


union
var text, window_caption, button_text
end
))


If I use the Python record() to create two records R and S, then
type(R) and type(S) seem to give the same result (both 'type').


Of course: the type of a class is "type":

py> type(str), type(int), type(list), type(type)
(, , , )


This is called the metaclass; the class of a class. And yes, type is its own
type. What you should be doing is comparing the types of record *instances*
instead:



(.Inner'>,
.Inner'>)


> Hmmm. I didn't expect that. I expected to see the names of the
> classes:

Actually I thought I was comparing instances, but I was applying type() 
to the return value of record(). But doing that now, I get the above too.


I get this behaviour with my scheme:

d := new(date)   # d is an instance of a date record

println date #  is displayed
println date.type# 
println d.type   # 
println d.basetype   # 

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


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

2016-08-14 Thread BartC

On 14/08/2016 05:20, Chris Angelico wrote:

On Sun, Aug 14, 2016 at 2:13 PM, Steven D'Aprano
 wrote:

What you should be doing is comparing the types of record *instances*
instead:

py> x = record('Spam', 'breakfast lunch dinner')('spam',
... 'spam and eggs', 'spam and eggs and spam with a side-dish of spam')
py> y = record('Date', 'year month day')(1999, 'August', 14)
py> type(x), type(y)
(.Inner'>,
.Inner'>)


Hmmm. I didn't expect that. I expected to see the names of the classes:

py> type(x).__name__, type(y).__name__
('Spam', 'Date')


If I was publishing this as a polished product, I'd want to fix that.


But at this point, you're at the level of minor tweaks, not core
functionality - and more importantly, proof of concept is successful.
You have a working class factory, which is downright *impossible* in
many languages.


Well, it's using exec(). So it is generating new program code at 
runtime. That is possible in quite a few languages, even C.


Python is much better however in being able to integrate the results 
into the currently running program that invoked exec().


(Myself, I generally stay away from language building and extension 
features. I believe it leads to more complex languages - look at C++ - 
that can be difficult to follow.)


--
Bartc

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


I'm looking for a part-time job in Python / Django

2016-08-14 Thread Uri Even-Chen
To [email protected],

I'm looking for a part-time job in Python / Django, do you know anything? I
live in Herzliya, Israel. I can't relocate but I can work from home. You
can see my CV on LinkedIn. Please let me know if you have any job for me.

Thanks,
Uri.

*Uri Even-Chen*
[image: photo] Phone: +972-54-3995700
Email: [email protected]
Website: http://www.speedysoftware.com/uri/en/
  
    
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Creating dictionary of items from Excel with mutliple keys

2016-08-14 Thread Peter Otten
Atri Mahapatra wrote:

> I am trying to create a following dictionary. I am reading data from excel
> which has data in the following format:
> 
> Sl no:  Name Thickness Length Material Width Quantity Side
> 
> It has 20 rows of data.
> 
> 
> The dictionary for the 20 rows, I would like to make is
> 
> Data_Dict = [
> { 'Name': 'X', 'Length': '10' , 'Width': '5', 'Quantity': 2 'Area': 50};
> { 'Name': 'Y', 'Length': '20' , 'Width': 10', 'Quantity': 1 'Area': 200};
> .
> .
> .
> .
> till 20 rows
> ];
> 
> I would like to add another key 'Area' as shown above. I used the
> following code(s):
> 
> using openpyxl:
> d={}
> for i  in range(3,sheet.max_row+1):
> #for j in range(3,9):
> #for k in range(0,5):
>   Name = sheet.cell(row= i,column=3).value
>   Length =sheet.cell(row =i,column=6).value
>   Breadth= sheet.cell(row=i,column=7).value
>   Quantity = sheet.cell (row=i,column=8).value
>   Area = sheet.cell(row
>   =i,column=6).value*sheet.cell(row=i,column=7).value d[Name]=
>   Length,Breadth,Quantity,Area
> 
> which gave an output like:
> ['X': (10, 5, 2, 50), 'Y': (20, 10, 1, 2232600), 'Z': (5, 2, 1, 10),
> [.]
> 
> Another code using xlrd:
> 
> keys = [sheet.cell(2, col_index).value for col_index in range(0,8)]
> print (keys)
> dict_list = []
> d = {}
> for row_index in range(1, xl_sheet.nrows):
>for col_index in range(0,8):
>d = {keys[col_index]: xl_sheet.cell(row_index, col_index).value
>for col_index in range(0,8)}
>dict_list.append(d)
> 
> print (dict_list)
> which did not have the area and neither the output was little messy.
> 
> 
> 
> The main purpose is to sort the dictionary based on different criteria
> like Length or Area. I think the first one may be easier to sort. However
> if there are any better way to represent  the dictionary  and the code so
> that it can be sorted later based on different attributes please feel free
> to suggest.

Things become clearer when you abstract out reading the data. For example:

import openpyxl


def open_sheet(filename, sheetname):
wb = openpyxl.load_workbook(filename=filename)
return wb.get_sheet_by_name(sheetname)


def read_table(sheet, columnnames, header_row=0):
name_to_index = {
n: i for i, n
in enumerate(c.value for c in sheet.rows[header_row])
if n is not None}
column_indices = [name_to_index[n] for n in columnnames]

for row in sheet.rows[header_row + 1:]:
yield dict(zip(columnnames, (row[x].value for x in column_indices)))


if __name__ == "__main__":
from operator import itemgetter
import pprint

# read data from Excel
rows = read_table(
open_sheet("sample.xlsx", "SampleSheet"),
"Name Length Width Quantity".split(),
2  # replace with actual header row
)

# add Area
dict_list = []
for row_dict in rows:
row_dict["Area"] = row_dict["Width"] * row_dict["Length"]
dict_list.append(row_dict)

# sort and print data
print("Unsorted:")
pprint.pprint(dict_list)
for sort_column in "Width", "Quantity":
print("\nby {}:".format(sort_column))
dict_list.sort(key=itemgetter(sort_column))
pprint.pprint(dict_list)


To use xlrd instead of openpyxl you have to replace the open_sheet() and 
read_table() functions with

def open_sheet(filename, sheetname):
wb = xlrd.open_workbook(filename)
return wb.sheet_by_name(sheetname)


def read_table(sheet, columnnames, header_row=0):
name_to_index = {
c.value: i for i, c
in enumerate(sheet.row(header_row))
if c.ctype == xlrd.XL_CELL_TEXT}
column_indices = [name_to_index[n] for n in columnnames]
for rowindex in range(header_row + 1, sheet.nrows):
row = sheet.row(rowindex)
yield dict(zip(columnnames, (row[x].value for x in column_indices)))


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


Re: A strange list concatenation result

2016-08-14 Thread ast


"Mok-Kong Shen"  a écrit dans le message de 
news:[email protected]...

Am 13.08.2016 um 03:08 schrieb Steven D'Aprano:

On Sat, 13 Aug 2016 06:44 am, Mok-Kong Shen wrote:


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

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


Does that help?


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


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


But in my function test() there is a code line "list2=list2+[4,5,6]".
Could you kindly explain why this didn't work?

M. K. Shen



Hello

list1 += [4, 5, 6 ] is slightly different than list1 = list1 + [4, 5, 6]

see:


list1 = [1, 2, 3]
id(list1)

45889616
list1 += [4, 5, 6 ]

id(list1)

45889616   # < -same address

list1

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

so list1 += [4, 5, 6 ]  updates the existing list


list1 = [1, 2, 3]
id(list1)

45888656

list1 = list1 + [4, 5, 6]
id(list1)

45862424 # <- a new address

list1

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

so list1 = list1 + [4, 5, 6] create a new list 


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


Re: How do I make a video animation with transparent background?

2016-08-14 Thread Martin Schöön
Den 2016-08-10 skrev Lawrence D’Oliveiro :
> On Thursday, August 11, 2016 at 9:34:33 AM UTC+12, Martin Schöön wrote:
>> Next on my TODO list is to look into the color composition idea. I
>> have already looked this tutorial:
>> https://youtu.be/sZzmksnzrX0
>
> If the images you generate have an alpha channel, Blender can composite
> them directly, without needing colour-keying.

Colour-keying works in Blender -- I only need to tweak all those parameters
a bit more to get a satisfying end result.

Case closed and it was not a Python/Matplotlib issue.

I still need the animated numerical thing but drop it here. I will be
back with a separate thread/subject for that.

Again, thanks for the input.

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


Re: Anyone here running Python on a PowerPC?

2016-08-14 Thread Tim Chase
On 2016-08-14 12:28, Steven D'Aprano wrote:
> Is there anyone here running Python on a PowerPC willing to help me
> diagnose and fix this issue?
> 
> http://bugs.python.org/issue27761

Not so savvy in the building, but I've got a Mac PPC (still on 10.4
or whatever the last-ish PPC build of OS X was) that's sitting on my
desk disconnected.  I can easily hook it back up and boot Mac or
install/boot some other OS if needed, and then follow debugging steps
as directed...

-tkc


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


SmallTalk-like interactive environment on base of cPython 2.7.x + wx

2016-08-14 Thread Dmitry Ponyatov
Does anybody can recomend some links on tutorials on making custom dynamic 
languages or objects systems on top of cPython2 ?

I want some interactive dynamic object environment with SmallTalk look&feel but 
with Python syntax.

Other tutorials I'm interested in are reflection, dynamic bytecode 
(de)compilation, using internal Python parser (for syntax highlighting for 
example), metacompilation (?) and making dynamic object VMs on top of cPython.
-- 
https://mail.python.org/mailman/listinfo/python-list


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

2016-08-14 Thread Chris Angelico
On Sun, Aug 14, 2016 at 8:49 PM, BartC  wrote:
> Well, it's using exec(). So it is generating new program code at runtime.
> That is possible in quite a few languages, even C.

It doesn't have to; that's only so it doesn't have to manually
construct a Function object. Or you could make a simpler __init__ that
requires keyword arguments. Here's an exec-free version:

def record(name, fieldnames):
if isinstance(fieldnames, str):
fieldnames = fieldnames.split()
class Inner(object):
__slots__ = fieldnames
def __init__(self, **kwargs):
for n in fieldnames:
if n in kwargs: setattr(self, n, kwargs[n])
def __repr__(self):
fields = ', '.join("%s=%r" % (n, getattr(self, n)) for n
in fieldnames)
return "record %s(%s)" % (type(self).__name__, fields)
Inner.__name__ = Inner.__qualname__ = name
return Inner

You lose the ability to construct a record with positional args, and
you lose the argument name info from help() and other forms of
introspection, but it works, and it doesn't use exec.

How do you do that in C? Do you have an 'exec' function in C?
Certainly not in any of my compilers. The nearest equivalent would be
a compile-time directive, eg a preprocessor macro, and that most
definitely isn't this flexible. Types just aren't first-class objects
in C.

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


Re: I'm looking for a part-time job in Python / Django

2016-08-14 Thread Chris Angelico
On Sun, Aug 14, 2016 at 9:00 PM, Uri Even-Chen  wrote:
> To [email protected],
>
> I'm looking for a part-time job in Python / Django, do you know anything? I
> live in Herzliya, Israel. I can't relocate but I can work from home. You
> can see my CV on LinkedIn. Please let me know if you have any job for me.
>

Hi! This isn't a job posting list, but there are a few places that
accept job ads. One good one is the Python Job Board:

https://www.python.org/jobs/

Browse that, and possibly subscribe to it; there are a lot of jobs
that would demand relocation, but every now and then you'll see a
remote opening. You could also search the web for "django jobs" and
see what you find.

Good luck! There are definitely Python jobs out there, but you may
find that most of them require physical presence in the US or Europe.
But don't give up!

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


Re: SmallTalk-like interactive environment on base of cPython 2.7.x + wx

2016-08-14 Thread Chris Angelico
On Sun, Aug 14, 2016 at 9:53 PM, Dmitry Ponyatov  wrote:
> Does anybody can recomend some links on tutorials on making custom dynamic 
> languages or objects systems on top of cPython2 ?
>
> I want some interactive dynamic object environment with SmallTalk look&feel 
> but with Python syntax.

Any particular reason for using 2.7 rather than 3.x?

As a general rule, I would advise you to make your dynamic language
actually *be* Python. With judicious use of decorators and other
metaprogramming tools, you can craft a DSL (domain-specific language)
that has whatever broad look-and-feel you choose, but will be directly
executable using a Python interpreter.

For anything more specific than that, it'd be helpful to know more
details about what you're trying to do. What's a "dynamic object
environment"?

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


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

2016-08-14 Thread Steven D'Aprano
On Thu, 11 Aug 2016 06:33 am, Juan Pablo Romero Méndez wrote:

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

Python has more than one type. Don't confuse dynamic typing with weak typing
or untyped (typeless) languages. More on this below.

I don't believe that you will find "an example of a situation..." as you say
above. It sounds like you are hope to find a clear example of "If you do
This, then dynamic languages are the Clear Winner". But I don't think you
will. Dynamic languages tend to produce clear productivity improvements
over statically typed languages, but of course this is only "typically"
true, not a guarantee that applies to every single programmer or project.

Typically:

- dynamic languages are less verbose;
- dynamic languages are faster to develop in; many organisations 
  prototype applications in Python (say) before re-writing it in
  C++/Java/whatever;
- far less time spent fighting the compiler;
- dynamic languages often have fewer bugs, because it is easier to 
  reason about the code (no "undefined behaviour" like in C!) and 
  fewer lines of code to reason about;
- but statically typed languages allow you to prove the absence 
  of certain types of bugs.

The exception is if you try to write statically typed code in a dynamic
language. Then you get the worst of both styles of coding: the verbose,
heavyweight style of many static languages, but without the automated
correctness proofs, plus the performance costs of dynamic typing, but
without the rapid development.



Regarding types and type systems, if you haven't already read this, you
should:

https://cdsmith.wordpress.com/2011/01/09/an-old-article-i-wrote/

"Static typing" (e.g. Pascal, C, Java, Haskell) and "dynamic typing" (e.g.
Python, Javascript, Ruby, Lua) differ on when and how values are checked
for type-compatibility.

"Strong" and "weak" typing are ends of a continuum. Nearly all languages are
a little bit weak (they allow automatic coercions between numeric types)
but mostly strong (they don't automatically coerce integers to arrays).
Javascript, Perl and PHP are weaker than Python because they'll coerce
strings to numbers automatically and Python won't.

I don't know many untyped languages apart from machine code or maybe
assembly. Perhaps Forth? (Maybe not -- some Forths include a separate
floating point stack as well as the usual stack.) Hypertalk treated
everything as strings. Tcl treats nearly everything as strings, although it
also has arrays.

So, Python has types, and it is a mostly strong typed language. It will do
relatively few automatic coercions.



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

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


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

2016-08-14 Thread breamoreboy
On Sunday, August 14, 2016 at 7:09:47 AM UTC+1, Paul Rubin wrote:
> Steven D'Aprano writes:
> > If the Python community rallies around this "record" functionality and
> > takes to it like they took too namedtuple
> 
> I like namedtuple and I think that it's a feature that they're modified
> by making a new copy.  I know that has overhead but it's palpably
> bug-avoidant.  I've used them extensively in some programs and they took
> a considerable burden off my mind compared to using something like
> structs or records.

You might find this https://glyph.twistedmatrix.com/2016/08/attrs.html an 
interesting read.

Kindest regards.

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


Module pyswisseph

2016-08-14 Thread Lallo

Hi,

I would like to install the module pypi.python.org/pypi/pyswisseph in 
Python 2.7.12 on Windows 7 / 64 Bit.

In the instructions it says to use CMake and MinGW.
How should I do? Can you help me please?

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


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

2016-08-14 Thread BartC

On 14/08/2016 14:18, Chris Angelico wrote:

On Sun, Aug 14, 2016 at 8:49 PM, BartC  wrote:

Well, it's using exec(). So it is generating new program code at runtime.
That is possible in quite a few languages, even C.


It doesn't have to; that's only so it doesn't have to manually
construct a Function object. Or you could make a simpler __init__ that
requires keyword arguments. Here's an exec-free version:

def record(name, fieldnames):
if isinstance(fieldnames, str):
fieldnames = fieldnames.split()
class Inner(object):
__slots__ = fieldnames
def __init__(self, **kwargs):
for n in fieldnames:
if n in kwargs: setattr(self, n, kwargs[n])
def __repr__(self):
fields = ', '.join("%s=%r" % (n, getattr(self, n)) for n
in fieldnames)
return "record %s(%s)" % (type(self).__name__, fields)
Inner.__name__ = Inner.__qualname__ = name
return Inner

You lose the ability to construct a record with positional args, and
you lose the argument name info from help() and other forms of
introspection, but it works, and it doesn't use exec.

How do you do that in C? Do you have an 'exec' function in C?


Not built-in to the language. C code can however write out source code, 
and invoke a C compiler to turn it into a shared library, which can then 
be linked to dynamically.


But it can't create a new record or struct type at runtime which can 
then be accessed using normal syntax, in compiled code that already 
existed before the record was created. (Only if access code is generated 
too.)


In general, doing the equivalent of what Steven's record() example was 
doing can also be done across a few languages, up to a point anyway (and 
usually not in five minutes!):


* Creating a handle to a data structure representing a new record 
(giving record name and a list of fields).


* Using that handle to create an instance of such a record (represented 
by any means, even just using a list).


* Allowing some way to set or get fields of each instance.

The last is harder if you want a method that looks like a field access. 
Doing it via functions is straightforward; allowing dot operators 
('d.month') is much harder, especially with more rigid languages where 
everything is expected to be resolved at compile time.


It's a bit easier in Python because 'd.month' already involves dynamic 
mechanisms which can be hooked into. In fact, the above implementation 
and the original are based on classes and their existing means of 
setting and getting attributes.


So it's about having a more streamlined way to define such a class with 
a fixed set of attributes. Both use the __slots__ feature to restrict 
the number of attributes to that fixed list (I imagine it would have 
been more difficult without that).


--
Bartc



Certainly not in any of my compilers. The nearest equivalent would be
a compile-time directive, eg a preprocessor macro, and that most
definitely isn't this flexible. Types just aren't first-class objects
in C.

ChrisA



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


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

2016-08-14 Thread MRAB

On 2016-08-14 15:29, Steven D'Aprano wrote:
[snip]

I don't know many untyped languages apart from machine code or maybe
assembly. Perhaps Forth? (Maybe not -- some Forths include a separate
floating point stack as well as the usual stack.) Hypertalk treated
everything as strings. Tcl treats nearly everything as strings, although it
also has arrays.


[snip]

BCPL (ancestor of C) had a single data type, the bit-pattern.

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


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

2016-08-14 Thread Chris Angelico
On Mon, Aug 15, 2016 at 2:44 AM, MRAB  wrote:
> On 2016-08-14 15:29, Steven D'Aprano wrote:
> [snip]
>>
>> I don't know many untyped languages apart from machine code or maybe
>> assembly. Perhaps Forth? (Maybe not -- some Forths include a separate
>> floating point stack as well as the usual stack.) Hypertalk treated
>> everything as strings. Tcl treats nearly everything as strings, although
>> it
>> also has arrays.
>>
> [snip]
>
> BCPL (ancestor of C) had a single data type, the bit-pattern.

REXX treats everything as strings, and has a special type of variable
(the "stem") which can do the jobs that arrays and mappings can.
Booleans are just strings of either "1" or "0". Numbers are strings
containing nothing but decimal digits, an optional decimal point, and
maybe an exponent. Open files are referenced by their file names.
(Which means you can never have a file open for two different jobs at
once. You explicitly close it in between.)

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


Re: Python slang

2016-08-14 Thread Random832
On Wed, Aug 10, 2016, at 19:57, Michael Torrie wrote:
> But the grammar must still be a bit complex as sometimes the LHS of the
> = is an expression, as well as the RHS.

The only place that an *arbitrary* expression (including e.g. = as
equality) can appear in the LHS is inside parentheses, otherwise you're
limited to dot and call/index (which are both parentheses, which isn't a
problem for the parser since no object can do both and it's the same
kind of expression) just like in Python.
-- 
https://mail.python.org/mailman/listinfo/python-list


Finding the first index in a list greater than a particular value

2016-08-14 Thread Atri Mahapatra
I have a list of dictionaries which look like this:
[{'Width': 100, 'Length': 20.0, 'Object': 'Object1'}, {'Width': 12.0, 'Length': 
40.0, 'Object': 'Object2'}.. so on till 10] 

I would like to find the first index in the list of dictionaries whose length 
is greater than a particular value

f=lambda seq, m: [ii for ii in range(0, len(seq)) if seq[ii]['Length'] > m][0] 
and it throws an error

can  anyone suggest a way  to do it?

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


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

2016-08-14 Thread Paul Rubin
MRAB  writes:
>> I don't know many untyped languages apart from machine code or maybe
>> assembly. Perhaps Forth? (Maybe not -- some Forths include a separate
>> floating point stack as well as the usual stack.) 

Forth is essentially untyped.  There's no distinction between integers,
characters, and pointers.  There is usually a separate FP stack, but FP
variables are stored in memory just like integers, and there's no
distinction between the memory cells.  You just use a different command
to push the contents of a memory cell onto the FP stack than onto the
integers stack.
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Finding the first index in a list greater than a particular value

2016-08-14 Thread MRAB

On 2016-08-14 19:17, Atri Mahapatra wrote:

I have a list of dictionaries which look like this:
[{'Width': 100, 'Length': 20.0, 'Object': 'Object1'}, {'Width': 12.0, 'Length': 
40.0, 'Object': 'Object2'}.. so on till 10]

I would like to find the first index in the list of dictionaries whose length 
is greater than a particular value

f=lambda seq, m: [ii for ii in range(0, len(seq)) if seq[ii]['Length'] > m][0] 
and it throws an error

can  anyone suggest a way  to do it?


What is the error?

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


Re: Finding the first index in a list greater than a particular value

2016-08-14 Thread Atri Mahapatra
On Monday, 15 August 2016 00:03:59 UTC+5:30, MRAB  wrote:
> On 2016-08-14 19:17, Atri Mahapatra wrote:
> > I have a list of dictionaries which look like this:
> > [{'Width': 100, 'Length': 20.0, 'Object': 'Object1'}, {'Width': 12.0, 
> > 'Length': 40.0, 'Object': 'Object2'}.. so on till 10]
> >
> > I would like to find the first index in the list of dictionaries whose 
> > length is greater than a particular value
> >
> > f=lambda seq, m: [ii for ii in range(0, len(seq)) if seq[ii]['Length'] > 
> > m][0] and it throws an error
> >
> > can  anyone suggest a way  to do it?
> >
> What is the error?

Traceback (most recent call last):
  File "", line 1, in 
f(lengthlist, 10)
   f=lambda seq, m: [ii for ii in range(0, len(seq)) if seq[ii] > m][0]
TypeError: unorderable types: dict() > int()
-- 
https://mail.python.org/mailman/listinfo/python-list


RE: Issue with ctypes and callback functions

2016-08-14 Thread Bill Somerville
-Original Message-
From: Bill Somerville 
Sent: 04 August 2016 18:23
To: 'eryk sun' ; [email protected]
Subject: RE: Issue with ctypes and callback functions

-Original Message-
From: eryk sun [mailto:[email protected]] 

from_param is a hook method for a type that's set in a function pointer's 
argtypes. It gets called to convert an argument when the function pointer is 
called from Python. The return value can be a ctypes instance, an object with a 
hard-coded conversion (e.g. a string or integer), or an object that defines an 
_as_parameter_ attribute.

Only ctypes types are supported in callbacks, which unfortunately isn't 
documented clearly. Specifically, the class dict needs to be an extended C 
storage dict (i.e. StgDictObject), either to look up the getfunc of a simple 
type or to ensure that instantiating a non-simple type returns a ctypes 
instance (i.e. CDataObject) with a known size.
The relevant code in _CallPythonObject is as follows (when stripped of 
declarations and error handling):

cnv = PySequence_GetItem(converters, i);
dict = PyType_stgdict(cnv);
if (dict && dict->getfunc && !_ctypes_simple_instance(cnv)) {
v = dict->getfunc(*pArgs, dict->size);
PyTuple_SET_ITEM(arglist, i, v);
} else if (dict) {
obj = (CDataObject *)PyObject_CallFunctionObjArgs(cnv, NULL);
memcpy(obj->b_ptr, *pArgs, dict->size);
PyTuple_SET_ITEM(arglist, i, (PyObject *)obj);
} else {
PyErr_SetString(PyExc_TypeError,
"cannot build parameter");

I don't have much experience with SWIG. Does it provide some means to 
instantiate a wrapped type from an address? If it does, then you can use a void 
pointer as the callback parameter.

Hi Eryk,

Thanks for looking at this. I had got as far as looking at the code above and 
guessed that only ctypes types were supported. This does seem like a bit of a 
functionality gap that cannot be solved easily by users of ctypes. It would 
appear to be fairly easy to define a protocol for objects that can be 
instantiated from ctypes arguments before passing to Python callables, i.e. 
some requirement for a class method factory that takes a ctypes type as a 
parameter and returns a Python object. This would seem to be a natural 
complement to from_param()/_as_parameter_ for the C/C++ to Python direction of 
function calls.

I had started with an implementation that converted a c_void_p value and 
instantiated the required SWIG Python wrapper. This is not too difficult as 
SWIG allows its types to be extended in both the C/C++ world and the Python 
world. The result is only a one liner for each callback parameter but it is 
still ugly as the writer of the callback has to know what type the argument is 
and remember to call the "constructor" before accessing the attributes of the 
object.

My requirement is to wrap an API for testers and customers to write simple test 
cases and any ugliness in the test cases is undesirable given that the users 
will probably not be Python experts and maybe not even developers.

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


Re: Finding the first index in a list greater than a particular value

2016-08-14 Thread MRAB

On 2016-08-14 19:40, Atri Mahapatra wrote:

On Monday, 15 August 2016 00:03:59 UTC+5:30, MRAB  wrote:

On 2016-08-14 19:17, Atri Mahapatra wrote:
> I have a list of dictionaries which look like this:
> [{'Width': 100, 'Length': 20.0, 'Object': 'Object1'}, {'Width': 12.0, 
'Length': 40.0, 'Object': 'Object2'}.. so on till 10]
>
> I would like to find the first index in the list of dictionaries whose length 
is greater than a particular value
>
> f=lambda seq, m: [ii for ii in range(0, len(seq)) if seq[ii]['Length'] > 
m][0] and it throws an error
>
> can  anyone suggest a way  to do it?
>
What is the error?


Traceback (most recent call last):
  File "", line 1, in 
f(lengthlist, 10)
   f=lambda seq, m: [ii for ii in range(0, len(seq)) if seq[ii] > m][0]
TypeError: unorderable types: dict() > int()


That's not the same code as your original post.

Your original post has:

seq[ii]['Length'] > m

which is correct, but your traceback shows:

seq[ii] > m

which tries to compare the entire dict to m, hence the exception.

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


compile a python3 project with autotools

2016-08-14 Thread bnrj . rudra
Hi,

I am trying to compile a python project using autotools. The project runs 
absolutely fine from command line, and it uses Gtk3 library. 

the src file is:

├── src
│   ├── Makefile.am
│   ├── Makefile.in
│   ├── Mkbib
│   │   ├── cell.py
│   │   ├── dialogue.py
│   │   ├── filemanager.py
│   │   ├── getdata.py
│   │   ├── __init__.py
│   │   ├── main.py
│   │   ├── Makefile.am
│   │   ├── Makefile.in
│   │   ├── menu.py
│   │   ├── mkbib.in
│   │   ├── pybib.py
│   │   └── view.py
│   └── mkbib.in

with the mkbib.in is:

#!/usr/bin/env python3

import sys
sys.path.insert(1, '@pythondir@')

from mkbib.main import mkbib
if __name__ == "__main__":
app = mkbib()
r = app.run()
sys.exit(r)


src/Mkbib/main.py is the main program. This is structured as:

class Window(Gtk.ApplicationWindow):
def __init__(self, application, giofile=None):
Gtk.ApplicationWindow.__init__(self,
   application=application,
   default_width=1000,
   default_height=200,
   border_width=5)

class mkbib(Gtk.Application):
def __init__(self):
Gtk.Application.__init__(self)
def new_window(self, filename=None):
window = Window(self)
window.show_all()
def run(self):
self.new_window()
Gtk.main()

Now, the problem is it is compiling fine (make; make install goes without any 
error)

make --silent
Making all in data
Making all in icons
Making all in hicolor
Making all in 48x48
Making all in apps
Making all in scalable
Making all in apps
Making all in ui
Making all in src
Making all in Mkbib

but while running the application, I am getting error:
(mkbib:14843): Gtk-CRITICAL **: New application windows must be added after the 
GApplication::startup signal has been emitted.


May I be kindly helped?
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: compile a python3 project with autotools

2016-08-14 Thread Rudra Banerjee
Plz ignore src/Mkbib/mkbib.in file, as appeared in the tree
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: compile a python3 project with autotools

2016-08-14 Thread Lawrence D’Oliveiro
On Monday, August 15, 2016 at 9:24:04 AM UTC+12, Rudra Banerjee wrote:
> but while running the application, I am getting error:
> (mkbib:14843): Gtk-CRITICAL **: New application windows must be added after
> the GApplication::startup signal has been emitted.

Does the application actually fail?

Because I see a lot of this sort of thing, which doesn’t seem to actually 
affect the running of the application.

So I ignore it. :)
-- 
https://mail.python.org/mailman/listinfo/python-list


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

2016-08-14 Thread Lawrence D’Oliveiro
On Monday, August 15, 2016 at 4:31:44 AM UTC+12, BartC wrote:

> But it can't create a new record or struct type at runtime which can 
> then be accessed using normal syntax, in compiled code that already 
> existed before the record was created.

That’s an awful lot of “which ... that” qualification going on.

Remember, Python, which is supposed to be able to do all that, is implemented 
in C.
-- 
https://mail.python.org/mailman/listinfo/python-list


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

2016-08-14 Thread Michael Torrie
On 08/14/2016 04:45 PM, Lawrence D’Oliveiro wrote:
> On Monday, August 15, 2016 at 4:31:44 AM UTC+12, BartC wrote:
> 
>> But it can't create a new record or struct type at runtime which can 
>> then be accessed using normal syntax, in compiled code that already 
>> existed before the record was created.
> 
> That’s an awful lot of “which ... that” qualification going on.
> 
> Remember, Python, which is supposed to be able to do all that, is implemented 
> in C.

Though PyPy proves that Python can bootstrap itself, and do that fairly
efficiently.

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


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

2016-08-14 Thread Steven D'Aprano
On Mon, 15 Aug 2016 08:45 am, Lawrence D’Oliveiro wrote:

> On Monday, August 15, 2016 at 4:31:44 AM UTC+12, BartC wrote:
> 
>> But it can't create a new record or struct type at runtime which can
>> then be accessed using normal syntax, in compiled code that already
>> existed before the record was created.
> 
> That’s an awful lot of “which ... that” qualification going on.
> 
> Remember, Python, which is supposed to be able to do all that, is
> implemented in C.


Corollary to the Fundamental Theorem of Software Engineering:

Every problem in software engineering can be solved in any language by
writing an interpreter for another language, except the problem of poor
performance.




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

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


Re: Finding the first index in a list greater than a particular value

2016-08-14 Thread Michael Selik
On Sun, Aug 14, 2016 at 2:21 PM Atri Mahapatra 
wrote:

> I have a list of dictionaries which look like this:
> [{'Width': 100, 'Length': 20.0, 'Object': 'Object1'}, {'Width': 12.0,
> 'Length': 40.0, 'Object': 'Object2'}.. so on till 10]
>
> I would like to find the first index in the list of dictionaries whose
> length is greater than a particular value
>
> f=lambda seq, m: [ii for ii in range(0, len(seq)) if seq[ii]['Length'] >
> m][0] and it throws an error
>
> can  anyone suggest a way  to do it?
>

There's no need to play code golf (squishing your thoughts into one line).

def index_of_first_record_of_minimum_size(records, m):
'records is an iterable of dictionaries'

for i, dictionary in enumerate(list_of_dicts):
if dictionary['Length'] > m:
return i
raise ValueError('no record found with "Length" greater than %r' %
(m,))


If you have trouble coming up with a good name for a function, that
suggests you are either doing too much or too little with that function. If
you want the same task outside of a function, change the return to a break.

if not records:
raise ValueError('no records found')
for index, d in enumerate(records):
if d['Length'] > m:
break # `index` is now the index of the first record of minimum
length
else:
raise ValueError('no record found with "Length" greater than %r' %
(m,))
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Finding the first index in a list greater than a particular value

2016-08-14 Thread Jussi Piitulainen
Atri Mahapatra writes:

> I have a list of dictionaries which look like this:
> [{'Width': 100, 'Length': 20.0, 'Object': 'Object1'}, {'Width': 12.0, 
> 'Length': 40.0, 'Object': 'Object2'}.. so on till 10] 
>
> I would like to find the first index in the list of dictionaries whose
> length is greater than a particular value
>
> f=lambda seq, m: [ii for ii in range(0, len(seq)) if seq[ii]['Length'] > 
> m][0] and it throws an error

It doesn't throw an error (raise an exception) if there is at least one
such dictionary in the list.

If you see an error message for some other reason, you need to give more
information. Like, what error? And for what code exactly. This problem
can probably be tested with [{'Length':20.0}, {'Length':40.0}], so you
don't need much code to ask the question.

(Some people avoid giving actual details at a great cost. Even when
asked nicely. Repeatedly. Such people cannot be helped. A mystery.)

> can  anyone suggest a way  to do it?

Use a generator expression instead of a list comprehension. Ask for the
next value from the generator object, or a default when there is no next
value.

next((k for k,d in enumerate(ll) if d['Length'] > 23), 'sorry')
==> 1

next((k for k,d in enumerate(ll) if d['Length'] > 43), 'sorry')
==> 'sorry'

It's often better to iterate over enumerate(ll) to get both the index
and the value than over range(len(ll)) to get only the index.

Or just write an actual elementary for-loop that returns the index when
it finds the first good value:

def f(dictses, tooshort):
   for k,d in enumerate(dictses):
   if d['Length'] > tooshort:
   return k
   return 'sorry'

The default value from a function is None (if it falls through without a
return statement). There is a tradition of returning -1 when no valid
index is found. The one thing not to do is to use a non-index as if it
was a valid index. An exception in an exceptional situation may be a
good thing precisely because it calls attention to itself.
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: compile a python3 project with autotools

2016-08-14 Thread Rudra Banerjee
On Monday, August 15, 2016 at 12:39:33 AM UTC+2, Lawrence D’Oliveiro wrote:
> On Monday, August 15, 2016 at 9:24:04 AM UTC+12, Rudra Banerjee wrote:
> > but while running the application, I am getting error:
> > (mkbib:14843): Gtk-CRITICAL **: New application windows must be added after
> > the GApplication::startup signal has been emitted.
> 
> Does the application actually fail?
> 
> Because I see a lot of this sort of thing, which doesn’t seem to actually 
> affect the running of the application.
> 
> So I ignore it. :)

It fails to close, if I run it from command line. Also, even if I close it by 
close button, the  command line does not return.

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


Re: compile a python3 project with autotools

2016-08-14 Thread Chris Angelico
On Mon, Aug 15, 2016 at 7:23 AM,   wrote:
> #!/usr/bin/env python3
>
> import sys
> sys.path.insert(1, '@pythondir@')
>
> from mkbib.main import mkbib
> if __name__ == "__main__":
> app = mkbib()
> r = app.run()
> sys.exit(r)
>
>
> Now, the problem is it is compiling fine (make; make install goes without any 
> error)
>
> make --silent
> Making all in data
> Making all in icons
> Making all in hicolor
> Making all in 48x48
> Making all in apps
> Making all in scalable
> Making all in apps
> Making all in ui
> Making all in src
> Making all in Mkbib
>
> but while running the application, I am getting error:
> (mkbib:14843): Gtk-CRITICAL **: New application windows must be added after 
> the GApplication::startup signal has been emitted.
>

On Mon, Aug 15, 2016 at 4:23 PM, Rudra Banerjee  wrote:
> It fails to close, if I run it from command line. Also, even if I close it by 
> close button, the  command line does not return.

When you build Mkbib, is it supposed to run the app immediately? That
appears to be what's happening.

Also, your code seems to have some odd redundancies:

> src/Mkbib/main.py is the main program. This is structured as:
>
> class mkbib(Gtk.Application):
> def __init__(self):
> Gtk.Application.__init__(self)

You shouldn't need this. All it does is call the superclass's
initializer with the same arguments. The net result is that your class
plays poorly with multiple inheritance (probably not going to hurt you
here, but if ever you did use MI, this would bite you), and other than
that, no significant effect. Omit this function altogether unless you
actually need to initialize.

> def new_window(self, filename=None):
> window = Window(self)
> window.show_all()

What's the filename do?

> def run(self):
> self.new_window()
> Gtk.main()
>

This is what's sometimes called a "procedure" - a function that
doesn't have any meaningful return value. (When you run off the end of
the function, Python assumes a "return None". Since it has no other
return statements, this function will never return anything other than
None.) But in mkbib.in, you call it as a function - "r = app.run()".
Possibly not what you intended?

Generally, I would expect the 'make' process to not be invoking your
app, but perhaps I'm misunderstanding what mkbib.in does or when it's
invoked.

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