I'm making a video game in upbge (which for this aspect is basically python) but to send the info between sockets it needs to be in bytes but i only found a way to do it turning it into a string, (because the list get's created with the game (i have a script that once a new entity appears it get's added to the list in the form of (Player-entity|data) but i want the data to variate between entities and i ran out of symbols to use to split it into lists so i'm trying to send the data as a list to be able to simply check it's components, but as it's a list inside of a list (and sometimes more) the bytearray won't work (at least i can't figure out how to make it work)
I'm unsure of how to summarize the code, but this is the code that I have now to generate that info
import socket
mi_socket = socket.socket()
mi_socket.bind(('127.0.0.1',55555))
mi_socket.listen()
TP = 0 #Total players
AP = 0 #Actual player
PL = ["host"] #Player List
AE = 0 #Actual entity
TE = 0 #Total entities
EL = ["|PlayerEntity$[Data]|"] #Entity list
PI = [] #Player Intel (Player name, Total Players, Player Code) (apartado uno en las comunicaciones)
order = None #Variable reservada para guardar comandos u identificaciones (ej: nombre de la entidad) en las comunicaciones
content = None #Variable reservada para almacenar la información recibida
incoming = 0 #Variable reservada para analizar el mensaje recibido de entrada
def login():
global AP
global TP
global PL
PJ = str(incoming).split("$")[1] #incoming Player
if AP <= TP:
if PJ!=str(PL[int(AP)]): #Nombre jugador que se conecta
AP+=1
login()
pass
else:
identity()
pass
if AP > TP:
PL.append(str("Player" + str(AP))) #Agregar jugador a la lista
TP = int(AP)
def identity(): #identifica de qué se trata la orden para responder en consecuencia, si se trata de entidad anota tmb la entidad en la lista
global TE
global AE
global EL
global AP
global PL
PJ = str(incoming).split("$")[1] # incoming Player
PE = str(incoming).split("$")[2] # incoming entity
if AE <= TE:
# Si nombre de jugador|nombre de objeto no és EL[AE]([1]|[2])
if str(str(PL[AP])+"-"+str(PE)) != str(str(EL[AE]).split("|")[1]).split("$")[0]:
AE+=1
identity()
pass
else:
EL[AE] = "|"+PL[AP]+"-"+PE+"$"+str(incoming).split("$")[3]+"|"
if AE > TE:
EL.append("|"+PL[AP]+"-"+PE+"$"+str(incoming).split("$")[3]+"|")
TE = int(AE)
def main():
global AP
global AE
global TE
global incoming
conexion, ip = mi_socket.accept()
incoming = conexion.recv(1024)
login()
conexion.send(str("#"+str(PL[AP])+"#"+str(EL[1:TE+1])+"#").encode())
AP=0
AE=0
conexion.close()
while True:
main()
Windows 10 (x64)
Python 3.6.3
cx_Freeze 5.1.1
pypiwin32 223 / pywin32 224
I made a module for printing, the module works fine when launching it as a script.
Once passed through cx_Freeze, the print command doesn't work without producing any error message.
Here is my setup.py for creating builds (by: python setup.py build)
# -*- coding: utf-8 -*-
import sys,os
from cx_Freeze import setup, Executable
PythonPath = os.path.split(sys.executable)[0] #get python path
includes = []
excludes = []
packages = ["win32print"]
includefiles = ["OptiWeb.ico"]
options = {"includes": includes,
"excludes": excludes,
"packages": packages,
"include_files": includefiles,
"optimize":2
}
base=None
if sys.platform == 'win32':
base = "Win32GUI"
options["include_msvcr"] = True
executables = [Executable(script="OptiPrint.py",base=base,icon="Optiweb.ico")]
setup(name = "OptiPrint",options = {"build_exe": options},version = "1.0",author = "ND",description = "print test",executables = executables)
And now my code for printing:
# coding: utf8
import win32print
class ZPLLabel(object):
def __init__(self, printerName):
self.printerName = printerName
self.printerDevice = win32print.OpenPrinter(self.printerName)
self.job = win32print.StartDocPrinter(self.printerDevice, 1, ("Etiquette", None, "RAW"))
self.eraseAll()
self.defineFormat()
def eraseAll(self):
win32print.StartPagePrinter(self.printerDevice)
str2print="~JA"
win32print.WritePrinter(self.printerDevice, str2print.encode("utf8")) #écrit le format d'étiquette
win32print.EndPagePrinter(self.printerDevice) # indique la fin de ce qu'il y a à imprimer
self.printerDevice.close() # ferme le canal d'impression et déclenche l'impression de ce qui précède
#del self.job
self.printerDevice=win32print.OpenPrinter(self.printerName)
self.job = win32print.StartDocPrinter(self.printerDevice, 1, ("Etiquette", None, "RAW"))
def defineFormat(self):
margeLeft = 150
margeTop = 20
interLine = 39
shiftLeft = 20
vDec = 25
#win32print.StartPagePrinter(p)
str2print="^XA\n" #debut de format
str2print+="^CI28"
#FO origine du champ, 100 pos x du champ en dots, 50 pos y du champ en dots
# l'imprimantes est 200 dpi (dotsper inch = 7.874 dots par mm, ici 12.7mm, 6.35mm)
#ADN : A ==> font, D==> font D, N ==> Orientation Normale, 36 hauteur caractère en dots, 20 Largeur caractère en dots
#FD données à imprimer pour le champ
#FS fin du champ
str2print+="^DFFORMAT"
str2print+="^LH"+str(margeLeft)+","+str(margeTop)
#un cadre arrondi
str2print+="^FO0,0^GB500,330,3,B,2^FS"
#str2print+="^FO"+str(shiftLeft)+","+str(interLine)+"^ADN,24,12^FDEtiquette de débit Sangle^FS\n" #format de l'étiquette
str2print+="^FO"+str(shiftLeft)+","+str(1*interLine-vDec) +"^ADN,32,14^FDOF N° : ^FS^FO"+str(shiftLeft+160)+","+str(1*interLine-vDec) +"^ADN,32,14^FN1^FS"
str2print+="^FO"+str(shiftLeft)+","+str(2*interLine-vDec) +"^ADN,32,14^FDPRODUIT : ^FS^FO"+str(shiftLeft+215)+","+str(2*interLine-vDec) +"^ADN,32,14^FN2^FS"
str2print+="^FO"+str(shiftLeft)+","+str(3*interLine-vDec) +"^ADN,24,12^FN3^FS"
str2print+="^FO"+str(shiftLeft)+","+str(4*interLine-vDec) +"^ADN,32,14^FDSANGLE : ^FS^FO"+str(shiftLeft+200)+","+str(4*interLine-vDec) +"^ADN,32,14^FN4^FS"
str2print+="^FO"+str(shiftLeft)+","+str(5*interLine-vDec) +"^ADN,24,12^FN5^FS"
str2print+="^FO"+str(shiftLeft)+","+str(6*interLine-vDec) +"^ADN,28,13^FDNombre de coupe : ^FS^FO"+str(shiftLeft+250)+","+str(6*interLine-vDec) +"^ADN,28,13^FN6^FS"
str2print+="^FO"+str(shiftLeft)+","+str(7*interLine-vDec) +"^ADN,28,13^FDLongueur coupée : ^FS^FO"+str(shiftLeft+250)+","+str(7*interLine-vDec) +"^ADN,28,13^FN7^FS"
str2print+="^FO"+str(shiftLeft)+","+str(8*interLine-vDec) +"^ADN,24,12^FDEmplacement : ^FS^FO"+str(shiftLeft+160)+","+str(8*interLine-vDec) +"^ADN,24,12^FN8^FS"
str2print+="^XZ" # fin du format d'étiquette
win32print.WritePrinter(self.printerDevice, str2print.encode("utf8")) #écrit le format d'étiquette
def printLabel(self, orderNum, productSku, productName, webSku, webName, partNum, partLength, emplacement):
str2print="^XA\n" #debut étiquette
str2print+="^XFFORMAT" #rappel du format enregistré
str2print+="^FN1^FD"+orderNum+"^FS"
str2print+="^FN2^FD"+productSku+"^FS"
str2print+="^FN3^FD"+productName+"^FS"
str2print+="^FN4^FD"+webSku+"^FS"
str2print+="^FN5^FD"+webName+"^FS"
str2print+="^FN6^FD"+str(partNum)+"^FS"
str2print+="^FN7^FD"+partLength+"^FS"
str2print+="^FN8^FD"+emplacement+"^FS"
str2print+="^XZ" # fin du format d'étiquette
win32print.WritePrinter(self.printerDevice, str2print.encode("utf8")) #écrit l'étiquette
def endLabel(self):
self.printerDevice.close() # ferme le canal d'impression et déclenche l'impression de ce qui précède
del self.job
def newPrintLabel():
zpl = ZPLLabel('Zebra ZP 450 CTP')
zpl.printLabel("20009999", "1035691", "Harnais Energy TWIN ss porte outil L/XL",
"90008318", "SA/SANGLE NOIRE 20 MM", 35, "0.38m", "Bavaroise réglable")
zpl.endLabel()
if __name__ == '__main__':
app = newPrintLabel()
I suppose, some package or dll is missing to make it run when frozen.
I tried to add win32api, win32com but it doesn't change the result.
Thanks for your help which is for sure welcome.
Try to use win32ui and win32con as done in the answers of python win32print not printing.
In this case you should also keep base defined as in your original question (regarding my comment to your question).
For those who experiment a such issue.
My code was not properly written:
To each StartDocPrinter instruction must correspond a EndDocPrinter instruction, apparently this not cause trouble with script but has impact on frozen version.
So the correct thread of instructions must be something like:
self.printerName = printerName
self.printerDevice = win32print.OpenPrinter(self.printerName)
self.job = win32print.StartDocPrinter(self.printerDevice, 1, ("Etiquette", None, "RAW"))
win32print.StartPagePrinter(self.printerDevice)
str2print="..." # define what you want to print
win32print.WritePrinter(self.printerDevice, str2print.encode("utf8")) #write
win32print.EndPagePrinter(self.printerDevice) # end the page
win32print.EndDocPrinter(self.printerDevice) # end the doc
self.printerDevice.close() # close the printer thread
I am implement a simple simulator of soccer with python using threads and lock, the app works fine but I have doubts in the way that implement the thread it seems to me that the first team has an advantage because is executing first.
def jugar(Equipo1, Equipo2):
# Busco las probabilidades de encajar por cada equipo
prob_encajar_eq1 = Equipo1.probabilidad_encajar()
prob_encajar_eq2 = Equipo2.probabilidad_encajar()
def jugar_equipo1(defensa_rival):
semaforo.acquire()
if Equipo1.hacer_pases():
Equipo1.shoot(defensa_rival)
semaforo.release()
def jugar_equipo2(defensa_rival):
semaforo.acquire()
if Equipo2.hacer_pases():
Equipo2.shoot(defensa_rival)
semaforo.release()
hilo_equipo1 = threading.Thread(name = 'hilo_eq1', target = jugar_equipo1, args = (prob_encajar_eq2,))
hilo_equipo2 = threading.Thread(name = 'hilo_eq2', target = jugar_equipo2, args = (prob_encajar_eq1,))
hilo_equipo1.start()
hilo_equipo2.start()
hilo_equipo1.join()
hilo_equipo2.join()
to make several attempts both teams, I do a cicle for a few seconds and inside the function jugar() which is the one that does the work with threads but here is were I have the doubts, because every time that jugar is executing the threads are declared again.
if __name__ == '__main__':
cargar_informacion()
eqA = Equipo(equipoA, ranking_eqA)
eqB = Equipo(equipoB, ranking_eqB)
probabilidades = porcenajes_ranking(ranking_eqA)
eqA.cargar_probabilidades(probabilidades)
probabilidades = porcenajes_ranking(ranking_eqB)
eqB.cargar_probabilidades(probabilidades)
starttime=time.time()
tiempo = 0
# creo la barra de progreso
bar = progressbar.ProgressBar(widgets=[
progressbar.Percentage(),
progressbar.Bar(),
], max_value=100).start()
# hacemos que el juego tarde aproximadamente 10seg en simularse.
while tiempo < 10:
time.sleep(0.3 - ((time.time() - starttime) % 0.3))
jugar(eqA,eqB)
tiempo = time.time() - starttime
bar += 2.8
bar.finish() # Para que finalice la barra de progreso
resultados_finales(eqA, eqB) # Mostramos el resultado final del partido.
Here's my situation:
I got a table on a database, as following:
nome_imagem estado type
57260-tracker-_tracker_face awake 0
57261-tracker-_tracker_face drowsiness 1
57268-tracker-_tracker_face noface 2
57289-tracker-_tracker_face distracted 3
57290-tracker-_tracker_face awake 1
57291-tracker-_tracker_face drowsiness 2
57293-tracker-_tracker_face noface 3
And I want to update de type column according to serveral if conditions, but I'm getting a error on line 29:
mysql.connector.errors.InternalError: Unread result found
I'm pretty sure its the way im making the query, and I already searched another questions about this before opening this one, but I couldnt solve it anyway.
EDIT 1: Changed the query and applied the solution gave by gzc to get rid of the error, but now it updates all the type column instead of just the cases where the if is true
import mysql.connector
from mysql.connector import errorcode
import os
cnx = mysql.connector.connect(user='root', database='empresa')
cursor = cnx.cursor()
fileDir = os.path.dirname(os.path.realpath(__file__))
textDir = os.path.join(fileDir, "test_img")
query = ("SELECT nome_imagem, estado, type FROM alertas ")
cursor.execute(query)
results = list(cursor)
for (nome_imagem, estado, type) in results:
print nome_imagem, estado
my_file_name = nome_imagem+'.txt'
my_file = open("test_img/"+my_file_name, 'r')
content = my_file.readline()
status = content.strip().split()[-1].split("=")[1]
face = content.strip().split()[0].split("=")[1]
print status, face #1 tem face, 0 nao tem
if (face == '1' and estado == status): #se tem cara e o estado que tem na bd for igual ao estado que o programa classificou = correto
print "correto"
cursor.execute("UPDATE alertas SET type='1' WHERE nome_imagem=nome_imagem")
if (face == '1' and estado == 'drowsiness' and status == 'awake') or (face == 1 and estado == 'awake' and status == 'drowsiness'): #verificar isto
print "trocado"
if (estado != '' and face == '0'): # se tiver estado mas nao tiver cara classifico logo como errado 3
print "errado"
if (estado == 'distracted' and face == '1'): # se tem cara mas for distracted deixo normal pois nao consigo classificar
print "normal"
cursor.close()
cnx.close()
What am I doing wrong?
Thanks
EDIT 2: gzc solver it again :)
You must read all results before doing another operation on the same cursor.
...
query = ("SELECT nome_imagem, estado, type FROM alertas ")
cursor.execute(query)
results = list(cursor)
for (nome_imagem, estado, type) in results:
...
I'm working on the implementation of several algorithms to compute shortest paths on graphs.
I have managed to implement Dijkstra's algorithm sequentially and I'm now trying to optimize my algorithm through the multiprocessing module of Python.
As a whole the code works. What I am trying to do here is :
First to check how many cpus I can work on with nb_cpu = mp.cpu_count()
Then dividing all the nodes I have in my graph accordingly
Finally calling the method subprocess_dijkstra that should compute the dijkstra algorithm for each of the nodes it is given as an argument (the idea being that each process only has to compute the algorithm for a smaller part of the graph).
When I run my script (called from a main.py file where I just format the data to suit my needs), I have 4 processes launched as I should.
However, they do not seem to execute the for node in nodes loop defined in subprocess_dijkstra.
Each process only computes the code once and then they go on hold indefinitely...
It is my first attempt at multiprocessing under Python so I may have missed a detail. Does anybody have an idea ?
When I interrupt the script, python tells me that the interruption takes place on the p.join() line.
Thanks to anyone helping me :)
Here is my code :
import multiprocessing as mp
def subprocess_dijkstra(do_print, nodes, tab_contenu, tab_distances):
tab_dist_initial = dict(tab_distances)
tab_dist = dict()
for node in nodes:
visited_nodes = list()
tab_dist = dict(tab_dist_initial)
dmin = -1
resultat = ""
filename = "dijkstra"+str(node)+".txt"
if do_print:
dt = open(filename, 'w')
tab_dist[node] = 0
"""Ligne de résultat initiale"""
for valeur in tab_dist.values():
resultat += str(valeur)
resultat += " "
resultat += "\n"
dt.write(resultat)
while len(visited_nodes) != len(tab_contenu):
""" On se place sur le noeud non visité qui a la distance minimale de notre départ """
for cle, valeur in tab_dist.items():
if cle not in visited_nodes:
if dmin ==-1 or valeur<dmin:
dmin = valeur
node = cle
""" On vérifie que le noeud n'a pas déjà été visité """
if (node not in visited_nodes):
""" On regarde les fils de ce noeud et la longueur des arcs"""
for cle,valeur in tab_contenu[node].items():
tab_dist[cle] = min(tab_dist[cle], tab_dist[node]+valeur)
visited_nodes.append(node)
if do_print:
resultat = ""
""" Ligne de résultat """
for valeur in tab_dist.values():
resultat += str(valeur)
resultat += " "
resultat += "\n"
dt.write(resultat)
if do_print:
dt.close()
def main(do_print,donnees):
tab_contenu = donnees[1]
nb_nodes = int(donnees[0])
tab_distances = {x: float('inf') for x in range(nb_nodes)}
args=[(do_print, x, tab_contenu, tab_distances) for x in range(nb_nodes)]
nb_cpu = mp.cpu_count()
pool = mp.Pool(processes = nb_cpu)
pool.starmap(subprocess_dijkstra, args)
pool.close()
pool.join()
I have found the source of my problems.
The tab_dist[node] = 0 was misplaced and should have been put before the if do_print: statement.
All is now working.