Re: What's an elegant way to test for list index existing?

2018-09-29 Thread Glen D souza
i have a approach, it may not be best

fld = [ ]
for data in shlex.split(ln):
   fld.append(data)



On Sat, 29 Sep 2018 at 07:52,  wrote:

> On Friday, September 28, 2018 at 11:03:17 AM UTC-7, Chris Green wrote:
> > I have a list created by:-
> >
> > fld = shlex.split(ln)
> >
> > It may contain 3, 4 or 5 entries according to data read into ln.
> > What's the neatest way of setting the fourth and fifth entries to an
> > empty string if they don't (yet) exist? Using 'if len(fld) < 4:' feels
> > clumsy somehow.
>
> How about this?
>
> from itertools import chain, repeat
> temp = shlex.split(ln)
> fld = list(chain(temp, repeat("", 5-len(temp
> --
> https://mail.python.org/mailman/listinfo/python-list
>
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: What's an elegant way to test for list index existing?

2018-09-29 Thread Glen D souza
fld = [ ]
data = shlex.split(ln)
for item in data:
   fld.append(item)
fld = fld + [0] * (5 - len(data))


On Sat, 29 Sep 2018 at 11:03, Glen D souza  wrote:

> i have a approach, it may not be best
>
> fld = [ ]
> for data in shlex.split(ln):
>fld.append(data)
>
>
>
> On Sat, 29 Sep 2018 at 07:52,  wrote:
>
>> On Friday, September 28, 2018 at 11:03:17 AM UTC-7, Chris Green wrote:
>> > I have a list created by:-
>> >
>> > fld = shlex.split(ln)
>> >
>> > It may contain 3, 4 or 5 entries according to data read into ln.
>> > What's the neatest way of setting the fourth and fifth entries to an
>> > empty string if they don't (yet) exist? Using 'if len(fld) < 4:' feels
>> > clumsy somehow.
>>
>> How about this?
>>
>> from itertools import chain, repeat
>> temp = shlex.split(ln)
>> fld = list(chain(temp, repeat("", 5-len(temp
>> --
>> https://mail.python.org/mailman/listinfo/python-list
>>
>
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: What's an elegant way to test for list index existing?

2018-09-29 Thread Chris Angelico
On Sat, Sep 29, 2018 at 12:21 PM Chris Green  wrote:
>
> I have a list created by:-
>
> fld = shlex.split(ln)
>
> It may contain 3, 4 or 5 entries according to data read into ln.
> What's the neatest way of setting the fourth and fifth entries to an
> empty string if they don't (yet) exist? Using 'if len(fld) < 4:' feels
> clumsy somehow.

shlex.split(ln) + ["", ""]

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


Re: What's an elegant way to test for list index existing?

2018-09-29 Thread Alister via Python-list
On Fri, 28 Sep 2018 19:00:29 +0100, Chris Green wrote:

> I have a list created by:-
> 
> fld = shlex.split(ln)
> 
> It may contain 3, 4 or 5 entries according to data read into ln. What's
> the neatest way of setting the fourth and fifth entries to an empty
> string if they don't (yet) exist? Using 'if len(fld) < 4:' feels clumsy
> somehow.

how about simply
adding 2 or more null entries to the list then slicing?

string = "a b c"
a=string.split()
(a+['',''])[0:5]







-- 
QOTD:
If you're looking for trouble, I can offer you a wide selection.
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: What's an elegant way to test for list index existing?

2018-09-29 Thread Dan Sommers

On 9/28/18 2:00 PM, Chris Green wrote:

I have a list created by:-

 fld = shlex.split(ln)

It may contain 3, 4 or 5 entries according to data read into ln.
What's the neatest way of setting the fourth and fifth entries to an
empty string if they don't (yet) exist? Using 'if len(fld) < 4:' feels
clumsy somehow.


Do you care whether there are more than 5 entries in the list?  If not,
then just add two empty strings to the end of the list:

fld.extend(["", ""])

If fld already contained 5 entries, then the extra two empty strings may
or may not affect the subsequent logic.  If possible extra entries
bother you, then truncate the list:

fld = (fld + ["", ""])[:5]

Or add empty strings as long as the list contains 5 entries:

while len(fld) < 5:
fld.append("")

Which one is "better" or "best"?  Your call, depending on what your
criteria are.  I think the last one expresses the intent the most
clearly, but YMMV.
--
https://mail.python.org/mailman/listinfo/python-list


Re: I am not able to run Python in Powershell

2018-09-29 Thread Calvin Spealman
Did you actually confirm the PATH variable contains the right path?

echo $env:Path

And look for a path entry that mentions Python. Then, make sure you can
actually find python.exe in that location.

As long as you keep the PATH option checked with the Python installer it
absolutely should work from PowerShell. What version of Python did you
install? Have you also tried to invoke Python from the Command Prompt to
determine if the issue only affects PowerShell or not?

On Fri, Sep 28, 2018 at 1:10 PM  wrote:

> On Friday, 1 September 2017 19:37:41 UTC+1, The Cat Gamer  wrote:
> > fter I installed Python I try to open it in Powershell, by typing
> > python/python.exe.
> > It gives me an error:
> > python : The term 'python' is not recognized as the name of a cmdlet,
> > function, script file, or operable program. Check the spelling of the
> name,
> > or if a path was included, verify that the path is correct and try again.
> > At line:1 char:1
> > + python
> > + ~~
> > + CategoryInfo  : ObjectNotFound: (python:String) [],
> > CommandNotFoundException
> > + FullyQualifiedErrorId : CommandNotFoundException
> > This happens with version 3 and version 2. The newest versions and the
> > older versions none of them makes me able to open Python in Windows
> > Powershell. Are you guys aware of a fix?
> >
> > (I already tried the environment fix and that didnt work as well)
>
> --
> https://mail.python.org/mailman/listinfo/python-list
>
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: What's an elegant way to test for list index existing?

2018-09-29 Thread Peter Otten
Ben Finney wrote:

> Ben Finney  writes:
> 
>> You can use a comprehension, iterating over the full range of index you
>> want::
>>
>> words = shlex.split(line)
>> padding_length = 5
>> words_padded = [
>> (words[index] if index < len(words))
>> for index in range(padding_length)]
> 
> That omits the important case you were concerned with: when `index <
> len(words)` is false. In other words, that example fails to actually pad
> the resulting list.

It would if it weren't a syntax error. 

No harm done ;)

> Try this instead::
> 
> words = shlex.split(line)
> padding_length = 5
> padding_value = None
> words_padded = [
> (words[index] if index < len(words) else padding_value)
> for index in range(padding_length)]
> 


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


Re: What's an elegant way to test for list index existing?

2018-09-29 Thread Ben Finney
Ben Finney  writes:

> You can use a comprehension, iterating over the full range of index you
> want::
>
> words = shlex.split(line)
> padding_length = 5
> words_padded = [
> (words[index] if index < len(words))
> for index in range(padding_length)]

That omits the important case you were concerned with: when `index <
len(words)` is false. In other words, that example fails to actually pad
the resulting list.

Try this instead::

words = shlex.split(line)
padding_length = 5
padding_value = None
words_padded = [
(words[index] if index < len(words) else padding_value)
for index in range(padding_length)]

-- 
 \   “When a well-packaged web of lies has been sold to the masses |
  `\over generations, the truth will seem utterly preposterous and |
_o__)its speaker a raving lunatic.” —Dresden James |
Ben Finney

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


Re: JPEGImage() hangs

2018-09-29 Thread Chris Green
Cameron Simpson  wrote:
> On 28Sep2018 20:12, Chris Green  wrote:
> >Peter Pearson  wrote:
> >> On Fri, 28 Sep 2018 15:01:41 +0100, Chris Green  wrote:
> >> > Chris Green  wrote:
> >> >> Brian Oney  wrote:
> >> >> > Could you please try another tool like `convert'? E.g.
> >> >> >
> >> >> > $ convert 102_PANA/P1020466.JPG test.png
> >> >> >
> >> >> > What does that say?
> >> >>
> >> >> Well, after having returned home with the laptop where this was
> >> >> failing and doing exactly the same thing again, it now works.  However
> >> >> it did take several seconds before the >>> prompt appeared.
> >> >>
> >> >> The problem seems to be intermittent as I'm calling the function while
> >> >> importing images from a camera SD card and, sometimes, the import
> >> >> hangs but most times it works OK.
> 
> Can you separate the conversion from the copy? Copy the images off, run 
> convert 
> against the copies? That would give you more info as to whether it was the 
> copy 
> (implying an issue with the SD card as Peter suggests) or some pathologial 
> image data (eg spinning out convert).
> 
> Also, you can strace the hanging process; I'm a big fan of this for 
> diagnostic 
> purposes. A "hanging" process will normally be either spinning (using lots of 
> CPU, or a mix of CPU and OS calls), or blocked (using no CPU at all while it 
> waits for a OS call to complete). If it is blocked doing a read() then you 
> immediately suspect the device from which the read is taking place.
> 
It's blocked, I watched using top and it's using no CPU.  So,
definitely points at a dodgy SD card.

-- 
Chris Green
·
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: What's an elegant way to test for list index existing?

2018-09-29 Thread Chris Green
Thanks all, several possible ways of doing it there.

-- 
Chris Green
·
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: What's an elegant way to test for list index existing?

2018-09-29 Thread Peter Otten
jlada...@itu.edu wrote:

> On Friday, September 28, 2018 at 11:03:17 AM UTC-7, Chris Green wrote:
>> I have a list created by:-
>> 
>> fld = shlex.split(ln)
>> 
>> It may contain 3, 4 or 5 entries according to data read into ln.
>> What's the neatest way of setting the fourth and fifth entries to an
>> empty string if they don't (yet) exist? Using 'if len(fld) < 4:' feels
>> clumsy somehow.
> 
> How about this?
> 
> from itertools import chain, repeat
> temp = shlex.split(ln)
> fld = list(chain(temp, repeat("", 5-len(temp

If you are OK with silently dropping extra entries

fld = list(islice(chain(shlex.split(ln), repeat("")), 5))

or its non-itertools equivalent

fld = (shlex.split(ln) + [""] * 5)[:5]

are also possible. Personally I consider none of these to be elegant.
If you are going to unpack the fld entries I'd do it in a function with 
default values:

>>> def use_args(foo, bar, baz, ham="", spam=""):
... print("\n".join("{} = {!r}".format(*p) for p in locals().items()))
... 
>>> use_args(*shlex.split("a b c"))
spam = ''
ham = ''
baz = 'c'
foo = 'a'
bar = 'b'
>>> use_args(*shlex.split("a b c d"))
spam = ''
ham = 'd'
baz = 'c'
foo = 'a'
bar = 'b'

This has the advantage that it fails for the unforeseen cases:

>>> use_args(*shlex.split("a b c d e f"))
Traceback (most recent call last):
  File "", line 1, in 
TypeError: use_args() takes from 3 to 5 positional arguments but 6 were 
given
>>> use_args(*shlex.split("a b"))
Traceback (most recent call last):
  File "", line 1, in 
TypeError: use_args() missing 1 required positional argument: 'baz'


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