Guest User

Corrections

a guest
Jun 11th, 2020
64
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.74 KB | None | 0 0
  1. Hi, well done! Nice little applet!
  2.  
  3. I read your code, didn't try it, but I can give you some suggestions. I'am not a programmer, I do this as a hobby so my suggestions might be plain wrong so take it with a grain of salt.
  4.  
  5. First the `__init__` function should be at the beginning of the class, so one can see directly which members are part of the class. This is my preference, can be done in different ways.
  6.  
  7. The __init__ delegates to other functions the member creations, yes makes the init cleaner but one loses track of what are your class memebers.
  8.  
  9. You have save_size(self) which creates a new file, but in said file you have other parameters and you don't append those? I imagine that those parameters will disappear after using save_size() more than once?
  10.  
  11. I would make a function with save_config() where you save your configurations and read_config() were you read the configuration parameters.
  12.  
  13.  
  14.  
  15. In ` get_info_of_conff_file ` you have a global variable that you dont use? globals are bad practice but why you created it?
  16.  
  17.  
  18.  
  19. I'm not sure I like the use of Decimal for the currency, Decimal is a fixed point decimal number, this means you can set how many digits are before and after the . It is used for some applications.
  20.  
  21. But for example:
  22.  
  23. number = Decimal(self.textbox_amount.get("1.0", tk.END))
  24.  
  25. number *= Decimal(self.Values[index_to]) / Decimal(self.Values[index_from])
  26.  
  27. if Decimal(round((number), self.precision)) % Decimal(1.0) == 0:
  28.  
  29. you could use floating point numbers and then at the end represent the number with the correct round as `f"{number:." + self.precision + "f}"` -> (`f"{number:.4f}"`) this is the synthax of formatted strings ( https://docs.python.org/3/library/string.html#format-string-syntax ) Which does automatically what you are trying to do here.
  30.  
  31. In print_num(self):
  32.  
  33. you have number = "0" to initialize the number to a string, I know that 0123.2 os equal to 123.2 but you can really avoid to put the extra 0, and just say number = "" then add to the number.
  34.  
  35. There is a method of string that can check if is a digit the method isdigit, so your function could be
  36.  
  37. is_point = False
  38. for i in str(self.textbox_amount.get("1.0", tk.END)):
  39. if i.isdigit():
  40. number += i
  41.  
  42. if i == '.' and not is_point:
  43. is_point = True
  44. number += i
  45.  
  46. this will parse "1.222.222" as "1.222222" if is what you want?
  47.  
  48. You could also do
  49.  
  50. def convert_number(s):
  51.  
  52. try:
  53. return float(s)
  54.  
  55. except ValueError:
  56. return False
  57.  
  58. and then check if the conversion failed and maybe change the text box to red if the input is wrong?
  59.  
  60. def save_to_file(self,data):
  61.  
  62. data_to_save = str(data[0][0]) + "," + str(data[0][1])
  63. data = data[1:]
  64.  
  65. for i in range(data.__len__()):
  66. data_to_save += "|" + str(data[i][0]) + "," + str(data[i][1])
  67. with open('data.txt', 'w') as f:
  68. f.write(data_to_save)
  69. return data_to_save
  70.  
  71. data is not a good variable name, data means everything and if I read this function I have no idea what you are doing.
  72.  
  73. Why do you take out the first one? data = data[1:] fails if data is empty, btw you can write also len(data)
  74.  
  75. I'm guessing you try to save as comma separated the exhcange rates? and vertical bar separated the different currencies?
  76.  
  77. I would make a class named CurrencyRate
  78.  
  79. class CurrencyRate:
  80.  
  81. def __init__(self, name, rate):
  82. self.name = name
  83. self.rate = rate
  84.  
  85. then use this small data structure to rewirte the function
  86.  
  87. def save_to_file(self, exchange_rates):
  88.  
  89. data_to_save = ""
  90.  
  91. for x_rate in exchange_rates:
  92. data_to_save += x_rate.name + "," + str(x_rate.rate) + "|"
  93.  
  94. # removes the last |
  95. data_to_save = data_to_save[:-1]
  96.  
  97. with open('data.txt', 'w') as f:
  98. f.write(data_to_save)
  99.  
  100. return data_to_save
  101.  
  102. there are more things to add ofc... But I dont have time anymore
Advertisement
Add Comment
Please, Sign In to add comment