Sometimes. And telling when it’s stupid is important to being a good coder, in my opinion.
Maybe I’m wrong. I don’t have a degree yet. But I know that when I write a Python script that I want to rotate the screen on my tablet, I’m not worrying about extensibility or modularity, I just want a small procedural script that does this, this, and this, and then finishes. The faster the better, both in writing the script and in the script’s execution.
So, in beginning work on JORSnix, I decided I’d get my tablet working perfectly first, as a system to model it on. Part of this made rewriting my tablet.rb script that rotated my tablet between landscape and portrait mode in Python. I spent about 20 minutes on it, not counting the time I spent learning that the os module was deprecated and thus learning how to use the subprocess module.
Anyway, the finished product is this:
#!/usr/bin/python
from subprocess import *
import re
p1 = Popen(["xrandr"],stdout=PIPE)
p2 = Popen(["grep", "current"], stdin=p1.stdout, stdout=PIPE)
output = p2.communicate()[0]
current = output[output.find("current "):output.find(", max")]
pattern = re.compile('[0-9]+')
resolution = pattern.findall(current)
if resolution[0] > resolution[1] : #landscape
Popen("./xrotate"+" 3", shell=True)
Popen("cellwriter &",shell=True)
Popen("easystroke &",shell=True)
else : #portrait
Popen("./xrotate"+" 0", shell=True)
Popen("pkill cellwriter && pkill easystroke", shell=True)
As you can see, it's pretty simple and straightforward. It's no piece of art, but it does what it needs to do. No more, no less. And it will work on any single-monitored tablet, no matter the resolution, for swapping the screen from portrait to landscape and back.
If it needs to be turned into a couple functions so I can use it in a class, that would work. But that's not what the plan is, at least not yet. But I was told by a friend on IRC that it was unreadable, and wrong simply because my code isn't "modular and extensible" without first examining why it would need to be extended upon. He gave this as the "correct version:"
#!/usr/bin/python
from subprocess import Popen,PIPE
def bash(c):
if c.startswith('cd '):
chdir(''.join(list(c)[3:]))
else:
return Popen(c, shell=True, stdout=PIPE).stdout.read().strip()
class Display():
def getCurrentResolution(self):
res = bash("xrandr")
res = res[res.find("current ")+8:res.find(", max")].replace(' ','')
return (res[:res.find('x')],res[res.find('x')+1:])
def rotate(self):
res = getCurrentResolution()
if res[0] > res[1]: # Landscape Mode
self.xrotate(3);
bash('cellwriter &')
bash('easystroke &')
else: # Portrait Mode
self.xrotate(0)
bash('killall cellwriter')
bash('killall easystroke')
def xrotate(self, mode):
# unknownmosquito can fill it in from here
class Wacom(Display):
def getStylusMode(self):
return bash('xsetwacom get stylus Mode')
display = Wacom()
display.rotate()
Besides going into the fact that the "better version" would no longer use regular expressions (and thus would be less resilient to oddly shaped displays), it aliases Popen to bash for "readability." Overall, being accused of using procedural programming for a systems script in a language like Python (which allows you to do this for a reason), and to have this given to me as a "correction," just rubs me the wrong way.
Especially when I'm told that it needs to be done like this to make it more "readable."
Object Oriented Programming is wonderful. Unless using it is just wasting time.
Note: If it becomes necessary to integrate this script into a larger part, obviously it will get OOP'd. But for now, it'll remain a script.