Re: [Tutor] Validating String contains IP address
On 03/31/2017 06:44 PM, Alex Kleider wrote: > On 2017-03-31 16:35, Ed Manning wrote: >> What's the best way to validate a string contains a IP address >> Sent from my iPad >> ___ >> Tutor maillist - Tutor@python.org >> To unsubscribe or change subscription options: >> https://mail.python.org/mailman/listinfo/tutor > > The re module perhaps? > > How about: > > import re > > ip_regex = r""" > [12]?\d?\d[.] > [12]?\d?\d[.] > [12]?\d?\d[.] > [12]?\d?\d > """ > > ip_pattern = re.compile(ip_regex, re.VERBOSE) > > # then test for ip_pattern.search(source).group(): > res = ip_pattern.search(source).group() > if res: > print("IP is {}".format(res)) > else: > print("Source doesn't contain an IP address") > > # This assumes that an address would never be written as follows: > 076.191.211.205 This assumes "an IP address" is the four dotted numbers characteristic of IPv4. These days that's a bad assumption unless you're absolutely sure an IPv6 address can never appear. Can you? ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Validating String contains IP address
On 2017-03-31 18:01, Mats Wichmann wrote: On 03/31/2017 06:44 PM, Alex Kleider wrote: On 2017-03-31 16:35, Ed Manning wrote: What's the best way to validate a string contains a IP address Sent from my iPad ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor The re module perhaps? This assumes "an IP address" is the four dotted numbers characteristic of IPv4. These days that's a bad assumption unless you're absolutely sure an IPv6 address can never appear. Can you? Good point! I hadn't considered IPV6 and didn't know about the ipaddress module. Live and learn. ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Validating String contains IP address
On 01/04/17 17:29, Alex Kleider wrote: > Good point! I hadn't considered IPV6 and didn't know about the ipaddress > module. Me too, on both counts. Actually I should have known about ipaddress because this question has come up before but I'd forgotten... :-( And as for forgetting IP6 - as an ex telco engineer - sheesh! mea mucho culpa! -- Alan G Author of the Learn to Program web site http://www.alan-g.me.uk/ http://www.amazon.com/author/alan_gauld Follow my photo-blog on Flickr at: http://www.flickr.com/photos/alangauldphotos ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Function question
Many thanks! On 25-03-17 11:17, Alan Gauld via Tutor wrote: On 25/03/17 10:01, Peter O'Doherty wrote: def myFunc(num): for i in range(num): print(i) print(myFunc(4)) 0 1 2 3 None #why None here? Because your function does not have an explicit return value so Python returns its default value - None. So the print() inside the function body prints the 0-3 values then the function terminates and returns the (default) None to your top level print. def myFunc(num): for i in range(num): return i print(myFunc(4)) 0 #why just 0? Because return always returns from the function immediately. So you call the function, it enters the loop, sees the return for the first element and exits. The print() then prints that returned value. The preferred method to do what I think you were expecting is to build a list: def anotherFunction(num): result = [] for i in range(num): result.append(i) return result Which is more concisely written using a list comprehension but that would hide the general point - that you should accumulate results in a collection if you want to return more than a single value. To print the result you would typically use the string.join() method: print(' '.join(anotherFunction(4)) //= // Peter O'Doherty // http://www.peterodoherty.net // m...@peterodoherty.net //= ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Function question
On 25-03-17 11:17, Alan Gauld via Tutor wrote: method: print(' '.join(anotherFunction(4)) Many thanks! ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] python gtk serial loop thread readind data close
On 30Mar2017 12:40, Alexandru Achim wrote: Dear users, I had a problem regarding Threads in python and Gtk3. I want to stop a while loop in Gtk , a loop starded with a thread. I want to control a delay timer laser board with give me ,when I send a command by serial connection, give back continuous status values ; but I want to stop this loop , to modify parameters and start the loop again. A part of my code is here : import serial import threading import gi gi.require_version('Gtk', '3.0') from gi.repository import Gtk from gi.repository import GObject,Pango, GLib from threading import Thread GObject.threads_init(). #START BUTTON def start(self,button_start): Thread(target=self.start_on,args=(self.button_start)) You don't seem to start the Thread here. def start_on(self,widget,data=None): if ser.isOpen(): cmd = 's' ser.write(cmd.encode('ascii')+'\r\n') while True: try: values = ser.readline() self.label_result.set_text(values) except: pass #STOP BUTTON def stop(self,button_stop): Thread(target=self.stops,args=(button_stop)).start() def stops(self,widget,data=None): if ser.isOpen(): try: cmd = 'f' ser.write(cmd.encode('ascii')+'\r\n') status = ser.readline() self.label_result.set_text(status) except: pass ...win=sincrolaser() win.show_all() Gtk.main() But the problem is when I send STOP button program freeze , probably the loop from the start is looping forever. How do I stop the start thread? Is there a posibility to give a thread flag or identity to find him and close? It is easy enough to keep the identities of threads. Example: def start(self, button,start): self.start_thread = Thread(target=...) self.start_thread.start() But to stop a thread you pretty much need to set a flag and have the thread check it periodicly. Example: def start(self, button,start): self.start_thread = Thread(target=...) self.start_run = True self.start_thread.start() def start_one(self, widget, data=None): ... while self.start_run: try: ... and to stop it one goes: self.start_run = False Next time around the loop the thread checks the flag and quits the while loop. Another concern I have with your code is that nothing prevents _both_ the stop and start threads from running at the same time. I Imagine that calling ser.readline from two threads at once leads to unpredictable and possible insane behaviour. And having both threads writing to the serial line at one may write nonsense to the other end, etc. I recommand you take a mutex around your worker threads, so that only one can be active at once. Example: from threading import Lock # wherever your setup stuff happens; you need to allocate the Lock def __init__(self, ...): self.ser_lock = Lock() def start_one(self, widget, data=None): with self.ser_lock: ... while start_run: try: .. and likewise in stops: def stop(self,button_stop): self.start_run = False# ask "start" tyhread to terminate self.stop_thread = Thread(target=self.stops,args=(button_stop)) self.stop_thread.start() def stops(self,widget,data=None): with self.ser_lock: if ser.isOpen(): try: ... You can see that neither thread will try to user the serial device until it holds the lock, avoiding conflict. The stop button active sets the "start_run" flag variable to False, causing the start thread to exit when it notices. Hopefully this will give you more control over your program's behaviour. A few helpful print() calls scattered throughout should show you what is going on, too. Note that it would also be prudent to prevent more than one start thread running, and so forth. Cheers, Cameron Simpson ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor