I’m late on the “bitwise serie” but…

Well… none is reading my serie but I’ll finish it anyway! I didn’t do it yet because I’m studying PYTHON! I’m very excited about Python, Machine Learning related things and, to setup an environment, I used the WSL (Windows Subsystem for Linux) and the Visual Studio Code to do it ( https://code.visualstudio.com/docs/remote/wsl ).

I’m very impressed about the Python itself and how the Remote WSL Development works flawlessly, even being a preview version!

Why I’m impressed about Python?

The language is easy to learn and its syntax is almost plain English!

For example, a ternary operator example:

# ternary operator
print("message if true" if True else "message if not true (false)")
print("message if true" if False else "message if not true (false)")

Easy, eh!

Another example, this time related to our ‘Bitwise posts’:

admin = 1
owner = 2
regular_user = 4

can_edit = owner
can_delete = admin | owner
can_read = admin | owner | regular_user

user_a = admin
user_b = owner
user_c = regular_user

# EDITING
print("EDITING")
action = can_edit
accessing = user_a

# no
print("Q: user_a (admin) accessing... can edit? hint: no")
message = "Edited" if bool(accessing & action) else "Denied ;)"
print(f"A: {message}")

# yes
accessing = user_b
print("\nQ: user_b (owner) accessing... can edit? hint: yes")
message = "Edited" if bool(accessing & action) else "Denied ;)"
print(f"A: {message}")

# no
accessing = user_c
print("\nQ: user_c (regular user) accessing... can edit? hint: no")
message = "Edited" if bool(accessing & action) else "Denied ;)"
print(f"A: {message}")

# DELETING
print("\n\nDELETING")
action = can_delete
accessing = user_a

# yes
print("Q: user_a (admin) accessing... can delete? hint: yes")
message = "Deleted!" if bool(accessing & action) else "Denied ;)"
print(f"A: {message}")

# yes
accessing = user_b
print("\nQ: user_b (owner) accessing... can delete? hint: yes")
message = "Deleted!" if bool(accessing & action) else "Denied ;)"
print(f"A: {message}")

# no
accessing = user_c
print("\nQ: user_c (regular user) accessing... can delete? hint: no")
message = "Deleted!" if bool(accessing & action) else "Denied ;)"
print(f"A: {message}")

# READING
print("\n\nREADING")
action = can_read
accessing = user_c

# YES
print("Q: user_a (admin) accessing... can read? hint: yes")
message = "Reading :D" if bool(accessing & action) else "Denied ;)"
print(f"A: {message}")

# YES
accessing = user_b
print("\nQ: user_b (owner) accessing... can read? hint: yes")
message = "Reading :D" if bool(accessing & action) else "Denied ;)"
print(f"A: {message}")

# YES
accessing = user_c
print("\nQ: user_c (regular user) accessing... can read? hint: YES!")
message = "Reading :D" if bool(accessing & action) else "Denied ;)"
print(f"A: {message}")

The result:

Python output

I love it! “print something” if <this expression is true> else “print this message”! For me, it’s plain English!

This is important because makes the learning curve less steep for new developers!

As soon I finish the javascript + bitwise posts, I’ll think in something about Python.

See you soon and, happy coding!

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s