Re: [Tutor] No file or directory error using subprocess and Popen
On Sun, May 14, 2017 at 10:57 PM, Jimwrote: > I am running this on Mint 18. > This is the third script I have written to open and position windows in > workspaces. The first two work, but trying to open ebook-viewe r (calibre) > with a specific book produces the following error. > If I run the same command in the terminal it works without an error. > > > Exception in thread Thread-4: > Traceback (most recent call last): > File "/usr/lib/python3.5/threading.py", line 914, in _bootstrap_inner > self.run() > File "/usr/lib/python3.5/threading.py", line 862, in run > self._target(*self._args, **self._kwargs) > File "/home/jfb/MyProgs/Scripts/place_windows_OO_WS3.py", line 24, in > open_it > subprocess.call([self.program]) > File "/usr/lib/python3.5/subprocess.py", line 557, in call > with Popen(*popenargs, **kwargs) as p: > File "/usr/lib/python3.5/subprocess.py", line 947, in __init__ > restore_signals, start_new_session) > File "/usr/lib/python3.5/subprocess.py", line 1551, in _execute_child > raise child_exception_type(errno_num, err_msg) > FileNotFoundError: [Errno 2] No such file or directory: 'ebook-viewer > /home/jfb/Documents/eBooks/Javascript/GOOGLE_SHEETS/googlespreadsheetprogramming.epub' > > Code: > > # place_windows_OO_WS3.py > > import subprocess > from subprocess import Popen,PIPE > import threading > import time > > class Place(): > > def __init__(self): > self.programs = ['jedit', 'google-chrome', 'doublecmd', > 'ebook-viewer > /home/jfb/Documents/eBooks/Javascript/GOOGLE_SHEETS/googlespreadsheetprogramming.epub'] > self.classname = {'jedit' : 'sun-awt-X11-XFramePeer', > 'google-chrome':'google-chrome', > 'doublecmd':'doublecmd', > 'calibre-ebook-viewer': 'libprs500'} > self.open_and_move() > > def open_it(self): > subprocess.call([self.program]) I'm not very familiar with using the subprocess module yet, but when the above call to "subprocess.call([self.program])" occurs, isn't subprocess.call() expecting a list like ['ebook-viewer', '/home/jfb ...'] ? Hope I am not off-track here. boB ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
[Tutor] No file or directory error using subprocess and Popen
I am running this on Mint 18. This is the third script I have written to open and position windows in workspaces. The first two work, but trying to open ebook-viewe r (calibre) with a specific book produces the following error. If I run the same command in the terminal it works without an error. Exception in thread Thread-4: Traceback (most recent call last): File "/usr/lib/python3.5/threading.py", line 914, in _bootstrap_inner self.run() File "/usr/lib/python3.5/threading.py", line 862, in run self._target(*self._args, **self._kwargs) File "/home/jfb/MyProgs/Scripts/place_windows_OO_WS3.py", line 24, in open_it subprocess.call([self.program]) File "/usr/lib/python3.5/subprocess.py", line 557, in call with Popen(*popenargs, **kwargs) as p: File "/usr/lib/python3.5/subprocess.py", line 947, in __init__ restore_signals, start_new_session) File "/usr/lib/python3.5/subprocess.py", line 1551, in _execute_child raise child_exception_type(errno_num, err_msg) FileNotFoundError: [Errno 2] No such file or directory: 'ebook-viewer /home/jfb/Documents/eBooks/Javascript/GOOGLE_SHEETS/googlespreadsheetprogramming.epub' Code: # place_windows_OO_WS3.py import subprocess from subprocess import Popen,PIPE import threading import time class Place(): def __init__(self): self.programs = ['jedit', 'google-chrome', 'doublecmd', 'ebook-viewer /home/jfb/Documents/eBooks/Javascript/GOOGLE_SHEETS/googlespreadsheetprogramming.epub'] self.classname = {'jedit' : 'sun-awt-X11-XFramePeer', 'google-chrome':'google-chrome', 'doublecmd':'doublecmd', 'calibre-ebook-viewer': 'libprs500'} self.open_and_move() def open_it(self): subprocess.call([self.program]) def open_and_move(self): for self.program in self.programs: opener = threading.Thread(target=self.open_it) opener.start() time.sleep(2) p = Popen(['xdotool', 'search', '--classname', self.classname[self.program]], stdout=subprocess.PIPE) if self.classname[self.program] == 'sun-awt-X11-XFramePeer': wid = str(p.stdout.read()) wid = wid[len(wid) - 11 : len(wid) - 3] x = '0' y = '0' print('***jedit***') elif self.classname[self.program] == 'google-chrome': wid = str(p.stdout.read()) wid = wid[len(wid) - 11 : len(wid) - 3] x = '1924' y = '0' print('***google***') elif self.classname[self.program] == 'doublecmd': wid = str(p.stdout.read()) wid = wid[len(wid) - 11 : len(wid) - 3] x = '1924' y = '537' print('***double***') else: wid = str(p.stdout.read()) wid = wid[len(wid) - 11 : len(wid) - 3] x = '2540' #'1924' y = '537' print('***calibre***') subprocess.call(['xdotool', 'windowmove', str(wid), x, y]) I did some googling and it seems that subprocess does not have a length limit in linux. Could someone tell me how to correct the above error. Thanks, Jim ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] How to write the __str__ function
Not exactly sure what you're trying to do here but maybe you want to join those two things instead? Because of course it's a tuple the way you have it now. Shu Sent from my iTypo (with enhanced embarrassing auto-correcting) > On May 14, 2017, at 11:03 AM, Sydney Shallwrote: > > I need some advice that I have been embarrased to ask for, because I think > that my error is so elementary. > > I have written, as advised by the tutors, a complex program in a topic that > interests me. The program works well and the tests are OK. > > Now I want to add a __str__ function, which I thought would be > straightforward. But I cannot get it right. > > The code that I have so far is as folows: > > def __str__(self): >return("\n" > " Output from __str__ of POCWP. " > "\n" > "\n After the first turnover, during the " > "'Population Of Capitals Init' cycle," > "\n the productivities were raised from 1.0 " > "\n to a specific Unit Constant Capital (UCC) " > "for each specific capital: " > "\n The input value for the mean of UCC " > "was %7.5f" % (self.ucc), > "\n The fractional sigma (FractionalSTD)" > " of UCC that was input was %7.5f " % (self.fractsigma_ucc)) > > The error message is: > > TypeError: __str__ returned non-string (type tuple) > > When I omit either or both of the objects; self.ucc or self.fractsigma_ucc, > the code works fine. > > So, I guess my code has an error. But I cannot detect the error. > > Guidance would be much appreciated. > > -- > Sydney > ___ > Tutor maillist - Tutor@python.org > To unsubscribe or change subscription options: > https://mail.python.org/mailman/listinfo/tutor ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] How to write the __str__ function
On 14/05/17 19:03, Sydney Shall wrote: > The code that I have so far is as folows: > > def __str__(self): > return("\n" > " Output from __str__ of POCWP. " > "\n" > "\n After the first turnover, during the " > "'Population Of Capitals Init' cycle," > "\n the productivities were raised from 1.0 " > "\n to a specific Unit Constant Capital (UCC) " > "for each specific capital: " > "\n The input value for the mean of UCC " > "was %7.5f" % (self.ucc), Here endeth the first string > "\n The fractional sigma (FractionalSTD)" > " of UCC that was input was %7.5f " % (self.fractsigma_ucc)) And here the second. Returning two strings separated by a comma makes it a tuple. Instead put the two values in a tuple at the end of a single concatenated string. And while at it make life easier for your self and use triple quotes: def __str__(self): return """ Output from __str__ of POCWP. After the first turnover, during the 'Population Of Capitals Init' cycle, the productivities were raised from 1.0 to a specific Unit Constant Capital (UCC) for each specific capital: The input value for the mean of UCC was %7.5f The fractional sigma (FractionalSTD) of UCC that was input was %7.5f """ % ( self.ucc, self.fractsigma_ucc) Tweak the formatting to suit. However, I'm not sure thats really a good use of __str__, I might be tempted to make that an explicit method that's called pprint() - for pretty-print - or somesuch. __str__() methods are usually a fairly cocise depiction of the objects state that you can embed in a bigger string. Maybe pprint() would look like def pprint(self): return """ Output from __str__ of POCWP. After the first turnover, during the 'Population Of Capitals Init' cycle, the productivities were raised from 1.0 to a specific Unit Constant Capital (UCC) for each specific capital: %s""" % self And __str__() def __str__(self): return """ The input value for the mean of UCC was %7.5f The fractional sigma (FractionalSTD) of UCC that was input was %7.5f """ % (self.ucc, self.fractsigma_ucc) Thus pprint() uses str() to create the long version while str() just gives the bare bones result. Just a thought. -- Alan G Author of the Learn to Program web site http://www.alan-g.me.uk/ http://www.amazon.com/author/alan_gauld Follow my photo-blog on Flickr at: http://www.flickr.com/photos/alangauldphotos ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] How to write the __str__ function
Am 14.05.2017 um 20:59 schrieb Martin A. Brown: Hello and greetings, I need some advice that I have been embarrased to ask for, because I think that my error is so elementary. Well, there are two benefits to trying to write down questions like this when you encounter them. 1) Rubber Duck debugging (if you have not heard of it); sometimes the act of describing the problem / question is enough for you to figure it out in the process 2) If you don't quite get there, then you have a description that somebody can easily review and respond to. I have written, as advised by the tutors, a complex program in a topic that interests me. The program works well and the tests are OK. Right on! Now I want to add a __str__ function, which I thought would be straightforward. But I cannot get it right. The code that I have so far is as folows: def __str__(self): return("\n" " Output from __str__ of POCWP. " "\n" "\n After the first turnover, during the " "'Population Of Capitals Init' cycle," "\n the productivities were raised from 1.0 " "\n to a specific Unit Constant Capital (UCC) " "for each specific capital: " "\n The input value for the mean of UCC " "was %7.5f" % (self.ucc), "\n The fractional sigma (FractionalSTD)" " of UCC that was input was %7.5f " % (self.fractsigma_ucc)) The error message is: TypeError: __str__ returned non-string (type tuple) ... I have, therefore, a few small suggestions: 1. Put all of the variables replacements at the end. thing = ("var x=%s\nvar y=%s" % (x,y)) 2. When creating the replacements, also use tuples (see next point, too): "was %7.5f" % (self.ucc,) If you put this into your original string, it won't work. Try this very simple example: st = "abc %d" % 7 "def" # <- SyntaxError The replacements definitely belong at the end, all together. But an additional question: do you really want to get this very long text every time you need an instance of your class as a string? Even if it's put into another sentence using string formatting? Like this for example (assuming your class is called MyClass): x = MyClass(some, args) s = """This is an instance of MyClass with the value %s. It was initialized with the values some = %s, args = %s.""" % (x, some, args) print(s) Greetings, Sibylle ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] How to write the __str__ function
Hello and greetings, > I need some advice that I have been embarrased to ask for, because > I think that my error is so elementary. Well, there are two benefits to trying to write down questions like this when you encounter them. 1) Rubber Duck debugging (if you have not heard of it); sometimes the act of describing the problem / question is enough for you to figure it out in the process 2) If you don't quite get there, then you have a description that somebody can easily review and respond to. > I have written, as advised by the tutors, a complex program in a > topic that interests me. The program works well and the tests are > OK. Right on! > Now I want to add a __str__ function, which I thought would be > straightforward. But I cannot get it right. > > The code that I have so far is as folows: > > def __str__(self): >return("\n" > " Output from __str__ of POCWP. " > "\n" > "\n After the first turnover, during the " > "'Population Of Capitals Init' cycle," > "\n the productivities were raised from 1.0 " > "\n to a specific Unit Constant Capital (UCC) " > "for each specific capital: " > "\n The input value for the mean of UCC " > "was %7.5f" % (self.ucc), > "\n The fractional sigma (FractionalSTD)" > " of UCC that was input was %7.5f " % (self.fractsigma_ucc)) > > The error message is: > > TypeError: __str__ returned non-string (type tuple) > > When I omit either or both of the objects; self.ucc or > self.fractsigma_ucc, the code works fine. > > So, I guess my code has an error. But I cannot detect the error. It is not the omission of the objects, but rather the comma which is posing you this puzzle. > Guidance would be much appreciated. So, the error message tells you that you are creating a tuple, so let's look at why / how you are creating a tuple. I'm going to remove your variable names and use x and y. x, y = 5, 7 thing = ("var x=%s" % (x,), "var y=%s" % (y,)) # -- your code type(thing) # you will see: But, this is what you are doing when you remove one of the variables: thing = ("var x=unknown var y=%s" % (y,)) type(thing) # you will see: What's going on here? thing = ("no comma here") type(thing) # you will see: thing = ("no comma here",) type(thing) # you will see: So, when you are using parentheses, you can create a tuple, but you must include a comma, otherwise, you are not actually creating a tuple. Of course, you wish to create a string, not a tuple, so you want to bear in mind that you are creating a tuple when you include the comma in the line that ends with: (self.ucc), I have, therefore, a few small suggestions: 1. Put all of the variables replacements at the end. thing = ("var x=%s\nvar y=%s" % (x,y)) 2. When creating the replacements, also use tuples (see next point, too): "was %7.5f" % (self.ucc,) 3. Use separate statements for creating and returning the string. See below where I'm using triple-quoted strings for easier editing [0].) Good luck with Python, -Martin === def thing(): x, y = 3.141592653589793, 2.718281828459045 text = '''\n Output from __str__ of POCWP. After the first turnover, during the 'Population Of Capitals Init' cycle, the productivities were raised from 1.0 to a specific Unit Constant Capital (UCC) for each specific capital: The input value for the mean of UCC was %7.5f The fractional sigma (FractionalSTD) of UCC that was input was %7.5f''' return text % (x, y) === [0] https://docs.python.org/3/library/stdtypes.html#text-sequence-type-str -- Martin A. Brown http://linux-ip.net/ ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
[Tutor] How to write the __str__ function
I need some advice that I have been embarrased to ask for, because I think that my error is so elementary. I have written, as advised by the tutors, a complex program in a topic that interests me. The program works well and the tests are OK. Now I want to add a __str__ function, which I thought would be straightforward. But I cannot get it right. The code that I have so far is as folows: def __str__(self): return("\n" " Output from __str__ of POCWP. " "\n" "\n After the first turnover, during the " "'Population Of Capitals Init' cycle," "\n the productivities were raised from 1.0 " "\n to a specific Unit Constant Capital (UCC) " "for each specific capital: " "\n The input value for the mean of UCC " "was %7.5f" % (self.ucc), "\n The fractional sigma (FractionalSTD)" " of UCC that was input was %7.5f " % (self.fractsigma_ucc)) The error message is: TypeError: __str__ returned non-string (type tuple) When I omit either or both of the objects; self.ucc or self.fractsigma_ucc, the code works fine. So, I guess my code has an error. But I cannot detect the error. Guidance would be much appreciated. -- Sydney ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] cffi version mismatch stalling pip install pygit2
On 05/12/2017 01:44 PM, MR ZenWiz wrote: > How do I fix this? > > We are using a newer version of libgit2 than the standard release > (libgit2.0.25.0 instead of libgit2.0.24.0 - some hard dependency in > our code). > > However, after I install libgit2 (25), I get this error chain: If you use a virtualenv (google for instructions if you're not familiar), you can have all the versions be what they need to be for this install, without impacting your system versions of things - that's about the only advice from here. Although, I tried to install pygit2 in a virtualenv and it failed, though a bit of a different error, so it may be the pygit2 project itself is having some issues getting their dependencies right. I got this: raise DistutilsError("Setup script exited with %s" % (v.args[0],)) distutils.errors.DistutilsError: Setup script exited with error: command 'gcc' failed with exit status 1 without any hint of *why* gcc failed, probably it was a missing C header file but haven't researched. > > pip install pygit2 > Collecting pygit2 > Using cached pygit2-0.25.1.tar.gz > Complete output from command python setup.py egg_info: > /usr/lib/python2.7/site-packages/setuptools/version.py:1: > UserWarning: Module cffi was already imported from > /usr/lib64/python2.7/site-packages/cffi/__init__.pyc, but > /tmp/easy_install-HY89WT/cffi-1.9.1 is being added to sys.path > import pkg_resources > > Installed > /tmp/pip-build-OX9KGK/pygit2/.eggs/cffi-1.9.1-py2.7-linux-x86_64.egg > /usr/lib/python2.7/site-packages/setuptools/dist.py:378: > UserWarning: Module cffi was already imported from > /usr/lib64/python2.7/site-packages/cffi/__init__.pyc, but > /tmp/pip-build-OX9KGK/pygit2/.eggs/cffi-1.9.1-py2.7-linux-x86_64.egg > is being added to sys.path > pkg_resources.working_set.add(dist, replace=True) > Traceback (most recent call last): > File "", line 1, in > File "/tmp/pip-build-OX9KGK/pygit2/setup.py", line 210, in > **extra_args) > File "/usr/lib64/python2.7/distutils/core.py", line 112, in setup > _setup_distribution = dist = klass(attrs) > File "/usr/lib/python2.7/site-packages/setuptools/dist.py", line > 321, in __init__ > _Distribution.__init__(self, attrs) > File "/usr/lib64/python2.7/distutils/dist.py", line 287, in __init__ > self.finalize_options() > File "/usr/lib/python2.7/site-packages/setuptools/dist.py", line > 390, in finalize_options > ep.load()(self, ep.name, value) > File "/usr/lib64/python2.7/site-packages/cffi/setuptools_ext.py", > line 188, in cffi_modules > add_cffi_module(dist, cffi_module) > File "/usr/lib64/python2.7/site-packages/cffi/setuptools_ext.py", > line 49, in add_cffi_module > execfile(build_file_name, mod_vars) > File "/usr/lib64/python2.7/site-packages/cffi/setuptools_ext.py", > line 25, in execfile > exec(code, glob, glob) > File "pygit2/_run.py", line 67, in > ffi = FFI() > File "/usr/lib64/python2.7/site-packages/cffi/api.py", line 54, > in __init__ > backend.__version__, backend.__file__)) > Exception: Version mismatch: this is the 'cffi' package version > 1.10.0, located in '/usr/lib64/python2.7/site-packages/cffi/api.pyc'. > When we import the top-level '_cffi_backend' extension module, we get > version 1.9.1, located in > '/tmp/pip-build-OX9KGK/pygit2/.eggs/cffi-1.9.1-py2.7-linux-x86_64.egg/_cffi_backend.so'. > The two versions should be equal; check your installation. > > > Command "python setup.py egg_info" failed with error code 1 in > /tmp/pip-build-OX9KGK/pygit2/ > > Thanks. > MR (long time s/w professional but python newb) > ___ > Tutor maillist - Tutor@python.org > To unsubscribe or change subscription options: > https://mail.python.org/mailman/listinfo/tutor > ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor