r/Python • u/[deleted] • Jul 25 '20
I Made This I made a project that generates the graph
100
45
u/aidankkm Jul 25 '20
how do you do the finished in 0.3 seconds text. i remember i saw it in a video but was too lazy.
18
Jul 25 '20
It always works This way in my pc
45
u/aidankkm Jul 25 '20
o wait i’m stupid it’s probably a feature of sublime. thanks anyways
22
20
u/FruscianteDebutante Jul 25 '20
... You can compile in sublime??
Also, look at python's time module.
At the beginning of your program start = time.time()
At the end: end = time.time()
Total time = end - start
10
u/Richey4TheStars Jul 25 '20
Yeah it’s been a feature for as long as I remember
Pretty sure you just hit Ctrl+B
2
u/NeonNingen Jul 25 '20
Sublime is best text editor that you can transform into an ide
4
u/FruscianteDebutante Jul 25 '20
I usually use it as an editor and compile with GCC and just run in the terminal haha. Very interesting though.
I just love sublime for its text editing capabilities
1
30
u/toastedstapler Jul 25 '20
If on Linux or Mac you can use the time command from the terminal. Not sure about Windows
time python3 script.py1
1
u/Boraini Jul 26 '20 edited Jul 26 '20
I guess it would add the compilation time too and yield inaccurate results.
Edit: I just checked the code and couldn’t find any mentions of timing libraries. They could really have timed the python3 command - it wouldn’t take 300ms to execute this code.
2
u/toastedstapler Jul 26 '20
it's fine, it basically does nothing
https://stackoverflow.com/questions/11838458/overhead-of-time-command-in-unix
1
u/Boraini Jul 26 '20
What about the overhead of python3?
2
u/toastedstapler Jul 26 '20
how else are you planning on running your script? sublime calls
python3when you run the code you've been writing1
3
Jul 25 '20
Most good shells have basic timing functionality inbuilt.
timeworks with zsh/bash/powershell. You can even set your prompt to show last execution time automatically.1
153
Jul 25 '20
no need to make your own reversed_range you can simply do range(X, -1, -1)
128
u/Indivicivet Jul 25 '20
or
reversed(range())which tends to be more readable37
12
u/__xor__ (self, other): Jul 25 '20
Do you lose the bonus that it doesn't generate a list if you do that though? Or is the return of
range()smart enough to let you getitem the last bit without generating the numbers.eg: if you want to iterate over
range(9999999, -1, -1)doesreversed(range(10000000))generate a list with every value first?39
u/Skasch Jul 25 '20
reversedcalls the__reversed__magic method, which is implemented byrangeobjects to return an iterator.Edit: you can try to run
reversed(range(10 ** 100)), which returned the iterator instantly :)3
2
u/Indivicivet Jul 26 '20
yes, as /u/Skasch mentions it uses
__reversed__magic to just return an iterator (arange_iteratorin fact).one possible issue is, what if you really do want a sequence, rather than an iterator? in this case, you can use
range(9999999999)[::-1]which uses__getitem__(slice)magic instead. this returns arange(999999998, -1, -1)object. I personally think this is clearer than therange(X, -1, -1)form in most contexts.1
6
u/CrChCh Jul 25 '20
or
x[::-1]11
Jul 25 '20
Only if you already have a list of things and aren’t interested in the actual numbers counting nog down. Good point though. Because usually this is exactly the case. I would use reversed(x) though.
34
3
u/both_objective Jul 25 '20
Good job. How did u come up with this idea? What is your next project?
4
Jul 25 '20
I don't know what should I make next. Can you suggest me any?
4
u/both_objective Jul 25 '20
What is your interest? With this function, can you use it to generate functions? i.e sine, cosine, square, sawtooth waves?
2
2
u/MaliciousAccount Jul 27 '20
powerline.io
This game needs some developers to maintain it, you may contact the admin for more info.1
14
12
Jul 25 '20
nice, I would extend the function you use to grab indices which are larger than 10, instead of just grabbing the last digit.
8
u/mathisfun271 Jul 26 '20 edited Jul 26 '20
I made a similar program once, but instead of using asterisks, I used three types of dots ( . , · , ˙ ) for more precise plotting. It ended like this:
3┤
│ ..·
│ .·˙˙
2┤ .·˙˙
│ .˙˙
│ ·
1 ┤ ·˙
│ ·
│ ··˙
0 ┤··
└┬─────┬─────┬─────┬─────
0 1 2 35
u/fecesmuncher69 Jul 25 '20
Thanks a lot! I will definitely try it. Is that a module you created or is it available for everyone? I have a project doing some calculations like distance between 2 points and things like that, a graph is a major improvement for it
6
u/Theonetheycallgreat Jul 25 '20
To be fair, just using pychart will probably be easier for a project
1
u/fecesmuncher69 Jul 25 '20
I’m a beginner so idk what that is. I will check it out, thx
2
u/Theonetheycallgreat Jul 25 '20
Easy python library that allows for creating and rendering charts. There is also Graphviz that is good as well.
1
u/bminixhofer Jul 25 '20
You could try permon, a library I made some time ago, if you're interested in that sort of thing.
4
u/Tadabito Jul 25 '20
You can try drawing the chart with braille characters, that would give you 3x vertical and 2x horizontal resolution.
4
u/NonBinaryGay Jul 25 '20
Do you think I could use this to cheat with my maths and science??
5
u/thornofcrown Jul 25 '20
Totally. It definitely looks equipped to deal with logarithms, exponentials, and complex functions.
1
2
2
2
2
5
u/ALABAMA-SHIT Jul 25 '20
Have you considered make them into 2d lists instead (e.g. pos = [[1,2],[2,3],[4,5]])
1
u/both_objective Jul 26 '20
Yes. He has. There is a condition in his code checking the type of input and interpret it accordingly. He posted his repo in the thread.
3
u/FpggyJohnson18 Jul 25 '20
While this is really cool and definitely worthy of notoriety, I suggest, for practicality's sake, that you rewrite the code to ingest coordinates in a list of dictionaries as opposed to 2 lists. Instead of >>>x=[1,2] y=[3,2]... >>>coords = [{'x': 1, 'y': 3}, {'x': 2, 'y': 2}]
EDIT: spelling and grammar
8
u/Deadly_chef Jul 25 '20
Or just tuples for better readability, easier writing and parsing
1
u/FpggyJohnson18 Jul 25 '20 edited Jul 25 '20
#DictionaryMasterRace lol, but yeah either would work. The important thing is grouping them together... And how quickly the code runs of course
EDIT: escaped hashtag
3
u/Deadly_chef Jul 25 '20
Tuples should be a lot faster then dicts. Also if you are going for speed I suggest pypy instead of regular python
1
u/FpggyJohnson18 Jul 25 '20
Of course. I wasn't suggesting dictionaries was the faster method, just my personal preference. And a 2nd point that all what really matters is speed (and I/O ops).
3
u/DrShocker Jul 25 '20
(psst, if you meant to do #Whatever as a hashtag, you need to escape the # since it has meaning in markdown.)
1
u/FpggyJohnson18 Jul 25 '20
New to Reddit, I didn't even notice it wasn't there when I posted. Thanks a mil!
6
u/TofuCannon Jul 25 '20
Absolutely not. The dict is bloating the format a lot and makes it awful to type for hand written data.
If you want to use a different representation, you can easily convert from one another (zip(x, y) or vice versa zip(*coords) is your friend)
2
u/FpggyJohnson18 Jul 25 '20
Sure sure. But code running in production isn't fed hand-written code so that point is moot. Then for transporting the data, if you transport over json (which is relatively standard nowadays) everything's already in the proper format.
2
1
1
1
1
u/glmdev Jul 25 '20
This is an awesome project! You might consider exploring ANSI box drawing characters. It would let you smooth out the lines.
1
u/shadowkat0 Jul 25 '20
Hey cool project. gnuplot does the same with terminal set to 'dumb'. There's a gnuplotlib too for python.
1
1
u/uQQ_iGG Jul 25 '20
Nothing better than some ascii a e s t h e t i c s.
What if you get a more continous datapoints? Does it skip characters to generate graph?
1
1
Jul 25 '20
I like this, there is a lack out there of graphing nice looking charts on the terminal. A few packages can do it but it looks like ass
1
u/Rutherfordio Jul 25 '20
This is a really cool project! I see myself using it (I mostly use python to do math and data analysis) when testing algorithms in cli scripts.
Still haven't checked the code, but, does it handle big amounts of numbers? (like, define an amount of points in the graph and then calculate means to plot in the graph -not sure if I'm explaining myself nice-) if not, it could be an interesting upgrade for this.
Cool work!
1
1
u/moocat Jul 25 '20
Having separate arrays for the x values and the y values seems like an odd choice. For example, did you notice that in your example that len(x) != len(y)?
1
Jul 25 '20
Really cool. How does it cope with larger arrays of numbers to render on the graph? I mean, say you wanted to scatter plot two arrays each of length 1,000. Would the resolution reduce in order to accommodate the size of the graph? Obviously, you can't really reduce the resolution of the console output, and so in that case does it just leave some observations out?
1
1
u/dogs_like_me Jul 26 '20
If you wrap it in a simple CLI (e.g. with argparse) you could print charts in a console, design a whole ascii dashboard.
1
u/usernamecorrupted Jul 26 '20
Maybe a very big hassle but have you tried Manim to generate stuff like this
1
1
u/maxic62 Jul 25 '20
Nice! I love it. Should be nerd to use it to display home temp? 😆
2
u/NemexiaM Jul 25 '20
You connect it to a sensor?
2
u/maxic62 Jul 25 '20
Yes! From my home automation I have a lot of sensors, rooms temp/hum, solar panel info, well sensors.
1
1
1
-6
49
u/[deleted] Jul 25 '20
Source:
https://github.com/jeel2331/Graph.git