6
6
7
7
__all__ = [
8
8
'is_string' ,
9
+ 'is_full_string' ,
9
10
'is_number' ,
10
11
'is_integer' ,
11
12
'is_decimal' ,
12
- 'is_full_string' ,
13
13
'is_url' ,
14
14
'is_email' ,
15
15
'is_credit_card' ,
@@ -83,6 +83,11 @@ def is_string(obj: Any) -> bool:
83
83
"""
84
84
Checks if an object is a string.
85
85
86
+ *Example:*
87
+
88
+ >>> is_string('foo') # returns true
89
+ >>> is_string(b'foo') # returns false
90
+
86
91
:param obj: Object to test.
87
92
:return: True if string, false otherwise.
88
93
"""
@@ -423,43 +428,61 @@ def is_ip(input_string: Any) -> bool:
423
428
return is_ip_v6 (input_string ) or is_ip_v4 (input_string )
424
429
425
430
426
- def is_palindrome (input_string : Any , strict : bool = True ) -> bool :
431
+ def is_palindrome (input_string : Any , ignore_spaces : bool = False , ignore_case : bool = False ) -> bool :
427
432
"""
428
433
Checks if the string is a palindrome (https://en.wikipedia.org/wiki/Palindrome).
429
434
435
+ *Examples:*
436
+
437
+ >>> is_palindrome('LOL') # returns true
438
+ >>> is_palindrome('Lol') # returns false
439
+ >>> is_palindrome('Lol', ignore_case=True) # returns true
440
+ >>> is_palindrome('ROTFL') # returns false
441
+
430
442
:param input_string: String to check.
431
443
:type input_string: str
432
- :param strict: True if white spaces matter (default), false otherwise.
433
- :type strict: bool
444
+ :param ignore_spaces: False if white spaces matter (default), true otherwise.
445
+ :type ignore_spaces: bool
446
+ :param ignore_case: False if char case matters (default), true otherwise.
447
+ :type ignore_case: bool
434
448
:return: True if the string is a palindrome (like "otto", or "i topi non avevano nipoti" if strict=False),\
435
449
False otherwise
436
450
"""
437
- if is_full_string (input_string ):
438
- if strict :
439
- string_len = len (input_string )
451
+ if not is_full_string (input_string ):
452
+ return False
440
453
441
- # Traverse the string one char at step, and for each step compares the
442
- # "head_char" (the one on the left of the string) to the "tail_char" (the one on the right).
443
- # In this way we avoid to manipulate the whole string in advance if not necessary and provide a faster
444
- # algorithm which can scale very well for long strings.
445
- for index in range (string_len ):
446
- head_char = input_string [index ]
447
- tail_char = input_string [string_len - index - 1 ]
454
+ if ignore_spaces :
455
+ input_string = SPACES_RE .sub ('' , input_string )
448
456
449
- if head_char != tail_char :
450
- return False
457
+ string_len = len (input_string )
451
458
452
- return True
459
+ # Traverse the string one char at step, and for each step compares the
460
+ # "head_char" (the one on the left of the string) to the "tail_char" (the one on the right).
461
+ # In this way we avoid to manipulate the whole string in advance if not necessary and provide a faster
462
+ # algorithm which can scale very well for long strings.
463
+ for index in range (string_len ):
464
+ head_char = input_string [index ]
465
+ tail_char = input_string [string_len - index - 1 ]
453
466
454
- return is_palindrome (SPACES_RE .sub ('' , input_string ))
467
+ if ignore_case :
468
+ head_char = head_char .lower ()
469
+ tail_char = tail_char .lower ()
455
470
456
- return False
471
+ if head_char != tail_char :
472
+ return False
473
+
474
+ return True
457
475
458
476
459
477
def is_pangram (input_string : Any ) -> bool :
460
478
"""
461
479
Checks if the string is a pangram (https://en.wikipedia.org/wiki/Pangram).
462
480
481
+ *Examples:*
482
+
483
+ >>> is_pangram('The quick brown fox jumps over the lazy dog') # returns true
484
+ >>> is_pangram('hello world') # returns false
485
+
463
486
:param input_string: String to check.
464
487
:type input_string: str
465
488
:return: True if the string is a pangram, False otherwise.
@@ -474,6 +497,11 @@ def is_isogram(input_string: Any) -> bool:
474
497
"""
475
498
Checks if the string is an isogram (https://en.wikipedia.org/wiki/Isogram).
476
499
500
+ *Examples:*
501
+
502
+ >>> is_isogram('dermatoglyphics') # returns true
503
+ >>> is_isogram('hello') # returns false
504
+
477
505
:param input_string: String to check.
478
506
:type input_string: str
479
507
:return: True if isogram, false otherwise.
@@ -485,6 +513,11 @@ def is_slug(input_string: Any, sign: str = '-') -> bool:
485
513
"""
486
514
Checks if a given string is a slug.
487
515
516
+ *Examples:*
517
+
518
+ >>> is_slug('my-blog-post-title') # returns true
519
+ >>> is_slug('My blog post title') # returns false
520
+
488
521
:param input_string: String to check.
489
522
:type input_string: str
490
523
:param sign: Join sign used by the slug.
@@ -505,6 +538,11 @@ def contains_html(input_string: str) -> bool:
505
538
By design, this function is very permissive regarding what to consider html code, don't expect to use it
506
539
as an html validator, its goal is to detect "malicious" or undesired html tags in the text.
507
540
541
+ *Examples:*
542
+
543
+ >>> contains_html('my string is <strong>bold</strong>') # returns true
544
+ >>> contains_html('my string is not bold') # returns false
545
+
508
546
:param input_string: Text to check
509
547
:type input_string: str
510
548
:return: True if string contains html, false otherwise.
@@ -524,6 +562,11 @@ def words_count(input_string: str) -> int:
524
562
Moreover it is aware of punctuation, so the count for a string like "one,two,three.stop"
525
563
will be 4 not 1 (even if there are no spaces in the string).
526
564
565
+ *Examples:*
566
+
567
+ >>> words_count('hello world') # returns 2
568
+ >>> words_count('one,two,three') # returns 3 (no need for spaces, punctuation is recognized!)
569
+
527
570
:param input_string: String to check.
528
571
:type input_string: str
529
572
:return: Number of words.
@@ -540,6 +583,12 @@ def is_isbn_10(input_string: str, normalize: bool = True) -> bool:
540
583
By default hyphens in the string are ignored, so digits can be separated in different ways, by calling this
541
584
function with `normalize=False` only digit-only strings will pass the validation.
542
585
586
+ *Examples:*
587
+
588
+ >>> is_isbn_10('1506715214') # returns true
589
+ >>> is_isbn_10('150-6715214') # returns true
590
+ >>> is_isbn_10('150-6715214', normalize=False) # returns false
591
+
543
592
:param input_string: String to check.
544
593
:param normalize: True to ignore hyphens ("-") in the string (default), false otherwise.
545
594
:return: True if valid ISBN 10, false otherwise.
@@ -554,6 +603,12 @@ def is_isbn_13(input_string: str, normalize: bool = True) -> bool:
554
603
By default hyphens in the string are ignored, so digits can be separated in different ways, by calling this
555
604
function with `normalize=False` only digit-only strings will pass the validation.
556
605
606
+ *Examples:*
607
+
608
+ >>> is_isbn_13('9780312498580') # returns true
609
+ >>> is_isbn_13('978-0312498580') # returns true
610
+ >>> is_isbn_13('978-0312498580', normalize=False) # returns false
611
+
557
612
:param input_string: String to check.
558
613
:param normalize: True to ignore hyphens ("-") in the string (default), false otherwise.
559
614
:return: True if valid ISBN 13, false otherwise.
@@ -568,6 +623,11 @@ def is_isbn(input_string: str, normalize: bool = True) -> bool:
568
623
By default hyphens in the string are ignored, so digits can be separated in different ways, by calling this
569
624
function with `normalize=False` only digit-only strings will pass the validation.
570
625
626
+ *Examples:*
627
+
628
+ >>> is_isbn('9780312498580') # returns true
629
+ >>> is_isbn('1506715214') # returns true
630
+
571
631
:param input_string: String to check.
572
632
:param normalize: True to ignore hyphens ("-") in the string (default), false otherwise.
573
633
:return: True if valid ISBN (10 or 13), false otherwise.
0 commit comments