個稅計算 物件導向的練習

2021-08-15 11:51:33 字數 3808 閱讀 3791

個稅計算公式:

分別使用幾個類,放置相關社保(-c ***x)、員工資訊(-d ***x)、個稅計算結果(-o ***x)等資訊

# -*- coding: utf-8 -*-

#匯入模組

import sys

import csv

#argv獲取引數

class args(object):

def __init__(self):

self.args = sys.argv[1:]

#索引路徑位置

def get_mark(self, option):

index = self.args.index(option)

return self.args[index + 1]

#社保檔案路徑

@property

def config_path(self):

return self.get_mark('-c')

#社保員工資訊路徑

@property

def userdata_path(self):

return self.get_mark('-d')

#社保結果檔案路徑

@property

def export_path(self):

return self.get_mark('-o')

#例項化

args = args()

#configfile社保

class config(object):

def __init__(self):

self.config = self._read_config()

#將檔案內容讀取到字典中

def _read_config(self):

config_path = args.config_path

config = {}

with open(config_path) as confignews:

for one_config in confignews.readlines():

news, nums = one_config.strip().split('=')

config[news.strip()] = float(nums.strip())

return config

def get_config(self, key):

return self.config[key]

config = config()

#user data員工資訊

class userdata(object):

def __init__(self):

self.userdata = self._read_users_data()

#將檔案內容讀取到列表中

def _read_users_data(self):

userdata_path = args.userdata_path

userdata =

with open(userdata_path) as datanews:

for one_data in datanews.readlines():

idnum, wage = one_data.strip().split(',')

return userdata

def __iter__(self):

return iter(self.userdata)

#tax_income

class incometax(object):

#comployeedata =

def __init__(self, userdata):

self.userdata = userdata

#salary after tax

@staticmethod

def calc_fordata(wage):

#social security tax社保

if wage < config.get_config('jishul'):

return config.get_config('jishul')*0.165

if wage > config.get_config('jishuh'):

return config.get_config('jishuh')*0.165

else:

return wage*0.165

# tax_oneself個稅

@classmethod

def tax_for_one(cls, wage):

social_wage = cls.calc_fordata(wage)

taxwage = wage - social_wage

if taxwage<=0:

tax_self = 0

elif taxwage<=1500:

tax_self = taxwage*0.03

elif taxwage<=4500:

tax_self= taxwage*0.1-105

elif taxwage<=9000:

tax_self = taxwage*0.2-555

elif taxwage<=35000:

tax_self = taxwage*0.25-1005

elif taxwage<=55000:

tax_self = taxwage*0.3-2755

elif taxwage<=80000:

tax_self = taxwage*0.35-5505

else:

tax_self = taxwage*0.45-13505

return tax_self

#the money we can get at the end最後收入

def money(self):

comployeedata=

for idnum, wage in self.userdata:

comployee = [idnum, wage]

shebao = self.calc_fordata(wage)

taxone = self.tax_for_one(wage)

get_money = wage - shebao - taxone

comployee += [''.format(shebao), ''.format(taxone),''.format(get_money)]

return comployeedata

def __iter__(self):

return iter(self.userdata)

#output csv file輸出結果到檔案

def export(self, default = 'csv'):

result = self.money()

with open(args.export_path, 'w', newline = '') as f:

writer = csv.writer(f)

writer.writerows(result)

#exec

if __name__=='__main__':

last_money = incometax(userdata())

last_money.export()

#本次小專案練習,題目**於實驗樓挑戰專案,主要為物件導向的練習,包括類、屬性、魔法方法、靜態方法等的使用和理解。還包括格式化輸出,迭代器等內容。在物件導向這一塊內容上,還需進一步練習。

#這是乙個好的開始!

物件導向練習

定義boat和car兩個類,兩者都有私有成員weight屬性,定義兩者的乙個友員函式totalweight 計算兩個類的物件的重量和。請根據給定的main函式和totalweight 函式的定義,完善boat和car兩個類。友元函式的使用 include using namespace std cl...

物件導向練習

include hero.class.php hero new hero 張三 建立英雄 hero daguai hero show hero daguai hero show hero daguai hero show hero daguai hero show hero daguai hero ...

工資和個稅的計算

工資的個人所得稅怎麼算 計算方式 應交所得稅 應納稅所得額 適用稅率 1 應納稅所得額 月度收入 5000元 起徵點 專項扣除 三險一金等 專項附加扣除 依法確定的其他扣除。2 綜合所得 工資 薪金所得 勞務報酬所得,稿費所得,特許權使用費所得 適用7級超額累進稅額,按月應納稅所得額計算徵稅,該稅率...