0
0

More than 1 year has passed since last update.

[python]オブジェクト指向で記述するgithubのURLのあれこれを取得

Last updated at Posted at 2022-02-14

訳あってpythonを個人的に書いているのだが、最近の意識として、クソコードならクソコードらしく、ちゃんとオブジェクト指向で書いてみようじゃんかよ!ということで改めて書き直してみた。
ただ、この書き方が正しいのかどうかは分からん。

やっていることとしては、アカウント名とリポジトリ名を取得する事。
リポジトリ名については.gitが含まれる場合があるので、それについての判定も書いてみた。

main.py
from array import array

url = "https://github.com/TatsuyaMaeta/tama-camp-univ-janken.git"

class SplitUrl:
    def __init__(self,url):
        self.url = url
        # self.lastArrIndex = len(self.url.split(self))-1
        self.slsh = "/"
        self.dot = "."
        self.repositoryName = ""
        self.dotIndex = 0
        self.arrIndex = 0
        self.arrayURL = []
        self.dotflg = False
        self.tmpStr = ""

    def split(self):
        self.arrayURL = self.url.split(self.slsh)
        # 配列を返す
        return self.arrayURL

    def getArrIndex(self):
        self.arrayURL = self.url.split(self.slsh)
        self.arrIndex = len(self.arrayURL)
        # 数値を返す
        return self.arrIndex

    def checkIncludeDot(self):
        self.arrayURL = self.split()
        self.arrIndex = self.getArrIndex()
        self.dotIndex = self.arrayURL[self.arrIndex - 1].find(self.dot)
        if self.dotIndex > -1:
            print(f'ドットの位置は{self.dotIndex}文字目です')
            self.dotflg = True
            return self.dotflg
        else:
            return self.dotflg

    def getRepoName(self):
        self.tmpStr = self.arrayURL[self.arrIndex -1]
        self.repositoryName = self.tmpStr[0:self.dotIndex]
        return self.repositoryName

gitRepo = SplitUrl(url)

print(gitRepo.split())
print(gitRepo.getArrIndex())
print(gitRepo.checkIncludeDot())
print(gitRepo.dotIndex)
print(gitRepo.getRepoName())

プリント結果

ターミナル表示結果
['https:', '', 'github.com', 'TatsuyaMaeta', 'tama-camp-univ-janken.git']
5
ドットの位置は21文字目です
True
21
tama-camp-univ-janken

書き方についてよく分からず書いているので、オブジェクト指向はこう書いたらもっと良くなるよ!とかあれば是非教えてもらいたいです。

以上

0
0
2

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0