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
Make a GET request to https://clouddata.scratch.mit.edu/logs?projectid={PROJECTID}&limit=LIMIT&offset=OFFSET and turn it into jsonThank You!!!!!!!!
- nampinanathali
-
Scratcher
1000+ 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 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
I also have some docs on the wiki of the GH repo but they're not finishedHey, 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
- wvj
-
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 jsonWhat is “LIMIT” and “OFFSET”?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}"
- Maximouse
-
Scratcher
1000+ posts
How to Encode and Decode in python
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).Make a GET request to https://clouddata.scratch.mit.edu/logs?projectid={PROJECTID}&limit=LIMIT&offset=OFFSET and turn it into jsonWhat is “LIMIT” and “OFFSET”?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}"
- imfh
-
Scratcher
1000+ posts
How to Encode and Decode in python
bumpWhy 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
The OP edited the first postbumpWhy 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.
- wvj
-
Scratcher
1000+ posts
How to Encode and Decode in python
Encoding:
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 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
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: "))))
- TheCloudDev
-
Scratcher
100+ posts
How to Encode and Decode in python
Encoding:I tried to decode a variable itself but I get: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: "))))

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
I tried to decode a variable itself but I get:What was the value of the variable?
But when I try a plain string of numbers, (In single quotes) It works. How do I fix this?
- wvj
-
Scratcher
1000+ posts
How to Encode and Decode in python
What value did you try to decode?Encoding:I tried to decode a variable itself but I get: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: "))))
But when I try a plain string of numbers, (In single quotes) It works. How do I fix this?
- TheCloudDev
-
Scratcher
100+ posts
How to Encode and Decode in python
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 nowWhat value did you try to decode?Encoding:I tried to decode a variable itself but I get:
snip
But when I try a plain string of numbers, (In single quotes) It works. How do I fix this?
Thanks!

- wvj
-
Scratcher
1000+ posts
How to Encode and Decode in python
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:D![]()
Thanks!