Discuss Scratch

rishi272011
Scratcher
100 posts

PyHelp - The official Scratch Python help forum | Ask, help, discuss, & more! | +800 posts! |

applejuiceproduc wrote:

Not sure if this is still active but whatever.

I have this function:
def got(line):
    global goto_line
    goto_line = line - 1
When I run it I get this error
Traceback (most recent call last):
File "/Users/michael/repos/chipm/assembler.py", line 93, in <module>
get_command(command, args, lineno + 1)
File "/Users/michael/repos/chipm/assembler.py", line 18, in get_command
ret()
File "/Users/michael/repos/chipm/assembler.py", line 56, in ret
got(return_line)
File "/Users/michael/repos/chipm/assembler.py", line 40, in got
goto_line = line - 1
~~~~~^~~
TypeError: unsupported operand type(s) for -: 'NoneType' and 'int'

I have checked that the line argument is an int, not a None type using the vscode debugger. It also works completely fine when using the vscode debugger.

Full code:
<removed>

Input file:
PUT Start 1 2 3
PTN s
PUT n
SRO hello
PTN hello world
RET
GOT 5
PTN hello moon
END

I believe that cal was never called in the program so return_line is None when ret is called.
applejuiceproduc
Scratcher
1000+ posts

PyHelp - The official Scratch Python help forum | Ask, help, discuss, & more! | +800 posts! |

rishi272011 wrote:

applejuiceproduc wrote:

snip

I believe that cal was never called in the program so return_line is None when ret is called.
Yeah thanks I see what I did wrong now. I coded it so the ret function would go to return_line which is set by cal, but I used the got function instead, which means it should skip over the ret function. I changed the code so it only goes to return_line if it has a value.
wvzack
Scratcher
500+ posts

PyHelp - The official Scratch Python help forum | Ask, help, discuss, & more! | +800 posts! |

I am trying to install the opensky api modual however I keep getting a pip error:
'pip' is not recognized as an internal or external command,
operable program or batch file.
I have tried a few things however none of them have worked. Here is the command to download the api:
pip install -e "git+https://github.com/openskynetwork/opensky-api.git#egg=python&subdirectory=python"

Thanks!
mybearworld
Scratcher
1000+ posts

PyHelp - The official Scratch Python help forum | Ask, help, discuss, & more! | +800 posts! |

wvzack wrote:

(#1003)
I am trying to install the opensky api modual however I keep getting a pip error:
'pip' is not recognized as an internal or external command,
operable program or batch file.
Try using “pip3” instead
wvzack
Scratcher
500+ posts

PyHelp - The official Scratch Python help forum | Ask, help, discuss, & more! | +800 posts! |

mybearworld wrote:

wvzack wrote:

(#1003)
I am trying to install the opensky api modual however I keep getting a pip error:
'pip' is not recognized as an internal or external command,
operable program or batch file.
Try using “pip3” instead
same error but pip3 instead of pip
Maximouse
Scratcher
1000+ posts

PyHelp - The official Scratch Python help forum | Ask, help, discuss, & more! | +800 posts! |

wvzack wrote:

same error but pip3 instead of pip
Try python -m pip and python3 -m pip.
wvzack
Scratcher
500+ posts

PyHelp - The official Scratch Python help forum | Ask, help, discuss, & more! | +800 posts! |

Maximouse wrote:

wvzack wrote:

same error but pip3 instead of pip
Try python -m pip and python3 -m pip.
worked thanks!

wvzack
Scratcher
500+ posts

PyHelp - The official Scratch Python help forum | Ask, help, discuss, & more! | +800 posts! |

I have a list of aircraft information and I need to turn them into a dictionary. Here is the string before it has been modified:
[<(A319) N715UW - Altitude: 0 - Ground Speed: 0 - Heading: 188>, <(A321) N375DN - Altitude: 0 - Ground Speed: 0 - Heading: 323>, <(B739) N844DN - Altitude: 0 - Ground Speed: 4 - Heading: 357>, <(B38M) N322TH - Altitude: 0 - Ground Speed: 0 - Heading: 2>, <(A321) N304DN - Altitude: 0 - Ground Speed: 0 - Heading: 90>, <(B739) N948DZ - Altitude: 0 - Ground Speed: 0 - Heading: 39>, <(B739) N843DN - Altitude: 0 - Ground Speed: 2 - Heading: 90>, <(B712) N896AT - Altitude: 1425 - Ground Speed: 166 - Heading: 267>, <(B739) N938DZ - Altitude: 0 - Ground Speed: 19 - Heading: 90>, <(B739) N928DU - Altitude: 0 - Ground Speed: 11 - Heading: 2>]

I made a simple script to try and fix this and make it a dictionary:
replace_dict= {":": ",", " - ": ", ", "(": "", ")": ":", "<": "",  ">": ""}
for old, new in replace_dict.items():
flights = flights.replace(old, new)
print(flights)
(flights is the string up above)

This outputs:
[A319: N715UW, Altitude, 0, Ground Speed, 0, Heading, 188, A321: N375DN, Altitude, 0, Ground Speed, 0, Heading, 323, B739: N844DN, Altitude, 0, Ground Speed, 4, Heading, 357, B38M: N322TH, Altitude, 0, Ground Speed, 0, Heading, 2, A321: N304DN, Altitude, 0, Ground Speed, 0, Heading, 90, B739: N948DZ, Altitude, 0, Ground Speed, 0, Heading, 39, B739: N843DN, Altitude, 0, Ground Speed, 2, Heading, 90, B712: N896AT, Altitude, 1425, Ground Speed, 166, Heading, 267, B739: N938DZ, Altitude, 0, Ground Speed, 19, Heading, 90, B739: N928DU, Altitude, 0, Ground Speed, 11, Heading, 2]

Which could work if there weren't multiple of the same type of aircraft. So what I need to do is swap the aircraft type (what would currently be the key) with the call sign (the first value of what the dictionary would be if it was a dictionary) as no to aircraft have the same call sign.

Edit: I know I am leaving out some stuff I would need to change to make it a dictionary.

Thanks!!

Last edited by wvzack (Aug. 13, 2024 15:10:26)

MonkeyBean2
Scratcher
100+ posts

PyHelp - The official Scratch Python help forum | Ask, help, discuss, & more! | +800 posts! |

wvzack wrote:

MonkeyBean2 wrote:

wvzack wrote:

Not a problem here but when I run my code in run mode in vscode it says no such file as cobblestone.png however when I run the same code in debug mode it works!
Vscode might be running it in a different directory, try this to see where it's running it:
import os
print("dir", os.getcwd())
I'd recommend just running your code from your terminal.
If i run it in my terminal the same thing happens. It is running from the proper directory.

Type “ls” in your terminal when you are in the proper directory and check if you see cobblestone.png. Is it in a sub-directory?
wvzack
Scratcher
500+ posts

PyHelp - The official Scratch Python help forum | Ask, help, discuss, & more! | +800 posts! |

MonkeyBean2 wrote:

wvzack wrote:

MonkeyBean2 wrote:

wvzack wrote:

Not a problem here but when I run my code in run mode in vscode it says no such file as cobblestone.png however when I run the same code in debug mode it works!
Vscode might be running it in a different directory, try this to see where it's running it:
import os
print("dir", os.getcwd())
I'd recommend just running your code from your terminal.
If i run it in my terminal the same thing happens. It is running from the proper directory.

Type “ls” in your terminal when you are in the proper directory and check if you see cobblestone.png. Is it in a sub-directory?
so i fixed this problem by getting bookworm (the newest raspberry pi os) which automatically runs python 3.11 which is compatible with what I am doing.
Thanks for the help though!
MonkeyBean2
Scratcher
100+ posts

PyHelp - The official Scratch Python help forum | Ask, help, discuss, & more! | +800 posts! |

wvzack wrote:

I have a list of aircraft information and I need to turn them into a dictionary. Here is the string before it has been modified:
[<(A319) N715UW - Altitude: 0 - Ground Speed: 0 - Heading: 188>, <(A321) N375DN - Altitude: 0 - Ground Speed: 0 - Heading: 323>, <(B739) N844DN - Altitude: 0 - Ground Speed: 4 - Heading: 357>, <(B38M) N322TH - Altitude: 0 - Ground Speed: 0 - Heading: 2>, <(A321) N304DN - Altitude: 0 - Ground Speed: 0 - Heading: 90>, <(B739) N948DZ - Altitude: 0 - Ground Speed: 0 - Heading: 39>, <(B739) N843DN - Altitude: 0 - Ground Speed: 2 - Heading: 90>, <(B712) N896AT - Altitude: 1425 - Ground Speed: 166 - Heading: 267>, <(B739) N938DZ - Altitude: 0 - Ground Speed: 19 - Heading: 90>, <(B739) N928DU - Altitude: 0 - Ground Speed: 11 - Heading: 2>]

I made a simple script to try and fix this and make it a dictionary:
replace_dict= {":": ",", " - ": ", ", "(": "", ")": ":", "<": "",  ">": ""}
for old, new in replace_dict.items():
flights = flights.replace(old, new)
print(flights)
(flights is the string up above)

This outputs:
[A319: N715UW, Altitude, 0, Ground Speed, 0, Heading, 188, A321: N375DN, Altitude, 0, Ground Speed, 0, Heading, 323, B739: N844DN, Altitude, 0, Ground Speed, 4, Heading, 357, B38M: N322TH, Altitude, 0, Ground Speed, 0, Heading, 2, A321: N304DN, Altitude, 0, Ground Speed, 0, Heading, 90, B739: N948DZ, Altitude, 0, Ground Speed, 0, Heading, 39, B739: N843DN, Altitude, 0, Ground Speed, 2, Heading, 90, B712: N896AT, Altitude, 1425, Ground Speed, 166, Heading, 267, B739: N938DZ, Altitude, 0, Ground Speed, 19, Heading, 90, B739: N928DU, Altitude, 0, Ground Speed, 11, Heading, 2]

Which could work if there weren't multiple of the same type of aircraft. So what I need to do is swap the aircraft type (what would currently be the key) with the call sign (the first value of what the dictionary would be if it was a dictionary) as no to aircraft have the same call sign.

Edit: I know I am leaving out some stuff I would need to change to make it a dictionary.

Thanks!!
How are you getting that string? It looks suspiciously like a list of python objects converted to a string… If it is that, then I recommend just accessing the objects directly instead of converting it to a string. Otherwise, you can use this:

def parseAircraftDataString(z):
    return dict([(y[0][0], dict([y[0][1]] + y[1:])) for y in [[(lambda x: (x[1], ("type", x[0].strip("()"))))(m.strip("><").split(" ")) if i == 0 else (["altitude", "ground_speed", "heading"][i-1], float(m.split(": ")[1].strip("<>"))) for i, m in enumerate(d.split(" - "))] for d in z.strip("[]").split(", ")]])

It creates a python dictionary like so (shortened and formatted):

{
  'N715UW': {
      'type': 'A319',
      'altitude': 0.0,
      'ground_speed': 0.0,
      'heading': 188.0
  },
  'N375DN': {
      'type': 'A321',
      'altitude': 0.0,
      'ground_speed': 0.0,
      'heading': 323.0
  },
  'N844DN': {
      'type': 'B739',
      'altitude': 0.0,
      'ground_speed': 4.0,
      'heading': 357.0
  }
}

Last edited by MonkeyBean2 (Aug. 13, 2024 15:35:06)

wvzack
Scratcher
500+ posts

PyHelp - The official Scratch Python help forum | Ask, help, discuss, & more! | +800 posts! |

MonkeyBean2 wrote:

wvzack wrote:

-snip-
How are you getting that string? It looks suspiciously like a list of python objects converted to a string… If it's that, then I recommend just accessing the objects directly instead of converting it to a string. Otherwise, you can use this:

def parseAircraftDataString(z):
    return dict([(y[0][0], dict([y[0][1]] + y[1:])) for y in [[(lambda x: (x[1], ("type", x[0].strip("()"))))(m.strip("><").split(" ")) if i == 0 else (["altitude", "ground_speed", "heading"][i-1], m.split(": ")[1].strip("<>")) for i, m in enumerate(d.split(" - "))] for d in z.strip("[]").split(", ")]])
I am getting it from a (badly documented) api.
Thanks this works well. (though I dont understand a thing about it and am about to go ask Gemini(google chatgpt) to explain it)
MonkeyBean2
Scratcher
100+ posts

PyHelp - The official Scratch Python help forum | Ask, help, discuss, & more! | +800 posts! |

wvzack wrote:

MonkeyBean2 wrote:

wvzack wrote:

-snip-
How are you getting that string? It looks suspiciously like a list of python objects converted to a string… If it's that, then I recommend just accessing the objects directly instead of converting it to a string. Otherwise, you can use this:

def parseAircraftDataString(z):
    return dict([(y[0][0], dict([y[0][1]] + y[1:])) for y in [[(lambda x: (x[1], ("type", x[0].strip("()"))))(m.strip("><").split(" ")) if i == 0 else (["altitude", "ground_speed", "heading"][i-1], m.split(": ")[1].strip("<>")) for i, m in enumerate(d.split(" - "))] for d in z.strip("[]").split(", ")]])
I am getting it from a (badly documented) api.
Thanks this works well. (though I dont understand a thing about it and am about to go ask Gemini(google chatgpt) to explain it)
Ok, by the way use my updated version, it converts the altitudes, headings and ground speed values to python floats. If I did this with regex it would probably look a lot nicer lol.

If the API ever decides to change the order of stuff or add more parameters, tell me and I'll write a more robust parser using regex. This code will break and either error or fail silently. It does not handle errors very well lol.

Last edited by MonkeyBean2 (Aug. 13, 2024 15:42:34)

redspacecat
Scratcher
500+ posts

PyHelp - The official Scratch Python help forum | Ask, help, discuss, & more! | +800 posts! |

Does anyone know how I would make an auto clicker in python?
wvzack
Scratcher
500+ posts

PyHelp - The official Scratch Python help forum | Ask, help, discuss, & more! | +800 posts! |

redspacecat wrote:

Does anyone know how I would make an auto clicker in python?
I remember this tutorial being good https://www.youtube.com/watch?v=jXx3acg34S0
redspacecat
Scratcher
500+ posts

PyHelp - The official Scratch Python help forum | Ask, help, discuss, & more! | +800 posts! |

wvzack wrote:

redspacecat wrote:

Does anyone know how I would make an auto clicker in python?
I remember this tutorial being good https://www.youtube.com/watch?v=jXx3acg34S0
Thanks
MonkeyBean2
Scratcher
100+ posts

PyHelp - The official Scratch Python help forum | Ask, help, discuss, & more! | +800 posts! |

redspacecat wrote:

Does anyone know how I would make an auto clicker in python?

import pyautogui
pyautogui.click()
wvzack
Scratcher
500+ posts

PyHelp - The official Scratch Python help forum | Ask, help, discuss, & more! | +800 posts! |

redspacecat wrote:

wvzack wrote:

redspacecat wrote:

Does anyone know how I would make an auto clicker in python?
I remember this tutorial being good https://www.youtube.com/watch?v=jXx3acg34S0
Thanks
oh sorry that was the wrong link heres the right one https://www.youtube.com/watch?v=4hdK9Gey76E
_NovaNebula_
Scratcher
1000+ posts

PyHelp - The official Scratch Python help forum | Ask, help, discuss, & more! | +800 posts! |

MonkeyBean2 wrote:

redspacecat wrote:

Does anyone know how I would make an auto clicker in python?

import pyautogui
pyautogui.click()
I actually made a typing bot yesterday, but I used pynput instead of pyautogui.

(also for auto clicking you would probably want to put the clicking function in a loop)
wvzack
Scratcher
500+ posts

PyHelp - The official Scratch Python help forum | Ask, help, discuss, & more! | +800 posts! |

for some reason my script is outputting (in what i can see) extremely random stuff. This code is setting the file to what it should be ( ) the first time it runs however the second time it is not…. and is set to:
['[', "'", 'w', 'v', 'z', 'a', 'c', 'k', '1', "'", ']', 'wvzack1']
which is very wrong

Here is my code:
import json

def read_file(filename):
with open(filename, "r") as f:
return f.read().strip()

def write_file(filename, content):
with open(filename, "w") as f:
if isinstance(content, dict):
f.write(json.dumps(content, indent=4))
else:
f.write(str(content))

def read_file_votes(filename="vote_integer.txt"):
vote_data = read_file(filename)
if vote_data:
return json.loads(vote_data)
else:
return {}

def write_file_votes(votes_dict):
write_file("vote_integer.txt", votes_dict)

def vote(username, choice):
votes_dict = read_file_votes()
if choice not in votes_dict:
votes_dict[choice] = 0

votes_dict[choice] += 1

# Update username file
usernames_list = list(read_file("usernames.txt"))
if username not in usernames_list:
usernames_list.append(username)
write_file("usernames.txt", usernames_list) # Just write the list directly
write_file_votes(votes_dict)

# Example usage
vote("wvzack1", "test1")

Thanks!!!

Last edited by wvzack (Sept. 15, 2024 03:24:05)

Powered by DjangoBB