Any inputs on what is wrong with line phCmd = "ph %s return all".split(' ') % (qgroup) ? I am trying to decipher the string %s.
from subprocess import Popen, PIPE, STDOUT
def main ():
qgroups = ['tech.sw.list','tech.sw.amss']
for qgroup in qgroups:
print qgroup
phCmd = "ph %s return all".split(' ') % (qgroup)
phPipe = Popen(phCmd, stdout=PIPE, stderr=PIPE)
(output, error) = phPipe.communicate()
print output
if phPipe.returncode != 0:
print output
raise IOError, "phcommand %s failed" % (phCmd)
return output
ERROR:
Traceback (most recent call last):
File "test.py", line 20, in <module>
main()
File "test.py", line 9, in main
phCmd = "ph %s return all".split(' ') % (qgroup)
if __name__ == '__main__':
main()
The .split(' ') method call of a string returns a list. Try something like
phCmd = ("ph %s return all" % (qgroup)).split(' ')
instead.
"ph %s return all".split(' ') % (qgroup)
The split() call returns a list, and % is undefined for the argument types list and tuple. I'm not sure what you mean to do here, but it looks like you want:
("ph %s return all" % (qgroup)).split(' ')
When using "%" with strings, you have to place it right after the string. This line of code
phCmd = "ph %s return all".split(' ') % (qgroup)
is actually telling Python to take the list returned by "ph %s return all".split(' ') and run an operation similar to:
>>> 2 % 2
0
>>>
on it using (qgroup), which blows up.
To fix your problem, do this:
phCmd = ("ph %s return all" % qgroup).split(' ')
Related
I need some help with a python script. The script is an Example Script form the Pyinsane Module for Python. (Pyinsane https://github.com/jflesch/pyinsane)
I want to write my own scanner script but to do that i should understand the example code or the output error.
Example Script.
import sys
``from PIL import Image
try:
import src.abstract as pyinsane
except ImportError:
import pyinsane.abstract as pyinsane
def set_scanner_opt(scanner, opt, value):
print("Setting %s to %s" % (opt, str(value)))
try:
scanner.options[opt].value = value
except (KeyError, pyinsane.SaneException) as exc:
print("Failed to set %s to %s: %s" % (opt, str(value), str(exc)))
if __name__ == "__main__":
steps = False
args = sys.argv[1:]
if len(args) <= 0 or args[0] == "-h" or args[0] == "--help":
print("Syntax:")
print(" %s [-s] <output file (JPG)>" % sys.argv[0])
print("")
print("Options:")
print(" -s : Generate intermediate images (may generate a lot of"
" images !)")
sys.exit(1)
for arg in args[:]:
if arg == "-s":
steps = True
args.remove(arg)
output_file = args[0]
print("Output file: %s" % output_file)
print("Looking for scanners ...")
devices = pyinsane.get_devices()
if (len(devices) <= 0):
print("No scanner detected !")
sys.exit(1)
print("Devices detected:")
print("- " + "\n- ".join([str(d) for d in devices]))
print("")
device = devices[0]
print("Will use: %s" % str(device))
print("")
source = 'Auto'
if (device.options['source'].constraint_type
== pyinsane.SaneConstraintType.STRING_LIST):
if 'Auto' in device.options['source'].constraint:
source = 'Auto'
elif 'FlatBed' in device.options['source'].constraint:
source = 'FlatBed'
else:
print("Warning: Unknown constraint type on the source: %d"
% device.options['source'].constraint_type)
set_scanner_opt(device, 'resolution', 300)
set_scanner_opt(device, 'source', source)
set_scanner_opt(device, 'mode', 'Color')
print("")
print("Scanning ... ")
scan_session = device.scan(multiple=False)
if steps and scan_session.scan.expected_size[1] < 0:
print("Warning: requested step by step scan images, but"
" scanner didn't report the expected number of lines"
" in the final image --> can't do")
print("Step by step scan images won't be recorded")
steps = False
if steps:
last_line = 0
expected_size = scan_session.scan.expected_size
img = Image.new("RGB", expected_size, "#ff00ff")
sp = output_file.split(".")
steps_filename = (".".join(sp[:-1]), sp[-1])
try:
PROGRESSION_INDICATOR = ['|', '/', '-', '\\']
i = -1
while True:
i += 1
i %= len(PROGRESSION_INDICATOR)
sys.stdout.write("\b%s" % PROGRESSION_INDICATOR[i])
sys.stdout.flush()
scan_session.scan.read()
if steps:
next_line = scan_session.scan.available_lines[1]
if (next_line > last_line):
subimg = scan_session.scan.get_image(last_line, next_line)
img.paste(subimg, (0, last_line))
img.save("%s-%05d.%s" % (steps_filename[0], last_line,
steps_filename[1]), "JPEG")
last_line = next_line
except EOFError:
pass
print("\b ")
print("Writing output file ...")
img = scan_session.images[0]
img.save(output_file, "JPEG")
print("Done")
Now the Output:
sudo python /home/pi/Desktop/scantest.py -s 1
Output file: 1
Looking for scanners ...
Devices detected:
- Scanner 'genesys:libusb:001:006' (Canon, LiDE 110, flatbed scanner)
Will use: Scanner 'genesys:libusb:001:006' (Canon, LiDE 110, flatbed scanner)
Setting resolution to 300
Setting source to Auto
Failed to set source to Auto: <class 'pyinsane.rawapi.SaneStatus'> : Data is invalid (4)
Setting mode to Color
Scanning ...
|Traceback (most recent call last):
File "/home/pi/Desktop/scantest.py", line 107, in <module>
steps_filename[1]), "JPEG")
File "/usr/local/lib/python2.7/dist-packages/PIL/Image.py", line 1439, in save
save_handler(self, fp, filename)
File "/usr/local/lib/python2.7/dist-packages/PIL/PngImagePlugin.py", line 572, in _save
ImageFile._save(im, _idat(fp, chunk), [("zip", (0,0)+im.size, 0, rawmode)])
File "/usr/local/lib/python2.7/dist-packages/PIL/ImageFile.py", line 481, in _save
e = Image._getencoder(im.mode, e, a, im.encoderconfig)
File "/usr/local/lib/python2.7/dist-packages/PIL/Image.py", line 399, in _getencoder
return apply(encoder, (mode,) + args + extra)
TypeError: an integer is required
Please Can anybody explain the example code or help me with the output error?
Thanks a lot
laurin
Is your PIL / Pillow library compiled with JPEG support ?
If not, try replacing:
img.save("%s-%05d.%s" % (steps_filename[0], last_line,
steps_filename[1]), "JPEG")
by
img.save("%s-%05d.%s" % (steps_filename[0], last_line,
steps_filename[1]), "PNG")
And
img.save(output_file, "JPEG")
by
img.save(output_file, "PNG")
I have this code:
#!/usr/bin/python
import os.path
import sys
if len(sys.argv)<2:
print"You need to specify file!"
if (os.path.isfile(sys.argv[1])):
print "File <%s> exist" % sys.argv[1]
elif (sys.argv[1] == "--help"):
print "Add (only)one file argument to command"
print "--help print this screen"
print "--autor autor name and email adress"
print "--about about this program"
elif (sys.argv[1] == "--about"):
print"Program to identify if the file exists"
print"Copyright Vojtech Horanek 2015"
elif (sys.argv[1] == "--autor"):
print"Vojtech Horanek <vojtechhoranek#gmail.com>"
else:
print"No file <%s> found" % sys.argv[1]
and i want execute this piece of code only while sys.argv[1] is exist:
if (os.path.isfile(sys.argv[1])):
print "File <%s> exist" % sys.argv[1]
elif (sys.argv[1] == "--help"):
print "Add (only)one file argument to command"
print "--help print this screen"
print "--autor autor name and email adress"
print "--about about this program"
elif (sys.argv[1] == "--about"):
print"Program to identify if the file exists"
print"Copyright Vojtech Horanek 2015"
elif (sys.argv[1] == "--autor"):
print"Vojtech Horanek <vojtechhoranek#gmail.com>"
else:
print"No file <%s> found" % sys.argv[1]
if i only start the program without an arguments (python program.py)
it's print this text:
You need to specify file!
Traceback (most recent call last):
File "program.py", line 7, in <module>
if (os.path.isfile(sys.argv[1])):
IndexError: list index out of range
I tried "if sys.argv == 1" but doesnt worked.
Any solutions? Thanks
if len(sys.argv)<2:
print"You need to specify file!"
sys.exit()
Now your program will terminate entirely if the user didn't supply any arguments, rather than continuing and raising an exception.
I want to keep some file content loaded in memory so that it can be queried in retrived instantly.
In gearman worker, I am loading the file and put it in listening mode. While making request using gearman client, worker returns loaded content only once, next time client receives None
worker :
class GetLexiconFiles(object):
def __init__(self):
self.gm_worker = gearman.GearmanWorker(['localhost:4730'])
self.loadFiles()
self.gm_worker.register_task('load_db', self.task_listener_reverse)
#self.loadFiles()
#self.gm_worker.work()
def task_listener_reverse(self, gearman_worker, gearman_job):
k=float('inf')
#print "Started loading file"
self.input_text = self.input_text.split('\n')
print "Loading completed"
lexicon = defaultdict(list)
for i, line in enumerate(self.input_text):
#print "line is : ", line
if i >= k: break
#if i % 100000 == 0: print >>sys.stderr, i
try:
if line != '':
nl, dbs = line.split(' ', 1)
nl = int(nl)
dbs = self.str2obj(dbs)
lexicon[nl].append(dbs)
else:
pass
except:
print >>sys.stderr, 'could not parse line %r' % line
print traceback.format_exc()
continue
return json.dumps(lexicon)
if __name__ == '__main__':
GetLexiconFiles().gm_worker.work()
client :
def check_request_status(job_request):
if job_request.complete:
#data = json.loads(job_request.result)
print "Job %s finished! Result: %s - %s" % (job_request.job.unique, job_request.state, job_request.result)
elif job_request.timed_out:
print "Job %s timed out!"
elif job_request.state == JOB_UNKNOWN:
print "Job %s connection failed!"
gm_client = gearman.GearmanClient(['localhost:4730'])
tasks = [{'task': 'load_lexicon', 'data': 'This is testing sentence'}, {'task': 'load_db', 'data': 'This is db testing'}]
submitted_requests = gm_client.submit_multiple_jobs(tasks, background=False, wait_until_complete=False)
completed_requests = gm_client.wait_until_jobs_completed(submitted_requests)
print completed_requests[1].result
for completed_job_request in completed_requests:
check_request_status(completed_job_request)
self.input_text = self.input_text.split('\n')
With this line of code you are converting a string to a list of strings.
Since you save the result back in self.input_text the next time that that function gets called self.input_text will already be a list and it'll raise an exception.
I am trying to make some approximations discard.
The function given below is throwing attribute error. What might be the issue? Kindly help me if possible.
def calculate_tps(filename,identity):
##command='tshark -r %s -R -T pdml diameter'
first_time = None
last_time=None
tshark='tshark -r %s -R \'(diameter.Destination-Host==%s)&& (diameter.CC-Request-Type==3)\' -T pdml'% (filename,identity)
time = None
req = 0
for line in os.popen(tshark, 'r'):
m=pattern.search(line)
if m:
#print m.group(1)
if m.group(1) == 'timestamp':
time=m.group(2)
if not first_time:
first_time=time
elif m.group(1) == 'diameter.Session-Id':
req +=1
if(req % 100) ==0:
print 'processed 1000 requests req number %s ' % req
# making some approximations discard ms
first = long(first_time.split('.').pop(0))
last = long(time.split('.').pop(0))
time_elapsed = first -last
print "%s requests in %s seconds, tps = %s " % ( req, time_elapsed, req/time_elapsed)
Here is the full code. I am trying to calculate number of request per second using tshark.
#!/usr/bin/python
import re
import sys
import commands
import os
import time
pattern = re.compile('<field name="(timestamp|diameter.Session-Id)".*value="([^"]+)')
from optparse import OptionParser
def parse_options():
parser = OptionParser()
# parser.add_option("-f", "--file", dest="filename",
# help="input pcap file", metavar="FILE")
parser.add_option("-i", "--identity", dest="identity",
help="diameter identity to filter on", metavar="IDENTITY")
opts, args = parser.parse_args()
return opts, args
def calculate_tps(filename,identity):
##command='tshark -r %s -R -T pdml diameter'
first_time = None
last_time=None
tshark='tshark -r %s -R \'(diameter.Destination-Host==%s)&& (diameter.CC-Request-Type==3)\' -T pdml'% (filename,identity)
time = None
req = 0
for line in os.popen(tshark, 'r'):
m=pattern.search(line)
if m:
#print m.group(1)
if m.group(1) == 'timestamp':
time=m.group(2)
print 'time is %s' % time
if not first_time:
first_time=time
prin
elif m.group(1) == 'diameter.Session-Id':
req +=1
if(req % 100) ==0:
print 'processed 1000 requests req number %s ' % req
# making some approximations discard ms
first = long(first_time.split('.').pop(0))
last = long(time.split('.').pop(0))
time_elapsed = first -last
print "%s requests in %s seconds, tps = %s " % ( req, time_elapsed,
req/time_elapsed)
def main():
global options
options, args = parse_options()
if len(args) < 1:
print >>sys.stderr, "missing pcap file.Please specify the pcap"
sys.exit(1)
#print options.identity
start_time = time.time()
calculate_tps(args.pop(0), options.identity)
end_time = time.time()
print 'completed in %s', (end_time - start_time)
if __name__ == '__main__':
main()
When you make the calls here:
first = long(first_time.split('.').pop(0))
last = long(time.split('.').pop(0))
You've assumed that each of these is a string. They appear to not have been reassigned during your loop over the file contents, suggesting that your matches are not taking place. You could wrap those in a conditional check with something like this:
if None in (first_time, time):
print "Uh oh, one of first_time or time is None - ", first_time, time
That would give you an indicator of the problem at the very least.
This is because your first and last are not string. So there is no attribute split of the type.
So if you need split attribute, you better change them to string type.
I am trying to populate lists self.images and self.servers with functions self.refresh_images_list and self.refresh_server_list using multiprocessing. I am doing this so when the object is created they will kick off async. I am using shared lists so the child copy will update the original objects list.
However, I am getting a Pickle error so I am pretty stuck.
class Account():
def __init__(self, username, api, pipe_object):
manager = Manager()
self.images = manager.list()
self.servers = manager.list()
self.images_timestamp = None
self.servers_timestamp = None
#needed a dictionary instead of
#list/tuple. This works best for
#the generator.
self.regions = {
"DFW" : pyrax.connect_to_cloudservers("DFW"),
"ORD" : pyrax.connect_to_cloudservers("ORD"),
"SYD" : pyrax.connect_to_cloudservers("SYD")
}
p1 = Process(target = self.refresh_server_list, args=())
p2 = Process(target = self.refresh_image_list, args=())
p1.start()
p2.start()
p1.join()
p2.join()
flavors = None
#multiprocessing shares lists only for __init__
#after __init__, we want to break the share
unshare_lists = False
def refresh_server_list(self):
if self.unshare_lists:
self.servers = []
self.servers_timestamp = time.strftime(
"%I:%M:%S", time.localtime()
)
with Redirect(self.pipe_object):
print "\nRefreshing server cache...hold on!"
for region, value in self.regions.iteritems():
region_servers = value.servers.list()
for region_servers in generator(region_servers, region):
self.servers.append(region_servers)
with Redirect(self.pipe_object):
print "\nServer cache completed!"
def server_list(self):
if not self.servers:
self.refresh_server_list()
with Redirect(self.pipe_object):
print_header("Cached Server List", "-")
for srv in self.servers:
print "\nName: %s" % srv.name
print "Created: %s" % srv.created
print "Progress: %s" % srv.progress
print "Status: %s" % srv.status
print "URL: %s" % srv.links[0]["href"]
print "Networks: %s" % srv.networks
print "\nLast Refresh time: %s" % self.servers_timestamp
def refresh_image_list(self):
if self.unshare_lists:
self.images = []
self.images_timestamp = time.strftime(
"%I:%M:%S", time.localtime()
)
with Redirect(self.pipe_object):
# print_header("Active Image List", "-")
print "\nRefreshing image cache...hold on!"
for region, value in self.regions.iteritems():
region_images = value.images.list()
for region_images in generator(region_images, region):
self.images.append(region_images)
with Redirect(self.pipe_object):
print "\nImage cache completed!"
def image_list(self):
if not self.images:
self.refresh_image_list()
with Redirect(self.pipe_object):
print_header("List Images", "-")
for img in self.images:
print (
str(self.images.index(img)+1) + ") "
+ "Name: %s\n ID: %s Status: %s" %
(img.name, img.id, img.status)
)
print "\nLast Refresh time: %s" % self.images_timestamp
The error I get:
Refreshing server cache...hold on!
Traceback (most recent call last):
File "menu.py", line 162, in <module>
main()
File "menu.py", line 156, in main
menus[value](hash_table, accounts)
File "menu.py", line 104, in menu
choices[value]()
File "/home/work/modules/classes.py", line 87, in server_list
self.refresh_server_list()
File "/home/work/modules/classes.py", line 80, in refresh_server_list
self.servers.append(region_servers)
File "<string>", line 2, in append
File "/usr/lib64/python2.7/multiprocessing/managers.py", line 758, in _callmethod
conn.send((self._id, methodname, args, kwds))
cPickle.PicklingError: Can't pickle <type 'instancemethod'>: attribute lookup __builtin__.instancemethod failed