Pandas/dataexplore, how do you get data into them?
I am looking at dataexplore and Pandas, they look as if they may provide useful tools but at the moment I can't quite understand how you get data into them. How do you load a large table into dataexplore? Ultimetely I want to get data from a database table but any help would be useful. -- Chris Green · -- https://mail.python.org/mailman/listinfo/python-list
Re: Pandas/dataexplore, how do you get data into them?
Stefan Ram wrote: > Chris Green writes: > >How do you load a large table into dataexplore? > > I have not tested the following lines, I have no experience > with "dataexplore", this is just from what I heard: > > from pandastable.app import DataExplore > app = DataExplore() > table = app.getCurrentTable() > > You can now supposedly use »table.importCSV« to import CSV. > Read the docs for more details. It might not even work as I > wrote, but maybe just the names of those callables can get > you started. > > Also check out »app.load_dataframe« which supposedly can > load a "dataframe" (interactively?). > Thanks, those might get me going. -- Chris Green · -- https://mail.python.org/mailman/listinfo/python-list
Re: Pandas/dataexplore, how do you get data into them?
Thomas Jollans wrote: > On 08/10/17 18:02, Chris Green wrote: > > I am looking at dataexplore and Pandas, they look as if they may > > provide useful tools but at the moment I can't quite understand how > > you get data into them. > > > > How do you load a large table into dataexplore? > > > > Ultimetely I want to get data from a database table but any help would > > be useful. > > > I know nothing about dataexplore, but pandas has tonnes of handy I/O > functions: > > http://pandas.pydata.org/pandas-docs/version/0.20/io.html > > I mostly use pandas.read_csv to get data in, but there is also a > read_sql function that you might find useful > Ah, thank you, this is getting more interesting! :-) -- Chris Green · -- https://mail.python.org/mailman/listinfo/python-list
Re: how to read in the newsreader
Michael Torrie wrote: > On 10/15/2017 08:50 PM, Andrew Z wrote: > > Gents, > > how do i get this group in a newsreader? The digest i'm getting is not > > workable for me - i can't reply , can only read the replies from the > > members of the group. Or. maybe, it shouldn't be a news reader > > please advise.. > > > > P.S. Oh the comp.lang.python is a nightmare because of spam... > > Regardless of what usenet reader you use, com.lang.python will have the > same spam everywhere. So maybe reading via usenet isn't that useful anyway. > The mostly very silly spam is trivial to filter with some very simple rules, most newsreaders have easy ways to specify subjects and/or senders to ignore. I have (I think) just three or four rules that eliminate just about all the junk. -- Chris Green · -- https://mail.python.org/mailman/listinfo/python-list
Re: how to read in the newsreader
Paul Moore wrote: > On 16 October 2017 at 15:41, Grant Edwards wrote: > > On 2017-10-16, Terry Reedy wrote: > >> On 10/15/2017 10:50 PM, Andrew Z wrote: > >>> Gents, > >>> how do i get this group in a newsreader? > >> > >> Point your newsreader to news.gmane.org, > > > > That, IMO, is the only sane way to read mailing lists. If a mailing > > list isn't carried on gmane, I don't bother with it. > > Unless you work regularly on multiple PCs, as there's no newsreader I > know of that maintains your settings (what articles you have read, in > particular) across multiple installations. And gmane's UI sucks. > > For that situation, reading mailing lists as mails in gmail is the > best option I've been able to find (not ideal, but adequate). I read newsgroups using tin, a text-mode/command line newsreader. I always run tin on my home desktop machine, even if I'm away from home by using ssh. So I maintain my settings that way. By the way tin *is* mouse away even though it's a text mode program. For reading mailing lists (and all my mail for that matter) I use mutt which is also a text mode program. It makes reading mailing lists easy because it provides threading etc. I use like I use tin, alway on my desktop machine using ssh where necessary. -- Chris Green · -- https://mail.python.org/mailman/listinfo/python-list
Re: how to read in the newsreader
Marko Rauhamaa wrote: > Chris Green : > > > I read newsgroups using tin, a text-mode/command line newsreader. I > > always run tin on my home desktop machine, even if I'm away from home > > by using ssh. So I maintain my settings that way. By the way tin *is* > > mouse away even though it's a text mode program. > > > > For reading mailing lists (and all my mail for that matter) I use mutt > > which is also a text mode program. It makes reading mailing lists easy > > because it provides threading etc. I use like I use tin, alway on my > > desktop machine using ssh where necessary. > > Same thing, except that I do all of my typing on emacs (GNUS in the case > of news and mail). > That's the other advantage of working this way, one always uses the same text editor for composing messages, I use a vi clone (let's not start a vi/emacs war!). -- Chris Green · -- https://mail.python.org/mailman/listinfo/python-list
Re: Please tell me how to execute python file in Ubuntu by double clicking on file. (Posting On Python-List Prohibited)
Chris Angelico wrote: > On Sun, Dec 10, 2017 at 12:56 PM, Steve D'Aprano > wrote: > > Remember the context here: we're replying to a thread discussing somebody > > who > > is running Ubuntu with a GUI desktop environment. Of course there are *some* > > Linux systems which don't run a GUI at all, but you can't double-click on > > files on such systems, and they aren't Ubuntu, so they aren't relevant. > > Been a long time since I had an Ubuntu, but is it really the case that > you can't install Ubuntu without a GUI? Anyhow, your first part is > correct: without a GUI, you can't exactly double-click on a file. > You'd have to install 'ubuntu server' to get an Ubuntu without a GUI, -- Chris Green · -- https://mail.python.org/mailman/listinfo/python-list
Re: Goto (Posting On Python-List Prohibited)
Dennis Lee Bieber wrote: > > Well... "break" does bypass the rest of the block, but it still exits > via the end of the block. I have a tendency to try for one "return" per > procedure (so I'm more likely to have an "if ...: break" then "if ...: > return"). I have always tried to enforce 'only one return per function'. If there are multiple returns it makes maintenance very difficult as 'clear up' code can get bypassed. -- Chris Green · -- https://mail.python.org/mailman/listinfo/python-list
How to diagnose this, fails on 3.6.3, works on 3.5.2?
I have a fairly simple little python program to automate starting an editor on a wiki page. It works fine on the system where I wrote it (xubuntu 16.04, python 3 version 3.5.2) but it comes up with the following error on a newer system (xubuntu 17.10, python 3 version 3.6.3). Here is the error:- chris$ no Traceback (most recent call last): File "/home/chris/bin/no", line 59, in os.execvp("vi", ("", monthFile,)) File "/usr/lib/python3.6/os.py", line 559, in execvp _execvpe(file, args) File "/usr/lib/python3.6/os.py", line 594, in _execvpe exec_func(fullname, *argrest) ValueError: execv() arg 2 first element cannot be empty Has execvp() become stricter in 3.6.3 or what? ... and here is the program:- #!/usr/bin/python3 # # # Create Dokuwiki journal month pages # import sys import os import time import calendar jdir = "/home/chris/wiki/data/pages/journal" # # # Default month and year is 'now' # month = time.localtime().tm_mon year = time.localtime().tm_year # # # If one parameter is given then it's the month # if len(sys.argv) == 2: month = int(sys.argv[1]) # # # If two parameters are given they are month and year # if len(sys.argv) == 3: year = int(sys.argv[2]) month = int(sys.argv[1]) # # # # # # # Check if the year directory exists and create it if it doesn't # yearDir = os.path.join(jdir, str(year)) if not os.path.exists(yearDir): os.mkdir(yearDir) # # # Check if month file exists, create it if it doesn't and write heading and links # if month < 10: monthFile = os.path.join(yearDir, '0' + str(month) + '.txt') else: monthFile = os.path.join(yearDir, str(month) + '.txt') if not os.path.exists(monthFile): monthName = calendar.month_name[month] f = open(monthFile, 'w') f.write(monthName + " " + str(year) + "\n") for i in range(len(monthName) + 5): f.write("=") f.write("\n") f.close() os.execvp("vi", ("", monthFile,)) -- Chris Green · -- https://mail.python.org/mailman/listinfo/python-list
Re: How to diagnose this, fails on 3.6.3, works on 3.5.2?
Peter Otten <__pete...@web.de> wrote: > Chris Green wrote: > > > I have a fairly simple little python program to automate starting an > > editor on a wiki page. It works fine on the system where I wrote it > > (xubuntu 16.04, python 3 version 3.5.2) but it comes up with the > > following error on a newer system (xubuntu 17.10, python 3 version > > 3.6.3). > > > > Here is the error:- > > > > chris$ no > > Traceback (most recent call last): > > File "/home/chris/bin/no", line 59, in > > os.execvp("vi", ("", monthFile,)) > > File "/usr/lib/python3.6/os.py", line 559, in execvp > > _execvpe(file, args) > > File "/usr/lib/python3.6/os.py", line 594, in _execvpe > > exec_func(fullname, *argrest) > > ValueError: execv() arg 2 first element cannot be empty > > > > Has execvp() become stricter in 3.6.3 or what? > > Yes; the relevant issue on the bug tracker seems to be > > https://bugs.python.org/issue28732 > OK, thanks all, I just need to put 'vi' in that empty parameter. -- Chris Green · -- https://mail.python.org/mailman/listinfo/python-list
Re: This newsgroup (comp.lang.python) may soon be blocked by Google Groups
jlada...@itu.edu wrote: > On Thursday, February 1, 2018 at 9:07:15 AM UTC-8, alister wrote: > > > simple solution stop using google groups & use either the mailing list or > > a news server with an NNTP client > > Sigh. I've been on Usenet since 1986. Is this how it ends? I will move if > I must. > > Let me ask those of you who are not using Google Groups: how do you search? > In my experience, searching through mailing list archives has been poor. > I read using NNTP. I run my own local newserver (leafnode, very simple to configure) that aggregates feeds from two public NNTP servers which provides some robustness if things go wrong with either. By configuring the time for which posts are retained by leafnode (to quite a long time) I can search back a long way. As I only read a few tens of newsgroups this doesn't require much disk space. -- Chris Green · -- https://mail.python.org/mailman/listinfo/python-list
Re: This newsgroup (comp.lang.python) may soon be blocked by Google Gro
Mark Lawrence wrote: > On 09/02/18 11:23, John Ladasky wrote: > > > > I haven't visited GMane in a few years, but I found it difficult to > > navigate. > In particular, I found searching to be cumbersome. Weren't the archives > broken into 30-day blocks? > > > > I just tried GMane again two minutes ago. I can't confirm my recollections, > but right now what I'm seeing is worse. If you follow this link right now... > (http://gmane.org/find.php?list=comp.lang.python)... you get this: > > > > "Not all of Gmane is back yet - We're working hard to restore everything" > > > > And if you follow the link labeled "The latest news is at Gmane News" > at the bottom of that page, it takes you here... (http://home.gmane.org/)... > and the top blog post discussing site repairs is dated September 2016! > > > > > I'm not too excited about trying GMane again after seeing that. > > > > Please don't waste your time with the gmane website. Just point any > (semi-)decent mail client like Thunderbird at news.gmane.org and you've Surely you mean NNTP/Usenet client. > access to hundreds of Python lists and thousands of other technical > lists. I find the search facilities perfectly adequate. > -- Chris Green · -- https://mail.python.org/mailman/listinfo/python-list
Re: This newsgroup (comp.lang.python) may soon be blocked by Google Gro
Steven D'Aprano wrote: > On Fri, 09 Feb 2018 12:45:29 +0000, Chris Green wrote: > > > Mark Lawrence wrote: > [...] > >> Please don't waste your time with the gmane website. Just point any > >> (semi-)decent mail client like Thunderbird at news.gmane.org and you've > > > > Surely you mean NNTP/Usenet client. > > That's what he said :-) > > Thunderbird does both mail and news. Or at least it did, last time I > checked. > He said ".(semi-)decent mail client like Thunderbird" That seems to imply (to me anyway) that one needs a mail client to read from news.gmane.org and that's just plain wrong. While *some* mail clients are also NNTP readers it's by no means universally true. -- Chris Green · -- https://mail.python.org/mailman/listinfo/python-list
Re: This newsgroup (comp.lang.python) may soon be blocked by Google Gro
codewiz...@gmail.com wrote: > On Saturday, February 3, 2018 at 7:15:16 PM UTC-5, pyotr filipivich wrote: > > [snip] > > Those of us who do not use google-groups may not notice the loss > > of the google groupies. > > I use GG to read comp.lang.python because of the following combination > of factors. I would definitely be happier if there was another way to > do this, so that I wouldn't have to wade through lots of spam. > > - I read it from both home and work. > > - I can't install any additional software at work. > > - There is no newsgroup reader software available at work. > > - GG tracks read/unread status of every post. This is shared > between work and home through my Google account. > > - When I (very rarely) post from work, I can't > do it by email, since outgoing email is blocked. > Alternative approach, what I do:- Run a text mode (but very capable and mouse aware) newsreader on my home system, read news locally using that. Use ssh (is that available at worK?) to connect from work to home and run the same newsreader in the same environment. If you can't even ssh from work then you can always use an 'ssh from the web' app from your wenb browser. The newsreader I use is tin by the way. -- Chris Green · -- https://mail.python.org/mailman/listinfo/python-list
Re: This newsgroup (comp.lang.python) may soon be blocked by Google Gro
Michael F. Stemper wrote: > On 2018-02-09 13:37, Chris Green wrote: > > > Alternative approach, what I do:- > > > > Run a text mode (but very capable and mouse aware) newsreader on > > my home system, read news locally using that. > > > > Use ssh (is that available at worK?) to connect from work to home > > Your ISP provides that capability? I'm surprised. I'm with Charter and > the only external IP address is for the cable modem (I think). > "icanhazip" shows the same one, no matter which computer I use. > You use the same external IP address, just use the right port number (which you set up your router to allow in). -- Chris Green · -- https://mail.python.org/mailman/listinfo/python-list
Re: Gmane seems to be gone
Grant Edwards wrote: > I've been dreading this moment for a couple years: it looks like > gmane.org is gone. The original operator/maintainer gave up a couple > years ago and pulled the plug. Somebody else took over at that point. > The Web UI was never revived, but the basic NNTP<->mailing-list gateway > continue to work -- until tonight. Now the domain is gone. Perhaps > it's just an oversight, but I've got a bad feeling... > I think it was a short term hiccough, a posting of mine got bounced yesterday but subsequent ones worked OK. -- Chris Green · -- https://mail.python.org/mailman/listinfo/python-list
Re: Gmane seems to be gone
Marko Rauhamaa wrote: > Grant Edwards : > > > Does anybody have any idea what it would take to set up a private NNTP > > server that served articles from a dozen or so IMAP mailboxes? > > > > How hard would it be to write something like that in Python? I've got > > a pretty good handle on the IMAP end of things, but I've got no clue > > about the NNTP server end. > > I bet NNTP (https://tools.ietf.org/html/rfc3977>) is a simpler > beast than IMAP. > > Maybe Twisted has some support for it, I don't know, but writing a > server from scratch wouldn't be too bad, either. > > However, the simplest way forward might be to just take an off-the-shelf > NNTP server and write a IMAP/NNTP gateway bot that acts as a client > bothways. Then you can use Python's nntplib and imaplib. > Leafnode might be a good place to start. -- Chris Green · -- https://mail.python.org/mailman/listinfo/python-list
Re: Gmane seems to be gone
Grant Edwards wrote: > On 2018-02-18, Chris Green wrote: > > Grant Edwards wrote: > >> I've been dreading this moment for a couple years: it looks like > >> gmane.org is gone. The original operator/maintainer gave up a couple > >> years ago and pulled the plug. Somebody else took over at that point. > >> The Web UI was never revived, but the basic NNTP<->mailing-list gateway > >> continue to work -- until tonight. Now the domain is gone. Perhaps > >> it's just an oversight, but I've got a bad feeling... > >> > > I think it was a short term hiccough, a posting of mine got bounced > > yesterday but subsequent ones worked OK. > > Does news.gmane.org (or any gmane.org domain) resolve for you? > chris$ host gmane.org gmane.org has address 104.31.85.1 gmane.org has address 104.31.84.1 gmane.org has IPv6 address 2400:cb00:2048:1::681f:5401 gmane.org has IPv6 address 2400:cb00:2048:1::681f:5501 gmane.org mail is handled by 10 mxfilter-048034.atla03.us.yomura.com. gmane.org mail is handled by 10 mxfilter-048035.atla03.us.yomura.com. chris$ host news.gmane.org news.gmane.org is an alias for blaine.gmane.org. blaine.gmane.org has address 195.159.176.226 blaine.gmane.org mail is handled by 5 blaine.gmane.org. chris$ -- Chris Green · -- https://mail.python.org/mailman/listinfo/python-list
Re: Find the path of a shell command
jak wrote: > Il 12/10/2022 09:40, jkn ha scritto: > > On Wednesday, October 12, 2022 at 6:12:23 AM UTC+1, jak wrote: > >> Il 12/10/2022 06:00, Paulo da Silva ha scritto: > >>> Hi! > >>> > >>> The simple question: How do I find the full path of a shell command > >>> (linux), i.e. how do I obtain the corresponding of, for example, > >>> "type rm" in command line? > >>> > >>> The reason: > >>> I have python program that launches a detached rm. It works pretty well > >>> until it is invoked by cron! I suspect that for cron we need to specify > >>> the full path. > >>> Of course I can hardcode /usr/bin/rm. But, is rm always in /usr/bin? > >>> What about other commands? > >>> > >>> Thanks for any comments/responses. > >>> Paulo > >>> > >> I'm afraid you will have to look for the command in every path listed in > >> the PATH environment variable. > > > > erm, or try 'which rm' ? > > You might but if you don't know where the 'rm' command is, you will have > the same difficulty in using 'which' command. Do not you think? > From a command prompt use the bash built-in 'command' :- command -v rm ... and rm will just about always be in /usr/bin. -- Chris Green · -- https://mail.python.org/mailman/listinfo/python-list
Re: Find the path of a shell command
Michael F. Stemper wrote: > On 12/10/2022 07.20, Chris Green wrote: > > jak wrote: > >> Il 12/10/2022 09:40, jkn ha scritto: > >>> On Wednesday, October 12, 2022 at 6:12:23 AM UTC+1, jak wrote: > > >>>> I'm afraid you will have to look for the command in every path listed in > >>>> the PATH environment variable. > >>> > >>> erm, or try 'which rm' ? > >> > >> You might but if you don't know where the 'rm' command is, you will have > >> the same difficulty in using 'which' command. Do not you think? > > From a command prompt use the bash built-in 'command' :- > > > > command -v rm > > > > ... and rm will just about always be in /usr/bin. > > On two different versions of Ubuntu, it's in /bin. > I think you'll find it's in both /bin and /usr/bin, usually /usr/bin is earlier in the path so /usr/bin/rm is the one that will normally be found first. It's only in /bin/rm in case one has a system which mounts /bin separately and earlier in the boot sequence and rm is one of the commands needed early on. -- Chris Green · -- https://mail.python.org/mailman/listinfo/python-list
Re: What might suddenly provoke this poplib error?
A further little bit of information, I tried running getCatchall.py from the command prompt and there was a long wait before it output the same error message. I.e. it looks rather as if the server is not responding to requests. (A 'long wait' is a minute or two) -- Chris Green · -- https://mail.python.org/mailman/listinfo/python-list
What might suddenly provoke this poplib error?
I have a short python3 program that collects E-Mails from a 'catchall' mailbox, sends the few that might be interesting to me and dumps the rest. It has suddenly (after working for some years) started throwing the following:- Traceback (most recent call last): File "/home/chris/.mutt/bin/getCatchall.py", line 83, in pop3.pass_('brzmilla') File "/usr/lib/python3.10/poplib.py", line 218, in pass_ return self._shortcmd('PASS %s' % pswd) File "/usr/lib/python3.10/poplib.py", line 181, in _shortcmd return self._getresp() File "/usr/lib/python3.10/poplib.py", line 157, in _getresp raise error_proto(resp) poplib.error_proto: b'-ERR internal server error' The section of code throwing the error is as follows:- # # # Connect to the POP3 server, get message count, exit if no messages # for t in range(10): # retry 10 times try: pop3 = poplib.POP3_SSL('mail3.gridhost.co.uk',timeout=300) break except TimeoutError: if t == 9: log.err("Timed out 10 times, giving up") exit(1) else: log.warn("Timed out, try " + str(t)) pop3.user('catch...@isbd.net') pop3.pass_('brzmilla') numMessages = len(pop3.list()[1]) if (numMessages == 0): break It seems to be saying that the POP3 server has a problem, if so there's not much I can do about it as it's my hosting provider's mail server. Is it really saying the server has a problem? -- Chris Green · -- https://mail.python.org/mailman/listinfo/python-list
Re: What might suddenly provoke this poplib error?
MRAB wrote: [snip boring code] > > > > It seems to be saying that the POP3 server has a problem, if so there's not > > much I can do about it as it's my hosting provider's mail server. Is it > > really saying the server has a problem? > > > As you've already ascertained that it's a server error, I'd just like to > suggest that you add a sleep before retrying. If it has timed out after > 5 minutes, I doubt there's much point in retrying immediately. I guess I could do that but this has only happened once in quite a few years so I'm not sure if I;ll bother! :-) The program is only run half-hourly by cron. -- Chris Green · -- https://mail.python.org/mailman/listinfo/python-list
Re: What might suddenly provoke this poplib error?
Peter J. Holzer wrote: > [-- text/plain, encoding quoted-printable, charset: us-ascii, 28 lines --] > > On 2022-10-13 13:47:07 +0100, Chris Green wrote: > > I have a short python3 program that collects E-Mails from a 'catchall' > > mailbox, sends the few that might be interesting to me and dumps the > > rest. > > > > It has suddenly (after working for some years) started throwing the > > following:- > [...] > > poplib.error_proto: b'-ERR internal server error' > [...] > > It seems to be saying that the POP3 server has a problem, > > Yes. "-ERR" is the normal start of a POP error reply, so the message > "-ERR internal server error" was sent by the POP server. > > > if so there's not much I can do about it as it's my hosting provider's > > mail server. > > You can call you hosting provider and ask them to fix the problem. > After a couple of hours someone obviously fixed something and the error has stopped now. Thanks everyone. -- Chris Green · -- https://mail.python.org/mailman/listinfo/python-list
Re: How to manage python shebang on mixed systems?
rbowman wrote: > On Sun, 6 Nov 2022 10:03:50 +0000, Chris Green wrote: > > > > Is there a neat way of handling this? I could write a sort of wrapper > > script to run via the shebang but that seems overkill to me. > > Can you symlink? Not really, since the system where there's no python3 is one where I only have user access. -- Chris Green · -- https://mail.python.org/mailman/listinfo/python-list
Re: How to manage python shebang on mixed systems?
Chris Green wrote: > > 3: with your pseudo "python3" script in place, make all the scripts use > > the "#!/usr/bin/env python3" shebang suggested above. > > > Yes, that sounds a good plan to me, thanks Cameron. > Doesn't '#!/usr/bin/env python3' suffer from the same problem as '#!/usr/bin/python3' in the sense that the env executable might not be in /usr/bin? Wouldn't '#! env python3' be better? -- Chris Green · -- https://mail.python.org/mailman/listinfo/python-list
Re: How to manage python shebang on mixed systems?
Cameron Simpson wrote: > On 06Nov2022 20:51, jak wrote: > >Il 06/11/2022 11:03, Chris Green ha scritto: > >>I have a number of python scripts that I run on a mix of systems. I > >>have updated them all to run on python 3 but many will also run quite > >>happily with python 2. They all have a #!/usr/bin/python3 shebang. > > I usually use: > > #!/usr/bin/env python3 > > This runs the default "python3" from my $PATH, whatever that is, > avoiding a hardwired path to the python3 executable. > Yes, that's probably a good idea, less likely to break than mine. > >>This works almost everywhere but there is one system where only > >>python 2 is available (at /usr/bin/python). > >> > >>I don't have python 2 on any of the systems I manage myself now so a > >>#!/usr/bin/python shebang will fail. > >> > >>Is there a neat way of handling this? I could write a sort of wrapper > >>script to run via the shebang but that seems overkill to me. > > It is overkill. I generally dislike batch editing scripts. > > 1: do these scripts work on both python2 and python3? It seems like they > would need to. Yes, they do, they're mostly very simple utility scripts for doing things like changing spaces to underscores in filenames and such. Just putting 'print' parameters in brackets was all that most of them needed to work in python 3. > 2: write a tiny script _named_ "python3" which invokes python 2. I keep > a personal "~/bin-local" directory for just such per-system special > commands like this. > 3: with your pseudo "python3" script in place, make all the scripts use > the "#!/usr/bin/env python3" shebang suggested above. > Yes, that sounds a good plan to me, thanks Cameron. -- Chris Green · -- https://mail.python.org/mailman/listinfo/python-list
Re: How to manage python shebang on mixed systems?
Schachner, Joseph (US) wrote: > Maybe you can't do this, but I would suggest only running on the Python > 3 systems. Refuse to jump through hoops for the Python 2 system. It is > years out of date. Exactly! That's why I (OP) have only one system where this is a problem. The system is my hosting provider's cPanel platform which is running a very old Linux and, as I've said has only python 2. I can ask for python 3 on their system but I suspect that my voice is a very tiny one and there are higher priority things to get done. -- Chris Green · -- https://mail.python.org/mailman/listinfo/python-list
Re: How to manage python shebang on mixed systems?
Barry Scott wrote: > > > > On 7 Nov 2022, at 09:28, Chris Green wrote: > > > > Chris Green wrote: > >>> 3: with your pseudo "python3" script in place, make all the scripts use > >>> the "#!/usr/bin/env python3" shebang suggested above. > >>> > >> Yes, that sounds a good plan to me, thanks Cameron. > >> > > Doesn't '#!/usr/bin/env python3' suffer from the same problem as > > '#!/usr/bin/python3' in the sense that the env executable might not be > > in /usr/bin? > > env is always available as /usr/bin/env - I think its spec'ed in posix that > way. > > The only reason that things are in /bin are for systems that need a subset of > programs to boot the system to point it can mount /usr. env is not going to be > needed for that use case. > Given that the problem system is running a very old Linux I'm not sure what chance there is that it's fully posix compliant. If using "#!/usr/bin/env python3" is a way of avoiding problems if python3 isn't in /usr/bin then why is it any better depending on env being in /usr/bin. -- Chris Green · -- https://mail.python.org/mailman/listinfo/python-list
Does one have to use curses to read single characters from keyboard?
Is the only way to read single characters from the keyboard to use curses.cbreak() or curses.raw()? If so how do I then read characters, it's not at all obvious from the curses documentation as that seems to think I'm using a GUI in some shape or form. All I actually want to do is get 'Y' or 'N' answers to questions on the command line. Searching for ways to do this produces what seem to me rather clumsy ways of doing it. -- Chris Green · -- https://mail.python.org/mailman/listinfo/python-list
Re: Does one have to use curses to read single characters from keyboard?
Stefan Ram wrote: > Chris Green writes: > >Is the only way to read single characters from the keyboard to use > >curses.cbreak() or curses.raw()? If so how do I then read characters, > > It seems that you want to detect keypresses and not read > characters from a line-buffered console with editing > features. > > Curses is not portable IIRC. A more portable means would > be to use tkinter with the "bind" function to bind keys. > > >All I actually want to do is get 'Y' or 'N' answers to questions on > >the command line. > > answer = input( 'Format drive C: (Y/N)?' ) > ... and therein lies the fundamental problem, you have to type Y or N followed by Return. See my own follow-up though. -- Chris Green · -- https://mail.python.org/mailman/listinfo/python-list
Re: Does one have to use curses to read single characters from keyboard?
My solution in the end was copied from one I found that was much simpler and straightforward than most. I meant to post this earlier but it got lost somewhere:- import sys, termios, tty # # # Read a single character from teminal, specifically for 'Y/N' # fdInput = sys.stdin.fileno() termAttr = termios.tcgetattr(0) # # # Get a single character, setcbreak rather than setraw meands CTRL/C etc. still work # def getch(): sys.stdout.flush() tty.setcbreak(fdInput) ch = sys.stdin.buffer.raw.read(1).decode(sys.stdin.encoding) termios.tcsetattr(fdInput, termios.TCSAFLUSH, termAttr) sys.stdout.write(ch) return ch # # # Get a y or n answer, ignore other characters # def getyn(): ch = 'x' while ch != 'y' and ch != 'n': ch = getch().lower() return ch So getyn() reads a y or an n, ignores anything else and doesn't wait for a return key. Keyboard input operation is restored to normal after doing this. Using tty.setcbreak() rather than tty.setraw() means that CTRL/C etc. still work if things go really wrong. -- Chris Green · -- https://mail.python.org/mailman/listinfo/python-list
Re: Does one have to use curses to read single characters from keyboard?
Stefan Ram wrote: > Chris Green writes: > >import sys, termios, tty > > There might be some versions of Python and the Microsoft® > Windows operating system where "termios" is not available. > Ah, I did originally say that this was a Unix/Linux only solution but that was in my first response that got lost somewhere. -- Chris Green · -- https://mail.python.org/mailman/listinfo/python-list
Re: Does one have to use curses to read single characters from keyboard?
Barry Scott wrote: > > > > On 11 Dec 2022, at 18:50, Chris Green wrote: > > > > My solution in the end was copied from one I found that was much > > simpler and straightforward than most. I meant to post this earlier > > but it got lost somewhere:- > > > >import sys, termios, tty > ># > ># > ># Read a single character from teminal, specifically for 'Y/N' > ># > >fdInput = sys.stdin.fileno() > >termAttr = termios.tcgetattr(0) > ># > ># > ># Get a single character, setcbreak rather than setraw meands CTRL/C > >etc. still work > ># > >def getch(): > >sys.stdout.flush() > >tty.setcbreak(fdInput) > >ch = sys.stdin.buffer.raw.read(1).decode(sys.stdin.encoding) > > Will not work for uncode code points above 255. > > This is what happened when I typed € key: > > :>>> a.getch() > Traceback (most recent call last): > File "", line 1, in > File "/private/var/folders/ll/08dwwqkx6v9bcd15sh06x14wgn/T/a.py", line > 15, in getch > ch = sys.stdin.buffer.raw.read(1).decode(sys.stdin.encoding) > UnicodeDecodeError: 'utf-8' codec can't decode byte 0xe2 in position 0: > unexpected end of data > Since it's just for me to use the above doesn't worry me! :-) I'd have to try quite hard to enter a multi-byte character from my keyboard so it's very unlikely to happen by mistake. -- Chris Green · -- https://mail.python.org/mailman/listinfo/python-list
Re: Single line if statement with a continue
Thomas Passin wrote: >I personally tend to use > > if test: return > > even inside larger blocks. I always try to avoid multiple returns from functions/methods, as soon as things get complex it's all to easy to miss clean-up etc. "No multiple returns" is often found in programming guidelines. -- Chris Green · -- https://mail.python.org/mailman/listinfo/python-list
Re: Mailing-Lists (pointer)
dn wrote: [snip] > See also the wisdom of enabling comp.lang.python and python-list as > 'mirrors', enabling those who prefer one mechanism/client to another, > yet maintaining a single 'community'. > Yes, this is important I think. Plus, if possible, if it's decided to move to a forum format make that accessible by E-Mail. -- Chris Green · -- https://mail.python.org/mailman/listinfo/python-list
Re: Mailing-Lists (pointer)
Cameron Simpson wrote: > On 10Jan2023 08:45, Chris Green wrote: > >dn wrote: > >> See also the wisdom of enabling comp.lang.python and python-list as > >> 'mirrors', enabling those who prefer one mechanism/client to another, > >> yet maintaining a single 'community'. > >> > >Yes, this is important I think. Plus, if possible, if it's decided to > >move to a forum format make that accessible by E-Mail. > > There's a Discourse forum over at discuss.python.org. I use it in > "mailing list mode" and do almost all my interactions via email, exactly > as I do for python-list. Posts come to me and land in the same local > mail folder I use for python-list. My replies land on the forum as > expected (and of course also go by email to those members who have > turned that mode on). > > So I'm using both the new forum and the currently mailing list still, > and broadly in exactly the same way. > Yes, Discourse is one of the few web forums that also provides full E-Mail access. -- Chris Green · -- https://mail.python.org/mailman/listinfo/python-list
Re: Usenet vs. Mailing-list
Jon Ribbens wrote: > On 2023-01-28, Peter J. Holzer wrote: > > On 2023-01-27 21:04:58 +, Ben Bacarisse wrote: > >> It looks like you posted this question via Usenet. comp.lang.python is > >> essentially dead as a Usenet group. It exists, and gets NNTP versions > >> of mail sent to the mailing list, but nothing posted to the group via > >> NNTP get send on the mailing list. > > > > This is wrong. I did get Muttley's any your postings via the > > mailing-list. > > Yes, it's certainly false. I only ever post via the newsgroup, > and I can see my postings reach the list because they appear > in the list archive on the web. > As far as I am aware the mirroring of the Python mailing list on comp.lan.python works perfectly. I love gmane! :-) -- Chris Green · -- https://mail.python.org/mailman/listinfo/python-list
Re: Usenet vs. Mailing-list
Chris Green wrote: > Jon Ribbens wrote: > > On 2023-01-28, Peter J. Holzer wrote: > > > On 2023-01-27 21:04:58 +, Ben Bacarisse wrote: > > >> It looks like you posted this question via Usenet. comp.lang.python is > > >> essentially dead as a Usenet group. It exists, and gets NNTP versions > > >> of mail sent to the mailing list, but nothing posted to the group via > > >> NNTP get send on the mailing list. > > > > > > This is wrong. I did get Muttley's any your postings via the > > > mailing-list. > > > > Yes, it's certainly false. I only ever post via the newsgroup, > > and I can see my postings reach the list because they appear > > in the list archive on the web. > > > As far as I am aware the mirroring of the Python mailing list on > comp.lan.python works perfectly. I love gmane! :-) > That is, of course, comp.lang.python. Too much wine! :-) -- Chris Green · -- https://mail.python.org/mailman/listinfo/python-list
DeprecationWarning but replacement doesn't work
I am using Image from PIL and I'm getting a deprecation warning as follows:- /home/chris/bin/picShrink.py:80: DeprecationWarning: ANTIALIAS is deprecated and will be removed in Pillow 10 (2023-07-01). Use Resampling.LANCZOS instead. The code is very simple:- ... ... from PIL import Image ... ... im = Image.open(srcFile) im.thumbnail(size, Image.ANTIALIAS) im.save(dstFile, "JPEG") But if I change line 80 to:- im.thumbnail(size, Resampling.LANCZOS) I simply get an error as follows:- picShrink.py Traceback (most recent call last): File "/home/chris/bin/picShrink.py", line 80, in im.thumbnail(size, Resampling.LANCZOS) NameError: name 'Resampling' is not defined So, presumably there's more I need to change. Where can I find out what I need to do? -- Chris Green · -- https://mail.python.org/mailman/listinfo/python-list
Re: DeprecationWarning but replacement doesn't work
Roel Schroeven wrote: > Chris Green schreef op 4/02/2023 om 16:17: > > I am using Image from PIL and I'm getting a deprecation warning as > > follows:- > > > > /home/chris/bin/picShrink.py:80: DeprecationWarning: ANTIALIAS is > > deprecated > and will be removed in Pillow 10 (2023-07-01). Use Resampling.LANCZOS > instead. > > > > > > > But if I change line 80 to:- > > > > im.thumbnail(size, Resampling.LANCZOS) > > > > I simply get an error as follows:- > > > > picShrink.py > > Traceback (most recent call last): > >File "/home/chris/bin/picShrink.py", line 80, in > > im.thumbnail(size, Resampling.LANCZOS) > > NameError: name 'Resampling' is not defined > > > > > > So, presumably there's more I need to change. Where can I find out what > > I need to do? > There's some information about changes in constants in the release notes > for version 9.1.0, see > https://pillow.readthedocs.io/en/stable/releasenotes/9.1.0.html. At > first sight it looks like that indicates you have to do either > > from PIL import Image.Resampling as Resampling > ... > im.thumbnail(size, Resampling.LANCZOS) > > or > > from PIL import Image.Resampling > ... > im.thumbnail(size, Image.Resampling.LANCZOS) > > BUT according to the release notes of version 9.4.0 (see > https://pillow.readthedocs.io/en/stable/releasenotes/9.4.0.html#restored-image-constants) > > the constants in Image will remain (it's not clear to me if the ones in > Image.Resample will remain too). As far as I can see ANTIALIAS is still > deprecated in favor of LANCZOS though. All in all, it looks like the way > to go is to use Image.LANCZOS. In versions 9.1.0 up to but not including > 9.4.0 that will give you a deprecation warning but that can safely be > ignored. > Thank you Roel (and Chris in the previous reply) for your helpful explanations, it's all cleared up now. -- Chris Green · -- https://mail.python.org/mailman/listinfo/python-list
Looking for package/library to extract MP4 metadata
I'm looking for a Python (3) library to access (read only at present) the metadata in MP4 video files, in particular I want to get at dates and times. What's available to do this? Ideally something available in the Ubuntu repositories but I can install with PIP if necessary. -- Chris Green · -- https://mail.python.org/mailman/listinfo/python-list
Re: Looking for package/library to extract MP4 metadata
jak wrote: > rbowman ha scritto: > > On Sun, 9 Apr 2023 09:40:51 +0100, Chris Green wrote: > > > >> I'm looking for a Python (3) library to access (read only at present) > >> the metadata in MP4 video files, in particular I want to get at dates > >> and times. > >> > >> What's available to do this? Ideally something available in the Ubuntu > >> repositories but I can install with PIP if necessary. > > > > https://mutagen.readthedocs.io/en/latest/ > > > > I thought it only dealt about audio. That's why I hadn't thought it would help me as I'm after getting metadata from an MP4 video file but I guess the metadata format may be the same regardless of whether it's video or audio. -- Chris Green · -- https://mail.python.org/mailman/listinfo/python-list
Re: Looking for package/library to extract MP4 metadata
rbowman wrote: > On Sun, 9 Apr 2023 09:40:51 +0100, Chris Green wrote: > > > I'm looking for a Python (3) library to access (read only at present) > > the metadata in MP4 video files, in particular I want to get at dates > > and times. > > > > What's available to do this? Ideally something available in the Ubuntu > > repositories but I can install with PIP if necessary. > > https://mutagen.readthedocs.io/en/latest/ > Oh, OK, thanks. I already have mutagen installed on my systems because I use QuodLibet. I hadn't realised it would let me look at MP4 video files but I guess it should. -- Chris Green · -- https://mail.python.org/mailman/listinfo/python-list
Re: Looking for package/library to extract MP4 metadata
jak wrote: > Chris Green ha scritto: > > jak wrote: > >> rbowman ha scritto: > >>> On Sun, 9 Apr 2023 09:40:51 +0100, Chris Green wrote: > >>> > >>>> I'm looking for a Python (3) library to access (read only at present) > >>>> the metadata in MP4 video files, in particular I want to get at dates > >>>> and times. > >>>> > >>>> What's available to do this? Ideally something available in the Ubuntu > >>>> repositories but I can install with PIP if necessary. > >>> > >>> https://mutagen.readthedocs.io/en/latest/ > >>> > >> > >> I thought it only dealt about audio. > > > > That's why I hadn't thought it would help me as I'm after getting > > metadata from an MP4 video file but I guess the metadata format may be > > the same regardless of whether it's video or audio. > > > > Easiest way I found was run ffprobe command via popen. It can output the > information you need in json format which is easily readable with the > json library. > > command: > ffprobe -v warning -i "input.mp4" -show_streams -of json > python: > Popen(command, stderr=STDOUT, stdout=PIPE, encoding='utf8') > json: > json.loads(''.join(p.stdout.readlines())) > > It's easy to find a version of ffmpeg/ffprobe for every platform. Thank you, that worked straight away, ffprobe is installed on my systems already and I can probably just grep for the tag I want as all I'm looking for is the date of its creation which appears (twice) with the tag "creation_time". This is just to handle the occasional MP4 that a python program which basically manages JPEGs can't handle. It throws an exception so I can just get that to run a simple bash script to get the creation date. -- Chris Green · -- https://mail.python.org/mailman/listinfo/python-list
How to 'ignore' an error in Python?
I'm sure I'm missing something obvious here but I can't see an elegant way to do this. I want to create a directory, but if it exists it's not an error and the code should just continue. So, I have:- for dirname in listofdirs: try: os.mkdir(dirname) except FileExistsError: # so what can I do here that says 'carry on regardless' except: # handle any other error, which is really an error # I want code here to execute whether or not dirname exists Do I really have to use a finally: block? It feels rather clumsy. I suppose I could test if the directory exists before the os.mkdir() but again that feels a bit clumsy somehow. I suppose also I could use os.mkdirs() with exist_ok=True but again that feels vaguely wrong somehow. -- Chris Green · -- https://mail.python.org/mailman/listinfo/python-list
Re: How to 'ignore' an error in Python?
Kushal Kumaran wrote: > On Fri, Apr 28 2023 at 04:55:41 PM, Chris Green wrote: > > I'm sure I'm missing something obvious here but I can't see an elegant > > way to do this. I want to create a directory, but if it exists it's > > not an error and the code should just continue. > > > > So, I have:- > > > > for dirname in listofdirs: > > try: > > os.mkdir(dirname) > > except FileExistsError: > > # so what can I do here that says 'carry on regardless' > > except: > > # handle any other error, which is really an error > > > > # I want code here to execute whether or not dirname exists > > > > > > Do I really have to use a finally: block? It feels rather clumsy. > > > > I suppose I could test if the directory exists before the os.mkdir() > > but again that feels a bit clumsy somehow. > > > > I suppose also I could use os.mkdirs() with exist_ok=True but again > > that feels vaguely wrong somehow. > > > > Why does exist_ok=True feel wrong to you? This is exactly what it is > there for. > It was rather using os.mekedirs() to create a single directory that seemed wrong. If os.mkdir() had exist_ok=True than that would have been the obvious way to do it. -- Chris Green · -- https://mail.python.org/mailman/listinfo/python-list
Re: How to 'ignore' an error in Python?
Chris Angelico wrote: > On Sat, 29 Apr 2023 at 14:27, Kushal Kumaran wrote: > > > > On Fri, Apr 28 2023 at 04:55:41 PM, Chris Green wrote: > > > I'm sure I'm missing something obvious here but I can't see an elegant > > > way to do this. I want to create a directory, but if it exists it's > > > not an error and the code should just continue. > > > > > > So, I have:- > > > > > > for dirname in listofdirs: > > > try: > > > os.mkdir(dirname) > > > except FileExistsError: > > > # so what can I do here that says 'carry on regardless' > > > except: > > > # handle any other error, which is really an error > > > > > > # I want code here to execute whether or not dirname exists > > > > > > > > > Do I really have to use a finally: block? It feels rather clumsy. > > > > > > I suppose I could test if the directory exists before the os.mkdir() > > > but again that feels a bit clumsy somehow. > > > > > > I suppose also I could use os.mkdirs() with exist_ok=True but again > > > that feels vaguely wrong somehow. > > > > > > > Why does exist_ok=True feel wrong to you? This is exactly what it is > > there for. > > > > Using mkdirs when you only want to make one is inviting problems of > being subtly wrong, where it creates too many levels of directory. > Personally, I would just do: > > try: os.mkdir(dirname) > except FileExistsError: pass > > and not try to handle anything else at all. > Yes, OP here, that seems to me to be the 'right' way to do it. Basically I hadn't realised the effect of pass in a try block and that's why I asked the question originally. -- Chris Green · -- https://mail.python.org/mailman/listinfo/python-list
Problem with accented characters in mailbox.Maildir()
I have a custom mail filter in python that uses the mailbox package to open a mail message and give me access to the headers. So I have the following code to open each mail message:- # # # Read the message from standard input and make a message object from it # msg = mailbox.MaildirMessage(sys.stdin.buffer.read()) and then later I have (among many other bits and pieces):- # # # test for string in Subject: # if searchTxt in str(msg.get("subject", "unknown")): do various things This works exactly as intended most of the time but occasionally a message whose subject should match the test is missed. I have just realised when this happens, it's when the Subject: has accented characters in it (this is from a mailing list about canals in France). So, for example, the latest case of this happening has:- Subject: aka Marne à la Saône (Waterways Continental Europe) where the searchTxt in the code above is "Waterways Continental Europe". Is there any way I can work round this issue? E.g. is there a way to strip out all extended characters from a string? Or maybe it's msg.get() that isn't managing to handle the accented string correctly? Yes, I know that accented characters probably aren't allowed in Subject: but I'm not going to get that changed! :-) -- Chris Green · -- https://mail.python.org/mailman/listinfo/python-list
Re: Problem with accented characters in mailbox.Maildir()
A bit more information, msg.get("subject", "unknown") does return a string, as follows:- Subject: =?utf-8?Q?aka_Marne_=C3=A0_la_Sa=C3=B4ne_(Waterways_Continental_Europe)?= So it's the 'searchTxt in msg.get("subject", "unknown")' that's failing. I.e. for some reason 'in' isn't working when the searched string has utf-8 characters. Surely there's a way to handle this. -- Chris Green · -- https://mail.python.org/mailman/listinfo/python-list
Re: What do these '=?utf-8?' sequences mean in python?
Chris Green wrote: > I'm having a real hard time trying to do anything to a string (?) > returned by mailbox.MaildirMessage.get(). > What a twit I am :-) Strings are immutable, I have to do:- newstring = oldstring.replace("_", " ") Job done! -- Chris Green · -- https://mail.python.org/mailman/listinfo/python-list
Re: Problem with accented characters in mailbox.Maildir()
Chris Green wrote: > A bit more information, msg.get("subject", "unknown") does return a > string, as follows:- > > Subject: > =?utf-8?Q?aka_Marne_=C3=A0_la_Sa=C3=B4ne_(Waterways_Continental_Europe)?= > > So it's the 'searchTxt in msg.get("subject", "unknown")' that's > failing. I.e. for some reason 'in' isn't working when the searched > string has utf-8 characters. > > Surely there's a way to handle this. > ... and of course I now see the issue! The Subject: with utf-8 characters in it gets spaces changed to underscores. So searching for '(Waterways Continental Europe)' fails. I'll either need to test for both versions of the string or I'll need to change underscores to spaces in the Subject: returned by msg.get(). It's a long enough string that I'm searching for that I won't get any false positives. Sorry for the noise everyone, it's a typical case of explaining the problem shows one how to fix it! :-) -- Chris Green · -- https://mail.python.org/mailman/listinfo/python-list
Re: What do these '=?utf-8?' sequences mean in python?
Keith Thompson wrote: > Chris Green writes: > > Chris Green wrote: > >> I'm having a real hard time trying to do anything to a string (?) > >> returned by mailbox.MaildirMessage.get(). > >> > > What a twit I am :-) > > > > Strings are immutable, I have to do:- > > > > newstring = oldstring.replace("_", " ") > > > > Job done! > > Not necessarily. > > The subject in the original article was: > =?utf-8?Q?aka_Marne_=C3=A0_la_Sa=C3=B4ne_(Waterways_Continental_Europe)?= > > That's some kind of MIME encoding. Just replacing underscores by spaces > won't necessarily give you anything meaningful. (What if there are > actual underscores in the original subject line?) > > You should probably apply some kind of MIME-specific decoding. (I don't > have a specific suggestion for how to do that.) > Yes, OK, but my problem was that my filter looks for the string "Waterways Continental Europe" in the message Subject: to route the message to the appropriate mailbox. When the Subject: has accents the string becomes "Waterways_Continental_Europe" and thus the match fails. Simply changing all underscores back to spaces makes my test for "Waterways Continental Europe" work. The changed Subject: line gets thrown away after the test so I don't care about anything else getting changed. (When there are no accented characters in the Subject: the string is "Waterways Continental Europe" so I can't easily change the search text. I guess I could use an RE.) -- Chris Green · -- https://mail.python.org/mailman/listinfo/python-list
What do these '=?utf-8?' sequences mean in python?
I'm having a real hard time trying to do anything to a string (?) returned by mailbox.MaildirMessage.get(). I'm extracting the Subject: header from a message and, if I write what it returns to a log file using the python logging module what I see in the log file (when the Subject: has non-ASCII characters in it) is:- =?utf-8?Q?aka_Marne_=C3=A0_la_Sa=C3=B4ne_(Waterways_Continental_Europe)?= Whatever I try I am unable to change the underscore characters in the above string back to spaces. So, what do those =?utf-8? and ?= sequences mean? Are they part of the string or are they wrapped around the string on output as a way to show that it's utf-8 encoded? If I have the string in a variable how do I replace the underscores with spaces? Simply doing "subject.replace('_', ' ')" doesn't work, nothing happens at all. All I really want to do is throw the non-ASCII characters away as the string I'm trying to match in the subject is guaranteed to be ASCII. -- Chris Green · -- https://mail.python.org/mailman/listinfo/python-list
Re: What to use instead of nntplib?
Grizzy Adams wrote: > Tuesday, May 16, 2023 at 9:26, Alan Gauld wrote: > Re: What to use instead of nntplib? (at least in part) > > >On 15/05/2023 22:11, Grant Edwards wrote: > >> I got a nice warning today from the inews utility I use daily: > >> > >> DeprecationWarning: 'nntplib' is deprecated and slated for removal in > >> Python 3.13 > >> > >> What should I use in place of nntplib? > > >I'm curious as to why nntplib is deprecated? Surely there are still a > >lot of nntp servers around > > there must be this list is mirrored on one, and AFAICS some pythoners use > that > way to post (over the list) Yes, me for one, a good newsreader is really a wonderful way to manage technical 'lists' like this one. Usenet news is still very much alive though a minority interest now I suspect. -- Chris Green · -- https://mail.python.org/mailman/listinfo/python-list
Is there a Python module to parse a date like the 'date' command in Linux?
I'm converting a bash script to python as it has become rather clumsy in bash. However I have hit a problem with converting dates, the bash script has:- dat=$(date --date "$1" +"%Y/%m/%d") and this will accept almost anything reasonably sensible that can be interpreted as a date, in particular it accepts things like "tomorrow", "yesterday" and "next thursday". Is there anything similar in Python or would I be better off simply using os.system() to run date from the python program? -- Chris Green · -- https://mail.python.org/mailman/listinfo/python-list
Re: Is there a Python module to parse a date like the 'date' command in Linux?
Mike Dewhirst wrote: > [-- multipart/mixed, encoding 7bit, 22 lines --] > > [-- text/plain, encoding base64, charset: UTF-8, 16 lines --] > > On 21/05/2023 5:53 am, Chris Green wrote: > > I'm converting a bash script to python as it has become rather clumsy > > in bash. > > What is the use case? > A script I use to create diary entries, so it's very handy to be able to give the date as 'yesterday' or 'friday'. -- Chris Green · -- https://mail.python.org/mailman/listinfo/python-list
Re: why sqrt is not a built-in function?
Michael F. Stemper wrote: > On 14/01/2021 13.00, Rob Cliffe wrote: > > On 14/01/2021 17:44, Denys Contant wrote: > > >> I don't understand why sqrt is not a built-in function. > >> Why do we have to first import the function from the math module? > >> I use it ALL THE TIME! > > I agree that, especially if you have experience in other languages, this > > feels odd, and I have some sympathy for you. > > I cut my teeth on FORTRAN, which has SQRT() as part of the > language. At least 3, 4, and 5 did; I never used later versions. > Surely III, IV and V. I was definitely Fortran IV -- Chris Green · -- https://mail.python.org/mailman/listinfo/python-list
Re: How do you debug in Python? Coming from a Matlab and R user. I'm already aware of pdb.
Skip Montanaro wrote: > CW> How do you troubleshooting/debugging in Python? > > GE> Mostly I read exception trace and read the code and think about it. > GE> If that doesn't work, I add some print() or syslog() calls. > > CW> I know hardcore computer scientists would tell me about Python debugger. > > GE> I've been writing Python for 20+ years. I've never tried a debugger. > > Agree with Grant on these points. I certainly use gdb to debug C code > (like the interpreter), but for Python code, tracebacks and print > statements pretty much take care of things. I know pdb has been around > for a long while and many people like IDEs, but I've not generally > found them all that useful. I'm stuck in the 90s I guess. > Me too! :-) I tend to use multiple terminal windows so I have the code being edited in one window, run the code in another window and sometimes a third window for looking at configuration etc. Add print statement etc. for debugging. -- Chris Green · -- https://mail.python.org/mailman/listinfo/python-list
Is email.message.get() case insensitive for the header name?
It isn't clear from the documentation. Does email.message.get() care about the case of the header it's getting? I checking mailing list mails and the "List-Id:" header is a bit 'mixed', i.e. it can be List-Id:, or List-ID: or list-id:, will email.message.get("List-Id:", "unknown") find all of them? -- Chris Green · -- https://mail.python.org/mailman/listinfo/python-list
Change first occurrence of character x in a string - how?
What's the easiest way to change the first occurrence of a specified character in a string? E.g. I want to change linux-raid.vger.kernel.org to linux-r...@vger.kernel.org, it's a fairly general requirement of needing to change '.' to '@'. Alternatively is there an RE 'match' function that would test if linux-r...@vger.kernel.org matches linux-raid.vger.kernel.org? I don't really care if the '.' are all regarded as wild cards, the match will be accurate enough. -- Chris Green · -- https://mail.python.org/mailman/listinfo/python-list
Re: Change first occurrence of character x in a string - how?
Gary Herron wrote: > > On 2/14/21 1:14 PM, c...@isbd.net wrote: > > What's the easiest way to change the first occurrence of a specified > > character in a string? > > > > E.g. I want to change linux-raid.vger.kernel.org to > > linux-r...@vger.kernel.org, it's a fairly general requirement of > > needing to change '.' to '@'. > > > > Alternatively is there an RE 'match' function that would test if > > linux-r...@vger.kernel.org matches linux-raid.vger.kernel.org? I don't > > really care if the '.' are all regarded as wild cards, the match will > > be accurate enough. > > > The string type has a replace function that does what you want. > Except you can't change a string -- they are immutable -- so this > creates a new string. > > > >>> s = 'linux-raid.vger.kernel.org' > >>> new_s = s.replace('.', '@', 1) > >>> new_s > 'linux-r...@vger.kernel.org' > A new string is fine, thank you, that'll do what I need. Efficiency isn't a major issue with this. -- Chris Green · -- https://mail.python.org/mailman/listinfo/python-list
Re: Change first occurrence of character x in a string - how?
MRAB wrote: > On 2021-02-14 21:14, Chris Green wrote: > > What's the easiest way to change the first occurrence of a specified > > character in a string? > > > > E.g. I want to change linux-raid.vger.kernel.org to > > linux-r...@vger.kernel.org, it's a fairly general requirement of > > needing to change '.' to '@'. > > > > Alternatively is there an RE 'match' function that would test if > > linux-r...@vger.kernel.org matches linux-raid.vger.kernel.org? I don't > > really care if the '.' are all regarded as wild cards, the match will > > be accurate enough. > > > Use the .replace method. Its 3rd argument is the maximum number of > replacements, in this case 1: > > >>> s = 'linux-raid.vger.kernel.org' > >>> s.replace('.', '@', 1) > 'linux-r...@vger.kernel.org' Yes, thank you, simple! :-) -- Chris Green · -- https://mail.python.org/mailman/listinfo/python-list
Re: Is email.message.get() case insensitive for the header name?
Peter Otten <__pete...@web.de> wrote: > On 14/02/2021 21:50, Chris Green wrote: > > > It isn't clear from the documentation. Does email.message.get() care > > about the case of the header it's getting? > > > > I checking mailing list mails and the "List-Id:" header is a bit > > 'mixed', i.e. it can be List-Id:, or List-ID: or list-id:, will > > email.message.get("List-Id:", "unknown") find all of them? > Let's have a look: > > >>> import email.message > >>> email.message.get > Traceback (most recent call last): > File "", line 1, in > email.message.get > AttributeError: module 'email.message' has no attribute 'get' > > > OK, you probably meant > > >>> email.message.Message.get > > > > Enter the inspect module for a quick glance at the method's source: > > >>> import inspect > >>> print(inspect.getsource(email.message.Message.get)) > def get(self, name, failobj=None): > """Get a header value. > > Like __getitem__() but return failobj instead of None when the > field > is missing. > """ > name = name.lower() > for k, v in self._headers: > if k.lower() == name: > return self.policy.header_fetch_parse(k, v) > return failobj > > Both the `name` argument and the header keys are converted to lowercase > before the comparison, so yes, the method is case-insensitive (whether > it should be casefold() doesn't matter for ascii-strings). Excellent, thank you. -- Chris Green · -- https://mail.python.org/mailman/listinfo/python-list
I need some help interpreting this error
I'm running this using Python 3.7 on a Linux system. Most of the time (i.e. for a couple of days now) the program has been satifactorily delivering mail messages, hundreds of them. However one mail message has provoked the following error:- chris@cheddar$ tail mail.err Traceback (most recent call last): File "/home/chris/.mutt/bin/filter.py", line 95, in if sbstrip in msghdr["subject"]: TypeError: argument of type 'Header' is not iterable But msghdr["subject"] is surely just a string isn't it? Why is it complaining about something of type 'Header'? As I said the program has been running happily for a couple of days with no errors, I guess it must be something strange in a mail that has broken things - but what? Full program listed below:- #!/usr/bin/python3 # # # licenseApache v2 (http://www.apache.org/licenses/LICENSE-2.0) # author Chris Green - ch...@isbd.co.uk # # # # Mail filtering script # import mailbox import os import sys import time import mailLib import shlex # # # Redirect any exceptions to a file # sys.stderr = open("/home/chris/tmp/mail.err", 'a') # # # Some constants (i.e. configuration) # home = "/home/chris" logfile = home + "/tmp/mail.log" filtfile = home + "/.mutt/filter" mldir = home + "/mail/" indir = mldir + "In/" # # # Set to log to mail.log in ~/tmp with name 'filter' and the envelope/from # log = mailLib.initLog("filter") # # # Initialise destination mailbox name to its default "In/default" # dest = indir + "default" # # # Read the message from standard input and make a message object from it # msg = mailbox.MaildirMessage(sys.stdin.buffer.read()) # # # Extract various headers and the envelope/from # msghdr = {} msghdr["to"] = msg.get("To", "unknown").lower() msghdr["subject"] = msg.get("Subject", "unknown") msghdr["list-id"] = msg.get("List-Id", "unknown").lower() msghdr["list-post"] = msg.get("List-Post", "unknown").lower() msghdr["x-mailing-list"] = msg.get("X-Mailing-List", "unknown").lower() msghdr["unknown"] = "unknown" # # # See if there's a match in our filter file # f = open(filtfile, 'r') for ln in f:# for each line in filter if ln[0] == '#':# ignore comments continue # # # split the line into fields, shlex.split() does quoted strings, add a field # to create a dummy fourth field if there isn't one in the filter file # fld = shlex.split(ln) fld.append("") # # # copy the fields into better named variables # nm = fld[0] # name/alias destdir = fld[1] + "/" # the destination directory header = fld[2] # the header to find the match in address = fld[3].lower()# list address to match sbstrip = '[' + fld[4] + ']'# string to strip out of subject # # # Does the address in the header match this entry # if (address in msghdr[header]): # # # set the destination directory # dest = mldir + destdir + nm # # # Strip out list name (4th field) from subject if it's there # if sbstrip in msghdr["subject"]: msg.replace_header("Subject", msghdr["subject"].replace(sbstrip, '')) # # # we've found a match so assume we won't get another # break # # # deliver the message # mailLib.deliverMdMsg(dest, msg, log) -- Chris Green · -- https://mail.python.org/mailman/listinfo/python-list
Re: I need some help interpreting this error
2qdxy4rzwzuui...@potatochowder.com wrote: > On 2021-02-17 at 15:40:27 +, > Chris Green wrote: > > > I'm running this using Python 3.7 on a Linux system. > > > > Most of the time (i.e. for a couple of days now) the program has been > > satifactorily delivering mail messages, hundreds of them. However one > > mail message has provoked the following error:- > > > > chris@cheddar$ tail mail.err > > Traceback (most recent call last): > > File "/home/chris/.mutt/bin/filter.py", line 95, in > > if sbstrip in msghdr["subject"]: > > TypeError: argument of type 'Header' is not iterable > > > > > > But msghdr["subject"] is surely just a string isn't it? Why is it > > complaining about something of type 'Header'? > > Isn't it? ;-) > > First step: Print msghdr["subject"] and its type to know for sure. The > worst case is that you'll verify your assumption. > The documentation says "Headers are represented by customized subclasses of str", so it's a sub-class of str. > IIRC, the subject header is actually optional. Maybe someone sent a > message without a subject? Is msghdr["subject"] None? If you look at the code (and the documentation) if there's no subject header I'll get the string "unknown", I've also tried sending myself an E-Mail with no header and not provoked the error. -- Chris Green · -- https://mail.python.org/mailman/listinfo/python-list
Re: I need some help interpreting this error
Stefan Ram wrote: > Chris Green writes: > >But msghdr["subject"] is surely just a string isn't it? Why is it > >complaining about something of type 'Header'? > > What would you do to debug-print the type of an object? > I don't know, what would I do? :-) Without knowing what provokes the problem I could be waiting for days or weeks even before I see the error again. As I'd need to print the type for every message I'd get some big logs or I'd need to add a try: except to trap the specific error. -- Chris Green · -- https://mail.python.org/mailman/listinfo/python-list
Re: I need some help interpreting this error
2qdxy4rzwzuui...@potatochowder.com wrote: > On 2021-02-17 at 16:42:03 +, > Chris Green wrote: > > > 2qdxy4rzwzuui...@potatochowder.com wrote: > > > On 2021-02-17 at 15:40:27 +, > > > Chris Green wrote: > > > > > > > I'm running this using Python 3.7 on a Linux system. > > > > > > > > Most of the time (i.e. for a couple of days now) the program has been > > > > satifactorily delivering mail messages, hundreds of them. However one > > > > mail message has provoked the following error:- > > > > > > > > chris@cheddar$ tail mail.err > > > > Traceback (most recent call last): > > > > File "/home/chris/.mutt/bin/filter.py", line 95, in > > > > if sbstrip in msghdr["subject"]: > > > > TypeError: argument of type 'Header' is not iterable > > > > > > > > > > > > But msghdr["subject"] is surely just a string isn't it? Why is it > > > > complaining about something of type 'Header'? > > > > > > Isn't it? ;-) > > > > > > First step: Print msghdr["subject"] and its type to know for sure. The > > > worst case is that you'll verify your assumption. > > > > > The documentation says "Headers are represented by customized > > subclasses of str", so it's a sub-class of str. > > So we still don't know what the content of msghdr["subject"] is at the > time the error occurs. I don't mean to sound harsh, but that the > documentation and the code are correct, and that they match, remain > assumptions. Sometimes, seeing an actual value tells you what went > wrong (e.g., "oh, that's the sender's address, not the receiver's > address," "oh, that's my 'time' class, not the one from the standard > library"). > > The traceback tells you that msghdr["subject"] is of type Header. Is > Header a sub-class of str? > That's exactly what puzzled me! The line that gets the value is:- msghdr["subject"] = msg.get("Subject", "unknown") What I need to know is how that can return a value of type Header, and not a str. > Again, the worst case of looking at the value (whether in a log or in a > debugger) is that you verify your assumption. > > > > IIRC, the subject header is actually optional. Maybe someone sent a > > > message without a subject? Is msghdr["subject"] None? > > > > If you look at the code (and the documentation) if there's no subject > > header I'll get the string "unknown", I've also tried sending myself > > an E-Mail with no header and not provoked the error. > > That's good news about subject-less emails not generating an error, and > separate (possibly related) good news about your code handling an email > without a subject header. ;-) I think the only sane approach at the moment may be to add a try: except: and output some diagnostic information. Though there may still be an issue when trying to output the "what is it" object to the error log of course. -- Chris Green · -- https://mail.python.org/mailman/listinfo/python-list
Re: I need some help interpreting this error
Stefan Ram wrote: > Chris Green writes: > >chris@cheddar$ tail mail.err > >Traceback (most recent call last): > > File "/home/chris/.mutt/bin/filter.py", line 95, in > >if sbstrip in msghdr["subject"]: > >TypeError: argument of type 'Header' is not iterable > >But msghdr["subject"] is surely just a string isn't it? Why is it > >complaining about something of type 'Header'? > > I presume that the error message has been edited (abbreviated). > > In "if sbstrip in msghdr["subject"]:", there is no argument. > > But the error message says "argument of ...". > > When something that is not iterable is presented to a for loop, > the error message does not mention "argument": > I have output everything that appears, I've not changed it at all. It's the whole content of the file ~/tmp/mail.err as it's the only error that has occurred for the last day or so. The error log is created by the line:- sys.stderr = open("/home/chris/tmp/mail.err", 'a') So that's everything that was output to stderr. I think you are puzzled in the same way that I was, the error message doesn't make a lot of sense. > |>>> for i in 0: > |...print(i) > |... > |Traceback (most recent call last): > | File "", line 1, in > |TypeError: 'int' object is not iterable > > . > > -- Chris Green · -- https://mail.python.org/mailman/listinfo/python-list
Re: I need some help interpreting this error
Stestagg wrote: > I don't particularly like to encourage this shotgun help request because, > as previous commenter suggests, debugging this yourself is best. > > Sometimes debugging is super hard, and especially so when uncommon > situations occur, but it's always far easier to debug things when you have > visibility into the system under test. > > However, in this case, the email code is super complex, and this scenario > also looks very uncommon, but not unique: ( > https://github.com/Sydius/mbox-to-txt/issues/2), so you successfully > nerd-sniped me :). > > My *guess*, from reading the python standard library source code is that > you came across an email with some content in the subject line that is > considered a "surrogate", roughly, some badly encoded unicode or binary > data in it. > > When this happens, the code in some situations (depending on the policy...) > may return a header.Header() instance, rather than a > headerregistry.UniqueUnstructuredHeader > (which would have had a headerregistry.BaseHeader (mro: str) dynamically > attached). > > header.Header() does not inherit from str, and thus would throw the > traceback you observed. > Ah, thank you, a possible explanation. > Your suggestion of a try: catch: may make sense, alternately, you could > wrap the result in a call to str(): > > if sbstrip in str(msghdr["subject"]): > > which should attempt to encode the binary into some form of string object > for comparison (I haven't checked exactly what would happen, beyond: it > tries). > Yes, I did consider the str() approach but it feels a bit like making the problem go away without actually fixing it. However since the code in question is only 'cosmetic' (removing unwanted [list name] from the subject, it's not all *that* important to handle it properly. I just need to stop the error from killing my program. > It should be possible to create a test mbox with some funky bytes in the > subject, and try to reproduce it that way. > That's a point, with the clues you have given me I can try some 'bad' subject text and see if I can reproduce the error. Thanks again. -- Chris Green · -- https://mail.python.org/mailman/listinfo/python-list
Re: editor recommendations?
Cameron Simpson wrote: > On 03Mar2021 10:00, Lele Gaifax wrote: > >Cameron Simpson writes: > >> My fingers know vim. Some others' fingers know emacs. > > > >Emacs has also an Evil[1] mode, that mimics some vi/vim features. > > Whenever I've tries emulate-vi modes they tend to lack some coner case > known to my fingers. > I use [x]vile, "vi like Emacs". It's vi in terms of what the keys do (I can use vim and 'real' vi without issues) but it's built on an old eMacs engine and so has quite a few eMacs-like abiliities. For example it has a gnuclient type mode where it runs as a server and you can squirt files at it to be edited. It's available in most distributions and is actively maintained and developed still. -- Chris Green · -- https://mail.python.org/mailman/listinfo/python-list
Re: Proposal: Disconnect comp.lang.python from python-list
Paul Rubin wrote: > "Avi Gross" writes: > > Given some notice, what stops anyone from joining the mailing list before > > there is a divorce between the forums? > > Some of us don't want to be on mailing lists, and prefer using news > client software. I am in agreement here, if a list/group is available both on Usenet and as a mailing list then I will always use the Usenet/NNTP access as it is simply better for discussion groups like this. I do have a 'proper' threaded mail reader (mutt) that I use for reading lists but a 'proper' newreader that uses that extra facilities of NNTP is always going to be better. So my vote is to keep comp.lang.python as it is in terms of content, whether that means changing it to a moderated group or something else I don't mind. The current level of 'noise' is IMHO acceptable, if the people who deal with it now are OK to continue then let's not change anything. -- Chris Green · -- https://mail.python.org/mailman/listinfo/python-list
Re: Proposal: Disconnect comp.lang.python from python-list
Grant Edwards wrote: > On 2021-05-06, Chris Angelico wrote: > > On Thu, May 6, 2021 at 10:43 AM Avi Gross via Python-list > > wrote: > >> > >> Chris, > >> > >> Given some notice, what stops anyone from joining the mailing list before > >> there is a divorce between the forums? > > > > Nothing! Nothing at all. That is, if you're talking about *people*. > > People are absolutely welcome to join. The reason the S/N would > > improve is all the non-people who post to the newsgroup. > > > > But there are some people who simply don't want to use the mailing > > list, and that's currently a fully-welcomed option. I won't name > > names, but there definitely are people who would prefer a newsreader, > > Pointing a newsreader at news.gmane.io allows one to participate in > the mailing list just fine without using Usenet. > ??? Surely that *is* using Usenet, at least you're using NNTP which is the Usenet protocol. What's "not Usenet" about it? > Not that I support shutting down the Usenet/email gateway -- the > signal/noise ration seems fine to me. > On that I quite agree. :-) -- Chris Green · -- https://mail.python.org/mailman/listinfo/python-list
Re: Proposal: Disconnect comp.lang.python from python-list
Grant Edwards wrote: > On 2021-05-06, Chris Green wrote: > > Grant Edwards wrote: > > > >> Pointing a newsreader at news.gmane.io allows one to participate in > >> the mailing list just fine without using Usenet. > >> > > ??? Surely that *is* using Usenet, at least you're using NNTP which > > is the Usenet protocol. What's "not Usenet" about it? > > Usenet was a distributed network of computers that transferred > articles amongst themselves using various protocols, and provided > access to readers in various ways (NNTP being one of them). > Usenet *is* still this > Gmane was not and is not part of that network. It is a single, > stand-alone machine operating as an email list archiver/gateway that > provides access to read/post via NTTP. > It is effectively part of Usenet because the mailing lists it hosts and gateways to its newserver are peered with Usenet. I read several gmane 'lists' via usenet, I most certainly don't get them directly from gmane, I get them from other usenet servers. -- Chris Green · -- https://mail.python.org/mailman/listinfo/python-list
Re: Proposal: Disconnect comp.lang.python from python-list
Grant Edwards wrote: > On 2021-05-06, Chris Green wrote: > > Grant Edwards wrote: > >> On 2021-05-06, Chris Green wrote: > >> > Grant Edwards wrote: > >> > > >> >> Pointing a newsreader at news.gmane.io allows one to participate in > >> >> the mailing list just fine without using Usenet. > >> >> > >> > ??? Surely that *is* using Usenet, at least you're using NNTP which > >> > is the Usenet protocol. What's "not Usenet" about it? > >> > >> Usenet was a distributed network of computers that transferred > >> articles amongst themselves using various protocols, and provided > >> access to readers in various ways (NNTP being one of them). > >> > > Usenet *is* still this > > Good point. > > >> Gmane was not and is not part of that network. It is a single, > >> stand-alone machine operating as an email list archiver/gateway that > >> provides access to read/post via NTTP. > > > > It is effectively part of Usenet because the mailing lists it hosts > > and gateways to its newserver are peered with Usenet. > > I don't consider that as "being part of Usenet". Being part of Usenet > means that you peer with other the news hosts within Usenet, provide > the same group hiearchary (or some defined subset) and obey the normal > newsgroup control messages. > > > I read several gmane 'lists' via usenet, I most certainly don't get > > them directly from gmane, I get them from other usenet servers. > > Are you saying that the gmane news server is peered with other news > servers? And that you can read gmane.comp.python.general on other news > servers? That didn't used to be the case... > Yes, that's exactly what I'm saying. ... ah, but, you're probably right! Sorry. I've just looked at my leafnode configuration and I *have* got news.gmane.io in there. I must have done it so long ago that I'd forgotten all about it. -- Chris Green · -- https://mail.python.org/mailman/listinfo/python-list
How to iterate through maildir messages in python 3?
In python 2 one can do:- for msg in maildir: print msg # or whatever you want to do with the message However in python 3 this produces "TypeError: string argument expected, got 'bytes'". How should one iterate over a maildir in python3? (I've been here before but this problem is subtly different from the one I had before and I can't find the answer) -- Chris Green · -- https://mail.python.org/mailman/listinfo/python-list
Re: How to iterate through maildir messages in python 3?
Greg Ewing wrote: > On 25/06/21 7:06 am, Chris Green wrote: > > In python 2 one can do:- > > > > for msg in maildir: > >print msg # or whatever you want to do with the message > > > > > > However in python 3 this produces "TypeError: string argument > > expected, got 'bytes'". > > > > How should one iterate over a maildir in python3? > > You're already iterating over it just fine. Your problem is > actually how to *print* a mail message. > > The answer to this will depend on what your purpose is. If > you just want a rough-and-ready idea of what the message > contains, you could do this: > > print(repr(msg)) > > However, that won't work very well if the message doesn't > consist of mostly text in an ASCII-compatible encoding. > > If you want something better, Python comes with some standard > library code for dealing with mail messages. Check out the > 'email' module. > The error comes from the line "for msg in maildir:", not the print. Here's the full program where I'm encountering the error (yes, I should have posted this first time around) :- #!/usr/bin/python3 import mailbox import sys import email #open the existing maildir and the target mbox file maildir = mailbox.Maildir(sys.argv [-2], email.message_from_file) mbox = mailbox.mbox(sys.argv[-1]) #lock the mbox mbox.lock() #iterate over messages in the maildir and add to the mbox for msg in maildir: mbox.add(msg) #close and unlock mbox.close() maildir.close() (... and it's not my coding style, just copied) Here's the error trace:- chris$ ./md.py houseSitting fred Traceback (most recent call last): File "/home/chris/tmp/./md.py", line 18, in for msg in maildir: File "/usr/lib/python3.9/mailbox.py", line 110, in itervalues value = self[key] File "/usr/lib/python3.9/mailbox.py", line 77, in __getitem__ return self._factory(file) File "/usr/lib/python3.9/email/__init__.py", line 54, in message_from_file return Parser(*args, **kws).parse(fp) File "/usr/lib/python3.9/email/parser.py", line 56, in parse feedparser.feed(data) File "/usr/lib/python3.9/email/feedparser.py", line 175, in feed self._input.push(data) File "/usr/lib/python3.9/email/feedparser.py", line 103, in push self._partial.write(data) TypeError: string argument expected, got 'bytes' chris$ Line 18 is the "for msg in maildir:". Presumably the program worked as posted under python 2, all I have done is to change the shebang line to use python 3 (I no longer have python 2 on my system, otherwise I'd have used it as this is only a quick and dirty conversion script to be used once). -- Chris Green · -- https://mail.python.org/mailman/listinfo/python-list
Re: How to iterate through maildir messages in python 3?
Gilmeh Serda wrote: > On Fri, 25 Jun 2021 09:19:49 +0100, Chris Green wrote: > > > TypeError: string argument expected, got 'bytes' > > couple things comes to mind: > > 1. find py2 as archive, put it somewhere and run it from that > Hmm! :-) > 2. convert the bytes to str (find and replace) > How? It's failing at "for msg in maildir:", you can't change it to "for str(msg) in maildir" (well you can but it gives a syntax error). > 3. run the py2>py3 conversion script > That does nothing, says "No files need to be modified" -- Chris Green · -- https://mail.python.org/mailman/listinfo/python-list
Re: How to iterate through maildir messages in python 3?
Chris Angelico wrote: > On Sat, Jun 26, 2021 at 12:28 AM Chris Green wrote: > > > > Greg Ewing wrote: > > > On 25/06/21 7:06 am, Chris Green wrote: > > > > In python 2 one can do:- > > > > > > > > for msg in maildir: > > > >print msg # or whatever you want to do with the message > > > > > > > > > > > > However in python 3 this produces "TypeError: string argument > > > > expected, got 'bytes'". > > > > > > > > How should one iterate over a maildir in python3? > > > > > > You're already iterating over it just fine. Your problem is > > > actually how to *print* a mail message. > > > > > > The answer to this will depend on what your purpose is. If > > > you just want a rough-and-ready idea of what the message > > > contains, you could do this: > > > > > > print(repr(msg)) > > > > > > However, that won't work very well if the message doesn't > > > consist of mostly text in an ASCII-compatible encoding. > > > > > > If you want something better, Python comes with some standard > > > library code for dealing with mail messages. Check out the > > > 'email' module. > > > > > The error comes from the line "for msg in maildir:", not the print. > > > > Here's the full program where I'm encountering the error (yes, I > > should have posted this first time around) :- > > > > #!/usr/bin/python3 > > > > import mailbox > > import sys > > import email > > > > > > #open the existing maildir and the target mbox file > > maildir = mailbox.Maildir(sys.argv [-2], email.message_from_file) > > mbox = mailbox.mbox(sys.argv[-1]) > > > > #iterate over messages in the maildir and add to the mbox > > for msg in maildir: > > mbox.add(msg) > > > > Maildir says that the factory has to take a binary file object. It > looks like email.message_from_file is expecting a text file object, > but there's a very similar function message_from_binary_file that > might be what you want. > > Haven't tested it, but worth a try. > Even simpler, just remove email.message_from_file! :-) The following works fine:- #!/usr/bin/python3 import mailbox import sys import email #open the existing maildir and the target mbox file maildir = mailbox.Maildir(sys.argv [-2]) mbox = mailbox.mbox(sys.argv[-1]) #lock the mbox mbox.lock() #iterate over messages in the maildir and add to the mbox for msg in maildir: mbox.add(msg) #close and unlock mbox.close() maildir.close() Thank you for pointing me in the right direction. -- Chris Green · -- https://mail.python.org/mailman/listinfo/python-list
Re: How to iterate through maildir messages in python 3?
Dennis Lee Bieber wrote: > On Fri, 25 Jun 2021 09:19:49 +0100, Chris Green declaimed the > following: > > > > >Here's the full program where I'm encountering the error (yes, I > >should have posted this first time around) :- > > > >#!/usr/bin/python3 > > > >import mailbox > >import sys > >import email > > > > > >#open the existing maildir and the target mbox file > >maildir = mailbox.Maildir(sys.argv [-2], email.message_from_file) > > Is there some reason you are overriding whatever the default "factory" > is for Maildir? > Only that it was 'as copied' off the blog where I found it. As you can see elsewhere in the thread that was exactly the problem, simply removing the email.message_from_file fixed the error. -- Chris Green · -- https://mail.python.org/mailman/listinfo/python-list
Searching pypi.org, is there an 'advanced search'?
Every time I go to pypi.org to look for a neat utility or something I curse the stupid search. Is there really no better search available? Apart from anything else it appears to OR the elements together so, for example, I wanted to search for programs/libraries that will allow me to access Google Contacts:- Putting the words Google and Contacts in the search box produces more than 1 hits. Putting just Google in the search box produces 8,629 projects. So, it would appear the terms are OR'ed together, surely OR'ing terms in a search (by default) went out with the ark! When I put 'Google contacts' in the search box I want projects that have both 'Google' AND 'Contacts' in their description somewhere! (... and, no, I tried 'Google AND Contacts', still more than 1 results). pypi.org is a wonderful resource but its size now demands a better search engine. -- Chris Green · -- https://mail.python.org/mailman/listinfo/python-list
Re: Searching pypi.org, is there an 'advanced search'?
Peter Otten <__pete...@web.de> wrote: > On 18/07/2021 03:40, MRAB wrote: > > On 2021-07-17 13:01, Chris Green wrote: > > >> pypi.org is a wonderful resource but its size now demands a better > >> search engine. > >> > > There's always Google. I find that the search terms: > > > > "google contacts" pypi > > > > finds some results. > > With a small modification > > "google contacts" site:pypi.org > > the number of results goes down to 25 (of which 13 are shown by default). > But of course that only finds places where the words "google contacts" occur together. I think I (OP) should probably try:- google contacts site:pypi.org I don't use Google but I tried it on my preferrred search engine (DuckDuckGo) and it produces rather more than 25 results but, as they're sorted (among other things) by the distance between the words 'google' and 'contacts' (I think) it produces the sort of result I was hoping for. Thanks all. -- Chris Green · -- https://mail.python.org/mailman/listinfo/python-list
Re: Searching pypi.org, is there an 'advanced search'?
Mark Lawrence wrote: > On Saturday, July 17, 2021 at 1:03:21 PM UTC+1, Chris Green wrote: > > Every time I go to pypi.org to look for a neat utility or something I > > curse the stupid search. > > > > Is there really no better search available? Apart from anything else > > it appears to OR the elements together so, for example, I wanted to > > search for programs/libraries that will allow me to access Google > > Contacts:- > > > > Putting the words Google and Contacts in the search box produces > > more than 1 hits. > > > > Putting just Google in the search box produces 8,629 projects. > > > > So, it would appear the terms are OR'ed together, surely OR'ing terms > > in a search (by default) went out with the ark! > > > > When I put 'Google contacts' in the search box I want projects that > > have both 'Google' AND 'Contacts' in their description somewhere! > > (... and, no, I tried 'Google AND Contacts', still more than 1 > > results). > > > > pypi.org is a wonderful resource but its size now demands a better > > search engine. > > > > As this is an open source project I'm looking forward to the huge amount > of work that you personally will be putting in to remedy this situation. > In summary put up or shut up :) I'm quite happy to help, however I was just asking (maybe rather more agressively than I should have done) if I was missing something obvious. E.g. is there some syntax that I don't know about that I can use in the search box, or maybe I should use an external search engine? ... and Open Source doesn't mean one can't criticise surely, rather the opposite! :-) (I have, by the way, contributed in a minor way to quite a few open source projects over the years, and that's a lot of years! I even have a small credit in the 'bible' for the Kermit file transfer protocol, remember that?) -- Chris Green · -- https://mail.python.org/mailman/listinfo/python-list
Where to keep local Python modules?
This isn't a question about how to set PYTHONPATH so that Python code can find imported modules, it's about what is a sensible layout for one's home directory - i.e. where to put Python modules. I'm running Linux and have a number of Python modules that are only used by my own code. My top level Python code is all in ~/bin. I'd prefer to separate the modules so that they don't clutter the name space. Currently I have my Python modules in a subdirectory of ~/bin and my Python path is set as:- PYTHONPATH=/home/chris/bin/pymods Is this a reasonable approach? Is there a 'standard' name for the directory containing modules, or a standard place for it? (I don't mean a system-wide standard place, I mean a 'my' standard place). -- Chris Green · -- https://mail.python.org/mailman/listinfo/python-list
Re: Where to keep local Python modules?
Roland Mueller wrote: > Hello, > > pe 23. heinäk. 2021 klo 21.44 Chris Green (c...@isbd.net) kirjoitti: > > > This isn't a question about how to set PYTHONPATH so that Python code > > can find imported modules, it's about what is a sensible layout for > > one's home directory - i.e. where to put Python modules. > > > > I'm running Linux and have a number of Python modules that are only > > used by my own code. My top level Python code is all in ~/bin. I'd > > prefer to separate the modules so that they don't clutter the name > > space. > > > > Currently I have my Python modules in a subdirectory of ~/bin and my > > Python path is set as:- > > > > PYTHONPATH=/home/chris/bin/pymods > > > > Is this a reasonable approach? Is there a 'standard' name for the > > directory containing modules, or a standard place for it? (I don't > > mean a system-wide standard place, I mean a 'my' standard place). > > > > Under recent Linux distros there is $HOME/.local/ containing bin and lib > directories. May be it's ok to use this for user-specific python settings > under Linux. > > E.g. in .bashrc > export PYTHONPATH=$HOME/.local/python > > Result can be checked then in Python: > python > >>> import sys > >>> sys.path > ['', '/home/someuser/.local/lib/python', '/usr/lib64/python39.zip', > '/usr/lib64/python3.9', '/usr/lib64/python3.9/lib-dynload', > '/usr/local/lib/python3.9/site-packages', > '/usr/lib64/python3.9/site-packages', '/usr/lib/python3.9/site-packages'] > That's certainly a possibility, thanks. -- Chris Green · -- https://mail.python.org/mailman/listinfo/python-list
Re: Where to keep local Python modules?
Cameron Simpson wrote: > > Almost everything I use comes either from pip or from my own modules. My > $PYTHONPATH on the Mac has this: > > /Users/cameron/lib/python:/Users/cameron/rc/python > > being, respectively, my personal modules and a place for third party > modules which do not come from pip. That latter is an empty set, but > it's there. I doubt you'd need to bother with the latter; I doubt I need > to either. > Yes, I think maybe something in a 'lib' directory somewhere makes sense. I like to keep my home directory as uncluttered as possible though so maybe I'll keep the lib directory under ~/.config or ~/.local. -- Chris Green · -- https://mail.python.org/mailman/listinfo/python-list
Why is there no get_body() in mailbox.Maildir?
I have the following error:- chris@esprimo$ ./showmail.py boating/Odin/moorings/angers "01 Oct 2009 18:03:00" Traceback (most recent call last): File "/home/chris/dev/cgi-bin/./showmail.py", line 24, in print(msg.get_body('plain',)) AttributeError: 'MaildirMessage' object has no attribute 'get_body' It's being returned by the following (rather trivial) Python program:- #!/usr/bin/python3 # # # replacement for complex Zend/PHP E-Mail message extraction # import mailbox import email import sys import os mbroot = "/home/chris/mail/folder" mbname = os.path.join(mbroot, sys.argv[1]) mb = mailbox.Maildir(mbname) # # # run through the messages in the mailbox looking for date/time match # for msg in mb: # print(msg.get("Subject")) # print(msg.get("Date")) if sys.argv[2] in msg.get("Date"): print(msg.get_body('plain',)) exit(0) exit(1) I thought MaildirMessage inherited from email.message, it has inherited the get() method so why not the get_body() method? I'm running Python 3.9.7 on xubuntu Linux 21.10. Is there another way of doing this or do I have to sort through the message parts and stuff? It sounded like get_body() would make things easier. -- Chris Green · -- https://mail.python.org/mailman/listinfo/python-list
How to decode ISO8859-1 in Python 3?
This sounds as if it should be trivial but searching only seems to produce ways ofd doing it in Python 2. I have some text files which are ISO8859-1 encoded and I want to output them to screen using Python. -- Chris Green · -- https://mail.python.org/mailman/listinfo/python-list
Re: How to decode ISO8859-1 in Python 3?
Stefan Ram wrote: > Chris Green writes: > >I have some text files which are ISO8859-1 encoded and I want to output > >them to screen using Python. > > Well, the first attempt would be: > > import pathlib > name = r"C:\example.txt" > encoding = r"iso8859-1" > path = pathlib.Path( name ) > with path.open( encoding=encoding, errors="ignore" )as file: >print( file.read() ) > > , but if you write to a console which needs a special encoding, > you might need: > > sys.stdout.buffer.write( file.read().encode( 'your screen encoding' )) > > (which is not always possible) instead of "print( file.read())", > and "import sys" at the start of the script. > Thanks! It's rather messy isn't it! :-) I think I'll try converting the files before letting Python loose on them, iconv should manage that. -- Chris Green · -- https://mail.python.org/mailman/listinfo/python-list
Re: Why is there no get_body() in mailbox.Maildir?
Stefan Ram wrote: > Chris Green writes: > >I thought MaildirMessage inherited from email.message, > > It inherits from mailbox.Message which inherits from > email.message.Message which has no "get_body" method. > So the documentation at:- https://docs.python.org/3/library/email.message.html is very confusing then. It has a get_body method listed in the email.message.EmailMessage class. Where is email.message.Message documented? > email.message.MIMEPart has a get_body method. > > Also check out: get_payload. > > -- Chris Green · -- https://mail.python.org/mailman/listinfo/python-list
Odd locale error that has disappeared on reboot.
I have a very short Python program that runs on one of my Raspberry Pis to collect temperatures from a 1-wire sensor and write them to a database:- #!/usr/bin/python3 # # # read temperature from 1-wire sensor and store in database with date and time # import sqlite3 import time ftxt = str(open("/sys/bus/w1/devices/28-01204e1e64c3/w1_slave").read(100)) temp = (float(ftxt[ftxt.find("t=") +2:]))/1000 # # # insert date, time and temperature into the database # tdb = sqlite3.connect("/home/chris/.cfg/share/temperature/temperature.db") cr = tdb.cursor() dt = time.strftime("%Y-%m-%d %H:%M") cr.execute("Insert INTO temperatures (DateTime, Temperature) VALUES(?, round(?, 2))", (dt, temp) ) tdb.commit() tdb.close() It's run by cron every 10 minutes. At 03:40 last night it suddenly started throwing the following error every time it ran:- Fatal Python error: initfsencoding: Unable to get the locale encoding LookupError: unknown encoding: UTF-8 Current thread 0xb6f8db40 (most recent call first): Aborted Running the program from the command line produced the same error. Restarting the Pi system has fixed the problem. What could have caused this? I certainly wasn't around at 03:40! :-) There aren't any automatic updates enabled on the system, the only thing that might have been going on was a backup as that Pi is also my 'NAS' with a big USB drive connected to it. The backups have been running without problems for more than a year. Looking at the system logs shows that a backup was started at 03:35 so I suppose that *could* have provoked something but I fail to understand how. -- Chris Green · -- https://mail.python.org/mailman/listinfo/python-list
Re: Odd locale error that has disappeared on reboot.
Julio Di Egidio wrote: > On 07/12/2021 16:28, Chris Green wrote: > > I have a very short Python program that runs on one of my Raspberry > > Pis to collect temperatures from a 1-wire sensor and write them to a > > database:- > > > > #!/usr/bin/python3 > > # > > # > > # read temperature from 1-wire sensor and store in database with date > > and time > > # > > import sqlite3 > > import time > > > > ftxt = > > str(open("/sys/bus/w1/devices/28-01204e1e64c3/w1_slave").read(100)) > > temp = (float(ftxt[ftxt.find("t=") +2:]))/1000 > > # > > # > > # insert date, time and temperature into the database > > # > > tdb = > > sqlite3.connect("/home/chris/.cfg/share/temperature/temperature.db") > > cr = tdb.cursor() > > dt = time.strftime("%Y-%m-%d %H:%M") > > cr.execute("Insert INTO temperatures (DateTime, Temperature) VALUES(?, > round(?, 2))", (dt, temp) > > ) > > tdb.commit() > > tdb.close() > > > > It's run by cron every 10 minutes. > > > > > > At 03:40 last night it suddenly started throwing the following error every > > time it ran:- > > > > Fatal Python error: initfsencoding: Unable to get the locale encoding > > LookupError: unknown encoding: UTF-8 > > > > Current thread 0xb6f8db40 (most recent call first): > > Aborted > > > > Running the program from the command line produced the same error. > > Restarting the Pi system has fixed the problem. > > > > > > What could have caused this? I certainly wasn't around at 03:40! :-) > > There aren't any automatic updates enabled on the system, the only > > thing that might have been going on was a backup as that Pi is also > > my 'NAS' with a big USB drive connected to it. The backups have been > > running without problems for more than a year. Looking at the system > > logs shows that a backup was started at 03:35 so I suppose that *could* > > have provoked something but I fail to understand how. > > Since it's a one-off, doesn't sound like a system problem. The easiest > might be that you try-catch that call and retry when needed, and I'd > also check that 'ftxt' is what it should be: "external devices" may > fail, including when they do produce output... > Well it repeated every ten minutes through the night until I rebooted the system, so it wasn't really a "one off". I hasn't repeated since though. -- Chris Green · -- https://mail.python.org/mailman/listinfo/python-list
Re: Odd locale error that has disappeared on reboot.
Julio Di Egidio wrote: > On 08/12/2021 10:50, Chris Green wrote: > > Julio Di Egidio wrote: > >> On 07/12/2021 16:28, Chris Green wrote: > >>> What could have caused this? I certainly wasn't around at 03:40! :-) > >>> There aren't any automatic updates enabled on the system, the only > >>> thing that might have been going on was a backup as that Pi is also > >>> my 'NAS' with a big USB drive connected to it. The backups have been > >>> running without problems for more than a year. Looking at the system > >>> logs shows that a backup was started at 03:35 so I suppose that *could* > >>> have provoked something but I fail to understand how. > >> > >> Since it's a one-off, doesn't sound like a system problem. The easiest > >> might be that you try-catch that call and retry when needed, and I'd > >> also check that 'ftxt' is what it should be: "external devices" may > >> fail, including when they do produce output... > >> > > Well it repeated every ten minutes through the night until I rebooted > > the system, so it wasn't really a "one off". I hasn't repeated since > > though. > > Still your code wouldn't pass review: you do need some exception > handling there, that it's not failing right now is no excuse. Moreover, > next time it fails, you won't be any wiser since you keep not handling > it... Unless this is all just for fun, in which case of course never mind. > It is basically 'just for fun' we wanted to see how warm/cold the cats' home above our garage was now that winter has arrived. However catching and re-trying isn't going to help at all. It happily produced the same arror every 10 minutes throughout the night until I rebooted the system. I suppose I *could* reboot after (say) three failures but it seems a bit drastic! :-) -- Chris Green · -- https://mail.python.org/mailman/listinfo/python-list
Re: Odd locale error that has disappeared on reboot.
Inada Naoki wrote: > On Wed, Dec 8, 2021 at 2:52 AM Chris Green wrote: > > > > > > At 03:40 last night it suddenly started throwing the following error every > > time it ran:- > > > > Fatal Python error: initfsencoding: Unable to get the locale encoding > > LookupError: unknown encoding: UTF-8 > > > > Current thread 0xb6f8db40 (most recent call first): > > Aborted > > > > Running the program from the command line produced the same error. > > Restarting the Pi system has fixed the problem. > > > > This error means Python can not find its standard libraries. There are > some possibilities. > > * You set the wrong PYTHONHOME > PYTHONHOME is very rarely useful. It shouldn't be used if you can > not solve this kind of problem. > > * Your Python installation is broken. > Some files are deleted or overwritten. You need to *clean* install > Python again. > So how was it that rebooting the system fixed the error? It has been working perfectly OK now for a few days since the above failure and I've changed nothing. I think it was probably a bit of power supply noise or something that corrupted a memory read or write, or something of that sort anyway. -- Chris Green · -- https://mail.python.org/mailman/listinfo/python-list
Is there any way to make sense of these E-Mail subjects?
I have a Python 3 script which processes E-Mail caught in my hosting provider's 'catchall' mailbox. It looks for things that *might* be useful E-Mails, forwards them, and throws the rest away. I have a function which, given a header name, extracts the header and returns it as a string:- # # # Get a message header as a string # def getHdr(msg, header): return str("\n " + header + ": " + str(msg.get(header, "empty"))) msg is a mailbox.mboxMessage object. This is mostly working as expected, returning the header contents as strings so I can output them to my log files as necessary. However some Subject: lines are being returned like the following:- Subject: [SPAM] =?UTF-8?B?8J+TtyBKb2huIEJheHRlci1C?= =?UTF-8?B?cm93biByZWNlbnRseSBw?= =?UTF-8?B?b3N0ZWQgYSBuZXcgcGhv?= =?UTF-8?B?dG8=?= It looks like some sort of mis-encoding of UTF-8 strings, can anyone suggest what might be going on and/or a way to get some sense out of this? FWIW the above example is from "Facebook" so while it is probably (as indicated) [SPAM] it shouldn't be so illegible. At the moment I can't see an easy way to actually inspect the message as it's disappeared off somewhere else. I guess I could add some code to the script to send it to myself as well but if there's something obvious in the above it would avoid having to do this. -- Chris Green · -- https://mail.python.org/mailman/listinfo/python-list
Re: Is there any way to make sense of these E-Mail subjects?
Python wrote: > Chris Green wrote: > >Subject: [SPAM] =?UTF-8?B?8J+TtyBKb2huIEJheHRlci1C?= > > =?UTF-8?B?cm93biByZWNlbnRseSBw?= > > =?UTF-8?B?b3N0ZWQgYSBuZXcgcGhv?= > > =?UTF-8?B?dG8=?= > > > > It looks like some sort of mis-encoding of UTF-8 strings, can anyone > > suggest what might be going on and/or a way to get some sense out of > > this? > > It's not mis-encoding, it the standard way to embed non-ascii > characters in the header. ?UTF-8?B? means base64 encoded utf-8, > then the content is on multiple lines: > > $ base64 -d < t > 📷 John Baxter-Brown recently posted a new photo > > email.header.decode_header should be able to handle it. > Brilliant, thank you, just what I needed to know. -- Chris Green · -- https://mail.python.org/mailman/listinfo/python-list
How to test for maildir 'folder' in Python?
I have a script that walks a quite deep tree of mail messages to find and archive old messages. I'm trying to convert it from mbox to maildir (as I now store my mail in maildir format). So I need to test whether a point I have reached in the hierarchy is a maildir mailbox or not. Using mbox format it's easy because 'folders' are directories and mailboxes are files. However with maildir the 'folders' have directories within them so the simple tree walking goes down a level too far and finds 'folders' which aren't mailboxes called 'cur', 'new' and 'tmp'. Is there any 'ready made' way in python to tell whether a directory is a maildir mailbox? If not I suppose I'll simply have to check if there are 'cur', 'new' and 'tmp' directories within the directory which may or may not be a maildir. -- Chris Green · -- https://mail.python.org/mailman/listinfo/python-list
Re: How to test for maildir 'folder' in Python?
Cameron Simpson wrote: > On 22Jan2022 21:26, Chris Green wrote: > >So I need to test whether a point I have reached in the hierarchy is a > >maildir mailbox or not. Using mbox format it's easy because 'folders' > >are directories and mailboxes are files. However with maildir the > >'folders' have directories within them so the simple tree walking goes > >down a level too far and finds 'folders' which aren't mailboxes called > >'cur', 'new' and 'tmp'. > > > >Is there any 'ready made' way in python to tell whether a directory is > >a maildir mailbox? If not I suppose I'll simply have to check if > >there are 'cur', 'new' and 'tmp' directories within the directory > >which may or may not be a maildir. > > I was hoping that the provided mailbox/Maildir class had something, but > apparently not. > So was I! :-) It seems strange that something isn't provided as it's rather fundamental to handling maildir. > So I suggest fetching my cs.mailutils module from PyPI and using its > ismaildir(path) function, which does exactly what you describe. > Thanks Cameron. -- Chris Green · -- https://mail.python.org/mailman/listinfo/python-list
Re: How to test for maildir 'folder' in Python?
Barry Scott wrote: > > > > On 22 Jan 2022, at 21:26, Chris Green wrote: > > > > I have a script that walks a quite deep tree of mail messages to find > > and archive old messages. I'm trying to convert it from mbox to > > maildir (as I now store my mail in maildir format). > > > > So I need to test whether a point I have reached in the hierarchy is a > > maildir mailbox or not. Using mbox format it's easy because 'folders' > > are directories and mailboxes are files. However with maildir the > > 'folders' have directories within them so the simple tree walking goes > > down a level too far and finds 'folders' which aren't mailboxes called > > 'cur', 'new' and 'tmp'. > > > > Is there any 'ready made' way in python to tell whether a directory is > > a maildir mailbox? If not I suppose I'll simply have to check if > > there are 'cur', 'new' and 'tmp' directories within the directory > > which may or may not be a maildir. > > You do not need to walk the tree. > > The structure is > Maildir/cur, new, tmp - The INBOX > Maildir//cur, new, tmp > > The encoding prefixed with a "." and uses a "." between folder name parts. > > An example from my Maildir is ".Python.Users/" for a folder that client > shows as Python/Users > > You can see this by doing: > > ls -a ~/Maildir > You're talking about one specific (and IMHO not very good) way of creating a maildir hierarchy. Here's what I see on my system:- chris@esprimo$ cd mail /home/chris/mail chris@esprimo$ ls -a . .. Gm In Ju Li Tm folder chris@esprimo$ ls -a folder . apexLodge entertainment greville houseHome maxMum reshapers tractor ..archive family hardware internet money riding travel Scans boating france healthisbd pcc software ukraa Shopping buySellAdmin friendsholidays jrml personal telecoms vehicles chris@esprimo$ ls -a folder/Shopping . cabin cycling garden maxine pets stationery .. car diy giftsEtc maxmum photography telecoms apexLodge clothesEtc electrical grouponmedicineriding toolstation boat computerelectronics houseHome motorcycle screwfix books cpc food jrml music sportsEtc No dot prefixes and my maildir hierarchy is a *real* directory hierarchy so all the other file management utilities on my system can be used easily. -- Chris Green · -- https://mail.python.org/mailman/listinfo/python-list
Re: Best way to check if there is internet?
Chris Angelico wrote: > On Tue, 22 Feb 2022 at 19:33, Abdur-Rahmaan Janhangeer > wrote: > > > > As discussed here but, it would have been nevertheless great to have this > > tiny function instead of > > nothing > > > > Here's a function that determines whether or not you have an internet > connection. It's almost as reliable as some of the other examples > given - I know this, because I tried it ten times, and it gave the > correct result every time! > > def has_internet(): > return True > > Tell me, is it useful to have something that doesn't always give the > right answer, even if it usually does? Is there any value whatsoever > in a lie? > That's sort of in the same area as a stopped clock being right more often than one that runs just a bit slow (or fast). :-) -- Chris Green · -- https://mail.python.org/mailman/listinfo/python-list