100
u/Namensplatzhalter Dec 15 '19 edited Dec 15 '19
Looks nice.
I've just had a look on your code and have to say it looks nicely organised and is easily readable. Kudos to you for writing it like that.
Two little suggestions, if you don't mind:
Consider using a
if __name__ == "__main__"block to initialise your script when it's run directlyConsider using tuples for your
vel_listvel_table as they are immutable
Both of those definitely not necessary, just things to think about. :)
Best wishes
26
u/Gawd_is_great Dec 15 '19
Thanks for the suggestions =) I have a couple of questions:
Why should I use if(__name__=="main") ? It wont make a difference..will it?
is there a better way than to use vel_list?
50
u/Namensplatzhalter Dec 15 '19
if __name__ == "__main__"is just a best practice for coding in python. not necessary for such simple apps but good to get in the habit of using it.Using vel_table as a mapper for the direction of the new points is perfectly fine. Is was just suggesting using tuples instead of lists because tuples are immutable and your directions are a fixed set (i.e. 8 directions)
8
u/Gawd_is_great Dec 15 '19
Oh ok Regarding the vel_table I was wondering if there was a way to do it mathematically. In my code I split the points at 45 degree. But suppose I wanted to split them at 15 degrees , how would that work out? I wouldn't go out and make a vel_table for that.
3
u/jespernielsen1982 Dec 15 '19
You can do it using the trigonometric functions "sine" and "cosine". It's not exactly hard, but it is more math than I want to be explaining on my phone.
1
u/Gawd_is_great Dec 15 '19
Ah yes okay thanks. I'll see into it. Thanks for guiding me onto the right path.
18
u/Endemoniada Dec 15 '19
Just to clarify on #1: it’s best practice because there’s two ways to execute code in a python file, either by importing its functions or classes, or by executing the entire file. The thing is, even when you import it, it executes all the code that is not specifically written inside a function or class, which can be a problem when all you expect is to be silently importing something from it.
Hence, by wrapping all active code in the script inside a function and then calling the execution of that function only when the code is run as a script, not being imported, you make it usable in both instances without surprise output or variables.
9
u/Namensplatzhalter Dec 15 '19
But suppose I wanted to split them at 15 degrees , how would that work out? I wouldn't go out and make a vel_table for that.
You could for example ask for a user
inputin the beginning where the user provides a number between 1 and 45. This sets the degree at which the splits should occur.For your code to work as it is, you would always have to make sure that the degree value don't leave a residue value (e.g. 13° wouldn't work because after 27 splits you'd have a residue of 9). This could be achieved by using the % operator in python, e.g. check for something like
360%input == 0. Go ahead and read up on it if you don't know it already.If this check returns true, you can then use the user input to determine the amount of directions you need to provide for your script by running
360/input.Maybe this will get you on the way of what you need to do. :) Best of luck
2
u/djimbob Dec 16 '19 edited Dec 17 '19
I'm not saying you need it for this, but they didn't really explain the purpose of the clause
if __name__ == 'main'clause.Looking at your script lines 1-56 seem to just define classes and functions, and then lines 57 and beyond seem to be doing things (and executing the program).
Its good software design to refactor and have the first section defining everything, then the run part of the program in a function
# define stuff above def draw_fractal(max_splits = 2**7, size=(700,700)): vel_table = [[0,-1],[-1,-1],[-1,0],[-1,1],[0,1],[1,1],[1,0],[1,-1]] [...] if __name__ == '__main__': draw_fractal()When you run your code
python fractal.pyit works the same. But now you can open the interpreter up andimport fractalwithout actually popping up a window to draw anything. Then you could maybe calldraw_fractal(max_splits=256)or call from another program. Or maybe you decide to create fractal2.py with aDot2class that inherits fromDotand is brought in withfrom fractal import Dot. You wouldn't want that import to start drawing the first fractal. By having 'executing' code (not just function/class/variable definitions) at the top level auto-run, you make code-reuse impossible.E.g.,
$ ipython In [1]: from fractal import Dot pygame 1.9.4 Hello from the pygame community. https://www.pygame.org/contribute.html 8At which point a window pops up and starts drawing.
Also, I'm not sure why you have
del objectanddel trailat the ends ofcreateandmakeTrailas they don't actually do anything (no memory is freed up or anything). And inmakeTrail, whenobjectsis an empty list it creates anUnboundLocalErrorwhen you try deletingtrail.The code
del some_varjust unsetssome_varin memory (and if there are no other references to the memory referenced bysome_varit lets thegcgive back the allocated memory to the OS on its next collection; though in your case their are other references -- you add the objects to list) and would make it an error to try and reusesome_var. However, all local variables are lost when a function exits (and it would be illegal to use them) -- there's no reason to explicitly delete a local variable at the end of a function.1
u/Gawd_is_great Dec 16 '19
Oh wow. Thanks a lot for the suggestions. It's amazing that people like you are willing to share knowledge with others =) Really appreciate it =)
3
u/graingert Dec 16 '19 edited Dec 16 '19
It's even better to use:
def main(): ... if __name__ == "__main__": main()Then you can use
module.mainas a setuptools entry point1
u/MB1211 Dec 15 '19
I didn't look at your code but the way it would make a difference is if you wanted to import your module, it will essentially run the module when you import. If you have the if name = main part, that part won't run because the module is not main when it's imported
2
u/ned334 Dec 15 '19
Where did you look at his code?
3
17
u/sharkbound github: sharkbound, python := 3.8 Dec 15 '19
in Dot.__init__ you are using lists as default values, having mutable default value is very bad and can become a bug very quickly, since functions are objects too, the argument default will all be the same reference each __init__ call
so because of this, when you do anything to mutate the default argument, it will affect all the places where the default argument value is used
its much better to use a tuple or None or some other immutable value for a default value check do a check in the init
code here: https://gist.github.com/785534176eca51545ce7d6cbbbb18e62
console output here: https://gist.github.com/50fbc58ed04fc1ac8089d3497b80c5f1
EDIT 0:
also spotted a name inconsistancy for function updatePos in Dot, everything except types/classes should use snake_case, update_pos
2
1
u/otterom Dec 16 '19
Pretty sweet! Ive never used pygame, so it's always fun to see some of the techniques used to make it run.
Unrelated: Does that maze repo generate a solvable maze each time?
2
3
3
u/muntoo R_{μν} - 1/2 R g_{μν} + Λ g_{μν} = 8π T_{μν} Dec 15 '19
4
1
1
1
0
0
0
Dec 15 '19
[deleted]
7
u/Gawd_is_great Dec 15 '19
Just upload the gif to your repo and in the README.md file put this line:
alt text
Alt text - the text you want to display in place of the image of it fails to laid
Link to image - Well...link to image in repo
35
u/Gawd_is_great Dec 15 '19
Source code at : https://github.com/Perseus-Perry/Fractal