- Active Posts:
- 124 (0.11 per day)
- Most Active In:
- General Discussion (26 posts)
- 21-March 10
- Profile Views:
- Last Active:
- May 24 2013 10:15 PM
- Age Unknown
- January 5
- Wouldn't you like to know?
Topics I've Started
22 August 2012 - 04:28 PMHere's a PersistantTCP class I wrote for another project I was working on. It seems like something people might end up needing to use often, so here's my code. Feel free to leave constructive criticism.
#!/usr/bin/env python3.2 import socket import errno from time import sleep from json import dumps, loads class PersistentTCP: """ A new protocol on top of TCP; it queries existing connections and checks if they're still alive or not and starts a new socket if dead """ def __init__(self, addr, timeout = 5): self.addr = addr self.timeout = timeout self.start() def send(self, data): try: self.sock.sendall(bytes(dumps(data), 'utf-8')) except socket.error as e: if e.errno is errno.EPIPE: self.end() else: self.end('full') self.start() self.send(data) def get(self): try: return loads(sock.recv('utf-8')) # Might need str()? except socket.error as e: if e.errno is errno.EPIPE: self.end() else: self.end('full') self.end() self.start() self.get() def start(self): try: self.sock = socket.create_connection(self.addr,self.timeout) self.sock.setblocking(False) # Set socket to non-blocking connected = True except: connected = False while not connected: try: self.sock = socket.create_connection(self.addr,self.timeout) self.sock.setblocking(False) # Set socket to non-blocking connected = True except: pass sleep(2) # Time is in seconds def end(self, mode = None): if self.sock: if mode == 'full': self.sock.shutdown(2) self.sock.close()
The end method is set up so that you can extend it for different end types depending on your needs
17 August 2012 - 10:18 AMWhile trawling google for the magic that is python, I came across a question that led to another question.
Question 1 (has to do with inheritance):
class SuperClass: def __init__(self, key1 = value, key2 = value, key3 = value): self.var1 = key1 self.var2 = key2 self.var3 = key3 class SubClass(SuperClass): def __init__(self, wthdoiputhere): SuperClass.__init__(self, wthdoiputhere)
Is there someway I can use **kwargs in the subclass __init__ so that I can choose which of the key1, key2, key3 variables to pass through?
Edit: I think I found the solution to question 1 here.
Question 2 (came about while searching for an answer to question 1):
Should I even have this line in my subclass
SuperClass.__init__(self, wthdoiputhere)or should I be using this super keyword I've been hearing about every time I google ineritance?
Edit: Solution to question 2 is here (also applies to question 1).
I'll leave this post here in-case it'll be useful for someone else.
16 August 2012 - 12:56 PMSo I've recently moved to sublime text 2 and it seems pretty awesome so far. The only problem I've found with it though, is that it doesn't have a python 3 package. I googled it and apparently no one has a working python 3 package up for download. I was wondering if anyone here might have made one themselves (since several people here use sublime text 2) that they would be willing to let me use?
Edit: I'm not sure exactly which forum this should be in, so I put it here because it has to do with python. Mods, please move it to where it's supposed to be if this spot doesn't quite work out.
14 August 2012 - 05:53 PMSo this is a pretty simple question question overall, but I have no idea what to type into google to get the answer.
My question is, when would you do:
#!/usr/bin/env python3.2 test = 'asdf' def blah(string): print(string) blah(test)
and when would you do:
#!/usr/bin/env python3.2 test = 'asdf' def blah(): print(test) blah()
13 August 2012 - 04:26 PMI'm relatively new to python and I've only skimmed the surface of programming so far, which is why I'm confused about daemons. I generally know what they do, but I'm not sure what the best way to implement them is in python. I found this link which demonstrates how to create daemons in python. However, I was wondering if this
#!/usr/bin/env python3.2 import threading class Update(threading.Thread): def __init__(self): threading.Thread.__init__(self) def run(self): pass #something you want to run in the background continous = Update continous.daemon = True continous.start()
would be just as effective?