Error when defining python class (threading) - python

class Downloader(threading.Thread):
def __init__(self, priority_level, output_function):
self.IDs = self.load_IDs(priority_level)
self.sleep_interval = self.gen_sleep(priority_level)
self.output = output_function
self.name = '[Downloader::%s]'%(str(priority_level))
self.output('[Downloader] New downloader created (prio: %s)!'%(str(priority_level))
def load_IDs(self, prio):
filename = 'id_prio%s.data'%str(prio)
ID_file = open(filename, 'r')
ID_data = ID_file.read()
ID_file.close()
temp = open(filename, 'w')
temp.write('\n')
temp.close()
IDs = [line.split(':') for line in ID_data.split('\n') if ID != '']
return IDs
[MORE CODE...]
For some reason, I get the following error:
File "pastebin_rip_2.py", line 40
def load_IDs(self, prio):
^
SyntaxError: invalid syntax
What am I doing wrong? for a moment I considered the issue might have been the placing of init, because when I moved it to the end of the Downloader class it worked fine (which doesn't make sense?). Well, Downloader did, anyway. Instead I got a message complaining about the class after Downloader.
I really don't see what's wrong. Help?
(Entire code: http://snipt.org/xkky)

I think you're missing a closing paren at this line:
self.output('[Downloader] New downloader created (prio: %s)!'%(str(priority_level))

Related

How to convert a normal function into a function inside a class?

I'm trying to organize my code I already have by implementing classes and execute methods on classes instantiations. I have put some hours into figuring out how to use classes, but still haven't figured it out. Could someone help me?
This is the original code:
def readSignalAcquisitionData(fileName):
f = open(fileName, 'r')
# dummy read
f.readline()
timeStamps = []
dataInput = []
for ln in f:
# parse info
timeStr, dataStr = ln.split(',')
timeStamps.append(float(timeStr))
dataInput.append(float(dataStr))
f.close()
return timeStamps, dataInput
And this is what I currently have:
class SignalDataIOUnit:
def __init__(self, fileName):
self.fileName = fileName
def readSignalAcquisitionData(self):
f = open(self.fileName, 'r')
self.timeStamps = []
self.dataInput = []
for ln in f:
# parse info
self.timeStr, self.dataStr = ln.split(',')
self.timeStamps.append(float(self.timeStr))
self.dataInput.append(float(self.dataStr))
f.close()
return self.timeStamps, self.dataInput
def writeFilteredData(self, fileName, timeStamps, dataOut):
pass
fileName="LabsWeek03_inputData.csv"
timeStamps, dataInput = SignalDataIOUnit.readSignalAcquisitionData(fileName)
print(timeStamps)
When I try running it through the terminal I get these error messages:
Traceback (most recent call last):
File "SignalDataEvaluationUnit_OOP.py", line 26, in <module>
timeStamps, dataInput = SignalDataIOUnit.readSignalAcquisitionData(fileName)
File "SignalDataEvaluationUnit_OOP.py", line 7, in readSignalAcquisitionData
f = open(self.fileName, 'r')
AttributeError: 'str' object has no attribute 'fileName'
As #decezeā™¦ says in comment, you haven't instantiated the class SignalDataIOUnit, that's why it doesn't work.
To make it work, you have 2 choices:
Instantiating SignalDataIOUnit object and call the method readSignalAcquisitionData:
timeStamps, dataInput = SignalDataIOUnit(fileName).readSignalAcquisitionData()
Use Python's #staticmethod decorator:
class SignalDataIOUnit:
def __init__(self, fileName):
self.fileName = fileName
#staticmethod
def readSignalAcquisitionData(fileName):
...
then just call it as usual
timeStamps, dataInput = SignalDataIOUnit.readSignalAcquisitionData(fileName)
yes, you should use like this
fileName="LabsWeek03_inputData.csv"
timeStamps, dataInput = SignalDataIOUnit(fileName).readSignalAcquisitionData()
print(timeStamps)

how to put a valid filepath as a function parameter in python

I am trying to write a save/load function for a text based game in python 3.5.1, and I found some helpful code on this site, unfortunately, both functions just need a file path as one of the parameters. I am learning python as I go and am relatively new to both this site and coding in general, so help on how to enter in a valid file path (and examples of what a valid file path is in this case!) would be greatly appreciated (once again, I'm like Jon Snow). The relevant methods are posted below:
def __init__(self, name):
self.name = name
self.maxHealth = 100
self.health = self.maxHealth
self.baseAttack = 10
self.credits = 10
self.augs = 0
self.weap = ["Basic Nanite Pack"]
self.currWeap = ["Basic Nanite Pack"]
global playerFile
playerFile = {"name":self.name, "health":self.maxHealth, "maxHealth":self.health, "baseAttack":self.baseAttack,
"weapon":self.weap, "currWeap":self.currWeap, "credits":self.credits, "augs":self.augs}
def saveGame(playerFile, filepath):
with open(filepath, 'w') as outfile:
for name,num in playerFile.items():
outfile.write("{};{};\n".format(num, name))
import csv
def loadGame(filepath):
with open(filepath) as infile:
# purposeful misspell for variable name
playerFil = dict((v,k) for v,k,_ in csv.reader(infile, delimiter=';'))
return playerFil
A file path is a path to a file from start to end.
e.g c:\users\John\my_document.txt
This is a valid file path and you should normally pass it like this:
r'c:\users\John\my_document.txt'
Note that if you dont give a path for an actual file
e.g c:\users\John
Python won't be able to open any file since that would be a directory path.

Is it possible to print a next line in a code?

Is it possible to make a method, which prints a next line of a code?
def print_next_line():
sth
import fxx
print 'XXX'
print_next_line()
file.split('/')
....
>>> 'XXX'
>>> 'file.split('/')'
I was thinking that It could be somewhere in the stack, but I'm not sure because it is next, not previous line.
Straight approach. I use inspect module to determine file and line where print_next_line was called. Later I read the file to find next string. You might want to add some error handling here (what if there is no next line in a file? and so on)
def print_next_line():
def get_line(f, lineno):
with open(f) as fp:
lines = fp.readlines()
return lines[lineno-1]
import inspect
callerframerecord = inspect.stack()[1]
frame = callerframerecord[0]
info = inspect.getframeinfo(frame)
line_ = info.lineno
file_ = info.filename
print get_line(file_, line_ + 1)
print 'XXX'
a = 1
print_next_line()
b = a*2
All you need is a profiling tool or just a debugger.
Use Python's inspect module:
import inspect
def print_next_line():
lineno = inspect.currentframe().f_back.f_lineno
with open(__file__) as f:
print(f.readlines()[lineno].rstrip())
Well you could open() your .py file and iterate to find specific line, then print it.

python: prevent creating empty files

For a bulk task I create a couple of instances of the ProgressLog object which will each create an empty log-file no matter if there actually will be any errors. what is the best way to prevent this?
class ProgressLog(object):
"""Write a message to log + progress indicator.
"""
total = 0
def __init__(self, name):
source_path, file_name = os.path.split(name)
self.name = file_name
self.source_path = source_path
self.log_dir_name = r'log'
self.make_log_dir(self.source_path, self.log_dir_name)
self.reset()
log_file = self._logfilename()
try:
self.f = open(log_file, 'w')
print('\n***logging errors to {0}***\n'.format(log_file))
except IOError, err:
msg = 'Cannot open logfile {0}. Traceback is: {1}'.format(
log_file, err)
raise msg
def _logfilename(self):
## hms_ddmmyyyy format
log_name = r'{1}_{0}{2}_errors.csv'.format(
time.strftime("%I%M%S"),
time.strftime("%d%m%Y"),
self.name)
return os.path.join(self.source_path, self.log_dir_name, log_name)
There is no "magical" way to do it, you simply need to refactor the code to open the log file only on first actual call to log.
To achieve this, extract the part of __init__ that opens the log file into a separate _open_log method. In __init__ initialize self.f to None. Then, your actual logging method can begin with:
if self.f is None:
self._open_log()

TypeError: '_sre.SRE_Match' object has no attribute '__getitem__'

I'm currently getting this error and don't know what is means. Its a scrapy python project, this is the error I'm seeing:
File "/bp_scraper/bp_scraper/httpmiddleware.py", line 22, in from_crawler
return cls(crawler.settings)
File "/bp_scraper/bp_scraper/httpmiddleware.py", line 12, in __init__
if parts[1]:
TypeError: '_sre.SRE_Match' object has no attribute '__getitem__'
The code:
import re
import random
import base64
from scrapy import log
class RandomProxy(object):
def __init__(self, settings):
self.proxy_list = settings.get('PROXY_LIST')
f = open(self.proxy_list)
self.proxies = {}
for l in f.readlines():
parts = re.match('(\w+://)(\w+:\w+#)?(.+)', l)
if parts[1]:
parts[1] = parts[1][:-1]
self.proxies[parts[0] + parts[2]] = parts[1]
f.close()
#classmethod
def from_crawler(cls, crawler):
return cls(crawler.settings)
Thanks in advance for your help!
The result of a re.match call is a SRE_Match object, which does not support the [] operator (a.k.a. __getitem__). I think you want
if parts is not None:
if parts.group(1):
<blah>
Unfortunately, parts.group(1) is not mutable, so you'll have to make another variable to hold the changes you want to make to it.
You can not access the matched results as:
if parts[1]:
parts[1] = parts[1][:-1]
Instead do this,
if parts:
matched = parts.group(1)[:-1]
More on regex matched groups here

Categories