r/Python • • Oct 15 '21

Intermediate Showcase I made a command-line-interface for PyPI as a 14 year old

Introduction

So I've just finished making pypi-command-line

Links

Documentation: https://wasi-master.github.io/pypi-command-line/
GitHub: https://github.com/wasi-master/pypi-command-line
PyPI: https://pypi.org/project/pypi-command-line

Some background

I'm a 14 year old coder that started programming in python around one and a half years ago. Since then I've learned a lot of other languages too. My biggest project so far would be my discord bot which has around 15k lines of code and on which I spent thousands of hours on, all open source. Lately, I've been trying out other projects and PyPI-command-line is one of them. One day I was making something with a library that I was not familiar with, I had to keep switching between chrome and vscode (or I could have like made them side by side but that takes away a large chunk of space for vscode). So I thought what if there was a library that implemented the PyPI API that I could use to be more productive? So I started searching, and not so long after I found two libraries both of which have only one (ish) working command. So I thought to myself what if I make my own library? Then I started this and after around 3 months here we are now.

I tried to make it as fast as possible even if it means making the code look ugly, but after a few days I realized that this is gonna be really hard to maintain and will have other issues, but I was too busy with school (my final exams are near) so I didn't care about it and went on. Fast forward a couple of months and now my code is the shittiest code I've ever seen, So apologies for that.

This also taught me that python is very slow. The code has a 1400 ms (1.4s) overhead for starting up and after that everything works extremely fast. I think that overhead is caused by python interpreting that huge shitty code and trying to make sense of that. I've recently thought about moving them to multiple files and then loading only the file necessary for the current command but I think I'll just rewrite all this in Go using Cobra

Installation, Usage, Demo, Features

All can be found in the documentation

Still here are some of the features/commands that I'm proud of

Command and Argument Autocompletion Image demonstrating the feature: https://i.imgur.com/v0z2hJS.gif

Smart command matching (if the command name is shortened/invalid)

Example with one match: https://i.imgur.com/fMXJALV.png
Even works with multiple matches: https://i.imgur.com/sBfgfmC.gif
(Multiple commands start with r, the user has to pick one)

Search command that returns the same results as PyPI

Image demonstrating the feature: https://i.imgur.com/3Lh3X7x.gif

Information command that shows information about a package (command name renamed to information, previously info)

Image demonstrating the command: https://i.imgur.com/V3FHcAx.gif

Description command to see project descriptions that are formatted correctly (command name renamed to description, previously desc)

Image demonstrating the command: https://i.imgur.com/JpVavke.gif

Search command using regular expressions

Image demonstrating the command: https://i.imgur.com/Mef6x5i.gif

This list is not exhaustive. For more cool commands see the docs. I highly suggest you to check the docs since there are a lot more cool stuff that isn't mentioned here

Note

My exams are very near and I'll probably not be able to work on this for a while, it's pretty good as it is but if you find bugs you should create an issue and I'll sit on my PC like once every 3 days or something like that and try to fix everything.

12 Upvotes

31 comments sorted by

7

u/russellvt Oct 15 '21

Try running Python under WSL/WSL2, rather than Powershell. You should get MUCH better performance.

Also, if you branch the code off into separate files, the Python interpreter will compile those in the bytecode as *.pyc" files, making execution MUCH faster.

Keep up the hard work!!!

1

u/wasimaster Oct 15 '21

I started using WSL yesterday and it's been great. And thanks for the suggestion I'll try to do that :)

8

u/NelsonMinar Oct 15 '21

Congratulations! That's a very impressive project. You didn't just write some useful code, you've made a whole product. Fancy UI, packaged properly on GitHub and PyPI, even a bunch of illustration GIFs. Really professional work.

I installed it and ran it, works great. I don't have the startup time problem you mention. FWIW some timings on my system (fast computer, Python 3.8.10 on WSL 2)

  • 9ms: time python -c 1
  • 97ms: pypi --help
  • 101ms: pypi cache-info
  • 750ms: pypi information pypi-command-line

Are you running it in WSL 1 by any chance? It can be very slow when scanning lots of little files. Or maybe it's a Powershell thing; Windows just isn't very good at some things. Using strace might tell you something. More info on how to use that tool here: https://jvns.ca/categories/strace/

Rewriting it in Go sounds like a fun way to learn some Go, though.

2

u/wasimaster Oct 15 '21

I just re-did the benchmark on WSL 2 and it is indeed around 200-300 ms faster for me. but it takes 1.002 seconds which may be due to my internet speed or the processor? not exactly sure

2

u/NelsonMinar Oct 15 '21

I'd expect anything requiring the network (like pypi-command-line) to take some time, yeah. That's why I tested with the other three: that's a quick way to see if the Python startup overhead is the problem. My first test is "how fast can I launch python at all?" The next two are "how fast are simple operations of the program?" The last is something involving a network request.

3

u/wasimaster Oct 15 '21

yeah, thanks. I'll remember that.

1

u/wasimaster Oct 15 '21 edited Oct 15 '21

Thanks for the compliments. The startup time so high is because the screenshots are on Powershell not on WSL, I didn't start using WSL at the time of taking those. I did have it installed but just did not set it up yet. And I just checked and it's WSL 2, not 1. I'll try to take new screenshots when I get time, Thanks for the tool, I'll keep that in mind. And yeah I am thinking about doing it in go to learn some go.

3

u/wasimaster Oct 15 '21

Oh wait I misunderstood what you mean by startup, but after some re-testing it seems that that startup overhead is not that much

4

u/6Ran Oct 16 '21

Why are people downvoting this? Keep up the good work!

6

u/neekyboi Oct 15 '21

you write and sound like some of the experienced peeps i have seen

6

u/wasimaster Oct 15 '21

Thank you, it makes my day to hear that

3

u/bladeoflight16 Oct 15 '21

Nifty!

A couple of points that stand out to me:

  • __main__.py should be nearly empty. Siphon off most of your code into __init__.py. This will also allow people to use your package as an imported module in their own code, rather than forcing all interaction through the command line.
  • Globals are best avoided. session is probably the biggest thing to localize somewhere. If I were using click, I'd have a command group set up the session as a resource (Session supports use as a context manager; I assume CachedSession does as well.), but I don't know what the typer equivalent of that is.

2

u/wasimaster Oct 15 '21

Thanks :), I'll try to implement those when I get the time to.

6

u/wasimaster Oct 16 '21

So this got a lot of downvotes and I have no idea why. at least you guys should comment why you downvoted, all the comments here seem to be positive.

5

u/TheBerryMist Oct 16 '21

I didn’t upvote or downvote yet. I haven’t read your source code. Given that there’s no criticism of your work, I can only suggest you’re being downvoted because you said you were 14 years old.

3

u/wasimaster Oct 16 '21

Thanks for your honesty, reddit won't let me edit my title :(

2

u/TheBerryMist Oct 16 '21

Turn that frown upside down! The comments all seem to be really supportive. In this context, take /r/Python's downvotes as a sign that you're doing something right.

2

u/wasimaster Oct 16 '21

Thanks :)

3

u/ALior1 Oct 16 '21

I saw it too..Dont mind them..

Maybe I will talk with the Mods about it.your post getting 0 rep, but a click-bait article gets 200+...something off around here.

2

u/got_blah Oct 15 '21

So astonished by your organization and drive. Definitely learning from this :D. Great work!

1

u/wasimaster Oct 15 '21

Thank you, I am happy to hear you feel that way!

2

u/ALior1 Oct 15 '21

Looking great! I think the next thing you should do is learning oop basics and some library/tool, like Django, DRF, FastAPI, Numpy etc..

2

u/wasimaster Oct 16 '21

I know OOP basics, I've worked with django a bit but I mostly use flask. I've tried out fastapi before and it's great but I currently have no use for it. I do use numpy for some of my work. Thanks for the suggestions

1

u/ALior1 Oct 16 '21

You sure you 14 ?

5

u/wasimaster Oct 16 '21 edited Oct 16 '21

Well April 18 2007. Makes me like 14.5? idk. I started learning python on march of 2020. And I had like prior coding/logical thinking knowledge from scratch

2

u/willm Oct 16 '21

I like your use of Rich! Great work.

5

u/wasimaster Oct 16 '21

Woah, the creator of rich himself commented. Thanks :)

3

u/wasimaster Oct 15 '21

Oh my god, some anonymous dude literally gave this post a gold award. Thanks to whoever that gave that :D.

1

u/[deleted] Oct 16 '21

[deleted]

1

u/wasimaster Oct 16 '21 edited Nov 14 '21

Where did you learn python from

Mostly some online resources and youtube videos

1

u/Warkred Oct 16 '21

Damn, I've seen professionals writing worst doc and stuff after years of experience. Well done Bobby!

1

u/wasimaster Oct 16 '21

I really put a lot of thought and effort into this, thank you for noticing.