Re: Lawrence D'Oliveiro

2016-10-02 Thread Bob Martin
in 765736 20161001 072941 Paul Rubin  wrote:
>Chris Angelico  writes:
>>> Why can't you block "PEDOFILO"?
>> I've no idea who you're talking about
>
>That's the weird Italian spam that the newsgroup has been getting for a
>while.  I've been wondering for a while if anyone knows what the story
>is, i.e. why it's on comp.lang.python but not on other newsgroups that
>I've noticed.

It's in several of the groups that I follow.
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: unintuitive for-loop behavior

2016-10-02 Thread Marko Rauhamaa
Jussi Piitulainen :
> I could have just posted that yes, it *could* be done, there's nothing
> so special about variables and scope in Python. But I pretty much know
> that the response to *that* would have been yet another round of "You
> don't understand that Python variables are different, they are not
> boxes in fixes locations but name bindings, and no, it cannot be
> done."

Your valiant effort to get a point across has been noted.


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


Re: unintuitive for-loop behavior

2016-10-02 Thread Jussi Piitulainen
Steve D'Aprano writes:

> On Sun, 2 Oct 2016 11:44 am, Gregory Ewing wrote:
>
>> Steve D'Aprano wrote:
>>
>>> Certainly when you call a function, the local bindings need to be
>>> created. Obviously they didn't exist prior to calling the function!
>>> I didn't think that was the difference you were referring to, and I
>>> fail to see how it could be relevant to the question of for-loop
>>> behaviour.
>> 
>> My proposed change is (mostly) equivalent to turning the
>> loop body into a thunk and passing the loop variable in as
>> a parameter.
>
> A thunk is not really well-defined in Python, because it doesn't
> exist, and therefore we don't know what properties it will have. But
> generally when people talk about thunks, they mean something like a
> light-weight anonymous function without any parameters:
>
> https://en.wikipedia.org/wiki/Thunk

According to a story (probably told in that page), thunks got their name
from being "already thunk of". Scheme at least doesn't have any special
"light-weight" anonymous functions, it just has (lambda () ...).

> You say "passing the loop variable in as a parameter" -- this doesn't
> make sense. Variables are not values in Python. You cannot pass in a
> *variable* -- you can pass in a name (the string 'x') or the *value*
> bound to the name, but there's no existing facility in Python to pass
> in a variable.

He means it like this:

   def g4159(i):
   

   g4159(next(g4158))

The loop variable becomes the parameter of a function. The values are
passed to that function, one by one.

[snip tangent]

> hard to talk about your hypothetical change except in hand-wavy terms:
>
> "Something magically and undefined happens, which somehow gives the
> result I want."

The following is intended to be more concrete. You, Steve, have already
declared (in a followup to me in this thread) that it is not magical
(but is obfuscated, convoluted, non-idiomatic, and so on, but then you
had not understood that the expansion is not intended to be actual
source code).

So a Python for-loop *could* behave (*could have been defined to*
behave) so that

   for v in s:
   c

is equivalent to

   try:
   gs = iter(s)
   while True:
   def gf(v):
   
   c
   gf(next(gs))
   except StopIteration:
   pass

where gs and gf are new names and  make it so
that only v becomes local to gf.

>> This is the way for-loops or their equivalent are actually
>> implemented in Scheme, Ruby, Smalltalk and many other similar
>> languages, which is why they don't have the same "gotcha".
>
> Instead, they presumably have some other gotcha -- "why don't for
> loops work the same as unrolled loops?", perhaps.

I don't think so, but then I only know Scheme where tail recursion is
its own reward.

On the other hand, in Scheme I can actually implement any number of
different looping constructs if I find them desirable.  Instead of
trying to explain that something is, in principle, not only possible but
also quite straightforward, I could just show an actual implementation.

(I did that once, but then it was a beginner who thought that some
really basic thing would be impossible merely because they were such a
beginner.)

> In Python, the most obvious gotcha would be that if for-loops
> introduced their own scope, you would have to declare any other
> variables in the outer scope nonlocal. [snip]

The intention is that the for-loop would own the loop variables. Other
variables would stay in the outer scope.
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: unintuitive for-loop behavior

2016-10-02 Thread Jussi Piitulainen
Marko Rauhamaa writes:

> Jussi Piitulainen wrote:
>> I could have just posted that yes, it *could* be done, there's
>> nothing so special about variables and scope in Python. But I pretty
>> much know that the response to *that* would have been yet another
>> round of "You don't understand that Python variables are different,
>> they are not boxes in fixes locations but name bindings, and no, it
>> cannot be done."
>
> Your valiant effort to get a point across has been noted.

Thank you. It's nice to be heard.
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: inplace text filter - without writing file

2016-10-02 Thread breamoreboy
On Sunday, October 2, 2016 at 6:19:14 AM UTC+1, Sayth Renshaw wrote:
> 
> I just can't quite get it.
> 
> def return_files(file_list):
> for filename in sorted(file_list):
> file = os.path.join(dir_path, filename)
> print(file)
> with open(file) as fd:
> print(fd)
> for fileItem in fd:
> print(fileItem)
> for line in fileItem:
> print(line[0])
> if line.startswith('

Re: unintuitive for-loop behavior

2016-10-02 Thread Steve D'Aprano
On Sun, 2 Oct 2016 04:06 pm, Chris Angelico wrote:

> On Sun, Oct 2, 2016 at 3:19 PM, Steve D'Aprano
>  wrote:
>> In IronPython, you could have the following occur in a function locals,
>> just as it could happen CPython for globals:
>>
>> - delete the name binding "x"
>> - which triggers a dictionary resize
>> - bind a value to x again
>> - because the dictionary is resized, the new "slot" for x is in a
>>   completely different position of the dictionary to the old one
>>
>> There is no user-visible difference between these two situations. Your
>> code works the same way whether it is executed inside a function or at
>> the global top level, whether functions use the CPython local variable
>> optimization or not.
> 
> Hmm, interesting. I don't have IronPython here, but maybe you can tell
> me what this does:
> 
> print(str)
> str = "demo"
> print(str)
> del str
> print(str)
> 
> and the same inside a function. In CPython, the presence of 'str =
> "demo"' makes str function-local, ergo UnboundLocalError on the first
> reference; but globals quietly shadow built-ins, so this will print
> the class, demo, and the class again. If IronPython locals behave the
> way CPython globals behave, that would most definitely be a
> user-visible change to shadowing semantics.

That's a nice catch! 

But its not a difference between "update binding" versus "new binding" --
its a difference between LOAD_FAST and LOAD_whatever is used for things
besides locals.

steve@orac:~$ ipy
IronPython 2.6 Beta 2 DEBUG (2.6.0.20) on .NET 2.0.50727.1433
Type "help", "copyright", "credits" or "license" for more information.
>>> def func():
... print(str)
... str = 1
...
>>> func()
Traceback (most recent call last):
UnboundLocalError: Local variable 'str' referenced before assignment.

Although IronPython does the same thing as CPython here, I'm not 100% sure
that this behaviour would be considered language specification or a mere
guideline. If the author of an alternate implementation wanted to specify a
single name resolution procedure:

- look for local variable;
- look for nonlocal;
- look for global;
- look for builtin;
- fail

rather than two:

(1) 
- look for local;
- fail;

(2)
- look for nonlocal;
- look for global;
- look for builtin;
- fail


I'm not entirely sure that Guido would say No. I think that "fail early if
the local doesn't exist" as CPython does would be permitted rather than
mandatory. But I could be wrong.



By the way, here's an example showing that IronPython does allowing writing
to locals to affect the local namespace:

>>> def func():
... locals()['x'] = 1
... print(x)
... if False:
... x = 
...
>>> func()
1

And *that* behaviour is most definitely allowed -- the fact that writing to
locals() isn't supported by CPython is most definitely an implementation-
specific limitation, not a feature.




-- 
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: ConfigParser: use newline in INI file

2016-10-02 Thread Thorsten Kampe
* Ned Batchelder (Sat, 1 Oct 2016 17:41:28 -0700 (PDT))
> 
> On Saturday, October 1, 2016 at 6:25:16 PM UTC-4, Thorsten Kampe wrote:
> > * Ben Finney (Sun, 02 Oct 2016 07:12:46 +1100)
> > > 
> > > Thorsten Kampe  writes:
> > > 
> > > > ConfigParser escapes `\n` in ini values as `\\n`.
> > 
> > Indenting solves the problem. I'd rather keep it one line per value 
> > but it solves the problem.
> 
> If you want to have \n mean a newline in your config file, you can 
> do the conversion after you read the value:
> 
> >>> "a\\nb".decode("string-escape")
> 'a\nb'

Interesting approach (although it does not work with Python 3: decode 
is only for byte-strings and the string-escape encoding is not 
defined).

If I understand this correctly, this is equivalent to
`.replace('\\n', '\n')` ?

Thorsten

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


Re: unintuitive for-loop behavior

2016-10-02 Thread Chris Angelico
On Sun, Oct 2, 2016 at 9:20 PM, Steve D'Aprano
 wrote:
> On Sun, 2 Oct 2016 04:06 pm, Chris Angelico wrote:
>> Hmm, interesting. I don't have IronPython here, but maybe you can tell
>> me what this does:
>>
>> print(str)
>> str = "demo"
>> print(str)
>> del str
>> print(str)
>>
>> and the same inside a function. In CPython, the presence of 'str =
>> "demo"' makes str function-local, ergo UnboundLocalError on the first
>> reference; but globals quietly shadow built-ins, so this will print
>> the class, demo, and the class again. If IronPython locals behave the
>> way CPython globals behave, that would most definitely be a
>> user-visible change to shadowing semantics.
>
> That's a nice catch!
>
> But its not a difference between "update binding" versus "new binding" --
> its a difference between LOAD_FAST and LOAD_whatever is used for things
> besides locals.

The only way to prove that something is a new binding is to
demonstrate that, when this binding is removed, a previous one becomes
visible. In Python, that only ever happens across namespaces, and in
CPython, the only way I can make it happen is with globals->builtins.

> steve@orac:~$ ipy
> IronPython 2.6 Beta 2 DEBUG (2.6.0.20) on .NET 2.0.50727.1433
> Type "help", "copyright", "credits" or "license" for more information.
 def func():
> ... print(str)
> ... str = 1
> ...
 func()
> Traceback (most recent call last):
> UnboundLocalError: Local variable 'str' referenced before assignment.

Beautiful, thank you.

> Although IronPython does the same thing as CPython here, I'm not 100% sure
> that this behaviour would be considered language specification or a mere
> guideline. If the author of an alternate implementation wanted to specify a
> single name resolution procedure:
>
> - look for local variable;
> - look for nonlocal;
> - look for global;
> - look for builtin;
> - fail
>
> rather than two:
>
> (1)
> - look for local;
> - fail;
>
> (2)
> - look for nonlocal;
> - look for global;
> - look for builtin;
> - fail
>
>
> I'm not entirely sure that Guido would say No. I think that "fail early if
> the local doesn't exist" as CPython does would be permitted rather than
> mandatory. But I could be wrong.

Interesting. The real question, then, is: Are function-local names
inherently special? I can't make anything else do this. Class
namespaces don't shadow globals or builtins, even during construction:

>>> class X:
...str = "test"
...def func(self):
...print(self)
...return str
...print("123 =", func(123))
...print("str =", func(str))
...
123
123 = 
test
str = 
>>> X().func()
<__main__.X object at 0x7f98643cc2b0>


The 'str' as a function parameter uses the class's namespace, but in
the function, no sir. (As it should be.) I don't know of any other
namespaces to test.

> By the way, here's an example showing that IronPython does allowing writing
> to locals to affect the local namespace:
>
 def func():
> ... locals()['x'] = 1
> ... print(x)
> ... if False:
> ... x = 
> ...
 func()
> 1
>
> And *that* behaviour is most definitely allowed -- the fact that writing to
> locals() isn't supported by CPython is most definitely an implementation-
> specific limitation, not a feature.

Yep. Would the same have happened if you'd omitted the "if False"
part, though - that is, if 'x' were not a local name due to
assignment, could it *become* local through mutation of locals()?

I would just grab IronPython and test, except that it isn't available
in the current Debian repositories:

https://archive.debian.net/search?searchon=names&keywords=ironpython

This most likely means I'll have to do some fiddling around with deps
to get it to install :(

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


Re: ConfigParser: use newline in INI file

2016-10-02 Thread Chris Angelico
On Sun, Oct 2, 2016 at 9:34 PM, Thorsten Kampe
 wrote:
>> If you want to have \n mean a newline in your config file, you can
>> do the conversion after you read the value:
>>
>> >>> "a\\nb".decode("string-escape")
>> 'a\nb'
>
> Interesting approach (although it does not work with Python 3: decode
> is only for byte-strings and the string-escape encoding is not
> defined).
>
> If I understand this correctly, this is equivalent to
> `.replace('\\n', '\n')` ?

Yes, and a bunch of other conversions too. Notably, it will convert
'' into '\\' (I'd explain that with raw string literals but they
get wonky with trailing backslashes!), without breaking the
replacement of other constructs. But if you're okay with the string
r"\n" being impossible to enter, then go ahead and just use replace().
And you don't have to use \n specifically - if you're using replace(),
you can use any other token you like to separate lines. VX-REXX had a
nifty way to handle this: you may use any one-character separator you
like, simply by starting the string with it.

;foo;bar;quux;spam
!foo!bar!quux!spam
=foo=bar=quux=spam

If only a few of your tokens are multi-line, you could use this
technique - no escaping needed.

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


Re: ConfigParser: use newline in INI file

2016-10-02 Thread Peter Otten
Thorsten Kampe wrote:

> * Ned Batchelder (Sat, 1 Oct 2016 17:41:28 -0700 (PDT))

>> If you want to have \n mean a newline in your config file, you can
>> do the conversion after you read the value:
>> 
>> >>> "a\\nb".decode("string-escape")
>> 'a\nb'
> 
> Interesting approach (although it does not work with Python 3: decode
> is only for byte-strings and the string-escape encoding is not
> defined).

The equivalent for Python 3 is

>>> import codecs
>>> codecs.decode("a\\nb", "unicode-escape")
'a\nb'

You can teach ConfigParser to do this automatically with a custom 
Interpolation:

$ cat cpdemo.py
#!/usr/bin/env python3
import configparser as cp
import codecs

class MyInterpolation(cp.BasicInterpolation):
def before_get(self, parser, section, option, value, defaults):
return super().before_get(
parser, section, option,
codecs.decode(value, "unicode-escape"),
defaults)

p = cp.ConfigParser(interpolation=MyInterpolation())
p.read_string("""\
[foo]
bar=ham\\nspam
""")
print(p["foo"]["bar"])
$ ./cpdemo.py 
ham
spam


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


Re: rocket simulation game with just using tkinter

2016-10-02 Thread Irmen de Jong
On 1-10-2016 22:07, Irmen de Jong wrote:
> On 1-10-2016 15:44, Christian Gollwitzer wrote:
>> Am 01.10.16 um 00:59 schrieb Irmen de Jong:
>>> Hi,
>>>
>>> I've made a very simple rocket simulation game, inspired by the recent 
>>> success of SpaceX
>>
>>> You can get the code here if you want to give it a try:
>>> https://github.com/irmen/rocketsimulator
>>
>> Nice! I'll have to rebind the keys before I can successfully play this, 
>> though. My []
>> "keys" are in fact combinations of Alt+5 and Alt+6 on a German Macbook 
>> keyboard (on a
>> German PC, it is AltGr+8, AltGr+9). Why not using the arrow keys? These 
>> should be pretty
>> universal
> 
> Oh, I'm sorry about that, my little knowledge of non-US keyboard layouts 
> shows.
> Arrow keys could be an option.   For my education what are the 2 keys to the 
> right of
> the P then on your keyboard?
> 
> Rebinding the keys should be easy though just change them in the keypress and 
> keyrelease
> methods.
> 
> Irmen


I've updated the keybindings and you can now use [ and ] but also the cursor 
keys to
control the rocket's rotation.

Also I've discovered that there seems to be an issue with some Tkinter 
versions; it
sometimes doesn't update the screen fast enough. On OSX this problem is not 
present but
it is on Python 2.7 on Windows for example. I don't know what is causing it or 
how to
fix it at this time.   The FPS counter in the top right of the window should 
say 30,
that is when the game is updating at the intended speed.


Irmen

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


Re: RASTER analysis(slope)

2016-10-02 Thread Xristos Xristoou
Τη Σάββατο, 1 Οκτωβρίου 2016 - 8:52:13 μ.μ. UTC+3, ο χρήστης Xristos Xristoou 
έγραψε:
> hello team
> 
> i want to calculate slope and aspect from some RASTER 
> IMAGE(.grid,tiff,geotiff)
> who is the better method to can i do this ?
> with numpy and scipy or with some package?
> 
> 
> thnx you team

pydem like easy but have errors like this :

from pydem.dem_processing import DEMProcessor
filename_to_elevation_geotiff = 'dem.tif'
dem_proc = DEMProcessor(filename_to_elevation_geotiff)
mag, aspect = dem_proc.calc_slopes_directions()

error message :
Warning (from warnings module):
  File 
"C:\Python27\lib\site-packages\pydem-0.1.1-py2.7-win32.egg\pydem\dem_processing.py",
 line 563
self.data[data.mask] = FILL_VALUE
MaskedArrayFutureWarning: setting an item on a masked array which has a shared 
mask will not copy the mask and also change the original mask array in the 
future.
Check the NumPy 1.11 release notes for more information.
starting slope/direction calculation for chunk 1 [0:516, 0:516]

Traceback (most recent call last):
  File "C:/Python27/test/test/slop.py", line 4, in 
mag, aspect = dem_proc.calc_slopes_directions()
  File 
"C:\Python27\lib\site-packages\pydem-0.1.1-py2.7-win32.egg\pydem\dem_processing.py",
 line 853, in calc_slopes_directions
self.dY[te:be-1])
  File 
"C:\Python27\lib\site-packages\pydem-0.1.1-py2.7-win32.egg\pydem\dem_processing.py",
 line 879, in _slopes_directions
return self._tarboton_slopes_directions(data, dX, dY)
  File 
"C:\Python27\lib\site-packages\pydem-0.1.1-py2.7-win32.egg\pydem\dem_processing.py",
 line 890, in _tarboton_slopes_directions
self.facets, self.ang_adj)
  File "C:\Python27\lib\site-packages\numba\dispatcher.py", line 285, in 
_compile_for_args
return self.compile(tuple(argtypes))
  File "C:\Python27\lib\site-packages\numba\dispatcher.py", line 531, in compile
cres = self._compiler.compile(args, return_type)
  File "C:\Python27\lib\site-packages\numba\dispatcher.py", line 80, in compile
flags=flags, locals=self.locals)
  File "C:\Python27\lib\site-packages\numba\compiler.py", line 725, in 
compile_extra
return pipeline.compile_extra(func)
  File "C:\Python27\lib\site-packages\numba\compiler.py", line 369, in 
compile_extra
return self.compile_bytecode(bc, func_attr=self.func_attr)
  File "C:\Python27\lib\site-packages\numba\compiler.py", line 378, in 
compile_bytecode
return self._compile_bytecode()
  File "C:\Python27\lib\site-packages\numba\compiler.py", line 690, in 
_compile_bytecode
return self._compile_core()
  File "C:\Python27\lib\site-packages\numba\compiler.py", line 677, in 
_compile_core
res = pm.run(self.status)
  File "C:\Python27\lib\site-packages\numba\compiler.py", line 257, in run
raise patched_exception
LoweringError: Caused By:
Traceback (most recent call last):
  File "C:\Python27\lib\site-packages\numba\compiler.py", line 249, in run
stage()
  File "C:\Python27\lib\site-packages\numba\compiler.py", line 455, in 
stage_objectmode_frontend
cres = self.frontend_looplift()
  File "C:\Python27\lib\site-packages\numba\compiler.py", line 446, in 
frontend_looplift
func_attr=self.func_attr)
  File "C:\Python27\lib\site-packages\numba\compiler.py", line 743, in 
compile_ir
return pipeline.compile_ir(interp=interp, lifted=lifted, 
lifted_from=lifted_from, func_attr=func_attr)
  File "C:\Python27\lib\site-packages\numba\compiler.py", line 389, in 
compile_ir
return self._compile_ir()
  File "C:\Python27\lib\site-packages\numba\compiler.py", line 697, in 
_compile_ir
return self._compile_core()
  File "C:\Python27\lib\site-packages\numba\compiler.py", line 677, in 
_compile_core
res = pm.run(self.status)
  File "C:\Python27\lib\site-packages\numba\compiler.py", line 257, in run
raise patched_exception
LoweringError: Caused By:
Traceback (most recent call last):
  File "C:\Python27\lib\site-packages\numba\compiler.py", line 249, in run
stage()
  File "C:\Python27\lib\site-packages\numba\compiler.py", line 595, in 
stage_objectmode_backend
self._backend(lowerfn, objectmode=True)
  File "C:\Python27\lib\site-packages\numba\compiler.py", line 572, in _backend
lowered = lowerfn()
  File "C:\Python27\lib\site-packages\numba\compiler.py", line 546, in 
backend_object_mode
self.flags)
  File "C:\Python27\lib\site-packages\numba\compiler.py", line 869, in 
py_lowering_stage
lower.lower()
  File "C:\Python27\lib\site-packages\numba\lowering.py", line 125, in lower
self.lower_normal_function(self.fndesc)
  File "C:\Python27\lib\site-packages\numba\lowering.py", line 160, in 
lower_normal_function
entry_block_tail = self.lower_function_body()
  File "C:\Python27\lib\site-packages\numba\lowering.py", line 185, in 
lower_function_body
self.lower_block(block)
  File "C:\Python27\lib\site-packages\numba\lowering.py", line 200, in 
lower_block
self.lower_inst(inst)
  File "C:\Python27\lib\contextlib.py", line 35, in __exit__
self.gen.

Re: Lawrence D'Oliveiro

2016-10-02 Thread Skip Montanaro
On Sun, Oct 2, 2016 at 1:05 AM, Bob Martin  wrote:

> It's in several of the groups that I follow.


Yeah, we've been struggling with it over at mail.python.org. While incoming
mail has all sorts of spam fighting bells and whistles, messages arriving
from Usenet only has the SpamBayes filter. I'm sure I have a few more of
these messages held. I will continue to train on these messages to try and
get it to suppress them better.

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


Re: Lawrence D'Oliveiro

2016-10-02 Thread Skip Montanaro
If you have one or more of these messages saved, please forward it along to
me (in its entirety, headers and all). Attaching it as a compressed tar
file will help sneak it past other spam filters.

Thanks,

Skip


On Sun, Oct 2, 2016 at 8:00 AM, Skip Montanaro 
wrote:

>
> On Sun, Oct 2, 2016 at 1:05 AM, Bob Martin  wrote:
>
>> It's in several of the groups that I follow.
>
>
> Yeah, we've been struggling with it over at mail.python.org. While
> incoming mail has all sorts of spam fighting bells and whistles, messages
> arriving from Usenet only has the SpamBayes filter. I'm sure I have a few
> more of these messages held. I will continue to train on these messages to
> try and get it to suppress them better.
>
> Skip
>
>
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: announcing fython

2016-10-02 Thread nicolasessisbreton
**Is Fython a full-featured Python implementation that compiles to Fortran, or 
a thin wrapper around Fortran that uses Python syntax, or something in between? 

It's something in between. Fython is a programming langugage with its own 
syntax. For example

def mean:
int in n
real in x(n)
real out r
int i

for i in [1, n]:
r = x[i] + x[n-i+1] # a funky mean

The syntax is very close to Python. All variables must be typed, because this 
is the key to performance. To use this program in Python, we do

from fython import *

m = load('.mean') # assuming previous code is in file: mean.fy

n = Int(3)
x = Real(value=[1,2,3])
r = Real()

m.value(n,x,r)

print(r)

So Fython provides a wrapper around itself for Python to use.
-- 
https://mail.python.org/mailman/listinfo/python-list


Italian libel spam

2016-10-02 Thread Marko Rauhamaa
Skip Montanaro :
> Yeah, we've been struggling with it over at mail.python.org. While
> incoming mail has all sorts of spam fighting bells and whistles,
> messages arriving from Usenet only has the SpamBayes filter. I'm sure
> I have a few more of these messages held. I will continue to train on
> these messages to try and get it to suppress them better.

I'd imagine just having a list of 100 Italian words should give you a
pretty accurate spam score. Extra points for all caps.


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


Re: announcing fython

2016-10-02 Thread BartC

On 02/10/2016 14:43, [email protected] wrote:

**Is Fython a full-featured Python implementation that compiles to Fortran, or 
a thin wrapper around Fortran that uses Python syntax, or something in between?

It's something in between. Fython is a programming langugage with its own 
syntax. For example

def mean:
int in n
real in x(n)
real out r
int i

for i in [1, n]:
r = x[i] + x[n-i+1] # a funky mean


One problem with using very similar syntax for distinct languages is 
that it can get confusing.


In Python, that loop has two iterations. Here presumably it has n 
iterations.



The syntax is very close to Python. All variables must be typed, because this 
is the key to performance. To use this program in Python, we do

from fython import *

m = load('.mean') # assuming previous code is in file: mean.fy

n = Int(3)
x = Real(value=[1,2,3])
r = Real()

m.value(n,x,r)

print(r)

So Fython provides a wrapper around itself for Python to use.


How does it work in practice: does an actual Fortran compiler need to be 
invoked? (And do you need to install one, or is it all included?)


If so, at what point in the above example is it invoked? Is it every 
time you run that Python code, or will the load() used a cached copy of 
the compiled mean.fy?


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


Re: Italian libel spam

2016-10-02 Thread Skip Montanaro
On Sun, Oct 2, 2016 at 9:14 AM, Marko Rauhamaa  wrote:

> I'd imagine just having a list of 100 Italian words should give you a
> pretty accurate spam score. Extra points for all caps.
>

Thanks, but that's not how SpamBayes works. Have a look here if you're
interested:

http://www.spambayes.org/

You might find the Background link particularly interesting.

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


Re: announcing fython

2016-10-02 Thread nicolasessisbreton
>One problem with using very similar syntax for distinct languages is that it 
>can get confusing.

The first inspiration for Fython was to be close to Fortran, while improving 
the syntax. The project is in the early days, so all suggestions are welcome.
Some difference to the Python language are inevitable though, as Fython is a 
compiled language.

>does an actual Fortran compiler need to be invoked? 

Yes, this is done in the background.


>And do you need to install one, or is it all included?

A Fortran compiler must be avalaible on the machine.
A free often used compiler is gfortran.


>If so, at what point in the above example is it invoked? 
>Is it every time you run that Python code, or will the load() used a cached 
>copy of the compiled mean.fy? 

The compiler is invoked whenever the Fython sources are new or have changed.
A cached copy is used to avoid unnessecary compilation.
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Merecurial and Python-2.7.x, Python-3.Y

2016-10-02 Thread Michael Felt



On 02/10/2016 08:34, Terry Reedy wrote:

No.  By default, the working directory for a clone is for the default
branch.  Any clone can be 'updated' to any branch.  I have each share
clone updated to a different branch, so I never update to a different
branch.

--
This answers my underlying question - start for the 'base' and then 
'update' to a specific branch.


Guess I just have to experiment to see what works best for "I am 
inherently lazy".


p.s. The OS is AIX, but for hg I would not think that is a an issue.
--
https://mail.python.org/mailman/listinfo/python-list


Re: Merecurial and Python-2.7.x, Python-3.Y

2016-10-02 Thread Ned Batchelder
On Saturday, October 1, 2016 at 9:25:57 PM UTC-4, Michael Felt wrote:
> Finally, I got to where I understood what needed to be done to get both 
> Mercurial built - and the the new SSL requirements met.
> 
> So, running:
> 
> # hg clone https://hg.python.org/cpython
> 
> works. What is the next step to getting Python-2.7 AND Python-3.7 so I 
> can submit patches against both versions and/or other versions?

BTW, only very few changes are being accepted for 2.7.x these days, you 
might want to check ahead of time before putting in much work on that
branch.

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


Re: Need help for the print() function with a better order

2016-10-02 Thread Peter Pearson
On Sat, 1 Oct 2016 18:12:29 -0700 (PDT), [email protected] wrote:
> I am trying to print a simple decision tree for my homework.
> The answer must keep in this format:
>
> Top 7,4,0.95
> career gain = 100
>   1.Management 2, 3, 0.9709505944546686
>   2.Service 5, 1, 0.6500224216483541
> location gain = 100
>   1.Oregon 4, 1, 0.7219280948873623
>   2.California 3, 3, 1.0
> edu_level gain = 100
>   1.High School 5, 1, 0.6500224216483541
>   2.College 2, 3, 0.9709505944546686
> years_exp gain = 100
>   1.Less than 3 3, 1, 0.8112781244591328
>   2.3 to 10 2, 1, 0.9182958340544896
>   3.More than 10 2, 2, 1.0
>
> Here is my code:
> features={'edu_level':['High School',
 'College'],
'career':['Management',
  'Service'],
'years_exp':['Less than 3',
 '3 to 10',
 'More than 10'],
'location':['Oregon',
'California']}
>
> print('Top 7,4,0.95')
> for key in features:
> print('{} gain = {}'.format(key,100))
> attributes_list=features[key]
> kargs={}
> for i in range(len(attributes_list)):
> kargs[key]=attributes_list[i]
> low=table.count('Low',**kargs)
> high=table.count('High',**kargs)
> print('\t{}.{} {}, {}, {}'.format(
i+1,attributes_list[i],low,high,entropy(low,high)))
>
> I set all the gain as 100 now.But actually the gain must calculate
> with the data below.  For example, the career gain need the data of
> 'Management' and 'Service'.  I don't know how to do.  or Anyone can
> provide me a better logic?

I interpret your question as meaning that the value that you 
print after "gain =" should depend on features[key].  To do that,
you'll need to insert a line resembling
  gain = gain_from_features(features[key])
before the print statement.  You'll have to write the gain_from_features
function, and provide it with the numbers from which it will
compute the gain.

As a stylistic suggestion, note that Python allows you to break
your "features=" line into a more readable format, as I have done
above.

Another stylistic suggestions:
  for key, attributes_list in features.iteritems():

-- 
To email me, substitute nowhere->runbox, invalid->com.
-- 
https://mail.python.org/mailman/listinfo/python-list


working with ctypes and complex data structures

2016-10-02 Thread Michael Felt
I am trying to understand the documentation re: ctypes and interfacing 
with existing libraries.


I am reading the documentation, and also other sites that have largely 
just copied the documentation - as well as the "free chapter" at 
O'Reilly (Python Cookbook).


I am missing anything on CFields.

My test (template) is:

"""Internal ctypes definitions for AIX libperfstat"""
from sys import maxsize
from ctypes import Structure, Union
from ctypes import c_bool, c_char, c_wchar, c_byte, c_ubyte, c_short, 
c_ushort

from ctypes import c_int, c_uint, c_long, c_ulong, c_longlong, c_ulonglong
from ctypes import c_float, c_double, c_longdouble
from ctypes import c_char_p, c_wchar_p, c_void_p
from ctypes import sizeof

IDENTIFIER_LENGTH = 64

class time_t(Structure):
"""
#ifndef _TIME_T
#define _TIME_T
#ifdef  _LINUX_SOURCE_COMPAT
typedef long inttime_t;
#else   /* !_LINUX_SOURCE_COMPAT */
#ifdef __64BIT__
typedef longtime_t;
#else
typedef int time_t;
#endif
#endif  /* !_LINUX_SOURCE_COMPAT */
#endif
"""
if (maxsize > 2**32):
_fields_ = [("v", c_long)]
else:
_fields_ = [("v", c_int)]

class perfstat_cpu_total_t(Structure):
"""
typedef struct { /* global cpu information */
int ncpus;/* number of active logical processors */
int ncpus_cfg; /* number of configured processors */
char description[IDENTIFIER_LENGTH]; /* processor description 
(type/official name) */
u_longlong_t buffer1[15];   /*  several variables being skipped 
for now */

time_t lbolt;  /* number of ticks since last reboot */
u_longlong_t loadavg[3];  /* (1

Re: Merecurial and Python-2.7.x, Python-3.Y

2016-10-02 Thread Michael Felt



On 02/10/2016 19:01, Ned Batchelder wrote:

On Saturday, October 1, 2016 at 9:25:57 PM UTC-4, Michael Felt wrote:

Finally, I got to where I understood what needed to be done to get both
Mercurial built - and the the new SSL requirements met.

So, running:

# hg clone https://hg.python.org/cpython

works. What is the next step to getting Python-2.7 AND Python-3.7 so I
can submit patches against both versions and/or other versions?


BTW, only very few changes are being accepted for 2.7.x these days, you
might want to check ahead of time before putting in much work on that
branch.

Yes, I understand that - I have been working for months on something - 
that brings in - imho - something that should have been there years ago 
(AIX support for ctypes CDLL_LoadLibrary and ctypes.util.find_library)


Not new functionality - just the equivalent of what has been customized 
for Linux, Solaris, Windows and Mac/OS (aka darwin).


Hence I mentioned 2.7 and 3.7 - as my patch has been held back even for 
3.6. I know it was not, maybe even is not, perfect - but the base 
functionality has not changed since March (so I am a bit frustrated with 
the difficulties of getting anything accepted). Part of that is because 
my patch was not submitted in "hg" form. So I am trying to remove that 
obstacle.


(p.s. My email account settings do not support newsgroups - so someone 
else will need to forward it, if so desired)



--Ned.


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


Re: inplace text filter - without writing file

2016-10-02 Thread MRAB

On 2016-10-02 06:33, Sayth Renshaw wrote:

On Sunday, 2 October 2016 16:19:14 UTC+11, Sayth Renshaw  wrote:

On Sunday, 2 October 2016 12:14:43 UTC+11, MRAB  wrote:
> On 2016-10-02 01:21, Sayth Renshaw wrote:
> > Hi
> >
> > I have a fileobject which was fine however now I want to delete a line from 
the file object before yielding.
> >
> > def return_files(file_list):
> > for filename in sorted(file_list):
>
> When joining paths together, it's better to use 'os.path.join'.
>
> > with open(dir_path + filename) as fd:
> >  for fileItem in fd:
> >  yield fileItem
> >
> > Ned gave an answer over here http://stackoverflow.com/a/6985814/461887
> >
> > for i, line in enumerate(input_file):
> > if i == 0 or not line.startswith('#'):
> > output.write(line)
> >
> > which I would change because it is the first line and I want to rid 

Re: Need help for the print() function with a better order

2016-10-02 Thread 380162267qq
在 2016年10月2日星期日 UTC-4下午1:05:58,Peter Pearson写道:
> On Sat, 1 Oct 2016 18:12:29 -0700 (PDT), [email protected] wrote:
> > I am trying to print a simple decision tree for my homework.
> > The answer must keep in this format:
> >
> > Top 7,4,0.95
> > career gain = 100
> > 1.Management 2, 3, 0.9709505944546686
> > 2.Service 5, 1, 0.6500224216483541
> > location gain = 100
> > 1.Oregon 4, 1, 0.7219280948873623
> > 2.California 3, 3, 1.0
> > edu_level gain = 100
> > 1.High School 5, 1, 0.6500224216483541
> > 2.College 2, 3, 0.9709505944546686
> > years_exp gain = 100
> > 1.Less than 3 3, 1, 0.8112781244591328
> > 2.3 to 10 2, 1, 0.9182958340544896
> > 3.More than 10 2, 2, 1.0
> >
> > Here is my code:
> > features={'edu_level':['High School',
>  'College'],
> 'career':['Management',
>   'Service'],
> 'years_exp':['Less than 3',
>  '3 to 10',
>  'More than 10'],
> 'location':['Oregon',
> 'California']}
> >
> > print('Top 7,4,0.95')
> > for key in features:
> > print('{} gain = {}'.format(key,100))
> > attributes_list=features[key]
> > kargs={}
> > for i in range(len(attributes_list)):
> > kargs[key]=attributes_list[i]
> > low=table.count('Low',**kargs)
> > high=table.count('High',**kargs)
> > print('\t{}.{} {}, {}, {}'.format(
> i+1,attributes_list[i],low,high,entropy(low,high)))
> >
> > I set all the gain as 100 now.But actually the gain must calculate
> > with the data below.  For example, the career gain need the data of
> > 'Management' and 'Service'.  I don't know how to do.  or Anyone can
> > provide me a better logic?
> 
> I interpret your question as meaning that the value that you 
> print after "gain =" should depend on features[key].  To do that,
> you'll need to insert a line resembling
>   gain = gain_from_features(features[key])
> before the print statement.  You'll have to write the gain_from_features
> function, and provide it with the numbers from which it will
> compute the gain.
> 
> As a stylistic suggestion, note that Python allows you to break
> your "features=" line into a more readable format, as I have done
> above.
> 
> Another stylistic suggestions:
>   for key, attributes_list in features.iteritems():
> 
> -- 
> To email me, substitute nowhere->runbox, invalid->com.

Thank you for your suggestion.I was so tired last night printing the structure 
of decision tree crushed me.
I am not familiar with python and now I will try to modify my code.
-- 
https://mail.python.org/mailman/listinfo/python-list


scipy tutorial question

2016-10-02 Thread Xristos Xristoou
hello

i want to follow this scipy tutorial 
http://www2.geog.ucl.ac.uk/~plewis/geogg122-2011-12/dem2.html to calculate 
slope/aspect and more.

first question,this is a good way to calculate slope/aspect with scipy ?
can i find more easy way for this calculate ?

second question and most important,how can i use mine DEM (raster image) inside
the tutorial code to calculate slope/aspect from mine DEM (raster image)?
and how to export that calculation to new tiff images ?
i thing so easy but i am very new.
i have and gdal package in my python

the tutorial code :


import numpy as np

def gaussianFilter(sizex,sizey=None,scale=0.333):
'''
Generate and return a 2D Gaussian function
of dimensions (sizex,sizey)

If sizey is not set, it defaults to sizex
A scale can be defined to widen the function (default = 0.333)
'''
sizey = sizey or sizex
x, y = np.mgrid[-sizex:sizex+1, -sizey:sizey+1]
g = np.exp(-scale*(x**2/float(sizex)+y**2/float(sizey)))
return g/g.sum()

def randomDEM(nx,sizex,max,min,sizey=None,ny=None):
'''
Generate a pseudo-DEM from random correlated noise.

Parameters:
nx  :   number of pixels of the output
sizex   :   number of pixels of the smoothing filter
max :   max height in DEM
min :   min height in DEM

Options:
sizey   :   defaults to sizex
ny  :   defaults to nx

'''
from scipy import signal
ny = ny or nx
sizey = sizey or sizex
dem1 = np.random.rand(nx+2*sizex,ny+2*sizey)
demSmooth = signal.convolve(dem1,gaussianFilter(sizex,sizey),mode='valid')
demSmooth = (demSmooth - demSmooth.min())/(demSmooth.max() - 
demSmooth.min())
demSmooth = demSmooth*(max-min)+min
return demSmooth

def plot(array,file=None):
'''
Produce a plot of the 2D array

Options:
file:   specify an output file
'''
import matplotlib.pylab as plt
plt.imshow(array,interpolation='nearest')
if file == None:
plt.show()
else:
plt.savefile(file)
smoothDEM = randomDEM(300,20,100.,0.,sizey=5)
#plot(smoothDEM)
def padding(dem,size=1):
'''
Apply a border of size to a spatial dataset

Return the padded data with the original centred in the array
'''
out = np.zeros([dem.shape[0]+2*size,dem.shape[1]+2*size])
out[:,:] = np.max(dem)+1
out[size:-size,size:-size] = dem
return out

padDEM = padding(smoothDEM)
#plot(padDEM)
def localMin(dem):
'''
We wish to return the location of the minimum grid value
in a neighbourhood.

We assume the array is 2D and defined (y,x)

We return wx,wy which are the cell displacements in x and y directions.

'''
wy = np.zeros_like(dem).astype(int)
wx = np.zeros_like(dem).astype(int)
winx = np.ones([3,3])
for i in xrange(3):
winx[:,i] = i - 1
winy = winx.transpose()
demp = padding(dem,size=1)
for y in np.arange(1,demp.shape[0]-1):
for x in np.arange(1,demp.shape[1]-1):
win = demp[y-1:y+2,x-1:x+2]
ww = np.where(win == np.min(win))
whereX = winx[ww][0]
whereY = winy[ww][0]
wy[y-1,x-1] = whereY
wx[y-1,x-1] = whereX
return wx,wy

(wx,wy) = localMin(smoothDEM)
#plot(wx)


def grad2d(dem):
'''
Calculate the slope and gradient of a DEM
'''
from scipy import signal
f0 = gaussianFilter(3)
I = signal.convolve(dem,f0,mode='valid')
f1 = np.array([[-1,0,1],[-2,0,2],[-1,0,1]])
f2 = f1.transpose()
g1 = signal.convolve(I,f1,mode='valid')
g2 = signal.convolve(I,f2,mode='valid')
slope = np.sqrt(g1**2 + g2**2)
aspect = np.arctan2(g2,g1)
return slope, aspect
slope,  aspect = grad2d(smoothDEM)
plot(slope)
plot(aspect)
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: working with ctypes and complex data structures

2016-10-02 Thread eryk sun
 On Sun, Oct 2, 2016 at 5:50 PM, Michael Felt  wrote:
>
> a) where is documentation on "CField"'s?

It's undocumented. A CField is a data descriptor that accesses a
struct field with the given type, size, and offset. Like most
descriptors, it's meant to be accessed as an attribute of an instance,
not as an attribute of the type.

When accessed as an attribute of a struct type, the value returned is
the CField descriptor itself. One reason to reference the descriptor
is for its "offset" attribute, since there's no offsetof() in ctypes.
At least in this regard it should be documented.

> b) what I am not understanding - as the basic documentation shows FOO.value
> as the way to set/get the value of a _field_

You may be surprised when accessing simple-type fields such as c_int
and c_char_p. These simple types have getter and setter functions that
get called automatically whenever the type is used as a function
result, array index, or struct field. For example:

class Foo(ctypes.Structure):
_fields_ = (('v', ctypes.c_int),)

>>> foo = Foo()
>>> foo.v = 5
>>> foo.v
5

You can use a subclass to avoid the getfunc's automatic conversion. For example;

class my_int(ctypes.c_int):
pass

class Bar(ctypes.Structure):
_fields_ = (('v', my_int),)

>>> bar = Bar()
>>> bar.v = 5
>>> bar.v

>>> bar.v.value
5

> class time_t(Structure):
> ...
> if (maxsize > 2**32):
> _fields_ = [("v", c_long)]
> else:
> _fields_ = [("v", c_int)]

I'd alias the type instead of defining a struct, e.g. `time_t =
c_long`. This preserves automatic conversion of the simple type.

Also, sys.maxsize is based on the platform's size_t type. That's
generally the same size as a pointer, but C doesn't require this.
Instead use sizeof(c_void_p), which will be 8 on a 64-bit platform and
4 on a 32-bit platform.

Also, while it's unrelated to your current problem, bear in mind that
int and long are both always 32-bit on Windows. c_int64 is a
cross-platform 64-bit integer.
-- 
https://mail.python.org/mailman/listinfo/python-list


Learning python data types

2016-10-02 Thread Crane Ugly
I am not new in scripting in general.
And about a year I create scripts in python, but my approach was always based 
on my habits from UNIX shell, where data types are not obvious.
Going deeper with python I see necessity to know better data types, differences 
between them and most importantly when one data type if better then the other, 
with life examples.
Unfortunately, books I've see so far describe data types in very formal way and 
without taking into account day-to-day practical aspects.
For example in my practice data rarely hardcoded in the program code and come 
from files, streams or generated in the program. How to put them together for 
better processing?
I need some good examples of books or web articles, that can describe language 
from data processing perspective, not just academical explanations of when 
language is.

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


Re: Byte code descriptions somewhere?

2016-10-02 Thread Cem Karan
I kind of got the feeling that was so from reading the docs in the source code. 
 Too bad! :(

Cem

On Oct 1, 2016, at 7:53 PM, Paul Rubin  wrote:

> Cem Karan  writes:
>> how do I create a stream of byte codes that can be interpreted by
>> CPython directly?
> 
> Basically, study the already existing code and do something similar.
> The CPython bytecode isn't standardized like JVM bytecode.  It's
> designed for the interpreter's convenience, not officially documented,
> and (somewhat) subject to change between versions.
> -- 
> https://mail.python.org/mailman/listinfo/python-list

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


Re: Byte code descriptions somewhere?

2016-10-02 Thread Cem Karan
On Oct 1, 2016, at 7:56 PM, Chris Angelico  wrote:

> On Sun, Oct 2, 2016 at 10:47 AM, Cem Karan  wrote:
>> Cool, thank you!  Quick experimentation suggests that I don't need to worry 
>> about marking anything for garbage collection, correct?  The next question 
>> is, how do I create a stream of byte codes that can be interpreted by 
>> CPython directly?  I don't mean 'use the compile module', I mean writing my 
>> own byte array with bytes that CPython can directly interpret.
>> 
> 
> "Marking for garbage collection" in CPython is done by refcounts; the
> bytecode is at a higher level than that.
> 
 dis.dis("x = y*2")
>  1   0 LOAD_NAME0 (y)
>  3 LOAD_CONST   0 (2)
>  6 BINARY_MULTIPLY
>  7 STORE_NAME   1 (x)
> 10 LOAD_CONST   1 (None)
> 13 RETURN_VALUE
> 
> A LOAD operation will increase the refcount (a ref is on the stack),
> BINARY_MULTIPLY dereferences the multiplicands and adds a ref to the
> product, STORE will deref whatever previously was stored, etc.
> 
> To execute your own code, look at types.FunctionType and
> types.CodeType, particularly the latter's 'codestring' argument
> (stored as the co_code attribute). Be careful: you can easily crash
> CPython if you mess this stuff up :)

Ah, but crashing things is how we learn! :)  

That said, types.CodeType and types.FunctionType appear to be EXACTLY what I'm 
looking for!  Thank you!  Although I have to admit, the built-in docs for 
types.CodeType are concerning... "Create a code object.  Not for the faint of 
heart." Maybe that should be updated to "Here there be dragons"?  I'll poke 
through python's sources to get an idea of how to use codestring argument, but 
I'll probably be asking more questions on here about it.

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


Re: Byte code descriptions somewhere?

2016-10-02 Thread Cem Karan

On Oct 1, 2016, at 8:30 PM, Ned Batchelder  wrote:

> On Saturday, October 1, 2016 at 7:48:09 PM UTC-4, Cem Karan wrote:
>> Cool, thank you!  Quick experimentation suggests that I don't need to worry 
>> about marking anything for garbage collection, correct?  The next question 
>> is, how do I create a stream of byte codes that can be interpreted by 
>> CPython directly?  I don't mean 'use the compile module', I mean writing my 
>> own byte array with bytes that CPython can directly interpret.
> 
> In Python 2, you use new.code: 
> https://docs.python.org/2/library/new.html#new.code  It takes a bytestring of 
> byte codes as one of its
> twelve (!) arguments.
> 
> Something that might help (indirectly) with understanding bytecode:
> byterun (https://github.com/nedbat/byterun) is a pure-Python implementation
> of a Python bytecode VM.
> 
> --Ned.

byterun seems like the perfect project to work through to understand things.  
Thank you for pointing it out!

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


Re: Byte code descriptions somewhere?

2016-10-02 Thread Cem Karan

On Oct 1, 2016, at 7:34 PM, [email protected] wrote:

> On Saturday, October 1, 2016 at 11:57:17 PM UTC+1, Cem Karan wrote:
>> Hi all, I've all of a sudden gotten interested in the CPython interpreter, 
>> and started trying to understand how it ingests and runs byte code.  I found 
>> Include/opcode.h in the python sources, and I found some basic documentation 
>> on how to add in new opcodes online, but I haven't found the equivalent of 
>> an assembly manual like you might for x86, etc.  Is there something similar 
>> to a manual dedicated to python byte code?  Also, is there a manual for how 
>> the interpreter expects the stack, etc. to be setup so that all interactions 
>> go as expected (garbage collections works, exceptions work, etc.)?  
>> Basically, I want a manual similar to what Intel or AMD might put out for 
>> their chips so that all executables behave nicely with one another.
>> 
>> Thanks,
>> Cem Karan
> 
> Further to Ben Finney's answer this 
> https://docs.python.org/devguide/compiler.html should help.
> 
> Kindest regards.
> 
> Mark Lawrence.
> -- 
> https://mail.python.org/mailman/listinfo/python-list

Thank you!

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


Re: Byte code descriptions somewhere?

2016-10-02 Thread Chris Angelico
On Mon, Oct 3, 2016 at 9:58 AM, Cem Karan  wrote:
>> To execute your own code, look at types.FunctionType and
>> types.CodeType, particularly the latter's 'codestring' argument
>> (stored as the co_code attribute). Be careful: you can easily crash
>> CPython if you mess this stuff up :)
>
> Ah, but crashing things is how we learn! :)
>

I never said not to do it, just to be careful :)

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


PyQt5, OpenGL, where to start, minimal example code?

2016-10-02 Thread John Ladasky
Hi there,

I am making my first attempt at 3D rendering.  My system configuration:

OS : Ubuntu 16.04 64-bit
Python : 3.5.1
Qt : 5.5.1
PyQt   : 5.5.1
OpenGL : 4.5.0 (I have a modern GPU)

All software was installed from the Canonical repository.  I didn't build any 
binaries myself.

>From my reading, I have concluded that Qt, PyQt, and OpenGL are all 
>rapidly-evolving, and huge, software packages.  There may be compatibility 
>problems, and relevant examples with the right software combination may be 
>hard to find.

Two weeks of searching web pages hasn't turned up a single example which 
demonstrates PyQt5 doing something simple in 3D with OpenGL that I can study.  
My own attempt to write such a program displays my QOpenGLWidget subclass 
instance, and calls the methods I defined, but throws this exception:

ImportError: No module named 'PyQt5._QOpenGLFunctions_4_5_Compatibility'

Here's a snippet of the offending method of my QOpenGLWidget:

def initializeGL(self):
ctx = self.context()  # This works, ctx is a PyQt5.QtGui.QOpenGLContext
print(ctx.versionFunctions())  # ImportError occurs here

Searching for that ImportError, I found a page which says that error occurs 
when "you have a more capable version of OpenGL than PyQt has support for", but 
no advice is offered on how to fix that problem.

I like the idea of using PyQt5, because I'm getting pretty familiar with it, 
BUT I am open to trying any quick and clear route to 3D rendering.  I would 
appreciate the community's recommendations.  Thanks.
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: SOAP XML webserver in Python/Django

2016-10-02 Thread ivan77
Thank you for the info dieter.  

I'll take a look at your suggestions.  

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


Re: unintuitive for-loop behavior

2016-10-02 Thread Steve D'Aprano
On Sun, 2 Oct 2016 09:41 pm, Chris Angelico wrote:

> The only way to prove that something is a new binding is to
> demonstrate that, when this binding is removed, a previous one becomes
> visible. In Python, that only ever happens across namespaces, and in 
> CPython, the only way I can make it happen is with globals->builtins.

It isn't necessary for a previous binding to become visible, because there
might not be any previous binding. There's no builtin 'x', but there is a
builtin 'str'. Hence:

x = str = 1
assert x == 1 and str == 1
del x, str
assert str  # succeeds
assert x  # NameError
x = str = 2  # create new bindings, or update existing ones?

Is it our conclusion that therefore Python creates a new binding for str
but not for x? Or that the evidence for x is "inconclusive"? Either answer
is illogical.

NameError should be sufficient to prove that the binding to 'x' is gone and
therefore any binding to 'x' is semantically a new binding, not an update.



[...]
>> I'm not entirely sure that Guido would say No. I think that "fail early
>> if the local doesn't exist" as CPython does would be permitted rather
>> than mandatory. But I could be wrong.
> 
> Interesting. The real question, then, is: Are function-local names
> inherently special? I can't make anything else do this. Class
> namespaces don't shadow globals or builtins, even during construction:

Actually, they do:

py> class Example:
... str = 1
... x = str(999)
...
Traceback (most recent call last):
  File "", line 1, in 
  File "", line 3, in Example
TypeError: 'int' object is not callable


Locals are certainly special. The language definition singles out binding[1]
to a variable inside a function as the trigger to treat it as a local, so
there are distinct "lookup name which is a local" and "lookup name which is
not a local" modes. Those distinct modes go back to at least Python 1.5.

"Local lookup" (LOAD_FAST) only looks for a local, and fails fast if it
isn't found, rather than searching globals and builtins; "not a local
lookup" never looks for a local (even if one exists -- see below).


>> By the way, here's an example showing that IronPython does allowing
>> writing to locals to affect the local namespace:
>>
> def func():
>> ... locals()['x'] = 1
>> ... print(x)
>> ... if False:
>> ... x = 
>> ...
> func()
>> 1
>>
>> And *that* behaviour is most definitely allowed -- the fact that writing
>> to locals() isn't supported by CPython is most definitely an
>> implementation- specific limitation, not a feature.
> 
> Yep. Would the same have happened if you'd omitted the "if False"
> part, though - that is, if 'x' were not a local name due to
> assignment, could it *become* local through mutation of locals()?

Yes the local would have been created, but you wouldn't see it from an
ordinary lookup of name x. You would need to use

locals()['x']

to see that it exists. For x to return it, you have to convince the compiler
to use LOAD_FAST bytecode rather than LOAD_whatever.

IronPython doesn't use LOAD_* bytecodes, it compiles to whatever .Net's CLR
uses, but it follows the same semantics. Likewise Jython.

My feeling is that we're in a rather murky part of Python's behaviour here,
one where a clean name lookup model has been most definitely put aside in
favour of speed, but it isn't quite clear whether this is defined language
behaviour, reference behaviour that all implementations are required to
follow, or implementation-specific behaviour that may be varied but so far
hasn't been, at least not by any of the Big Four implementations (CPython,
PyPy, IronPython, Jython).





[1] For the purposes of deciding whether something is local or not,
unbinding (del) is considered a binding.


-- 
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: unintuitive for-loop behavior

2016-10-02 Thread Steve D'Aprano
On Sun, 2 Oct 2016 05:35 pm, Jussi Piitulainen wrote:

[...]
>> I'm sorry, I don't understand what you mean by "Python semantics" versus
>> "a translation". A translation of what? In what way is it "magical
>> semantics"? I see nothing magical in your code: it is Python code.
> 
> "Python semantics" is the semantics that Python actually uses when it
> updated the variables of a for-loop. I would like to call it "assignment
> semantics".
> 
> The second function is an executable translation of the first function
> under a different semantics. I would like to call it "binding semantics"
> to distinguish it from "assignment semantics", but that doesn't work
> when people insist that "binding" and "assignment" are the same thing in
> Python, so I called it "magical semantics" instead.

Why shouldn't people say that binding and assignment are the same thing in
Python? What's the difference?



[...]
>> Of course, it is complex, complicated, convoluted, obfuscated, hard to
>> understand, non-idiomatic Python code, but there's nothing magical in
>> it (unless you count nonlocal as magic). And it does nothing that
>> can't be done more simply: just change the tabulate inner loop
>> variable to a different name.
> 
> It was stated, in this thread, that it would have been impossible to
> make Python for-loops behave the way the corresponding Python code in my
> translations of the two nested for-loops behaves. I thought it would be
> a good idea to *show* how the "impossible" thing can be done.

I don't recall ever using the word "impossible", or seeing anyone else use
it. Obviously nothing is *literally* impossible unless it breaks the laws
of physics or requires more energy than available in the entire universe.

It goes without saying that if the Python core developers decide to change
the semantics of Python, add new features and functionality, add new
syntax, etc then such things are possible. They might be easy or they might
be hard -- in some cases *very* hard, requiring the Python VM to be rebuilt
from the ground up. But possible.

One should be careful about using the term "impossible", as well as taking
it literally.


> I could have just posted that yes, it *could* be done, there's nothing
> so special about variables and scope in Python. But I pretty much know
> that the response to *that* would have been yet another round of "You
> don't understand that Python variables are different, they are not boxes
> in fixes locations but name bindings, and no, it cannot be done."

I'm glad you know so much about what we would say before we say it.


>> def fabulate2(m, n):
>> # The simple, Pythonic, non-complicated way.
>> for i in range(m):
>> print(i, end = ': ')
>> c = 0
>> for j in range(n):
>> print(j, end = ', ' if j + 1 < n else ' : ')
>> c += 1
>> print(i, c)
> 
> It misses only the point of the exercise.

Perhaps that's because your point wasn't clear.


>>> A summary of sorts: it's possible to demonstrate the scope difference in
>>> Python code, with no box in sight; boxes are irrelevant; the relevant
>>> issue is what function and when the loop variable is associated with,
>>> explicitly or implicitly.
>>
>> I don't know what "scope difference" you think you are demonstrating.
>> tabulate() has a single scope, fabulate() has multiple scopes because it
>> has inner functions that take i as argument, making them local to the
>> inner functions. Um, yeah, of course they are different. They're
>> different because you've written them differently. What's your point?
> 
> The scope difference is the topic of this thread.
> 
> My point is that the for-loops could be translated/compiled/expanded
> into the lower-level code so that the loop variables would be in their
> own scopes.

Well why didn't you say so?

Perhaps I missed it the first time, but the point of the exercise would have
been so much more clear if only you had introduced it was an explanation
like:

"Here is a proof of concept that shows that Python could give for loops
their own scope. If this Python code [nested for-loops] were translated
mechanically by the compiler [nested while loops with iterators] then each
loop would have their own scope."


And you are absolutely right: boxes are irrelevant. I think that boxes only
came into this with Greg's argument that there's a difference
between "creating a new binding" versus "updating an existing one".


> The code might not seem so obfuscated to you if you thought of it not as
> source code but as compiled code. Except it's still Python. What word
> should I use instead of "translation"? Would "transformation" be
> understood?

That might have helped.





-- 
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: PyQt5, OpenGL, where to start, minimal example code?

2016-10-02 Thread blue
You have here a PyQt5 Reference Guide 
http://pyqt.sourceforge.net/Docs/PyQt5/index.html
Some example can be found here 4 and 5 
http://codeprogress.com/python/libraries/pyqt/

Support for OpenGL http://pyqt.sourceforge.net/Docs/PyQt5/opengl.html told us:

When compiled against Qt v5.1 or later, PyQt5 implements a set of either 
desktop QOpenGL bindings or OpenGL ES v2 bindings depending on how Qt was 
configured. This removes the dependency on any third-party OpenGL bindings such 
as PyOpenGL.

At the moment the desktop bindings are for OpenGL v2.0 and are mostly complete. 
Other versions will be added in later releases. If there are calls which you 
need, but are currently unsupported, then please ask for the support to be 
added.

Obtaining an object that implements the bindings for a particular OpenGL 
version and profile is done in the same way as it is done from C++, i.e. by 
calling versionFunctions(). In addition, the bindings object also contains 
attributes corresponding to all of the OpenGL constants.
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Learning python data types

2016-10-02 Thread Rustom Mody
On Monday, October 3, 2016 at 3:38:24 AM UTC+5:30, Crane Ugly wrote:
> I am not new in scripting in general.
> And about a year I create scripts in python, but my approach was always based 
> on my habits from UNIX shell, where data types are not obvious.
> Going deeper with python I see necessity to know better data types, 
> differences between them and most importantly when one data type if better 
> then the other, with life examples.
> Unfortunately, books I've see so far describe data types in very formal way 
> and without taking into account day-to-day practical aspects.
> For example in my practice data rarely hardcoded in the program code and come 
> from files, streams or generated in the program. How to put them together for 
> better processing?
> I need some good examples of books or web articles, that can describe 
> language from data processing perspective, not just academical explanations 
> of when language is.
> 
> Leonid

Yes this is a real need and very badly satisfied at the moment

- Language texts are language centered — mostly what you'll get out here
- Data Structure texts tend to emphasize storage structures — eg 
  ‘linked-lists’ — rather than data structures eg sets, bags, lists and their 
  complementary semantics
- And the academic texts are too academic/theoretical

Something I had written [Sorry! very much in the 3rd category!!] nearly two 
decades ago:
http://cs.unipune.ac.in/admin/obx/hod/course.pdf
particularly the Intro to programming, discrete structures and data structures
syllabi
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: PyQt5, OpenGL, where to start, minimal example code?

2016-10-02 Thread John Ladasky
On Sunday, October 2, 2016 at 7:45:50 PM UTC-7, Lawrence D’Oliveiro wrote:
> On Monday, October 3, 2016 at 2:14:13 PM UTC+13, John Ladasky wrote:
> > 
> > I am making my first attempt at 3D rendering.
> 
> Bear in mind there are two kinds of 3D rendering: realtime (with OpenGL) and 
> non-realtime.

Real time is what I want.  My object is not complex, but I need to rotate it.
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: PyQt5, OpenGL, where to start, minimal example code?

2016-10-02 Thread John Ladasky
On Sunday, October 2, 2016 at 7:21:15 PM UTC-7, blue wrote:
> You have here a PyQt5 Reference Guide 
> http://pyqt.sourceforge.net/Docs/PyQt5/index.html
> Some example can be found here 4 and 5 
> http://codeprogress.com/python/libraries/pyqt/

That's a nice page of examples, but there are no OpenGL examples.
 
> Support for OpenGL http://pyqt.sourceforge.net/Docs/PyQt5/opengl.html told us:
> 
> When compiled against Qt v5.1 or later, PyQt5 implements a set of either 
> desktop QOpenGL bindings or OpenGL ES v2 bindings depending on how Qt was 
> configured. This removes the dependency on any third-party OpenGL bindings 
> such as PyOpenGL.
> 
> At the moment the desktop bindings are for OpenGL v2.0 and are mostly 
> complete. Other versions will be added in later releases. If there are calls 
> which you need, but are currently unsupported, then please ask for the 
> support to be added.

I found that page many days ago.  I am not sure whether OpenGL 4.5, which is 
what Ubuntu installed for me, is an extension of OpenGL ES v2, or something 
completely different.

PyQt5 seems to be at least nominally aware of recent versions of OpenGL, and it 
knows that I have version 4.5 -- since my program went looking for a module 
called "QOpenGLFunctions_4_5_Compatibility".  On this page...

http://doc.qt.io/qt-5/qtgui-module.html

...the Qt5 QTGui documentation lists a plethora of functions to retrieve OpenGL 
"specifications" and "compatibility profiles", ranging from OpenGL 1.0 through 
4.5.

Note, these are not PyQt docs, they are for Qt.  I have been sometimes 
frustrated by the fact that the PyQt modules do not appear to have a one-to-one 
mapping with the hierarchy of Qt itself.

> Obtaining an object that implements the bindings for a particular OpenGL 
> version and profile is done in the same way as it is done from C++, i.e. by 
> calling versionFunctions(). In addition, the bindings object also contains 
> attributes corresponding to all of the OpenGL constants.

And as you can see: trying to call versionFunctions() is exactly where my 
program failed.
-- 
https://mail.python.org/mailman/listinfo/python-list


Locals [was Re: unintuitive for-loop behavior]

2016-10-02 Thread Steven D'Aprano
On Monday 03 October 2016 12:42, Steve D'Aprano wrote:

> Yes the local would have been created, but you wouldn't see it from an
> ordinary lookup of name x. You would need to use
> 
> locals()['x']
> 
> to see that it exists. For x to return it, you have to convince the compiler
> to use LOAD_FAST bytecode rather than LOAD_whatever.

Sorry, that first sentence is misleading. The question is, can you create local 
variables inside a function using locals()[name]? IronPython and Jython allow 
it; CPython doesn't.

But if it did, then just as in the similar situation in IronPython and Jython, 
you would still need to convince the compiler to use LOAD_FAST or equivalent in 
order to see it. Hence:


x = y = 'global'
def example():
locals()['x'] = 'local'
locals()['y'] = 'local'
if False:
# convince the compiler to treat y as a local
del y
print(x)  # => 'global' in everything
print(y)  # => 'local' in Jython/IronPython, UnboundLocalError in CPython



-- 
Steven
git gets easier once you get the basic idea that branches are homeomorphic 
endofunctors mapping submanifolds of a Hilbert space.

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


Re: Locals [was Re: unintuitive for-loop behavior]

2016-10-02 Thread Chris Angelico
On Mon, Oct 3, 2016 at 3:09 PM, Steven D'Aprano
 wrote:
> But if it did, then just as in the similar situation in IronPython and Jython,
> you would still need to convince the compiler to use LOAD_FAST or equivalent 
> in
> order to see it. Hence:
>
>
> x = y = 'global'
> def example():
> locals()['x'] = 'local'
> locals()['y'] = 'local'
> if False:
> # convince the compiler to treat y as a local
> del y
> print(x)  # => 'global' in everything
> print(y)  # => 'local' in Jython/IronPython, UnboundLocalError in CPython

Or convince it to use LOAD_NAME. They're Python 2 implementations,
right? Here's CPython at work...

Python 2.7.12+ (default, Sep  1 2016, 20:27:38)
[GCC 6.2.0 20160822] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> x = y = 'global'
>>> def example():
... locals()['x'] = 'local'
... locals()['y'] = 'local'
... if False:
... exec("")
... print(x)
... print(y)
...
>>> example()
local
local

So apparently, locals() mutations CAN affect name lookups. Happens the
same way in Jython, too.

This is quite a rabbit-hole.

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


Re: announcing fython

2016-10-02 Thread Denis Akhiyarov
On Sunday, October 2, 2016 at 10:57:57 AM UTC-5, [email protected] wrote:
> >One problem with using very similar syntax for distinct languages is that it 
> >can get confusing.
> 
> The first inspiration for Fython was to be close to Fortran, while improving 
> the syntax. The project is in the early days, so all suggestions are welcome.
> Some difference to the Python language are inevitable though, as Fython is a 
> compiled language.
> 
> >does an actual Fortran compiler need to be invoked? 
> 
> Yes, this is done in the background.
> 
> 
> >And do you need to install one, or is it all included?
> 
> A Fortran compiler must be avalaible on the machine.
> A free often used compiler is gfortran.
> 
> 
> >If so, at what point in the above example is it invoked? 
> >Is it every time you run that Python code, or will the load() used a cached 
> >copy of the compiled mean.fy? 
> 
> The compiler is invoked whenever the Fython sources are new or have changed.
> A cached copy is used to avoid unnessecary compilation.

Have you looked at f2py? You can write Fortran and then use it Python by just 
wrapping it with f2py. Actually this is a project bundled in numpy. There is 
also fortran magic for Jupyter notebooks. f90wrap extends f2py to support 
modern Fortran.

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


Re: unintuitive for-loop behavior

2016-10-02 Thread Jussi Piitulainen
Steve D'Aprano writes:

> On Sun, 2 Oct 2016 05:35 pm, Jussi Piitulainen wrote:
>
> [...]
>>> I'm sorry, I don't understand what you mean by "Python semantics" versus
>>> "a translation". A translation of what? In what way is it "magical
>>> semantics"? I see nothing magical in your code: it is Python code.
>> 
>> "Python semantics" is the semantics that Python actually uses when it
>> updated the variables of a for-loop. I would like to call it "assignment
>> semantics".
>> 
>> The second function is an executable translation of the first function
>> under a different semantics. I would like to call it "binding semantics"
>> to distinguish it from "assignment semantics", but that doesn't work
>> when people insist that "binding" and "assignment" are the same thing in
>> Python, so I called it "magical semantics" instead.
>
> Why shouldn't people say that binding and assignment are the same
> thing in Python? What's the difference?

Outside Python, (lambda x : f(x)) is said to "bind" x. It's different
from assigning a new value to x. It's similar to how quantifiers in
logic are said to bind their variables, so that variables bound to
different quantifiers are different variables.

[...]

>>> def fabulate2(m, n):
>>> # The simple, Pythonic, non-complicated way.
>>> for i in range(m):
>>> print(i, end = ': ')
>>> c = 0
>>> for j in range(n):
>>> print(j, end = ', ' if j + 1 < n else ' : ')
>>> c += 1
>>> print(i, c)
>> 
>> It misses only the point of the exercise.
>
> Perhaps that's because your point wasn't clear.

Perhaps.

 A summary of sorts: it's possible to demonstrate the scope
 difference in Python code, with no box in sight; boxes are
 irrelevant; the relevant issue is what function and when the loop
 variable is associated with, explicitly or implicitly.
>>>
>>> I don't know what "scope difference" you think you are
>>> demonstrating.  tabulate() has a single scope, fabulate() has
>>> multiple scopes because it has inner functions that take i as
>>> argument, making them local to the inner functions. Um, yeah, of
>>> course they are different. They're different because you've written
>>> them differently. What's your point?
>> 
>> The scope difference is the topic of this thread.
>> 
>> My point is that the for-loops could be translated/compiled/expanded
>> into the lower-level code so that the loop variables would be in
>> their own scopes.
>
> Well why didn't you say so?

I tried.

> Perhaps I missed it the first time, but the point of the exercise
> would have been so much more clear if only you had introduced it was
> an explanation like:
>
> "Here is a proof of concept that shows that Python could give for
> loops their own scope. If this Python code [nested for-loops] were
> translated mechanically by the compiler [nested while loops with
> iterators] then each loop would have their own scope."

Something like that but not quite that. The nonlocal declarations are
also important, but since they would not be visible in the code that the
programmer writes, it would be misleading to say that the loop has its
own scope.

In my original sketch perhaps "each loop variable is in its own scope",
and in the current version "the loop variable of each iteration step is
in its own scope"? Would those words work for you?

I would like to be able to say simply that the for-loop would "bind" its
variable, as opposed to assigning to it (and maybe update it by binding
as opposed to assignment), but this clashes with the way these terms are
used in the Python community.

> And you are absolutely right: boxes are irrelevant. I think that boxes
> only came into this with Greg's argument that there's a difference
> between "creating a new binding" versus "updating an existing one".

The frustrating thing is that Greg's posts make perfect sense to me, and
I agree with him about the difference.

How about "creating a new scope for the loop variable" versus "updating
the loop variable in the existing scope" then?

>> The code might not seem so obfuscated to you if you thought of it not
>> as source code but as compiled code. Except it's still Python. What
>> word should I use instead of "translation"? Would "transformation" be
>> understood?
>
> That might have helped.

Well. I think you understand now.
-- 
https://mail.python.org/mailman/listinfo/python-list


Create a map for data to flow through

2016-10-02 Thread Sayth Renshaw
Is there a standard library feature that allows you to define a declarative map 
or statement that defines the data and its objects to be parsed and output 
format?

Just wondering as for loops are good but when i end up 3-4 for loops deep and 
want multiple matches at each level i am finding it harder to manage. I am 
reading here https://docs.python.org/3/howto/functional.html

Is there a better way than loops on loops on loops etc?

Thinking that for loop is quick at the start but there probably is a more 
direct way which while slower may be clearer over the long run.

Cheers 

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


scipy tutorial question

2016-10-02 Thread Sayth Renshaw
I would ask on scipy mailing list as it may provide a better response.

https://www.scipy.org/scipylib/mailing-lists.html

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