Reputation: 10
Good
- Group:
- Members
- Active Posts:
- 124 (0.11 per day)
- Most Active In:
- General Discussion (26 posts)
- Joined:
- 21-March 10
- Profile Views:
- 2828
- Last Active:
May 24 2013 10:15 PM- Currently:
- Offline
My Information
- Age:
- Age Unknown
- Birthday:
- January 5
- Gender:
-
Male
- Location:
- Wouldn't you like to know?
- Expertise:
-
HTML
CSS
Contact Information
- E-mail:
- Click here to e-mail me
- Website URL:
-
http://guysthatcode.com
Topics I've Started
-
PersistantTCP
22 August 2012 - 04:28 PM
Here'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 -
**kwargs with ineritance?
17 August 2012 - 10:18 AM
While 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 subclassSuperClass.__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. -
Python Package
16 August 2012 - 12:56 PM
So 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. -
Functions and parameters
14 August 2012 - 05:53 PM
So 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() -
Different daemon implementations
13 August 2012 - 04:26 PM
I'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?






Cartoon Clouds
Mountains
Sunrise
Clouds
Green Clouds
None
Find My Content
Display name history

Help
Comments
Sole_Wolf
08 Jan 2012 - 02:56lobabob
11 Sep 2011 - 18:33Sole_Wolf
11 Sep 2011 - 13:20lobabob
09 Sep 2011 - 21:49Sole_Wolf
23 Aug 2011 - 14:35