Make code less complex and more readable

Qex

I need to rewrite my simple code. I'm getting simple strings as below:

  • Distrib ABC 1-2-x
  • Distrib ABC DEF 1-2-x
  • Distrib ABC DEF GHI 1-2-x

I'm getting to .split() all words after "Distrib " and I've to fulfill following conditions:

  1. If string[0] is text && string[1] is as integer then join only these to get as result "ABC/1"

  2. If string[0] is text && string[1] is text join only them and then get as result "ABC/DEF"

  3. If string[0] is text && string[1] is text && string[2] is text join them all and get as result: "ABC/DEF/GHI"

I wrote a simple code to do this but I'm really interested how to write it less complex and more readable ;)

import re

def main_execute():
    #input_text = "Distrib ABC 1-2-x"
    #input_text = "Distrib ABC DEF 1-2-x"
    #input_text = "Distrib ABC DEF GHI 1-2-x"

    print(str(input_text))
    load_data = re.search('\s[A-Z]*.[A-Z]*.[A-Z]+ [0-9]', input_text).group()
    print("Pobrany ciąg znaków: " + load_data)

    words_array = load_data.split()

    if re.match('[0-9]', words_array[1]):
        print("Złożony ciąg: "
              + words_array[0]
              + "/"
              + words_array[1])
        elif re.match('[A-Z]', words_array[0]) and re.match('[A-Z]', words_array[1]) and re.match('[0-9]', words_array[2]):
        print("Złożony ciąg: "
              + words_array[0]
              + "/"
              + words_array[1])
    elif re.match('[A-Z]', words_array[0]) and re.match('[A-Z]', words_array[1]) and re.match('[A-Z]', words_array[2]) and re.match('[0-9]', words_array[3]):
        print("Złożony ciąg: "
              + words_array[0]
              + "/"
              + words_array[1]
              + "/"
              + words_array[2])


    if __name__ == "__main__":
        main_execute()
Jan

This can be vastly simplified to

import re

data = """
Distrib ABC 1-2-x
Distrib ABC DEF 1-2-x
Distrib ABC DEF GHI 1-2-x
"""

rx = re.compile(r'Distrib (\w+) (\w+)\s*((?:(?!\d)\w)+)?')

results = ["/".join([n for n in m.groups() if n]) for m in rx.finditer(data)]
print(results)

Which yields

['ABC/1', 'ABC/DEF', 'ABC/DEF/GHI']

See a demo for the expression on regex101.com.


Another approach, as proposed by @Wiktor, could be

Distrib (\w+) (\w+)\s*([^\W\d]+)?

The part [^\W\d]+ is saying: not not (the doubling is no mistake!) word characters, not digits, as long as possible.

この記事はインターネットから収集されたものであり、転載の際にはソースを示してください。

侵害の場合は、連絡してください[email protected]

編集
0

コメントを追加

0

関連記事

分類Dev

How to group props and make code more readable?

分類Dev

How to make more readable and shorter a karate scenario

分類Dev

How do I make an if statement not execute and go to the else if statement if one or more variables are less than 0?

分類Dev

Limit readable text with a 'read more' link?

分類Dev

How can I make a code more efficient and shorter?

分類Dev

How can I use interfaces to allow for more than 1 struct type to make code more useable?

分類Dev

less or more - which to use when?

分類Dev

why is this less code not compiling?

分類Dev

Format JSON encoded string into more human readable in Silverstripe

分類Dev

Is `tail -f` more efficient than `less +F`?

分類Dev

How to implement Read More and Read Less with dotdotdot?

分類Dev

Grid column responsive width but no more and no less than

分類Dev

SQLでの「more」、「less」の使用

分類Dev

Finding two integers that multiply to 20. Can I make this code more "pythonic"?

分類Dev

Why does my prime factorization code in processing 3 use so much memory and how can I make it use less memory?

分類Dev

Creating domain events only for the purpose of making the projection logic less complex

分類Dev

How to make less output persistent on the terminal, after quitting less?

分類Dev

Html Read More code

分類Dev

More Expressive Code

分類Dev

AutoHotkey make remove less than typed

分類Dev

Make Python RegEx More Concise

分類Dev

Is there any ways to make this more efficient?

分類Dev

Make string checker more efficient

分類Dev

Entity Framework & ASP.NET MVC more complex models

分類Dev

Setting up watches in Angular controller vs. more complex template

分類Dev

How convert this type of data <hdf5 object reference> to something more readable in the python?

分類Dev

jQuery - test if img width is 'more than' and 'less than'

分類Dev

Writing more concise code in go

分類Dev

How to list more lines of code code in LLDB

Related 関連記事

  1. 1

    How to group props and make code more readable?

  2. 2

    How to make more readable and shorter a karate scenario

  3. 3

    How do I make an if statement not execute and go to the else if statement if one or more variables are less than 0?

  4. 4

    Limit readable text with a 'read more' link?

  5. 5

    How can I make a code more efficient and shorter?

  6. 6

    How can I use interfaces to allow for more than 1 struct type to make code more useable?

  7. 7

    less or more - which to use when?

  8. 8

    why is this less code not compiling?

  9. 9

    Format JSON encoded string into more human readable in Silverstripe

  10. 10

    Is `tail -f` more efficient than `less +F`?

  11. 11

    How to implement Read More and Read Less with dotdotdot?

  12. 12

    Grid column responsive width but no more and no less than

  13. 13

    SQLでの「more」、「less」の使用

  14. 14

    Finding two integers that multiply to 20. Can I make this code more "pythonic"?

  15. 15

    Why does my prime factorization code in processing 3 use so much memory and how can I make it use less memory?

  16. 16

    Creating domain events only for the purpose of making the projection logic less complex

  17. 17

    How to make less output persistent on the terminal, after quitting less?

  18. 18

    Html Read More code

  19. 19

    More Expressive Code

  20. 20

    AutoHotkey make remove less than typed

  21. 21

    Make Python RegEx More Concise

  22. 22

    Is there any ways to make this more efficient?

  23. 23

    Make string checker more efficient

  24. 24

    Entity Framework & ASP.NET MVC more complex models

  25. 25

    Setting up watches in Angular controller vs. more complex template

  26. 26

    How convert this type of data <hdf5 object reference> to something more readable in the python?

  27. 27

    jQuery - test if img width is 'more than' and 'less than'

  28. 28

    Writing more concise code in go

  29. 29

    How to list more lines of code code in LLDB

ホットタグ

アーカイブ