How can I prepare to use curses.ascii.isalnum?

I don’t know how (or whether) to use modulefinder and import. Python documentation is incomprehensible.

To use a name you need to either (a) import the name from the module or
(b) import the module and use the name via the module name.

For (a):

 from curses.ascii import isalnum

This defines the name isalnum to be the isalnum function from
curses.ascii. You can use expressions like isalnum(c) to test c.

For (b):

 import curses.ascii

This imports the name curses and you can use expressions like
curses.ascii.isalnum(c) to test c.

Did you try the tutorial, specifically the section about modules?

import curses.ascii

File “C:\python\lib\curses_init_.py”, line 13, in
from _curses import *
ModuleNotFoundError: No module named ‘_curses’

I found it incomprehensible.

Ah. The Python curses module is UNIX only, and you’re on Windows.

Apparently there’s a Windows package you can install:

I’ve never used it and do not know how well it works.

If you’re on Windows 11 in principle you could install WSL (Windows
Services for Linux) and run Python using curses inside it, but that will
be a whole lot more complicated.

Are you following some tutorial which is asking you to write a curses
based program?

No, I’m just trying to check a list of email addresses for syntax errors. Yahoo claimed there was one, but I didn’t find any.

No, I’m just trying to check a list of email addresses for syntax
errors. Yahoo claimed there was one, but I didn’t find any.

Then I’m not sure how you got to the curses module - it’s got nothing to
do with email at all.

I’d use Pythons email package for this. The email.utils module has
several functions:

I’d probably reach for email.utils.parseaddr if you’ve got some list
of addresses: email.utils: Miscellaneous utilities — Python 3.12.4 documentation

It returns a 2-tuple of the realname and email address components
from an email address. Here’s me using it interactively at Pythons >>>
prompt.

 >>> from email.utils import parseaddr
 >>> address = "Cameron Simpson <cs@cskk.id.au>"
 >>> parseaddr(address)
 ('Cameron Simpson', 'cs@cskk.id.au')
 >>> parseaddr("some invlaid address")
 ('', 'some')

You might do that and then make sure there’s an '@' in the second
value?

I need curses to check that characters in email addresses are alphanumeric. But it turned out that the email client secretly had a limited buffer that truncated an email address. Yahoo should hire better programmers.

I can’t imagine why. The .isalnum method is built in to strings.

As Karl mentioned, the str type (strings) has an isalnum method for
this test: Built-in Types — Python 3.12.4 documentation
But note that it does Unicode alphanumeric checks, with details
described in the isalpha and isnumeric methods lower on that page.

But if you’re trying to test for a valid email address, which is what
you said, you need to do a proper email address parse. This is why I
pointed you at email.utils.parseaddr which is specificly for that
purpose.

The address syntax is defined here:

It’s a lot more than just “alphanumeric”.

1 Like

I can’t figure out how to use it. The python documentation is inscrutable.

Of course. But that’s the part I can’t figure out.

How do I use email.utils.parseaddr (what else is needed?)?

I showed you a working example back in

All that’s needed is you email addresses you need to check and a little
control logic to test each of them.