I'm trying to edit a library in python (perfmon)
- the file is session.py
I want to add a module that can record some readings from a USB port.
This is also my first trial with classes in python
from perfmon import *
import os
import sys
import serial
# Common base class
class Session:
def __init__(self, events):
self.system = System()
self.event_names = events
self.events = []
self.fds = []
for e in events:
err, encoding = pfm_get_perf_event_encoding(e, PFM_PLM0 | PFM_PLM3,
None, None)
self.events.append(encoding)
self.USB = serial.Serial("/dev/ttyUSB0",115200)
def __del__(self):
pass
def read(self, fd):
# TODO: determine counter width
return os.read(fd, 8)
class PerThreadSession(Session):
def __init__(self, pid, events):
self.pid = pid
Session.__init__(self, events)
def __del__(self):
Session.__del__(self)
def start(self):
for e in self.events:
self.fds.append(perf_event_open(e, self.pid, -1, -1, 0))
def read(self, i):
return Session.read(self, self.fds[i])
class USB(Session):
def __init__(self):
return self.USB
This is the following error I am getting:
Python 2.7.3 (default, Apr 14 2012, 08:58:41) [GCC] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import perfmon
>>> test = perfmon.USB()
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
AttributeError: 'module' object has no attribute 'USB'
What is the mistake I am doing? I understood that self acts as a constructor in python, Can I return a value there?
as I understand in Your case answer is
But this way You move is wrong. You should create your own module in your workspace/project dir. Import session module and Create class there
You can not return anything (besides None) from a class constructor.
otherwise one will get