Bizarre behavior of the 'find' method of strings
Greetings, folks, I am using python 2.7.2. Here is something I got: >>> a = 'popular' >>> i = a.find('o') >>> j = a.find('a') >>> a[i:j] 'opul' Well, I expected a[i:j] to be 'opula', and can't think of any reason why this is not happening. So, can anybody help me out about this? Thank you very much. Jim -- http://mail.python.org/mailman/listinfo/python-list
Re: Bizarre behavior of the 'find' method of strings
Thanks for all you guys. Now I got it. To Steven, I was in a little rush when I put this post. But you are right. It's not the find method's problem. -- http://mail.python.org/mailman/listinfo/python-list
Re: Literate Programming
On Apr 7, 2:09 pm, Hans Georg Schaathun wrote: > Has anyone found a good system for literate programming in python? Are you aware of pyweb http://sourceforge.net/projects/pywebtool/ ? -- http://mail.python.org/mailman/listinfo/python-list
Re: Literate Programming
On Apr 8, 3:21 pm, Hans Georg Schaathun wrote: > Interesting tool, but it solves only part of the problem. > I could use it as a replacement for pylit, but I would then still > have the problem of commenting code within a block, which is a > reST/sphinx problem. I'm sorry; I don't understand "commenting code within a block" but I wondered if it meant you were not fully familiar with the idea of the web-type programs. Instead of looking to typeset the comments, you uses chunks. Thus a <> chunk may be something like (pyweb has somewhat different syntax) <> def main(argv=None,log=None): <> <> <> <> Before each chunk comes the description of what that chunk does. Something like This routine factors $n$ finding any factors that are powers of a prime number. <> def driver(n,opt1,opt2): <> <> is a rough idea (here $n$ is a LaTeX; you can do HTML or RST). So you are commenting the chunks, which can be blocks of the code. > Alternatively, I could use pyweb directly with LaTeX. However, then > I would need to find or create macro packages which provide the > features of reST directly in LaTeX. Do you know of a good candidate? What features? Jim -- http://mail.python.org/mailman/listinfo/python-list
Re: This newsgroup (comp.lang.python) may soon be blocked by Google Gro
On 02/09/2018 08:07 AM, Steven D'Aprano wrote: On Fri, 09 Feb 2018 12:45:29 +, Chris Green wrote: Mark Lawrence wrote: [...] Please don't waste your time with the gmane website. Just point any (semi-)decent mail client like Thunderbird at news.gmane.org and you've Surely you mean NNTP/Usenet client. That's what he said :-) Thunderbird does both mail and news. Or at least it did, last time I checked. It does, using it for this reply. Regards, Jim -- https://mail.python.org/mailman/listinfo/python-list
Using sudo with pip3?
Setting up a new computer to run Ubuntu 16.04. Started using pip3 to install all the python stuff I had on the old machine and got this message: jfb@jims-1604:~$ sudo pip3 install matplotlib [sudo] password for jfb: The directory '/home/jfb/.cache/pip/http' or its parent directory is not owned by the current user and the cache has been disabled. Please check the permissions and owner of that directory. If executing pip with sudo, you may want sudo's -H flag. I (jfb) own the directory in question. I used sudo because I recall needing to use it on the old machine to get something to install. So is it necessary or even desirable to use sudo with pip3? Thanks, Jim -- https://mail.python.org/mailman/listinfo/python-list
Re: Using sudo with pip3?
On 01/06/2017 08:24 PM, Cameron Simpson wrote: On 06Jan2017 23:03, Clint Moyer wrote: Thanks everyone for the advice. Please note in my following comments I am not arguing with anyone, merely trying to understand. I am not a professional programmer, just someone who enjoys programing for my own use/enjoyment and I am the only user of this system. Packages supplied by your distribution can be trusted more than packages from PyPi. Just my two cents. Most distros offer nearly all the useful Python modules directly from the repo. I would agree with this on the whole. And also that it is generally better to add modules to your system python via the distro's repo because that bring benefit to every user on the system, not just yourself. What is "system python"? If I do $ which python I get /usr/bin/python which points to python 2.7xx. So if everything I added was for python 3 either using pip3 or apt-get would I be adding to "system python"? I see that most of the "major" python3 modules I had installed, with the exception of scripy, are in the repository. If I upgraded one of the repository modules using pip3 would I lose the benefits of installing from the repository? I know it seems to be possible as I installed pip3 from the repository. The first time I used it to install a module it informed me there was a more current version available and showed me the command to update. I updated and it seems to be fine. Virtual environments are great, but if you want to add libraries to your system interpreter I'd recommend a simple sync through your repo. I'm directly advocating _not_ adding PyPI packages to the system interpreter. If nothing else, they may differ in behaviour and potentially actually break system behaviour. Having your on virtualenv is good for: adding packages no provided by your vendor, adding packages deliberately different from those from your vendor (eg newer versions with specific bugfixes or extra features), having an isolated environment for packages (you can make more than one virtual environment). And of course it avoids interfering with your system python install. A number of years ago I had virtualenv installed. At the time I remember it took me a while to get it installed and working. Right now I am working on some scripts to download some financial date using Selenium and paste it into Libreoffice Calc spreadsheets. Will using virtualenv have any effect on me running those scripts? Thanks, Jim Cheers, Cameron Simpson -- https://mail.python.org/mailman/listinfo/python-list
Re: Using sudo with pip3?
On 01/07/2017 03:17 PM, Dennis Lee Bieber wrote: On Sat, 7 Jan 2017 12:22:45 -0600, jim declaimed the following: What is "system python"? If I do $ which python I get /usr/bin/python which points to python 2.7xx. So if everything I added was for python 3 either using pip3 or apt-get would I be adding to "system python"? What does which python3 return? jfb@jims-1604:~$ which python3 /usr/bin/python3 Regards, Jim wulfraed@Jessie:~$ which python /usr/bin/python wulfraed@Jessie:~$ which python3 /usr/bin/python3 wulfraed@Jessie:~$ python Python 2.7.9 (default, Mar 1 2015, 12:57:24) [GCC 4.9.2] on linux2 Type "help", "copyright", "credits" or "license" for more information. wulfraed@Jessie:~$ python3 Python 3.4.2 (default, Oct 8 2014, 10:45:20) [GCC 4.9.1] on linux Type "help", "copyright", "credits" or "license" for more information. wulfraed@Jessie:~$ I consider both of those to be "system" as I don't recall explicitly asking for either (Debian running in VirtualBox) -- https://mail.python.org/mailman/listinfo/python-list
Re: Using sudo with pip3?
On 01/07/2017 05:58 PM, Clint Moyer wrote: Not sure how you guys got this thread so far off topic, but I think it is valuable to present the current situation in the context of Jim's sudo question. Staying on topic, the emphasis should be on taking the path of least resistance with your current OS. The only thing to be gleaned from PEP394 is that users should not put faith or expectations in what their /usr/bin/python symlink points to. Most systems point to Python2, but it is not guaranteed. So to minimize your issues with installing Python packages, take the path of least resistance and install through your system repo. And use Python2 or Python3 explicitly to avoid conflicts. -- Clint As I mentioned in another post, most of the more popular modules I had installed on my old system using pip are available in the repository and I will use the repository to install them on the new system. I now understand that using sudo is a bad idea. One question from the earlier post that did not get answered concerned upgrading a repository installed module with pip. To get started on the new system I installed pip3 from the repository. The first time I used it to install a module it said a newer version was available and gave the command to update it. What are the consequences of using pip to upgrade repository installed modules? I ask because 16.04 is LTS and won't be getting version upgrades unless they are security related. Also pandas is in the repositories but the module pandas-datareader, which I may need to use, is not. Regards, Jim On Sat, Jan 7, 2017 at 4:39 PM, Chris Angelico wrote: On Sun, Jan 8, 2017 at 9:34 AM, Michael Torrie wrote: On 01/07/2017 11:39 AM, Clint Moyer wrote: All Linux operating systems come with Python installed, with more recent systems such as Arch defaulting /usr/bin/python to Python3, since Python2 discontinued some 7-9 years ago. Poor choice of words, in my opinion. Python 2 has not received new features for 7-9 years now but it certainly hasn't been "discontinued" and won't be for some years yet, though new programming (and distros) should be with Python 3 now. Also, /usr/bin/python shouldn't be Python 3. https://www.python.org/dev/peps/pep-0394/ But various distros are moving towards "don't have Python 2 installed by default", which consequently means "no system scripts depend on Python 2". ChrisA -- https://mail.python.org/mailman/listinfo/python-list
Re: Using sudo with pip3?
On 01/07/2017 08:42 PM, Cameron Simpson wrote: On 07Jan2017 19:45, jim wrote: On 01/07/2017 05:58 PM, Clint Moyer wrote: So to minimize your issues with installing Python packages, take the path of least resistance and install through your system repo. And use Python2 or Python3 explicitly to avoid conflicts. As I mentioned in another post, most of the more popular modules I had installed on my old system using pip are available in the repository and I will use the repository to install them on the new system. I now understand that using sudo is a bad idea. Cool. One question from the earlier post that did not get answered concerned upgrading a repository installed module with pip. I would recommend not. As soon as you get there: - if the vendor updates it, use apt-get (I know this doesn't fit your situation with Ubuntu 16.04 LTS) - if you want a more modern version, now is the time to use virtualenv The thing about LTS is that the vendor guarentees its stability. If you upgrade the vendor installed package using /usr/bin/pip (or pip3) as root, you're replacing the vendor supplied module with a newer one, which may break vendor supplied packages which use that module expected the _old_ behaviour. So endeavour to leave the vendor suppplied stuff entirely alone except for apt-get style updates. Instead, make a virtualenv an upgrade it with a newer package. To get started on the new system I installed pip3 from the repository. The first time I used it to install a module it said a newer version was available and gave the command to update it. What are the consequences of using pip to upgrade repository installed modules? In theory, if the newer version of the module installs a breaking change it can break things in the system which might use that module and expect its old behaviour. Also, _sometimes_, vendors supply patches versions of packages, possibly including python modules. If they're modified/patched, there is probably a reason. You'd be undoing that patch. I ask because 16.04 is LTS and won't be getting version upgrades unless they are security related. Also pandas is in the repositories but the module pandas-datareader, which I may need to use, is not. Again: make a virtualenv as yourself. Its pip can be run as yourself, therefore cannot accidentally modify system module. It's pip can freely upgrade a older module. If you install a module _not_ supplied by the vendor, the virtualenv pip can freely do that, and _also_ upgrade a required module should that be required (PyPI package definitions let a module specify required revisions of other modules it may use). Cheers, Cameron Simpson You've convinced me. I will have to buckle down and figure out to use virtualenv/venv. I see Chris recommends venv. I read through PEP405 and this link https://docs.python.org/3.5/library/venv.html. I don't pretend to fully understand it all but it does seem to make a good case for using venv over virtualenv. thanks, Jim -- https://mail.python.org/mailman/listinfo/python-list
pyuno in Libreoffice 5.1.4.2
Does anyone know if the changes outlined here [1] have been implemented? Supposedly changes have been made to pyuno to make it more pythonic. Item 2 Cellranges says that: cell = sheet.getCellByPosition(cellCol + col, cellRow + row) Can be written as: cell = sheet.cellrange[cellRow + row, cellCol + col] But when I try that I get: Traceback (most recent call last): File "/home/jfb/.config/libreoffice/4/user/Scripts/python/enter_INV/enter_INV.py", line 68, in keyPressed move_selected_cell(1, 0) File "/home/jfb/.config/libreoffice/4/user/Scripts/python/enter_INV/enter_INV.py", line 112, in move_selected_cell cell = sheet.cellrange[cellRow + row, cellCol + col] AttributeError: cellrange Or maybe I am misunderstanding how to use it. Regards, Jim [1] https://cgit.freedesktop.org/libreoffice/core/commit/?id=af8143bc40cf2cfbc12e77c9bb7de01b655f7b30 -- https://mail.python.org/mailman/listinfo/python-list
Re: pyuno in Libreoffice 5.1.4.2
On 01/22/2017 07:02 PM, MRAB wrote: On 2017-01-23 00:10, Jim wrote: Does anyone know if the changes outlined here [1] have been implemented? Supposedly changes have been made to pyuno to make it more pythonic. Item 2 Cellranges says that: cell = sheet.getCellByPosition(cellCol + col, cellRow + row) Can be written as: cell = sheet.cellrange[cellRow + row, cellCol + col] But when I try that I get: Traceback (most recent call last): File "/home/jfb/.config/libreoffice/4/user/Scripts/python/enter_INV/enter_INV.py", line 68, in keyPressed move_selected_cell(1, 0) File "/home/jfb/.config/libreoffice/4/user/Scripts/python/enter_INV/enter_INV.py", line 112, in move_selected_cell cell = sheet.cellrange[cellRow + row, cellCol + col] AttributeError: cellrange Or maybe I am misunderstanding how to use it. I think it might be direct indexing of the sheet: cell = sheet[cellRow + row, cellCol + col] Regards, Jim [1] https://cgit.freedesktop.org/libreoffice/core/commit/?id=af8143bc40cf2cfbc12e77c9bb7de01b655f7b30 You are correct, that worked. Thank you very much. Regards, Jim -- https://mail.python.org/mailman/listinfo/python-list
How to write libreoffice python macros in venv?
I have all the necessary libreoffice modules installed in my Mint 18.1 system to allow me to write libreoffice calc macros in python. I now have venv installed. If I try to import uno for a calc macro in it I get an error that there is no uno module. How can I get my venv to find uno and the other necessary modules so I can write calc macros in it? Thanks, Jim -- https://mail.python.org/mailman/listinfo/python-list
Problems after upgrading Python 3.6.3 to 3.6.5
I am running Linux Mint 18.1 and Python3 version 3.5.2, Awhile ago I installed Python3.6 from LP-PPA-jonathonf-python-3.6/now. I did it to run 3.6 in virtual environment. Everything was working until I allowed the package manager to upgrade 3.6.3 to 3.6.5. See (1) below. First problem I had was pip would not run. I solved that problem with help from another forum. Then I discovered I could not import tkinter in my 3.6 EV. At the end of the error message (2) it said to install python3-tk. After i did so I could import tkinter in 3.6 but not the default system python3.5. At this point I checked the log (1) and saw that python3-tk had been removed. Looking at the installed version of python3-tk it now said 3.6.5~16.04.york0.2. So does the version matter between python3.5 and 3.6 or do I just have some kind of path problem. At this point I am somewhat lost as to how to get tkinter working again in 3.5. Regards, Jim (1) Commit Log for Thu May 3 13:26:38 2018 Removed the following packages: python3-tk Upgraded the following packages: libpython3.6-minimal (3.6.3-1ubuntu1~16.04.york1) to 3.6.5-5~16.04.york0 libpython3.6-stdlib (3.6.3-1ubuntu1~16.04.york1) to 3.6.5-5~16.04.york0 python3.6 (3.6.3-1ubuntu1~16.04.york1) to 3.6.5-5~16.04.york0 python3.6-minimal (3.6.3-1ubuntu1~16.04.york1) to 3.6.5-5~16.04.york0 python3.6-venv (3.6.3-1ubuntu1~16.04.york1) to 3.6.5-5~16.04.york0 Commit Log for Fri May 4 14:32:44 2018 Upgraded the following packages: python3-gdbm (3.6.3-0ubuntu1~16.04.york0) to 3.6.5-3~16.04.york0.2 Commit Log for Sun May 6 09:28:25 2018 Installed the following packages: python3-tk (3.6.5-3~16.04.york0.2) Commit Log for Sun May 6 11:42:31 2018 Installed the following packages: libexpat1-dev (2.1.0-7ubuntu0.16.04.3) libpython3-dev (3.5.1-3) libpython3.5-dev (3.5.2-2ubuntu0~16.04.4) python3-dev (3.5.1-3) python3.5-dev (3.5.2-2ubuntu0~16.04.4) (2) jfb@jims-mint18 ~ $ python3 Python 3.5.2 (default, Nov 23 2017, 16:37:01) [GCC 5.4.0 20160609] on linux Type "help", "copyright", "credits" or "license" for more information. >>> import tkinter Traceback (most recent call last): File "/usr/lib/python3.5/tkinter/__init__.py", line 36, in import _tkinter ImportError: No module named '_tkinter' During handling of the above exception, another exception occurred: Traceback (most recent call last): File "", line 1, in File "/usr/lib/python3.5/tkinter/__init__.py", line 38, in raise ImportError(str(msg) + ', please install the python3-tk package') ImportError: No module named '_tkinter', please install the python3-tk package >>> -- https://mail.python.org/mailman/listinfo/python-list
Python 3.6 causes error, python 3.5 does not.
Mint 18 Libreoffice 5.1.6.2 Python 3.6.5 in one virtual environment Python 3.5.2 in another I am writing a script that uses pyautogui to get some data and paste it into a Libreoffice calc file, there by bypassing the complexity of uno. The problem is it runs fine if I use python 3.5. If I use python 3.6 it opens the calc file then pops up a dialog saying "std::bad_alloc". There are no relevant errors in the terminal. At this point I must reload the file and let calc recover it. I googled "std::bad_alloc" but didn' This is the portion of the code that causes the error: import pyautogui import subprocess import threading import time LONG_ENOUGH = 5 class LO(): '''Manipulate libreoffice not using UNO''' def __init__(self): self.filename = '/home/jfb/Documents/Financial/Investing/Stocks.ods' opener = threading.Thread(target=self.open_it, args=(self.filename,)) opener.start() time.sleep(5) #LONG_ENOUGH) #self.manipulate_LO() #Open and work with libreoffice def open_it(self, filename): #subprocess.call(["libreoffice", filename]) subprocess.run(["libreoffice", filename]) lo = LO() To complicate matters not all .ods files show this problem. I ran some tests. Two of my .ods files are effected but a couple of others are not. A new test file I created is not effected. I don't know if I have a Libreoffice problem, file corruption or a Python problem. The fact that 3.6 gives an error but 3.5 does not is the reason I decided to ask here first. Regards, Jim -- https://mail.python.org/mailman/listinfo/python-list
Re: Python 3.6 causes error, python 3.5 does not.
On 05/20/2018 02:03 PM, Jim wrote: Mint 18 Libreoffice 5.1.6.2 Python 3.6.5 in one virtual environment Python 3.5.2 in another I am writing a script that uses pyautogui to get some data and paste it into a Libreoffice calc file, there by bypassing the complexity of uno. The problem is it runs fine if I use python 3.5. If I use python 3.6 it opens the calc file then pops up a dialog saying "std::bad_alloc". There are no relevant errors in the terminal. At this point I must reload the file and let calc recover it. I googled "std::bad_alloc" but didn' This is the portion of the code that causes the error: import pyautogui import subprocess import threading import time LONG_ENOUGH = 5 class LO(): '''Manipulate libreoffice not using UNO''' def __init__(self): self.filename = '/home/jfb/Documents/Financial/Investing/Stocks.ods' opener = threading.Thread(target=self.open_it, args=(self.filename,)) opener.start() time.sleep(5) #LONG_ENOUGH) #self.manipulate_LO() #Open and work with libreoffice def open_it(self, filename): #subprocess.call(["libreoffice", filename]) subprocess.run(["libreoffice", filename]) lo = LO() To complicate matters not all .ods files show this problem. I ran some tests. Two of my .ods files are effected but a couple of others are not. A new test file I created is not effected. I don't know if I have a Libreoffice problem, file corruption or a Python problem. The fact that 3.6 gives an error but 3.5 does not is the reason I decided to ask here first. Regards, Jim To follow up. I just discovered the error only happens if I start the script from inside the 3.6 virtual environment. If I start it from another terminal with the full path to the 3.6 VE and the script it runs without error. Regards, Jim -- https://mail.python.org/mailman/listinfo/python-list
Looking for a Scrapy cheatsheet
I'm in the process of learning Scrapy. I've read through the docs and a couple of tutorials, but I am getting bogged down because I can't find a page/table/chart that gives a nice concise overview of the available commands and methods. Googling hasn't found anything usable. So does anyone know of a cheatsheet I can download. Thanks, Jim -- https://mail.python.org/mailman/listinfo/python-list
Re: Looking for a Scrapy cheatsheet
On 09/14/2018 01:27 AM, Danyelle Davis wrote: The one that sans provides seems pretty decent. Did you not like it? What is sans? Do you have a url. Thanks, Jim On Thu, Sep 13, 2018 at 4:05 PM Jim wrote: I'm in the process of learning Scrapy. I've read through the docs and a couple of tutorials, but I am getting bogged down because I can't find a page/table/chart that gives a nice concise overview of the available commands and methods. Googling hasn't found anything usable. So does anyone know of a cheatsheet I can download. Thanks, Jim -- https://mail.python.org/mailman/listinfo/python-list -- https://mail.python.org/mailman/listinfo/python-list
Re: Looking for a Scrapy cheatsheet
On 09/14/2018 08:15 AM, Jim wrote: On 09/14/2018 01:27 AM, Danyelle Davis wrote: The one that sans provides seems pretty decent. Did you not like it? What is sans? Do you have a url. Thanks, Jim Nevermind. I googled scrapy sans and I think we are talking about two different programs with the same name. I was inquiring about the web scraping program Scrapy. The one at SANS seems to be about penetration testing. Regards, Jim On Thu, Sep 13, 2018 at 4:05 PM Jim wrote: I'm in the process of learning Scrapy. I've read through the docs and a couple of tutorials, but I am getting bogged down because I can't find a page/table/chart that gives a nice concise overview of the available commands and methods. Googling hasn't found anything usable. So does anyone know of a cheatsheet I can download. Thanks, Jim -- https://mail.python.org/mailman/listinfo/python-list -- https://mail.python.org/mailman/listinfo/python-list
Re: Python3 packages installation
On 10/10/2018 03:05 PM, Rich Shepard wrote: Here, both python-2.7.15 and python3-3.6.6 are installed. Using pip (which I understand works with both python versions) I was able to install ipython, matplotlib, numpy, and pandas. They're all in /usr/lib64/python2.7/site-packages. Now I want to install the same packages for python3 so they are are found also in /usr/lib64/python3.6/site-packages. I've not found the proper command line syntax to do this. When I try, for example, 'python3 -m pip install numpy3' the system tells me that pip is not available. I want to learn how to manage both series since some of the applications I regularly use require python2 while any python scripts I now write use python3. If there's a document or web site that explains how to manage both versions please point me to it. Rich I think you have to first install a package called 'python3-pip'. At least that's what I had to do on Ubuntu. Regards, Jim -- https://mail.python.org/mailman/listinfo/python-list
Re: why no camelCase in PEP 8?
On 5/19/20 1:24 PM, Chris Angelico wrote: On Wed, May 20, 2020 at 4:03 AM Schachner, Joseph wrote: And, because '_' looks sort of like a space, the individual words are more easily readable. notEveyoneThinksReadingCamelCaseIsEasy. Me: "What does casels mean?" *beat* Me: "Well, I guess that's the point then, isn't it." ChrisA Couldn't resist: Why not Chris_A :) Jim -- https://mail.python.org/mailman/listinfo/python-list
Re: why no camelCase in PEP 8?
On 5/19/20 3:49 PM, Chris Angelico wrote: On Wed, May 20, 2020 at 6:38 AM Jim wrote: On 5/19/20 1:24 PM, Chris Angelico wrote: On Wed, May 20, 2020 at 4:03 AM Schachner, Joseph wrote: And, because '_' looks sort of like a space, the individual words are more easily readable. notEveyoneThinksReadingCamelCaseIsEasy. Me: "What does casels mean?" *beat* Me: "Well, I guess that's the point then, isn't it." ChrisA Couldn't resist: Why not Chris_A :) Heh. Actually a fair question! It's actually been my shorthand name for many many years, and originally I used the username "chrisa" since that was the email convention at the company. I later started uppercasing it to avoid being thought of as a "Christa" or somesuch, and wanted to remain compatible with the existing address, so case changes were all I'd do. But if I were to identify myself in a more Pythonic way, it would be Chris_Angelico. Backward compatibility trumps PEP 8. ChrisA Fair point. Jim -- https://mail.python.org/mailman/listinfo/python-list
Does this dataframe look correct?
linux mint 19.3, python 3.6 I wrote a program to download stock info from yahoo using yfinance. I have been running it unchanged for the past 3 months, today it gave an error. When looping through a list of stocks the error is random, never the same position in the list. I wrote the following little test script to show the error: import yfinance as yf import pandas as pd day = '2020-06-25' aapl = yf.Ticker('AAPL') hist = aapl.history(start=day) print(hist) close = hist.loc[day]['Close'] I ran it 10 times 8 times I got a dataframe and 2 times I got the error shown below: (env36) jfb@jims-mint18 ~ $ /home/jfb/EVs/env36/bin/python3 /home/jfb/Dev/Python/test_yfinance.py OpenHigh Low CloseVolume Date 2020-06-25 360.70 365.00 357.57 364.84 34380600 2020-06-26 364.41 365.32 353.02 353.63 51270100 (env36) jfb@jims-mint18 ~ $ /home/jfb/EVs/env36/bin/python3 /home/jfb/Dev/Python/test_yfinance.py Traceback (most recent call last): File "/home/jfb/Dev/Python/test_yfinance.py", line 13, in hist = aapl.history(start=day) File "/home/jfb/EVs/env36/lib/python3.6/site-packages/yfinance/base.py", line 155, in history data = data.json() File "/home/jfb/EVs/env36/lib/python3.6/site-packages/requests/models.py", line 897, in json return complexjson.loads(self.text, **kwargs) File "/home/jfb/EVs/env36/lib/python3.6/site-packages/simplejson/__init__.py", line 518, in loads return _default_decoder.decode(s) File "/home/jfb/EVs/env36/lib/python3.6/site-packages/simplejson/decoder.py", line 370, in decode obj, end = self.raw_decode(s) File "/home/jfb/EVs/env36/lib/python3.6/site-packages/simplejson/decoder.py", line 400, in raw_decode return self.scan_once(s, idx=_w(s, idx).end()) simplejson.errors.JSONDecodeError: Expecting value: line 1 column 1 (char 0) I don't know pandas that well. My only contact with it is when a module I am using depends on it. So does the dataframe look correct? The error complains of line 1, column 1. Just looking at the dataframe it looks like Date is on a different line from the rest of the headers or is that just the result of being printed in the terminal? On the yfinance github issues page there were a few people reporting this error. A couple of people reported a work around using try/except. It worked for some people and not others. It didn't work for me. I'd appreciate any advice you could give. Thanks, Jim -- https://mail.python.org/mailman/listinfo/python-list
Re: Does this dataframe look correct?
On 6/28/20 8:53 PM, MRAB wrote: On 2020-06-28 23:11, Jim wrote: linux mint 19.3, python 3.6 I wrote a program to download stock info from yahoo using yfinance. I have been running it unchanged for the past 3 months, today it gave an error. When looping through a list of stocks the error is random, never the same position in the list. I wrote the following little test script to show the error: import yfinance as yf import pandas as pd day = '2020-06-25' aapl = yf.Ticker('AAPL') hist = aapl.history(start=day) print(hist) close = hist.loc[day]['Close'] I ran it 10 times 8 times I got a dataframe and 2 times I got the error shown below: (env36) jfb@jims-mint18 ~ $ /home/jfb/EVs/env36/bin/python3 /home/jfb/Dev/Python/test_yfinance.py Open High Low Close Volume Date 2020-06-25 360.70 365.00 357.57 364.84 34380600 2020-06-26 364.41 365.32 353.02 353.63 51270100 (env36) jfb@jims-mint18 ~ $ /home/jfb/EVs/env36/bin/python3 /home/jfb/Dev/Python/test_yfinance.py Traceback (most recent call last): File "/home/jfb/Dev/Python/test_yfinance.py", line 13, in hist = aapl.history(start=day) File "/home/jfb/EVs/env36/lib/python3.6/site-packages/yfinance/base.py", line 155, in history data = data.json() File "/home/jfb/EVs/env36/lib/python3.6/site-packages/requests/models.py", line 897, in json return complexjson.loads(self.text, **kwargs) File "/home/jfb/EVs/env36/lib/python3.6/site-packages/simplejson/__init__.py", line 518, in loads return _default_decoder.decode(s) File "/home/jfb/EVs/env36/lib/python3.6/site-packages/simplejson/decoder.py", line 370, in decode obj, end = self.raw_decode(s) File "/home/jfb/EVs/env36/lib/python3.6/site-packages/simplejson/decoder.py", line 400, in raw_decode return self.scan_once(s, idx=_w(s, idx).end()) simplejson.errors.JSONDecodeError: Expecting value: line 1 column 1 (char 0) I don't know pandas that well. My only contact with it is when a module I am using depends on it. So does the dataframe look correct? The error complains of line 1, column 1. Just looking at the dataframe it looks like Date is on a different line from the rest of the headers or is that just the result of being printed in the terminal? On the yfinance github issues page there were a few people reporting this error. A couple of people reported a work around using try/except. It worked for some people and not others. It didn't work for me. I'd appreciate any advice you could give. It's complaining about the JSON data that it's getting. What does that data look like when it complains? It might be that there's some kind of limit to how often you can get the data and it's trying to tell you that, but you're not expecting anything back except the data. You could add some temporary code at line 897 of "/home/jfb/EVs/env36/lib/python3.6/site-packages/requests/models.py" to save the data to a file just before the decoding. Remember to make a backup copy of any source file that you modify! I don't think it is a limit problem. It happened the first time I ran the script after a week of not using it. I am only geting info on 33 stocks and I know that people use this module to get info on 100's of stocks. Anyway I was wrong about the try/except not solving the problem. I made a mistake in the try/except and when I corrected it, like a dummy, I never saved the change before running it again. I will use your suggestions to see if I can figure out the root cause of the problem. as before today it ran for months with no errors. Sorry for taking up the lists time with my mistake and thanks for your help. Regards, Jim -- https://mail.python.org/mailman/listinfo/python-list
Re: Does this dataframe look correct?
On 6/29/20 2:16 AM, Peter Otten wrote: Jim wrote: linux mint 19.3, python 3.6 I wrote a program to download stock info from yahoo using yfinance. I have been running it unchanged for the past 3 months, today it gave an error. When looping through a list of stocks the error is random, never the same position in the list. I wrote the following little test script to show the error: import yfinance as yf import pandas as pd day = '2020-06-25' aapl = yf.Ticker('AAPL') hist = aapl.history(start=day) print(hist) close = hist.loc[day]['Close'] I ran it 10 times 8 times I got a dataframe and 2 times I got the error shown below: (env36) jfb@jims-mint18 ~ $ /home/jfb/EVs/env36/bin/python3 /home/jfb/Dev/Python/test_yfinance.py OpenHigh Low CloseVolume Date 2020-06-25 360.70 365.00 357.57 364.84 34380600 2020-06-26 364.41 365.32 353.02 353.63 51270100 (env36) jfb@jims-mint18 ~ $ /home/jfb/EVs/env36/bin/python3 /home/jfb/Dev/Python/test_yfinance.py Traceback (most recent call last): File "/home/jfb/Dev/Python/test_yfinance.py", line 13, in hist = aapl.history(start=day) File "/home/jfb/EVs/env36/lib/python3.6/site-packages/yfinance/base.py", line 155, in history data = data.json() File "/home/jfb/EVs/env36/lib/python3.6/site-packages/requests/models.py", line 897, in json return complexjson.loads(self.text, **kwargs) File "/home/jfb/EVs/env36/lib/python3.6/site-packages/simplejson/__init__.py", line 518, in loads return _default_decoder.decode(s) File "/home/jfb/EVs/env36/lib/python3.6/site-packages/simplejson/decoder.py", line 370, in decode obj, end = self.raw_decode(s) File "/home/jfb/EVs/env36/lib/python3.6/site-packages/simplejson/decoder.py", line 400, in raw_decode return self.scan_once(s, idx=_w(s, idx).end()) simplejson.errors.JSONDecodeError: Expecting value: line 1 column 1 (char 0) I don't know pandas that well. My only contact with it is when a module I am using depends on it. So does the dataframe look correct? The error complains of line 1, column 1. Just looking at the dataframe it looks like Date is on a different line from the rest of the headers or is that just the result of being printed in the terminal? On the yfinance github issues page there were a few people reporting this error. A couple of people reported a work around using try/except. It worked for some people and not others. It didn't work for me. I'd appreciate any advice you could give. My guess is that pandas is not the source of the problem. The error occurs when simplejson tries to parse an empty string: import simplejson simplejson.loads("") Traceback (most recent call last): File "", line 1, in File "/usr/lib/python3/dist-packages/simplejson/__init__.py", line 488, in loads return _default_decoder.decode(s) File "/usr/lib/python3/dist-packages/simplejson/decoder.py", line 370, in decode obj, end = self.raw_decode(s) File "/usr/lib/python3/dist-packages/simplejson/decoder.py", line 389, in raw_decode return self.scan_once(s, idx=_w(s, idx).end()) simplejson.scanner.JSONDecodeError: Expecting value: line 1 column 1 (char 0) This probably means that yahoo returns an empty string instead of the expected JSON. If the error occurs only sporadically and you can identify the downloading code in the API source you can try and replace (pseudo-code) json_data = download_from_yahoo() df = dataframe_from_json(json_data) with while True: json_data = download_from_yahoo() if json_data: break time.sleep(1) # wait a moment, then retry download df = dataframe_from_json(json_data) Thanks, I have it working now by wrapping a for loop in a try/except. When I get a chance I will use yours and MRAB's suggestions to try to figure out just what caused the problem. Regards, Jim -- https://mail.python.org/mailman/listinfo/python-list
Re: A rule for your twitlist/mailing list
On 7/14/20 9:51 PM, Grant Edwards wrote: On 2020-07-15, Cameron Simpson wrote: On 14Jul2020 08:49, Nomen Nescio wrote: Is the mailing list for comp.lang.python still open? If you mean the python-list mailing list, yes. It is what I use, and it does not suffer from the spam you describe. Here: https://mail.python.org/mailman/listinfo/python-list It gateways with the newsgroup, but has much better spam qualities. And if you prefer to read the mailing list with an NNTP client, you can point your favorite newsreader at news.gmane.org and subscribe to gmane.comp.python.general -- Grant I think that should now be news.gmane.io, at least that's how I get comp.python. I think gmane.org shut down. Regards, Jim -- https://mail.python.org/mailman/listinfo/python-list
Set up Windows environment with python
My company resells windows machines, and we install our software, and do a bunch of customization to make sure that all the desktop settings are optimal... we adjust the screen resolution, color depth, and referesh rate, remove shadows from menus and the mouse pointer, set the power management options, disable the screensaver, etc... (very long list) I've started doing most of what I want with ctypes: def disableShadows(): # constants taken from WinUser.h in the PlatformSDK SPI_SETCURSORSHADOW = 0x101B rv = ctypes.windll.user32.SystemParametersInfoA(SPI_SETCURSORSHADOW, 0, False, win32con.SPIF_SENDWININICHANGE) # remove shadows from menus SPI_SETDROPSHADOW = 0x1025 rv = ctypes.windll.user32.SystemParametersInfoA(SPI_SETDROPSHADOW, 0, False, win32con.SPIF_SENDWININICHANGE) But I'm finding that none of the changes seem to be permanent. I'm wondering if I need to start changing the current Explorer theme information instead, or go right to the registry and start changing values. Desktop wallpaper changes work, but they aren't permanent... rebooting restores the previous desktop wallpaper. Does anyone have any experience with this sort of system preperation scripting? Thanks, -Jim -- http://mail.python.org/mailman/listinfo/python-list
Re: Set up Windows environment with python
Thanks Thomas! That did it. I can now set the wallpaper, mouse shadows, menu shadows, and I can disable the screensaver. Does anyone know how I can adjust the power options? I want to make sure any hibernate or standby options are set to Never. Thanks, -Jim -- http://mail.python.org/mailman/listinfo/python-list
MS Word Outline -> reStructuredText script?
Hi, I'm using reStructuredText as a format for some group documentation, and often my co-workers take notes during meetings in Word's outline mode. Does anyone already have a python script that will convert from Word (or the Open Office file format version of a word document) to reStructured Text? If not I've got a fun project ahead of me. Thanks, -Jim -- http://mail.python.org/mailman/listinfo/python-list
Re: MS Word Outline -> reStructuredText script?
I'll look into that. I also found that Open Office can read the .doc file and then saveAs a DocBook xml file. I think DocUtils can parse and write both DocBook and reStructured text, but It'll take me some time to experiment with it. David Mertz has an article that shows reSt -> DocBook, I just need to do the opposite. http://www-128.ibm.com/developerworks/library/x-matters24/?ca=dnt-45 -Jim -- http://mail.python.org/mailman/listinfo/python-list
urlencode with high characters
Hello, I'm trying to do urllib.urlencode() with unicode correctly, and I wonder if some kind person could set me straight? My understanding is that I am supposed to be able to urlencode anything up to the top half of latin-1 -- decimal 128-255. I can't just send urlencode a unicode character: Python 2.3.5 (#2, May 4 2005, 08:51:39) [GCC 3.3.5 (Debian 1:3.3.5-12)] on linux2 Type "help", "copyright", "credits" or "license" for more information. >>> import urllib >>> s=u'abc'+unichr(246)+u'def' >>> dct={'x':s} >>> urllib.urlencode(dct) Traceback (most recent call last): File "", line 1, in ? File "/usr/lib/python2.3/urllib.py", line 1206, in urlencode v = quote_plus(str(v)) UnicodeEncodeError: 'ascii' codec can't encode character u'\xf6' in position 3: ordinal not in range(128) Is it instead Right that I should send a unicode string to urlencode by first encoding it to 'latin-1' ? >>> import urllib >>> s=u'abc'+unichr(246)+u'def' >>> dct={'x':s.encode('latin-1')} >>> urllib.urlencode(dct) 'x=abc%F6def' If it is Right, I'm puzzled as to why urlencode doesn't do it. Or am I missing something? urllib.ulrencode() contains the lines: elif _is_unicode(v): # is there a reasonable way to convert to ASCII? # encode generates a string, but "replace" or "ignore" # lose information and "strict" can raise UnicodeError v = quote_plus(v.encode("ASCII","replace")) l.append(k + '=' + v) so I think that it is *not* liking latin-1. Thank you, Jim -- http://mail.python.org/mailman/listinfo/python-list
Re: lists <-> tuple
> Tuples or lists for matrix-like functionality? Use lists. Tuples are meant for small immutable sets of things that go together. Lists are more like arrays, and you can assign to one existing element if you want. One exception, is a short vector is often a tuple like (x, y, z) and you might want to multiply that vector by your matrix. You can convert a tuple to a list with list(aTuple) or back with tuple(aList.) Even better, take a look at numarray (or numpy or scipy or scipy_core.) They all have really nice matrix code and there are C APIs that let you manipulate them. Chances are they do everything you're intending to implement. Immutability example: tup = ("a", "b", "c") tup[1] = "g" Traceback (most recent call last): File "", line 1, in ? TypeError: object does not support item assignment lst = ["a", "b", "c"] lst[1] = "g" lst ['a', 'g', 'c'] -Jim -- http://mail.python.org/mailman/listinfo/python-list
Re: generate HTML
Perhaps you are trying to do this: 'text to go here: %s' % ('text',) ? For that you need a double-quoted string: "text to go here: %s" % ('text',) (or triple-doubles: """ .. """ as you noted). Jim -- http://mail.python.org/mailman/listinfo/python-list
zipfile and soft links
Hello, I'm trying to unzip archives that have soft links in them (that is, were created on unix with a -y option). I'm not finding that ZipFile will do that. That I can see, it creates the link file, and that file has the right contents, but it is not a soft link in the directory structure. (I've both tested it interactively and looked at the zipfile.py source, and didn't see the behaviour in either place.) If I have that wrong, I'd greatly appreciate it if some kind soul could correct me. If it does not do soft links, I wonder if someone has tried patching it? That I can see the specs are on the web, at ftp://ftp.info-zip.org/pub/infozip/doc/appnote-iz-latest.zip , so I wondered if someone who is a better programmer than I (not hard!) had already done it. The alternative is to shell out, which happens to be quite involved in the context that I am using (e.g., what if the soft link is to '/bin/ls' ?). Thanks, Jim Hefferon -- http://mail.python.org/mailman/listinfo/python-list
Images & Connected Component labeling
Does anyone know where I can find a 'connected component' image processing function? I don't see it in numarray or scipy core... In matlab the function is called bwlabel(bw,4); and step 8 on this page is an example of what I'm trying to do: http://www.mathworks.com/access/helpdesk_r13/help/toolbox/images/getting3.html Thanks, -Jim -- http://mail.python.org/mailman/listinfo/python-list
Re: newby question: Splitting a string - separator
Hi Tom, > a regex for "more than one whitespace". RegEx for whitespace is \s, but > what would i use for "more than one"? \s+? For more than one, I'd use \s\s+ -Jim -- http://mail.python.org/mailman/listinfo/python-list
Re: Upgraded to python2.3 but httpd taking old version
Gurpreet Sachdeva wrote: > I upgraded my python to 2.3 from 2.2 but > [Tue Jan 11 16:18:45 2005] [error] [client 127.0.0.1] import cgi > [Tue Jan 11 16:18:45 2005] [error] [client 127.0.0.1] File > "/usr/lib/python2.2/cgi.py", line 38, in ? Does the first line of your file say #!/usr/bin/python2.3 (or whatever is appropriate for your system)? Jim -- http://mail.python.org/mailman/listinfo/python-list
Re: script to automate GUI application (newbie)
It sounds like a case for the Expect program, to me. Try Google-ing for "Expect". If you are looking for a Python approach, then try googling for "Expect Python". Jim -- http://mail.python.org/mailman/listinfo/python-list
Re: create a pdf file
If it is a plain text file (or close to it, like the Gutenburg Project files), think about using pdfLaTeX. Jim -- http://mail.python.org/mailman/listinfo/python-list
Re: Python Search Engine app
I am finding this discussion illuminating. Can I ask: does it possibly make sense to use a dB? For an application of mine, I had been looking at putting the text into PostGres, and using their OpenFTS, at //http://openfts.sourceforge.net/ (there is a Python module to talk to OpenFTS, although it is in an early stage of development). Jim -- http://mail.python.org/mailman/listinfo/python-list
Loop in list.
Where did this type of structure come from: mat = ['a' for i in range(3)]? This will produce a list of three elements but I don't see reference for it in any of the books. -- http://mail.python.org/mailman/listinfo/python-list
Re: Loop in list.
Thanks for the help. Python is somewhat ïmysterious to an old fortan programer. Jim -- http://mail.python.org/mailman/listinfo/python-list
Re: Loop in list.
Particularly one who can't spell. Fortran. Jim -- http://mail.python.org/mailman/listinfo/python-list
Re: Loop in list.
Wow! All I wanted to do was write the equivalence of the Fortran statement: Real*4 matrix(n,n). I'm going to have to go to the intrepreter to see what your saying. Thanks for all the help. Jim -- http://mail.python.org/mailman/listinfo/python-list
Re: Loop in list.
I did appreciate the reference. I started with Fortran on an IBM (7040 maybe, not sure) using keypunched cards. Some of the concepts of the newer languages take some to seem useable. Jim -- http://mail.python.org/mailman/listinfo/python-list
Re: Loop in list.
I assume this is one of the addons for Python. I know that there is a great deal of stuff out there available for Python that does some of the stuff that I am looking at, but I am interested in learning to use Python. When I want to get faster and more general, I will get some of this stuff or use a different language. Thanks for the help. Jim -- http://mail.python.org/mailman/listinfo/python-list
beeping portably
Hello, I'd like to emit beeps. The twists are that (1) I hope to have control over the frequency of the beeps and their duration and (2) I'd like the solution to be portable across Linux, Windows, and OS X. I've done some searching of this group and the solutions that people have offered in the past seem not to satisfy both points. For instance, that I can tell, "\a" or curses.beep() doesn't give you control over the frequency or duration. I also looked at wxWindows but there were warnings about the MMedia module ("status is unclear") that made me leery. Thanks, Jim -- http://mail.python.org/mailman/listinfo/python-list
Re: Python for a 10-14 years old?
My kids like http://www.alice.org (although they run it under Windows). Jim Hefferon -- http://mail.python.org/mailman/listinfo/python-list
Re: Archives and magic bytes
This is something I've recently thought about; perhaps you wouldn't mind some points? 1) I've been running 'file' via os.popen, and I've had trouble with it incorrectly spotting file types (Fedora Core 1). I can name a specific example where it thinks a plain text README file is HTML (despite that the configuration file for 'file' at least looks right). That makes me suspicious of its ability to spot more obscure types. (No, I haven't tried to get the latest 'file'; the days are long but they are filled with negative time and in the end I don't always get done what I should.) 2) Watch out for someone giving you, say, a bogus /bin/ls in a .zip file. You may want to look into chroot (which I believe requires you to run as root), or at least examine the output of "unzip -l" 3) You might also have to worry about the possibility that unpacking a bundle will fill up your disk's partition. At least for a while you hold both the bundle and the unpacked bundle. 4) Using os.popen to unpack the bundle has a lot of advantages, including that during debugging you can test the stuff from the command line and feel that you completely understand which steps are working (I think I use popen2, IIRC, and capture stderr for error messages). Perhaps this is mostly a reflection on me as a programmer :-} but I found the job surprisingly tricky. Jim -- http://mail.python.org/mailman/listinfo/python-list
import and scope inconsistency?
I would be very grateful for help on the following. I have the following modules in a program. Names changed to protect the innocent. 1.Simulation 2.Branches 3.MyFiles I import Branches, Myfiles and the publicly available module Numeric inside Simulation. Branches and MyFiles both contain class definitions. I can call MyFiles methods inside methods of the Brances classes. I cannot call Numeric methods inside methods of the Brances classes. 1. I was surprised I could call MyFiles methods in Branches methods. 2. Since I was used to using modules imported in the parent module I was surprised I couldn't use Numeric methods. What's going on? Thanks Jim O'Donnell -- http://mail.python.org/mailman/listinfo/python-list
Re: Raise Error in a Module and Try/Except in a different Module
Issa-Ahmed SIDIBE wrote: I Have a function FUNC1 that is define in ModuleA. This function raise an exception EXCP1 (raise EXCP1), with EXCP1 a global variable in ModuleA. In ModuleB, I have some classes that call FUNC1. I would like to catch EXCP1 and make some processing. How can I do that. I tried in Module B import ModuleA ... class(): ... try: a = ModuleA.FUNC1 except ModuleA.EXCP1: print 'catch' But It does not work. What is wrong? I'm just back from holiday so rusty but I tried to do this w few weeks ago. What's the error? Jim -- http://mail.python.org/mailman/listinfo/python-list
Re: Raise Error in a Module and Try/Except in a different Module
You're not *calling* FUNC1 here, you're just assigning a reference to it to the name 'a'. Try FUNC1() instead. Oh yeah. -- http://mail.python.org/mailman/listinfo/python-list
Re: import and scope inconsistency?
Heiko Wundram wrote: You're putting a Reply-To header in your posts to the mailing-list, but the Reply-To address bounces. Please correct: on't put in a Reply-To header, or at least put in some address that doesn't bounce. Thanks -- http://mail.python.org/mailman/listinfo/python-list
A beginer question about SOAP and Python: : {}
Hi all, I am new to SOAP and Python. I am practicing learning SOAP with Python. I sent a request and I got the following response: : {} What does that mean? and how can I print the result hash string ? Please reply to the group. Thanks for your help. Jim. -- http://mail.python.org/mailman/listinfo/python-list
How to debug SOAP using TCpMon and Python?
Hello, I am trying to debug a Python SOAP application using tcpmon. I am wondering what listen port, target port number and host address should I use. What about optional parameters: Http Proxy support, host and port? My PC is behind a firewall. Thanks for your help. Jim -- http://mail.python.org/mailman/listinfo/python-list
Re: numarray.array can be VERY slow
Edward C. Jones wrote: Steven Bethard wrote: > As mentioned, this has nothing to do with numarray, and everything to > do with your inexplicable use of lists. Why don't you just write this > as: > > arr = numarray.ones((8, 8, 256, 256), Float64) The code I posted was simplified from a larger program which I have now revised. But I still ask: why did it take 4.3 seconds to run? Is the simple answer not that repeated use of array.append is inefficient as python has to repeatedly re-allocate memory for the array? I had a similar problem as I ran a simulation and appended the results to a results set. Now I create a list of objects of the size required and the overwrite them. Much faster :) Jim Getting in there late cos I've been skiving -- http://mail.python.org/mailman/listinfo/python-list
Re: how to explain such codes, python's bug or mine?
MaHahaXixi wrote: j = range(20) print j [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19] for k in j: if k <= 10: j.remove(k) print j [1, 3, 5, 7, 9, 11, 12, 13, 14, 15, 16, 17, 18, 19] Python 2.3.4 (#53, May 25 2004, 21:17:02) [MSC v.1200 32 bit (Intel)] on win32 Type "copyright", "credits" or "license()" for more information. i think python do convert there codes to such style: for (i = 0; i < len(j); i++) k = j[i] .. what do u think? I'm not quite sure of your question but with the second style you're not attempting to change the original list but make a copy. That's perfectly easy to do in Python as it is. The exampmle is a cautionary one about changing the list on which you are iterating. Jim -- http://mail.python.org/mailman/listinfo/python-list
terminate exectutioin in PythonWin
Hi all 1.Could someone tell me how to terminate execution in PythonWin? 2.Can anyone recommend a free development environment in which you can save your workspace (e.g. open files, debugging breakpoints etc.) Jim -- http://mail.python.org/mailman/listinfo/python-list
Re: terminate exectutioin in PythonWin
Larry Bates wrote: 1) To exit any application: import sys sys.exit(0) ... Thanks Larry. Actually I meant how do I stop a program running that I have started in the interpreter. If I get into an infinite loop I can't stop it without qutting PythonWin. Then I have to open all the files and re-insert breakpoints etc. :( Jim -- http://mail.python.org/mailman/listinfo/python-list
preallocate list
Hi all Is this the best way to preallocate a list of integers? listName = range(0,length) What about non integers? I've just claimed in the newsgroup above that pre-allocating helps but I might be getting confused with matlab ;) If I have a file with a floating point number on each line, what is the best way of reading them into a list (or other ordered structure)? I was iterating with readline and appending to a list but it is taking ages. Jim -- http://mail.python.org/mailman/listinfo/python-list
Re: preallocate list
rbt wrote: Jim wrote: If I have a file with a floating point number on each line, what is the best way of reading them into a list (or other ordered structure)? I was iterating with readline and appending to a list but it is taking ages. Perhaps you should use readlines (notice the s) instead of readline. I don't know if I thought of that, but I'm tokenizing each line before adding to a list of lists. for line in f: factor = [] tokens = line.split() for i in tokens: factor.append(float(i)) factors.append(factor) Is this nasty? Jim -- http://mail.python.org/mailman/listinfo/python-list
Re: preallocate list
Thanks for the suggestions. I guess I must ensure that this is my bottle neck. def readFactorsIntoList(self,filename,numberLoads): factors = [] f = open(self.basedir + filename,'r') line = f.readline() tokens = line.split() columns = len(tokens) if int(columns) == number: for line in f: factor = [] tokens = line.split() for i in tokens: factor.append(float(i)) factors.append(loadFactor) else: for line in f: tokens = line.split() factors.append([float(tokens[0])] * number) return factors OK. I've just tried with 4 lines and the code works. With 11000 lines it uses all CPU for at least 30 secs. There must be a better way. Jim -- http://mail.python.org/mailman/listinfo/python-list
Re: terminate exectutioin in PythonWin
Neil Hodgson wrote: Jim, 1.Could someone tell me how to terminate execution in PythonWin? Use the "Break into running code" command on the context menu of the PythonWin icon in the bottom right of the taskbar. Neil Great thanks! PythonWin isn't hot on keyboard shortcuts but ironically calls this a KeyboardInterrupt :| Know any keyboard shortcuts for debug view? e.g. open it, run(debug), step etc. Jim -- http://mail.python.org/mailman/listinfo/python-list
Re: preallocate list
Steven Bethard wrote: Jim wrote: Thanks for the suggestions. I guess I must ensure that this is my bottle neck. def readFactorsIntoList(self,filename,numberLoads): factors = [] f = open(self.basedir + filename,'r') line = f.readline() tokens = line.split() columns = len(tokens) if int(columns) == number: for line in f: factor = [] tokens = line.split() for i in tokens: factor.append(float(i)) factors.append(loadFactor) else: for line in f: tokens = line.split() factors.append([float(tokens[0])] * number) return factors OK. I've just tried with 4 lines and the code works. With 11000 lines it uses all CPU for at least 30 secs. There must be a better way. Was your test on *just* this function? Or were you doing something with the list produced by this function as well? Just this. I had a breakpoint on the return. I'm going to try peufeu's line of code and I'll report back. Jim -- http://mail.python.org/mailman/listinfo/python-list
Re: preallocate list
[EMAIL PROTECTED] wrote: what about : factors = [map(float, line.split()) for line in file] should be a hell of a lot faster and nicer. for line in f: factor = [] tokens = line.split() for i in tokens: factor.append(float(i)) factors.append(factor) Is this nasty? Jim Oh the relief :) Of course, line.split() is already a list. Couple of seconds for the 1 line file. Thanks. What I really want is a Numeric array but I don't think Numeric supports importing files. Jim -- http://mail.python.org/mailman/listinfo/python-list
Re: preallocate list
Steven Bethard wrote: Jim wrote: .. OK. I've just tried with 4 lines and the code works. With 11000 lines it uses all CPU for at least 30 secs. There must be a better way. Was your test on *just* this function? Or were you doing something with the list produced by this function as well? STeVe Well it's fast enough now. Thanks for having a look. Jim -- http://mail.python.org/mailman/listinfo/python-list
Re: terminate exectutioin in PythonWin
Steve Holden wrote: Jim wrote: [...] Actually I meant how do I stop a program running that I have started in the interpreter. If I get into an infinite loop I can't stop it without qutting PythonWin. Then I have to open all the files and re-insert breakpoints etc. :( Jim Look in the system tray! Right-mouse on the Python and select "break into running program". regards Steve D'oh. I didn't think of that. Thanks Jim -- http://mail.python.org/mailman/listinfo/python-list
Re: preallocate list
F. Petitjean wrote: Le Wed, 13 Apr 2005 16:46:53 +0100, Jim a écrit : What I really want is a Numeric array but I don't think Numeric supports importing files. Numeric arrays can be serialized from/to files through pickles : import Numeric as N help(N.load) help(N.dump) (and it is space efficient) Jim Yeah thanks. I'm generating them using Matlab though so I'd have to get the format the same. I use Matlab because I get the results I want. When I get to know Python + scipy etc. better I might remove that step. Thanks again Jim -- http://mail.python.org/mailman/listinfo/python-list
Re: preallocate list
Steven Bethard wrote: Jim wrote: What I really want is a Numeric array but I don't think Numeric supports importing files. Hmmm... Maybe the scipy package? I think scipy.io.read_array might help, but I've never used it. STeVe Sounds promising. I only got Numeric because I wanted scipy but I've hardly explored it as I kept running into problems even with the complicated examples cut and paste into a file ;) Oh yeah, I wanted to explore the GA module but no docs :( and I got busy doing other stuff. Thanks Jim -- http://mail.python.org/mailman/listinfo/python-list
Re: preallocate list
ivec = n*[None] so that if I use a list element before intializing it, for example ivec[0] += 1 I get an error message File "xxnone.py", line 2, in ? ivec[0] += 1 TypeError: unsupported operand type(s) for +=: 'NoneType' and 'int' This is in the same spirit as Python's (welcome) termination of a program when one tries to use an uninitalized scalar variable. I feel foolish that I forgot about *. I've just started with Python then took 2 weeks off. I'll explore pre-allocation when I'm back up to speed. Yep, I use None a lot. Thanks Jim -- http://mail.python.org/mailman/listinfo/python-list
Re: preallocate list
John Machin wrote: On Wed, 13 Apr 2005 14:28:51 +0100, Jim <[EMAIL PROTECTED]> wrote: Thanks for the suggestions. I guess I must ensure that this is my bottle neck. def readFactorsIntoList(self,filename,numberLoads): 1. "numberLoads" is not used. factors = [] f = open(self.basedir + filename,'r') line = f.readline() tokens = line.split() columns = len(tokens) if int(columns) == number: 2. "columns" is already an int (unless of course you've redefined "len"!). Doing int(columns) is pointless. 3. What is "number"? Same as "numberLoads"? 4. Please explain in general what is the layout of your file and in particular, what is the significance of the first line of the file and of the above "if" test. for line in f: factor = [] tokens = line.split() for i in tokens: factor.append(float(i)) 4. "factor" is built and then not used any more?? factors.append(loadFactor) 5. What is "loadFactor"? Same as "factor"? else: for line in f: tokens = line.split() factors.append([float(tokens[0])] * number) 6. You throw away any tokens in the line after the first?? return factors OK. I've just tried with 4 lines and the code works. Which code works? The code you posted? Please define "works". With 11000 lines it uses all CPU for at least 30 secs. There must be a better way. Perhaps after you post the code that you've actually run, and explained what your file layout is, and what you are trying to achieve, then we can give you some meaningful help. Cheers, John Thanks for looking John. For that I should take a little time to explain. I tried to rename the variables, some of them were four words long. I got a couple of the renames wrong. Sorry. Regarding 'works'. I meant that with a text file of four lines the code completed. With my desired size 11000 lines it didn't complete within the limits of my patience. I didn't try any other size. Also I perhaps wrongly use the newsgroup threads paradigm in trying to restart my query with extra information (that turned out a little faulty). Luckily the other branches yielded fruit. Thanks again Jim -- http://mail.python.org/mailman/listinfo/python-list
zipfile module and directories and symlinks
I'm using the zipfile module to unpack bundles. Is there a Right way to recognize directories, or do I just check if the name ends in '/' (i.e., os.sep)? That is, I have this. for bundleInfo in bundleObject.infolist(): ...stuff... if bundleInfo.filename[-1:]==os.sep: make a dir else make a file Is that right? What about symlinks? How do I know one of those? Thanks for any help, Jim -- http://mail.python.org/mailman/listinfo/python-list
Re: Quickest way to make py script Web accessible
[EMAIL PROTECTED] wrote: > What is the quickiest and easiest way to make a py script run on a Web > server? I have access to an Apache Web server running on Linux. > > I'm often asked to run some of my scripts on behalf of others. My hope > is to make a simple Web-based interface where people could run the > scripts themselves whenever they like... instead of asking me. > Perhaps I'm not understanding the question, but .. Can you write a CGI script that calls the desired program using Python's system call, like: os.system("/home/rtilley/cleanup.py") and then returns a little HTML saying that the cleanup was done at such-and-such a time? Is that what you are asking to do? Jim -- http://mail.python.org/mailman/listinfo/python-list
Re: ANN: Python training, 2006 Feb 1-3, San Francisco
On Wed, 11 Jan 2006, w chun wrote: ... > as promised, this is the FINAL reminder i'll send out about our > upcoming Python course at the beginning of February. Is it too much to ask that you refrain altogether from using these lists for advertising, with the possible exception of the local one? > it'll be at a hotel with BART and CalTrain access (San Bruno > stations) for those already in the Bay Area, and for those coming in > from out-of-town, there's a free shuttle directly from the San > Francisco airport, which is only about 2-3 miles away. > > discounts available for multiple registrants as well as students, > teachers, and those with financial hardship. also, there is a > [snip] >> Note: i will only send out ONE MORE REMINDER in January... yeah, i >> don't like spam either. :-) but this is a special occasion? >> >> cheers, >> -- wesley >> - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - >> "Core Python Programming", Prentice Hall, (c)2006,2001 >> http://corepython.com >> >> wesley.j.chun :: wescpy-at-gmail.com >> cyberweb.consulting : silicon valley, ca >> http://cyberwebconsulting.com > -- > http://mail.python.org/mailman/listinfo/python-announce-list > >Support the Python Software Foundation: >http://www.python.org/psf/donations.html > [EMAIL PROTECTED] SDF-EU Public Access UNIX System - http://sdf-eu.org -- http://mail.python.org/mailman/listinfo/python-list
Re: a more precise re for email addys
There is a precise one in a Perl module, I believe. http://www.ex-parrot.com/~pdw/Mail-RFC822-Address.html Can you swipe that? Jim -- http://mail.python.org/mailman/listinfo/python-list
Re: unicode html
Sybren Stuvel wrote: > [EMAIL PROTECTED] enlightened us with: > > > As an example I would like to do this kind of conversion: > > \uc3B4 => ô > > for all available html entities. > > Why would you want that? Just make sure you declare your document as > UTF-8, encode it as such, and you're done. Much easier. For example, I am programming a script that makes html pages, but I do not have the ability to change the "Content-Type .. charset=.." line that is sent preceeding those pages. Jim -- http://mail.python.org/mailman/listinfo/python-list
Re: unicode html
Sybren Stuvel wrote: > Jim enlightened us with: > > For example, I am programming a script that makes html pages, but I > > do not have the ability to change the "Content-Type .. charset=.." > > line that is sent preceeding those pages. > > "line"? Are you talking about the HTTP header? If it is wrong, it > should be corrected. If you are in control of the content, you should > also be control of the Content-Type header. Otherwise, use a > tag that describes the content. Ah, but I cannot change it. It is not my machine and the folks who own the machine perceive that the charset line that they use is the right one for them. (Many people ship pages off this machine.) Unfortunately, the tag idea also does not fly: see http://www.w3.org/TR/html4/charset.html in section 5.2.2 where it states that in a contest the charset parameter wins. My only point is that things are complicated and that there are times when HTML entities are the answer (or anyway, an answer). Jim -- http://mail.python.org/mailman/listinfo/python-list
Re: unicode html
Sybren Stuvel wrote: > Jim enlightened us with: > > Ah, but I cannot change it. It is not my machine and the folks who > > own the machine perceive that the charset line that they use is the > > right one for them. > > Well, _you_ are the one providing the content, aren't you? ? This site has many people operating off of it (it is sourceforge-like) and the operators (who are volunteers) are kind enough to let us use it in the first place. I presume that they think the charset line that they use is the one that most people want. Probably if they changed it then someone else would complain. > Sounds like they either don't know what they are talking about, or use > incompetent software. With Apache, it's very easy to give every > directory its own default character encoding header. I am operating under constraints. Asking the operators of the site has led to the understanding that I must work with the charset parameter that I have. That is, I have an environment in which I must work, and whether you or I think the people providing the service should do it differently doesn't matter. I replied originally because I thought I could give an example of HTML entities providing a way that I can solve the problem that is entirely under my control. > > Unfortunately, the tag idea also does not fly: see > > http://www.w3.org/TR/html4/charset.html in section 5.2.2 where it > > states that in a contest the charset parameter wins. > > I assume that with "the charset parameter" you mean "the HTTP header", > as the tag also has a "charset parameter". AIUI "charset parameter" is the language of the HTML standard that I referred to. For the meta tag, I at least would use "charset attribute". > > My only point is that things are complicated > > Call me thick, but from my point of view they aren't. ;-) Jim -- http://mail.python.org/mailman/listinfo/python-list
Re: Understanding Unicode & encodings
[EMAIL PROTECTED] wrote: > Hello, > > For my application, I would like to execute an SQL query like this: > self.dbCursor.execute("INSERT INTO track (name, nbr, idartist, idalbum, > path) VALUES ('%s', %s, %s, %s, '%s')" % (track, nbr, idartist, > idalbum, path)) No, I'll bet that you'd like to run something like self.dcCursor.execute("INSERT INTO track (name, nbr, idartist, idalbum,path) VALUES (%(track)s, %(nbr)s, %(idartist)s,%(idalbum)s,'%(path)s')", {'track':track,'nbr':nbr,'idartist':idartist,'idalbum':idalbum,'path':path}) (only without my typos). That's an improvment for a number of reasons, one of which is that the system will quote for you, for instance in idartist="John's Beer" changing the single quote to two single quotes to suit SQL. > Every time I execute this, I get an exception like > this: > > UnicodeDecodeError: 'ascii' codec can't decode byte 0xa1 in position > 64: ordinal not in range(128) > > I tried to encode the different variables in many different encodings > (latin1), but I always get an exception. Where does this ascii codec > error comes from? How can I simply build this query string? Some more information may help: is the error returned before or during the execute call? If before, then the execute() call is a distraction. If during, then what is your dB, what is it's encoding (is the dB using latin1, or does the dB only accept ascii?), and what are you using to connect to it? Jim -- http://mail.python.org/mailman/listinfo/python-list
Re: Understanding Unicode & encodings
John Machin wrote: > Jim wrote: > > No, I'll bet that you'd like to run something like > > self.dcCursor.execute("INSERT INTO track (name, nbr, idartist, > > idalbum,path) VALUES (%(track)s, %(nbr)s, > > %(idartist)s,%(idalbum)s,'%(path)s')", > > {'track':track,'nbr':nbr,'idartist':idartist,'idalbum':idalbum,'path':path}) > > (only without my typos). That's an improvment for a number of reasons, > > one of which is that the system will quote for you, for instance in > > idartist="John's Beer" changing the single quote to two single quotes > > to suit SQL. > I see no improvement here. > > The OP's code is effectively:: > > sql = "INSERT INTO track (name, ..., path) VALUES ('%s', ..., '%s')" > value_tuple = (track, , path) > self.dcCursor.execute(sql % value_tuple) > > Your suggested replacement is effectively: > > sql = "INSERT INTO track (name, ...,path) VALUES (%(track)s, > ...,'%(path)s')" > str_fmt_dict = {'track':track, ...,'path':path} > self.dcCursor.execute(sql, str_fmt_dict) > > Well, that won't run at all. Let's correct the presumed typo: > >self.dcCursor.execute(sql % str_fmt_dict) I'm sorry, that wasn't a typo. I was using what the dBapi 2.0 document calls 'pyformat' (see the text under "paramstyle" in that document). > Now, the only practical difference is that you have REMOVED the OP's > explicit quoting of the first column value. Changing the string > formatting from the %s style to the %(column_name) style achieves > nothing useful. You are presenting the "system" with a constant SQL > string -- it is not going to get any chance to fiddle with the quoting. > However the verbosity index has gone off the scale: each column name is > mentioned 4 times (previously 1). Gee, I like the dictionary; it has a lot of advantages. > I would suggest the standard default approach: > > sql = "INSERT INTO track (name, ..., path) VALUES (?, ..., ?)" > value_tuple = (track, , path) > self.dcCursor.execute(sql, value_tuple) > > The benefits of doing this include that the DBAPI layer gets to > determine the type of each incoming value and the type of the > corresponding DB column, and makes the appropriate adjustments, > including quoting each value properly, if quoting is necessary. I'll note that footnote [2] of the dBapi format indicates some preference for pyformat over the format above, called there 'qmark'. But it all depends on what the OP is using to connect to the dB; their database module may well force them to choose a paramstyle, AIUI. Anyway, the point is that to get quote escaping right, to prevent SQL injection, etc., paramstyles are better than direct string %-ing. Jim -- http://mail.python.org/mailman/listinfo/python-list
Re: Get age of a file/dir
Carl J. Van Arsdall wrote: > I've been looking around the OS module and I haven't found anything > useful yet. Does anyone know how to get the age of a file or directory > in days? I'm using unix and don't seem to find anything that will help > me. The only function that comes close so far is > > os.path.getctime(path) > > > However this only gets creation time on Windows, on Unix it gets the the > time of the last change. Any ideas? > > Thanks! > > -carl > > -- > > Carl J. Van Arsdall > [EMAIL PROTECTED] > Build and Release > MontaVista Software Hi, You should check out the datetime module. And convert dates to an ordinal number. today = datetime.date.today().toordinal() age = today - datetime.date(year, month, day).toordinal() Jim -- http://mail.python.org/mailman/listinfo/python-list
Re: XML parsing and writing
c00i90wn wrote: On first write of the xml everything goes > as it should but on subsequent writes it starts to add more and more > unneeded newlines to it making it hard to read and ugly. Pretty make it pretty by putting in newlines (and spaces) that are not in the original data. That is, if you have text "John Smith" associated with the element then pretty gives you something like John Smith here with an extra two newlines and some whitespace indentation. (I don't recall 100% when it puts in stuff, but the point of pretty is to put in extra stuff.) You need to strip out the extra stuff (or print it out not pretty; can you get a viewer that buffs-up a notbuff file so you are seeing pretty but the data isn't actually pretty?). Jim -- http://mail.python.org/mailman/listinfo/python-list
Why do I require an "elif" statement here?
Could somebody tell me why I need the "elif char == '\n'" in the following code? This is required in order the pick up lines with just spaces in them. Why doesn't the "else:" statement pick this up? OLD_INDENT = 5 # spaces NEW_INDENT = 4 # spaces print 'Reindent.py:' print '\nFrom file %s' % infile print 'Change %i space indentation to %i space indentation.' % ( OLD_INDENT, NEW_INDENT) print 'And place revised file into %s' % outfile whitespace = ' ' n = 0 nline = 0 for line in input.readlines(): nline += 1 # Only look at lines that start with a space. if line[0] == whitespace: i = 0 for char in line: i += 1 if char == whitespace: pass elif char == '\n': # Why do I need this for a blank line with only spaces? output.write(line) break else:# Why doesn't the blank line get picked up here? x = line.count(whitespace*OLD_INDENT,0,i) # Reindent lines that have exactly a multiple of OLD_INDENT. if x > 0 and (i-1)%OLD_INDENT == 0: output.write(whitespace*NEW_INDENT*x+line.lstrip()) n += 1 break else: output.write(line) break else: output.write(line) input.close() output.close() print 'Total number of %i lines reindented out of %i lines.' % (n, nline) -- http://mail.python.org/mailman/listinfo/python-list
Re: Why do I require an "elif" statement here?
Tim Chase wrote: > > Could somebody tell me why I need the "elif char == '\n'" in > > the following code? > > > > This is required in order the pick up lines with just spaces > > in them. > > Why doesn't the "else:" statement pick this up? > > Following through with the below code: > > if the line consists of only a newline, it gets ignored due to > the "if line[0] == whitespace" line. However, if the line > consists of only whitespace followed by a newline you *do* > successfully get to the "else" in question. There's no other > place for you to go. > > However, what happens then? If you fall into you the top half of > your "if x > 0 ..." statement: > > you strip **all** *true* whitespace from the line with your > lstrip() call. Since there's nothing between your "whitespace" > (simple spaces) and the \n, the \n gets swallowed by the lstrip() > call. Thus, you output.write() an empty string. > > I recommend a few judiciously placed "print repr(thing)" lines as > you try to debug to see where things aren't what you expect them > to be. > > As another sidelight, rather than using the "i=0, i+= 1" aspect, > you can use the more pythonic idiom of > > for i, char in enumerate(line): > > (taking into consideration that i becomes zero-based). This will > automatically update "i" on each pass. > > -tkc > > > > > OLD_INDENT = 5 # spaces > > NEW_INDENT = 4 # spaces > > > > print 'Reindent.py:' > > print '\nFrom file %s' % infile > > print 'Change %i space indentation to %i space indentation.' % ( > > OLD_INDENT, NEW_INDENT) > > print 'And place revised file into %s' % outfile > > > > whitespace = ' ' > > n = 0 > > nline = 0 > > > > for line in input.readlines(): > > nline += 1 > > # Only look at lines that start with a space. > > if line[0] == whitespace: > > i = 0 > > for char in line: > > i += 1 > > if char == whitespace: > > pass > > elif char == '\n': # Why do I need this for a > > blank line with only spaces? > > output.write(line) > > break > > else:# Why doesn't the blank line > > get picked up here? > > x = line.count(whitespace*OLD_INDENT,0,i) > > # Reindent lines that have exactly a multiple of > > OLD_INDENT. > > if x > 0 and (i-1)%OLD_INDENT == 0: > > output.write(whitespace*NEW_INDENT*x+line.lstrip()) > > n += 1 > > break > > else: > > output.write(line) > > break > > else: > > output.write(line) > > > > input.close() > > output.close() > > print 'Total number of %i lines reindented out of %i lines.' % (n, > > nline) > > Thank you Tim. Hard to believe that lstrip() produces an empty string on lines with just spaces and doesn't remove the '\n' with lines that have characters. I'm now using all your suggestions, even "print repr(thing)" which I wasn't aware of. Thanks, Jim -- http://mail.python.org/mailman/listinfo/python-list
Re: Why do I require an "elif" statement here?
Good stuff! Since I'm only interested in spaces being my only whitespace it makes sense for me to use "line.lstrip(whitespace)" in my script, thus eliminating the "elif char == '\n':" statement. Thanks, Jim Tim Chase wrote: > > Hard to believe that lstrip() produces an empty string on lines with > > just spaces and doesn't remove the '\n' with lines that have > > characters. > > It's easy to understand that lstrip() is doing exactly what it's > supposed to. It evaluates from the left of your string, > discarding whitespace (spaces, tabs, and cr/lf characters) until > it hits a non-whitespace character or the end of the string. > When there's no non-whitespace, it returns an empty string. > > If you wanted to remove the \n from the right of lines, there was > an earlier discussion on the list where someone (Bruno?) and I > went back and forth and I think we finally decided that the > "best" solution was > > s.rstrip('\n') > > which had the fewest side-effects. > > However, when you use the output.write() method, you'd then have > to add the \n back in to make sure it ended up in the output stream. > > If you wanted to continue to use lstrip(), you could also just > ensure that you're only stripping spaces (chr(0x20)) by using > > s.lstrip(' ') > > This would leave \t and \n characters unmolested. > > More info can be found at > > >>> help("".lstrip) > >>> help("".rstrip) > >>> help("".strip) > > Hope this helps, > > -tkc -- http://mail.python.org/mailman/listinfo/python-list
Re: do people really complain about significant whitespace?
> He points out that if some code gets accidentally dedented, it is > difficult for another programmer to determine which lines were supposed > to be in the indented block. I pointed out that if someone > accidentally moves a curly brace, the same problem can occur. > Anecdotally, I've never had either problem. > I have many times found that in moving a multi-screen block of code from one place to another (where the indent is less or more) then I have trouble re-indenting the code. That is not to say that I don't in the end prefer the significant whitespace, but I have had errors moving code like: if .. try: .. except .. else .. where it wound up (with the help of the emacs tab key) as if .. try: .. except .. else .. . I attempt to be careful, but certainly it has happened to me. Jim -- http://mail.python.org/mailman/listinfo/python-list
Re: trouble using "\" as a string
Try using: tempname = "\\" Jim OriginalBrownster wrote: > Hi there... > > I'm still pretty new to turbogears. but i have gotten pretty familiar > with it > > i'm just trying to clear something up, i'm having a difficult time > using \ when declaring a string expression > > such as tempname="\"..it says that the line is single qouted. > > i want this because using python I am pulling in filenames from a > mac..thus they are "/" in the pathways..and i want to .split it at the > "/" to obtain the filename at the end...but its proving diffucult with > this obstacle in the way. > > Why is this happening?? -- http://mail.python.org/mailman/listinfo/python-list
Re: CGI Tutorial
Clodoaldo Pinto Neto wrote: > I'm just building a Python CGI Tutorial and would appreciate any > feedback from the many experts in this list. I'm not an expert, but I have written a lot of these and I have a couple of $0.02's. * All code you put in your writing needs to be correct. That is, on the web you can't say something and later in the text say "but this has a problem and needs to be tightened up" because people will paste in code that they got from you and won't read the rest. They will. Instead, you need the scripts to be right, from the start. Then you say "Lets look at lines 1-5. The reason for those is ..". * All cgi scripts need logging. Debugging cgi can be hard and you need to have a place to write statements of the form log.debug("in getValues(): value of x is %s" % (repr(x),)). * You need a DEBUG variable: from defaults import DEBUG : if DEBUG: .. * I've been impressed by Guido's writing that a main() routine makes sense. One reason is that you can more easily make unit tests. Because testing cgi is so hard, this is especially useful in this context. (I admit that I'm only a recent convert to this but it really makes sense.) So, continuing with my opinions as though they were facts, the skeleton of all cgi's is something like this, IMHO: import sys, os, os.path, urllib, cgi from cgi import escape from xml.sax.saxutils import quoteattr from defaults import DEBUG, LOGGING THIS_SCRIPT=os.path.basename(sys.argv[0]) LOGFILE_NAME=os.path.splitext(THIS_SCRIPT)[0]+'.log' if DEBUG: import cgitb cgitb.enable() # all kinds of functions here def main(fs,argv=None,log=None,debug=False): if argv is None: argv=sys.argv # logic here if __name__=='__main__': log=None if LOGGING: log=openLog(LOGFILE_NAME) fs=cgi.FieldStorage(keep_blank_values=1) try: main(fs,argv=sys.argv,log=log,debug=DEBUG) except StandardError, err: mesg="General programming error" bail(mesg,devel=mesg+": error=%(err)s",log=log,debug=DEBUG,err=err) except SystemExit, err: # bailed out in a subroutine pass sys.exit(0) (where bail() is a routine that puts up an error page -- on that page, I have one of two messages, the second of which, using the "devel" string, only appears when DEBUG is True). In my humble experience, all cgi programs should follow something like that scheme. You asked for an opinion! :-) Jim -- http://mail.python.org/mailman/listinfo/python-list
Re: CGI Tutorial
Sells, Fred wrote: > content is great, my comments are editorial. > > I prefer PDF with bookmarks rather than HTML. > > If you choose to go the PDF route, I've found OpenOffice 2.0 pretty good at > generating PDF with bookmarks. Just don't get too complex or OO may hose > you. Since you replied to a person who replied to me and who mentioned it, I assume that you are talking about my text _Linear Algebra_ (I have another text up, _Number Theory_, that no one seems to know about so I assume you are not talking about it). If that's true then it is way off topic, not just for the thread but for the newsgroup. Please write me separately at the address given on the web page for the text. But, my book doesn't use any HTML. It also doesn't come from a word processor, since for mathematics the standard is LaTeX, so maybe you are talking about something entirely unrelated? But at all events it seems not to have to do with CGI. Jim -- http://mail.python.org/mailman/listinfo/python-list
where to put .pth file
I am trying to tell Python where it can find some modules it will need when embedded in my app. The convenient way would be to plop a .pth file somewhere Python can find it. I found that the doc is wrong when it says this kind of filecan go anywhere on the python path. It is not found if it goes into the python root dir, or python\lib -- the only place I found it would work was in lib\site-packages. So the next question is, how do I know where this dir is located? In order to look in the Registry to find where Python is installed, I need to know the version! There is a set of keys under Python24 and another set under Python25 on my machine. That means I need to know what version is installed before I can find out where its install dir is. Bizarre. So how do I find out what version is installed? The best way I came up with so far is to ask Python for the value of sys.prefix. Trouble is, when I issue a PyRun statement, it fires up 24, not 25, and gives me the 24 prefix. In short: how do I find the latest installed site-packages directory? -- Jim -- http://mail.python.org/mailman/listinfo/python-list
Re: unescape HTML entities
Rares Vernica wrote: > How can I unescape HTML entities like " "? Can I ask what you mean by "unescaping"? Do you mean converting into numeric references? Into Unicode? Jim -- http://mail.python.org/mailman/listinfo/python-list
win32file.DeviceIoControl(FSCTL_GET_RETRIEVAL_POINTERS)
I'm not sure how to perform this operation in Python. The difficulty is in knowing the size of the output buffer: if it is too small, I get an "insufficient buffer" exception; too large, and I get an "end of file" exception. In neither case is any partial data available. I'd rather not resort to a binary seach... -- http://mail.python.org/mailman/listinfo/python-list
Re: win32file.DeviceIoControl(FSCTL_GET_RETRIEVAL_POINTERS)
Mea culpa: files with *no* clusters return the end-of-file error. -- http://mail.python.org/mailman/listinfo/python-list
Re: building python with utf-8 default encoding?
[EMAIL PROTECTED] wrote: > I am playing around with OpenSwarm and was shocked to see that I cannot > build Python with default encoding of utf-8 by passing a flag to > configure... did I miss the option for doing so? It is not set when you build but is instead set in sitecustomize.py. But changing it is typically inadvisable in that any programs you write will not port to other people's machines. Jim -- http://mail.python.org/mailman/listinfo/python-list
deciding what is a dir (folder) in a zip file
Hello, I'm trying to read .zip files and drop from the listing those files that are directories. I'm using the zipfile module. Does anyone know how I can I tell which files in the .zip are directories? I have looked around the net for the information but I cannot make it out: the pkzip specification hints that it is in the "external file attribute" but also hints that it is dependent on the platform on which the .zip was made (I get .zips from lots of platforms). The info-zip mailing list is unfortunately swamped by spam, so I can't ask there. Googling has increased my suspicion that it is not just a matter of seeing if the file name ends in '/' and that the relevant external file attribute seems to have different values for people from different platforms, so just experimenting on my platform doesn't seem to be a safe solution. (I could os.system("unzip "+fn) and search the disk for directories but I'd rather not, for reasons I won't list.) I'd be grateful for any help, Jim -- http://mail.python.org/mailman/listinfo/python-list
Re: Cross-site scripting (XSS) defense
Have a look at http://feedparser.org/docs/html-sanitization.html . Jim -- http://mail.python.org/mailman/listinfo/python-list
Re: Cross-site scripting (XSS) defense
Have a look at http://feedparser.org/docs/html-sanitization.html . Jim -- http://mail.python.org/mailman/listinfo/python-list
can't find win32api from embedded pyrun call
I am trying to figure out how to embed Python in a little C++ Windows console app. Here's the code: // Py_Initialize(); Py_InitializeEx(0); PyRun_SimpleString("from win32com.client import *"); Here's what it does on the last line: File "D:\Python\Lib\site-packages\win32com\__init__.py", line 5, in ? import win32api, sys, ok ImportError: No module named win32api The same line runs fine in IDLE or at the command prompt. It also runs without complaint if I run a release version of the app. To build the debug version I had to build python42_d.lib/dll myself. The reason I can't call PyInitialize is that it crashes with an "invalid signal" error. I don't think it's a path issue -- I've checked system path, sys.path, pythonpath env var, and the pythonpath entry in the registry, all of them loaded with directories including the one with win32api.pyd. Besides, if it were an install or path problem, why would it work at the command prompt? Could it be a problem with the debug lib I built? Any suggestions are welcome. -- Jim -- http://mail.python.org/mailman/listinfo/python-list
Re: can't find win32api from embedded pyrun call
Miki wrote: > IIRC you need to set the path explicitly in an embedded interpreter. > See the code in "site.py" (which again IMO is *not* imported when the > interpreter starts). Thanks Miki. Actually that doesn't turn out to be the problem. If I display sys.path within the embedded script, it shows the complete list of paths, there is no need to pre-load. The problem turned out to be a mismatch of parts. For one thing, building the debug lib from Python sources using VS2005 leads to the crash in PyInitialize (I don't know why others haven't run into this); building with VS2003 did away with that. But that then led to the well-known problem with a FILE* where the app is using one msvcrt and the lib another. At some point in fixing these issues, the "cannot find win32api" went away. I now use this trick from a colleague which allows me to build a debug version of my app but load the release version of the lib: bracket the "#include python.h" statement with undef/redef of _DEBUG. And this trick to get around the FILE* problem (since my app is VS2005 and the lib is 2003): instead of PyRun_SimpleFile, use PyRun_SimpleString ("execfile(fname)"). I got this from the win32 FAQ. I hope someone benefits from this, it cost me plenty to figure out. -- Jim -- http://mail.python.org/mailman/listinfo/python-list
embedded Python calling app via COM
I have a C++ app which fires up a Python script using C API calls. That script operates the app via Automation calls, like this: from win32com.client import * from mywrapper import * myapp = Application() myapp.Visible = True mydoc = myapp.Documents.Open(...) My problem is to make sure the instance of myapp is the same one I am calling from. The above code starts up a new instance of the app, which is not what I want. In other words I need something like a GetObject instead of a CreateObject, and I need some way to identify the calling app so I can tell GetObject about it. Before I go and do something hack, I thought I'd see if anyone else is in this situation and knows a good way to do the job. -- Jim -- http://mail.python.org/mailman/listinfo/python-list