Movatterモバイル変換


[0]ホーム

URL:


Skip to content
DEV Community
Log in Create account

DEV Community

Cover image for 6 Python Built-in Modules You Need To Know About
Mithil Poojary
Mithil Poojary

Posted on

     

6 Python Built-in Modules You Need To Know About

1. Convert Python2 code to Python3 with 2to3

How many times did you turn down a python script just because it was written in Python2? 2to3 converts Python2 code to Python3.

2to3-w example.py
Enter fullscreen modeExit fullscreen mode
Before | After

Alt Text

2. Create an HTTP server with http.server

Place your index.html file and start a local webserver.
In the same directory as the index file, run:

python3-m http.server 8000
Enter fullscreen modeExit fullscreen mode

Now you can visitlocalhost:8000.

3. Pretty Print JSON using json.tool

I like to deal with most of my developer things inside the terminal, as it is generally faster to implement. Ever had to deal with JSON in your terminal? You must have if you usedcurl before.

curl-s https://dev.to/api/articles\?username=mithil467
Enter fullscreen modeExit fullscreen mode

Alt Text

The output is not pretty printed and it's impossible to make anything out of the result. Python's json.tool has got your back!

curl-s https://api.github.com/users/Mithil467 | python-m json.tool
Enter fullscreen modeExit fullscreen mode

Alt Text
I further piped the output topygmentize -l javascript for better visual impact.

4. Better text wrapping with textwrap

Back then when I was developingMitype, I had this exact problem. I had to do text wrapping based on the width of the terminal.
I ended up writing my algorithm, but now I have found this!

S="I have started my journey into blogging! Can I call myself a writer now?"S_wrapped=textwrap.fill(S,width=14)Ihavestartedmyjourneyintoblogging!CanIcallmyselfawriternow?
Enter fullscreen modeExit fullscreen mode

5. Print tokens for your code with tokenize

Tokenizing is a stage in the compilation phase where source code elements are converted into tokens. Let's tokenize the same file that we used in our 2to3 example.

python3-m tokenize app.py
Enter fullscreen modeExit fullscreen mode

Alt Text
You might want to have a look at some more builtin modules herehttps://docs.python.org/3/py-modindex.html

| Cover pic by Chris Ried on Unsplash

Top comments(0)

Subscribe
pic
Create template

Templates let you quickly answer FAQs or store snippets for re-use.

Dismiss

Are you sure you want to hide this comment? It will become hidden in your post, but will still be visible via the comment'spermalink.

For further actions, you may consider blocking this person and/orreporting abuse

Mainly working with Python. Feels amazing to be an Open-Sourcerer!
  • Location
    Mumbai, India
  • Education
    B.E. Information Technology
  • Work
    Student
  • Joined

More fromMithil Poojary

DEV Community

We're a place where coders share, stay up-to-date and grow their careers.

Log in Create account

[8]ページ先頭

©2009-2025 Movatter.jp