Re: Is there a conflict of libraries here?

2020-11-09 Thread Ethan Furman

On 11/8/20 9:20 PM, Chris Angelico wrote:


Perhaps, but certainly the confusion is far less when the module is
always imported as itself, and then the class is "datetime.datetime"
which nicely parallels "datetime.date" and "datetime.time".


I find doubled names such as "datetime.datetime" both jarring and visual noise.

Thankfully, we can rename modules on import:

  import datetime as dt
  import time as t
  import PIL.Image as im

As for "from blablah import *" -- that should only be done with modules specifically designed for it; having said that, 
it looks like datetime.py was so designed.  So, if I had a module making extensive use of the datetime contents, and 
wasn't using the time module, I would have no problem with "from datetime import *".  On the other hand, if datetime was 
only a small portion of my module, I would "import datetime as dt".


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


How can I make this more complex?

2020-11-09 Thread Quentin Bock
grade = input("Enter your grade: ")
if grade >= 90:
print("You got an A ")
if grade >= 80:
print("You got a B ")
if grade >= 70:
print("You got a C")
if grade >= 60:
print("You got a D ")
if grade >= 50:
print("You failed")




How can I make this to say if your grade is 90 or higher BUT less than 100
you got an A,
Thanks
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: How can I make this more complex?

2020-11-09 Thread inhahe
if 100 > grade >= 90:



On Mon, Nov 9, 2020 at 6:01 PM Quentin Bock  wrote:

> grade = input("Enter your grade: ")
> if grade >= 90:
> print("You got an A ")
> if grade >= 80:
> print("You got a B ")
> if grade >= 70:
> print("You got a C")
> if grade >= 60:
> print("You got a D ")
> if grade >= 50:
> print("You failed")
>
>
>
>
> How can I make this to say if your grade is 90 or higher BUT less than 100
> you got an A,
> Thanks
> --
> https://mail.python.org/mailman/listinfo/python-list
>
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: How can I make this more complex?

2020-11-09 Thread Bob Gailer
On Nov 9, 2020 5:59 PM, "Quentin Bock"  wrote:
>
> grade = input("Enter your grade: ")
> if grade >= 90:
> print("You got an A ")
> if grade >= 80:
> print("You got a B ")
> if grade >= 70:
> print("You got a C")
> if grade >= 60:
> print("You got a D ")
> if grade >= 50:
> print("You failed")
>
>
>
>
> How can I make this to say if your grade is 90 or higher BUT less than 100
> you got an A

if 100 > grade <= 90:

Additional suggestions:

change all but the first if to elif
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: How can I make this more complex?

2020-11-09 Thread MRAB

On 2020-11-09 21:04, Quentin Bock wrote:

grade = input("Enter your grade: ")
if grade >= 90:
 print("You got an A ")
if grade >= 80:
 print("You got a B ")
if grade >= 70:
 print("You got a C")
if grade >= 60:
 print("You got a D ")
if grade >= 50:
 print("You failed")




How can I make this to say if your grade is 90 or higher BUT less than 100
you got an A,

In addition to other the answers, you should note that the 'input' 
function returns a string, so you'll need to convert that string to a 
number, most probably by using 'int', and, ideally, print a helpful 
message if that raises ValueError because it's not a valid number.

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


Re: How can I make this more complex?

2020-11-09 Thread dn via Python-list

On 10/11/2020 10:04, Quentin Bock wrote:

grade = input("Enter your grade: ")
if grade >= 90:
 print("You got an A ")
if grade >= 80:
 print("You got a B ")
if grade >= 70:
 print("You got a C")
if grade >= 60:
 print("You got a D ")
if grade >= 50:
 print("You failed")



First: open a Python terminal (REPL) and try:

import this

(wrt the post's title, lines three and four apply. This output is known 
as "The Zen of Python"!)



Did you ever run the code? Hint: it won't work, and even when 'fixed' 
won't work the way you want, either. What if the grade is < 50?


If the grade is 55, which message(s) should be printed?
(compare: syntax errors, semantic errors, and errors in logic)


Others have mentioned elif. Why?
If the code looks like a "ladder" with criteria being applied to the 
same variable at every step, only one will be chosen with an elif 
structure, but >=1 choice will be made without (as above).



Complexity?
Sure, if that's what you want, we've got complexity, but it'll cost you...
(as I tell my trainees: "just because we can do it, does not make it a 
good idea!")


Build a dictionary of "buckets" - with the (lowest point) grade-steps as 
keys and the applicable messages as values (this will actually be easier 
to maintain than the 'ladder'!), eg


90:"You got an A "
80:"You got a B "
...
0:"You didn't say what should happen"

Take the grade, check, and properly prepare it(!)
(why "check"?)

Loop through the dictionary:
if the grade 'fits into' this bucket:
print the message
break# no point in continuing to loop
# otherwise continue looping


Not to be recommended
- but if you are a 'glutton for punishment', don't bother with the 
dictionary's 0/last entry. Instead use a for-else structure...
Such would be an excellent case-study illustration of why 'simple' beats 
'complex'!
(sorry for the sardonic humor, disregard the last paragraph - most 
professional coders (in fact, all that I know) eschew for-else, or-else!)

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


Changing strings in files

2020-11-09 Thread Manfred Lotz
I have a situation where in a directory tree I want to change a certain
string in all files where that string occurs.

My idea was to do

- os.scandir and for each file
   - check if a file is a text file
   - if it is not a text file skip that file
   - change the string as often as it occurs in that file


What is the best way to check if a file is a text file? In a script I
could use the `file` command which is not ideal as I have to grep the
result. In Perl I could do  -T file.

How to do best in Python?

-- 
Thanks,
Manfred

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


Re: Changing strings in files

2020-11-09 Thread Loris Bennett
Manfred Lotz  writes:

> I have a situation where in a directory tree I want to change a certain
> string in all files where that string occurs.
>
> My idea was to do
>
> - os.scandir and for each file
>- check if a file is a text file
>- if it is not a text file skip that file
>- change the string as often as it occurs in that file
>
>
> What is the best way to check if a file is a text file? In a script I
> could use the `file` command which is not ideal as I have to grep the
> result. In Perl I could do  -T file.
>
> How to do best in Python?

If you are on Linux and more interested in the result than the
programming exercise, I would suggest the following non-Python solution:

   find . -type -f -exec sed -i 's/foo/bar/g' {} \;

Having said that, I would be interested to know what the most compact
way of doing the same thing in Python might be.

Cheers,

Loris

-- 
This signature is currently under construction.
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Changing strings in files

2020-11-09 Thread Cameron Simpson
On 10Nov2020 07:24, Manfred Lotz  wrote:
>I have a situation where in a directory tree I want to change a certain
>string in all files where that string occurs.
>
>My idea was to do
>
>- os.scandir and for each file

Use os.walk for trees. scandir does a single directory.

>   - check if a file is a text file

This requires reading the entire file. You want to check that it 
consists entirely of lines of text. In your expected text encoding - 
these days UTF-8 is the common default, but getting this correct is 
essential if you want to recognise text. So as a first cut, totally 
untested:

for dirpath, filenames, dirnames in os.walk(top_dirpath):
is_text = False
try:
# expect utf-8, fail if non-utf-8 bytes encountered
with open(filename, encoding='utf-8', errors='strict') as f:
for lineno, line in enumerate(f, 1):
... other checks on each line of the file ...
if not line.endswith('\n'):
raise ValueError("line %d: no trailing newline" lineno)
if str.isprintable(line[:-1]):
raise ValueError("line %d: not all printable" % lineno)
# if we get here all checks passed, consider the file to 
# be text
is_text = True
except Exception as e:
print(filename, "not text", e)
if not is_text:
print("skip", filename)
continue

You could add all sorts of other checks. "text" is a loosely defined 
idea. But you could assert: all these lines decoded cleanly, so I can't 
do much damage rewriting them.

>   - if it is not a text file skip that file
>   - change the string as often as it occurs in that file

You could, above, gather up all the lines in the file in a list. If you 
get through, replace your string in the list and if anything was 
changed, rewrite the file from the list of lines.

>What is the best way to check if a file is a text file? In a script I
>could use the `file` command which is not ideal as I have to grep the
>result.

Not to mention relying on file, which (a) has a simple idea of text and 
(b) only looks at the start of each file, not the whole content. Very 
dodgy.

If you're really batch editing files, you could (a) put everything into 
a VCS (eg hg or git) so you can roll back changes or (b) work on a copy 
of your directory tree or (c) just print the "text" filenames to stdout 
and pipe that into GNU parallel, invoking "sed -i.bak s/this/that/g" to 
batch edit the checked files, keeping a backup.

Cheers,
Cameron Simpson 
-- 
https://mail.python.org/mailman/listinfo/python-list