Re: how to use pycallgraph in ubuntu and window?

2016-10-14 Thread dieter
meInvent bbird  writes:

> i install in ubunbu 14
>
> pip install graphviz
> pip install pycallgraph
>
> martin@ubuntu:~/Downloads$ pycallgraph graphviz -- ./pusher.py
> Traceback (most recent call last):
> ...
> pycallgraph.exceptions.PyCallGraphException: The command "dot" is required to 
> be in your path.

This tells you that a required (operating system level) utility
(named "dot") is not available.

Carefully read the installation instructions, especially any
remark about dependencies. This should tell you where "dot" is supposed
to come from. Install all operating system level dependencies.

If "dot" should have come from "graphviz", then maybe a
"hash" is necessary to let your shell see new commands.

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


Re: Python-based monads essay (Re: Assignment versus binding)

2016-10-14 Thread Rustom Mody
On Friday, October 14, 2016 at 10:31:36 AM UTC+5:30, Marko Rauhamaa wrote:
> Gregory Ewing :
> 
> > Marko Rauhamaa wrote:
> >> This suggests even the promoters of functional programming
> >> intuitively prefer imperative programming, but that's ok as long as
> >> it's all functional under the hood.
> >
> > You make it sound like functional programmers like functional
> > programming because it gives them a warm fuzzy feeling. I don't think
> > that's true -- there are specific reasons they like it, and those
> > reason still apply when I/O is expressed using a monadic structure.
> >
> > Read Part 2 - I have something to say about that at the end.
> 
> I've read it. This looks awfully imperative to me:


You are an assembly language programmer.
You've mastered interrupts, instruction formats, IO nitty gritties,
protection modes and all the other good stuff

Up comes this ‘structured programming' kid believing his new religion will save
the world.
You ask him about all the stuff youve spent your life on
1st answer: none of thats available
You scratch your head: So the kewlest paradigm is the poweroff button
2nd answer: Well if you must have all that there's
- separate compilation
- inline assembly
- intrinsics
- etc
- etc

You: (scratching the head harder): Every solution you are giving me makes my 
life worse.
You want to qualify as sadist? Be by guest!
Programmers messiah? Not mine. Thank you!


FP is not much more than structured programming carried to its logical limit:
http://blog.languager.org/2012/11/imperative-programming-lessons-not.html
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: how to use pycallgraph in ubuntu and window?

2016-10-14 Thread meInvent bbird
succeed to draw graph, thanks

originally i think pip install graphviz is enough

then search again

there is an win32 executable file graphviz need to be install

On Friday, October 14, 2016 at 2:58:48 PM UTC+8, dieter wrote:
> meInvent bbird  writes:
> 
> > i install in ubunbu 14
> >
> > pip install graphviz
> > pip install pycallgraph
> >
> > martin@ubuntu:~/Downloads$ pycallgraph graphviz -- ./pusher.py
> > Traceback (most recent call last):
> > ...
> > pycallgraph.exceptions.PyCallGraphException: The command "dot" is required 
> > to be in your path.
> 
> This tells you that a required (operating system level) utility
> (named "dot") is not available.
> 
> Carefully read the installation instructions, especially any
> remark about dependencies. This should tell you where "dot" is supposed
> to come from. Install all operating system level dependencies.
> 
> If "dot" should have come from "graphviz", then maybe a
> "hash" is necessary to let your shell see new commands.

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


Re: How to process syntax errors

2016-10-14 Thread Pierre-Alain Dorange
 wrote:

> Steve, You are absolutely right. I am trying to eliminate the method of
> using parenthesis while calling in my file. Especially when I call it
> from a instance.

Then write a simple interpreter.
You want to change the language syntax, as said previously, do not
except python to understand your new syntax, just write a small
interpreter to translate your new language to python.

-- 
Pierre-Alain Dorange   Moof 

Ce message est sous licence Creative Commons "by-nc-sa-2.0"

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


Re: Without compilation, how to find bugs?

2016-10-14 Thread BartC

On 14/10/2016 01:59, [email protected] wrote:

On Thursday, October 13, 2016 at 4:06:36 PM UTC-7, pozz wrote:



Are the things exactly how I understood, or do I miss something in Python?


As others have said, user a linter.


With Python you're supposed to just be able run any source code 
instantly; how will using a 'lint' tool impact that process? Or is it 
only meant to be used infrequently?



I'd go a step further and use an actual code editor or IDE that includes some 
basic static analysis.  Using this example that Skip used:

def func2(a, b):
print(a, b)

def func1(a):
print(a)

func2(1)

Any code editor worth using will highlight the ) on the last line and tell you 
that there's a missing parameter.


How can that work? I thought one of the biggest deals with Python is 
that you can re-bind function names to anything else. So:


if cond:
func2 = 38
else:
func2 = func1

Then func2(1) can either be perfectly correct, or completely erroneous!

(I have my own suspicions that functions in Python are predominantly 
used in a boring, predictable, static manner (so allowing certain 
optimisations - or error checking), but I got the impression from some 
threads here that many apparently do little else in their code but bind 
and rebind function names.)


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


Re: how to send a json of yield list

2016-10-14 Thread meInvent bbird
succeed to run,

is it the yield return the whole list 2000 * 2000 * 2000 items?

as i know that yield is return [1,1,1] etc one by one once it get

if it return 2000*2000*2000 items, why?

i have to add a queue get this yield in order to succeed

but i do not understand the situation when
using queue and yield at the same time

will the speed same as the situation that no yield?

while True:
#time.sleep(1)
if q.qsize() > 0:
print("here1")
item = q.get()
print("here2")
#item = getcombinations()
sock.send(json.dumps(item))
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: how to send a json of yield list

2016-10-14 Thread meInvent bbird
when not to use queue, it is faster now

while True:
for ii in getcombinations():
item = ii
print(item)
sock.send(json.dumps(ii))


On Friday, October 14, 2016 at 5:11:35 PM UTC+8, meInvent bbird wrote:
> succeed to run,
> 
> is it the yield return the whole list 2000 * 2000 * 2000 items?
> 
> as i know that yield is return [1,1,1] etc one by one once it get
> 
> if it return 2000*2000*2000 items, why?
> 
> i have to add a queue get this yield in order to succeed
> 
> but i do not understand the situation when
> using queue and yield at the same time
> 
> will the speed same as the situation that no yield?
> 
> while True:
> #time.sleep(1)
> if q.qsize() > 0:
> print("here1")
> item = q.get()
> print("here2")
> #item = getcombinations()
> sock.send(json.dumps(item))

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


Re: Without compilation, how to find bugs?

2016-10-14 Thread Chris Angelico
On Fri, Oct 14, 2016 at 8:04 PM, BartC  wrote:
> On 14/10/2016 01:59, [email protected] wrote:
>>
>> On Thursday, October 13, 2016 at 4:06:36 PM UTC-7, pozz wrote:
>
>
>>> Are the things exactly how I understood, or do I miss something in
>>> Python?
>>
>>
>> As others have said, user a linter.
>
>
> With Python you're supposed to just be able run any source code instantly;
> how will using a 'lint' tool impact that process? Or is it only meant to be
> used infrequently?
>

In any serious project (in any language), you should have unit tests.
Python doesn't force you to run those every time you build - nor does
C, nor does any other language. It's up to you how often you run your
tests. Linters are basically just heuristic testing systems - less
precise, but more general. Type checkers in Python are the same.

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


Re: TCL/TK as default UI toolkit, and wayland

2016-10-14 Thread kerbingamer376
Python's "standard" (and bundled on most platforms) UI tookkit is TCL/TK. 
However, this has A LOT of drawbacks:

* It's eyesore on a lot of platforms
* It's non-pythonic
* It just flat out fails on some desktop environments
* On linux it requires X, however lots of distros are now using wayland
and so on.

I think python needs a new "standard" UI toolkit.
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: TCL/TK as default UI toolkit, and wayland

2016-10-14 Thread kerbingamer376
On Friday, October 14, 2016 at 12:40:53 PM UTC+1, kerbingamer376 wrote:
> Python's "standard" (and bundled on most platforms) UI tookkit is TCL/TK. 
> However, this has A LOT of drawbacks:
> 
> * It's eyesore on a lot of platforms
> * It's non-pythonic
> * It just flat out fails on some desktop environments
> * On linux it requires X, however lots of distros are now using wayland
> and so on.
> 
> I think python needs a new "standard" UI toolkit.

Sorry for typos.
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Without compilation, how to find bugs?

2016-10-14 Thread Steve D'Aprano
On Fri, 14 Oct 2016 08:04 pm, BartC wrote:

> On 14/10/2016 01:59, [email protected] wrote:
>> On Thursday, October 13, 2016 at 4:06:36 PM UTC-7, pozz wrote:
> 
>>> Are the things exactly how I understood, or do I miss something in
>>> Python?
>>
>> As others have said, user a linter.
> 
> With Python you're supposed to just be able run any source code
> instantly; how will using a 'lint' tool impact that process? Or is it
> only meant to be used infrequently?

The process is little different between C and Python:

With C: during development, you run the compiler (which includes built-in
static analysis) or stand-alone linter, and optionally any tests you have,
etc. The deployed software rarely if ever includes static analysis.

With Python: during development, you optionally run linters, static
analysis, tests, etc. After deployment, you rarely run static tests,
linting, etc.


>> I'd go a step further and use an actual code editor or IDE that includes
>> some basic static analysis.  Using this example that Skip used:
>>
>> def func2(a, b):
>> print(a, b)
>>
>> def func1(a):
>> print(a)
>>
>> func2(1)
>>
>> Any code editor worth using will highlight the ) on the last line and
>> tell you that there's a missing parameter.

That's a matter of opinion.



> How can that work? I thought one of the biggest deals with Python is
> that you can re-bind function names to anything else. So:
> 
> if cond:
>  func2 = 38
> else:
>  func2 = func1
> 
> Then func2(1) can either be perfectly correct, or completely erroneous!

Of course. Unless the compiler can perform full-program analysis and rule
out the possibility that func2(1) has been rebound to something that will
accept a single argument, it cannot reject that code. The design principle
here is that anything which isn't provably wrong should be allowed, in case
the programmer knows better than the compiler.

A corollary of this is that most (all?) existing Python compilers are quite
simple-minded and naive. As has been pointed out, even the most obviously
wrong code:

x = 1 + "2"

is not rejected until runtime. Why not? You'd have to ask Guido for a
ruling, but I think he would say:

- he's not interested in making static analysis part of the language
specification;

- as far as the CPython reference implementation is concerned, he thinks it
is a waste of time and effort to do such static analysis in the compiler/
interpreter;

- but third-party tools (linters, IDEs, code checkers, etc) or compilers are
welcome to do so if they choose.

(I stress that I don't *know* this is what Guido will say, I'm just
guessing.)

As external tools, linters and IDEs etc aren't constrained by that strict
rule "never reject code that isn't provably wrong". They are permitted to
make a reasonable guess. They are only giving optional warnings which
indicate something which *might* be incorrect.


> (I have my own suspicions that functions in Python are predominantly
> used in a boring, predictable, static manner (so allowing certain
> optimisations - or error checking), 

Indeed. I have a vague memory of somebody actually doing a survey of some
large code base, and finding that (excluding decorators, which technically
perform a rebinding) something like 98% or more of functions and classes
were used in a boring, static manner.

But of course, that's partly a matter of convention and coding styles. Some
code bases are written in a boring, static manner because the corporate
style prohibits dynamic rebinding of functions.


> but I got the impression from some 
> threads here that many apparently do little else in their code but bind
> and rebind function names.)

Heh, everybody loves to talk about the most extreme forms of Python
dynamicism, but hardly anyone uses them often. But they are used, just
enough to rule out banning them.

You may be interested in some work being done by one of the core developers.
One of the features of Python that keeps it relatively slow is that
function calls are resolved at runtime: calling len(x) has to do a runtime
search for the name "len" before calling it. Victor Stinner is working on
run-time optimizations which can detect when it is safe to replace that
run-time search with a compile-time static call.

That's also one of the future optimizations Nuitika is looking at.

And of course PyPy does a similar thing, except as just-in-time compilation
rather than ahead-of-time.



 

-- 
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: TCL/TK as default UI toolkit, and wayland

2016-10-14 Thread Chris Warrick
On 14 October 2016 at 13:40, kerbingamer376  wrote:
> Python's "standard" (and bundled on most platforms) UI tookkit is TCL/TK. 
> However, this has A LOT of drawbacks:
>
> * It's eyesore on a lot of platforms
> * It's non-pythonic
> * It just flat out fails on some desktop environments
> * On linux it requires X, however lots of distros are now using wayland
> and so on.
>
> I think python needs a new "standard" UI toolkit.
> --
> https://mail.python.org/mailman/listinfo/python-list

Okay. What else do you suggest?

* PyQt/PySide requires massive Qt packages, and has licensing issues.
* GTK looks bad outside of GNOME.
* wxPython claims to be back to development, but it wasn’t for the past 2 years.
* Kivy doesn’t even try to feel native anywhere.

I think we’ve just run out of reasonable cross-platform GUI libraries
for Python… You are free to use any of those four, though (or anything
less cross-platform). You don’t have to use Tkinter if you don’t like
it. And it’s not a hard requirement on many Linux distributions.

-- 
Chris Warrick 
PGP: 5EAAEA16
-- 
https://mail.python.org/mailman/listinfo/python-list


Namespace for timeit

2016-10-14 Thread ROGER GRAYDON CHRISTMAN
Trying to use timeit within a function:

def test(name, sorter, size):
"""Tests and times the sorting algorithm on given array size"""
print(name,end='\t')
array = [0]*size
for i in range(size):
array[i] = randrange(20)
timeit('sorter( array, size )', number=1)

Since 'sorter' is a name local to test, it is not yet recognized by timeit,
so I know I must somehow define a new namespace for that,
but the documentation doesn't really help me with how to do that

timeit('sorter( array, size )', number=1, globals='test')

That tells me the namespace isn't supposed to be a string
test.__dict__ is just an empty dictionary

so where do I find 'sorter', 'array', and 'size'?

Roger Christman
Pennsylvania State University


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


PyDev 5.3.0 Released

2016-10-14 Thread Fabio Zadrozny
Release Highlights:
---

* **Important** PyDev now requires Java 8 and Eclipse 4.5 onwards.

* PyDev 4.5.5 is the last release supporting Java 7 and Eclipse 3.8.
* See: update sites page for the update site of older versions of PyDev.
* See: the **PyDev does not appear after install** section on the
download page for help on using a Java 8 vm in Eclipse.

* **Syntax validation for multiple grammars**

* Helps to make code which is **Python 2 and 3 compatible**.
* To customize, go to Project Properties > PyDev - Interpreter/Grammar,
and select **grammars for "additional syntax validation"**.

* **Code completion**

* The code-completion can now do substring based matches (i.e.: the
proposals will be shown if any part of the completion matches the requested
name).
* It's **still** not the default (to activate it, change the setting
**"Preferences > PyDev > Editor > Code Completion > Match substrings on
code completion?"** to true).
* Completion proposals have the part of the completion used to do the
match in bold.
* Qualifiers of the completion (i.e.: package name) are styled
differently (color may be customized in **General > Appearance > Colors and
Fonts > Basic Qualifier Information Color**).
* Completions are re-sorted when the name used to request a code
completion changes.
* **Sorting** is based on:

* The current name typed (so that matches that are exact or start
with the requested token appear first).
* The type of the completion (parameter, local, context insensitive
with auto-import, etc).
* Where the completion was found (so, matches from the same project
go first, referenced projects second and standard library last).

* **Ctrl and Shift Behavior when applying code-completion proposal**

* Ctrl is always **"replace the current name with the completion"**
for all completions.
* Pressing Ctrl to override the next name in code completion no
longer looses the highlight in the editor.
* On code completion with auto-import, for doing local imports, the
pop-up must be focused and Shift must be kept pressed while the completion
is applied.

* **PyQt5 support in Interactive Console**

* PyQt5 may now be used as a backend in the interactive console so that
widgets/plots can be inspected interactively while using the console.
* May be activated with **%matplotlib qt5** (when using IPython) or in
**"Preferences > PyDev > Interactive Console > Enable GUI event loop
integration > PyQt5"**.


What is PyDev?
---

PyDev is an open-source Python IDE on top of Eclipse for Python, Jython and
IronPython development.

It comes with goodies such as code completion, syntax highlighting, syntax
analysis, code analysis, refactor, debug, interactive console, etc.

Details on PyDev: http://pydev.org
Details on its development: http://pydev.blogspot.com


What is LiClipse?
---

LiClipse is a PyDev standalone with goodies such as support for Multiple
cursors, theming, TextMate bundles and a number of other languages such as
Django Templates, Jinja2, Kivy Language, Mako Templates, Html, Javascript,
etc.

It's also a commercial counterpart which helps supporting the development
of PyDev.

Details on LiClipse: http://www.liclipse.com/



Cheers,

--
Fabio Zadrozny
--
Software Developer

LiClipse
http://www.liclipse.com

PyDev - Python Development Environment for Eclipse
http://pydev.org
http://pydev.blogspot.com

PyVmMonitor - Python Profiler
http://www.pyvmmonitor.com/
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: TCL/TK as default UI toolkit, and wayland

2016-10-14 Thread Michael Torrie
On 10/14/2016 05:40 AM, kerbingamer376 wrote:
> Python's "standard" (and bundled on most platforms) UI tookkit is TCL/TK. 
> However, this has A LOT of drawbacks:
> 
> * It's eyesore on a lot of platforms

I thought this was largely solved in recent versions of Tcl/Tk that use
the new Tile widget set (ttk) which recent versions of Python, certainly
3.5, support.

https://docs.python.org/3/library/tkinter.ttk.html

> * It's non-pythonic

Neither is PyQt/Pyside or PyGObject (GTK+ 3), sadly.  PyGTK was rather
pythonic, but since GTK3+ bindings are produced through introspection
utilities, so things aren't nearly so pythonic.  A lot more details are
now leaking through into the python side.  Code tends to read more like
transliterated C++ code now.  But at least the bindings are easy to
generate and keep up to date for the devs.

> * It just flat out fails on some desktop environments

Not sure what you mean by this.  Programs using tkinter crash with an
un-handled exception?

On platforms where tkinter is available, I would say this statement is
flat-out false!

> * On linux it requires X, however lots of distros are now using wayland
> and so on.

I don't think this is a problem.  For one, Tcl/Tk will move to wayland
eventually anyway, and as new Python releases come out, they will tend
to track the Tcl/Tk releases.  Besides that, desktops that use Wayland
will be supporting X11 applications for a long time to come using an
integrated X server.

> I think python needs a new "standard" UI toolkit.

The xkcd comic comes to mind!

Most developers who want to do desktop apps already pick their own tools
to do it.  They make their choice based on a lot of factors that are
unique to their own circumstance.  One size does not fit all.

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


Re: Without compilation, how to find bugs?

2016-10-14 Thread breamoreboy
On Friday, October 14, 2016 at 12:06:36 AM UTC+1, pozz wrote:
> I come from the C language, that is a compiled and strongly typed 
> language.

Python is compiled and dynamically and strongly typed but C is compiled and 
statically and weakly typed.

> 
> All the tricks have a common goal: to discover bugs as soon as possible, 
> mostly during compilation process. Indeed I usually find some bugs 
> during compilation (or static analysis). It seems to me very important.

As others have all ready mentioned there are plenty of static analysis tools 
for Python.

> 
> Now I'm learning Python and it appears to me very simple, but at the 
> same time highly dangerous. For example, I can write a function that 
> accepts two arguments and call it with only one argument. I can execute 
> the script without any problem and I will not notice the bug until I 
> test exactly the erroneous line of code (the call with only one argument).

On the other hand some errors occur far less frequently in Python than in C, 
for example off-by-one, or the memory management being done for you.

> 
> However, I think the language interpreter could emit the error before 
> launching the script even without executing the wrong instruction, 
> because it perfectly knows how many arguments the function wants and 
> that one instruction calls it with a wrong number of arguments.
> 
> Are the things exactly how I understood, or do I miss something in Python?

You've missed plenty but please stick with it, you'll learn :)

Kindest regards.

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


Different behaviour of regexp in 3.6.0b2

2016-10-14 Thread Lele Gaifax
Hi all,

trying out pgcli with Python 3.6.0b2 I got an error related to what seem a
different behaviour, or even a bug, of re.sub().

The original intent is to replace spaces within a string with the regular 
expression
\s+ (see 
https://github.com/dbcli/pgcli/blob/master/pgcli/packages/prioritization.py#L11,
ignore the fact that the re.sub() call seem underoptimal).

With Python 3.5.2 is straightforward:

  $ python3.5
  Python 3.5.2+ (default, Sep 22 2016, 12:18:14) 
  [GCC 6.2.0 20160927] on linux
  Type "help", "copyright", "credits" or "license" for more information.
  >>> import re
  >>> re.sub(r'\s+', r'\s+', 'foo bar')
  'foo\\s+bar'

While Python 3.6.0b2 gives:

  $ python3.6
  Python 3.6.0b2+ (default, Oct 11 2016, 08:30:05) 
  [GCC 6.2.0 20160927] on linux
  Type "help", "copyright", "credits" or "license" for more information.
  >>> import re
  >>> re.sub(r'\s+', r'\s+', 'foo bar')
  Traceback (most recent call last):
File "/usr/local/python3.6/lib/python3.6/sre_parse.py", line 945, in 
parse_template
  this = chr(ESCAPES[this][1])
  KeyError: '\\s'

  During handling of the above exception, another exception occurred:

  Traceback (most recent call last):
File "", line 1, in 
File "/usr/local/python3.6/lib/python3.6/re.py", line 191, in sub
  return _compile(pattern, flags).sub(repl, string, count)
File "/usr/local/python3.6/lib/python3.6/re.py", line 326, in _subx
  template = _compile_repl(template, pattern)
File "/usr/local/python3.6/lib/python3.6/re.py", line 317, in _compile_repl
  return sre_parse.parse_template(repl, pattern)
File "/usr/local/python3.6/lib/python3.6/sre_parse.py", line 948, in 
parse_template
  raise s.error('bad escape %s' % this, len(this))
  sre_constants.error: bad escape \s at position 0

Accordingly to the documentation 
(https://docs.python.org/3.6/library/re.html#re.sub) 
“unknown escapes [in the repl argument] such as \& are left alone”.

Am I missing something, or is this a regression?

In the meantime, I will alert the pgcli people.

Thanks in advance,
bye, lele.
-- 
nickname: Lele Gaifax | Quando vivrò di quello che ho pensato ieri
real: Emanuele Gaifas | comincerò ad aver paura di chi mi copia.
[email protected]  | -- Fortunato Depero, 1929.

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


Re: Different behaviour of regexp in 3.6.0b2

2016-10-14 Thread Chris Angelico
On Sat, Oct 15, 2016 at 2:40 AM, Lele Gaifax  wrote:
> Accordingly to the documentation 
> (https://docs.python.org/3.6/library/re.html#re.sub)
> “unknown escapes [in the repl argument] such as \& are left alone”.
>
> Am I missing something, or is this a regression?

Further down, you'll find this note:

Changed in version 3.6: Unknown escapes consisting of '\' and an ASCII
letter now are errors.

There's a shift as of 3.6 to make unrecognized alphabetic escapes into
errors, or at least warnings.

rosuav@sikorsky:~$ python3 -Wall
Python 3.7.0a0 (default:a78446a65b1d+, Sep 29 2016, 02:01:55)
[GCC 6.1.1 20160802] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> "C:\Documents\my_user"
sys:1: DeprecationWarning: invalid escape sequence '\D'
sys:1: DeprecationWarning: invalid escape sequence '\m'
'C:\\Documents\\my_user'
>>>
rosuav@sikorsky:~$ python3.5 -Wall
Python 3.5.2+ (default, Sep 22 2016, 12:18:14)
[GCC 6.2.0 20160914] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> "C:\Documents\my_user"
'C:\\Documents\\my_user'
>>>

I wasn't specifically aware that the re module was doing the same
thing, but it'll be from the same purpose and goal. The idea is that,
for instance, Windows path names in non-raw string literals will no
longer behave differently based on whether the path is "my_user" or
"the_other_user". Definite improvement.

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


Re: Different behaviour of regexp in 3.6.0b2

2016-10-14 Thread Peter Otten
Lele Gaifax wrote:

> Hi all,
> 
> trying out pgcli with Python 3.6.0b2 I got an error related to what seem a
> different behaviour, or even a bug, of re.sub().
> 
> The original intent is to replace spaces within a string with the regular
> expression \s+ (see
> 
https://github.com/dbcli/pgcli/blob/master/pgcli/packages/prioritization.py#L11,
> ignore the fact that the re.sub() call seem underoptimal).
> 
> With Python 3.5.2 is straightforward:
> 
>   $ python3.5
>   Python 3.5.2+ (default, Sep 22 2016, 12:18:14)
>   [GCC 6.2.0 20160927] on linux
>   Type "help", "copyright", "credits" or "license" for more information.
>   >>> import re
>   >>> re.sub(r'\s+', r'\s+', 'foo bar')
>   'foo\\s+bar'
> 
> While Python 3.6.0b2 gives:
> 
>   $ python3.6
>   Python 3.6.0b2+ (default, Oct 11 2016, 08:30:05)
>   [GCC 6.2.0 20160927] on linux
>   Type "help", "copyright", "credits" or "license" for more information.
>   >>> import re
>   >>> re.sub(r'\s+', r'\s+', 'foo bar')
>   Traceback (most recent call last):
> File "/usr/local/python3.6/lib/python3.6/sre_parse.py", line 945, in
> parse_template
>   this = chr(ESCAPES[this][1])
>   KeyError: '\\s'
> 
>   During handling of the above exception, another exception occurred:
> 
>   Traceback (most recent call last):
> File "", line 1, in 
> File "/usr/local/python3.6/lib/python3.6/re.py", line 191, in sub
>   return _compile(pattern, flags).sub(repl, string, count)
> File "/usr/local/python3.6/lib/python3.6/re.py", line 326, in _subx
>   template = _compile_repl(template, pattern)
> File "/usr/local/python3.6/lib/python3.6/re.py", line 317, in
> _compile_repl
>   return sre_parse.parse_template(repl, pattern)
> File "/usr/local/python3.6/lib/python3.6/sre_parse.py", line 948, in
> parse_template
>   raise s.error('bad escape %s' % this, len(this))
>   sre_constants.error: bad escape \s at position 0
> 
> Accordingly to the documentation
> (https://docs.python.org/3.6/library/re.html#re.sub) “unknown escapes [in
> the repl argument] such as \& are left alone”.
> 
> Am I missing something, or is this a regression?

According to

https://docs.python.org/dev/library/re.html#re.sub

rejection of \s is intentional

"""
Changed in version 3.6: Unknown escapes consisting of '\' and an ASCII 
letter now are errors.
"""

though IMHO the traceback needs a cleanup.

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


Re: Different behaviour of regexp in 3.6.0b2

2016-10-14 Thread Lele Gaifax
Peter Otten <[email protected]> writes:

> Lele Gaifax wrote:
>
>> The original intent is to replace spaces within a string with the regular
>> expression \s+ (see
>> ...
>> Accordingly to the documentation
>> (https://docs.python.org/3.6/library/re.html#re.sub) “unknown escapes [in
>> the repl argument] such as \& are left alone”.

> According to
>
> https://docs.python.org/dev/library/re.html#re.sub
>
> rejection of \s is intentional
>
> """
> Changed in version 3.6: Unknown escapes consisting of '\' and an ASCII 
> letter now are errors.
> """

So, how am I supposed to achieve the mentioned intent? By doubling the escape
in the replacement?

> though IMHO the traceback needs a cleanup.

And the documentation as well, to clarify the fact immediately, without
assuming one will scroll down to the "changed in version" part (at least, that
is what seem the rule in other parts of the manual).

Thank you,
ciao, lele.
-- 
nickname: Lele Gaifax | Quando vivrò di quello che ho pensato ieri
real: Emanuele Gaifas | comincerò ad aver paura di chi mi copia.
[email protected]  | -- Fortunato Depero, 1929.

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


Re: Different behaviour of regexp in 3.6.0b2

2016-10-14 Thread Lele Gaifax
Chris Angelico  writes:

> There's a shift as of 3.6 to make unrecognized alphabetic escapes into
> errors, or at least warnings.

But we are talking about raw strings here, specifically r'\s+'.

I agree that with plain strings it's a plus.

ciao, lele.
-- 
nickname: Lele Gaifax | Quando vivrò di quello che ho pensato ieri
real: Emanuele Gaifas | comincerò ad aver paura di chi mi copia.
[email protected]  | -- Fortunato Depero, 1929.

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


Re: Different behaviour of regexp in 3.6.0b2

2016-10-14 Thread Lele Gaifax
Lele Gaifax  writes:

> And the documentation as well, to clarify the fact immediately, without
> assuming one will scroll down to the "changed in version" part (at least, that
> is what seem the rule in other parts of the manual).

Also, I'd prefer the "Changed in 3.6" be less ambiguous whether it refers to
the `pattern` or to the `repl` argument, or to both.

ciao, lele.
-- 
nickname: Lele Gaifax | Quando vivrò di quello che ho pensato ieri
real: Emanuele Gaifas | comincerò ad aver paura di chi mi copia.
[email protected]  | -- Fortunato Depero, 1929.

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


Re: Different behaviour of regexp in 3.6.0b2

2016-10-14 Thread Chris Angelico
On Sat, Oct 15, 2016 at 3:45 AM, Lele Gaifax  wrote:
> Chris Angelico  writes:
>
>> There's a shift as of 3.6 to make unrecognized alphabetic escapes into
>> errors, or at least warnings.
>
> But we are talking about raw strings here, specifically r'\s+'.
>
> I agree that with plain strings it's a plus.

Right; the main change is for non-raw string literals, but it looks
like the same change was made to regular expressions at the same time.
IMO that's a good thing - the rule is simply "starting with 3.6, you
should avoid \Z for any upper- or lower-case Z that doesn't have a
documented meaning".

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


Re: Different behaviour of regexp in 3.6.0b2

2016-10-14 Thread Peter Otten
Lele Gaifax wrote:

> Peter Otten <[email protected]> writes:
> 
>> Lele Gaifax wrote:
>>
>>> The original intent is to replace spaces within a string with the
>>> regular expression \s+ (see
>>> ...
>>> Accordingly to the documentation
>>> (https://docs.python.org/3.6/library/re.html#re.sub) “unknown escapes
>>> [in the repl argument] such as \& are left alone”.
> 
>> According to
>>
>> https://docs.python.org/dev/library/re.html#re.sub
>>
>> rejection of \s is intentional
>>
>> """
>> Changed in version 3.6: Unknown escapes consisting of '\' and an ASCII
>> letter now are errors.
>> """
> 
> So, how am I supposed to achieve the mentioned intent? By doubling the
> escape in the replacement?

If there are no escape sequences aimed to be handled by re.sub() you can 
escape the replacement wholesale:

>>> re.sub(r'\s+', re.escape(r'\s+'), 'foo bar')
'foo\\s\\+bar'

OK, that probably escaped too much. Second attempt:

>>> re.sub(r'\s+', lambda m: r'\s+', 'foo bar')
'foo\\s+bar'

Better? If that's too much work at runtime:

>>> def double_bs(s): return "".join(s.split("\\"))
... 
>>> re.sub(r'\s+', double_bs(r'\s+'), 'foo bar')
'foo\\s+bar'

>> though IMHO the traceback needs a cleanup.
> 
> And the documentation as well, to clarify the fact immediately, without
> assuming one will scroll down to the "changed in version" part (at least,
> that is what seem the rule in other parts of the manual).
> 
> Thank you,
> ciao, lele.


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


Re: Different behaviour of regexp in 3.6.0b2

2016-10-14 Thread Ned Batchelder
On Friday, October 14, 2016 at 12:50:44 PM UTC-4, Lele Gaifax wrote:
> Chris Angelico  writes:
> 
> > There's a shift as of 3.6 to make unrecognized alphabetic escapes into
> > errors, or at least warnings.
> 
> But we are talking about raw strings here, specifically r'\s+'.
> 
> I agree that with plain strings it's a plus.

The raw string means the regex engine gets three characters: backslash,
s, plus.  It then has to decide what backslash-s means. In 3.6, this is
an error.  You'll need to escape the backslash for the regex engine:

>>> re.sub(r'\s+', r'\\s+', 'foo bar')
'foo\\s+bar'

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


Re: Different behaviour of regexp in 3.6.0b2

2016-10-14 Thread Ned Batchelder
On Friday, October 14, 2016 at 1:00:12 PM UTC-4, Chris Angelico wrote:
> On Sat, Oct 15, 2016 at 3:45 AM, Lele Gaifax  wrote:
> > Chris Angelico  writes:
> >
> >> There's a shift as of 3.6 to make unrecognized alphabetic escapes into
> >> errors, or at least warnings.
> >
> > But we are talking about raw strings here, specifically r'\s+'.
> >
> > I agree that with plain strings it's a plus.
> 
> Right; the main change is for non-raw string literals, but it looks
> like the same change was made to regular expressions at the same time.
> IMO that's a good thing - the rule is simply "starting with 3.6, you
> should avoid \Z for any upper- or lower-case Z that doesn't have a
> documented meaning".

There doesn't seem to be a change to string literals at all. It's only a
change in the regex engine.

Python 3.6.0b2 (default, Oct 10 2016, 21:30:05)
[GCC 4.2.1 Compatible Apple LLVM 7.0.2 (clang-700.1.81)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> "\s"
'\\s'

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


Re: Different behaviour of regexp in 3.6.0b2

2016-10-14 Thread Chris Angelico
On Sat, Oct 15, 2016 at 4:12 AM, Ned Batchelder  wrote:
> There doesn't seem to be a change to string literals at all. It's only a
> change in the regex engine.
>
> Python 3.6.0b2 (default, Oct 10 2016, 21:30:05)
> [GCC 4.2.1 Compatible Apple LLVM 7.0.2 (clang-700.1.81)] on darwin
> Type "help", "copyright", "credits" or "license" for more information.
> >>> "\s"
> '\\s'

Try with -Wall. To avoid breaking every novice Windows program ever
written (bar two or three), it's only a warning for now.

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


Re: Different behaviour of regexp in 3.6.0b2

2016-10-14 Thread Lele Gaifax
Ned Batchelder  writes:

> On Friday, October 14, 2016 at 12:50:44 PM UTC-4, Lele Gaifax wrote:
>> Chris Angelico  writes:
>> 
>> > There's a shift as of 3.6 to make unrecognized alphabetic escapes into
>> > errors, or at least warnings.
>> 
>> But we are talking about raw strings here, specifically r'\s+'.
>> 
>> I agree that with plain strings it's a plus.
>
> The raw string means the regex engine gets three characters: backslash,
> s, plus.  It then has to decide what backslash-s means. In 3.6, this is
> an error.  You'll need to escape the backslash for the regex engine:
>
> >>> re.sub(r'\s+', r'\\s+', 'foo bar')
> 'foo\\s+bar'

Thanks for the clarification.

I tested the above syntax and works flawlessly on 2.7, 3.5 and 3.6b2, and I
will therefore suggest it on https://github.com/dbcli/pgcli/issues/595.

bye, lele.
-- 
nickname: Lele Gaifax | Quando vivrò di quello che ho pensato ieri
real: Emanuele Gaifas | comincerò ad aver paura di chi mi copia.
[email protected]  | -- Fortunato Depero, 1929.

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


Re: Different behaviour of regexp in 3.6.0b2

2016-10-14 Thread Ned Batchelder
On Friday, October 14, 2016 at 1:27:09 PM UTC-4, Chris Angelico wrote:
> On Sat, Oct 15, 2016 at 4:12 AM, Ned Batchelder  
> wrote:
> > There doesn't seem to be a change to string literals at all. It's only a
> > change in the regex engine.
> >
> > Python 3.6.0b2 (default, Oct 10 2016, 21:30:05)
> > [GCC 4.2.1 Compatible Apple LLVM 7.0.2 (clang-700.1.81)] on darwin
> > Type "help", "copyright", "credits" or "license" for more information.
> > >>> "\s"
> > '\\s'
> 
> Try with -Wall. To avoid breaking every novice Windows program ever
> written (bar two or three), it's only a warning for now.

I see. I'm not sure how novice users will know to enable warnings that are
off by default.  Do people regularly run their code with -Wall? I never have,
and I don't know how I would have seen these warnings.

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


Re: Different behaviour of regexp in 3.6.0b2

2016-10-14 Thread Chris Angelico
On Sat, Oct 15, 2016 at 4:48 AM, Ned Batchelder  wrote:
> On Friday, October 14, 2016 at 1:27:09 PM UTC-4, Chris Angelico wrote:
>> On Sat, Oct 15, 2016 at 4:12 AM, Ned Batchelder  
>> wrote:
>> > There doesn't seem to be a change to string literals at all. It's only a
>> > change in the regex engine.
>> >
>> > Python 3.6.0b2 (default, Oct 10 2016, 21:30:05)
>> > [GCC 4.2.1 Compatible Apple LLVM 7.0.2 (clang-700.1.81)] on darwin
>> > Type "help", "copyright", "credits" or "license" for more information.
>> > >>> "\s"
>> > '\\s'
>>
>> Try with -Wall. To avoid breaking every novice Windows program ever
>> written (bar two or three), it's only a warning for now.
>
> I see. I'm not sure how novice users will know to enable warnings that are
> off by default.  Do people regularly run their code with -Wall? I never have,
> and I don't know how I would have seen these warnings.

Tools like ipython can choose to enable warnings by default. I'm not
sure if they do or not, but it'd be a good thing.

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


Re: cannot import name pyhop

2016-10-14 Thread Irmen de Jong
On 14-10-2016 7:31, Gipper wrote:
> I'm trying to run a script that calls pyhop (from pyhop import pyhop).  
> Details here > https://docs.extrahop.com/5.0/extrahop-python-api/#metrics
> 
> I've followed the directions to install and import pyhop, but no matter what 
> I do, I always get the following error when running a script that calls pyhop 
> >
> 
> Traceback (most recent call last):
>   File "Migration.py", line 1, in 
> from pyhop import pyhop
> ImportError: cannot import name pyhop
> 
> Any ideas where I'm going wrong?  
> 

What does:

import pyhop
print(pyhop.__file__)

tell you?

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


Re: Namespace for timeit

2016-10-14 Thread Skip Montanaro
> timeit('sorter( array, size )', number=1)

I'm not a timeit whiz, but I'm kind of surprised there isn't a call form
where it goes something like

timeit(callable, *args, ...)

There is a globals keyword argument though. I think you could probably call
it with globals=locals() in your case.

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


trying to make a simple program help please :)

2016-10-14 Thread LongHairLuke
Hi, l l am trying to make a simple guess program. This is my script: 

def main():
print ("Guess a letter between a and e")
randomNumber = b

userGuess = input("Your guess: ")

if userGuess == randomNumber:
 print("You got it")
else:
 print ("That's not it")

main()


When l run the program l get error: userGuess is not defined, but l defined 
userGuess at line 5. I don't get it someone plese help :) 

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


Re: trying to make a simple program help please :)

2016-10-14 Thread MRAB

On 2016-10-14 19:11, LongHairLuke wrote:

Hi, l l am trying to make a simple guess program. This is my script:

def main():
print ("Guess a letter between a and e")
randomNumber = b

userGuess = input("Your guess: ")

if userGuess == randomNumber:
 print("You got it")
else:
 print ("That's not it")

main()


When l run the program l get error: userGuess is not defined, but l defined 
userGuess at line 5. I don't get it someone plese help :)

Lines 7 to 10 should be indented like the preceding lines. The way 
you've written them, they're outside 'main'.


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


Re: trying to make a simple program help please :)

2016-10-14 Thread Irmen de Jong
On 14-10-2016 20:11, LongHairLuke wrote:
> Hi, l l am trying to make a simple guess program. This is my script: 
> 
> def main():
> print ("Guess a letter between a and e")
> randomNumber = b
> 
> userGuess = input("Your guess: ")
> 
> if userGuess == randomNumber:
>  print("You got it")
> else:
>  print ("That's not it")
> 
> main()
> 
> 
> When l run the program l get error: userGuess is not defined, but l defined 
> userGuess at line 5. I don't get it someone plese help :) 
> 
> Lukas Alberts 
> 

The indentation of the if/else statement in your program is off.  Fix it by 
properly
indenting the if-else to be at the same indentation level as the other lines 
that should
be part of the main() function.


Irmen

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


Re: trying to make a simple program help please :)

2016-10-14 Thread LongHairLuke
Den fredag 14 oktober 2016 kl. 20:30:20 UTC+2 skrev MRAB:
> On 2016-10-14 19:11, LongHairLuke wrote:
> > Hi, l l am trying to make a simple guess program. This is my script:
> >
> > def main():
> > print ("Guess a letter between a and e")
> > randomNumber = b
> >
> > userGuess = input("Your guess: ")
> >
> > if userGuess == randomNumber:
> >  print("You got it")
> > else:
> >  print ("That's not it")
> >
> > main()
> >
> >
> > When l run the program l get error: userGuess is not defined, but l defined 
> > userGuess at line 5. I don't get it someone plese help :)
> >
> Lines 7 to 10 should be indented like the preceding lines. The way 
> you've written them, they're outside 'main'.

indented? can you explain :) 
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: trying to make a simple program help please :)

2016-10-14 Thread Irmen de Jong
Right, another troll.

plonk


Irmen

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


Re: trying to make a simple program help please :)

2016-10-14 Thread BartC

On 14/10/2016 19:53, LongHairLuke wrote:

Den fredag 14 oktober 2016 kl. 20:30:20 UTC+2 skrev MRAB:

On 2016-10-14 19:11, LongHairLuke wrote:

Hi, l l am trying to make a simple guess program. This is my script:

def main():
print ("Guess a letter between a and e")
randomNumber = b

userGuess = input("Your guess: ")

if userGuess == randomNumber:
 print("You got it")
else:
 print ("That's not it")

main()


When l run the program l get error: userGuess is not defined, but l defined 
userGuess at line 5. I don't get it someone plese help :)


Lines 7 to 10 should be indented like the preceding lines. The way
you've written them, they're outside 'main'.


indented? can you explain :)



def main():
print ("Guess a letter between a and e")
randomNumber = b

userGuess = input("Your guess: ")

if userGuess == randomNumber:
print("You got it")
else:
print ("That's not it")

main()

(Python doesn't have explicit block-end delimiters. The end of a block 
(of the one compromising the body of function main in this case) has to 
be inferred from encountering some statement at the same or lower indent 
level than the opening 'def'. Or the end of the file.)


Anyway, the above won't completely fix the program as you haven't 
defined b, although you may have intended "b", even though you've called 
the variable 'randomNUMBER'.


With "b" in place of b, the program works on Python 3, although it 
doesn't verify bad inputs.


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


How to sort this without 'cmp=' in python 3?

2016-10-14 Thread 380162267qq
nums=['3','30','34','32','9','5']
I need to sort the list in order to get the largest number string: '953433230'

nums.sort(cmp=lambda a,b: cmp(a+b, b+a), reverse=True)

But how to do this in python 3?

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


Re: Without compilation, how to find bugs?

2016-10-14 Thread sohcahtoa82
On Friday, October 14, 2016 at 2:05:01 AM UTC-7, BartC wrote:
> On 14/10/2016 01:59, [email protected] wrote:
> > On Thursday, October 13, 2016 at 4:06:36 PM UTC-7, pozz wrote:
> 
> >> Are the things exactly how I understood, or do I miss something in Python?
> >
> > As others have said, user a linter.
> 
> With Python you're supposed to just be able run any source code 
> instantly; how will using a 'lint' tool impact that process? Or is it 
> only meant to be used infrequently?

I'm not the person to ask about lint usage in Python.  I don't use a linter.  I 
just use the static analysis built into my IDE.

> 
> > I'd go a step further and use an actual code editor or IDE that includes 
> > some basic static analysis.  Using this example that Skip used:
> >
> > def func2(a, b):
> > print(a, b)
> >
> > def func1(a):
> > print(a)
> >
> > func2(1)
> >
> > Any code editor worth using will highlight the ) on the last line and tell 
> > you that there's a missing parameter.
> 
> How can that work? I thought one of the biggest deals with Python is 
> that you can re-bind function names to anything else. So:
> 
> if cond:
>  func2 = 38
> else:
>  func2 = func1
> 
> Then func2(1) can either be perfectly correct, or completely erroneous!
> 
> (I have my own suspicions that functions in Python are predominantly 
> used in a boring, predictable, static manner (so allowing certain 
> optimisations - or error checking), but I got the impression from some 
> threads here that many apparently do little else in their code but bind 
> and rebind function names.)
> 
> -- 
> Bartc

If you don't know if a variable is an integer or a function, then you've got 
other problems.  That just doesn't look like clean code.

And yes, I know, Python doesn't have "variables", but rather objects bound to 
names, or names bound to objects, or however you want to say it, the idea is 
similar.

Back on track, in the case you mentioned, my IDE does not flag any errors.  
Even if I do this:

def func(y):
return y

x = 0
if x:
f = 1
else:
f = func

f(0)

My IDE highlights the f(0) line and says that f is not callable.  Obviously 
it's wrong, but that just shows that my IDE's static analysis has its limits.

(For the record, my IDE is PyCharm 2.7.3, which I know is HORRENDOUSLY out of 
data, but it's what I'm stick with at work for now)

Anyways...if at any point in the execution of a program there's uncertainty 
whether a name could be bound to an integer OR a function, that seems like a 
code smell, especially when it's name looks like a function.
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Without compilation, how to find bugs?

2016-10-14 Thread sohcahtoa82
On Friday, October 14, 2016 at 5:46:14 AM UTC-7, Steve D'Aprano wrote:
> On Fri, 14 Oct 2016 08:04 pm, BartC wrote:
> 
> > On 14/10/2016 01:59, [email protected] wrote:
> >> On Thursday, October 13, 2016 at 4:06:36 PM UTC-7, pozz wrote:
> > 
> >>> Are the things exactly how I understood, or do I miss something in
> >>> Python?
> >>
> >> As others have said, user a linter.
> > 
> > With Python you're supposed to just be able run any source code
> > instantly; how will using a 'lint' tool impact that process? Or is it
> > only meant to be used infrequently?
> 
> The process is little different between C and Python:
> 
> With C: during development, you run the compiler (which includes built-in
> static analysis) or stand-alone linter, and optionally any tests you have,
> etc. The deployed software rarely if ever includes static analysis.
> 
> With Python: during development, you optionally run linters, static
> analysis, tests, etc. After deployment, you rarely run static tests,
> linting, etc.
> 
> 
> >> I'd go a step further and use an actual code editor or IDE that includes
> >> some basic static analysis.  Using this example that Skip used:
> >>
> >> def func2(a, b):
> >> print(a, b)
> >>
> >> def func1(a):
> >> print(a)
> >>
> >> func2(1)
> >>
> >> Any code editor worth using will highlight the ) on the last line and
> >> tell you that there's a missing parameter.
> 
> That's a matter of opinion.
> 

Fair enough.  vi/vim is a popular editor for writing code, but I personally 
can't stand them.

Of course, that's another subject entirely.

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


Re: How to sort this without 'cmp=' in python 3?

2016-10-14 Thread Robin Koch

Am 15.10.2016 um 01:33 schrieb [email protected]:

nums=['3','30','34','32','9','5']
I need to sort the list in order to get the largest number string: '953433230'

nums.sort(cmp=lambda a,b: cmp(a+b, b+a), reverse=True)

But how to do this in python 3?


https://docs.python.org/3/library/functools.html#functools.cmp_to_key

| Transform an old-style comparison function to a key function.

--
Robin Koch



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


Re: How to sort this without 'cmp=' in python 3?

2016-10-14 Thread sohcahtoa82
On Friday, October 14, 2016 at 4:35:08 PM UTC-7, [email protected] wrote:
> nums=['3','30','34','32','9','5']
> I need to sort the list in order to get the largest number string: '953433230'
> 
> nums.sort(cmp=lambda a,b: cmp(a+b, b+a), reverse=True)
> 
> But how to do this in python 3?
> 
> Thank you

You don't need a lambda in this case.

Sort the strings in reverse order:
nums.sort(reverse=True)

Create a string:
biggestNum = ''.join(nums)

Or in a single line, which doesn't change the original value of nums:
biggestNum = ''.join(sorted(nums, reverse=True))

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


Re: How to sort this without 'cmp=' in python 3?

2016-10-14 Thread Ben Finney
[email protected] writes:

> nums=['3','30','34','32','9','5']
> I need to sort the list in order to get the largest number string: '953433230'
> nums.sort(cmp=lambda a,b: cmp(a+b, b+a), reverse=True)

For demonstration, I'll re-write this such that the names and output
make more sense::

$ python2
Python 2.7.12+ (default, Sep  1 2016, 20:27:38)
[GCC 6.2.0 20160927] on linux2
Type "help", "copyright", "credits" or "license" for more information.

>>> digits = ['3','30','34','32','9','5']
>>> sorted(
... digits,
... cmp=(lambda a, b: cmp(a+b, b+a)),
... reverse=True)
['9', '5', '34', '3', '32', '30']

> But how to do this in python 3?

The Python 3 sorting functions take a ‘key’ parameter:

`key` specifies a function of one argument that is used to extract a
comparison key from each list element: `key=str.lower`. The default
value is `None` (compare the elements directly).

https://docs.python.org/3/library/functions.html#sorted>

The `functools.cmp_to_key` helper is designed to help you transition to
that style:

functools.cmp_to_key(func)

Transform an old-style comparison function to a key function. Used
with tools that accept key functions (such as sorted(), min(),
max(), heapq.nlargest(), heapq.nsmallest(), itertools.groupby()).
This function is primarily used as a transition tool for programs
being converted from Python 2 which supported the use of comparison
functions.

A comparison function is any callable that accept two arguments,
compares them, and returns a negative number for less-than, zero for
equality, or a positive number for greater-than. A key function is a
callable that accepts one argument and returns another value to be
used as the sort key.

https://docs.python.org/3/library/functools.html#functools.cmp_to_key>

This works in the latest Python 2 and Python 3.

>>> import functools
>>> digits = ['3','30','34','32','9','5']
>>> sorted(
... digits,
... key=functools.cmp_to_key(lambda a, b: cmp(a+b, b+a)),
... reverse=True)
['9', '5', '34', '3', '32', '30']

The trick is done by creating a key function that takes an item for
comparison, and returns a custom object, which knows how to compare
itself as specified by your comparison function.

>>> key_func = functools.cmp_to_key(lambda a, b: cmp(a+b, b+a))
>>> key_func("32")

>>> key_func("32") < key_func("5")
True

See the Sorting HOWTO https://docs.python.org/3/howto/sorting.html>
for this and other tricks.

-- 
 \  “I used to be an airline pilot. I got fired because I kept |
  `\   locking the keys in the plane. They caught me on an 80 foot |
_o__)stepladder with a coathanger.” —Steven Wright |
Ben Finney

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


Re: How to sort this without 'cmp=' in python 3?

2016-10-14 Thread Ned Batchelder
On Friday, October 14, 2016 at 7:49:33 PM UTC-4, Robin Koch wrote:
> Am 15.10.2016 um 01:33 schrieb [email protected]:
> > nums=['3','30','34','32','9','5']
> > I need to sort the list in order to get the largest number string: 
> > '953433230'
> >
> > nums.sort(cmp=lambda a,b: cmp(a+b, b+a), reverse=True)
> >
> > But how to do this in python 3?
> 
> https://docs.python.org/3/library/functools.html#functools.cmp_to_key
> 
> | Transform an old-style comparison function to a key function.

cmp_to_key is a neat magic trick of a function, because it's seemingly
doing the impossible: how can a two-argument function be turned into 
a one-argument function?

Studying the code turns up a few clever Python tricks.

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


Re: How to sort this without 'cmp=' in python 3?

2016-10-14 Thread breamoreboy
On Saturday, October 15, 2016 at 12:53:48 AM UTC+1, [email protected] wrote:
> On Friday, October 14, 2016 at 4:35:08 PM UTC-7, [email protected] wrote:
> > nums=['3','30','34','32','9','5']
> > I need to sort the list in order to get the largest number string: 
> > '953433230'
> > 
> > nums.sort(cmp=lambda a,b: cmp(a+b, b+a), reverse=True)
> > 
> > But how to do this in python 3?
> > 
> > Thank you
> 
> You don't need a lambda in this case.
> 
> Sort the strings in reverse order:
> nums.sort(reverse=True)
> 
> Create a string:
> biggestNum = ''.join(nums)
> 
> Or in a single line, which doesn't change the original value of nums:
> biggestNum = ''.join(sorted(nums, reverse=True))

No, you've made exactly the same mistake that I did :(

>>> nums=['3','30','34','32','9','5']
>>> wants='953433230'
>>> nums.sort(reverse=True)
>>> result = ''.join(nums)
>>> result == wants
False
>>> result
'953432303'
-- 
https://mail.python.org/mailman/listinfo/python-list