diff --git a/.idea/h2inc.iml b/.idea/h2inc.iml new file mode 100644 index 0000000..1de84ce --- /dev/null +++ b/.idea/h2inc.iml @@ -0,0 +1,12 @@ + + + + + + + + + + \ No newline at end of file diff --git a/.idea/misc.xml b/.idea/misc.xml new file mode 100644 index 0000000..7ba73c2 --- /dev/null +++ b/.idea/misc.xml @@ -0,0 +1,4 @@ + + + + \ No newline at end of file diff --git a/.idea/modules.xml b/.idea/modules.xml new file mode 100644 index 0000000..0cc5137 --- /dev/null +++ b/.idea/modules.xml @@ -0,0 +1,8 @@ + + + + + + + + \ No newline at end of file diff --git a/.idea/vcs.xml b/.idea/vcs.xml new file mode 100644 index 0000000..94a25f7 --- /dev/null +++ b/.idea/vcs.xml @@ -0,0 +1,6 @@ + + + + + + \ No newline at end of file diff --git a/Gui test/16_filechooser.glade b/Gui test/16_filechooser.glade new file mode 100644 index 0000000..d867d52 --- /dev/null +++ b/Gui test/16_filechooser.glade @@ -0,0 +1,145 @@ + + + + + + + image/* + + + + + image/jpeg + + + + + image/png + + + + 200 + True + False + 5 + + + 300 + 200 + False + + + + True + False + vertical + True + + + Choose an image file... + True + True + True + + + + False + True + 0 + + + + + Choose folder... + True + True + True + + + + False + True + 1 + + + + + + + + + + 800 + 500 + False + dialog + window + window + preview + False + + + + + + + False + vertical + + + False + + + gtk-cancel + True + True + True + True + + + True + True + 2 + + + + + gtk-apply + True + True + True + True + + + True + True + 3 + + + + + False + False + 0 + + + + + + button2 + button1 + + + + True + False + Choose image... + True + + + + button2 + button1 + + + diff --git a/Gui test/16_filechooser.py b/Gui test/16_filechooser.py new file mode 100644 index 0000000..f8be92c --- /dev/null +++ b/Gui test/16_filechooser.py @@ -0,0 +1,95 @@ +#!/usr/bin/python +# -*- coding: utf-8 -*- + +import os +import sys +import gi +gi.require_version('Gtk', '3.0') +from gi.repository import Gtk, Gio, GdkPixbuf + +class Handler: + + def on_window_destroy(self,window): + window.close() + + def on_dialog_close(self,widget,*event): + widget.hide_on_delete() + return True + + def on_filechooser_dialog_response(self,widget,response): + if response == -6: + print("Cancel") + elif response == -5: + print("File selection: %s" % widget.get_filename()) + self.on_dialog_close(widget) + + def on_filechooser_dialog_file_activated(self,widget): + self.on_filechooser_dialog_response(widget,-5) + + def on_filechooser_dialog_update_preview(self,widget): + if widget.get_filename() != None and os.path.isfile(widget.get_filename()): + pixbuf = GdkPixbuf.Pixbuf.new_from_file_at_scale(widget.get_filename(),200,200,True) + app.obj("preview").set_from_pixbuf(pixbuf) + + def on_file_button_clicked(self,widget): + app.obj("filechooser_dialog").show_all() + + def on_dir_button_clicked(self,widget): + + dialog = Gtk.FileChooserDialog("Choose a folder", + app.obj("window"), + Gtk.FileChooserAction.SELECT_FOLDER, + (Gtk.STOCK_CANCEL, Gtk.ResponseType.CANCEL, + Gtk.STOCK_APPLY, Gtk.ResponseType.OK)) + dialog.set_default_size(600, 300) + + response = dialog.run() + if response == Gtk.ResponseType.OK: + print("Folder selection: %s" % dialog.get_filename()) + elif response == Gtk.ResponseType.CANCEL: + print("Cancel") + + dialog.destroy() + +class ExampleApp: + + def __init__(self): + + self.app = Gtk.Application.new("org.application.test", Gio.ApplicationFlags(0)) + self.app.connect("activate", self.on_app_activate) + self.app.connect("shutdown", self.on_app_shutdown) + + def on_app_activate(self, app): + builder = Gtk.Builder() + builder.add_from_file("16_filechooser.glade") + builder.connect_signals(Handler()) + + self.obj = builder.get_object + self.obj("window").set_application(app) + self.obj("window").set_wmclass("Filechooser example","Filechooser example") + self.obj("window").show_all() + + #add filters to filechooser dialog + self.obj("filefilter").set_name("Image files") + self.obj("filechooser_dialog").add_filter(self.obj("filefilter")) + self.obj("png_filter").set_name("PNG files") + self.obj("filechooser_dialog").add_filter(self.obj("png_filter")) + self.obj("jpg_filter").set_name("JPG files") + self.obj("filechooser_dialog").add_filter(self.obj("jpg_filter")) + + #add buttons to headerbar of Glade generated dialog + button = Gtk.Button.new_from_stock(Gtk.STOCK_CANCEL) + button.set_property("can-default",True) + self.obj("filechooser_dialog").add_action_widget(button, Gtk.ResponseType.CANCEL) + button = Gtk.Button.new_from_stock(Gtk.STOCK_APPLY) + button.set_property("can-default",True) + self.obj("filechooser_dialog").add_action_widget(button, Gtk.ResponseType.OK) + + def on_app_shutdown(self, app): + self.app.quit() + + def run(self, argv): + self.app.run(argv) + +app = ExampleApp() +app.run(sys.argv) diff --git a/Gui test/gtk3_glade_test.py b/Gui test/gtk3_glade_test.py new file mode 100644 index 0000000..f690bba --- /dev/null +++ b/Gui test/gtk3_glade_test.py @@ -0,0 +1,31 @@ +import gi +gi.require_version('Gtk', '3.0') +from gi.repository import Gtk + +class Handler: + def onDeleteWindow(self, *args): + Gtk.main_quit(*args) + + def on_button1_clicked(self, button): + dialog.set_title('Select source folder') + dialog.set_default_size(800, 400) + + response = dialog.run() + if response == Gtk.ResponseType.OK: + print("Folder selected: " + dialog.get_filename()) + elif response == Gtk.ResponseType.CANCEL: + print("Cancel clicked") + + dialog.destroy() + +builder = Gtk.Builder() +builder.add_from_file("test.glade") +builder.connect_signals(Handler()) + +dialog = builder.get_object("filechooserdialog1") +window = builder.get_object("window1") +window.connect('delete-event', Gtk.main_quit) +window.show_all() + +Gtk.main() + diff --git a/Gui test/gtk_test.py b/Gui test/gtk_test.py new file mode 100644 index 0000000..b7b0d0f --- /dev/null +++ b/Gui test/gtk_test.py @@ -0,0 +1,48 @@ +import gi +gi.require_version('Gtk', '3.0') +from gi.repository import Gtk +import time + +class MyWindow(Gtk.Window): + + def __init__(self): + Gtk.Window.__init__(self, title = 'Hello World') + + self.val = 0 + + self.box = Gtk.Box(spacing=6) + self.add(self.box) + + self.button1 = Gtk.Button(label = 'Hello') + self.button1.connect('clicked', self.on_button1_clicked) + self.box.pack_start(self.button1, True, True, 0) + + self.button2 = Gtk.Button(label = 'Goodbye') + self.button2.connect('clicked', self.on_button2_clicked) + self.box.pack_start(self.button2, True, True, 0) + + self.pbar = Gtk.ProgressBar() + self.pbar.set_fraction(0.0) + self.box.pack_start(self.pbar, True, True, 0) + + def on_button1_clicked(self, widget): + print('Hello') + while self.val < 1: + self.update_pbar(self.pbar) + print('Goodbye') + Gtk.main_quit() + + def on_button2_clicked(self, widget): + print('Goodbye') + + def update_pbar(self, widget): + self.val += 0.1 + self.pbar.set_fraction(self.val) + time.sleep(0.5) + while Gtk.events_pending(): + Gtk.main_iteration() + +win = MyWindow() +win.connect('delete-event', Gtk.main_quit) +win.show_all() +Gtk.main() diff --git a/Gui test/gtk_test_1.py b/Gui test/gtk_test_1.py new file mode 100644 index 0000000..517805f --- /dev/null +++ b/Gui test/gtk_test_1.py @@ -0,0 +1,117 @@ +#!/usr/bin/env python3.5 + +# You are free to use and/or change this code for +# your own needs. + +# Original code (c)2018 Jan Lerking +# Program to convert C-header (*.h) files to nasm include files (*.inc), +# for direct usage in assembly programming using nasm/yasm. + +import gi +gi.require_version('Gtk', '3.0') +from gi.repository import Gtk + +class h2incGUI(Gtk.Window): + def __init__(self): + Gtk.Window.__init__(self) + self.set_title('Translate C-header files to Nasm include files!') + self.set_default_size(200, 200) + self.set_border_width(10) + self.connect("destroy", Gtk.main_quit) + + self.sourcedir = 'Select source directory!' + self.destdir = 'Select destination directory!' + self.addinc = False + self.totalvar = 0 + self.filecnt = 0 + self.infofolder = 'Number of folders: 0' + self.infofile = 'Number of headers: 0' + self.currentfile = 'Current file: ' + self.totprogress = 'Total progress: ' + self.val = 0 + + self.maingrid = Gtk.Grid() + self.add(self.maingrid) + + self.frame = Gtk.Frame(label='Select folders') + self.maingrid.add(self.frame) + + self.framegrid = Gtk.Grid() + self.framegrid.set_border_width(5) + self.frame.add(self.framegrid) + + self.sourcebox = Gtk.Box(orientation=Gtk.Orientation.HORIZONTAL, spacing=6) + self.framegrid.attach(self.sourcebox, 0, 0, 5, 1) + + self.sourcelabel = Gtk.Label(label='Source: ') + self.sourcelabel.set_justify(Gtk.Justification.RIGHT) + self.sourcebox.add(self.sourcelabel) + + self.sourceentry = Gtk.Entry() + self.sourceentry.set_text(self.sourcedir) + self.sourcebox.add(self.sourceentry) + + self.sourcedir_button = Gtk.Button(label="Source directory...") + #self.sourcedir_button.connect('clicked', self.select_sourcedir(self.sourcedir)) + self.sourcebox.add(self.sourcedir_button) + + self.destbox = Gtk.Box(orientation=Gtk.Orientation.HORIZONTAL, spacing=6) + self.framegrid.attach(self.destbox, 0, 1, 5, 1) + + self.destlabel = Gtk.Label(label='Destination: ') + self.destbox.add(self.destlabel) + + self.destentry = Gtk.Entry() + self.destentry.set_text(self.destdir) + self.destbox.add(self.destentry) + + self.destdir_button = Gtk.Button(label="Destination directory...") + #self.destdir_button.connect('clicked', self.select_destdir(self.destdir)) + self.destbox.add(self.destdir_button) + + self.incchkbox = Gtk.CheckButton('Create "include" folder if it does not exist.') + self.incchkbox.set_active(False) + #self.incchkbox.connect("toggled", self.on_button_toggled, self.addinc) + #self.framebox.add(self.incchkbox) + + self.transframe = Gtk.Frame(label='Translation') + #self.mainbox.add(self.transframe) + + self.framebox_1 = Gtk.Box(orientation=Gtk.Orientation.VERTICAL, spacing=6) + #self.transframe.add(self.framebox_1) + + self.infoframe = Gtk.Frame(label='Source information') + #self.framebox_1.add(self.infoframe) + + self.infobox = Gtk.Box(orientation=Gtk.Orientation.VERTICAL, spacing=6) + #self.infoframe.add(self.infobox) + + self.infofolders = Gtk.Label(label=self.infofolder) + #self.infobox.add(self.infofolders) + + self.infofiles = Gtk.Label(label=self.infofile) + #self.infobox.add(self.infofiles) + + self.translate_button = Gtk.Button(label="Translate!") + #self.translate_button.connect('clicked', self.translate(self.destdir, self.sourcedir, self.addinc)) + #self.framebox_1.add(self.translate_button) + + self.progressframe = Gtk.Frame(label='Progress') + #self.framebox_1.add(self.progressframe) + + self.progressbox = Gtk.Box(orientation=Gtk.Orientation.VERTICAL, spacing=6) + #self.progressframe.add(self.progressbox) + + self.cfilelabel = Gtk.Label(label=self.currentfile) + #self.progressbox.add(self.cfilelabel) + + self.totallabel = Gtk.Label(label=self.totprogress) + #self.progressbox.add(self.totallabel) + + self.totalprogress = Gtk.ProgressBar() + self.totalprogress.set_fraction(0.0) + #self.progressbox.add(self.totalprogress) + +win = h2incGUI() +win.show_all() +Gtk.main() \ No newline at end of file diff --git a/Gui test/gtk_test_2.py b/Gui test/gtk_test_2.py new file mode 100644 index 0000000..4130fe1 --- /dev/null +++ b/Gui test/gtk_test_2.py @@ -0,0 +1,82 @@ +#!/usr/bin/env python3.5 + +# You are free to use and/or change this code for +# your own needs. + +# Original code (c)2018 Jan Lerking +# Program to convert C-header (*.h) files to nasm include files (*.inc), +# for direct usage in assembly programming using nasm/yasm. + +import gi +gi.require_version('Gtk', '3.0') +from gi.repository import Gtk + +class FileChooserWindow(Gtk.Window): + + def __init__(self): + Gtk.Window.__init__(self, title="FileChooser Example") + + box = Gtk.Box(spacing=6) + self.add(box) + + button1 = Gtk.Button("Choose File") + button1.connect("clicked", self.on_file_clicked) + box.add(button1) + + button2 = Gtk.Button("Choose Folder") + button2.connect("clicked", self.on_folder_clicked) + box.add(button2) + + def on_file_clicked(self, widget): + dialog = Gtk.FileChooserDialog("Please choose a file", self, + Gtk.FileChooserAction.OPEN, + (Gtk.STOCK_CANCEL, Gtk.ResponseType.CANCEL, + Gtk.STOCK_OPEN, Gtk.ResponseType.OK)) + + self.add_filters(dialog) + + response = dialog.run() + if response == Gtk.ResponseType.OK: + print("Open clicked") + print("File selected: " + dialog.get_filename()) + elif response == Gtk.ResponseType.CANCEL: + print("Cancel clicked") + + dialog.destroy() + + def add_filters(self, dialog): + filter_text = Gtk.FileFilter() + filter_text.set_name("Text files") + filter_text.add_mime_type("text/plain") + dialog.add_filter(filter_text) + + filter_py = Gtk.FileFilter() + filter_py.set_name("Python files") + filter_py.add_mime_type("text/x-python") + dialog.add_filter(filter_py) + + filter_any = Gtk.FileFilter() + filter_any.set_name("Any files") + filter_any.add_pattern("*") + dialog.add_filter(filter_any) + + def on_folder_clicked(self, widget): + dialog = Gtk.FileChooserDialog("Please choose a folder", self, + Gtk.FileChooserAction.SELECT_FOLDER, + (Gtk.STOCK_CANCEL, Gtk.ResponseType.CANCEL, + "Select", Gtk.ResponseType.OK)) + dialog.set_default_size(800, 400) + + response = dialog.run() + if response == Gtk.ResponseType.OK: + print("Select clicked") + print("Folder selected: " + dialog.get_filename()) + elif response == Gtk.ResponseType.CANCEL: + print("Cancel clicked") + + dialog.destroy() + +win = FileChooserWindow() +win.connect("delete-event", Gtk.main_quit) +win.show_all() +Gtk.main() \ No newline at end of file diff --git a/Gui test/gtk_test_3.py b/Gui test/gtk_test_3.py new file mode 100644 index 0000000..40c0015 --- /dev/null +++ b/Gui test/gtk_test_3.py @@ -0,0 +1,80 @@ +import gi +gi.require_version('Gtk', '3.0') +from gi.repository import Gtk +import time +from multiprocessing import Process, Value, Lock, Pool + +class Counter(object): + def __init__(self, initval=0.0): + self.val = Value('d', initval) + self.lock = Lock() + + def increment(self): + with self.lock: + self.val.value += 1/2542 + print(self.val.value) + + def value(self): + with self.lock: + return self.val.value + +class MyWindow(Gtk.Window): + + def __init__(self): + Gtk.Window.__init__(self, title = 'Hello World') + + #self.val = 0 + + self.box = Gtk.Box(spacing=6) + self.add(self.box) + + self.button1 = Gtk.Button(label = 'Hello') + self.button1.connect('clicked', self.on_button1_clicked) + self.box.pack_start(self.button1, True, True, 0) + + self.button2 = Gtk.Button(label = 'Goodbye') + self.button2.connect('clicked', self.on_button2_clicked) + self.box.pack_start(self.button2, True, True, 0) + + self.pbar = Gtk.ProgressBar() + self.pbar.set_fraction(0.0) + self.box.pack_start(self.pbar, True, True, 0) + + def on_button1_clicked(self, widget): + print('Hello') + #procs = [Process(target=self.func, args=(counter,)) for i in range(10)] + + #for p in procs: p.start() + #for p in procs: p.join() + p = Pool(2) + p.map(self.func, range(2542)) + print (counter.value()) + + #while counter.val.value <10: + #self.update_pbar(self.pbar) + print('Goodbye') + Gtk.main_quit() + + def on_button2_clicked(self, widget): + print('Goodbye') + Gtk.main_quit() + + def update_pbar(self, widget, value): + #self.val += 0.1 + self.pbar.set_fraction(value) + #time.sleep(0.5) + while Gtk.events_pending(): + Gtk.main_iteration() + + def func(self, data): + #for i in range(2542): + time.sleep(0.01) + counter.increment() + #while counter.value() <1: + #self.update_pbar(self.pbar, counter.value()) + +counter = Counter(0.0) +win = MyWindow() +win.connect('delete-event', Gtk.main_quit) +win.show_all() +Gtk.main() diff --git a/Gui test/gtk_test_4.py b/Gui test/gtk_test_4.py new file mode 100644 index 0000000..08672c9 --- /dev/null +++ b/Gui test/gtk_test_4.py @@ -0,0 +1,118 @@ +import gobject +import pygtk +pygtk.require('2.0') +import gtk +import multiprocessing +import threading +import time + +gtk.gdk.threads_init() + +class Listener(gobject.GObject): + __gsignals__ = { + 'updated' : (gobject.SIGNAL_RUN_LAST, + gobject.TYPE_NONE, + (gobject.TYPE_FLOAT, gobject.TYPE_STRING)), + 'finished': (gobject.SIGNAL_RUN_LAST, + gobject.TYPE_NONE, + ()) + } + + def __init__(self, queue): + gobject.GObject.__init__(self) + self.queue = queue + + def go(self): + print("Listener has started") + while True: + # Listen for results on the queue and process them accordingly + data = self.queue.get() + # Check if finished + if data[1]=="finished": + print("Listener is finishing.") + self.emit("finished") + return + else: + self.emit('updated', data[0], data[1]) + +gobject.type_register(Listener) + +class Worker(): + def __init__(self, queue): + self.queue = queue + + def go(self): + print("The worker has started doing some work (counting from 0 to 9)") + for i in range(10): + proportion = (float(i+1))/10 + self.queue.put((proportion, "working...")) + time.sleep(0.5) + self.queue.put((1.0, "finished")) + print("The worker has finished.") + + +class Interface: + def __init__(self): + self.process = None + self.progress = gtk.ProgressBar() + button = gtk.Button("Go!") + button.connect("clicked", self.go) + vbox = gtk.VBox(spacing=5) + vbox.pack_start(self.progress) + vbox.pack_start(button) + vbox.show_all() + self.frame = vbox + + def main(self): + window = gtk.Window(gtk.WINDOW_TOPLEVEL) + window.set_border_width(10) + window.add(self.frame) + window.show() + window.connect("destroy", self.destroy) + + gtk.main() + + def destroy(self, widget, data=None): + gtk.main_quit() + + + def callbackDisplay(self, obj, fraction, text, data=None): + self.progress.set_fraction(fraction) + self.progress.set_text(text) + + def callbackFinished(self, obj, data=None): + if self.process==None: + raise RuntimeError("No worker process started") + print("all done; joining worker process") + self.process.join() + self.process = None + + self.progress.set_fraction(1.0) + self.progress.set_text("done") + + def go(self, widget, data=None): + if self.process!=None: + return + + print("Creating shared Queue") + queue = multiprocessing.Queue() + + print("Creating Worker") + worker = Worker(queue) + + print("Creating Listener") + listener = Listener(queue) + listener.connect("updated",self.callbackDisplay) + listener.connect("finished",self.callbackFinished) + + print("Starting Worker") + self.process = multiprocessing.Process(target=worker.go, args=()) + self.process.start() + + print("Starting Listener") + thread = threading.Thread(target=listener.go, args=()) + thread.start() + +if __name__ == '__main__': + gui = Interface() + gui.main() \ No newline at end of file diff --git a/Gui test/gtk_test_5.py b/Gui test/gtk_test_5.py new file mode 100644 index 0000000..044ed36 --- /dev/null +++ b/Gui test/gtk_test_5.py @@ -0,0 +1,166 @@ +import gi +gi.require_version('Gtk', '3.0') +from gi.repository import Gtk, GObject as gobject +import time +from multiprocessing import Process, Value, Lock, Pool, Queue +import threading + +gobject.threads_init() + +class Listener(gobject.GObject): + __gsignals__ = { + 'updated' : (gobject.SIGNAL_RUN_LAST, + gobject.TYPE_NONE, + (gobject.TYPE_FLOAT, gobject.TYPE_STRING)), + 'finished': (gobject.SIGNAL_RUN_LAST, + gobject.TYPE_NONE, + ()) + } + + def __init__(self, queue): + gobject.GObject.__init__(self) + self.queue = queue + + def go(self): + print("Listener has started") + while True: + # Listen for results on the queue and process them accordingly + data = self.queue.get() + # Check if finished + if data[1]=="finished": + print("Listener is finishing.") + self.emit("finished") + return + else: + self.emit('updated', data[0], data[1]) + +gobject.type_register(Listener) + +class Worker(): + def __init__(self, queue): + self.queue = queue + + def go(self): + print("The worker has started doing some work (counting from 0 to 9)") + for i in range(2542): + proportion = (float(i+1))/2542 + self.queue.put((proportion, "working...")) + time.sleep(0.01) + self.queue.put((1.0, "finished")) + print("The worker has finished.") + +class Counter(object): + def __init__(self, initval=0.0): + self.val = Value('d', initval) + self.lock = Lock() + + def increment(self): + with self.lock: + self.val.value += 1/2542 + print(self.val.value) + + def value(self): + with self.lock: + return self.val.value + +class MyWindow(Gtk.Window): + + def __init__(self): + Gtk.Window.__init__(self, title = 'Hello World') + + #self.val = 0 + self.process = None + + self.box = Gtk.Box(spacing=6) + self.add(self.box) + + self.button1 = Gtk.Button(label = 'Hello') + self.button1.connect('clicked', self.go) + self.box.pack_start(self.button1, True, True, 0) + + self.button2 = Gtk.Button(label = 'Goodbye') + self.button2.connect('clicked', self.on_button2_clicked) + self.box.pack_start(self.button2, True, True, 0) + + self.pbar = Gtk.ProgressBar() + self.pbar.set_fraction(0.0) + self.box.pack_start(self.pbar, True, True, 0) + + def callbackDisplay(self, obj, fraction, text, data=None): + self.pbar.set_fraction(fraction) + self.pbar.set_text(text) + + def callbackFinished(self, obj, data=None): + if self.process==None: + raise RuntimeError("No worker process started") + print("all done; joining worker process") + self.process.join() + self.process = None + + self.pbar.set_fraction(1.0) + self.pbar.set_text("done") + + print('Goodbye') + Gtk.main_quit() + + def go(self, widget, data=None): + if self.process!=None: + return + + print("Creating shared Queue") + queue = Queue() + + print("Creating Worker") + worker = Worker(queue) + + print("Creating Listener") + listener = Listener(queue) + listener.connect("updated",self.callbackDisplay) + listener.connect("finished",self.callbackFinished) + + print("Starting Worker") + self.process = Process(target=worker.go, args=()) + self.process.start() + + print("Starting Listener") + thread = threading.Thread(target=listener.go, args=()) + thread.start() + + #def on_button1_clicked(self, widget): + #print('Hello') + #procs = [Process(target=self.func, args=(counter,)) for i in range(10)] + + #for p in procs: p.start() + #for p in procs: p.join() + #p = Pool(2) + #p.map(self.func, range(2542)) + #print (counter.value()) + + #while counter.val.value <10: + #self.update_pbar(self.pbar) + #print('Goodbye') + #Gtk.main_quit() + + def on_button2_clicked(self, widget): + print('Goodbye') + Gtk.main_quit() + + #def update_pbar(self, widget, value): + #self.val += 0.1 + #self.pbar.set_fraction(value) + #time.sleep(0.5) + #while Gtk.events_pending(): + #Gtk.main_iteration() + + #def func(self, data): + #for i in range(2542): + #time.sleep(0.01) + #counter.increment() + #while counter.value() <1: + #self.update_pbar(self.pbar, counter.value()) + +#counter = Counter(0.0) +win = MyWindow() +win.connect('delete-event', Gtk.main_quit) +win.show_all() +Gtk.main() diff --git a/Gui test/h2inc.glade b/Gui test/h2inc.glade new file mode 100644 index 0000000..df693c9 --- /dev/null +++ b/Gui test/h2inc.glade @@ -0,0 +1,481 @@ + + + + + + False + dialog + + + False + vertical + 2 + + + False + + + + + + + + + + + + False + False + 0 + + + + + + + + + + False + + + True + False + vertical + + + True + False + 5 + 5 + 5 + 5 + 0 + etched-out + + + True + False + 12 + + + True + False + + + True + False + end + 5 + 5 + 5 + 5 + True + Source: + right + + + 0 + 0 + 2 + + + + + True + True + 5 + 5 + /usr/include + + + 2 + 0 + 2 + + + + + Select source directory... + True + True + True + start + 5 + 5 + 5 + 5 + + + + 4 + 0 + 2 + + + + + True + False + False + end + 5 + 5 + 5 + 5 + True + Destination: + right + start + 2.2351741811588166e-10 + + + 0 + 1 + 2 + + + + + True + False + True + 5 + 5 + ~/include + + + 2 + 1 + 2 + + + + + Select destination directory... + True + False + True + True + start + 5 + 5 + 5 + 5 + + + + 4 + 1 + 2 + + + + + Create "include" folder if it does not exist. + True + False + True + False + 5 + 5 + 5 + 5 + 0 + True + + + + 0 + 3 + 6 + + + + + Use default directories! + True + True + False + 5 + 5 + 5 + 5 + 0 + True + + + + 0 + 2 + 6 + + + + + + + + + True + False + 5 + 5 + Select folders + + + + + False + True + 0 + + + + + True + False + False + 5 + 5 + 5 + 5 + 0 + etched-out + + + True + False + 12 + + + True + False + + + True + False + 5 + 5 + 5 + True + 0 + etched-out + + + True + False + 12 + + + True + False + vertical + + + True + False + + + True + False + 5 + 5 + Number of folders: + + + 0 + 0 + + + + + True + False + 0 + + + 1 + 0 + + + + + False + True + 0 + + + + + True + False + + + True + False + 5 + 5 + Number of files: + + + 0 + 0 + + + + + True + False + 0 + + + 1 + 0 + + + + + False + True + 1 + + + + + + + + + True + False + 5 + 5 + Source information + + + + + 0 + 0 + + + + + Translate! + True + True + True + start + 5 + 5 + + + + 0 + 1 + + + + + True + False + 5 + 5 + 5 + 0 + etched-out + + + True + False + 12 + + + True + False + True + + + True + False + start + False + Total progress: + + + 0 + 0 + + + + + True + False + start + True + 0 of 0 + + + 1 + 0 + + + + + True + False + 5 + 5 + 5 + True + + + 0 + 1 + 2 + + + + + + + + + True + False + 5 + 5 + Progress + + + + + 0 + 2 + + + + + + + + + True + False + 5 + 5 + Translation + + + + + False + True + 1 + + + + + + diff --git a/Gui test/h2inc.glade~ b/Gui test/h2inc.glade~ new file mode 100644 index 0000000..df693c9 --- /dev/null +++ b/Gui test/h2inc.glade~ @@ -0,0 +1,481 @@ + + + + + + False + dialog + + + False + vertical + 2 + + + False + + + + + + + + + + + + False + False + 0 + + + + + + + + + + False + + + True + False + vertical + + + True + False + 5 + 5 + 5 + 5 + 0 + etched-out + + + True + False + 12 + + + True + False + + + True + False + end + 5 + 5 + 5 + 5 + True + Source: + right + + + 0 + 0 + 2 + + + + + True + True + 5 + 5 + /usr/include + + + 2 + 0 + 2 + + + + + Select source directory... + True + True + True + start + 5 + 5 + 5 + 5 + + + + 4 + 0 + 2 + + + + + True + False + False + end + 5 + 5 + 5 + 5 + True + Destination: + right + start + 2.2351741811588166e-10 + + + 0 + 1 + 2 + + + + + True + False + True + 5 + 5 + ~/include + + + 2 + 1 + 2 + + + + + Select destination directory... + True + False + True + True + start + 5 + 5 + 5 + 5 + + + + 4 + 1 + 2 + + + + + Create "include" folder if it does not exist. + True + False + True + False + 5 + 5 + 5 + 5 + 0 + True + + + + 0 + 3 + 6 + + + + + Use default directories! + True + True + False + 5 + 5 + 5 + 5 + 0 + True + + + + 0 + 2 + 6 + + + + + + + + + True + False + 5 + 5 + Select folders + + + + + False + True + 0 + + + + + True + False + False + 5 + 5 + 5 + 5 + 0 + etched-out + + + True + False + 12 + + + True + False + + + True + False + 5 + 5 + 5 + True + 0 + etched-out + + + True + False + 12 + + + True + False + vertical + + + True + False + + + True + False + 5 + 5 + Number of folders: + + + 0 + 0 + + + + + True + False + 0 + + + 1 + 0 + + + + + False + True + 0 + + + + + True + False + + + True + False + 5 + 5 + Number of files: + + + 0 + 0 + + + + + True + False + 0 + + + 1 + 0 + + + + + False + True + 1 + + + + + + + + + True + False + 5 + 5 + Source information + + + + + 0 + 0 + + + + + Translate! + True + True + True + start + 5 + 5 + + + + 0 + 1 + + + + + True + False + 5 + 5 + 5 + 0 + etched-out + + + True + False + 12 + + + True + False + True + + + True + False + start + False + Total progress: + + + 0 + 0 + + + + + True + False + start + True + 0 of 0 + + + 1 + 0 + + + + + True + False + 5 + 5 + 5 + True + + + 0 + 1 + 2 + + + + + + + + + True + False + 5 + 5 + Progress + + + + + 0 + 2 + + + + + + + + + True + False + 5 + 5 + Translation + + + + + False + True + 1 + + + + + + diff --git a/Gui test/h2inc_gtk.py b/Gui test/h2inc_gtk.py new file mode 100644 index 0000000..a268040 --- /dev/null +++ b/Gui test/h2inc_gtk.py @@ -0,0 +1,319 @@ +#!/usr/bin/env python3.5 + +# You are free to use and/or change this code for +# your own needs. + +# Original code (c)2018 Jan Lerking +# Program to convert C-header (*.h) files to nasm include files (*.inc), +# for direct usage in assembly programming using nasm/yasm. + +import os +import sys +import io +import gi +gi.require_version('Gtk', '3.0') +from gi.repository import Gtk, Gio, GObject as gobject +import time +from multiprocessing import Process, Value, Lock, Pool, Queue +import threading + +gobject.threads_init() + +class Listener(gobject.GObject): + __gsignals__ = { + 'updated' : (gobject.SIGNAL_RUN_LAST, + gobject.TYPE_NONE, + (gobject.TYPE_FLOAT, gobject.TYPE_STRING, gobject.TYPE_INT)), + 'finished': (gobject.SIGNAL_RUN_LAST, + gobject.TYPE_NONE, + ()) + } + + def __init__(self, queue): + gobject.GObject.__init__(self) + self.queue = queue + + def go(self): + print("Listener has started") + while True: + # Listen for results on the queue and process them accordingly + data = self.queue.get() + # Check if finished + if data[1]=="finished": + print("Listener is finishing.") + self.emit("finished") + return + else: + self.emit('updated', data[0], data[1], data[2]) + +gobject.type_register(Listener) + +class Worker(): + def __init__(self, queue, filecnt, filelist): + self.queue = queue + self.filecnt = filecnt + + def go(self): + print("The worker has started doing some work (counting from 0 to 9)") + for i in range(self.filecnt): + proportion = (float(i+1))/self.filecnt + self.queue.put((proportion, "working...", i)) + time.sleep(0.01) + process_file(filelist[i]) + self.queue.put((1.0, "finished")) + print("The worker has finished.") + +tupline = [] +preproc = () +filelist = [] +folderlist = [] +cnt = 0 +srcdir = '' +destdir = '' +#num_cores = multiprocessing.cpu_count() +fileindex = 0 +filecnt = 0 +incinc = False +defdir = False + +def sourcedir_filecnt(sourcedir): + ### Return the number of files, ending with '.h', in sourcedir - including subdirectories ### + cnt = 0 + global filelist + global srcdir + srcdir = sourcedir + for folderName, subfolders, files in os.walk(sourcedir): + for file in files: + if file.lower().endswith('.h'): + cnt += 1 + filelist += [folderName + '/' + file] + print(folderName + '/' + file) + # print(filelist) + return cnt + + +def sourcedir_foldercnt(sourcedir): + ### Return the number of folders, if it contains '*.h' files, in sourcedir - including subdirectories ### + global cnt + global folderlist + for folderName, subfolders, files in os.walk(sourcedir): + if subfolders: + for subfolder in subfolders: + sourcedir_foldercnt(subfolder) + tempf = [file for file in files if file.lower().endswith('.h')] + if tempf: + cnt = cnt + 1 + # print(folderName) + folderlist += [folderName] + # print(folderlist) + # print(len(folderlist)) + return cnt + +def process_file(data): + outfile = '' + inputfile = data + encodings = ['utf-8', 'latin-1', 'windows-1250', 'windows-1252', 'ascii', + 'big5', 'big5hkscs', 'cp037', 'cp273', 'cp424', 'cp437', 'cp500', + 'cp720', 'cp737', 'cp775', 'cp850', 'cp852', 'cp855', 'cp856', + 'cp857', 'cp858', 'cp860', 'cp861', 'cp862', 'cp863', 'cp864', 'cp865', + 'cp866', 'cp869', 'cp874', 'cp875', 'cp932', 'cp949', 'cp950', 'cp1006', + 'cp1026', 'cp1125', 'cp1140', 'cp1250', 'cp1251', 'cp1252', 'cp1253', 'cp1254', + 'cp1255', 'cp1256', 'cp1257', 'cp1258', 'cp65001', 'euc-jp', 'euc-jis-2004', + 'euc-jisx0213', 'euc-kr', 'gb2312', 'gbk', 'gb18030', 'hz', 'iso2022-jp', + 'iso2022-jp-1', 'iso2022-jp-2', 'iso2022-jp-2004', 'iso2022-jp-3', + 'iso2022-jp-ext', 'iso2022-kr', 'iso8859-2', 'iso8859-3', 'iso8859-4', + 'iso8859-5', 'iso8859-6', 'iso8859-7', 'iso8859-8', 'iso8859-9', 'iso8859-10', + 'iso8859-11', 'iso8859-13', 'iso8859-14', 'iso8859-15', 'iso8859-16', 'johab', + 'koi8-r', 'koi8-t', 'koi8-u', 'kz1048', 'mac-cyrillic', 'mac-greek', + 'mac-iceland', 'mac-latin2', 'mac-roman', 'mac-turkish', 'ptcp154', + 'shift-jis', 'shift-jis-2004', 'shift-jisx0213', 'utf-32', 'utf-32-be', + 'utf-32-le', 'utf-16', 'utf-16-be', 'utf-16-le', 'utf-7', 'utf-8-sig'] + for e in encodings: + try: + fh = io.open(data, 'r', encoding=e) + fh.readlines() + fh.seek(0) + except UnicodeDecodeError: + print('got unicode error with %s , trying different encoding' % e) + else: + # print('opening the file with encoding: %s ' % e) + break + # print(os.path.basename(data)) + for lines in fh: + outfile = outfile + lines + fh.close() + outputfile = os.path.splitext(inputfile)[0] + '.inc' + outputfile = str(outputfile).replace(srcdir, destdir) + print(outputfile) + if not os.path.exists(os.path.dirname(outputfile)): + try: + os.makedirs(os.path.dirname(outputfile)) + except OSError as exc: # Guard against race condition + if exc.errno != errno.EEXIST: + raise + newfile = open(outputfile, "w") + newfile.write(outfile) + newfile.close() + +class ExampleApp: + + global app + global destlabel + + def __init__(self): + + self.filecnt = 0 + self.fileindex = 0 + self.process = None + self.app = Gtk.Application.new("org.h2inc", Gio.ApplicationFlags(0)) + self.app.connect("activate", self.on_app_activate) + self.app.connect("shutdown", self.on_app_shutdown) + app = self.app + + def on_app_activate(self, app): + + builder = Gtk.Builder() + builder.add_from_file("h2inc.glade") + builder.connect_signals(self) + + self.obj = builder.get_object + self.obj("window").set_application(app) + self.obj("window").set_wmclass("h2inc_gtk","h2inc_gtk") + self.obj("window").show_all() + self.obj("default_dir_checkbutton").set_active(False) + self.obj("include_checkbutton").set_active(True) + + button = Gtk.Button.new_from_stock(Gtk.STOCK_CANCEL) + button.set_property("can-default",True) + button = Gtk.Button.new_from_stock(Gtk.STOCK_APPLY) + button.set_property("can-default",True) + + def on_app_shutdown(self, app): + self.app.quit() + + def run(self, argv): + self.app.run(argv) + + def callbackDisplay(self, obj, fraction, text, index, data=None): + self.obj("progress_label").set_text("{} of {}".format(index+1, self.filecnt)) + self.obj("progressbar").set_fraction(fraction) + + def callbackFinished(self, obj, data=None): + if self.process==None: + raise RuntimeError("No worker process started") + print("all done; joining worker process") + self.process.join() + self.process = None + + self.obj("progressbar").set_fraction(1.0) + self.obj("sourceframe").set_sensitive(True) + self.obj("translate_button").set_sensitive(True) + self.obj("destination_label").set_sensitive(True) + self.obj("destination_entry").set_sensitive(True) + self.obj("destination_button").set_sensitive(True) + self.obj("include_checkbutton").set_sensitive(True) + self.obj("translation_frame").set_sensitive(True) + + def on_window_destroy(self,window): + window.close() + + def on_dialog_close(self,widget,*event): + widget.hide_on_delete() + return True + + def on_filechooser_dialog_response(self,widget,response): + if response == -6: + print("Cancel") + elif response == -5: + print("File selection: %s" % widget.get_filename()) + self.on_dialog_close(widget) + + def on_source_button_clicked(self,widget): + + dialog = Gtk.FileChooserDialog("Select source directory!", + self.obj("window"), + Gtk.FileChooserAction.SELECT_FOLDER, + (Gtk.STOCK_CANCEL, Gtk.ResponseType.CANCEL, + Gtk.STOCK_APPLY, Gtk.ResponseType.OK)) + dialog.set_default_size(600, 300) + + response = dialog.run() + if response == Gtk.ResponseType.OK: + self.filecnt = sourcedir_filecnt(dialog.get_filename()) + if self.filecnt >0: + print(self.filecnt) + self.obj("source_entry").set_text(dialog.get_filename()) + self.obj("destination_label").set_sensitive(True) + self.obj("destination_entry").set_sensitive(True) + self.obj("destination_button").set_sensitive(True) + self.obj("numfiles_label").set_text(str(self.filecnt)) + self.obj("progress_label").set_text("{} of {}".format(fileindex, self.filecnt)) + foldercnt = sourcedir_foldercnt(dialog.get_filename()) + if foldercnt >0: + self.obj("numfolders_label").set_text(str(foldercnt)) + elif response == Gtk.ResponseType.CANCEL: + print("Cancel") + + dialog.destroy() + + def on_destination_button_clicked(self,widget): + global destdir + + dialog = Gtk.FileChooserDialog("Select destination directory!", + self.obj("window"), + Gtk.FileChooserAction.SELECT_FOLDER, + (Gtk.STOCK_CANCEL, Gtk.ResponseType.CANCEL, + Gtk.STOCK_APPLY, Gtk.ResponseType.OK)) + dialog.set_default_size(600, 300) + + response = dialog.run() + if response == Gtk.ResponseType.OK: + destdir = dialog.get_filename() + self.obj("destination_entry").set_text(dialog.get_filename()) + self.obj("include_checkbutton").set_sensitive(True) + self.obj("translation_frame").set_sensitive(True) + print(srcdir) + print(destdir) + elif response == Gtk.ResponseType.CANCEL: + print("Cancel") + + dialog.destroy() + + def on_include_checkbutton_toggled(self, widget): + global incinc + incinc = self.obj("include_checkbutton").get_active() + + def on_default_dir_checkbutton_toggled(self, widget): + global defdir + defdir = self.obj("default_dir_checkbutton").get_active() + + def on_translate_button_clicked(self, widget, data=None): + self.obj("sourceframe").set_sensitive(False) + self.obj("translate_button").set_sensitive(False) + + if self.process!=None: + return + + print("Creating shared Queue") + queue = Queue() + + print("Creating Worker") + worker = Worker(queue, self.filecnt, filelist) + + print("Creating Listener") + listener = Listener(queue) + listener.connect("updated",self.callbackDisplay) + listener.connect("finished",self.callbackFinished) + + print("Starting Listener") + thread = threading.Thread(target=listener.go, args=()) + thread.start() + + print("Starting Worker") + self.process = Process(target=worker.go, args=()) + self.process.start() + + + +app = ExampleApp() +app.run(sys.argv) diff --git a/Gui test/h2inc_orig.glade b/Gui test/h2inc_orig.glade new file mode 100644 index 0000000..ad6a6e9 --- /dev/null +++ b/Gui test/h2inc_orig.glade @@ -0,0 +1,367 @@ + + + + + + False + 5 + 5 + 5 + 5 + + + True + False + 5 + 5 + 5 + 5 + True + True + + + True + False + 0 + etched-out + + + True + False + 12 + + + True + False + + + True + False + end + 5 + 5 + 5 + True + Source: + right + + + 0 + 0 + 2 + + + + + True + True + 5 + 5 + 5 + 5 + Select source directory! + + + 2 + 0 + 2 + + + + + Source directory... + True + True + True + start + 5 + 5 + 5 + 5 + + + 4 + 0 + 2 + + + + + True + False + False + end + 5 + 5 + 5 + True + Destination: + right + start + 2.2351741811588166e-10 + + + 0 + 1 + 2 + + + + + True + False + True + 5 + 5 + 5 + 5 + Select destination directory! + + + 2 + 1 + 2 + + + + + Destination directory... + True + False + True + True + start + 5 + 5 + 5 + 5 + + + 4 + 1 + 2 + + + + + Create "include" folder if it does not exist. + True + False + True + False + 5 + 5 + 0 + True + + + 0 + 2 + 6 + + + + + + + + + True + False + Select folders + + + + + 0 + 0 + 5 + + + + + True + False + False + 5 + 5 + 0 + etched-out + + + True + False + 12 + + + True + False + + + True + False + 5 + 5 + True + 0 + etched-out + + + True + False + 12 + + + True + False + vertical + + + True + False + start + 5 + 5 + True + Number of folders: 0 + + + False + True + 0 + + + + + True + False + start + 5 + 5 + Number of files: 0 + + + False + True + 1 + + + + + + + + + True + False + Source information + + + + + 0 + 0 + + + + + Translate! + True + True + True + start + 5 + 5 + + + 0 + 1 + + + + + True + False + 5 + 5 + 5 + 0 + etched-out + + + True + False + 12 + + + True + False + vertical + + + True + False + start + 5 + 5 + Total progress: + + + False + True + 0 + + + + + True + False + 5 + 5 + 5 + + + False + True + 1 + + + + + + + + + True + False + Progress + + + + + 0 + 2 + + + + + + + + + True + False + Translation + + + + + 0 + 1 + 5 + + + + + + diff --git a/Gui test/test.glade b/Gui test/test.glade new file mode 100644 index 0000000..8cb0a99 --- /dev/null +++ b/Gui test/test.glade @@ -0,0 +1,433 @@ + + + + + + False + + + True + False + vertical + + + True + False + 5 + 0 + etched-out + + + True + False + 12 + + + True + False + + + True + False + end + 5 + 5 + 5 + Source: + start + + + 0 + 0 + + + + + True + False + False + end + 5 + 5 + 5 + Destination: + 0.059999999999999998 + + + 0 + 1 + + + + + True + True + 5 + 5 + + + 1 + 0 + + + + + True + False + True + 5 + 5 + + + 1 + 1 + + + + + Select source directory... + True + True + True + start + 5 + 5 + 0.52999997138977051 + + + 2 + 0 + + + + + Select destination directory... + True + False + True + True + start + 5 + 5 + + + 2 + 1 + + + + + checkbutton + True + False + True + False + 0 + True + True + + + 0 + 2 + 3 + + + + + + + + + True + False + Select folders + + + + + True + True + 0 + + + + + True + False + False + 5 + 0 + etched-out + + + True + False + 12 + + + True + False + vertical + + + True + False + 5 + 0 + etched-out + + + True + False + 12 + + + True + False + + + True + False + end + 5 + Number of folders: + + + 0 + 0 + + + + + True + False + 0.9882352941176471 + start + 5 + 0 + end + + + 1 + 0 + + + + + True + False + end + 5 + 5 + Number of files: + + + 0 + 1 + + + + + True + False + start + 5 + 5 + 0 + + + 1 + 1 + + + + + + + + + True + False + Source information + + + + + False + True + 0 + + + + + Translate! + True + True + True + start + 5 + 5 + + + False + False + 1 + + + + + True + False + 5 + 0 + etched-out + + + True + False + 12 + + + True + False + + + True + False + end + 5 + 5 + Total progress: + + + 0 + 0 + + + + + True + False + start + 5 + 5 + True + 0 of 0 + + + 1 + 0 + + + + + True + False + 5 + + + 0 + 1 + 2 + + + + + + + + + True + False + Progress + + + + + False + True + 2 + + + + + + + + + True + False + Translation! + + + + + True + True + 1 + + + + + + + False + dialog + select-folder + + + False + vertical + 2 + + + False + end + + + gtk-ok + True + True + True + True + True + + + True + True + 0 + + + + + gtk-cancel + True + True + True + True + True + + + True + True + 1 + + + + + False + False + 0 + + + + + + + + + button4 + button5 + + + diff --git a/Multiprocessing test/async_multiprocessing_test.py b/Multiprocessing test/async_multiprocessing_test.py new file mode 100644 index 0000000..6d03b72 --- /dev/null +++ b/Multiprocessing test/async_multiprocessing_test.py @@ -0,0 +1,34 @@ +import multiprocessing as mp +import random +import string + +random.seed(123) + +# Define an output queue +output = mp.Queue() + +# define a example function +def rand_string(length, output): + """ Generates a random string of numbers, lower- and uppercase chars. """ + rand_str = ''.join(random.choice( + string.ascii_lowercase + + string.ascii_uppercase + + string.digits) + for i in range(length)) + output.put(rand_str) + +# Setup a list of processes that we want to run +processes = [mp.Process(target=rand_string, args=(5, output)) for x in range(4)] + +# Run processes +for p in processes: + p.start() + +# Exit the completed processes +for p in processes: + p.join() + +# Get process results from the output queue +results = [output.get() for p in processes] + +print(results) diff --git a/Multiprocessing test/calc_pi.py b/Multiprocessing test/calc_pi.py new file mode 100644 index 0000000..5dceeac --- /dev/null +++ b/Multiprocessing test/calc_pi.py @@ -0,0 +1,171 @@ +#!/usr/bin/env python3 + +GUI_WIN = 0 +GUI_LINUX = 1 + +import sys +if sys.platform == 'win32': + GUI = GUI_WIN +else: + GUI = GUI_LINUX + +from tkinter import (Tk, BOTH, Text, E, W, S, N, END, + NORMAL, DISABLED, StringVar) +from tkinter.ttk import Frame, Label, Button, Progressbar, Entry +from tkinter import scrolledtext + +if GUI == GUI_WIN: + from multiprocessing import Process, Manager, Queue, Value + from queue import Empty +else: + from multiprocessing import Queue, Process + import queue + +from decimal import Decimal, getcontext +from time import sleep + +DELAY1 = 20 +DELAY2 = 20 + +if GUI == GUI_WIN: + # Queue must be global + q = Queue() + +class commonUI(Frame): + def __init__(self, parent): + Frame.__init__(self, parent) + self.parent = parent + self.initUI() + + def initUI(self): + + self.val = Value('i', 0) + self.dig = Value('i', 4000) + self.acc = Value('i', 100) + + self.parent.title("Pi computation") + self.pack(fill=BOTH, expand=True) + + self.grid_columnconfigure(4, weight=1) + self.grid_rowconfigure(3, weight=1) + + lbl1 = Label(self, text="Digits:") + lbl1.grid(row=0, column=0, sticky=E, padx=10, pady=10) + + self.ent1 = Entry(self, width=10) + self.ent1.insert(END, self.dig.value) + self.ent1.grid(row=0, column=1, sticky=W) + + lbl2 = Label(self, text="Accuracy:") + lbl2.grid(row=0, column=2, sticky=E, padx=10, pady=10) + + self.ent2 = Entry(self, width=10) + self.ent2.insert(END, self.acc.value) + self.ent2.grid(row=0, column=3, sticky=W) + + self.startBtn = Button(self, text="Start", command=self.onStart) + self.startBtn.grid(row=1, column=0, padx=10, pady=5, sticky=W) + + self.pbar = Progressbar(self, mode='determinate') + self.pbar.grid(row=1, column=1, columnspan=3, sticky=W+E) + self.pbar.config(value=0, maximum=self.dig.value) + + self.txt = scrolledtext.ScrolledText(self) + self.txt.grid(row=2, column=0, rowspan=4, padx=10, pady=5, columnspan=5, sticky=E+W+S+N) + + def onStart(self): + + self.startBtn.config(state=DISABLED) + self.txt.delete("1.0", END) + + digits = int(self.ent1.get()) + accuracy = int(self.ent2.get()) + + self.p1 = Process(target=generatePi, args=(q, digits, accuracy)) + self.p1.start() + self.after(DELAY1, self.onGetValue) + + + def onGetValue(self): + + if (self.p1.is_alive()): + self.val.value += 1000 + self.update_pbar() + self.after(DELAY1, self.onGetValue) + return + else: + + try: + + self.txt.insert('end', q.get(0)) + self.txt.insert('end', "\n") + self.startBtn.config(state=NORMAL) + + except Empty: + print("queue is empty") + + def update_pbar(self): + self.pbar.config(value=self.val.value) + +class win_Example(Frame): + + def __init__(self, parent): + Frame.__init__(self, parent, name="frame") + commonUI(parent) + +if GUI == GUI_WIN: + # Generate function must be a top-level module funtion + def generatePi(q, digs, acc): + + getcontext().prec = digs + + pi = Decimal(0) + k = 0 + n = acc + + while k < n: + pi += (Decimal(1)/(16**k))*((Decimal(4)/(8*k+1)) - \ + (Decimal(2)/(8*k+4)) - (Decimal(1)/(8*k+5))- \ + (Decimal(1)/(8*k+6))) + k += 1 + q.put(pi) + time.sleep(1) + +class linux_Example(Frame): + + def __init__(self, parent, q): + Frame.__init__(self, parent) + + self.queue = q + commonUI(parent) + + def generatePi(self, queue): + + getcontext().prec = self.digits + + pi = Decimal(0) + k = 0 + n = self.accuracy + + while k < n: + pi += (Decimal(1)/(16**k))*((Decimal(4)/(8*k+1)) - \ + (Decimal(2)/(8*k+4)) - (Decimal(1)/(8*k+5))- \ + (Decimal(1)/(8*k+6))) + k += 1 + print (self.p1.is_alive()) + + queue.put(pi) + print("end") + +def main(): + if GUI == GUI_LINUX: + q = Queue() + + root = Tk() + root.geometry("400x350+300+300") + app = win_Example(root) if GUI == GUI_WIN else linux_Example(root) + root.mainloop() + + +if __name__ == '__main__': + main() diff --git a/Multiprocessing test/calc_pi_linux.py b/Multiprocessing test/calc_pi_linux.py new file mode 100644 index 0000000..f27b52a --- /dev/null +++ b/Multiprocessing test/calc_pi_linux.py @@ -0,0 +1,136 @@ +#!/usr/bin/env python3 +# -*- coding: utf-8 -*- + +""" +ZetCode Tkinter e-book + +This script produces a long-running task of calculating +a large Pi number, while keeping the GUI responsive. + +Author: Jan Bodnar +Last modified: January 2016 +Website: www.zetcode.com +""" + +from tkinter import (Tk, BOTH, Text, E, W, S, N, END, + NORMAL, DISABLED, StringVar) +from tkinter.ttk import Frame, Label, Button, Progressbar, Entry +from tkinter import scrolledtext + +from multiprocessing import Queue, Process +import queue +from decimal import Decimal, getcontext + +import time + +DELAY1 = 20 +DELAY2 = 20 + +class Example(Frame): + + def __init__(self, parent, q): + Frame.__init__(self, parent) + + self.queue = q + self.parent = parent + self.initUI() + + + def initUI(self): + + self.parent.title("Pi computation") + self.pack(fill=BOTH, expand=True) + + self.grid_columnconfigure(4, weight=1) + self.grid_rowconfigure(3, weight=1) + + lbl1 = Label(self, text="Digits:") + lbl1.grid(row=0, column=0, sticky=E, padx=10, pady=10) + + self.ent1 = Entry(self, width=10) + self.ent1.insert(END, "4000") + self.ent1.grid(row=0, column=1, sticky=W) + + lbl2 = Label(self, text="Accuracy:") + lbl2.grid(row=0, column=2, sticky=E, padx=10, pady=10) + + self.ent2 = Entry(self, width=10) + self.ent2.insert(END, "100") + self.ent2.grid(row=0, column=3, sticky=W) + + self.startBtn = Button(self, text="Start", + command=self.onStart) + self.startBtn.grid(row=1, column=0, padx=10, pady=5, sticky=W) + + self.pbar = Progressbar(self, mode='indeterminate') + self.pbar.grid(row=1, column=1, columnspan=3, sticky=W+E) + + self.txt = scrolledtext.ScrolledText(self) + self.txt.grid(row=2, column=0, rowspan=4, padx=10, pady=5, + columnspan=5, sticky=E+W+S+N) + + + def onStart(self): + + self.startBtn.config(state=DISABLED) + self.txt.delete("1.0", END) + + self.digits = int(self.ent1.get()) + self.accuracy = int(self.ent2.get()) + + self.p1 = Process(target=self.generatePi, args=(self.queue,)) + self.p1.start() + self.pbar.start(DELAY2) + self.after(DELAY1, self.onGetValue) + + + def onGetValue(self): + + if (self.p1.is_alive()): + + self.after(DELAY1, self.onGetValue) + return + else: + + try: + self.txt.insert('end', self.queue.get(0)) + self.txt.insert('end', "\n") + self.pbar.stop() + self.startBtn.config(state=NORMAL) + + except queue.Empty: + print("queue is empty") + + + def generatePi(self, queue): + + getcontext().prec = self.digits + + pi = Decimal(0) + k = 0 + n = self.accuracy + + while k < n: + pi += (Decimal(1)/(16**k))*((Decimal(4)/(8*k+1)) - \ + (Decimal(2)/(8*k+4)) - (Decimal(1)/(8*k+5))- \ + (Decimal(1)/(8*k+6))) + k += 1 + print (self.p1.is_alive()) + + queue.put(pi) + print("end") + time.sleep(1) + + +def main(): + + q = Queue() + + root = Tk() + root.geometry("400x350+300+300") + app = Example(root, q) + root.mainloop() + + +if __name__ == '__main__': + main() \ No newline at end of file diff --git a/Multiprocessing test/calc_pi_win.py b/Multiprocessing test/calc_pi_win.py new file mode 100644 index 0000000..b712964 --- /dev/null +++ b/Multiprocessing test/calc_pi_win.py @@ -0,0 +1,134 @@ +#!/usr/bin/env python3 +# -*- coding: utf-8 -*- + +""" +ZetCode Tkinter e-book + +This script produces a long-running task of calculating +a large Pi number, while keeping the GUI responsive. +This is an example written for Windows. + +Author: Jan Bodnar +Last modified: January 2016 +Website: www.zetcode.com +""" + +from tkinter import (Tk, BOTH, Text, E, W, S, N, END, + NORMAL, DISABLED, StringVar) +from tkinter.ttk import Frame, Label, Button, Progressbar, Entry +from tkinter import scrolledtext + +from multiprocessing import Process, Manager, Queue +from queue import Empty +from decimal import Decimal, getcontext + + +DELAY1 = 80 +DELAY2 = 20 + +# Queue must be global +q = Queue() + +class Example(Frame): + + def __init__(self, parent): + Frame.__init__(self, parent, name="frame") + + self.parent = parent + self.initUI() + + + def initUI(self): + + self.parent.title("Pi computation") + self.pack(fill=BOTH, expand=True) + + self.grid_columnconfigure(4, weight=1) + self.grid_rowconfigure(3, weight=1) + + lbl1 = Label(self, text="Digits:") + lbl1.grid(row=0, column=0, sticky=E, padx=10, pady=10) + + self.ent1 = Entry(self, width=10) + self.ent1.insert(END, "4000") + self.ent1.grid(row=0, column=1, sticky=W) + + lbl2 = Label(self, text="Accuracy:") + lbl2.grid(row=0, column=2, sticky=E, padx=10, pady=10) + + self.ent2 = Entry(self, width=10) + self.ent2.insert(END, "100") + self.ent2.grid(row=0, column=3, sticky=W) + + self.startBtn = Button(self, text="Start", + command=self.onStart) + self.startBtn.grid(row=1, column=0, padx=10, pady=5, sticky=W) + + self.pbar = Progressbar(self, mode='indeterminate') + self.pbar.grid(row=1, column=1, columnspan=3, sticky=W+E) + + self.txt = scrolledtext.ScrolledText(self) + self.txt.grid(row=2, column=0, rowspan=4, padx=10, pady=5, + columnspan=5, sticky=E+W+S+N) + + + def onStart(self): + + self.startBtn.config(state=DISABLED) + self.txt.delete("1.0", END) + + digits = int(self.ent1.get()) + accuracy = int(self.ent2.get()) + + self.p1 = Process(target=generatePi, args=(q, digits, accuracy)) + self.p1.start() + self.pbar.start(DELAY2) + self.after(DELAY1, self.onGetValue) + + + def onGetValue(self): + + if (self.p1.is_alive()): + + self.after(DELAY1, self.onGetValue) + return + else: + + try: + + self.txt.insert('end', q.get(0)) + self.txt.insert('end', "\n") + self.pbar.stop() + self.startBtn.config(state=NORMAL) + + except Empty: + print("queue is empty") + +# Generate function must be a top-level module funtion +def generatePi(q, digs, acc): + + getcontext().prec = digs + + pi = Decimal(0) + k = 0 + n = acc + + while k < n: + pi += (Decimal(1)/(16**k))*((Decimal(4)/(8*k+1)) - \ + (Decimal(2)/(8*k+4)) - (Decimal(1)/(8*k+5))- \ + (Decimal(1)/(8*k+6))) + k += 1 + + q.put(pi) + + +def main(): + + root = Tk() + root.geometry("400x350+300+300") + app = Example(root) + root.mainloop() + + +if __name__ == '__main__': + main() \ No newline at end of file diff --git a/Multiprocessing test/gui_mp.py b/Multiprocessing test/gui_mp.py new file mode 100644 index 0000000..01001b0 --- /dev/null +++ b/Multiprocessing test/gui_mp.py @@ -0,0 +1,43 @@ +# Test Code for Tkinter with threads +import tkinter as Tk +import multiprocessing +from Queue import Empty, Full +import time + +class GuiApp(object): + def __init__(self,q): + self.root = Tk.Tk() + self.root.geometry('300x100') + self.text_wid = Tk.Text(self.root,height=100,width=100) + self.text_wid.pack(expand=1,fill=Tk.BOTH) + self.root.after(100,self.CheckQueuePoll,q) + + def CheckQueuePoll(self,c_queue): + try: + str = c_queue.get(0) + self.text_wid.insert('end',str) + except Empty: + pass + finally: + self.root.after(100, self.CheckQueuePoll, c_queue) + +# Data Generator which will generate Data +def GenerateData(q): + for i in range(10): + print("Generating Some Data, Iteration %s" %(i)) + time.sleep(2) + q.put("Some Data from iteration %s \n" %(i)) + + +if __name__ == '__main__': +# Queue which will be used for storing Data + + q = multiprocessing.Queue() + q.cancel_join_thread() # or else thread that puts data will not term + gui = GuiApp(q) + t1 = multiprocessing.Process(target=GenerateData,args=(q,)) + t1.start() + gui.root.mainloop() + + t1.join() + t2.join() \ No newline at end of file diff --git a/Multiprocessing test/mp_counter.py b/Multiprocessing test/mp_counter.py new file mode 100644 index 0000000..4dac468 --- /dev/null +++ b/Multiprocessing test/mp_counter.py @@ -0,0 +1,29 @@ +import time +from multiprocessing import Process, Value, Lock + +class Counter(object): + def __init__(self, initval=0): + self.val = Value('i', initval) + self.lock = Lock() + + def increment(self): + with self.lock: + self.val.value += 1 + + def value(self): + with self.lock: + return self.val.value + +def func(counter): + for i in range(500): + time.sleep(0.001) + counter.increment() + +if __name__ == '__main__': + counter = Counter(0) + procs = [Process(target=func, args=(counter,)) for i in range(10)] + + for p in procs: p.start() + for p in procs: p.join() + + print (counter.value()) \ No newline at end of file diff --git a/Multiprocessing test/mp_test.py b/Multiprocessing test/mp_test.py new file mode 100644 index 0000000..742fe5c --- /dev/null +++ b/Multiprocessing test/mp_test.py @@ -0,0 +1,40 @@ +import tkinter as tk +from tkinter import ttk +import tkinter.messagebox + +import time # to simulate long-running function + +from threading import Thread + +class RedactionSolutions: + + def __init__(self, master): + tk.Button(master, text="Run RS Wizard", fg='green', command=self.run).grid(row=5) + + def long_running_function(self): + #self.working = tk.Label(root, text='In Progress. Please Wait...') + self.working = ttk.Progressbar(root, orient='horizontal', mode='determinate') + self.working.config(value=0, maximum=10) + self.working.grid(row=6, columnspan=2) + + for x in range(10): + print(x) + self.working.config(value=x) + time.sleep(1) + + restart = tkinter.messagebox.askquestion('RS Wizard', 'Redaction complete! See file location for result.\nWould you like to redact another file?') + if restart == 'yes': + #self.reset() + self.working.grid_forget() + else: + root.destroy() + + def run(self): + Thread(target=self.long_running_function).start() + + + +root = tk.Tk() +root.wm_title("RS Wizard") +RedactionSolutions(root) +root.mainloop() \ No newline at end of file diff --git a/Multiprocessing test/mp_test_1.py b/Multiprocessing test/mp_test_1.py new file mode 100644 index 0000000..00183ad --- /dev/null +++ b/Multiprocessing test/mp_test_1.py @@ -0,0 +1,18 @@ +from multiprocessing import Process +import os + +def info(title): + print(title) + print('module name:', __name__) + print('parent process:', os.getppid()) + print('process id:', os.getpid()) + +def f(name): + info('function f') + print('hello', name) + +if __name__ == '__main__': + info('main line') + p = Process(target=f, args=('bob',)) + p.start() + p.join() \ No newline at end of file diff --git a/Multiprocessing test/mp_test_2.py b/Multiprocessing test/mp_test_2.py new file mode 100644 index 0000000..b94505d --- /dev/null +++ b/Multiprocessing test/mp_test_2.py @@ -0,0 +1,21 @@ +import multiprocessing +import time + +data = (['a', '2'], ['b', '4'], ['c', '6'], ['d', '8'], + ['e', '1'], ['f', '3'], ['g', '5'], ['h', '7'] +) + + +def mp_handler(var1): + for indata in var1: + p = multiprocessing.Process(target=mp_worker, args=(indata[0], indata[1])) + p.start() + + +def mp_worker(inputs, the_time): + print " Processs %s\tWaiting %s seconds" % (inputs, the_time) + time.sleep(int(the_time)) + print " Process %s\tDONE" % inputs + +if __name__ == '__main__': + mp_handler(data) \ No newline at end of file diff --git a/Multiprocessing test/mp_test_3.py b/Multiprocessing test/mp_test_3.py new file mode 100644 index 0000000..f1f9787 --- /dev/null +++ b/Multiprocessing test/mp_test_3.py @@ -0,0 +1,352 @@ +#!/usr/bin/env python + +import sys #exit +import gobject +import pygtk +pygtk.require('2.0') +import gtk +import multiprocessing +import threading +import time +import random + +gtk.gdk.threads_init() + +# Any time you connect to any of these signals, they should be +# protected with a threading.Lock or similar. +class ObserverBase(gobject.GObject): + __gsignals__ = { + 'message_event' : (gobject.SIGNAL_RUN_LAST, + gobject.TYPE_NONE, + (gobject.TYPE_STRING,)), + 'updated' : (gobject.SIGNAL_RUN_LAST, + gobject.TYPE_NONE, + (gobject.TYPE_FLOAT,)), + 'finished': (gobject.SIGNAL_RUN_LAST, + gobject.TYPE_NONE, + ()), + 'errored': (gobject.SIGNAL_RUN_LAST, + gobject.TYPE_NONE, + ()) + } + + def __init__(self): + gobject.GObject.__init__(self) + +gobject.type_register(ObserverBase) + +class Observer(ObserverBase): + def __init__(self, queue): + ObserverBase.__init__(self) + self.queue = queue + + def start(self): + # Create new thread to do the listening + self.thread = threading.Thread(target=self.listen, args=()) + self.thread.start() + + def join(self): + self.thread.join() + + def listen(self): + while True: + # Listen for results on the queue and process them accordingly + print "Observer is waiting for data" + data = self.queue.get() + print "Observer received: ",data + dataType = type(data).__name__ + if dataType=="bool" and data==True: + self.emit('finished') + return + if dataType=="bool" and data==False: + self.emit('errored') + return + elif dataType=="str": + self.emit('message_event', data) + elif dataType=="float": + self.emit('updated', data) + else: + raise RuntimeError("Queue had an unknown data type ('"+dataType+"') with value '"+str(data)+"'") + +gobject.type_register(Observer) + +class Process(gobject.GObject): + __gsignals__ = { + 'starting': (gobject.SIGNAL_RUN_LAST, + gobject.TYPE_NONE, + ()), + 'joined': (gobject.SIGNAL_RUN_LAST, + gobject.TYPE_NONE, + ()) + } + + def __init__(self, function, arguments): + gobject.GObject.__init__(self) + # Create queue + self.queue = multiprocessing.Queue() + + # Create process + if arguments==(): arguments=(self.queue,) + self.process = multiprocessing.Process(target=function, args=arguments) + + # Create observer + self.observer = Observer(self.queue) + + def getObserver(self): + return self.observer + + def start(self): + # Starting observer + self.emit("starting") + print "starting observer" + self.observer.start() + # Start process + print "starting process" + self.process.start() + + def join(self): + # Clean up after process + print "cleaning up after process" + self.process.join() + self.emit("joined") + # Clean up after observer + print "cleaning up after observer" + self.queue.put(False) # If the false is ever read, it's an error + self.observer.join() + # Clean up queue + #self.queue.close() + +gobject.type_register(Process) + +class CombinedObserver(ObserverBase): + def __init__(self, observers, weights=None): + ObserverBase.__init__(self) + self.observers = observers + numObservers = len(observers) + if weights==None: + self.weights = [1 for i in range(numObservers)] + else: + self.weights = weights + + sum_weights = sum(self.weights) + self.weights = [float(w)/sum_weights for w in self.weights] + print "weights = ",self.weights + + index = 0 + for observer in self.observers: + #observer.connect("message_event", onMessageReceived, index) + observer.connect("updated", self.onUpdate, index) + observer.connect("finished", self.onFinished, index) + observer.connect("errored", self.onErrored, index) + index = index+1 + + self.fractions = [0 for i in range(numObservers)] + self.finished = [False for i in range(numObservers)] + self.errored = [False for i in range(numObservers)] + self.lock = threading.RLock() + + def onUpdate(self, widget, fraction, index): + self.lock.acquire() + self.fractions[index] = fraction * self.weights[index] + self.emit("updated", sum(self.fractions)) + self.lock.release() + + def onFinished(self, widget, index): + self.lock.acquire() + self.finished[index] = True + if all(self.finished): + self.emit("finished") + self.lock.release() + + def onErrored(self, widget, index): + self.lock.acquire() + self.errored[index] = True + self.emit("errored") + self.lock.release() + +gobject.type_register(CombinedObserver) + +# A ProgressWorker is responsible for (a) getting the job done and (b) +# reporting progress. Progress is reported via the queue. Fractions +# (from 0 to 1) indicate the proportion of the job that is finished. +# Strings may indicate what has been done or what is about to be done. +# The boolean 'True' is placed on the queue when the job is finished. +class ProgressWorker(): + def getResult(self): + return None + + def setQueue(self, queue): + self.queue = queue + + def go(self): + raise RuntimeError("Worker.go() has not been overridden") + +class WorkerExample(ProgressWorker): + def __init__(self): + #Worker.__init__(self) + manager = multiprocessing.Manager() + self.result = manager.list() + + def getResult(self): + return self.result + + def go(self, queue): + self.setQueue(queue) + print "The worker has started doing some work (counting from 0 to 9)" + for i in range(10): + self.queue.put("working on step "+str(i)+"...") + proportion = (float(i)+1)/10 + slp = random.uniform(0.50, 3.50) + time.sleep(slp) + self.queue.put(proportion) + self.queue.put("done.") + self.queue.put(1.0) + self.queue.put(True) + print "The worker has finished." + self.result.append("The the work has been finished and the result isn't 42.") + + + + +class ProgressBar(gobject.GObject): + __gsignals__ = { + 'updated' : (gobject.SIGNAL_RUN_LAST, + gobject.TYPE_NONE, + (gobject.TYPE_FLOAT,)), + 'finished': (gobject.SIGNAL_RUN_LAST, + gobject.TYPE_NONE, + ()) + } + + def __init__(self, observer): + gobject.GObject.__init__(self) + self.observer = observer + self.observer.connect("updated",self.displayUpdate) + self.observer.connect("message_event",self.displayMessage) + self.observer.connect("finished",self.finish) + self.progress = gtk.ProgressBar() + self.progress.connect("destroy", self.destroy) + self.process=None + self.lock = threading.RLock() + + def displayUpdate(self, obj, fraction): + self.lock.acquire() + self.progress.set_fraction(fraction) + self.emit("updated", fraction) + self.lock.release() + + def displayMessage(self, obj, text): + self.lock.acquire() + self.progress.set_text(text) + self.lock.release() + + def finish(self, obj, data=None): + self.emit("finished") + + def destroy(self, widget, data=None): + print "Destroying" + if self.process==None: + return + self.queue.put("finished") + self.process.join() + self.thread.join() + + + +gobject.type_register(ProgressBar) + + +class ProgressBarWindow: + def __init__(self, observer, title): + self.progressBar = ProgressBar(observer) + self.progressBar.connect("finished", self.done) + window = gtk.Window(gtk.WINDOW_TOPLEVEL) + window.set_border_width(10) + window.set_title(title) + window.add(self.progressBar.progress) + window.show_all() + window.connect("destroy", self.destroy) + + def main(self): + gtk.main() + + def done(self, widget, data=None): + #self.destroy() + print "FINISHED!" + + def destroy(self, widget=None, data=None): + gtk.main_quit() + + + +# Test +if __name__ == '__main__': + # Three workers + worker1 = WorkerExample() + process1 = Process(worker1.go, ()) + observer1 = process1.getObserver() + + worker2 = WorkerExample() + process2 = Process(worker2.go, ()) + observer2 = process2.getObserver() + + worker3 = WorkerExample() + process3 = Process(worker3.go, ()) + observer3 = process3.getObserver() + + combo = CombinedObserver([observer1, observer2, observer3]) + + window0 = ProgressBarWindow(combo, "Workers' progress") + window1 = ProgressBarWindow(observer1, "Workers One's progress") + window2 = ProgressBarWindow(observer2, "Workers Two's progress") + window3 = ProgressBarWindow(observer3, "Workers Three's progress") + + process1.start() + #time.sleep(0.1) + process2.start() + #time.sleep(0.1) + process3.start() + gtk.main() + #process1.join() + #process2.join() + #process3.join() + print "finished" + sys.exit() + + # Two workers + worker1 = WorkerExample() + process1 = Process(worker1.go, ()) + observer1 = process1.getObserver() + + worker2 = WorkerExample() + process2 = Process(worker2.go, ()) + observer2 = process2.getObserver() + + combo = CombinedObserver([observer1, observer2]) + + window0 = ProgressBarWindow(combo, "Workers' progress") + window1 = ProgressBarWindow(observer1, "Workers One's progress") + window2 = ProgressBarWindow(observer2, "Workers Two's progress") + + process1.start() + time.sleep(0.1) + process2.start() + gtk.main() + process1.join() + process2.join() + print "finished" + sys.exit() + + # One worker + print "creating worker1" + worker1 = WorkerExample() # The task to do, carefully constructed to give feedback via a queue + print "creating process1" + process1 = Process(worker1.go, ()) # Responsible for looking after the worker: supplying the queue, setting up the observer, starting process + observer1 = process1.getObserver() # An observer of the worker's progress that listens to the queue + window1 = ProgressBarWindow(observer1, "Workers One's progress") # A display of the progress, connected to the observer + process1.start() + gtk.main() + process1.join() + print "result = ", worker1.result + print "finished" + sys.exit() diff --git a/Multiprocessing test/mp_test_4.py b/Multiprocessing test/mp_test_4.py new file mode 100644 index 0000000..956ec83 --- /dev/null +++ b/Multiprocessing test/mp_test_4.py @@ -0,0 +1,300 @@ +#!/usr/bin/env python + +import sys #exit +import gobject +import pygtk +pygtk.require('2.0') +import gtk +import multiprocessing +import threading +import time +import random + +gtk.gdk.threads_init() + +# Any time you connect to any of these signals, they should be +# protected with a threading.Lock or similar. +class ObserverBase(gobject.GObject): + __gsignals__ = { + 'message_event' : (gobject.SIGNAL_RUN_LAST, + gobject.TYPE_NONE, + (gobject.TYPE_STRING,)), + 'updated' : (gobject.SIGNAL_RUN_LAST, + gobject.TYPE_NONE, + (gobject.TYPE_FLOAT,)), + 'finished': (gobject.SIGNAL_RUN_LAST, + gobject.TYPE_NONE, + ()), + 'errored': (gobject.SIGNAL_RUN_LAST, + gobject.TYPE_NONE, + ()) + } + + def __init__(self): + gobject.GObject.__init__(self) + +gobject.type_register(ObserverBase) + +class Observer(ObserverBase): + def __init__(self, queue): + ObserverBase.__init__(self) + self.queue = queue + + def start(self): + # Create new thread to do the listening + self.thread = threading.Thread(target=self.listen, args=()) + self.thread.start() + + def join(self): + self.thread.join() + + def listen(self): + while True: + # Listen for results on the queue and process them accordingly + print "Observer is waiting for data" + data = self.queue.get() + print "Observer received: ",data + dataType = type(data).__name__ + if dataType=="bool" and data==True: + self.emit('finished') + return + if dataType=="bool" and data==False: + self.emit('errored') + return + elif dataType=="str": + self.emit('message_event', data) + elif dataType=="float": + self.emit('updated', data) + else: + raise RuntimeError("Queue had an unknown data type ('"+dataType+"') with value '"+str(data)+"'") + +gobject.type_register(Observer) + +class Process(gobject.GObject): + __gsignals__ = { + 'starting': (gobject.SIGNAL_RUN_LAST, + gobject.TYPE_NONE, + ()), + 'joined': (gobject.SIGNAL_RUN_LAST, + gobject.TYPE_NONE, + ()) + } + + def __init__(self, function, arguments): + gobject.GObject.__init__(self) + # Create queue + self.queue = multiprocessing.Queue() + + # Create process + if arguments==(): arguments=(self.queue,) + self.process = multiprocessing.Process(target=function, args=arguments) + + # Create observer + self.observer = Observer(self.queue) + + def getObserver(self): + return self.observer + + def start(self): + # Starting observer + self.emit("starting") + print "starting observer" + self.observer.start() + # Start process + print "starting process" + self.process.start() + + def join(self): + # Clean up after process + print "cleaning up after process" + self.process.join() + self.emit("joined") + # Clean up after observer + print "cleaning up after observer" + self.queue.put(False) # If the false is ever read, it's an error + self.observer.join() + # Clean up queue + #self.queue.close() + +gobject.type_register(Process) + +class CombinedObserver(ObserverBase): + def __init__(self, observers, weights=None): + ObserverBase.__init__(self) + self.observers = observers + numObservers = len(observers) + if weights==None: + self.weights = [1 for i in range(numObservers)] + else: + self.weights = weights + + sum_weights = sum(self.weights) + self.weights = [float(w)/sum_weights for w in self.weights] + print "weights = ",self.weights + + index = 0 + for observer in self.observers: + #observer.connect("message_event", onMessageReceived, index) + observer.connect("updated", self.onUpdate, index) + observer.connect("finished", self.onFinished, index) + observer.connect("errored", self.onErrored, index) + index = index+1 + + self.fractions = [0 for i in range(numObservers)] + self.finished = [False for i in range(numObservers)] + self.errored = [False for i in range(numObservers)] + self.lock = threading.RLock() + + def onUpdate(self, widget, fraction, index): + self.lock.acquire() + self.fractions[index] = fraction * self.weights[index] + self.emit("updated", sum(self.fractions)) + self.lock.release() + + def onFinished(self, widget, index): + self.lock.acquire() + self.finished[index] = True + if all(self.finished): + self.emit("finished") + self.lock.release() + + def onErrored(self, widget, index): + self.lock.acquire() + self.errored[index] = True + self.emit("errored") + self.lock.release() + +gobject.type_register(CombinedObserver) + +# A ProgressWorker is responsible for (a) getting the job done and (b) +# reporting progress. Progress is reported via the queue. Fractions +# (from 0 to 1) indicate the proportion of the job that is finished. +# Strings may indicate what has been done or what is about to be done. +# The boolean 'True' is placed on the queue when the job is finished. +class ProgressWorker(): + def getResult(self): + return None + + def setQueue(self, queue): + self.queue = queue + + def go(self): + raise RuntimeError("Worker.go() has not been overridden") + +class WorkerExample(ProgressWorker): + def __init__(self): + #Worker.__init__(self) + manager = multiprocessing.Manager() + self.result = manager.list() + + def getResult(self): + return self.result + + def go(self, queue): + self.setQueue(queue) + print "The worker has started doing some work (counting from 0 to 9)" + for i in range(10): + self.queue.put("working on step "+str(i)+"...") + proportion = (float(i)+1)/10 + slp = random.uniform(0.50, 3.50) + time.sleep(slp) + self.queue.put(proportion) + self.queue.put("done.") + self.queue.put(1.0) + self.queue.put(True) + print "The worker has finished." + self.result.append("The the work has been finished and the result isn't 42.") + +class ProgressBar(gobject.GObject): + __gsignals__ = { + 'updated' : (gobject.SIGNAL_RUN_LAST, + gobject.TYPE_NONE, + (gobject.TYPE_FLOAT,)), + 'finished': (gobject.SIGNAL_RUN_LAST, + gobject.TYPE_NONE, + ()) + } + + def __init__(self, observer): + gobject.GObject.__init__(self) + self.observer = observer + self.observer.connect("updated",self.displayUpdate) + self.observer.connect("message_event",self.displayMessage) + self.observer.connect("finished",self.finish) + self.progress = gtk.ProgressBar() + self.progress.connect("destroy", self.destroy) + self.process=None + self.lock = threading.RLock() + + def displayUpdate(self, obj, fraction): + self.lock.acquire() + self.progress.set_fraction(fraction) + self.emit("updated", fraction) + self.lock.release() + + def displayMessage(self, obj, text): + self.lock.acquire() + self.progress.set_text(text) + self.lock.release() + + def finish(self, obj, data=None): + self.emit("finished") + + def destroy(self, widget, data=None): + print "Destroying" + if self.process==None: + return + self.queue.put("finished") + self.process.join() + self.thread.join() + +gobject.type_register(ProgressBar) + +class ProgressBarWindow: + def __init__(self, observer, title): + self.progressBar = ProgressBar(observer) + self.progressBar.connect("finished", self.done) + window = gtk.Window(gtk.WINDOW_TOPLEVEL) + window.set_border_width(10) + window.set_title(title) + window.add(self.progressBar.progress) + window.show_all() + window.connect("destroy", self.destroy) + + def main(self): + gtk.main() + + def done(self, widget, data=None): + #self.destroy() + print "FINISHED!" + + def destroy(self, widget=None, data=None): + gtk.main_quit() + +# Test +if __name__ == '__main__': + # n workers + worker = [] + process = [] + observer =[] + window = [] + + for i in range(10): + worker.append(WorkerExample()) + process.append(Process(worker[i].go, ())) + observer.append(Process[i].getObserver()) + + combo = CombinedObserver(observer) + + window0 = ProgressBarWindow(combo, "Workers' progress") + for i in range(10): + window.append(ProgressBarWindow(observer[i], "Workers {}'s progress".format(str(i)))) + + for i in range(10): + process[i].start() + + gtk.main() + + print "finished" + sys.exit() + \ No newline at end of file diff --git a/Multiprocessing test/multiprocessing b/Multiprocessing test/multiprocessing new file mode 100644 index 0000000..2a5eb46 --- /dev/null +++ b/Multiprocessing test/multiprocessing @@ -0,0 +1 @@ +/data_2/Projects/h2inc/multiprocessing_test.py \ No newline at end of file diff --git a/Multiprocessing test/multiprocessing_test.py b/Multiprocessing test/multiprocessing_test.py new file mode 100644 index 0000000..748c70c --- /dev/null +++ b/Multiprocessing test/multiprocessing_test.py @@ -0,0 +1,314 @@ +#!/usr/bin/env python3.5 + +# You are free to use and/or change this code for +# your own needs. + +# Original code (c)2018 Jan Lerking +# Program to test various scenarios of +# single thread, multi thread, pool and process + +import os +import sys +import multiprocessing as mp +import time +import io +import cpuinfo +import queue +import threading +import time +import random +import string +import shutil +from tkinter import Tk, ttk, Label, Button, LabelFrame +from tkinter import filedialog, Entry, Checkbutton +from tkinter import Grid, StringVar, DoubleVar +from tkinter import N, E, S, W +from tkinter import DISABLED, NORMAL +from os import errno +import os +from os.path import expanduser +import platform +class file_progress: + def __init__(self, master): + self.sourcedir = StringVar() + self.destdir = StringVar() + self.addinc = StringVar() + self.cfilevar = DoubleVar() + self.totalvar = 0 + self.filecnt = 0 + self.infofolder = 'Number of folders: 0' + self.infofile = 'Number of headers: 0' + self.currentfile = 'Current file: ' + self.totprogress = 'Total progress: ' + self.sourcedir.set('Select source directory!') + self.destdir.set('Select destination directory!') + + self.master = master + self.master.title('File progress') + self.master.grid_columnconfigure(1, weight=1) + + self.frame = LabelFrame(master, text='Progress') + self.frame.grid(row=0, column=0, columnspan=3, sticky=N+S+E+W, padx=5, pady=5) + self.frame.grid_columnconfigure(1, weight=1) + + self.totallabel = Label(self.frame, text=self.totprogress) + self.totallabel.grid(row=8, column=0, sticky=W, padx=5, pady=5) + self.totallabel.config(state=DISABLED) + + self.totalprogress = ttk.Progressbar(self.frame, orient='horizontal', mode='determinate') + self.totalprogress.grid(row=9, column=0, columnspan=3, sticky=N+S+E+W, padx=5, pady=5) + self.totalprogress.config(variable=self.totalvar, maximum=self.filecnt) + + def select_sourcedir(self, sourcedir): + root.directory = os.path.abspath(filedialog.askdirectory()) + if root.directory: + sourcedir.set(root.directory) + filecnt = sourcedir_filecnt(root.directory) + if filecnt > 0: + tempstr = 'Number of headers: '+str(filecnt) + temptot = 'Total progress: 0 of '+str(filecnt) + print ('Source directory: ', sourcedir.get()) + self.destlabel.config(state=NORMAL) + self.destentry.config(state=NORMAL) + self.destdir_button.config(state=NORMAL) + self.infofiles.config(text=tempstr) + self.totallabel.config(text=temptot) + self.filecnt = filecnt + foldercnt = sourcedir_foldercnt(root.directory) + if foldercnt > 0: + tempstr = 'Number of folders: '+str(foldercnt) + self.infofolders.config(text=tempstr) + + def select_destdir(self, destdir): + root.directory = filedialog.askdirectory() + if root.directory: + destdir.set(root.directory) + print ('Destination directory: ', destdir.get()) + self.incchkbox.config(state=NORMAL) + self.infofolders.config(state=NORMAL) + self.infofiles.config(state=NORMAL) + self.translate_button.config(state=NORMAL) + self.cfilelabel.config(state=NORMAL) + self.totallabel.config(state=NORMAL) + + def translate(self, destdir, sourcedir, addinc): + doinc = addinc.get() + dest = destdir.get() + source = sourcedir.get() + if doinc == 'yes': + dest = dest+'/include' + print(os.path.exists(os.path.dirname(dest))) + if not os.path.exists(os.path.dirname(dest)): + try: + os.makedirs(os.path.dirname(dest)) + except OSError as exc: # Guard against race condition + if exc.errno != errno.EEXIST: + raise + destdir.set(dest) + print ('Destination directory: ', destdir.get()) + process_files(source, dest) + + def cfileprogress_update(cnt): + self.cfilevar = cnt + + def currentfile_update(current): + self.currentfile = 'Current file: '+current + +root = Tk() +root.update() +#root.minsize(350, 210) +#width = (root.winfo_screenwidth()/2)-(350/2) +#height = (root.winfo_screenheight()/2)-(210/2) +#root.geometry('+%d+%d' % (width, height)) +root.resizable(False, False) +f_progress = file_progress(root) +root.mainloop() + +cpu_info = cpuinfo.get_cpu_info() +num_cores = mp.cpu_count() +print(num_cores) + +filelist = [] +exitFlag = 0 +queueLock = threading.Lock() +workQueue = queue.Queue(0) +threads = [] +class myThread (threading.Thread): + def __init__(self, threadID, name, q): + threading.Thread.__init__(self) + self.threadID = threadID + self.name = name + self.q = q + def run(self): + print ("Starting " + self.name) + process_data(self.name, self.q) + print ("Exiting " + self.name) + +def process_data(threadName, q): + while not exitFlag: + queueLock.acquire() + if not workQueue.empty(): + data = q.get() + process_file(data) + queueLock.release() + print ("%s processing %s" % (threadName, data)) + else: + queueLock.release() + time.sleep(1) + +def sourcedir_filecnt(sourcedir): + ### Return the number of files, ending with '.h', in sourcedir - including subdirectories ### + cnt = 0 + global filelist + for folderName, subfolders, files in os.walk(sourcedir): + for file in files: + if file.lower().endswith('.h'): + cnt += 1 + filelist += [folderName+'/'+file] + return cnt + +def process_files(sourcedir, destdir): + global filelist + for f in filelist: + process_file(f) + +def process_file(data): + outfile = '' + inputfile = data + encodings = ['utf-8', 'latin-1', 'windows-1250', 'windows-1252', 'ascii', + 'big5', 'big5hkscs', 'cp037', 'cp273', 'cp424', 'cp437', 'cp500', + 'cp720', 'cp737', 'cp775', 'cp850', 'cp852', 'cp855', 'cp856', + 'cp857', 'cp858', 'cp860', 'cp861', 'cp862', 'cp863', 'cp864', 'cp865', + 'cp866', 'cp869', 'cp874', 'cp875', 'cp932', 'cp949', 'cp950', 'cp1006', + 'cp1026', 'cp1125', 'cp1140', 'cp1250', 'cp1251', 'cp1252', 'cp1253', 'cp1254', + 'cp1255', 'cp1256', 'cp1257', 'cp1258', 'cp65001', 'euc-jp', 'euc-jis-2004', + 'euc-jisx0213', 'euc-kr', 'gb2312', 'gbk', 'gb18030', 'hz', 'iso2022-jp', + 'iso2022-jp-1', 'iso2022-jp-2', 'iso2022-jp-2004', 'iso2022-jp-3', + 'iso2022-jp-ext', 'iso2022-kr', 'iso8859-2', 'iso8859-3', 'iso8859-4', + 'iso8859-5', 'iso8859-6', 'iso8859-7', 'iso8859-8', 'iso8859-9', 'iso8859-10', + 'iso8859-11', 'iso8859-13', 'iso8859-14', 'iso8859-15', 'iso8859-16', 'johab', + 'koi8-r', 'koi8-t', 'koi8-u', 'kz1048', 'mac-cyrillic', 'mac-greek', + 'mac-iceland', 'mac-latin2', 'mac-roman', 'mac-turkish', 'ptcp154', + 'shift-jis', 'shift-jis-2004', 'shift-jisx0213', 'utf-32', 'utf-32-be', + 'utf-32-le', 'utf-16', 'utf-16-be', 'utf-16-le', 'utf-7', 'utf-8-sig'] + for e in encodings: + try: + fh = io.open(data, 'r', encoding=e) + fh.readlines() + fh.seek(0) + except UnicodeDecodeError: + print('got unicode error with %s , trying different encoding' % e) + else: + #print('opening the file with encoding: %s ' % e) + break + #print(os.path.basename(data)) + for lines in fh: + outfile = outfile+lines + fh.close() + outputfile = os.path.splitext(inputfile)[0]+'.inc' + outputfile = str(outputfile).replace(sourcedir, destdir) + #print(outputfile) + if not os.path.exists(os.path.dirname(outputfile)): + try: + os.makedirs(os.path.dirname(outputfile)) + except OSError as exc: # Guard against race condition + if exc.errno != errno.EEXIST: + raise + newfile = open(outputfile, "w") + newfile.write(outfile) + newfile.close() + +def single_thread(): + t1 = time.time() + cnt = sourcedir_filecnt(sourcedir) + print('Files in: '+str(cnt)) + process_files(sourcedir, destdir) + print('Single thread process time: '+str(time.time()-t1)) + +def multi_thread(): + global filelist + global exitFlag + + t1 = time.time() + cnt = sourcedir_filecnt(sourcedir) + threadList = ["thread 1", "thread 2", "thread 3", "thread 4", "thread 5"] + nameList = filelist + threadID = 0 + + # Create new threads + for tName in threadList: + thread = myThread(threadID, tName, workQueue) + thread.start() + threads.append(thread) + threadID += 1 + + # Fill the queue + queueLock.acquire() + for word in nameList: + workQueue.put(word) + queueLock.release() + + # Wait for queue to empty + while not workQueue.empty(): + pass + + # Notify threads it's time to exit + exitFlag = 1 + + # Wait for all threads to complete + for t in threads: + t.join() + print ("Exiting Main Thread") + print('Files in: '+str(cnt)) + print('Multi thread process time: '+str(time.time()-t1)) + +def async_process(num): + t1 = time.time() + cnt = sourcedir_filecnt(sourcedir) + + pool = mp.Pool(processes=num) + #[pool.apply_async(process_file, args=(f,)) for f in filelist] + pool.map(process_file, filelist) + #output = [p.get() for p in results] + #print(output) + + # Setup a list of processes that we want to run + #processes = [mp.Process(target=rand_string, args=(5, output)) for x in range(8)] + #processes = [mp.Process(target=process_files, args=(sourcedir, destdir)) for x in range(num)] + + # Run processes + #for p in processes: + #p.start() + + # Exit the completed processes + #for p in processes: + #p.join() + + # Get process results from the output queue + #results = [output.get() for p in processes] + + #print(results) + print('Files in: '+str(cnt)) + print('Multi ('+str(num)+') processes time: '+str(time.time()-t1)) + +def cleanup(dest): + shutil.rmtree(dest) + +#sourcedir = 'C:/Users/dksojlg/Documents/gtk+-3.22.26' +sourcedir = '/usr/include' +#destdir = 'C:/Users/dksojlg/Documents/include' +destdir = '/data_2/include' + +#print(cpu_info) +#single_thread() #2543 files - Single thread process time: 1.3732633590698242 sec. +#os.rmdir(destdir) +#multi_thread() #2543 files - Syncronized 5 threads process time: 80.25179100036621 sec. +#os.rmdir(destdir) +async_process(16) #2543 files - 2 processes, process time: 1.2379400730133057 sec. +#cleanup(destdir) +#async_process(4) #2543 files - 2 processes, process time: 2.6622860431671143 sec. +#cleanup(destdir) +#async_process(6) #2543 files - 2 processes, process time: 2.6622860431671143 sec. +#cleanup(destdir) +#async_process(8) #2543 files - 2 processes, process time: 2.6622860431671143 sec. +#cleanup(destdir) diff --git a/Multiprocessing test/pool_test.py b/Multiprocessing test/pool_test.py new file mode 100644 index 0000000..d7db443 --- /dev/null +++ b/Multiprocessing test/pool_test.py @@ -0,0 +1,20 @@ +from multiprocessing import Pool + +import time + +work = (["A", 5], ["B", 2], ["C", 1], ["D", 3]) + + +def work_log(work_data): + print(" Process %s waiting %s seconds" % (work_data[0], work_data[1])) + time.sleep(int(work_data[1])) + print(" Process %s Finished." % work_data[0]) + + +def pool_handler(): + p = Pool(2) + p.map(work_log, work) + + +if __name__ == '__main__': + pool_handler() \ No newline at end of file diff --git a/thread_priority_queue.py b/Multiprocessing test/thread_priority_queue.py similarity index 100% rename from thread_priority_queue.py rename to Multiprocessing test/thread_priority_queue.py diff --git a/Multiprocessing test/tkinter_mp.py b/Multiprocessing test/tkinter_mp.py new file mode 100644 index 0000000..a4a290d --- /dev/null +++ b/Multiprocessing test/tkinter_mp.py @@ -0,0 +1,57 @@ +from multiprocessing import Process, Value, Lock +from tkinter import Tk +from tkinter import Tk, ttk, Label, Button, LabelFrame +from tkinter import Grid, StringVar, DoubleVar +from tkinter import N, E, S, W +from tkinter import DISABLED, NORMAL +import os +import sys + +dirname = 'C:\\Users\\dksojlg\\Documents' + +platform = sys.platform +print(platform) + +class testGUI: + def __init__(self, master, initval=0): + self.progress = Value('i', initval) + self.dircount = Value('i', initval) + self.foldercnt = 'Folders: {}'.format(self.dircount.value) + self.lock = Lock() + + self.master = master + self.master.title('Multiprocess progressbar test!') + self.master.grid_columnconfigure(1, weight=1) + + self.frame = LabelFrame(master, text='Folder count') + self.frame.grid(row=0, column=0, columnspan=3, sticky=N+S+E+W, padx=5, pady=5) + self.frame.grid_columnconfigure(1, weight=1) + + self.sourcelabel = Label(self.frame, text=self.foldercnt) + self.sourcelabel.grid(row=0, column=0, sticky=E, padx=(5, 1), pady=5) + + self.sourcedir_button = Button(self.frame, text="Count folders...", command= lambda: get_directories(self)) + self.sourcedir_button.grid(row=0, column=2, sticky=W, padx=(3, 5), pady=5) + + def increment_dir(self): + with self.lock: + self.dircount.value += 1 + self.sourcelabel.config(text=self.foldercnt) + +def read_dir(testgui, dirname): + for folderName, subfolders, files in os.walk(dirname): + if subfolders: + for subfolder in subfolders: + read_dir(testgui, subfolder) + #testgui.increment_dir() + +def get_directories(testgui): + p = Process(target=read_dir, args=(testgui, dirname,)) + p.start() + +if __name__ == "__main__": + root = Tk() + root.update() + root.resizable(False, False) + testgui = testGUI(root) + root.mainloop() \ No newline at end of file diff --git a/__pycache__/h2inc.cpython-35.pyc b/__pycache__/h2inc.cpython-35.pyc index 57272df..92eab8b 100644 Binary files a/__pycache__/h2inc.cpython-35.pyc and b/__pycache__/h2inc.cpython-35.pyc differ diff --git a/__pycache__/h2inc.cpython-36.pyc b/__pycache__/h2inc.cpython-36.pyc new file mode 100644 index 0000000..f4763d6 Binary files /dev/null and b/__pycache__/h2inc.cpython-36.pyc differ diff --git a/__pycache__/h2inc_gui.cpython-35.pyc b/__pycache__/h2inc_gui.cpython-35.pyc index 2f74536..b5fb497 100644 Binary files a/__pycache__/h2inc_gui.cpython-35.pyc and b/__pycache__/h2inc_gui.cpython-35.pyc differ diff --git a/__pycache__/h2inc_gui.cpython-36.pyc b/__pycache__/h2inc_gui.cpython-36.pyc new file mode 100644 index 0000000..fbc1112 Binary files /dev/null and b/__pycache__/h2inc_gui.cpython-36.pyc differ diff --git a/__pycache__/h2inc_parser.cpython-36.pyc b/__pycache__/h2inc_parser.cpython-36.pyc new file mode 100644 index 0000000..914c3fc Binary files /dev/null and b/__pycache__/h2inc_parser.cpython-36.pyc differ diff --git a/concurrency-futures.py b/concurrency-futures.py new file mode 100644 index 0000000..d253ab7 --- /dev/null +++ b/concurrency-futures.py @@ -0,0 +1,50 @@ +import concurrent.futures +import time +import multiprocessing + +num_cores = multiprocessing.cpu_count() + +def is_prime(num): + if num <= 1: + return False + elif num <= 3: + return True + elif num%2 == 0 or num%3 == 0: + return False + i = 5 + while i*i <= num: + if num%i == 0 or num%(i+2) == 0: + return False + i += 6 + return True + +def find_sum(num): + sum_of_primes = 0 + + ix = 2 + + while ix <= num: + if is_prime(ix): + sum_of_primes += ix + ix += 1 + print("{} : Sum = {}".format(num, sum_of_primes)) + return sum_of_primes + +def sum_primes_thread(nums): + with concurrent.futures.ThreadPoolExecutor(max_workers = num_cores) as executor: + executor.map(find_sum, nums) + +def sum_primes_process(nums): + with concurrent.futures.ProcessPoolExecutor(max_workers = num_cores) as executor: + executor.map(find_sum, nums) + +if __name__ == '__main__': + nums = [1000000, 900000, 800000, 700000, 600000, 500000, 400000, 300000, 200000, 100000] + print("Using multithreading (num_threads = num_cores):") + start = time.time() + sum_primes_thread(nums) + print("Time taken = {0:.5f}".format(time.time() - start)) + print("Using multiprocessing (num_processes = num_cores):") + start = time.time() + sum_primes_process(nums) + print("Time taken = {0:.5f}".format(time.time() - start)) \ No newline at end of file diff --git a/h2inc b/h2inc new file mode 120000 index 0000000..775f62b --- /dev/null +++ b/h2inc @@ -0,0 +1 @@ +/data_2/Projects/h2inc/h2inc.py \ No newline at end of file diff --git a/h2inc.glade b/h2inc.glade new file mode 100644 index 0000000..ad6a6e9 --- /dev/null +++ b/h2inc.glade @@ -0,0 +1,367 @@ + + + + + + False + 5 + 5 + 5 + 5 + + + True + False + 5 + 5 + 5 + 5 + True + True + + + True + False + 0 + etched-out + + + True + False + 12 + + + True + False + + + True + False + end + 5 + 5 + 5 + True + Source: + right + + + 0 + 0 + 2 + + + + + True + True + 5 + 5 + 5 + 5 + Select source directory! + + + 2 + 0 + 2 + + + + + Source directory... + True + True + True + start + 5 + 5 + 5 + 5 + + + 4 + 0 + 2 + + + + + True + False + False + end + 5 + 5 + 5 + True + Destination: + right + start + 2.2351741811588166e-10 + + + 0 + 1 + 2 + + + + + True + False + True + 5 + 5 + 5 + 5 + Select destination directory! + + + 2 + 1 + 2 + + + + + Destination directory... + True + False + True + True + start + 5 + 5 + 5 + 5 + + + 4 + 1 + 2 + + + + + Create "include" folder if it does not exist. + True + False + True + False + 5 + 5 + 0 + True + + + 0 + 2 + 6 + + + + + + + + + True + False + Select folders + + + + + 0 + 0 + 5 + + + + + True + False + False + 5 + 5 + 0 + etched-out + + + True + False + 12 + + + True + False + + + True + False + 5 + 5 + True + 0 + etched-out + + + True + False + 12 + + + True + False + vertical + + + True + False + start + 5 + 5 + True + Number of folders: 0 + + + False + True + 0 + + + + + True + False + start + 5 + 5 + Number of files: 0 + + + False + True + 1 + + + + + + + + + True + False + Source information + + + + + 0 + 0 + + + + + Translate! + True + True + True + start + 5 + 5 + + + 0 + 1 + + + + + True + False + 5 + 5 + 5 + 0 + etched-out + + + True + False + 12 + + + True + False + vertical + + + True + False + start + 5 + 5 + Total progress: + + + False + True + 0 + + + + + True + False + 5 + 5 + 5 + + + False + True + 1 + + + + + + + + + True + False + Progress + + + + + 0 + 2 + + + + + + + + + True + False + Translation + + + + + 0 + 1 + 5 + + + + + + diff --git a/h2inc.glade~ b/h2inc.glade~ new file mode 100644 index 0000000..d0e629e --- /dev/null +++ b/h2inc.glade~ @@ -0,0 +1,365 @@ + + + + + + False + 5 + 5 + 5 + 5 + + + True + False + 5 + 5 + 5 + 5 + + + True + False + 0 + etched-out + + + True + False + 12 + + + True + False + + + True + False + end + 5 + 5 + 5 + True + Source: + right + + + 0 + 0 + 2 + + + + + True + True + 5 + 5 + 5 + 5 + Select source directory! + + + 2 + 0 + 2 + + + + + Source directory... + True + True + True + start + 5 + 5 + 5 + 5 + + + 4 + 0 + 2 + + + + + True + False + False + end + 5 + 5 + 5 + True + Destination: + right + start + 2.2351741811588166e-10 + + + 0 + 1 + 2 + + + + + True + False + True + 5 + 5 + 5 + 5 + Select destination directory! + + + 2 + 1 + 2 + + + + + Destination directory... + True + False + True + True + start + 5 + 5 + 5 + 5 + + + 4 + 1 + 2 + + + + + Create "include" folder if it does not exist. + True + False + True + False + 5 + 5 + 0 + True + + + 0 + 2 + 6 + + + + + + + + + True + False + Select folders + + + + + 0 + 0 + 5 + + + + + True + False + False + 5 + 5 + 0 + etched-out + + + True + False + 12 + + + True + False + + + True + False + 5 + 5 + True + 0 + etched-out + + + True + False + 12 + + + True + False + vertical + + + True + False + start + 5 + 5 + True + Number of folders: 0 + + + False + True + 0 + + + + + True + False + start + 5 + 5 + Number of files: 0 + + + False + True + 1 + + + + + + + + + True + False + Source information + + + + + 0 + 0 + + + + + Translate! + True + True + True + start + 5 + 5 + + + 0 + 1 + + + + + True + False + 5 + 5 + 5 + 0 + etched-out + + + True + False + 12 + + + True + False + vertical + + + True + False + start + 5 + 5 + Total progress: + + + False + True + 0 + + + + + True + False + 5 + 5 + 5 + + + False + True + 1 + + + + + + + + + True + False + Progress + + + + + 0 + 2 + + + + + + + + + True + False + Translation + + + + + 0 + 1 + 5 + + + + + + diff --git a/h2inc.py b/h2inc.py old mode 100644 new mode 100755 index 1eff57e..df9bbd4 --- a/h2inc.py +++ b/h2inc.py @@ -1,87 +1,112 @@ -# You are free to use and/or change this code for -# your own needs. - -# Original code (c)2018 Jan Lerking -# Program to convert C-header (*.h) files to nasm include files (*.inc), -# for direct usage in assembly programming using nasm/yasm. - -import os -import sys -from h2inc_parser import parseline, parseparsed -import h2inc_gui - -tupline = [] -preproc = () -filelist = [] -folderlist = [] -cnt = 0 - -def sourcedir_filecnt(sourcedir): - ### Return the number of files, ending with '.h', in sourcedir - including subdirectories ### - cnt = 0 - global filelist - for folderName, subfolders, files in os.walk(sourcedir): - for file in files: - if file.lower().endswith('.h'): - cnt += 1 - filelist += [folderName+'/'+file] - print(folderName+'/'+file) - #print(filelist) - return cnt - -def sourcedir_foldercnt(sourcedir): - ### Return the number of folders, if it contains '*.h' files, in sourcedir - including subdirectories ### - global cnt - global folderlist - for folderName, subfolders, files in os.walk(sourcedir): - if subfolders: - for subfolder in subfolders: - sourcedir_foldercnt(subfolder) - tempf = [file for file in files if file.lower().endswith('.h')] - if tempf: - cnt = cnt+1 - #print(folderName) - folderlist += [folderName] - #print(folderlist) - #print(len(folderlist)) - return cnt - -def process_files(sourcedir, destdir, filevar): - #global folderlist - global filelist - global preproc - outfile = '' - cnt = 0 - for f in filelist: - current_cnt = 0 - inputfile = f - filehandle = open(f, 'r') - current_cnt += 1 - filevar = current_cnt - print(os.path.basename(f)) - currentfile_update(os.path.basename(f)) - for lines in filehandle: - print(lines) - preproc = preproc+tuple([parseline(lines)]) - filehandle.close() - current_cnt += 1 - filevar = current_cnt - for elements in preproc: - outfile = outfile+parseparsed(elements) - outputfile = os.path.splitext(inputfile)[0]+'.inc' - outputfile = str(outputfile).replace(str(sourcedir.get()), str(destdir.get())) - current_cnt += 1 - filevar = current_cnt - print(outputfile) - if not os.path.exists(os.path.dirname(outputfile)): - try: - os.makedirs(os.path.dirname(outputfile)) - except OSError as exc: # Guard against race condition - if exc.errno != errno.EEXIST: - raise - newfile = open(outputfile, "w") - newfile.write(outfile) - newfile.close() - current_cnt += 1 - filevar = current_cnt - cnt += 1 +#!/usr/bin/env python3.5 + +# You are free to use and/or change this code for +# your own needs. + +# Original code (c)2018 Jan Lerking +# Program to convert C-header (*.h) files to nasm include files (*.inc), +# for direct usage in assembly programming using nasm/yasm. + +import os +import sys +import multiprocessing as mp +import io +from h2inc_parser import parseline, parseparsed +import h2inc_gui + +tupline = [] +preproc = () +filelist = [] +folderlist = [] +cnt = 0 +sourcedir = '' +destdir = '' +num_cores = mp.cpu_count() + +def sourcedir_filecnt(sourcedir): + ### Return the number of files, ending with '.h', in sourcedir - including subdirectories ### + cnt = 0 + global filelist + for folderName, subfolders, files in os.walk(sourcedir): + for file in files: + if file.lower().endswith('.h'): + cnt += 1 + filelist += [folderName+'/'+file] + print(folderName+'/'+file) + #print(filelist) + return cnt + +def sourcedir_foldercnt(sourcedir): + ### Return the number of folders, if it contains '*.h' files, in sourcedir - including subdirectories ### + global cnt + global folderlist + for folderName, subfolders, files in os.walk(sourcedir): + if subfolders: + for subfolder in subfolders: + sourcedir_foldercnt(subfolder) + tempf = [file for file in files if file.lower().endswith('.h')] + if tempf: + cnt = cnt+1 + #print(folderName) + folderlist += [folderName] + #print(folderlist) + #print(len(folderlist)) + return cnt + +def process_files(gui, source, dest): + global sourcedir + global destdir + sourcedir = source + destdir = dest + pool = mp.Pool(processes=num_cores) + pool.map(process_file, filelist) + +def process_file(data): + outfile = '' + inputfile = data + encodings = ['utf-8', 'latin-1', 'windows-1250', 'windows-1252', 'ascii', + 'big5', 'big5hkscs', 'cp037', 'cp273', 'cp424', 'cp437', 'cp500', + 'cp720', 'cp737', 'cp775', 'cp850', 'cp852', 'cp855', 'cp856', + 'cp857', 'cp858', 'cp860', 'cp861', 'cp862', 'cp863', 'cp864', 'cp865', + 'cp866', 'cp869', 'cp874', 'cp875', 'cp932', 'cp949', 'cp950', 'cp1006', + 'cp1026', 'cp1125', 'cp1140', 'cp1250', 'cp1251', 'cp1252', 'cp1253', 'cp1254', + 'cp1255', 'cp1256', 'cp1257', 'cp1258', 'cp65001', 'euc-jp', 'euc-jis-2004', + 'euc-jisx0213', 'euc-kr', 'gb2312', 'gbk', 'gb18030', 'hz', 'iso2022-jp', + 'iso2022-jp-1', 'iso2022-jp-2', 'iso2022-jp-2004', 'iso2022-jp-3', + 'iso2022-jp-ext', 'iso2022-kr', 'iso8859-2', 'iso8859-3', 'iso8859-4', + 'iso8859-5', 'iso8859-6', 'iso8859-7', 'iso8859-8', 'iso8859-9', 'iso8859-10', + 'iso8859-11', 'iso8859-13', 'iso8859-14', 'iso8859-15', 'iso8859-16', 'johab', + 'koi8-r', 'koi8-t', 'koi8-u', 'kz1048', 'mac-cyrillic', 'mac-greek', + 'mac-iceland', 'mac-latin2', 'mac-roman', 'mac-turkish', 'ptcp154', + 'shift-jis', 'shift-jis-2004', 'shift-jisx0213', 'utf-32', 'utf-32-be', + 'utf-32-le', 'utf-16', 'utf-16-be', 'utf-16-le', 'utf-7', 'utf-8-sig'] + for e in encodings: + try: + fh = io.open(data, 'r', encoding=e) + fh.readlines() + fh.seek(0) + except UnicodeDecodeError: + print('got unicode error with %s , trying different encoding' % e) + else: + #print('opening the file with encoding: %s ' % e) + break + #print(os.path.basename(data)) + for lines in fh: + outfile = outfile+lines + fh.close() + outputfile = os.path.splitext(inputfile)[0]+'.inc' + outputfile = str(outputfile).replace(sourcedir, destdir) + #print(outputfile) + if not os.path.exists(os.path.dirname(outputfile)): + try: + os.makedirs(os.path.dirname(outputfile)) + except OSError as exc: # Guard against race condition + if exc.errno != errno.EEXIST: + raise + newfile = open(outputfile, "w") + newfile.write(outfile) + newfile.close() + +def async_process(num): + pool = mp.Pool(processes=num) + pool.map(process_file, filelist) \ No newline at end of file diff --git a/h2inc_gui.py b/h2inc_gui.py index b406bc3..68f04ac 100644 --- a/h2inc_gui.py +++ b/h2inc_gui.py @@ -27,7 +27,7 @@ class h2incGUI: self.filecnt = 0 self.infofolder = 'Number of folders: 0' self.infofile = 'Number of headers: 0' - self.currentfile = 'Current file: ' + #self.currentfile = 'Current file: ' self.totprogress = 'Total progress: ' self.sourcedir.set('Select source directory!') self.destdir.set('Select destination directory!') @@ -91,13 +91,13 @@ class h2incGUI: self.progressframe.grid(row=4, rowspan=2, column=0, columnspan=3, sticky=N+S+E+W, padx=5, pady=5) self.progressframe.grid_columnconfigure(1, weight=1) - self.cfilelabel = Label(self.progressframe, text=self.currentfile) - self.cfilelabel.grid(row=6, column=0, sticky=W, padx=5, pady=5) - self.cfilelabel.config(state=DISABLED) + #self.cfilelabel = Label(self.progressframe, text=self.currentfile) + #self.cfilelabel.grid(row=6, column=0, sticky=W, padx=5, pady=5) + #self.cfilelabel.config(state=DISABLED) - self.cfileprogress = ttk.Progressbar(self.progressframe, orient='horizontal', mode='determinate') - self.cfileprogress.grid(row=7, column=0, columnspan=3, sticky=N+S+E+W, padx=5, pady=5) - self.cfileprogress.config(variable=self.cfilevar, maximum=3) + #self.cfileprogress = ttk.Progressbar(self.progressframe, orient='horizontal', mode='determinate') + #self.cfileprogress.grid(row=7, column=0, columnspan=3, sticky=N+S+E+W, padx=5, pady=5) + #self.cfileprogress.config(variable=self.cfilevar, maximum=3) self.totallabel = Label(self.progressframe, text=self.totprogress) self.totallabel.grid(row=8, column=0, sticky=W, padx=5, pady=5) @@ -136,12 +136,13 @@ class h2incGUI: self.infofolders.config(state=NORMAL) self.infofiles.config(state=NORMAL) self.translate_button.config(state=NORMAL) - self.cfilelabel.config(state=NORMAL) + #self.cfilelabel.config(state=NORMAL) self.totallabel.config(state=NORMAL) def translate(self, destdir, sourcedir, addinc): doinc = addinc.get() dest = destdir.get() + source = sourcedir.get() if doinc == 'yes': dest = dest+'/include' print(os.path.exists(os.path.dirname(dest))) @@ -153,7 +154,7 @@ class h2incGUI: raise destdir.set(dest) print ('Destination directory: ', destdir.get()) - process_files(sourcedir, destdir, self.cfilevar) + process_files(self, source, dest) def cfileprogress_update(cnt): self.cfilevar = cnt diff --git a/old_h2inc/__pycache__/h2inc_gui_old.cpython-35.pyc b/old_h2inc/__pycache__/h2inc_gui_old.cpython-35.pyc new file mode 100644 index 0000000..ffc4e35 Binary files /dev/null and b/old_h2inc/__pycache__/h2inc_gui_old.cpython-35.pyc differ diff --git a/old_h2inc/__pycache__/h2inc_gui_old.cpython-36.pyc b/old_h2inc/__pycache__/h2inc_gui_old.cpython-36.pyc new file mode 100644 index 0000000..f92413a Binary files /dev/null and b/old_h2inc/__pycache__/h2inc_gui_old.cpython-36.pyc differ diff --git a/old_h2inc/__pycache__/h2inc_old.cpython-36.pyc b/old_h2inc/__pycache__/h2inc_old.cpython-36.pyc new file mode 100644 index 0000000..289d139 Binary files /dev/null and b/old_h2inc/__pycache__/h2inc_old.cpython-36.pyc differ diff --git a/old_h2inc/h2inc_gui_old.py b/old_h2inc/h2inc_gui_old.py new file mode 100644 index 0000000..97313c3 --- /dev/null +++ b/old_h2inc/h2inc_gui_old.py @@ -0,0 +1,173 @@ +# You are free to use and/or change this code for +# your own needs. + +# Original code (c)2018 Jan Lerking +# Program to convert C-header (*.h) files to nasm include files (*.inc), +# for direct usage in assembly programming using nasm/yasm. + +from tkinter import Tk, ttk, Label, Button, LabelFrame +from tkinter import filedialog, Entry, Checkbutton +from tkinter import Grid, StringVar, DoubleVar +from tkinter import N, E, S, W +from tkinter import DISABLED, NORMAL +from os import errno +import os +from os.path import expanduser +import platform +import sys +from h2inc_old import sourcedir_filecnt, sourcedir_foldercnt, process_files + +class h2incGUI: + def __init__(self, master): + self.sourcedir = StringVar() + self.destdir = StringVar() + self.addinc = StringVar() + self.cfilevar = DoubleVar() + self.totalvar = 0 + self.filecnt = 0 + self.infofolder = 'Number of folders: 0' + self.infofile = 'Number of headers: 0' + #self.currentfile = 'Current file: ' + self.totprogress = 'Total progress: ' + self.sourcedir.set('Select source directory!') + self.destdir.set('Select destination directory!') + + self.master = master + self.master.title('Translate C-header files to Nasm include files!') + self.master.grid_columnconfigure(1, weight=1) + + self.frame = LabelFrame(master, text='Select folders') + self.frame.grid(row=0, column=0, columnspan=3, sticky=N+S+E+W, padx=5, pady=5) + self.frame.grid_columnconfigure(1, weight=1) + + self.sourcelabel = Label(self.frame, text='Source: ') + self.sourcelabel.grid(row=0, column=0, sticky=E, padx=(5, 1), pady=5) + + self.sourceentry = Entry(self.frame, textvariable=self.sourcedir) + self.sourceentry.grid(row=0, column=1, sticky=E+W, pady=5) + + self.sourcedir_button = Button(self.frame, text="Source directory...", command= lambda: self.select_sourcedir(self.sourcedir)) + self.sourcedir_button.grid(row=0, column=2, sticky=W, padx=(3, 5), pady=5) + + self.destlabel = Label(self.frame, text='Destination: ') + self.destlabel.grid(row=1, column=0, sticky=E, padx=(5, 1), pady=5) + self.destlabel.config(state=DISABLED) + + self.destentry = Entry(self.frame, textvariable=self.destdir) + self.destentry.grid(row=1, column=1, sticky=E+W, pady=5) + self.destentry.config(state=DISABLED) + + self.destdir_button = Button(self.frame, text="Destination directory...", command= lambda: self.select_destdir(self.destdir)) + self.destdir_button.grid(row=1, column=2, sticky=W, padx=(3, 5), pady=5) + self.destdir_button.config(state=DISABLED) + + self.incchkbox = Checkbutton(self.frame, text='Create "include" folder if it does not exist.', variable=self.addinc, onvalue='yes', offvalue='no') + self.incchkbox.grid(row=2, column=0, columnspan=2, sticky=W, padx=5, pady=5) + self.incchkbox.config(state=DISABLED) + self.addinc.set('yes') + + self.transframe = LabelFrame(master, text='Translation') + self.transframe.grid(row=1, rowspan=2, column=0, columnspan=3, sticky=N+S+E+W, padx=5, pady=5) + self.transframe.grid_columnconfigure(1, weight=1) + self.transframe.grid_rowconfigure(1, weight=1) + + self.infoframe = LabelFrame(self.transframe, text='Source information') + self.infoframe.grid(row=1, rowspan=2, column=0, columnspan=3, sticky=N+S+E+W, padx=5, pady=5) + self.infoframe.grid_columnconfigure(1, weight=1) + + self.infofolders = Label(self.infoframe, text=self.infofolder) + self.infofolders.grid(row=0, column=0, sticky=W, padx=5, pady=5) + self.infofolders.config(state=DISABLED) + + self.infofiles = Label(self.infoframe, text=self.infofile) + self.infofiles.grid(row=1, column=0, sticky=W, padx=5, pady=5) + self.infofiles.config(state=DISABLED) + + self.translate_button = Button(self.transframe, text="Translate!", command= lambda: self.translate(self.destdir, self.sourcedir, self.addinc)) + self.translate_button.grid(row=3, column=0, sticky=W, padx=5, pady=5) + self.translate_button.config(state=DISABLED) + + self.progressframe = LabelFrame(self.transframe, text='Progress') + self.progressframe.grid(row=4, rowspan=2, column=0, columnspan=3, sticky=N+S+E+W, padx=5, pady=5) + self.progressframe.grid_columnconfigure(1, weight=1) + + #self.cfilelabel = Label(self.progressframe, text=self.currentfile) + #self.cfilelabel.grid(row=6, column=0, sticky=W, padx=5, pady=5) + #self.cfilelabel.config(state=DISABLED) + + #self.cfileprogress = ttk.Progressbar(self.progressframe, orient='horizontal', mode='determinate') + #self.cfileprogress.grid(row=7, column=0, columnspan=3, sticky=N+S+E+W, padx=5, pady=5) + #self.cfileprogress.config(variable=self.cfilevar, maximum=3) + + self.totallabel = Label(self.progressframe, text=self.totprogress) + self.totallabel.grid(row=8, column=0, sticky=W, padx=5, pady=5) + self.totallabel.config(state=DISABLED) + + self.totalprogress = ttk.Progressbar(self.progressframe, orient='horizontal', mode='determinate') + self.totalprogress.grid(row=9, column=0, columnspan=3, sticky=N+S+E+W, padx=5, pady=5) + self.totalprogress.config(variable=self.totalvar, maximum=self.filecnt) + + def select_sourcedir(self, sourcedir): + root.directory = os.path.abspath(filedialog.askdirectory()) + if root.directory: + sourcedir.set(root.directory) + filecnt = sourcedir_filecnt(root.directory) + if filecnt > 0: + tempstr = 'Number of headers: '+str(filecnt) + temptot = 'Total progress: 0 of '+str(filecnt) + print ('Source directory: ', sourcedir.get()) + self.destlabel.config(state=NORMAL) + self.destentry.config(state=NORMAL) + self.destdir_button.config(state=NORMAL) + self.infofiles.config(text=tempstr) + self.totallabel.config(text=temptot) + self.filecnt = filecnt + foldercnt = sourcedir_foldercnt(root.directory) + if foldercnt > 0: + tempstr = 'Number of folders: '+str(foldercnt) + self.infofolders.config(text=tempstr) + + def select_destdir(self, destdir): + root.directory = filedialog.askdirectory() + if root.directory: + destdir.set(root.directory) + print ('Destination directory: ', destdir.get()) + self.incchkbox.config(state=NORMAL) + self.infofolders.config(state=NORMAL) + self.infofiles.config(state=NORMAL) + self.translate_button.config(state=NORMAL) + #self.cfilelabel.config(state=NORMAL) + self.totallabel.config(state=NORMAL) + + def translate(self, destdir, sourcedir, addinc): + doinc = addinc.get() + dest = destdir.get() + source = sourcedir.get() + if doinc == 'yes': + dest = dest+'/include' + print(os.path.exists(os.path.dirname(dest))) + if not os.path.exists(os.path.dirname(dest)): + try: + os.makedirs(os.path.dirname(dest)) + except OSError as exc: # Guard against race condition + if exc.errno != errno.EEXIST: + raise + destdir.set(dest) + print ('Destination directory: ', destdir.get()) + process_files(self, source, dest) + + def cfileprogress_update(cnt): + self.cfilevar = cnt + + def currentfile_update(current): + self.currentfile = 'Current file: '+current + +root = Tk() +root.update() +#root.minsize(350, 210) +#width = (root.winfo_screenwidth()/2)-(350/2) +#height = (root.winfo_screenheight()/2)-(210/2) +#root.geometry('+%d+%d' % (width, height)) +root.resizable(False, False) +h2incgui = h2incGUI(root) +root.mainloop() \ No newline at end of file diff --git a/old_h2inc/h2inc_old.py b/old_h2inc/h2inc_old.py new file mode 100644 index 0000000..233a495 --- /dev/null +++ b/old_h2inc/h2inc_old.py @@ -0,0 +1,112 @@ +#!/usr/bin/env python3.5 + +# You are free to use and/or change this code for +# your own needs. + +# Original code (c)2018 Jan Lerking +# Program to convert C-header (*.h) files to nasm include files (*.inc), +# for direct usage in assembly programming using nasm/yasm. + +import os +import sys +import multiprocessing as mp +import io +from h2inc_parser import parseline, parseparsed +import h2inc_gui_old + +tupline = [] +preproc = () +filelist = [] +folderlist = [] +cnt = 0 +sourcedir = '' +destdir = '' +num_cores = mp.cpu_count() + +def sourcedir_filecnt(sourcedir): + ### Return the number of files, ending with '.h', in sourcedir - including subdirectories ### + cnt = 0 + global filelist + for folderName, subfolders, files in os.walk(sourcedir): + for file in files: + if file.lower().endswith('.h'): + cnt += 1 + filelist += [folderName+'/'+file] + print(folderName+'/'+file) + #print(filelist) + return cnt + +def sourcedir_foldercnt(sourcedir): + ### Return the number of folders, if it contains '*.h' files, in sourcedir - including subdirectories ### + global cnt + global folderlist + for folderName, subfolders, files in os.walk(sourcedir): + if subfolders: + for subfolder in subfolders: + sourcedir_foldercnt(subfolder) + tempf = [file for file in files if file.lower().endswith('.h')] + if tempf: + cnt = cnt+1 + #print(folderName) + folderlist += [folderName] + #print(folderlist) + #print(len(folderlist)) + return cnt + +def process_files(gui, source, dest): + global sourcedir + global destdir + sourcedir = source + destdir = dest + pool = mp.Pool(processes=num_cores) + pool.map(process_file, filelist) + +def process_file(data): + outfile = '' + inputfile = data + encodings = ['utf-8', 'latin-1', 'windows-1250', 'windows-1252', 'ascii', + 'big5', 'big5hkscs', 'cp037', 'cp273', 'cp424', 'cp437', 'cp500', + 'cp720', 'cp737', 'cp775', 'cp850', 'cp852', 'cp855', 'cp856', + 'cp857', 'cp858', 'cp860', 'cp861', 'cp862', 'cp863', 'cp864', 'cp865', + 'cp866', 'cp869', 'cp874', 'cp875', 'cp932', 'cp949', 'cp950', 'cp1006', + 'cp1026', 'cp1125', 'cp1140', 'cp1250', 'cp1251', 'cp1252', 'cp1253', 'cp1254', + 'cp1255', 'cp1256', 'cp1257', 'cp1258', 'cp65001', 'euc-jp', 'euc-jis-2004', + 'euc-jisx0213', 'euc-kr', 'gb2312', 'gbk', 'gb18030', 'hz', 'iso2022-jp', + 'iso2022-jp-1', 'iso2022-jp-2', 'iso2022-jp-2004', 'iso2022-jp-3', + 'iso2022-jp-ext', 'iso2022-kr', 'iso8859-2', 'iso8859-3', 'iso8859-4', + 'iso8859-5', 'iso8859-6', 'iso8859-7', 'iso8859-8', 'iso8859-9', 'iso8859-10', + 'iso8859-11', 'iso8859-13', 'iso8859-14', 'iso8859-15', 'iso8859-16', 'johab', + 'koi8-r', 'koi8-t', 'koi8-u', 'kz1048', 'mac-cyrillic', 'mac-greek', + 'mac-iceland', 'mac-latin2', 'mac-roman', 'mac-turkish', 'ptcp154', + 'shift-jis', 'shift-jis-2004', 'shift-jisx0213', 'utf-32', 'utf-32-be', + 'utf-32-le', 'utf-16', 'utf-16-be', 'utf-16-le', 'utf-7', 'utf-8-sig'] + for e in encodings: + try: + fh = io.open(data, 'r', encoding=e) + fh.readlines() + fh.seek(0) + except UnicodeDecodeError: + print('got unicode error with %s , trying different encoding' % e) + else: + #print('opening the file with encoding: %s ' % e) + break + #print(os.path.basename(data)) + for lines in fh: + outfile = outfile+lines + fh.close() + outputfile = os.path.splitext(inputfile)[0]+'.inc' + outputfile = str(outputfile).replace(sourcedir, destdir) + #print(outputfile) + if not os.path.exists(os.path.dirname(outputfile)): + try: + os.makedirs(os.path.dirname(outputfile)) + except OSError as exc: # Guard against race condition + if exc.errno != errno.EEXIST: + raise + newfile = open(outputfile, "w") + newfile.write(outfile) + newfile.close() + +def async_process(num): + pool = mp.Pool(processes=num) + pool.map(process_file, filelist) \ No newline at end of file diff --git a/multiprocessing_test.py b/testing solutions/multiprocessing_test.py similarity index 100% rename from multiprocessing_test.py rename to testing solutions/multiprocessing_test.py diff --git a/testing solutions/thread_priority_queue.py b/testing solutions/thread_priority_queue.py new file mode 100644 index 0000000..08daa57 --- /dev/null +++ b/testing solutions/thread_priority_queue.py @@ -0,0 +1,59 @@ +from Queue import Queue +import threading +import time + +exitFlag = 0 + +class myThread (threading.Thread): + def __init__(self, threadID, name, q): + threading.Thread.__init__(self) + self.threadID = threadID + self.name = name + self.q = q + def run(self): + print ("Starting " + self.name) + process_data(self.name, self.q) + print ("Exiting " + self.name) + +def process_data(threadName, q): + while not exitFlag: + queueLock.acquire() + if not workQueue.empty(): + data = q.get() + queueLock.release() + print ("%s processing %s" % (threadName, data)) + else: + queueLock.release() + time.sleep(1) + +threadList = ["Thread-1", "Thread-2", "Thread-3"] +nameList = ["One", "Two", "Three", "Four", "Five"] +queueLock = threading.Lock() +workQueue = Queue.Queue(10) +threads = [] +threadID = 1 + +# Create new threads +for tName in threadList: + thread = myThread(threadID, tName, workQueue) + thread.start() + threads.append(thread) + threadID += 1 + +# Fill the queue +queueLock.acquire() +for word in nameList: + workQueue.put(word) +queueLock.release() + +# Wait for queue to empty +while not workQueue.empty(): + pass + +# Notify threads it's time to exit +exitFlag = 1 + +# Wait for all threads to complete +for t in threads: + t.join() +print ("Exiting Main Thread") \ No newline at end of file