Discuss Scratch

Goldtabby
Scratcher
95 posts

How to Encode and Decode in python

Hey, I am making a project that interfaces with the python server I made, but i do not know how to encode and decode the way I do it on scratch, because the scratch project encodes the username to be put on a cloud variable. The python server gets that data, but I need to decode! Please Help!

Last edited by Goldtabby (May 15, 2021 16:07:21)

ninjaMAR
Scratcher
1000+ posts

How to Encode and Decode in python

Make a GET request to https://clouddata.scratch.mit.edu/logs?projectid={PROJECTID}&limit=LIMIT&offset=OFFSET and turn it into json
import requests #install it by running "pip install requests"
r = requests.get('https://clouddata.scratch.mit.edu/logs?projectid={PROJECTID}&limit=LIMIT&offset=OFFSET')
vars = r.json()[0]
print(vars) # each item is a dict which looks like "{"user":"username","verb":"create_var","name":"☁ name","value":0,"timestamp":timestamp}"
Goldtabby
Scratcher
95 posts

How to Encode and Decode in python

ninjaMAR wrote:

Make a GET request to https://clouddata.scratch.mit.edu/logs?projectid={PROJECTID}&limit=LIMIT&offset=OFFSET and turn it into json
Thank You!!!!!!!!
nampinanathali
Scratcher
1000+ posts

How to Encode and Decode in python

Goldtabby wrote:

Hey, I am making a project that interfaces with the python server I made, but i do not know how to retrieve a cloud variable, can anybody help?

You can also use the scratch client. The documentation is here.

Enjoy
Raihan142857
Scratcher
1000+ posts

How to Encode and Decode in python

nampinanathali wrote:

Goldtabby wrote:

Hey, I am making a project that interfaces with the python server I made, but i do not know how to retrieve a cloud variable, can anybody help?

You can also use the scratch client. The documentation is here.

Enjoy
I also have some docs on the wiki of the GH repo but they're not finished
wvj
Scratcher
1000+ posts

How to Encode and Decode in python

ninjaMAR wrote:

Make a GET request to https://clouddata.scratch.mit.edu/logs?projectid={PROJECTID}&limit=LIMIT&offset=OFFSET and turn it into json
import requests #install it by running "pip install requests"
r = requests.get('https://clouddata.scratch.mit.edu/logs?projectid={PROJECTID}&limit=LIMIT&offset=OFFSET')
vars = r.json()[0]
print(vars) # each item is a dict which looks like "{"user":"username","verb":"create_var","name":"☁ name","value":0,"timestamp":timestamp}"
What is “LIMIT” and “OFFSET”?
Maximouse
Scratcher
1000+ posts

How to Encode and Decode in python

wvj wrote:

ninjaMAR wrote:

Make a GET request to https://clouddata.scratch.mit.edu/logs?projectid={PROJECTID}&limit=LIMIT&offset=OFFSET and turn it into json
import requests #install it by running "pip install requests"
r = requests.get('https://clouddata.scratch.mit.edu/logs?projectid={PROJECTID}&limit=LIMIT&offset=OFFSET')
vars = r.json()[0]
print(vars) # each item is a dict which looks like "{"user":"username","verb":"create_var","name":"☁ name","value":0,"timestamp":timestamp}"
What is “LIMIT” and “OFFSET”?
Limit is the maximum number of log items to show (up to 40), offset is the index of the first item (0 for the first LIMIT items, then LIMIT, 2×LIMIT, 3×LIMIT etc. for subsequent pages).
Goldtabby
Scratcher
95 posts

How to Encode and Decode in python

bump
Goldtabby
Scratcher
95 posts

How to Encode and Decode in python

bump
imfh
Scratcher
1000+ posts

How to Encode and Decode in python

Goldtabby wrote:

bump
Why are you bumping? If you still need help, you need to explain what you are having trouble with or nobody will know how to help you.
ninjaMAR
Scratcher
1000+ posts

How to Encode and Decode in python

imfh wrote:

Goldtabby wrote:

bump
Why are you bumping? If you still need help, you need to explain what you are having trouble with or nobody will know how to help you.
The OP edited the first post
wvj
Scratcher
1000+ posts

How to Encode and Decode in python

Encoding:

chars = ['', '', '', '', '', '', '', '', '', 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z', '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', '+', '-', '.', ' ', '_']
 
 
def encode (val):
    val = val.lower()
    letternum = 1
    val = str(val)
    encoded = ""
    
    for i in range (1, len(str(val))+1):
       encoded = str(encoded) + str(chars.index(val[letternum-1])+1)
       letternum += 1
 
 
    return int(encoded + "00")
 
  
print(encode(input("Enter a word: ")))


Decoding:

chars = ['', '', '', '', '', '', '', '', '', 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z', '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', '+', '-', '.', ' ', '_']
 
def decode (val):
    letternum = 1
    value = ""
    idx = None
 
    while True:
        val = str(val)
        idx = val[letternum-1] + val[letternum]
        letternum += 2
        if int(idx) < 1:
            break
        value = value + chars[int(idx) - 1]
    return value
 
 
print(decode(int(input("Enter an encoded value: "))))
Goldtabby
Scratcher
95 posts

How to Encode and Decode in python

thanks man

wvj wrote:

Encoding:

chars = ['', '', '', '', '', '', '', '', '', 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z', '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', '+', '-', '.', ' ', '_']
 
 
def encode (val):
    val = val.lower()
    letternum = 1
    val = str(val)
    encoded = ""
    
    for i in range (1, len(str(val))+1):
       encoded = str(encoded) + str(chars.index(val[letternum-1])+1)
       letternum += 1
 
 
    return int(encoded + "00")
 
  
print(encode(input("Enter a word: ")))


Decoding:

chars = ['', '', '', '', '', '', '', '', '', 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z', '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', '+', '-', '.', ' ', '_']
 
def decode (val):
    letternum = 1
    value = ""
    idx = None
 
    while True:
        val = str(val)
        idx = val[letternum-1] + val[letternum]
        letternum += 2
        if int(idx) < 1:
            break
        value = value + chars[int(idx) - 1]
    return value
 
 
print(decode(int(input("Enter an encoded value: "))))
wvj
Scratcher
1000+ posts

How to Encode and Decode in python

Goldtabby wrote:

thanks man
np
TheCloudDev
Scratcher
100+ posts

How to Encode and Decode in python

wvj wrote:

Encoding:

chars = ['', '', '', '', '', '', '', '', '', 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z', '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', '+', '-', '.', ' ', '_']
 
 
def encode (val):
    val = val.lower()
    letternum = 1
    val = str(val)
    encoded = ""
    
    for i in range (1, len(str(val))+1):
       encoded = str(encoded) + str(chars.index(val[letternum-1])+1)
       letternum += 1
 
 
    return int(encoded + "00")
 
  
print(encode(input("Enter a word: ")))


Decoding:

chars = ['', '', '', '', '', '', '', '', '', 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z', '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', '+', '-', '.', ' ', '_']
 
def decode (val):
    letternum = 1
    value = ""
    idx = None
 
    while True:
        val = str(val)
        idx = val[letternum-1] + val[letternum]
        letternum += 2
        if int(idx) < 1:
            break
        value = value + chars[int(idx) - 1]
    return value
 
 
print(decode(int(input("Enter an encoded value: "))))
I tried to decode a variable itself but I get:

But when I try a plain string of numbers, (In single quotes) It works. How do I fix this?
Maximouse
Scratcher
1000+ posts

How to Encode and Decode in python

TheCloudDev wrote:

I tried to decode a variable itself but I get:

But when I try a plain string of numbers, (In single quotes) It works. How do I fix this?
What was the value of the variable?
wvj
Scratcher
1000+ posts

How to Encode and Decode in python

TheCloudDev wrote:

wvj wrote:

Encoding:

chars = ['', '', '', '', '', '', '', '', '', 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z', '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', '+', '-', '.', ' ', '_']
 
 
def encode (val):
    val = val.lower()
    letternum = 1
    val = str(val)
    encoded = ""
    
    for i in range (1, len(str(val))+1):
       encoded = str(encoded) + str(chars.index(val[letternum-1])+1)
       letternum += 1
 
 
    return int(encoded + "00")
 
  
print(encode(input("Enter a word: ")))


Decoding:

chars = ['', '', '', '', '', '', '', '', '', 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z', '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', '+', '-', '.', ' ', '_']
 
def decode (val):
    letternum = 1
    value = ""
    idx = None
 
    while True:
        val = str(val)
        idx = val[letternum-1] + val[letternum]
        letternum += 2
        if int(idx) < 1:
            break
        value = value + chars[int(idx) - 1]
    return value
 
 
print(decode(int(input("Enter an encoded value: "))))
I tried to decode a variable itself but I get:

But when I try a plain string of numbers, (In single quotes) It works. How do I fix this?
What value did you try to decode?
TheCloudDev
Scratcher
100+ posts

How to Encode and Decode in python

wvj wrote:

TheCloudDev wrote:

wvj wrote:

Encoding:
snip
I tried to decode a variable itself but I get:

But when I try a plain string of numbers, (In single quotes) It works. How do I fix this?
What value did you try to decode?
Sorry, It's working now. I got the values to decode from the API, but that function had a bug so it was returning blank. I fixed it and it's working now
Thanks!
wvj
Scratcher
1000+ posts

How to Encode and Decode in python

TheCloudDev wrote:

Sorry, It's working now. I got the values to decode from the API, but that function had a bug so it was returning blank. I fixed it and it's working now
Thanks!
:­­­­­­­­­­­­­­­­­­­­­­­­­­­D
Chiroyce
Scratcher
1000+ posts

How to Encode and Decode in python

wvj wrote:

:­­­­­­­­­­­­­­­­­­­­­­­­­­­D
where did you type that smiley from? it didnt turn into as it has extra unicode characters, source

Powered by DjangoBB