Re: clever exit of nested loops
On Wednesday, September 26, 2018 at 12:50:20 AM UTC-7, [email protected] wrote: > I have "abused" the "else" clause of the loops to makes a break "broke" more > loops I did this once upon a time. In recent years, when I start writing tricky nested loops, I frequently find myself reaching for itertools.product() to flatten the loops instead. This code accomplishes the same task as yours. I'll leave it to you to decide whether you prefer it. There are things that I dislike about it, but the flow control part is clear. from itertools import product msgs = ("i: {}", "\tj: {}", "\t\tk: {}") old = 3*[None] for new in product(range(10), repeat=3): for n, (msg, changed) in enumerate(zip(msgs, [x!=y for x, y in zip(old, new)])): if changed: print(msg.format(new[n])) if condition(*new):# your condition() took three separate arguments break old = new -- https://mail.python.org/mailman/listinfo/python-list
Re: clever exit of nested loops
Am 26.09.18 um 12:28 schrieb Bart:
On 26/09/2018 10:10, Peter Otten wrote:
class Break(Exception):
pass
try:
for i in range(10):
print(f'i: {i}')
for j in range(10):
print(f'\tj: {j}')
for k in range(10):
print(f'\t\tk: {k}')
if condition(i, j, k):
raise Break
except Break:
pass
For all such 'solutions', the words 'sledgehammer' and 'nut' spring to
mind.
Remember the requirement is very simple, to 'break out of a nested loop'
(and usually this will be to break out of the outermost loop). What
you're looking is a statement which is a minor variation on 'break'.
Which is exactly what it does. "raise Break" is a minor variation on
"break".
Not
to have to exercise your imagination in devising the most convoluted
code possible.
To the contrary, I do think this solution looks not "convoluted" but
rather clear. Also, in Python some other "exceptions" are used for a
similar purpose - for example "StopIteration" to signal that an iterator
is exhausted. One might consider to call these "signals" instead of
"exceptions", because there is nothing exceptional, apart from the
control flow.
Christian
--
https://mail.python.org/mailman/listinfo/python-list
Re: clever exit of nested loops
Christian Gollwitzer wrote:
> Am 26.09.18 um 12:28 schrieb Bart:
>> On 26/09/2018 10:10, Peter Otten wrote:
>>> class Break(Exception):
>>> pass
>>>
>>> try:
>>> for i in range(10):
>>> print(f'i: {i}')
>>> for j in range(10):
>>> print(f'\tj: {j}')
>>> for k in range(10):
>>> print(f'\t\tk: {k}')
>>>
>>> if condition(i, j, k):
>>> raise Break
>>> except Break:
>>> pass
>>>
>>
>> For all such 'solutions', the words 'sledgehammer' and 'nut' spring to
>> mind.
>>
>> Remember the requirement is very simple, to 'break out of a nested loop'
>> (and usually this will be to break out of the outermost loop). What
>> you're looking is a statement which is a minor variation on 'break'.
>
> Which is exactly what it does. "raise Break" is a minor variation on
> "break".
>
>> Not
>> to have to exercise your imagination in devising the most convoluted
>> code possible.
>
> To the contrary, I do think this solution looks not "convoluted" but
> rather clear. Also, in Python some other "exceptions" are used for a
> similar purpose - for example "StopIteration" to signal that an iterator
> is exhausted. One might consider to call these "signals" instead of
> "exceptions", because there is nothing exceptional, apart from the
> control flow.
>
> Christian
>
>
I've done the same before myself (exit from nested blocks to a containing
block using exception), but it does violate the principle "Exceptions should
be used for exceptional conditions).
--
https://mail.python.org/mailman/listinfo/python-list
Re: clever exit of nested loops
On 2018-09-26 21:06, Mark Lawrence wrote: > > To me the Ned Batchelder presentation > https://www.youtube.com/watch?v=EnSu9hHGq5o "Loop like a Native" is the > definitive way on how to deal with loops in Python. > Hear, hear. Great talk. -- https://mail.python.org/mailman/listinfo/python-list
What's needed for jpegtran in Python 3?
I'm converting an existing Python2 program to Python3, it uses jpegtran and I can't find what to install to get this in Python3. Can anyone advise what I need to install in Python3? -- Chris Green · -- https://mail.python.org/mailman/listinfo/python-list
Re: What's needed for jpegtran in Python 3?
On Fri, Sep 28, 2018 at 2:51 AM Chris Green wrote: > > I'm converting an existing Python2 program to Python3, it uses > jpegtran and I can't find what to install to get this in Python3. > > Can anyone advise what I need to install in Python3? > Do you mean this? https://pypi.org/project/jpegtran-cffi/ I can't find anything called just "jpegtran" for either Py2 or Py3, but that one claims to work on 3.3+ as well as 2.6 and 2.7. ChrisA -- https://mail.python.org/mailman/listinfo/python-list
Re: What's needed for jpegtran in Python 3?
Chris Angelico wrote: > On Fri, Sep 28, 2018 at 2:51 AM Chris Green wrote: > > > > I'm converting an existing Python2 program to Python3, it uses > > jpegtran and I can't find what to install to get this in Python3. > > > > Can anyone advise what I need to install in Python3? > > > > Do you mean this? > > https://pypi.org/project/jpegtran-cffi/ > > I can't find anything called just "jpegtran" for either Py2 or Py3, > but that one claims to work on 3.3+ as well as 2.6 and 2.7. > I think that must be what I have already installed, it doesn't make the module available in Python 3, it just says this when I try and install it:- root@t470:~# pip install jpegtran-cffi Requirement already satisfied: jpegtran-cffi in /usr/local/lib/python2.7/dist-packages Requirement already satisfied: cffi>=0.8 in /usr/lib/python2.7/dist-packages (from jpegtran-cffi) root@t470:~# Python 3 isn't going to find that is it? When I run my program it says:- chris$ picimport.py Traceback (most recent call last): File "/home/chris/bin/picimport.py", line 28, in from jpegtran import JPEGImage ModuleNotFoundError: No module named 'jpegtran' chris$ -- Chris Green · -- https://mail.python.org/mailman/listinfo/python-list
Re: What's needed for jpegtran in Python 3?
On Fri, Sep 28, 2018 at 3:51 AM Chris Green wrote: > > Chris Angelico wrote: > > On Fri, Sep 28, 2018 at 2:51 AM Chris Green wrote: > > > > > > I'm converting an existing Python2 program to Python3, it uses > > > jpegtran and I can't find what to install to get this in Python3. > > > > > > Can anyone advise what I need to install in Python3? > > > > > > > Do you mean this? > > > > https://pypi.org/project/jpegtran-cffi/ > > > > I can't find anything called just "jpegtran" for either Py2 or Py3, > > but that one claims to work on 3.3+ as well as 2.6 and 2.7. > > > I think that must be what I have already installed, it doesn't make > the module available in Python 3, it just says this when I try and > install it:- > > root@t470:~# pip install jpegtran-cffi > Requirement already satisfied: jpegtran-cffi in > /usr/local/lib/python2.7/dist-packages > Requirement already satisfied: cffi>=0.8 in > /usr/lib/python2.7/dist-packages (from jpegtran-cffi) > root@t470:~# > > Python 3 isn't going to find that is it? Ah! Correct. What you need to do is use pip from your Python 3 installation. The safest way is: python3 -m pip install jpegtran-cffi but you may be able to abbreviate it to just: pip3 install jpegtran-cffi ie just change "pip" to "pip3". ChrisA -- https://mail.python.org/mailman/listinfo/python-list
Re: What's needed for jpegtran in Python 3?
On Thursday, September 27, 2018 at 10:48:16 AM UTC-7, Chris Green wrote: > I think that must be what I have already installed, it doesn't make > the module available in Python 3, it just says this when I try and > install it:- > > root@t470:~# pip install jpegtran-cffi > Requirement already satisfied: jpegtran-cffi in > /usr/local/lib/python2.7/dist-packages > Requirement already satisfied: cffi>=0.8 in > /usr/lib/python2.7/dist-packages (from jpegtran-cffi) > root@t470:~# > > Python 3 isn't going to find that is it? When I run my program it > says:- > > chris$ picimport.py > Traceback (most recent call last): > File "/home/chris/bin/picimport.py", line 28, in > from jpegtran import JPEGImage > ModuleNotFoundError: No module named 'jpegtran' > chris$ It appears that you are working in Linux. Many of the command-line Linux utilities are written in Python 2.7. In a few years that may change, for now Py 2.7 is the system Python. On a Linux system, when you type "python" you will start the system's Py 2.7 interpreter. When you type "pip" you will start the installer for Py 2.7. So you installed jpegtran, but you installed it for Py 2.7. Linux (Ubuntu, at least, I'm not sure about other distros) also ships with a version of Python 3, but it's not the default. If you want to invoke Py 3.x from a Linux command prompt, you need to type "python3". If you want to install packages for your Python 3 platform, you need to install python3-pip, a system package which is not included in (Ubuntu) Linux by default. You can access that package from the command line by typing "pip3" where you would have typed "pip". It's good that you want to use Py 3, in a few years the changeover will be complete. The one thing you did not show was your text for picimport.py, which I expect is trying to use Py 3. -- https://mail.python.org/mailman/listinfo/python-list
JPEGImage() hangs
I have a program which uses jpegtran-cffi 0.5.2 and, while it seems to
work OK most of the time it's hanging very solidly on one jpeg file
which seems to be OK when viewed with other programs.
I'm simply doing:-
img = JPEGImage(srcFullFn)
I have checked that that srcFullFn points to a real jpeg file and I
have checked that it's actually a jpeg:-
chris$ file 102_PANA/P1020466.JPG
102_PANA/P1020466.JPG: JPEG image data, Exif standard: [TIFF image
data, little-endian, direntries=15, manufacturer=Panasonic,
model=DMC-TZ60, orientation=upper-left, xresolution=214,
yresolution=222, resolutionunit=2, software=Ver.1.0 ,
datetime=2018:09:26 10:55:31], baseline, precision 8, 4896x3672,
frames 3
Even running in the Python console hangs:-
chris$ python
Python 2.7.15rc1 (default, Apr 15 2018, 21:51:34)
[GCC 7.3.0] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> from jpegtran import JPEGImage
>>> x = JPEGImage("102_PANA/P1020466.JPG")
... and that's it, CTRL/C and CTRL/Z won't break it, I have to 'kill -9' the
process
Does anyone have any idea what may be wrong?
--
Chris Green
·
--
https://mail.python.org/mailman/listinfo/python-list
Re: JPEGImage() hangs
Could you please try another tool like `convert'? E.g. $ convert 102_PANA/P1020466.JPG test.png What does that say? -- https://mail.python.org/mailman/listinfo/python-list
ipython and prompt-toolkit
For a long time I cannot update prompt-toolkit, because ipython requires a version lower as 2. That is why I still use 1.0.15 instead of 2.0.4. Any chance that ipython will be updated concerning this dependency? -- Cecil Westerhof Senior Software Engineer LinkedIn: http://www.linkedin.com/in/cecilwesterhof -- https://mail.python.org/mailman/listinfo/python-list
Which class method is being called when we declare below expression?
Hello gauys, Which list class method will call for below codes? L = [1,2,3] And L =[] Thanks, Ajay -- https://mail.python.org/mailman/listinfo/python-list
Re: Which class method is being called when we declare below expression?
On Fri, Sep 28, 2018 at 8:52 AM Ajay Patel wrote:
>
> Hello gauys,
>
> Which list class method will call for below codes?
>
> L = [1,2,3]
> And
> L =[]
None. Simple assignment does not call any methods. It just takes the
value on the right hand side and says, hey, "L", you now mean that
thing, k? k. :)
With *augmented* assignment (eg "x += 1"), there are methods that get
called, and with non-assignment operators (eg "x + 1"), similarly.
Generally the left hand side defines the behaviour. But simple
assignment is defined purely within the language, as are a handful of
other operators ("x is y", "x and y"). Keeps things simple and
predictable!
ChrisA
--
https://mail.python.org/mailman/listinfo/python-list
Re: ipython and prompt-toolkit
On 27/09/2018 07:14, Cecil Westerhof wrote: For a long time I cannot update prompt-toolkit, because ipython requires a version lower as 2. That is why I still use 1.0.15 instead of 2.0.4. Any chance that ipython will be updated concerning this dependency? Well this is an interesting coincidence! I just had a look, and it turns out that the IPython 7.0 branch uses prompt_toolkit 2.0 (the change[1] is dated 29 December 2017). IPython 7.0 was released just a few hours ago, shortly after you sent this mail! [1] https://github.com/ipython/ipython/commit/8e256bd37373f98580ba1ef1d3fcfd7976802238 -- https://mail.python.org/mailman/listinfo/python-list
Re: clever exit of nested loops
Neal Becker wrote: but it does violate the principle "Exceptions should be used for exceptional conditions). Python doesn't really go in for that philosophy. Exceptions are often used for flow control, e.g. StopIteration. -- Greg -- https://mail.python.org/mailman/listinfo/python-list
How to change '\\' to '\'
I get a string item, for example path[0], from path = os.get_exec_path() It's something like "\\Borland\\Bcc55\\Include", a Python string. I want to use this "string" in a subprocess command as a parameter. Obviously this command can only recognize "\Borland\Bcc55\Include". I know there must have an easy way to convert it, but just can't figure it out:-( --Jach --- This email has been checked for viruses by Avast antivirus software. https://www.avast.com/antivirus -- https://mail.python.org/mailman/listinfo/python-list
Re: How to change '\\' to '\'
Jach Fong wrote: I get a string item, for example path[0], from path = os.get_exec_path() It's something like "\\Borland\\Bcc55\\Include" It doesn't actually have double backslashes in it, that's just a result of how the string is being displayed. No conversion is needed. -- Greg -- https://mail.python.org/mailman/listinfo/python-list
Re: Which class method is being called when we declare below expression?
Ajay Patel writes: > L = [1,2,3] That's not an expression; it is an assignment statement. The right-hand side is an expression. It will (at the top level) create a list. To create a new instance of the 'list' type, Python will call the type's '__new__' method. This is termed the constructor for that type. The constructor returns a new instance of the type; in this case, it returns a new instance of 'list'. That object is the result of evaluating the right-hand side of the expression. The statement then assigns the reference 'L' to that object. > And > L =[] All the above description also applies to that assignment statement. -- \“If you go parachuting, and your parachute doesn't open, and | `\you friends are all watching you fall, I think a funny gag | _o__) would be to pretend you were swimming.” —Jack Handey | Ben Finney -- https://mail.python.org/mailman/listinfo/python-list
