Re: Subject: problem activating python

2022-12-17 Thread Michael Torrie
On 12/17/22 15:45, Anne wrote:
>I tried several times to install and use python for youtube views with Tor
>using Youtube tutorials but I keep getting error after error. Please help
>me.
>regards Dimpho

Given the lack of any information in your post, I can only assume you're
trying to get Python installed on Windows.  Please read this page and
post here if you have any questions:
https://docs.python.org/3/using/windows.html

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


Re: Single line if statement with a continue

2022-12-17 Thread Chris Angelico
On Sun, 18 Dec 2022 at 10:59,  wrote:
>
> If a compiler or interpreter HAPPILY (as happy as machines/code get) compiles 
> or interprets your code without errors every time you use it a certain way, 
> then it is not wrong to use it. Of course if it subject to change or already 
> deprecated, ...
>

Source code is, first and foremost, for programmers to read. You're
confusing it with binary executables.

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


Re: Single line if statement with a continue

2022-12-17 Thread Thomas Passin

>if 5 > 3: a = a * 3
>  b = b * 3

That would be a fairly weird construction, neither one thing nor 
another.  But still, if you really want it that way, this is legal Python:


a = 2; b = 10
if 5 > 3: a = a * 3;\
  b = b * 3
print(a, b)  # 6 30

On 12/17/2022 6:57 PM, avi.e.gr...@gmail.com wrote:

I happen to be of two schools here.

Is something sort of taboo when using something like a computer language to 
write a program? What if another language tells you to do it a different way or 
sort of the opposite? Is it based on the details of the language and 
implementation or the prejudices of the one trying to make rules?

If a compiler or interpreter HAPPILY (as happy as machines/code get) compiles 
or interprets your code without errors every time you use it a certain way, 
then it is not wrong to use it. Of course if it subject to change or already 
deprecated, ...


That's not the point of using some consistent style. IMO, source code 
should be clear, compact, and easy for someone else to understand.  That 
someone might be you six months from now.


These objectives do not always align.  Consistency helps reduce mental 
effort by using constructions and formatting in a familiar way - 
basically, using a familiar programming idiom.  Compactness can help 
clarity, unless the code is too terse which can become a hindrance.  But 
too much verbosity can get in the way of grasping the essential processing.


Personal taste and familiarity also factor into assessing clarity and 
compactness.


It's always a balancing act.  Style guides can help by providing good 
basic idioms. There's no law** that says you *have* to follow them 
exactly all the time.  But it's helpful when you can.  If your own style 
guide i is similar to one used widely, so much the better.


**Except at some organizations


If people around you complain they do not like it, then the word "taboo" does 
apply but you can feel free to re-educate them or move on.

This reminds me too much of people who are taught some grammar such as some part of a sentence requiring a 
"noun-phrase" and later it explains that a non-phrase can be EITHER a noun accompanied by an assortment of 
other words that together form a phrase, or just a "noun" or just a "pronoun" or just a 
"nothing but an implied pronoun".

So which is it? The answer is all of them are legal, at least within bounds. A sentence like "Come over 
here" is an implied "You come over here" and works best if earlier sentences have laid some 
context on what is being talked about so the "you" is obvious or will be explained later but 
perhaps should be discouraged in other circumstances.

So back to computer languages. Many languages using grouping with something like 
"{...}" often do not care where you break your lines albeit some get touchy 
about an else statement placed improperly. It is perfectly legal to write:

If (condition) { first; second; third }

The grammar basically states that a "statement" or similar name can be a simple 
statement or a compound statement and anywhere one can go, within reason, so can the 
other.

Python has a twist here in that they discourage or outlaw some such things as 
they use mainly indentation rather than braces. This makes it hard to use 
multiple lines when the first line is way at the end as the others do not line 
up. It becomes all or ONE. I mean it allows a simple expression on the same 
line after the colon and then terminates the construct so a future indented 
line is seen as an indentation error. An experiment shows the following attempt 
to line up a second line way over below the first also fails:

if 5 > 3: a = a * 3
   b = b * 3

In a constant width font my second line is indented so "b is just below "a" and it fails because 
the interpreter does not measure the beginning column of the first line as being where the " a = a * 3" 
starts but at the "if" and that makes reasonable sense. I could imagine another design but since it is 
not what is done, the multi-line version MUST be done only on subsequent lines indented properly and identically.

So it is not really right or wrong to do one-liners. It is legal and often more 
readable. But if you ever want to extend your lines to be multi-line, it 
probably is best to use the multi-line approach regularly.

Still, using many editors, you will rapidly notice something is wrong when adding a line 
of code and seeing it is indented under the "if".

Is anyone really thrilled to read code in other languages that span so many 
lines:

If (condition)
   {
   var1 = 0
   }
else
   {
   var1 = 1
   }

It is a great way to get your line-of-code count up but the many briefer 
versions can be easier to read in one glance up to and including a bit murkier 
ones like

var1 =  (condition) ? 0 : 1

My view is that simple things that fit easily on a screen, and also held all at 
once in my mind, should be written fairly concisely. I do admit how much I can 
hold at once varies 

RE: Single line if statement with a continue

2022-12-17 Thread avi.e.gross
I happen to be of two schools here.

Is something sort of taboo when using something like a computer language to 
write a program? What if another language tells you to do it a different way or 
sort of the opposite? Is it based on the details of the language and 
implementation or the prejudices of the one trying to make rules?

If a compiler or interpreter HAPPILY (as happy as machines/code get) compiles 
or interprets your code without errors every time you use it a certain way, 
then it is not wrong to use it. Of course if it subject to change or already 
deprecated, ...

If people around you complain they do not like it, then the word "taboo" does 
apply but you can feel free to re-educate them or move on.

This reminds me too much of people who are taught some grammar such as some 
part of a sentence requiring a "noun-phrase" and later it explains that a 
non-phrase can be EITHER a noun accompanied by an assortment of other words 
that together form a phrase, or just a "noun" or just a "pronoun" or just a 
"nothing but an implied pronoun".

So which is it? The answer is all of them are legal, at least within bounds. A 
sentence like "Come over here" is an implied "You come over here" and works 
best if earlier sentences have laid some context on what is being talked about 
so the "you" is obvious or will be explained later but perhaps should be 
discouraged in other circumstances.

So back to computer languages. Many languages using grouping with something 
like "{...}" often do not care where you break your lines albeit some get 
touchy about an else statement placed improperly. It is perfectly legal to 
write:

If (condition) { first; second; third }

The grammar basically states that a "statement" or similar name can be a simple 
statement or a compound statement and anywhere one can go, within reason, so 
can the other.

Python has a twist here in that they discourage or outlaw some such things as 
they use mainly indentation rather than braces. This makes it hard to use 
multiple lines when the first line is way at the end as the others do not line 
up. It becomes all or ONE. I mean it allows a simple expression on the same 
line after the colon and then terminates the construct so a future indented 
line is seen as an indentation error. An experiment shows the following attempt 
to line up a second line way over below the first also fails:

if 5 > 3: a = a * 3
  b = b * 3

In a constant width font my second line is indented so "b is just below "a" and 
it fails because the interpreter does not measure the beginning column of the 
first line as being where the " a = a * 3" starts but at the "if" and that 
makes reasonable sense. I could imagine another design but since it is not what 
is done, the multi-line version MUST be done only on subsequent lines indented 
properly and identically.

So it is not really right or wrong to do one-liners. It is legal and often more 
readable. But if you ever want to extend your lines to be multi-line, it 
probably is best to use the multi-line approach regularly.

Still, using many editors, you will rapidly notice something is wrong when 
adding a line of code and seeing it is indented under the "if".

Is anyone really thrilled to read code in other languages that span so many 
lines:

If (condition)
  {
  var1 = 0
  }
else
  {
  var1 = 1
  }

It is a great way to get your line-of-code count up but the many briefer 
versions can be easier to read in one glance up to and including a bit murkier 
ones like

var1 =  (condition) ? 0 : 1

My view is that simple things that fit easily on a screen, and also held all at 
once in my mind, should be written fairly concisely. I do admit how much I can 
hold at once varies based on how well I can concentrate at that moment and have 
met people whose short-term memory for many things is larger or smaller than 
mine. But generally people can handle simple constructs like we are discussing.

Complicated things and ones that might need changes later should be written 
very cautiously and in detail including judicious use of comments around and 
within the code OR consider re-planning it into a less complicated form that 
calls on other fairly simple functions that can each  be decently understood 
based on naming and usage.

If you have a complicated calculation that eventually assigns values to a1, a2, 
a3, a4 then a language like Python makes a
One liner easy as in:

If (condition): 
  a1, a2, a3, a4 = func(args)

But now that four or more lines have been collapsed into one, maybe something 
like this works too and maybe is a tad more readable with parentheses:

If (condition): (a1, a2, a3, a4) = func(args)


I am not suggesting using something silly like this though:

if(1): (a, b, c, d) = (min(1,2), (1+2)/2, (1*2*2*3)/4, max(1,2))

That is so way beyond a one liner that it is best seen as multiple lines. It 
may be legal but is best used to obfuscate!

The problem with some RULES is that not only are they not 

Re: Subject: problem activating python

2022-12-17 Thread Mats Wichmann

On 12/17/22 15:45, Anne wrote:

I tried several times to install and use python for youtube views with Tor
using Youtube tutorials but I keep getting error after error. Please help
me.
regards Dimpho


Folks around here tend to be pretty helpful.  But only if you describe 
the problem in a way they can help with.  It's not clear what you're 
trying to do, and "I keep getting error after error" doesn't give anyone 
any information to work with.




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


RE: String to Float, without introducing errors

2022-12-17 Thread avi.e.gross
As often seems to happen, someone asks something that may not be fully clear 
and others chime in and extend the question.

Was the original question how to read in a ingle column of numbers from a file 
that are all numeric and NOT integers and be able to use them?

If so, the answer was quite trivial using the conversion function of your 
choice. Handling errors or what to do with something like a blank or NA are 
nice ideas if asked about.

My answer would be to ask if this was an assignment where they are EXPECTED to 
do things a certain way to master a concept, or part of a serious attempt to 
get things done.

For the latter case, it may make plenty of sense considering a single column of 
text as just a special case for the kind of multi-column files often read in 
from formatted data files and use some functionality from add-on modules like 
numpy or pandas that also allow you to deal with many other concerns.

Note such utilities also often make a decent guess on what data type a column 
should be turned into and it is possible they may occasionally decide based on 
the data that the contents all HAPPEN to be integer or there is at least one 
that makes it choose character. So any such use may well require the subsequent 
use of functions that check the dtype and, if needed, do an explicit conversion 
to what you really really really want.



-Original Message-
From: Python-list  On 
Behalf Of Mats Wichmann
Sent: Saturday, December 17, 2022 1:42 PM
To: python-list@python.org
Subject: Re: String to Float, without introducing errors

On 12/17/22 07:15, Thomas Passin wrote:
> You have strings, and you want to end up with numbers.  The numbers 
> are not integers.  Other responders have gone directly to whether you 
> should use float or decimal as the conversion, but that is a secondary matter.
> 
> If you have integers, convert with
> 
> integer = int(number_string)
>> -64550.727

they pretty clearly aren't integers:

>> -64511.489
>> -64393.637
>> -64196.763
>> -63920.2
>> -63563.037
>> -63124.156
>> -62602.254
>> -61995.895
>> -61303.548
>> -60523.651
>> -59654.66
--
https://mail.python.org/mailman/listinfo/python-list

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


Re: String to Float, without introducing errors

2022-12-17 Thread Chris Angelico
On Sun, 18 Dec 2022 at 09:46, Stefan Ram  wrote:
>
> Grant Edwards  writes:
> >Yes, fixed point (or decimal) is a better fit for what he's doing. but
> >I suspect that floating point would be a better fit for the problem
> >he's trying to solve.
>
>   I'd like to predict that within the next ten posts in this
>   thread someone will mention "What Every Computer Scientist
>   Should Know About Floating-Point Arithmetic".
>
> |>>> 0.1 + 0.2 - 0.3
> |5.551115123125783e-17
>

Looks like someone just did.

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


Subject: problem activating python

2022-12-17 Thread Anne
   I tried several times to install and use python for youtube views with Tor
   using Youtube tutorials but I keep getting error after error. Please help
   me.
   regards Dimpho

    

    

    

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


Re: Single line if statement with a continue

2022-12-17 Thread dn

On 16/12/2022 02.30, Rob Cliffe via Python-list wrote:

On 15/12/2022 04:35, Chris Angelico wrote:
On Thu, 15 Dec 2022 at 14:41, Aaron P  
wrote:

I occasionally run across something like:

for idx, thing in enumerate(things):
 if idx == 103:
 continue
 do_something_with(thing)

It seems more succinct and cleaner to use:

if idx == 103: continue.


Nothing at all wrong with writing that on a single line. If you have
issues with Flake8 not accepting your choices, reconfigure Flake8 :)

ChrisA
I'm so glad that Chris and others say this.  It (i.e. if plus 
break/continue/return on a single line) is something I have quite often 
done in my own code, albeit with a feeling of guilt that I was breaking 
a Python taboo.  Now I will do it with a clear conscience. 


Anxiety doesn't help anyone - least of all you.

Remember another Python mantra: "we're all adults here"!

(also (compulsory interjection) PEP-008 is not a set of rules for all 
Python code - see PEP-008)


--
Regards,
=dn
--
https://mail.python.org/mailman/listinfo/python-list


Re: String to Float, without introducing errors

2022-12-17 Thread dn

On 18/12/2022 01.39, Peter J. Holzer wrote:

On 2022-12-17 12:51:17 +0100, Paul St George wrote:

I have a large/long array of numbers in an external file. The numbers
look like this:

-64550.727
-64511.489
-64393.637

[...]


When I bring the numbers into my code, they are Strings. To use the
numbers in my code, I want to change the Strings to Float type because
the code will not work with Strings but I do not want to change the
numbers in any other way.



s = "-64550.727"
f = float(s)
f

-64550.727

type(f)



(Contrary to the other people posting in this thread I don't think float
is the wrong type for the job. It might be, but you haven't given enough
details to tell whether the inevitable rounding error matters or not. In
my experience in almost all cases where people think it matters it
really doesn't.)


Agreed: (ultimately) insufficient information-provided.
(but that probably doesn't matter either - as the OP seems to have come 
to a decision)



Agreed: probably doesn't matter.


'The world' agrees with both, having decided that Numerical Analysis is 
no-longer a necessary ComSc study.


In the ?good, old, days Numerical Analysis included contemplation of the 
difficulties and differences between "precision" and "accuracy". Thus, 
the highly accurate calculation of less-than precise numbers - or was it 
precise values subject to less than accurate computation?

(rhetorical!)

Sort of like giving highly-accurate answers to a less-than precise 
(complete) question, by presuming can ignore the latter.


--
Regards,
=dn
--
https://mail.python.org/mailman/listinfo/python-list


Re: String to Float, without introducing errors

2022-12-17 Thread Thomas Passin

On 12/17/2022 3:45 PM, Paul St George wrote:

Thanks to all!
It was the rounding rounding error that I needed to avoid (as Peter J. Holzer 
suggested). The use of decimal solved it and just in time. I was about to 
truncate the number, get each of the characters from the string mantissa, and 
then do something like this:

64550.727

64550 + (7 * 0.1) + (2 * 0.01) + (7 * 0.001)

Now I do not need to!


And that approach would not have helped you, because each of those 
calculations would be done as floating point, and you wouldn't have 
gotten any more precision (and maybe less) than simply doing 
float('64550.727').


Here is a small but interesting discussion thread about float vs Decimal:

https://stackoverflow.com/questions/32053647/comparing-python-decimals-created-from-float-and-string

Would you mind telling us why that degree of precision (that is, decimal 
vs float) matters for your problem?




On 17 Dec 2022, at 13:11, Alan Gauld  wrote:

On 17/12/2022 11:51, Paul St George wrote:

I have a large/long array of numbers in an external file. The numbers look like 
this:

-64550.727
-64511.489
-64393.637
-64196.763
-63920.2



When I bring the numbers into my code, they are Strings. To use the
numbers in my code, I want to change the Strings to Float type
because the code will not work with Strings but I do not want
to change the numbers in any other way.


That may be impossible. Float type is not exact and the conversion
will be the closest binary representation of your decimal number.
It will be very close but it may be slightly different when you
print it, for example. (You can usually deal with that by using
string formatting features.)

Another option is to use the decimal numeric type. That has other
compromises associated with it but, if retaining absolute decimal
accuracy is your primary goal, it might suit you better.


--
Alan G
Author of the Learn to Program web site
http://www.alan-g.me.uk/
http://www.amazon.com/author/alan_gauld
Follow my photo-blog on Flickr at:
http://www.flickr.com/photos/alangauldphotos






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


Re: String to Float, without introducing errors

2022-12-17 Thread Peter J. Holzer
On 2022-12-17 21:45:06 +0100, Paul St George wrote:
> It was the rounding rounding error that I needed to avoid (as Peter J.
> Holzer suggested). The use of decimal solved it and just in time. I
> was about to truncate the number, get each of the characters from the
> string mantissa, and then do something like this:
> 
> 64550.727
> 
> 64550 + (7 * 0.1) + (2 * 0.01) + (7 * 0.001)

That wouldn't have helped. In fact it would have made matters worse
because instead of a single rounding operation you now have nine!

hp

-- 
   _  | Peter J. Holzer| Story must make more sense than reality.
|_|_) ||
| |   | h...@hjp.at |-- Charles Stross, "Creative writing
__/   | http://www.hjp.at/ |   challenge!"


signature.asc
Description: PGP signature
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: String to Float, without introducing errors

2022-12-17 Thread Chris Angelico
On Sun, 18 Dec 2022 at 08:22, Grant Edwards  wrote:
>
> On 2022-12-17, Chris Angelico  wrote:
>
> >> It was the rounding rounding error that I needed to avoid (as Peter
> >> J. Holzer suggested). The use of decimal solved it and just in
> >> time. I was about to truncate the number, get each of the
> >> characters from the string mantissa, and then do something like
> >> this:
> >>
> >> 64550.727
> >>
> >> 64550 + (7 * 0.1) + (2 * 0.01) + (7 * 0.001)
> >>
> >> Now I do not need to!
> >
> > It sounds like fixed-point arithmetic might be a better fit for what
> > you're doing.
>
> Yes, fixed point (or decimal) is a better fit for what he's doing. but
> I suspect that floating point would be a better fit for the problem
> he's trying to solve.
>

Hard to judge, given how little info we have on the actual problem.
Could go either way.

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


Re: String to Float, without introducing errors

2022-12-17 Thread Grant Edwards
On 2022-12-17, Chris Angelico  wrote:

>> It was the rounding rounding error that I needed to avoid (as Peter
>> J. Holzer suggested). The use of decimal solved it and just in
>> time. I was about to truncate the number, get each of the
>> characters from the string mantissa, and then do something like
>> this:
>>
>> 64550.727
>>
>> 64550 + (7 * 0.1) + (2 * 0.01) + (7 * 0.001)
>>
>> Now I do not need to!
>
> It sounds like fixed-point arithmetic might be a better fit for what
> you're doing.

Yes, fixed point (or decimal) is a better fit for what he's doing. but
I suspect that floating point would be a better fit for the problem
he's trying to solve.

--
Grant



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


Re: String to Float, without introducing errors

2022-12-17 Thread Chris Angelico
On Sun, 18 Dec 2022 at 07:46, Paul St George  wrote:
>
> Thanks to all!
> It was the rounding rounding error that I needed to avoid (as Peter J. Holzer 
> suggested). The use of decimal solved it and just in time. I was about to 
> truncate the number, get each of the characters from the string mantissa, and 
> then do something like this:
>
> 64550.727
>
> 64550 + (7 * 0.1) + (2 * 0.01) + (7 * 0.001)
>
> Now I do not need to!

It sounds like fixed-point arithmetic might be a better fit for what
you're doing.

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


Re: String to Float, without introducing errors

2022-12-17 Thread Paul St George
Thanks to all!
It was the rounding rounding error that I needed to avoid (as Peter J. Holzer 
suggested). The use of decimal solved it and just in time. I was about to 
truncate the number, get each of the characters from the string mantissa, and 
then do something like this:

64550.727

64550 + (7 * 0.1) + (2 * 0.01) + (7 * 0.001)

Now I do not need to!





> On 17 Dec 2022, at 13:11, Alan Gauld  wrote:
> 
> On 17/12/2022 11:51, Paul St George wrote:
>> I have a large/long array of numbers in an external file. The numbers look 
>> like this:
>> 
>> -64550.727
>> -64511.489
>> -64393.637
>> -64196.763
>> -63920.2
> 
>> When I bring the numbers into my code, they are Strings. To use the 
>> numbers in my code, I want to change the Strings to Float type 
>> because the code will not work with Strings but I do not want 
>> to change the numbers in any other way.
> 
> That may be impossible. Float type is not exact and the conversion
> will be the closest binary representation of your decimal number.
> It will be very close but it may be slightly different when you
> print it, for example. (You can usually deal with that by using
> string formatting features.)
> 
> Another option is to use the decimal numeric type. That has other
> compromises associated with it but, if retaining absolute decimal
> accuracy is your primary goal, it might suit you better.
> 
> 
> -- 
> Alan G
> Author of the Learn to Program web site
> http://www.alan-g.me.uk/
> http://www.amazon.com/author/alan_gauld
> Follow my photo-blog on Flickr at:
> http://www.flickr.com/photos/alangauldphotos
> 
> 

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


Re: Single line if statement with a continue

2022-12-17 Thread Abdullah Nafees
Just wanted to say that a silent reader like me learnt more about PEP-8
solely from this thread than my mentor at work or any other course I have
taken earlier this year. Thank you so much.

On Sun, 18 Dec 2022, 00:16 Rob Cliffe via Python-list, <
python-list@python.org> wrote:

>
>
> On 15/12/2022 04:35, Chris Angelico wrote:
> > On Thu, 15 Dec 2022 at 14:41, Aaron P 
> wrote:
> >> I occasionally run across something like:
> >>
> >> for idx, thing in enumerate(things):
> >>  if idx == 103:
> >>  continue
> >>  do_something_with(thing)
> >>
> >> It seems more succinct and cleaner to use:
> >>
> >> if idx == 103: continue.
> >>
> >>
> >> Nothing at all wrong with writing that on a single line. If you have
> >> issues with Flake8 not accepting your choices, reconfigure Flake8 :)
> >>
> >> ChrisA
> I'm so glad that Chris and others say this.  It (i.e. if plus
> break/continue/return on a single line) is something I have quite often
> done in my own code, albeit with a feeling of guilt that I was breaking
> a Python taboo.  Now I will do it with a clear conscience. 
> Best wishes
> Rob Cliffe
> --
> https://mail.python.org/mailman/listinfo/python-list
>
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Keeping a list of records with named fields that can be updated

2022-12-17 Thread Albert-Jan Roskam
   On Dec 15, 2022 10:21, Peter Otten <__pete...@web.de> wrote:

 >>> from collections import namedtuple
 >>> Row = namedtuple("Row", "foo bar baz")
 >>> row = Row(1, 2, 3)
 >>> row._replace(bar=42)
 Row(foo=1, bar=42, baz=3)

   
   Ahh, I always thought these are undocumented methods, but: "In addition to
   the methods inherited from tuples, named tuples support three additional
   methods and two attributes. To prevent conflicts with field names, the
   method and attribute names start with an underscore."
   
https://docs.python.org/3/library/collections.html#collections.somenamedtuple._make
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Top level of a recursive function

2022-12-17 Thread Rob Cliffe via Python-list




On 14/12/2022 13:49, Stefan Ram wrote:

I also found an example similar to what was discussed here
   in pypy's library file "...\Lib\_tkinter\__init__.py":

|def _flatten(item):
|def _flatten1(output, item, depth):
|if depth > 1000:
|raise ValueError("nesting too deep in _flatten")
|if not isinstance(item, (list, tuple)):
|raise TypeError("argument must be sequence")
|# copy items to output tuple
|for o in item:
|if isinstance(o, (list, tuple)):
|_flatten1(output, o, depth + 1)
|elif o is not None:
|output.append(o)
|
|result = []
|_flatten1(result, item, 0)
|return tuple(result)

   .


This presumably results in an (avoidable) run-time overhead from 
constructing _flatten1 every time _flatten is called (and having it 
garbage-collected later).

Best wishes
Rob Cliffe
--
https://mail.python.org/mailman/listinfo/python-list


Re: Single line if statement with a continue

2022-12-17 Thread Rob Cliffe via Python-list



On 15/12/2022 04:35, Chris Angelico wrote:

On Thu, 15 Dec 2022 at 14:41, Aaron P  wrote:

I occasionally run across something like:

for idx, thing in enumerate(things):
 if idx == 103:
 continue
 do_something_with(thing)

It seems more succinct and cleaner to use:

if idx == 103: continue.


Nothing at all wrong with writing that on a single line. If you have
issues with Flake8 not accepting your choices, reconfigure Flake8 :)

ChrisA
I'm so glad that Chris and others say this.  It (i.e. if plus 
break/continue/return on a single line) is something I have quite often 
done in my own code, albeit with a feeling of guilt that I was breaking 
a Python taboo.  Now I will do it with a clear conscience. 

Best wishes
Rob Cliffe
--
https://mail.python.org/mailman/listinfo/python-list


Re: String to Float, without introducing errors

2022-12-17 Thread Thomas Passin

On 12/17/2022 1:41 PM, Mats Wichmann wrote:

On 12/17/22 07:15, Thomas Passin wrote:
You have strings, and you want to end up with numbers.  The numbers 
are not integers.  Other responders have gone directly to whether you 
should use float or decimal as the conversion, but that is a secondary 
matter.


If you have integers, convert with

integer = int(number_string)

-64550.727


they pretty clearly aren't integers:


Of course they aren't.  That's why I gave the line with float() too. 
It's useful to see that there is a basic pattern here:  Given a string, 
expect to need to convert it to what you actually want, int, float, 
decimal, whatever.



-64511.489
-64393.637
-64196.763
-63920.2
-63563.037
-63124.156
-62602.254
-61995.895
-61303.548
-60523.651
-59654.66


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


Re: String to Float, without introducing errors

2022-12-17 Thread Mats Wichmann

On 12/17/22 07:15, Thomas Passin wrote:
You have strings, and you want to end up with numbers.  The numbers are 
not integers.  Other responders have gone directly to whether you should 
use float or decimal as the conversion, but that is a secondary matter.


If you have integers, convert with

integer = int(number_string)

-64550.727


they pretty clearly aren't integers:


-64511.489
-64393.637
-64196.763
-63920.2
-63563.037
-63124.156
-62602.254
-61995.895
-61303.548
-60523.651
-59654.66

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


Re: String to Float, without introducing errors

2022-12-17 Thread Thomas Passin
You have strings, and you want to end up with numbers.  The numbers are 
not integers.  Other responders have gone directly to whether you should 
use float or decimal as the conversion, but that is a secondary matter.


If you have integers, convert with

integer = int(number_string)

If you don't have integers, convert with

number = float(number_string)

If the number is not an integer and you need to be extremely precise 
with the fractional part - for example, if you need to keep exact track 
of exact amounts of money - you can use decimal instead of float.  But 
in this example it seems unlikely that you need that.


The thing to be aware of that hasn't been mentioned so far is that the 
conversion might cause an exception if there is something wrong with one 
of the number strings.  If you don't handle it, your program will quit 
running when it hits the error.  That might not matter to you.  If it 
matters, you can handle the exception in the program, but first you will 
need to decide what to do about such an error: should that number be 
omitted, should it be replaced with a placeholder, should it be replaced 
with float("NaN"), or what else?


In your case here, I'd suggest not handling the error until you get the 
basics of your program working.  Then if you need to, figure out how you 
want to handle exceptions.



On 12/17/2022 6:51 AM, Paul St George wrote:

I have a large/long array of numbers in an external file. The numbers look like 
this:

-64550.727
-64511.489
-64393.637
-64196.763
-63920.2
-63563.037
-63124.156
-62602.254
-61995.895
-61303.548
-60523.651
-59654.66
...

When I bring the numbers into my code, they are Strings. To use the numbers in 
my code, I want to change the Strings to Float type because the code will not 
work with Strings but I do not want to change the numbers in any other way.

So, I want my Strings (above) to be these numbers.

-64550.727
-64511.489
-64393.637
-64196.763
-63920.2
-63563.037
-63124.156
-62602.254
-61995.895
-61303.548
-60523.651
-59654.66
...

Please help!












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


Re: String to Float, without introducing errors

2022-12-17 Thread Peter J. Holzer
On 2022-12-17 12:51:17 +0100, Paul St George wrote:
> I have a large/long array of numbers in an external file. The numbers
> look like this:
> 
> -64550.727
> -64511.489
> -64393.637
[...]
> 
> When I bring the numbers into my code, they are Strings. To use the
> numbers in my code, I want to change the Strings to Float type because
> the code will not work with Strings but I do not want to change the
> numbers in any other way.

>>> s = "-64550.727"
>>> f = float(s)
>>> f
-64550.727
>>> type(f)


(Contrary to the other people posting in this thread I don't think float
is the wrong type for the job. It might be, but you haven't given enough
details to tell whether the inevitable rounding error matters or not. In
my experience in almost all cases where people think it matters it
really doesn't.)

hp

-- 
   _  | Peter J. Holzer| Story must make more sense than reality.
|_|_) ||
| |   | h...@hjp.at |-- Charles Stross, "Creative writing
__/   | http://www.hjp.at/ |   challenge!"


signature.asc
Description: PGP signature
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: String to Float, without introducing errors

2022-12-17 Thread Alan Gauld
On 17/12/2022 11:51, Paul St George wrote:
> I have a large/long array of numbers in an external file. The numbers look 
> like this:
> 
> -64550.727
> -64511.489
> -64393.637
> -64196.763
> -63920.2

> When I bring the numbers into my code, they are Strings. To use the 
> numbers in my code, I want to change the Strings to Float type 
> because the code will not work with Strings but I do not want 
> to change the numbers in any other way.

That may be impossible. Float type is not exact and the conversion
will be the closest binary representation of your decimal number.
It will be very close but it may be slightly different when you
print it, for example. (You can usually deal with that by using
string formatting features.)

Another option is to use the decimal numeric type. That has other
compromises associated with it but, if retaining absolute decimal
accuracy is your primary goal, it might suit you better.


-- 
Alan G
Author of the Learn to Program web site
http://www.alan-g.me.uk/
http://www.amazon.com/author/alan_gauld
Follow my photo-blog on Flickr at:
http://www.flickr.com/photos/alangauldphotos


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


Re: String to Float, without introducing errors

2022-12-17 Thread Weatherby,Gerard
https://docs.python.org/3/library/decimal.html

Get Outlook for iOS

From: Python-list  on 
behalf of Paul St George 
Sent: Saturday, December 17, 2022 6:51:17 AM
To: python-list@python.org 
Subject: String to Float, without introducing errors

*** Attention: This is an external email. Use caution responding, opening 
attachments or clicking on links. ***

I have a large/long array of numbers in an external file. The numbers look like 
this:

-64550.727
-64511.489
-64393.637
-64196.763
-63920.2
-63563.037
-63124.156
-62602.254
-61995.895
-61303.548
-60523.651
-59654.66
...

When I bring the numbers into my code, they are Strings. To use the numbers in 
my code, I want to change the Strings to Float type because the code will not 
work with Strings but I do not want to change the numbers in any other way.

So, I want my Strings (above) to be these numbers.

-64550.727
-64511.489
-64393.637
-64196.763
-63920.2
-63563.037
-63124.156
-62602.254
-61995.895
-61303.548
-60523.651
-59654.66
...

Please help!










--
https://urldefense.com/v3/__https://mail.python.org/mailman/listinfo/python-list__;!!Cn_UX_p3!guE_zPsjxMW4k6nHIdOqZbrt8SdjUC9GELXgSHatARIr2PrAYr6tXCmixkZGNocjsf9SKLduQFjZjM7tOeaQ$
-- 
https://mail.python.org/mailman/listinfo/python-list


String to Float, without introducing errors

2022-12-17 Thread Paul St George
I have a large/long array of numbers in an external file. The numbers look like 
this:

-64550.727
-64511.489
-64393.637
-64196.763
-63920.2
-63563.037
-63124.156
-62602.254
-61995.895
-61303.548
-60523.651
-59654.66
...

When I bring the numbers into my code, they are Strings. To use the numbers in 
my code, I want to change the Strings to Float type because the code will not 
work with Strings but I do not want to change the numbers in any other way.

So, I want my Strings (above) to be these numbers.

-64550.727
-64511.489
-64393.637
-64196.763
-63920.2
-63563.037
-63124.156
-62602.254
-61995.895
-61303.548
-60523.651
-59654.66
...

Please help!










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