Re: How to test characters of a string

2022-06-09 Thread Christian Gollwitzer

Am 08.06.22 um 19:57 schrieb De ongekruisigde:

On 2022-06-08, 2qdxy4rzwzuui...@potatochowder.com 
<2qdxy4rzwzuui...@potatochowder.com> wrote:

On 2022-06-09 at 04:15:46 +1000,
Chris Angelico  wrote:

If you insist:

 >>> s = 'nm-iodine:x:996:57::/var/empty:/run/current-system/sw/bin/nologin'
 >>> print(s.split(':'))
 ['nm-iodine', 'x', '996', '57', '', '/var/empty', 
'/run/current-system/sw/bin/nologin']

Hesitantly, because this is the Python mailing list, I claim (a) ':' is
simpler than r'([^:]*):([^:]*):(\d+):(\d+):([^:]*):([^:]*):(.*)$', and
(b) string.split covers pretty much the same edge cases as re.search.


Ah, but you don't catch the be numeric of fields (0-based) 2 and 3! But
agreed, it's not the best of examples.



Yes, that is a simplistic example, since the : can't even be quoted to 
appear in that format (which would require higher-order parsing than a 
simple split)


Fortunately, the OP has just given another requirement to recognise 
different patterns, and now it went into a direction where it will 
become quite ugly if you avoid REs or any other pattern matching tools.


This is also the main reason I suggested REs initially - often if you 
see other patterns in the data, you can easily adapt a RE solution, 
whereas you'll have to write the thing from ground up anew if you do it 
manually.


Christian

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


Re: How to test characters of a string

2022-06-09 Thread Christian Gollwitzer

Am 09.06.22 um 07:50 schrieb Dave:

Hi,

I’ve found you also need to take care of multiple disk CD releases. These have 
a format of

“1-01 Track Name”
“2-02  Trackl Name"

Meaning Disk 1 Track1, Disk 2, Track 2.

Also A and B Sides (from Vinyl LPs)

“A1-Track Name”
“B2-Track Name”

Side A, Track 1, etc.



Now you're getting into the complexity that is better handled by REs 
than by individual character examination.

The first of your formats matches the RE

\d-\d{2}

(one digit, - two digits). Anchor that to check for a match at the 
beginning.


The second one matches

(A|B)\d-

As long as one digit is enough. What is your goal, to extract these 
numbers or to strip them? Regexes can do both relatively easily.


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


Re: How to test characters of a string

2022-06-09 Thread Avi Gross via Python-list
Dave,

Sometimes a task is done faster by NOT programming anything in any language!

Not only have you spent a lot of your own time but many dozens of messages here 
have dragged in others, who gain nothing ;-)

The domain you are operating in seems to have lots of variants in how the 
titles are stored as names and you keep finding new variants. Yes, if your goal 
is to use this as a way to learn more in general about Python, clearly it may 
meet that goal!

Contrary to what I (and some others) said earlier, it may be time to consider 
regular expressions and other heavier artillery! LOL!

I do not plan on sticking with your twists and turns but will quickly address 
your variants.

Sides that come in two's like records are presumably limited to using A and B 
in your example. But multiple disks connected can mean the digit(s) following 
can have double digits or even more. Your python code may have to contain lots 
of functions you create that match some pattern and return some value and 
perhaps other functions that know how to convert from that format to a common 
canonical format of your own so they can be compared.

Your main code may need to try them in various sequences till it finds a match 
and so on.

But when you are done, in what format do you save them? The original or your 
minimal? 

Still confusing to me, as someone who does not give a darn, is the reality that 
many songs may have the same name but be different as in a song from Sinatra 
when he was young and a later recording  with a different orchestra or by a 
Sinatra imitator. They may all be titled something like "New York, New York" or 
"NEW YORK -- NEW YORK" which your algorithm folds into the same characters.

So I am guessing you also need to access other data about the person who sings 
it or what year it was released to make comparisons. At some point you may want 
to create or borrow some sort of class/object that encapsulates your data as 
well as methods that let you do things like make a canonical version of the 
Title and then a way to ask if Object A is reasonably equal to object B might 
happen if you define a function/method of __eq__ for that class.

It might take you years and need periodic refining as you encounter ever more 
varied ways people have chosen to label their music, but so what? LOL!

Humor or sarcasm aside, your incremental exploratory method reminds me why it 
is a good idea to first scope out the outlines of your problem space and make 
some key decisions and write out a fairly detailed set of requirements before 
seriously making more than prototypes. You might get very different help from 
people if they understood that your first request was far from complete but 
only one of many that may better be worked on some other way.
And I wonder if you did any search of the internet to see if anyone had done 
anything similar in Python (or another language) that may handle parts of what 
you need before asking here. I note lots of people who come with what they 
consider a good programming background have to adjust to aspects of languages 
like python as what they know is in some ways wrong or inadequate in a new 
environment. 

-Original Message-
From: Dave 
To: python-list@python.org
Sent: Thu, Jun 9, 2022 2:50 am
Subject: Re: How to test characters of a string

Hi,

I’ve found you also need to take care of multiple disk CD releases. These have 
a format of

“1-01 Track Name”
“2-02  Trackl Name"

Meaning Disk 1 Track1, Disk 2, Track 2.

Also A and B Sides (from Vinyl LPs)

“A1-Track Name”
“B2-Track Name”

Side A, Track 1, etc.

Cheers
Dave


> On 8 Jun 2022, at 19:36, Dennis Lee Bieber  wrote:
> 
> On Wed, 8 Jun 2022 01:53:26 + (UTC), Avi Gross 
> declaimed the following:
> 
> 
>> 
>> So is it necessary to insist on an exact pattern of two digits followed by a 
>> space? 
>> 
>> 
>> That would fail on "44 Minutes", "40 Oz. Dream", "50 Mission Cap", "50 Ways 
>> to Say Goodbye", "99 Ways to Die" 
>> 
>> It looks to me like you need to compare TWICE just in case. If it matches in 
>> the original (perhaps with some normalization of case and whitespace, fine. 
>> If not will they match if one or both have something to remove as a prefix 
>> such as "02 ". And if you are comparing items where the same song is in two 
>> different numeric sequences on different disks, ...
> 
>     I suspect the OP really needs to extract the /track number/ from the
> ID3 information, and (converting to a 2digit formatted string) see if the
> file name begins with that track number... The format of the those
> filenames appear to be those generated by some software when ripping CDs to
> MP3s -- for example:
> 
> -=-=-
> c:\Music\Roger Miller\All Time Greatest Hits>dir
> Volume in drive C is OS
> Vo

Re: How to test characters of a string

2022-06-09 Thread Dave
Hi,

I’ve found you also need to take care of multiple disk CD releases. These have 
a format of

“1-01 Track Name”
“2-02  Trackl Name"

Meaning Disk 1 Track1, Disk 2, Track 2.

Also A and B Sides (from Vinyl LPs)

“A1-Track Name”
“B2-Track Name”

Side A, Track 1, etc.

Cheers
Dave


> On 8 Jun 2022, at 19:36, Dennis Lee Bieber  wrote:
> 
> On Wed, 8 Jun 2022 01:53:26 + (UTC), Avi Gross 
> declaimed the following:
> 
> 
>> 
>> So is it necessary to insist on an exact pattern of two digits followed by a 
>> space? 
>> 
>> 
>> That would fail on "44 Minutes", "40 Oz. Dream", "50 Mission Cap", "50 Ways 
>> to Say Goodbye", "99 Ways to Die" 
>> 
>> It looks to me like you need to compare TWICE just in case. If it matches in 
>> the original (perhaps with some normalization of case and whitespace, fine. 
>> If not will they match if one or both have something to remove as a prefix 
>> such as "02 ". And if you are comparing items where the same song is in two 
>> different numeric sequences on different disks, ...
> 
>   I suspect the OP really needs to extract the /track number/ from the
> ID3 information, and (converting to a 2digit formatted string) see if the
> file name begins with that track number... The format of the those
> filenames appear to be those generated by some software when ripping CDs to
> MP3s -- for example:
> 
> -=-=-
> c:\Music\Roger Miller\All Time Greatest Hits>dir
> Volume in drive C is OS
> Volume Serial Number is 4ACC-3CB4
> 
> Directory of c:\Music\Roger Miller\All Time Greatest Hits
> 
> 04/11/2022  05:06 PM  .
> 04/11/2022  05:06 PM  ..
> 07/26/2018  11:20 AM 4,493,279 01 Dang Me.mp3
> 07/26/2018  11:20 AM 5,072,414 02 Chug-A-Lug.mp3
> 07/26/2018  11:20 AM 4,275,844 03 Do-Wacka-Do.mp3
> 07/26/2018  11:20 AM 4,284,208 04 In the Summertime.mp3
> 07/26/2018  11:20 AM 6,028,730 05 King of the Road.mp3
> 07/26/2018  11:20 AM 4,662,182 06 You Can't Roller Skate in a
> Buffalo Herd.mp3
> 07/26/2018  11:20 AM 5,624,704 07 Engine, Engine #9.mp3
> 07/26/2018  11:20 AM 5,002,492 08 One Dyin' and a Buryin'.mp3
> 07/26/2018  11:21 AM 6,799,224 09 Last Word in Lonesome Is Me.mp3
> 07/26/2018  11:21 AM 5,637,230 10 Kansas City Star.mp3
> 07/26/2018  11:21 AM 4,656,910 11 England Swings.mp3
> 07/26/2018  11:21 AM 5,836,638 12 Husbands and Wives.mp3
> 07/26/2018  11:21 AM 5,470,216 13 I've Been a Long Time Leavin'.mp3
> 07/26/2018  11:21 AM 6,230,236 14 Walkin' in the Sunshine.mp3
> 07/26/2018  11:21 AM 6,416,060 15 Little Green Apples.mp3
> 07/26/2018  11:21 AM 9,794,442 16 Me and Bobby McGee.mp3
> 07/26/2018  11:22 AM 7,330,642 17 Where Have All the Average People
> Gone.mp3
> 07/26/2018  11:22 AM 7,334,752 18 South.mp3
> 07/26/2018  11:22 AM 6,981,924 19 Tomorrow Night in Baltimore.mp3
> 07/26/2018  11:22 AM 9,353,872 20 River in the Rain.mp3
>  20 File(s)121,285,999 bytes
>   2 Dir(s)  295,427,198,976 bytes free
> 
> c:\Music\Roger Miller\All Time Greatest Hits>
> -=-=-
> 
>   Untested (especially the ID3 "variable" -- substitute variables as
> needed to match the original code):
> 
 id3Track = 2
 track_number = "%2.2d " % id3Track
 track_number
> '02 '
 filename = "02 This is the life.mp3"
 if filename.startswith(track_number):
> ...   nametitle = filename[3:]
> ... else:
> ...   nametitle = filename
> ...   
 if nametitle.endswith(".mp3"):
> ...   nametitle = nametitle[:-4]
> ...   
 nametitle
> 'This is the life'
> 
>   Handling ASCII ' and " vs Unicode "smart" quotes is a different matter.
> 
>   One may still run the risk of having a filename without a track number
> BUT having a number that just manages to match the track number. To account
> for that I'd suggest using the sequence:
> 
> * Strip extension (if filename.lower().endswith(".mp3"): ...)
> * Handle any Unicode/ASCII quotes in both filename AND ID3 track title
> * Compare filename and title.
> * IF MATCHED -- done
> * IF NOT MATCHED
> * Format ID3 track number as shown above
> * Compare filename to (formatted track number + track 
> title)
> * IF MATCHED -- done
> * IF NOT MATCHED
> * Log full filename and ID3 track 
> title/track number to a
> log for later examination.
> 
> 
> 
> -- 
>   Wulfraed Dennis Lee Bieber AF6VN
>   wlfr...@ix.netcom.comhttp://wlfraed.microdiversity.freeddns.org/
> -- 
> https://mail.python.org/mailman/listinfo/python-list

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


Re: How to test characters of a string

2022-06-08 Thread De ongekruisigde
On 2022-06-08, 2qdxy4rzwzuui...@potatochowder.com 
<2qdxy4rzwzuui...@potatochowder.com> wrote:
> On 2022-06-09 at 04:15:46 +1000,
> Chris Angelico  wrote:
>
>> On Thu, 9 Jun 2022 at 04:14, <2qdxy4rzwzuui...@potatochowder.com> wrote:
>> >
>> > On 2022-06-09 at 03:18:56 +1000,
>> > Chris Angelico  wrote:
>> >
>> > > On Thu, 9 Jun 2022 at 03:15, <2qdxy4rzwzuui...@potatochowder.com> wrote:
>> > > >
>> > > > On 2022-06-08 at 08:07:40 -,
>> > > > De ongekruisigde  wrote:
>> > > >
>> > > > > Depending on the problem a regular expression may be the much simpler
>> > > > > solution. I love them for e.g. text parsing and use them all the 
>> > > > > time.
>> > > > > Unrivaled when e.g. parts of text have to be extracted, e.g. from 
>> > > > > lines
>> > > > > like these:
>> > > > >
>> > > > >   root:x:0:0:System 
>> > > > > administrator:/root:/run/current-system/sw/bin/bash
>> > > > >   dhcpcd:x:995:991::/var/empty:/run/current-system/sw/bin/nologin
>> > > > >   nm-iodine:x:996:57::/var/empty:/run/current-system/sw/bin/nologin
>> > > > >   avahi:x:997:996:avahi-daemon privilege separation 
>> > > > > user:/var/empty:/run/current-system/sw/bin/nologin
>> > > > >   sshd:x:998:993:SSH privilege separation 
>> > > > > user:/var/empty:/run/current-system/sw/bin/nologin
>> > > > >   geoclue:x:999:998:Geoinformation 
>> > > > > service:/var/lib/geoclue:/run/current-system/sw/bin/nologin
>> > > > >
>> > > > > Compare a regexp solution like this:
>> > > > >
>> > > > >   >>> g = 
>> > > > > re.search(r'([^:]*):([^:]*):(\d+):(\d+):([^:]*):([^:]*):(.*)$' , s)
>> > > > >   >>> print(g.groups())
>> > > > >   ('geoclue', 'x', '999', '998', 'Geoinformation service', 
>> > > > > '/var/lib/geoclue', '/run/current-system/sw/bin/nologin')
>> > > > >
>> > > > > to the code one would require to process it manually, with all the 
>> > > > > edge
>> > > > > cases. The regexp surely reads much simpler (?).
>> > > >
>> > > > Uh...
>> > > >
>> > > > >>> import pwd # https://docs.python.org/3/library/pwd.html
>> > > > >>> [x for x in pwd.getpwall() if x[0] == 'geoclue']
>> > > > [pwd.struct_passwd(pw_name='geoclue', pw_passwd='x', pw_uid=992, 
>> > > > pw_gid=992, pw_gecos='Geoinformation service', 
>> > > > pw_dir='/var/lib/geoclue', pw_shell='/sbin/nologin')]
>> > >
>> > > That's great if the lines are specifically coming from your system's
>> > > own /etc/passwd, but not so much if you're trying to compare passwd
>> > > files from different systems, where you simply have the files
>> > > themselves.
>> >
>> > In addition to pwent to get specific entries from the local password
>> > database, POSIX has fpwent to get a specific entry from a stream that
>> > looks like /etc/passwd.  So even POSIX agrees that if you think you have
>> > to process this data manually, you're doing it wrong.  Python exposes
>> > neither functon directly (at least not in the pwd module or the os
>> > module; I didn't dig around or check PyPI).
>> 
>> So.. we can go find some other way of calling fpwent, or we can
>> just parse the file ourselves. It's a very VERY simple format.
>
> If you insist:
>
> >>> s = 
> 'nm-iodine:x:996:57::/var/empty:/run/current-system/sw/bin/nologin'
> >>> print(s.split(':'))
> ['nm-iodine', 'x', '996', '57', '', '/var/empty', 
> '/run/current-system/sw/bin/nologin']
>
> Hesitantly, because this is the Python mailing list, I claim (a) ':' is
> simpler than r'([^:]*):([^:]*):(\d+):(\d+):([^:]*):([^:]*):(.*)$', and
> (b) string.split covers pretty much the same edge cases as re.search.

Ah, but you don't catch the be numeric of fields (0-based) 2 and 3! But
agreed, it's not the best of examples.


-- 
 You're rewriting parts of Quake in *Python*?
 MUAHAHAHA
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: How to test characters of a string

2022-06-08 Thread Dennis Lee Bieber
On Wed, 8 Jun 2022 01:53:26 + (UTC), Avi Gross 
declaimed the following:


>
>So is it necessary to insist on an exact pattern of two digits followed by a 
>space? 
>
>
>That would fail on "44 Minutes", "40 Oz. Dream", "50 Mission Cap", "50 Ways to 
>Say Goodbye", "99 Ways to Die" 
>
>It looks to me like you need to compare TWICE just in case. If it matches in 
>the original (perhaps with some normalization of case and whitespace, fine. If 
>not will they match if one or both have something to remove as a prefix such 
>as "02 ". And if you are comparing items where the same song is in two 
>different numeric sequences on different disks, ...

I suspect the OP really needs to extract the /track number/ from the
ID3 information, and (converting to a 2digit formatted string) see if the
file name begins with that track number... The format of the those
filenames appear to be those generated by some software when ripping CDs to
MP3s -- for example:

-=-=-
c:\Music\Roger Miller\All Time Greatest Hits>dir
 Volume in drive C is OS
 Volume Serial Number is 4ACC-3CB4

 Directory of c:\Music\Roger Miller\All Time Greatest Hits

04/11/2022  05:06 PM  .
04/11/2022  05:06 PM  ..
07/26/2018  11:20 AM 4,493,279 01 Dang Me.mp3
07/26/2018  11:20 AM 5,072,414 02 Chug-A-Lug.mp3
07/26/2018  11:20 AM 4,275,844 03 Do-Wacka-Do.mp3
07/26/2018  11:20 AM 4,284,208 04 In the Summertime.mp3
07/26/2018  11:20 AM 6,028,730 05 King of the Road.mp3
07/26/2018  11:20 AM 4,662,182 06 You Can't Roller Skate in a
Buffalo Herd.mp3
07/26/2018  11:20 AM 5,624,704 07 Engine, Engine #9.mp3
07/26/2018  11:20 AM 5,002,492 08 One Dyin' and a Buryin'.mp3
07/26/2018  11:21 AM 6,799,224 09 Last Word in Lonesome Is Me.mp3
07/26/2018  11:21 AM 5,637,230 10 Kansas City Star.mp3
07/26/2018  11:21 AM 4,656,910 11 England Swings.mp3
07/26/2018  11:21 AM 5,836,638 12 Husbands and Wives.mp3
07/26/2018  11:21 AM 5,470,216 13 I've Been a Long Time Leavin'.mp3
07/26/2018  11:21 AM 6,230,236 14 Walkin' in the Sunshine.mp3
07/26/2018  11:21 AM 6,416,060 15 Little Green Apples.mp3
07/26/2018  11:21 AM 9,794,442 16 Me and Bobby McGee.mp3
07/26/2018  11:22 AM 7,330,642 17 Where Have All the Average People
Gone.mp3
07/26/2018  11:22 AM 7,334,752 18 South.mp3
07/26/2018  11:22 AM 6,981,924 19 Tomorrow Night in Baltimore.mp3
07/26/2018  11:22 AM 9,353,872 20 River in the Rain.mp3
  20 File(s)121,285,999 bytes
   2 Dir(s)  295,427,198,976 bytes free

c:\Music\Roger Miller\All Time Greatest Hits>
-=-=-

Untested (especially the ID3 "variable" -- substitute variables as
needed to match the original code):

>>> id3Track = 2
>>> track_number = "%2.2d " % id3Track
>>> track_number
'02 '
>>> filename = "02 This is the life.mp3"
>>> if filename.startswith(track_number):
... nametitle = filename[3:]
... else:
... nametitle = filename
... 
>>> if nametitle.endswith(".mp3"):
... nametitle = nametitle[:-4]
... 
>>> nametitle
'This is the life'

Handling ASCII ' and " vs Unicode "smart" quotes is a different matter.

One may still run the risk of having a filename without a track number
BUT having a number that just manages to match the track number. To account
for that I'd suggest using the sequence:

*   Strip extension (if filename.lower().endswith(".mp3"): ...)
*   Handle any Unicode/ASCII quotes in both filename AND ID3 track title
*   Compare filename and title.
*   IF MATCHED -- done
*   IF NOT MATCHED
*   Format ID3 track number as shown above
*   Compare filename to (formatted track number + track 
title)
*   IF MATCHED -- done
*   IF NOT MATCHED
*   Log full filename and ID3 track 
title/track number to a
log for later examination.



-- 
Wulfraed Dennis Lee Bieber AF6VN
wlfr...@ix.netcom.comhttp://wlfraed.microdiversity.freeddns.org/
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: How to test characters of a string

2022-06-08 Thread De ongekruisigde
On 2022-06-08, 2qdxy4rzwzuui...@potatochowder.com 
<2qdxy4rzwzuui...@potatochowder.com> wrote:
> On 2022-06-08 at 08:07:40 -,
> De ongekruisigde  wrote:
>
>> Depending on the problem a regular expression may be the much simpler
>> solution. I love them for e.g. text parsing and use them all the time.
>> Unrivaled when e.g. parts of text have to be extracted, e.g. from lines
>> like these:
>> 
>>   root:x:0:0:System administrator:/root:/run/current-system/sw/bin/bash
>>   dhcpcd:x:995:991::/var/empty:/run/current-system/sw/bin/nologin
>>   nm-iodine:x:996:57::/var/empty:/run/current-system/sw/bin/nologin
>>   avahi:x:997:996:avahi-daemon privilege separation 
>> user:/var/empty:/run/current-system/sw/bin/nologin
>>   sshd:x:998:993:SSH privilege separation 
>> user:/var/empty:/run/current-system/sw/bin/nologin
>>   geoclue:x:999:998:Geoinformation 
>> service:/var/lib/geoclue:/run/current-system/sw/bin/nologin
>> 
>> Compare a regexp solution like this:
>> 
>>   >>> g = re.search(r'([^:]*):([^:]*):(\d+):(\d+):([^:]*):([^:]*):(.*)$' , s)
>>   >>> print(g.groups())
>>   ('geoclue', 'x', '999', '998', 'Geoinformation service', 
>> '/var/lib/geoclue', '/run/current-system/sw/bin/nologin')
>> 
>> to the code one would require to process it manually, with all the edge
>> cases. The regexp surely reads much simpler (?).
>
> Uh...
>
> >>> import pwd # https://docs.python.org/3/library/pwd.html
> >>> [x for x in pwd.getpwall() if x[0] == 'geoclue']
> [pwd.struct_passwd(pw_name='geoclue', pw_passwd='x', pw_uid=992, 
> pw_gid=992, pw_gecos='Geoinformation service', pw_dir='/var/lib/geoclue', 
> pw_shell='/sbin/nologin')]

Yeah... Well, it was just an example and it must be clear by now I'm not
a Python programmer.

-- 
 You're rewriting parts of Quake in *Python*?
 MUAHAHAHA
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: How to test characters of a string

2022-06-08 Thread 2QdxY4RzWzUUiLuE
On 2022-06-09 at 04:15:46 +1000,
Chris Angelico  wrote:

> On Thu, 9 Jun 2022 at 04:14, <2qdxy4rzwzuui...@potatochowder.com> wrote:
> >
> > On 2022-06-09 at 03:18:56 +1000,
> > Chris Angelico  wrote:
> >
> > > On Thu, 9 Jun 2022 at 03:15, <2qdxy4rzwzuui...@potatochowder.com> wrote:
> > > >
> > > > On 2022-06-08 at 08:07:40 -,
> > > > De ongekruisigde  wrote:
> > > >
> > > > > Depending on the problem a regular expression may be the much simpler
> > > > > solution. I love them for e.g. text parsing and use them all the time.
> > > > > Unrivaled when e.g. parts of text have to be extracted, e.g. from 
> > > > > lines
> > > > > like these:
> > > > >
> > > > >   root:x:0:0:System 
> > > > > administrator:/root:/run/current-system/sw/bin/bash
> > > > >   dhcpcd:x:995:991::/var/empty:/run/current-system/sw/bin/nologin
> > > > >   nm-iodine:x:996:57::/var/empty:/run/current-system/sw/bin/nologin
> > > > >   avahi:x:997:996:avahi-daemon privilege separation 
> > > > > user:/var/empty:/run/current-system/sw/bin/nologin
> > > > >   sshd:x:998:993:SSH privilege separation 
> > > > > user:/var/empty:/run/current-system/sw/bin/nologin
> > > > >   geoclue:x:999:998:Geoinformation 
> > > > > service:/var/lib/geoclue:/run/current-system/sw/bin/nologin
> > > > >
> > > > > Compare a regexp solution like this:
> > > > >
> > > > >   >>> g = 
> > > > > re.search(r'([^:]*):([^:]*):(\d+):(\d+):([^:]*):([^:]*):(.*)$' , s)
> > > > >   >>> print(g.groups())
> > > > >   ('geoclue', 'x', '999', '998', 'Geoinformation service', 
> > > > > '/var/lib/geoclue', '/run/current-system/sw/bin/nologin')
> > > > >
> > > > > to the code one would require to process it manually, with all the 
> > > > > edge
> > > > > cases. The regexp surely reads much simpler (?).
> > > >
> > > > Uh...
> > > >
> > > > >>> import pwd # https://docs.python.org/3/library/pwd.html
> > > > >>> [x for x in pwd.getpwall() if x[0] == 'geoclue']
> > > > [pwd.struct_passwd(pw_name='geoclue', pw_passwd='x', pw_uid=992, 
> > > > pw_gid=992, pw_gecos='Geoinformation service', 
> > > > pw_dir='/var/lib/geoclue', pw_shell='/sbin/nologin')]
> > >
> > > That's great if the lines are specifically coming from your system's
> > > own /etc/passwd, but not so much if you're trying to compare passwd
> > > files from different systems, where you simply have the files
> > > themselves.
> >
> > In addition to pwent to get specific entries from the local password
> > database, POSIX has fpwent to get a specific entry from a stream that
> > looks like /etc/passwd.  So even POSIX agrees that if you think you have
> > to process this data manually, you're doing it wrong.  Python exposes
> > neither functon directly (at least not in the pwd module or the os
> > module; I didn't dig around or check PyPI).
> 
> So.. we can go find some other way of calling fpwent, or we can
> just parse the file ourselves. It's a very VERY simple format.

If you insist:

>>> s = 'nm-iodine:x:996:57::/var/empty:/run/current-system/sw/bin/nologin'
>>> print(s.split(':'))
['nm-iodine', 'x', '996', '57', '', '/var/empty', 
'/run/current-system/sw/bin/nologin']

Hesitantly, because this is the Python mailing list, I claim (a) ':' is
simpler than r'([^:]*):([^:]*):(\d+):(\d+):([^:]*):([^:]*):(.*)$', and
(b) string.split covers pretty much the same edge cases as re.search.
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: How to test characters of a string

2022-06-08 Thread Chris Angelico
On Thu, 9 Jun 2022 at 04:14, <2qdxy4rzwzuui...@potatochowder.com> wrote:
>
> On 2022-06-09 at 03:18:56 +1000,
> Chris Angelico  wrote:
>
> > On Thu, 9 Jun 2022 at 03:15, <2qdxy4rzwzuui...@potatochowder.com> wrote:
> > >
> > > On 2022-06-08 at 08:07:40 -,
> > > De ongekruisigde  wrote:
> > >
> > > > Depending on the problem a regular expression may be the much simpler
> > > > solution. I love them for e.g. text parsing and use them all the time.
> > > > Unrivaled when e.g. parts of text have to be extracted, e.g. from lines
> > > > like these:
> > > >
> > > >   root:x:0:0:System administrator:/root:/run/current-system/sw/bin/bash
> > > >   dhcpcd:x:995:991::/var/empty:/run/current-system/sw/bin/nologin
> > > >   nm-iodine:x:996:57::/var/empty:/run/current-system/sw/bin/nologin
> > > >   avahi:x:997:996:avahi-daemon privilege separation 
> > > > user:/var/empty:/run/current-system/sw/bin/nologin
> > > >   sshd:x:998:993:SSH privilege separation 
> > > > user:/var/empty:/run/current-system/sw/bin/nologin
> > > >   geoclue:x:999:998:Geoinformation 
> > > > service:/var/lib/geoclue:/run/current-system/sw/bin/nologin
> > > >
> > > > Compare a regexp solution like this:
> > > >
> > > >   >>> g = 
> > > > re.search(r'([^:]*):([^:]*):(\d+):(\d+):([^:]*):([^:]*):(.*)$' , s)
> > > >   >>> print(g.groups())
> > > >   ('geoclue', 'x', '999', '998', 'Geoinformation service', 
> > > > '/var/lib/geoclue', '/run/current-system/sw/bin/nologin')
> > > >
> > > > to the code one would require to process it manually, with all the edge
> > > > cases. The regexp surely reads much simpler (?).
> > >
> > > Uh...
> > >
> > > >>> import pwd # https://docs.python.org/3/library/pwd.html
> > > >>> [x for x in pwd.getpwall() if x[0] == 'geoclue']
> > > [pwd.struct_passwd(pw_name='geoclue', pw_passwd='x', pw_uid=992, 
> > > pw_gid=992, pw_gecos='Geoinformation service', pw_dir='/var/lib/geoclue', 
> > > pw_shell='/sbin/nologin')]
> >
> > That's great if the lines are specifically coming from your system's
> > own /etc/passwd, but not so much if you're trying to compare passwd
> > files from different systems, where you simply have the files
> > themselves.
>
> In addition to pwent to get specific entries from the local password
> database, POSIX has fpwent to get a specific entry from a stream that
> looks like /etc/passwd.  So even POSIX agrees that if you think you have
> to process this data manually, you're doing it wrong.  Python exposes
> neither functon directly (at least not in the pwd module or the os
> module; I didn't dig around or check PyPI).

So.. we can go find some other way of calling fpwent, or we can
just parse the file ourselves. It's a very VERY simple format.

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


Re: How to test characters of a string

2022-06-08 Thread 2QdxY4RzWzUUiLuE
On 2022-06-09 at 03:18:56 +1000,
Chris Angelico  wrote:

> On Thu, 9 Jun 2022 at 03:15, <2qdxy4rzwzuui...@potatochowder.com> wrote:
> >
> > On 2022-06-08 at 08:07:40 -,
> > De ongekruisigde  wrote:
> >
> > > Depending on the problem a regular expression may be the much simpler
> > > solution. I love them for e.g. text parsing and use them all the time.
> > > Unrivaled when e.g. parts of text have to be extracted, e.g. from lines
> > > like these:
> > >
> > >   root:x:0:0:System administrator:/root:/run/current-system/sw/bin/bash
> > >   dhcpcd:x:995:991::/var/empty:/run/current-system/sw/bin/nologin
> > >   nm-iodine:x:996:57::/var/empty:/run/current-system/sw/bin/nologin
> > >   avahi:x:997:996:avahi-daemon privilege separation 
> > > user:/var/empty:/run/current-system/sw/bin/nologin
> > >   sshd:x:998:993:SSH privilege separation 
> > > user:/var/empty:/run/current-system/sw/bin/nologin
> > >   geoclue:x:999:998:Geoinformation 
> > > service:/var/lib/geoclue:/run/current-system/sw/bin/nologin
> > >
> > > Compare a regexp solution like this:
> > >
> > >   >>> g = re.search(r'([^:]*):([^:]*):(\d+):(\d+):([^:]*):([^:]*):(.*)$' 
> > > , s)
> > >   >>> print(g.groups())
> > >   ('geoclue', 'x', '999', '998', 'Geoinformation service', 
> > > '/var/lib/geoclue', '/run/current-system/sw/bin/nologin')
> > >
> > > to the code one would require to process it manually, with all the edge
> > > cases. The regexp surely reads much simpler (?).
> >
> > Uh...
> >
> > >>> import pwd # https://docs.python.org/3/library/pwd.html
> > >>> [x for x in pwd.getpwall() if x[0] == 'geoclue']
> > [pwd.struct_passwd(pw_name='geoclue', pw_passwd='x', pw_uid=992, 
> > pw_gid=992, pw_gecos='Geoinformation service', pw_dir='/var/lib/geoclue', 
> > pw_shell='/sbin/nologin')]
> 
> That's great if the lines are specifically coming from your system's
> own /etc/passwd, but not so much if you're trying to compare passwd
> files from different systems, where you simply have the files
> themselves.

In addition to pwent to get specific entries from the local password
database, POSIX has fpwent to get a specific entry from a stream that
looks like /etc/passwd.  So even POSIX agrees that if you think you have
to process this data manually, you're doing it wrong.  Python exposes
neither functon directly (at least not in the pwd module or the os
module; I didn't dig around or check PyPI).

IMO, higher level functions to process such data is way better than a
[insert your own adjective/expletive here] regular expression that
collects the pieces into numbered groups rather than labeled fields.
Readability counts.

Yes, absolutely, use a regular expression when all else fails.  Don't
forget to handle all the edge cases!  (I assume that sane OSes preclude
colons in paths that are likely to come up in the local password
database, but I don't know what happens, e.g., when there's a reason for
GECOS to contain a colon.)
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: How to test characters of a string

2022-06-08 Thread Chris Angelico
On Thu, 9 Jun 2022 at 03:15, <2qdxy4rzwzuui...@potatochowder.com> wrote:
>
> On 2022-06-08 at 08:07:40 -,
> De ongekruisigde  wrote:
>
> > Depending on the problem a regular expression may be the much simpler
> > solution. I love them for e.g. text parsing and use them all the time.
> > Unrivaled when e.g. parts of text have to be extracted, e.g. from lines
> > like these:
> >
> >   root:x:0:0:System administrator:/root:/run/current-system/sw/bin/bash
> >   dhcpcd:x:995:991::/var/empty:/run/current-system/sw/bin/nologin
> >   nm-iodine:x:996:57::/var/empty:/run/current-system/sw/bin/nologin
> >   avahi:x:997:996:avahi-daemon privilege separation 
> > user:/var/empty:/run/current-system/sw/bin/nologin
> >   sshd:x:998:993:SSH privilege separation 
> > user:/var/empty:/run/current-system/sw/bin/nologin
> >   geoclue:x:999:998:Geoinformation 
> > service:/var/lib/geoclue:/run/current-system/sw/bin/nologin
> >
> > Compare a regexp solution like this:
> >
> >   >>> g = re.search(r'([^:]*):([^:]*):(\d+):(\d+):([^:]*):([^:]*):(.*)$' , 
> > s)
> >   >>> print(g.groups())
> >   ('geoclue', 'x', '999', '998', 'Geoinformation service', 
> > '/var/lib/geoclue', '/run/current-system/sw/bin/nologin')
> >
> > to the code one would require to process it manually, with all the edge
> > cases. The regexp surely reads much simpler (?).
>
> Uh...
>
> >>> import pwd # https://docs.python.org/3/library/pwd.html
> >>> [x for x in pwd.getpwall() if x[0] == 'geoclue']
> [pwd.struct_passwd(pw_name='geoclue', pw_passwd='x', pw_uid=992, 
> pw_gid=992, pw_gecos='Geoinformation service', pw_dir='/var/lib/geoclue', 
> pw_shell='/sbin/nologin')]

That's great if the lines are specifically coming from your system's
own /etc/passwd, but not so much if you're trying to compare passwd
files from different systems, where you simply have the files
themselves.

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


Re: How to test characters of a string

2022-06-08 Thread 2QdxY4RzWzUUiLuE
On 2022-06-08 at 08:07:40 -,
De ongekruisigde  wrote:

> Depending on the problem a regular expression may be the much simpler
> solution. I love them for e.g. text parsing and use them all the time.
> Unrivaled when e.g. parts of text have to be extracted, e.g. from lines
> like these:
> 
>   root:x:0:0:System administrator:/root:/run/current-system/sw/bin/bash
>   dhcpcd:x:995:991::/var/empty:/run/current-system/sw/bin/nologin
>   nm-iodine:x:996:57::/var/empty:/run/current-system/sw/bin/nologin
>   avahi:x:997:996:avahi-daemon privilege separation 
> user:/var/empty:/run/current-system/sw/bin/nologin
>   sshd:x:998:993:SSH privilege separation 
> user:/var/empty:/run/current-system/sw/bin/nologin
>   geoclue:x:999:998:Geoinformation 
> service:/var/lib/geoclue:/run/current-system/sw/bin/nologin
> 
> Compare a regexp solution like this:
> 
>   >>> g = re.search(r'([^:]*):([^:]*):(\d+):(\d+):([^:]*):([^:]*):(.*)$' , s)
>   >>> print(g.groups())
>   ('geoclue', 'x', '999', '998', 'Geoinformation service', 
> '/var/lib/geoclue', '/run/current-system/sw/bin/nologin')
> 
> to the code one would require to process it manually, with all the edge
> cases. The regexp surely reads much simpler (?).

Uh...

>>> import pwd # https://docs.python.org/3/library/pwd.html
>>> [x for x in pwd.getpwall() if x[0] == 'geoclue']
[pwd.struct_passwd(pw_name='geoclue', pw_passwd='x', pw_uid=992, 
pw_gid=992, pw_gecos='Geoinformation service', pw_dir='/var/lib/geoclue', 
pw_shell='/sbin/nologin')]
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: How to test characters of a string

2022-06-08 Thread Barry Scott


> On 7 Jun 2022, at 23:24, Dave  wrote:
> 
> Yes, it was probably just a typeo on my part.
> 
> I’ve now fixed the majority of cases but still got two strings that look 
> identical but fail to match, this time (again by 10cc), “I’m Mandy Fly Me”.
> 
> I’m putting money on it being a utf8 problem but I’m stuck on how to handle 
> it. It’s probably the single quote in I’m, although it has worked with other 
> songs.
> 
> Any ideas?

You can use difflib to give you a diff of the two strings:

:>>> print('\n'.join(difflib.unified_diff('abc', 'adc')))
---

+++

@@ -1,3 +1,3 @@

 a
-b
+d
 c
:>>>

The docs talk about lines, but difflib works on sequence. I use it a lot to find
differences within lines.

Barry



> 
> All the Best
> Cheers
> Dave
> 
> Here is the whole function/method or whatever it’s called in Python:
> 
> 
> #
> #   checkMusicFiles
> #
> 
> def checkMusicFiles(theBaseMusicLibraryFolder):
>myArtistDict = []
> 
> #
> #  Loop thru Artists Folder
> #
>myArtistsFoldlerList = getFolderList(theBaseMusicLibraryFolder)
>myArtistCount = 0
>for myArtistFolder in myArtistsFoldlerList:
>print('Artist: ' + myArtistFolder)
> #
> #  Loop thru Albums Folder
> #
>myAlbumList = getFolderList(theBaseMusicLibraryFolder + myArtistFolder)
>for myAlbum in myAlbumList:
>print('Album: ' + myAlbum)
> 
> #
> #  Loop thru Tracks (Files) Folder
> #
>myAlbumPath = theBaseMusicLibraryFolder + myArtistFolder + '/' + 
> myAlbum + '/'
>myFilesList = getFileList(myAlbumPath)
>for myFile in myFilesList:
>myFilePath = myAlbumPath + myFile
>myID3 = eyed3.load(myFilePath)
>if myID3 is None:
>continue
> 
>myArtistName = myID3.tag.artist
>if myArtistName is None:
>continue
> 
>myAlbumName = myID3.tag.album
>if myAlbumName is None:
>continue
> 
>myTitleName = myID3.tag.title
>if myTitleName is None:
>continue
> 
>myCompareFileName = myFile[0:-4]
>if myCompareFileName[0].isdigit() and 
> myCompareFileName[1].isdigit():
>myCompareFileName = myFile[3:-4]
> 
>if myCompareFileName != myTitleName:
>myLength1 = len(myCompareFileName)
>myLength2 = len(myTitleName)
>print('File Name Mismatch - Artist: [' + myArtistName + '] 
>  Album: ['+ myAlbumName + ']  Track: [' + myTitleName + ']  File: [' + 
> myCompareFileName + ']')
>if (myLength1 == myLength2):
>print('lengths match: ',myLength1)
>else:
>print('lengths mismatch: ',myLength1,'  ',myLength2)
> 
>print(' ')
> 
> 
> 
> 
>return myArtistsFoldlerList
> 
> 
> 
> 
> 
> 
>> On 8 Jun 2022, at 00:07, MRAB  wrote:
>> 
>> On 2022-06-07 21:23, Dave wrote:
>>> Thanks a lot for this! isDigit was the method I was looking for and 
>>> couldn’t find.
>>> I have another problem related to this, the following code uses the code 
>>> you just sent. I am getting a files ID3 tags using eyed3, this part seems 
>>> to work and I get expected values in this case myTitleName (Track name) is 
>>> set to “Deadlock Holiday” and myCompareFileName is set to “01 Deadlock 
>>> Holiday” (File Name with the Track number prepended). The is digit test 
>>> works and myCompareFileName is set to  “Deadlock Holiday”, so they should 
>>> match, right?
>> OT, but are you sure about that name? Isn't it "Dreadlock Holiday" (by 10cc)?
>> 
>> [snip]
>> -- 
>> https://mail.python.org/mailman/listinfo/python-list
> 
> -- 
> https://mail.python.org/mailman/listinfo/python-list

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


Re: How to test characters of a string

2022-06-08 Thread De ongekruisigde
On 2022-06-08, Christian Gollwitzer  wrote:
> Am 07.06.22 um 23:01 schrieb Christian Gollwitzer:
>
>>> In [3]: re.sub(r'^\d+\s*', '', s) Out[3]: 'Trinket'
>>>
>
> that RE does match what you intended to do, but not exactly what you 
> wrote in the OP. that would be '^\d\d.'  start with exactly two digits 
> followed by any character.

Indeed but then I'd like '\d{2}' even better.


>   Christian


-- 
 You're rewriting parts of Quake in *Python*?
 MUAHAHAHA
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: How to test characters of a string

2022-06-08 Thread De ongekruisigde
On 2022-06-08, Dave  wrote:
> I hate regEx and avoid it whenever possible, I’ve never found something that 
> was impossible to do without it.

I love regular expressions and use them where appropriate. Saves tons of
code and is often much more readable than the pages of code required to
do the same.

-- 
 You're rewriting parts of Quake in *Python*?
 MUAHAHAHA
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: How to test characters of a string

2022-06-08 Thread De ongekruisigde
On 2022-06-08, dn  wrote:
> On 08/06/2022 10.18, De ongekruisigde wrote:
>> On 2022-06-08, Christian Gollwitzer  wrote:
>>> Am 07.06.22 um 21:56 schrieb Dave:
 It depends on the language I’m using, in Objective C, I’d use isNumeric, 
 just wanted to know what the equivalent is in Python.

>>>
>>> Your problem is also a typical case for regular expressions. You can 
>>> create an expression for "starts with any number of digits plus optional 
>>> whitespace" and then replace this with nothing:
>> 
>> Regular expressions are overkill for this and much slower than the
>> simple isdigit based solution.
>
> ...
>
>> Regular expressions are indeeed extremely powerful and useful but I tend
>> to avoid them when there's a (faster) normal solution.
>
> Yes, simple solutions are (likely) easier to read.

Depending on the problem a regular expression may be the much simpler
solution. I love them for e.g. text parsing and use them all the time.
Unrivaled when e.g. parts of text have to be extracted, e.g. from lines
like these:

  root:x:0:0:System administrator:/root:/run/current-system/sw/bin/bash
  dhcpcd:x:995:991::/var/empty:/run/current-system/sw/bin/nologin
  nm-iodine:x:996:57::/var/empty:/run/current-system/sw/bin/nologin
  avahi:x:997:996:avahi-daemon privilege separation 
user:/var/empty:/run/current-system/sw/bin/nologin
  sshd:x:998:993:SSH privilege separation 
user:/var/empty:/run/current-system/sw/bin/nologin
  geoclue:x:999:998:Geoinformation 
service:/var/lib/geoclue:/run/current-system/sw/bin/nologin

Compare a regexp solution like this:

  >>> g = re.search(r'([^:]*):([^:]*):(\d+):(\d+):([^:]*):([^:]*):(.*)$' , s)
  >>> print(g.groups())
  ('geoclue', 'x', '999', '998', 'Geoinformation service', '/var/lib/geoclue', 
'/run/current-system/sw/bin/nologin')

to the code one would require to process it manually, with all the edge
cases. The regexp surely reads much simpler (?).


> RegEx-s are more powerful (and well worth learning for this reason), but
> are only 'readable' to those who use them frequently.
>
> Has either of you performed a timeit comparison?

No need: the isdigit solution doesn't require the overhead of a regex
processor.

-- 
 You're rewriting parts of Quake in *Python*?
 MUAHAHAHA
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: How to test characters of a string

2022-06-07 Thread Avi Gross via Python-list
Amazing how some people bring out the heavy artillery, first! LOL!

If the question was how to remove any initial digits and perhaps whitespace in 
a string, it is fairly easy to do without any functions to test if there are 
digits before the title. I mean look at initial characters and move forward if 
it is between '0' and '9' or a space. Duh!

Sure, a regular expression that matches anything following a run of digits and 
whitespace and before a ".MPG" or the end of the entry will be easy to extract 
and compare after removing any left/right whitespace in both things being 
compared and coercing both to the same case.

But the solution may be doomed to failure when it sees things like:
"100 Letters" 
"1+1" 
"10,000 hours"
"1 Trillion Dollar$" 
"2,000 Light Years From Home"


So is it necessary to insist on an exact pattern of two digits followed by a 
space? 


That would fail on "44 Minutes", "40 Oz. Dream", "50 Mission Cap", "50 Ways to 
Say Goodbye", "99 Ways to Die" 

It looks to me like you need to compare TWICE just in case. If it matches in 
the original (perhaps with some normalization of case and whitespace, fine. If 
not will they match if one or both have something to remove as a prefix such as 
"02 ". And if you are comparing items where the same song is in two different 
numeric sequences on different disks, ...




-----Original Message-
From: Christian Gollwitzer 
To: python-list@python.org
Sent: Tue, Jun 7, 2022 6:01 pm
Subject: Re: How to test characters of a string

Am 07.06.22 um 21:56 schrieb Dave:
> It depends on the language I’m using, in Objective C, I’d use isNumeric, just 
> wanted to know what the equivalent is in Python.
> 

Your problem is also a typical case for regular expressions. You can 
create an expression for "starts with any number of digits plus optional 
whitespace" and then replace this with nothing:

> chris@linux-tb9f:~> ipython
> Python 3.6.15 (default, Sep 23 2021, 15:41:43) [GCC]
> Type 'copyright', 'credits' or 'license' for more information
> IPython 7.13.0 -- An enhanced Interactive Python. Type '?' for help.
> 
> In [1]: import re                                                             
>                                                                               
>                              
> 
> In [2]: s='05 Trinket'                                                        
>                                                                               
>                             
> 
> In [3]: re.sub(r'^\d+\s*', '', s)                                             
>                                                                               
>                              
> Out[3]: 'Trinket'
> 

If it doesn't match, it will do nothing:

> In [4]: s='Es geht los'                                                       
>                                                                               
>                              
> 
> In [5]: re.sub(r'^\d+\s*', '', s)                                             
>                                                                               
>                              
> Out[5]: 'Es geht los'

Some people on this list don't like regexes but for tasks like this they 
are made and working well.

^ is "starts with"
\d is any digit
\s is any space
+ is at least one
* is nothing or one of

Christian




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


Re: How to test characters of a string

2022-06-07 Thread MRAB

On 2022-06-07 23:24, Dave wrote:

Yes, it was probably just a typeo on my part.


You've misspelled "typo"!


I’ve now fixed the majority of cases but still got two strings that look 
identical but fail to match, this time (again by 10cc), “I’m Mandy Fly Me”.


Try printing the asciified string:

>>> print(ascii("I’m Mandy Fly Me"))
'I\u2019m Mandy Fly Me'

What you typed above has "smart quotes". Maybe that's also the problem 
in the program: straight single quote/apodtrophe vs smart single quote.



I’m putting money on it being a utf8 problem but I’m stuck on how to handle it. 
It’s probably the single quote in I’m, although it has worked with other songs.

Any ideas?

All the Best
Cheers
Dave

Here is the whole function/method or whatever it’s called in Python:


#
#   checkMusicFiles
#

def checkMusicFiles(theBaseMusicLibraryFolder):
 myArtistDict = []

#
#  Loop thru Artists Folder
#
 myArtistsFoldlerList = getFolderList(theBaseMusicLibraryFolder)
 myArtistCount = 0
 for myArtistFolder in myArtistsFoldlerList:
 print('Artist: ' + myArtistFolder)
#
#  Loop thru Albums Folder
#
 myAlbumList = getFolderList(theBaseMusicLibraryFolder + myArtistFolder)
 for myAlbum in myAlbumList:
 print('Album: ' + myAlbum)

#
#  Loop thru Tracks (Files) Folder
#
 myAlbumPath = theBaseMusicLibraryFolder + myArtistFolder + '/' + 
myAlbum + '/'
 myFilesList = getFileList(myAlbumPath)
 for myFile in myFilesList:
 myFilePath = myAlbumPath + myFile
 myID3 = eyed3.load(myFilePath)
 if myID3 is None:
 continue

 myArtistName = myID3.tag.artist
 if myArtistName is None:
 continue

 myAlbumName = myID3.tag.album
 if myAlbumName is None:
 continue

 myTitleName = myID3.tag.title
 if myTitleName is None:
 continue

 myCompareFileName = myFile[0:-4]
 if myCompareFileName[0].isdigit() and 
myCompareFileName[1].isdigit():
 myCompareFileName = myFile[3:-4]

 if myCompareFileName != myTitleName:
 myLength1 = len(myCompareFileName)
 myLength2 = len(myTitleName)
 print('File Name Mismatch - Artist: [' + myArtistName + '] 
 Album: ['+ myAlbumName + ']  Track: [' + myTitleName + ']  File: [' + 
myCompareFileName + ']')
 if (myLength1 == myLength2):
 print('lengths match: ',myLength1)
 else:
 print('lengths mismatch: ',myLength1,'  ',myLength2)

 print(' ')




 return myArtistsFoldlerList


"myArtistsFoldlerList"?

And so many variables starting with "my". Not wrong; just ... :-)








On 8 Jun 2022, at 00:07, MRAB  wrote:

On 2022-06-07 21:23, Dave wrote:

Thanks a lot for this! isDigit was the method I was looking for and couldn’t 
find.
I have another problem related to this, the following code uses the code you 
just sent. I am getting a files ID3 tags using eyed3, this part seems to work 
and I get expected values in this case myTitleName (Track name) is set to 
“Deadlock Holiday” and myCompareFileName is set to “01 Deadlock Holiday” (File 
Name with the Track number prepended). The is digit test works and 
myCompareFileName is set to  “Deadlock Holiday”, so they should match, right?

OT, but are you sure about that name? Isn't it "Dreadlock Holiday" (by 10cc)?

[snip]




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


Re: How to test characters of a string

2022-06-07 Thread Dave
I hate regEx and avoid it whenever possible, I’ve never found something that 
was impossible to do without it.

> On 8 Jun 2022, at 00:49, dn  wrote:
> 
> On 08/06/2022 10.18, De ongekruisigde wrote:
>> On 2022-06-08, Christian Gollwitzer  wrote:
>>> Am 07.06.22 um 21:56 schrieb Dave:
 It depends on the language I’m using, in Objective C, I’d use isNumeric, 
 just wanted to know what the equivalent is in Python.
 
>>> 
>>> Your problem is also a typical case for regular expressions. You can 
>>> create an expression for "starts with any number of digits plus optional 
>>> whitespace" and then replace this with nothing:
>> 
>> Regular expressions are overkill for this and much slower than the
>> simple isdigit based solution.
> 
> ...
> 
>> Regular expressions are indeeed extremely powerful and useful but I tend
>> to avoid them when there's a (faster) normal solution.
> 
> Yes, simple solutions are (likely) easier to read.
> 
> RegEx-s are more powerful (and well worth learning for this reason), but
> are only 'readable' to those who use them frequently.
> 
> Has either of you performed a timeit comparison?
> -- 
> Regards,
> =dn
> -- 
> https://mail.python.org/mailman/listinfo/python-list

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


Re: How to test characters of a string

2022-06-07 Thread dn
On 08/06/2022 10.18, De ongekruisigde wrote:
> On 2022-06-08, Christian Gollwitzer  wrote:
>> Am 07.06.22 um 21:56 schrieb Dave:
>>> It depends on the language I’m using, in Objective C, I’d use isNumeric, 
>>> just wanted to know what the equivalent is in Python.
>>>
>>
>> Your problem is also a typical case for regular expressions. You can 
>> create an expression for "starts with any number of digits plus optional 
>> whitespace" and then replace this with nothing:
> 
> Regular expressions are overkill for this and much slower than the
> simple isdigit based solution.

...

> Regular expressions are indeeed extremely powerful and useful but I tend
> to avoid them when there's a (faster) normal solution.

Yes, simple solutions are (likely) easier to read.

RegEx-s are more powerful (and well worth learning for this reason), but
are only 'readable' to those who use them frequently.

Has either of you performed a timeit comparison?
-- 
Regards,
=dn
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: How to test characters of a string

2022-06-07 Thread Dave
rotfl! Nice one! 

> On 8 Jun 2022, at 00:24, 2qdxy4rzwzuui...@potatochowder.com wrote:
> 
> On 2022-06-07 at 23:07:42 +0100,
> Regarding "Re: How to test characters of a string,"
> MRAB  wrote:
> 
>> On 2022-06-07 21:23, Dave wrote:
>>> Thanks a lot for this! isDigit was the method I was looking for and 
>>> couldn’t find.
>>> 
>>> I have another problem related to this, the following code uses the code 
>>> you just sent. I am getting a files ID3 tags using eyed3, this part seems 
>>> to work and I get expected values in this case myTitleName (Track name) is 
>>> set to “Deadlock Holiday” and myCompareFileName is set to “01 Deadlock 
>>> Holiday” (File Name with the Track number prepended). The is digit test 
>>> works and myCompareFileName is set to  “Deadlock Holiday”, so they should 
>>> match, right?
>>> 
>> OT, but are you sure about that name? Isn't it "Dreadlock Holiday" (by
>> 10cc)?
> 
> Edsger Dijkstra originally wrote Deadlock Holiday for his band, The
> Semaphores.  10cc lost the race condition and had to change the lyrics.
> 
> Sorry.
> -- 
> https://mail.python.org/mailman/listinfo/python-list

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


Re: How to test characters of a string

2022-06-07 Thread Dave
Yes, it was probably just a typeo on my part.

I’ve now fixed the majority of cases but still got two strings that look 
identical but fail to match, this time (again by 10cc), “I’m Mandy Fly Me”.

I’m putting money on it being a utf8 problem but I’m stuck on how to handle it. 
It’s probably the single quote in I’m, although it has worked with other songs.

Any ideas?

All the Best
Cheers
Dave

Here is the whole function/method or whatever it’s called in Python:


#
#   checkMusicFiles
#

def checkMusicFiles(theBaseMusicLibraryFolder):
myArtistDict = []

#
#  Loop thru Artists Folder
#
myArtistsFoldlerList = getFolderList(theBaseMusicLibraryFolder)
myArtistCount = 0
for myArtistFolder in myArtistsFoldlerList:
print('Artist: ' + myArtistFolder)
#
#  Loop thru Albums Folder
#
myAlbumList = getFolderList(theBaseMusicLibraryFolder + myArtistFolder)
for myAlbum in myAlbumList:
print('Album: ' + myAlbum)

#
#  Loop thru Tracks (Files) Folder
#
myAlbumPath = theBaseMusicLibraryFolder + myArtistFolder + '/' + 
myAlbum + '/'
myFilesList = getFileList(myAlbumPath)
for myFile in myFilesList:
myFilePath = myAlbumPath + myFile
myID3 = eyed3.load(myFilePath)
if myID3 is None:
continue

myArtistName = myID3.tag.artist
if myArtistName is None:
continue

myAlbumName = myID3.tag.album
if myAlbumName is None:
continue

myTitleName = myID3.tag.title
if myTitleName is None:
continue

myCompareFileName = myFile[0:-4]
if myCompareFileName[0].isdigit() and 
myCompareFileName[1].isdigit():
myCompareFileName = myFile[3:-4]

if myCompareFileName != myTitleName:
myLength1 = len(myCompareFileName)
myLength2 = len(myTitleName)
print('File Name Mismatch - Artist: [' + myArtistName + ']  
Album: ['+ myAlbumName + ']  Track: [' + myTitleName + ']  File: [' + 
myCompareFileName + ']')
if (myLength1 == myLength2):
print('lengths match: ',myLength1)
else:
print('lengths mismatch: ',myLength1,'  ',myLength2)

print(' ')




return myArtistsFoldlerList






> On 8 Jun 2022, at 00:07, MRAB  wrote:
> 
> On 2022-06-07 21:23, Dave wrote:
>> Thanks a lot for this! isDigit was the method I was looking for and couldn’t 
>> find.
>> I have another problem related to this, the following code uses the code you 
>> just sent. I am getting a files ID3 tags using eyed3, this part seems to 
>> work and I get expected values in this case myTitleName (Track name) is set 
>> to “Deadlock Holiday” and myCompareFileName is set to “01 Deadlock Holiday” 
>> (File Name with the Track number prepended). The is digit test works and 
>> myCompareFileName is set to  “Deadlock Holiday”, so they should match, right?
> OT, but are you sure about that name? Isn't it "Dreadlock Holiday" (by 10cc)?
> 
> [snip]
> -- 
> https://mail.python.org/mailman/listinfo/python-list

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


Re: How to test characters of a string

2022-06-07 Thread dn
 It depends on the language I’m using, in Objective C, I’d use isNumeric, 
 just wanted to know what the equivalent is in Python.

 If you know the answer why don’t you just tell me and if you don’t, don’t 
 post!
>>>
>>> People ask home work questions here and we try to teach a student with 
>>> hints not finished answers.
>>> Your post was confused with a home work question.
>>
>> In the future, to make it look less like a homework question, show
>> your current code, which would provide context. Last I checked,
>> homework questions don't usually involve ID3 tags in MP3 files :)

Ah, so that's where I've seen it before!
(thanks for scratching my head @Chris - but watch-out for splinters!)

Yes, the problem has been used as a training exercise, eg same song but
in different albums/play-lists, different capitalisation, and such-like;
ie 'data cleaning' and harmonisation - good for use at the intersection
of Python and SQL (or NoSQL).


Knowing the background, and thus the particular need, would have saved a
lot of time - giving the answer as code (per one of the contributions)
would have taken considerably less effort than looking-up and citing the
docs.

Perhaps then, the 'learning-opportunity' is that when such questions pop
into one's mind, 'the docs' is *the* recommended first-call?


> The original question in this thread didn't say anything about MP3
> files.  Jumping to that conclusion from strings like '05 Trinket' was
> left as an exercise for the interested reader.  :-)

This reader's interest was to figure-out why "trinket" didn't refer to
some small decoration or 'bling', nor to a Python training tool
(https://trinket.io/), but to a music group/video series.
(even more-surprising: that this grey-beard recognised one of their tracks).


On the other side of the relationship, writers are expected to follow
the PSF Code of Conduct (https://www.python.org/psf/conduct/), eg
respect, acknowledgement, grace...

Such also encourages (positive) responses when asking future questions...


Now that you (@Dave) have revealed yourself as more than a raw-beginner,
and to have skills transferable to the Python world, it'll be great to
see you 'here', contributing to others' posts...
-- 
Regards,
=dn
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: How to test characters of a string

2022-06-07 Thread De ongekruisigde
On 2022-06-08, Christian Gollwitzer  wrote:
> Am 07.06.22 um 21:56 schrieb Dave:
>> It depends on the language I’m using, in Objective C, I’d use isNumeric, 
>> just wanted to know what the equivalent is in Python.
>> 
>
> Your problem is also a typical case for regular expressions. You can 
> create an expression for "starts with any number of digits plus optional 
> whitespace" and then replace this with nothing:

Regular expressions are overkill for this and much slower than the
simple isdigit based solution.


>> chris@linux-tb9f:~> ipython
>> Python 3.6.15 (default, Sep 23 2021, 15:41:43) [GCC]
>> Type 'copyright', 'credits' or 'license' for more information
>> IPython 7.13.0 -- An enhanced Interactive Python. Type '?' for help.
>> 
>> In [1]: import re
>>  
>>
>> 
>> In [2]: s='05 Trinket'   
>>  
>>
>> 
>> In [3]: re.sub(r'^\d+\s*', '', s)
>>  
>>
>> Out[3]: 'Trinket'
>> 
>
> If it doesn't match, it will do nothing:
>
>> In [4]: s='Es geht los'  
>>  
>>
>> 
>> In [5]: re.sub(r'^\d+\s*', '', s)
>>  
>>
>> Out[5]: 'Es geht los'
>
> Some people on this list don't like regexes but for tasks like this they 
> are made and working well.

Regular expressions are indeeed extremely powerful and useful but I tend
to avoid them when there's a (faster) normal solution.


> ^ is "starts with"
> \d is any digit
> \s is any space
> + is at least one
> * is nothing or one of
>
> Christian

-- 
 You're rewriting parts of Quake in *Python*?
 MUAHAHAHA
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: How to test characters of a string

2022-06-07 Thread Christian Gollwitzer

Am 07.06.22 um 23:01 schrieb Christian Gollwitzer:


In [3]: re.sub(r'^\d+\s*', '', s) Out[3]: 'Trinket'



that RE does match what you intended to do, but not exactly what you 
wrote in the OP. that would be '^\d\d.'  start with exactly two digits 
followed by any character.


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


Re: How to test characters of a string

2022-06-07 Thread Christian Gollwitzer

Am 07.06.22 um 21:56 schrieb Dave:

It depends on the language I’m using, in Objective C, I’d use isNumeric, just 
wanted to know what the equivalent is in Python.



Your problem is also a typical case for regular expressions. You can 
create an expression for "starts with any number of digits plus optional 
whitespace" and then replace this with nothing:



chris@linux-tb9f:~> ipython
Python 3.6.15 (default, Sep 23 2021, 15:41:43) [GCC]
Type 'copyright', 'credits' or 'license' for more information
IPython 7.13.0 -- An enhanced Interactive Python. Type '?' for help.

In [1]: import re

In [2]: s='05 Trinket'   

In [3]: re.sub(r'^\d+\s*', '', s)
Out[3]: 'Trinket'




If it doesn't match, it will do nothing:

In [4]: s='Es geht los'  

In [5]: re.sub(r'^\d+\s*', '', s)
Out[5]: 'Es geht los'


Some people on this list don't like regexes but for tasks like this they 
are made and working well.


^ is "starts with"
\d is any digit
\s is any space
+ is at least one
* is nothing or one of

Christian




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


Re: How to test characters of a string

2022-06-07 Thread 2QdxY4RzWzUUiLuE
On 2022-06-07 at 23:07:42 +0100,
Regarding "Re: How to test characters of a string,"
MRAB  wrote:

> On 2022-06-07 21:23, Dave wrote:
> > Thanks a lot for this! isDigit was the method I was looking for and 
> > couldn’t find.
> > 
> > I have another problem related to this, the following code uses the code 
> > you just sent. I am getting a files ID3 tags using eyed3, this part seems 
> > to work and I get expected values in this case myTitleName (Track name) is 
> > set to “Deadlock Holiday” and myCompareFileName is set to “01 Deadlock 
> > Holiday” (File Name with the Track number prepended). The is digit test 
> > works and myCompareFileName is set to  “Deadlock Holiday”, so they should 
> > match, right?
> > 
> OT, but are you sure about that name? Isn't it "Dreadlock Holiday" (by
> 10cc)?

Edsger Dijkstra originally wrote Deadlock Holiday for his band, The
Semaphores.  10cc lost the race condition and had to change the lyrics.

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


Re: How to test characters of a string

2022-06-07 Thread MRAB

On 2022-06-07 21:23, Dave wrote:

Thanks a lot for this! isDigit was the method I was looking for and couldn’t 
find.

I have another problem related to this, the following code uses the code you 
just sent. I am getting a files ID3 tags using eyed3, this part seems to work 
and I get expected values in this case myTitleName (Track name) is set to 
“Deadlock Holiday” and myCompareFileName is set to “01 Deadlock Holiday” (File 
Name with the Track number prepended). The is digit test works and 
myCompareFileName is set to  “Deadlock Holiday”, so they should match, right?

OT, but are you sure about that name? Isn't it "Dreadlock Holiday" (by 
10cc)?


[snip]
--
https://mail.python.org/mailman/listinfo/python-list


Re: How to test characters of a string

2022-06-07 Thread 2QdxY4RzWzUUiLuE
On 2022-06-08 at 07:29:03 +1000,
Chris Angelico  wrote:

> On Wed, 8 Jun 2022 at 07:24, Barry  wrote:
> >
> >
> >
> > > On 7 Jun 2022, at 22:04, Dave  wrote:
> > >
> > > It depends on the language I’m using, in Objective C, I’d use isNumeric, 
> > > just wanted to know what the equivalent is in Python.
> > >
> > > If you know the answer why don’t you just tell me and if you don’t, don’t 
> > > post!
> >
> > People ask home work questions here and we try to teach a student with 
> > hints not finished answers.
> > Your post was confused with a home work question.
> >
> 
> In the future, to make it look less like a homework question, show
> your current code, which would provide context. Last I checked,
> homework questions don't usually involve ID3 tags in MP3 files :)

The original question in this thread didn't say anything about MP3
files.  Jumping to that conclusion from strings like '05 Trinket' was
left as an exercise for the interested reader.  :-)
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: How to test characters of a string

2022-06-07 Thread Dave
A, ok will do, was just trying to be a brief as possible, will post more 
fully in future.

> On 7 Jun 2022, at 23:29, Chris Angelico  wrote:
> 
> On Wed, 8 Jun 2022 at 07:24, Barry  wrote:
>> 
>> 
>> 
>>> On 7 Jun 2022, at 22:04, Dave  wrote:
>>> 
>>> It depends on the language I’m using, in Objective C, I’d use isNumeric, 
>>> just wanted to know what the equivalent is in Python.
>>> 
>>> If you know the answer why don’t you just tell me and if you don’t, don’t 
>>> post!
>> 
>> People ask home work questions here and we try to teach a student with hints 
>> not finished answers.
>> Your post was confused with a home work question.
>> 
> 
> In the future, to make it look less like a homework question, show
> your current code, which would provide context. Last I checked,
> homework questions don't usually involve ID3 tags in MP3 files :)
> 
> ChrisA
> -- 
> https://mail.python.org/mailman/listinfo/python-list

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


Re: How to test characters of a string

2022-06-07 Thread Chris Angelico
On Wed, 8 Jun 2022 at 07:24, Barry  wrote:
>
>
>
> > On 7 Jun 2022, at 22:04, Dave  wrote:
> >
> > It depends on the language I’m using, in Objective C, I’d use isNumeric, 
> > just wanted to know what the equivalent is in Python.
> >
> > If you know the answer why don’t you just tell me and if you don’t, don’t 
> > post!
>
> People ask home work questions here and we try to teach a student with hints 
> not finished answers.
> Your post was confused with a home work question.
>

In the future, to make it look less like a homework question, show
your current code, which would provide context. Last I checked,
homework questions don't usually involve ID3 tags in MP3 files :)

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


Re: How to test characters of a string

2022-06-07 Thread Barry


> On 7 Jun 2022, at 22:04, Dave  wrote:
> 
> It depends on the language I’m using, in Objective C, I’d use isNumeric, 
> just wanted to know what the equivalent is in Python.
> 
> If you know the answer why don’t you just tell me and if you don’t, don’t 
> post!

People ask home work questions here and we try to teach a student with hints 
not finished answers.
Your post was confused with a home work question.

Barry

> 
> 
>> On 7 Jun 2022, at 22:08, 2qdxy4rzwzuui...@potatochowder.com wrote:
>> 
>>> On 2022-06-07 at 21:35:43 +0200,
>>> Dave  wrote:
>>> 
>>> I’m new to Python and have a simple problem that I can’t seem to find
>>> the answer.
>> 
>>> I want to test the first two characters of a string to check if the
>>> are numeric (00 to 99) and if so remove the fist three chars from the
>>> string.
>> 
>>> Example: if “05 Trinket” I want “Trinket”, but “Trinket” I still want
>>> “Trinket”. I can’t for the life of work out how to do it in Python?
>> 
>> How would you do it without Python?
>> 
>> Given that if the string is called x, then x[y] is the y'th character
>> (where what you would call "the first character," Python calls "the
>> zeroth character"), describe the steps you would take *as a person* (or
>> in some other programming language, if you know one) to carry out this
>> task.
>> 
>> Translating that algorithm to Python is the next step.  Perhaps
>> https://docs.python.org/3/library/string.html can help.
>> -- 
>> https://mail.python.org/mailman/listinfo/python-list
> 
> -- 
> https://mail.python.org/mailman/listinfo/python-list

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


Re: How to test characters of a string

2022-06-07 Thread Dave
Hi,

Found it! The files name had .mp3 at the end, the problem was being masked by 
null objects (or whatever) being returned by eyed3.

Checked for null objects and then stripped off the .mp3 and its mostly working 
now. I’ve got a few other eyed3 errors to do with null objects but I can sort 
those out tomorrow.

Thanks for your help - All the Best
Dave

> On 7 Jun 2022, at 23:01, Dave  wrote:
> 
> Hi,
> 
> No, I’ve checked leading/trailing whitespace, it seems to be related to the 
> variables that are returned from eyed3 in this case, for instance, I added a 
> check for None:
> myTitleName = myID3.tag.title
> if myTitleName is None:
>continue
> Seems like it can return a null object (or none?).
> 
> 
>> On 7 Jun 2022, at 22:35, De ongekruisigde 
>> > > wrote:
>> 
>> On 2022-06-07, Dave >  > >> wrote:
>>> Thanks a lot for this! isDigit was the method I was looking for and 
>>> couldn’t find.
>>> 
>>> I have another problem related to this, the following code uses the code 
>>> you just sent. I am getting a files ID3 tags using eyed3, this part seems 
>>> to work and I get expected values in this case myTitleName (Track name) is 
>>> set to “Deadlock Holiday” and myCompareFileName is set to “01 Deadlock 
>>> Holiday” (File Name with the Track number prepended). The is digit test 
>>> works and myCompareFileName is set to  “Deadlock Holiday”, so they should 
>>> match, right? 
>>> 
>>> However the if myCompareFileName != myTitleName always gives a mismatch! 
>>> What could cause two string that look the fail to not match properly?
>> 
>> Possibly leading or trailing spaces, or upper/lower case differences?
>> 
>> 
>>> myCompareFileName = myFile
>>> if myCompareFileName[0].isdigit() and myCompareFileName[1].isdigit():
>>>   myCompareFileName = myCompareFileName[3:]
>>> 
>>> if myCompareFileName != myTitleName:
>>>   print('File Name Mismatch - Artist: ',myArtistName,'  Album: 
>>> ',myAlbumName,'  Track:',myTitleName,'  File: ',myFile)
>>> Thanks a lot
>>> Dave
>>> 
 On 7 Jun 2022, at 21:58, De ongekruisigde 
  wrote:
 
 On 2022-06-07, Dave  wrote:
> Hi,
> 
> I’m new to Python and have a simple problem that I can’t seem to find the 
> answer.
> 
> I want to test the first two characters of a string to check if the are 
> numeric (00 to 99) and if so remove the fist three chars from the string. 
> 
> Example: if “05 Trinket” I want “Trinket”, but “Trinket” I still want 
> “Trinket”. I can’t for the life of work out how to do it in Python?
 
 
 s[3:] if s[0:2].isdigit() else s
 
 
> All the Best
> Dave
> 
 
 -- 
  You're rewriting parts of Quake in *Python*?
  MUAHAHAHA
 -- 
 https://mail.python.org/mailman/listinfo/python-list
>>> 
>> 
>> 
>> -- 
>>  You're rewriting parts of Quake in *Python*?
>>  MUAHAHAHA
>> -- 
>> https://mail.python.org/mailman/listinfo/python-list 
>>  
>> > >
> -- 
> https://mail.python.org/mailman/listinfo/python-list 
> 
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: How to test characters of a string

2022-06-07 Thread Dave
Hi,

No, I’ve checked leading/trailing whitespace, it seems to be related to the 
variables that are returned from eyed3 in this case, for instance, I added a 
check for None:
myTitleName = myID3.tag.title
if myTitleName is None:
continue
Seems like it can return a null object (or none?).

 
> On 7 Jun 2022, at 22:35, De ongekruisigde 
>  wrote:
> 
> On 2022-06-07, Dave  > wrote:
>> Thanks a lot for this! isDigit was the method I was looking for and couldn’t 
>> find.
>> 
>> I have another problem related to this, the following code uses the code you 
>> just sent. I am getting a files ID3 tags using eyed3, this part seems to 
>> work and I get expected values in this case myTitleName (Track name) is set 
>> to “Deadlock Holiday” and myCompareFileName is set to “01 Deadlock Holiday” 
>> (File Name with the Track number prepended). The is digit test works and 
>> myCompareFileName is set to  “Deadlock Holiday”, so they should match, 
>> right? 
>> 
>> However the if myCompareFileName != myTitleName always gives a mismatch! 
>> What could cause two string that look the fail to not match properly?
> 
> Possibly leading or trailing spaces, or upper/lower case differences?
> 
> 
>> myCompareFileName = myFile
>> if myCompareFileName[0].isdigit() and myCompareFileName[1].isdigit():
>>myCompareFileName = myCompareFileName[3:]
>> 
>> if myCompareFileName != myTitleName:
>>print('File Name Mismatch - Artist: ',myArtistName,'  Album: 
>> ',myAlbumName,'  Track:',myTitleName,'  File: ',myFile)
>> Thanks a lot
>> Dave
>> 
>>> On 7 Jun 2022, at 21:58, De ongekruisigde 
>>>  wrote:
>>> 
>>> On 2022-06-07, Dave  wrote:
 Hi,
 
 I’m new to Python and have a simple problem that I can’t seem to find the 
 answer.
 
 I want to test the first two characters of a string to check if the are 
 numeric (00 to 99) and if so remove the fist three chars from the string. 
 
 Example: if “05 Trinket” I want “Trinket”, but “Trinket” I still want 
 “Trinket”. I can’t for the life of work out how to do it in Python?
>>> 
>>> 
>>> s[3:] if s[0:2].isdigit() else s
>>> 
>>> 
 All the Best
 Dave
 
>>> 
>>> -- 
>>>  You're rewriting parts of Quake in *Python*?
>>>  MUAHAHAHA
>>> -- 
>>> https://mail.python.org/mailman/listinfo/python-list
>> 
> 
> 
> -- 
>  You're rewriting parts of Quake in *Python*?
>  MUAHAHAHA
> -- 
> https://mail.python.org/mailman/listinfo/python-list 
> 
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: How to test characters of a string

2022-06-07 Thread Dave
It depends on the language I’m using, in Objective C, I’d use isNumeric, just 
wanted to know what the equivalent is in Python.

If you know the answer why don’t you just tell me and if you don’t, don’t post!


> On 7 Jun 2022, at 22:08, 2qdxy4rzwzuui...@potatochowder.com wrote:
> 
> On 2022-06-07 at 21:35:43 +0200,
> Dave  wrote:
> 
>> I’m new to Python and have a simple problem that I can’t seem to find
>> the answer.
> 
>> I want to test the first two characters of a string to check if the
>> are numeric (00 to 99) and if so remove the fist three chars from the
>> string.
> 
>> Example: if “05 Trinket” I want “Trinket”, but “Trinket” I still want
>> “Trinket”. I can’t for the life of work out how to do it in Python?
> 
> How would you do it without Python?
> 
> Given that if the string is called x, then x[y] is the y'th character
> (where what you would call "the first character," Python calls "the
> zeroth character"), describe the steps you would take *as a person* (or
> in some other programming language, if you know one) to carry out this
> task.
> 
> Translating that algorithm to Python is the next step.  Perhaps
> https://docs.python.org/3/library/string.html can help.
> -- 
> https://mail.python.org/mailman/listinfo/python-list

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


Re: How to test characters of a string

2022-06-07 Thread De ongekruisigde
On 2022-06-07, Dave  wrote:
> Thanks a lot for this! isDigit was the method I was looking for and couldn’t 
> find.
>
> I have another problem related to this, the following code uses the code you 
> just sent. I am getting a files ID3 tags using eyed3, this part seems to work 
> and I get expected values in this case myTitleName (Track name) is set to 
> “Deadlock Holiday” and myCompareFileName is set to “01 Deadlock Holiday” 
> (File Name with the Track number prepended). The is digit test works and 
> myCompareFileName is set to  “Deadlock Holiday”, so they should match, right? 
>
> However the if myCompareFileName != myTitleName always gives a mismatch! What 
> could cause two string that look the fail to not match properly?

Possibly leading or trailing spaces, or upper/lower case differences?


> myCompareFileName = myFile
> if myCompareFileName[0].isdigit() and myCompareFileName[1].isdigit():
> myCompareFileName = myCompareFileName[3:]
>
> if myCompareFileName != myTitleName:
> print('File Name Mismatch - Artist: ',myArtistName,'  Album: 
> ',myAlbumName,'  Track:',myTitleName,'  File: ',myFile)
> Thanks a lot
> Dave
>
>> On 7 Jun 2022, at 21:58, De ongekruisigde 
>>  wrote:
>> 
>> On 2022-06-07, Dave  wrote:
>>> Hi,
>>> 
>>> I’m new to Python and have a simple problem that I can’t seem to find the 
>>> answer.
>>> 
>>> I want to test the first two characters of a string to check if the are 
>>> numeric (00 to 99) and if so remove the fist three chars from the string. 
>>> 
>>> Example: if “05 Trinket” I want “Trinket”, but “Trinket” I still want 
>>> “Trinket”. I can’t for the life of work out how to do it in Python?
>> 
>> 
>>  s[3:] if s[0:2].isdigit() else s
>> 
>> 
>>> All the Best
>>> Dave
>>> 
>> 
>> -- 
>>  You're rewriting parts of Quake in *Python*?
>>  MUAHAHAHA
>> -- 
>> https://mail.python.org/mailman/listinfo/python-list
>


-- 
 You're rewriting parts of Quake in *Python*?
 MUAHAHAHA
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: How to test characters of a string

2022-06-07 Thread De ongekruisigde
On 2022-06-07, Stefan Ram  wrote:
> Dave  writes:
>>Example: if "05 Trinket" I want "Trinket"
>
>   We're not supposed to write complete solutions, 

Okay, wasn't aware of this group policy; will keep it in mind.

-- 
 You're rewriting parts of Quake in *Python*?
 MUAHAHAHA
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: How to test characters of a string

2022-06-07 Thread 2QdxY4RzWzUUiLuE
On 2022-06-07 at 21:35:43 +0200,
Dave  wrote:

> I’m new to Python and have a simple problem that I can’t seem to find
> the answer.

> I want to test the first two characters of a string to check if the
> are numeric (00 to 99) and if so remove the fist three chars from the
> string.

> Example: if “05 Trinket” I want “Trinket”, but “Trinket” I still want
> “Trinket”. I can’t for the life of work out how to do it in Python?

How would you do it without Python?

Given that if the string is called x, then x[y] is the y'th character
(where what you would call "the first character," Python calls "the
zeroth character"), describe the steps you would take *as a person* (or
in some other programming language, if you know one) to carry out this
task.

Translating that algorithm to Python is the next step.  Perhaps
https://docs.python.org/3/library/string.html can help.
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: How to test characters of a string

2022-06-07 Thread Dave
Thanks a lot for this! isDigit was the method I was looking for and couldn’t 
find.

I have another problem related to this, the following code uses the code you 
just sent. I am getting a files ID3 tags using eyed3, this part seems to work 
and I get expected values in this case myTitleName (Track name) is set to 
“Deadlock Holiday” and myCompareFileName is set to “01 Deadlock Holiday” (File 
Name with the Track number prepended). The is digit test works and 
myCompareFileName is set to  “Deadlock Holiday”, so they should match, right? 

However the if myCompareFileName != myTitleName always gives a mismatch! What 
could cause two string that look the fail to not match properly?
myCompareFileName = myFile
if myCompareFileName[0].isdigit() and myCompareFileName[1].isdigit():
myCompareFileName = myCompareFileName[3:]

if myCompareFileName != myTitleName:
print('File Name Mismatch - Artist: ',myArtistName,'  Album: 
',myAlbumName,'  Track:',myTitleName,'  File: ',myFile)
Thanks a lot
Dave

> On 7 Jun 2022, at 21:58, De ongekruisigde 
>  wrote:
> 
> On 2022-06-07, Dave  wrote:
>> Hi,
>> 
>> I’m new to Python and have a simple problem that I can’t seem to find the 
>> answer.
>> 
>> I want to test the first two characters of a string to check if the are 
>> numeric (00 to 99) and if so remove the fist three chars from the string. 
>> 
>> Example: if “05 Trinket” I want “Trinket”, but “Trinket” I still want 
>> “Trinket”. I can’t for the life of work out how to do it in Python?
> 
> 
>  s[3:] if s[0:2].isdigit() else s
> 
> 
>> All the Best
>> Dave
>> 
> 
> -- 
>  You're rewriting parts of Quake in *Python*?
>  MUAHAHAHA
> -- 
> https://mail.python.org/mailman/listinfo/python-list

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


Re: How to test characters of a string

2022-06-07 Thread De ongekruisigde
On 2022-06-07, Dave  wrote:
> Hi,
>
> I’m new to Python and have a simple problem that I can’t seem to find the 
> answer.
>
> I want to test the first two characters of a string to check if the are 
> numeric (00 to 99) and if so remove the fist three chars from the string. 
>
> Example: if “05 Trinket” I want “Trinket”, but “Trinket” I still want 
> “Trinket”. I can’t for the life of work out how to do it in Python?


  s[3:] if s[0:2].isdigit() else s


> All the Best
> Dave
>

-- 
 You're rewriting parts of Quake in *Python*?
 MUAHAHAHA
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: How to test characters of a string

2022-06-07 Thread dn
On 08/06/2022 07.35, Dave wrote:
> Hi,
> 
> I’m new to Python and have a simple problem that I can’t seem to find the 
> answer.

> I want to test the first two characters of a string to check if the are 
> numeric (00 to 99) and if so remove the fist three chars from the string. 
> 
> Example: if “05 Trinket” I want “Trinket”, but “Trinket” I still want 
> “Trinket”. I can’t for the life of work out how to do it in Python?

This sounds like an assignment or quiz-question. We could provide an/the
answer - but then you wouldn't learn how to solve it for yourself...


There is a gentle introduction to "slicing" (taking the first two
characters) at
https://docs.python.org/3/tutorial/introduction.html?highlight=slicing

Characters can be turned into int[egers], as discussed at
https://docs.python.org/3/library/stdtypes.html#typesnumeric

Of course precautions must be taken in case the string is not an
integer, eg https://docs.python.org/3/tutorial/errors.html?highlight=except

Another approach might be to use isnumeric(),
https://docs.python.org/3/library/stdtypes.html?highlight=isnum


NB the first URL-pointer is a page in the Python Tutorial. Reading that
in its entirety may be a good investment of time!

Are you aware of the Python Tutor list?

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