setup.py, how to find main program/module?
hello, i'm getting there... yet not quite all the way yet. i run the commands to make the sdist and wheels and upload it using twine to my test account, but when i install it and try to run it the ngfp.py is not found (i've also tried nfgp and main). test upload: https://test.pypi.org/project/ngfp/ i suspect i've not really understood entry points and may not have it specified correctly in setup.py. any help appreciated. :) to install it you'll need to use (to pull in pyglet): $ pip install --index-url https://test.pypi.org/simple/ --extra-index-url https://pypi.org/simple ngfp==0.1.5.post3 thank you and cheers, ant -- https://mail.python.org/mailman/listinfo/python-list
Re: setup.py, how to find main program/module?
dieter wrote: > ant writes: >> ... >> yet not quite all the way yet. i run the >> commands to make the sdist and wheels and upload >> it using twine to my test account, but when >> i install it and try to run it the ngfp.py is >> not found (i've also tried nfgp and main). >> ... >> i suspect i've not really understood entry >> points and may not have it specified correctly in >> setup.py. >> >> any help appreciated. :) > > I think you are right: the problem is likely with the > "entry_points" specification. > > > My "dm.zopepatches.ztest" package has > entry_points = dict( > console_scripts = [ >'ztest = dm.zopepatches.ztest:main', >] > ), > This is responsible for the creation of a script called > "ztest" in Python's "bin" directory. > > > Does your "entry_points" specification look similar? yes. > Do you find corresponding scripts in Python's "bin" directory? thank you, yes, it is there: = (env) me@ant(9)~/src/env/bin$ more ngfp #!/home/me/src/env/bin/python3 # EASY-INSTALL-ENTRY-SCRIPT: 'ngfp','console_scripts','ngfp' __requires__ = 'ngfp' import re import sys from pkg_resources import load_entry_point if __name__ == '__main__': sys.argv[0] = re.sub(r'(-script\.pyw?|\.exe)?$', '', sys.argv[0]) sys.exit( load_entry_point('ngfp', 'console_scripts', 'ngfp')() ) = (env) me@ant(11)~/src/test$ ngfp Traceback (most recent call last): File "/home/me/src/env/bin/ngfp", line 6, in from pkg_resources import load_entry_point File "/usr/lib/python3/dist-packages/pkg_resources/__init__.py", line 3126, in @_call_aside File "/usr/lib/python3/dist-packages/pkg_resources/__init__.py", line 3110, in _call_aside f(*args, **kwargs) File "/usr/lib/python3/dist-packages/pkg_resources/__init__.py", line 3139, in _initialize_master_working_set working_set = WorkingSet._build_master() File "/usr/lib/python3/dist-packages/pkg_resources/__init__.py", line 581, in _build_master ws.require(__requires__) File "/usr/lib/python3/dist-packages/pkg_resources/__init__.py", line 898, in require needed = self.resolve(parse_requirements(requirements)) File "/usr/lib/python3/dist-packages/pkg_resources/__init__.py", line 784, in resolve raise DistributionNotFound(req, requirers) pkg_resources.DistributionNotFound: The 'ngfp' distribution was not found and is required by the application = so, actually, the problem isn't what i expected it to be. but i'm still not sure how to fix it. :) ant -- https://mail.python.org/mailman/listinfo/python-list
ah, progress...
ant wrote: ... script was there, but the package was not actually installed. after installing again i get: = (env) me@ant(26)~/src/test$ ngfp Traceback (most recent call last): File "/home/me/src/env/bin/ngfp", line 7, in from ngfp import main ImportError: cannot import name 'main' from 'ngfp' (/home/me/src/salsa/env/lib/python3.7/site-packages/ngfp/__init__.py) = i'm not sure if this is because my __init__.py is another directory deeper (my project has a top level directory with the setup.py in it and then the ngfp directory with the __init__.py in it which contains the following: = name = "ngfp" = which obviously says nothing about main... i hate being a newbie. but at least i'm making progress. :) ant -- https://mail.python.org/mailman/listinfo/python-list
Re: ah, progress...
ant wrote: > ant wrote: > > ... > > script was there, but the package was not actually > installed. > > after installing again i get: > >= > > (env) me@ant(26)~/src/test$ ngfp > Traceback (most recent call last): > File "/home/me/src/env/bin/ngfp", line 7, in > from ngfp import main > ImportError: cannot import name 'main' from 'ngfp' > (/home/me/src/salsa/env/lib/python3.7/site-packages/ngfp/__init__.py) > >= > > i'm not sure if this is because my __init__.py is > another directory deeper (my project has a top level > directory with the setup.py in it and then the > ngfp directory with the __init__.py in it which > contains the following: > >= > name = "ngfp" >= > > which obviously says nothing about main... > i hate being a newbie. > > but at least i'm making progress. :) hmmm, but my main is defined in ngfp.py = ... def main(): window = Window(width=cfg.img_pix*(cfg.game_cols+cfg.control_cols+3), height=cfg.img_pix*(cfg.game_rows+2), caption="Ngfp", resizable=True, fullscreen=False, visible=False) pyglet.clock.schedule_interval(window.update, 1/120.0) # update at 60Hz pyglet.app.run() if __name__ == "__main__": main() = when i'm in the directory with the source code: $ python3 ngfp.py works exactly as it should. ant -- https://mail.python.org/mailman/listinfo/python-list
Re: ah, progress...
dieter wrote: > ant writes: >> ant wrote: >> ... >>> (env) me@ant(26)~/src/test$ ngfp >>> Traceback (most recent call last): >>> File "/home/me/src/env/bin/ngfp", line 7, in >>> from ngfp import main >>> ImportError: cannot import name 'main' from 'ngfp' >>> (/home/me/src/salsa/env/lib/python3.7/site-packages/ngfp/__init__.py) >> ... >> hmmm, but my main is defined in ngfp.py > > The error message tells you that it looks for "main" > in "...ngfp/__init__.py", not in "ngfp.py". i took an examply python program called afew and edited it down to work for a very simple program which i then took my other code and put it in that directory structure to make sure i wasn't doing anything strange, but i still ended up where running the program one way works and running it the other way it seems to miss something important which i don't understand. in order to get this far below i had to edit each file and put a try: except: around each import statment checking if the module could be found like (as an example): try: import config as cfg except: import frog.config as cfg this is very frustrating because the following makes no sense to me: when i run the code within the directory like this i get: = this works as it should (env) me@ant(30)~/src/salsa/frog/frog$ python3 ngfp.py current_dir : /home/me/src/salsa/frog/frog (env) me@ant(31)~/src/salsa/frog/frog$ = when i pack it up and then install it using (and then run it via the script): = it doesn't work, but the directory is the same...? (env) me@ant(150)~/src/salsa/test$ pip3 --no-cache-dir install -e ../frog/ --force-reinstall Obtaining file:///home/me/src/salsa/frog Requirement already satisfied: pyglet>=1.3.0 in /home/me/src/salsa/env/lib/python3.7/site-packages (from frog==1.0.0) Requirement already satisfied: future in /home/me/src/salsa/env/lib/python3.7/site-packages (from pyglet>=1.3.0->frog==1.0.0) Installing collected packages: frog Found existing installation: frog 1.0.0 Uninstalling frog-1.0.0: Successfully uninstalled frog-1.0.0 Running setup.py develop for frog Successfully installed frog (env) me@ant(151)~/src/salsa/test$ frog current_dir : /home/me/src/salsa/frog/frog Traceback (most recent call last): File "/home/me/src/salsa/env/bin/frog", line 11, in load_entry_point('frog', 'console_scripts', 'frog')() File "/home/me/src/salsa/frog/frog/commands.py", line 12, in main inner_main() File "/home/me/src/salsa/frog/frog/ngfp.py", line 284, in main window = Window(width=cfg.img_pix*(cfg.game_cols+cfg.control_cols+3), height=cfg.img_pix*(cfg.game_rows+2), caption="Ngfp", resizable=True, fullscreen=False, visible=False) File "/home/me/src/salsa/frog/frog/ngfp.py", line 63, in __init__ MyInitStuff (self) File "/home/me/src/salsa/frog/frog/my_init.py", line 113, in MyInitStuff self.game_bg_image = pyglet.image.load("png/mirrors/00_bg.png") File "/home/me/src/salsa/env/lib/python3.7/site-packages/pyglet/image/__init__.py", line 180, in load file = open(filename, 'rb') FileNotFoundError: [Errno 2] No such file or directory: 'png/mirrors/00_bg.png' (env) me@ant(152)~/src/salsa/test$ = the paths are exactly the same when i print the current directory. the png directory is there and the image is there. ugh, time to sleep... ant -- https://mail.python.org/mailman/listinfo/python-list
Re: Mask two images with python
Umar Yusuf wrote: ... > Hello Oscar, > Thank you for your time.. Here is attached is an example set of the images: > https://drive.google.com/file/d/1hyiWswx4GCZQDepXZjktq2zgM_LBbxXt/view?usp=sharing > basically, it is "design + mask and glow = result" that didn't work for me at all... are the images .png or some other format that allows transparency? is it critical that it be done via Python or do you just want it done at all? :) gimp and other tools (Graphics Magic) will allow this to be done interactively or via the command line. if it has to be done in python i'm only familiar with pyglet and could do it easily enough that way (sorry i'm a newbie about other ways in python :) ). make a sprite in a certain location in a window with the background image and then make another sprite with the next layer on top of that one, one at a time, each with it's own batch id. then when it appears how you like it you can grab a screen shot via: The following example shows how to grab a screenshot of your application window: pyglet.image.get_buffer_manager().get_color_buffer().save('screenshot.png') Note that images can only be saved in the PNG format unless the Pillow library is installed. = if you want an example of multiple layers i have my work in progress that does exactly this sort of thing (except i never save an image yet - that could be easily added as above). you're welcome to take the code and use it however you like: https://salsa.debian.org/ant-guest/gfpoken-in-python ant -- https://mail.python.org/mailman/listinfo/python-list
Re: ah, progress...
dieter wrote: > ant writes: >> ... >> in order to get this far below i had to edit each >> file and put a try: except: around each import >> statment checking if the module could be found >> like (as an example): >> >> try: >> import config as cfg >> except: >> import frog.config as cfg > > Is "frog" the package, you want to install? > Then always use "import frog.config ...". frog is my temporary adaptation of my project to test out how to do this without actually making all those changes until i'm sure it is doing what i want. frog is a package as i understand it. here is the structure: = (env) me@ant(15)~/src/salsa/frog$ find . -print | grep -v ".git" | grep -v "__pycache__" | grep -v "dist" | grep -v "build" | sort . ./frog ./frog/active.py ./frog/background.py ./frog/board.py ./frog/commands.py ./frog/config.py ./frog/dialog.py ./frog/doc ./frog/doc/ngfp.6 ./frog.egg-info ./frog.egg-info/dependency_links.txt ./frog.egg-info/entry_points.txt ./frog.egg-info/PKG-INFO ./frog.egg-info/requires.txt ./frog.egg-info/SOURCES.txt ./frog.egg-info/top_level.txt ./frog/history.py ./frog/__init__.py ./frog/labels.py ./frog/__main__.py ./frog/marbles.py ./frog/my_init.py ./frog/ngfp.py ./frog/png ./frog/png/arrows ./frog/png/arrows/picDDownW.png ./frog/png/arrows/picDLeftW.png ...80 lines skipped... ./frog/png/misc/sink_inv.png ./frog/png/misc/sink_orig.png ./frog/randboard.py ./LICENSE ./LICENSE.GPL ./MANIFEST.in ./NOTICE ./README.md ./setup.cfg ./setup.py = > Python locates (top level) packages/modules via the so > called "Python path" (--> "sys.path"). When > you run a script, the "Python path" is extended by the > directory the script resides in -- thus a script typically > can import "local" packages/module (residing in its directory). > However, it is good practise to always access installed > packages via their "official package path" (which > in your case likely means "frog.config"). this is the first i'm hearing of a path being extended. why isn't that reflected when i print the current working directory when the program is running (in both cases the paths show identical)? this is also the first time i'm hearing of "official package path". there are likely the details i'm beating my head against... thank you. :) > When you develop a package, it may not yet be installed. > This might hinder you to import in the "official" way. correct. when i am testing/writing code i am in the source code directory above called frog which looks like: = -rw--- 1 me me 8962 Dec 21 00:20 active.py -rw--- 1 me me 6673 Dec 21 00:11 background.py -rw--- 1 me me 11796 Dec 21 00:10 board.py -rw--- 1 me me 217 Dec 20 23:16 commands.py -rw--- 1 me me 5987 Dec 21 00:12 config.py -rw--- 1 me me 34077 Dec 21 00:14 dialog.py drwx-- 2 me me 4096 Dec 18 09:26 doc -rw--- 1 me me 1943 Dec 21 00:15 history.py -rw--- 1 me me 310 Dec 20 23:39 __init__.py -rw--- 1 me me 1845 Dec 21 00:15 labels.py -rw--- 1 me me 140 Dec 20 22:39 __main__.py -rw--- 1 me me 23973 Dec 21 00:17 marbles.py -rw--- 1 me me 13534 Dec 21 00:34 my_init.py -rw--- 1 me me 10354 Dec 21 09:02 ngfp.py drwx-- 10 me me 4096 Dec 18 09:26 png -rw--- 1 me me 5514 Dec 21 00:18 randboard.py (env) me@ant(18)~/src/salsa/frog/frog$ = so to test the code i simply run it via $ python3 ngfp.py which works as i expect it. = > I work around this by using "setuptools" and its "setup" > function in my "setup.py". "setuptools" supports the > command "develop" (activated via "python setup.py develop") > which "installs" the package in a way that it refers to > the developped sources. This way, I can always > import in the "official" way and nevertheless all > my changes are effective immediately. i'm not sure how this works, but i will look into it. > Another approach would be to lay out your package in such > a way that it refects the installed structure and > for testing put your script in the directory containing > your top level packages (or extend "sys.path" with this directory). i think that is what i've always assumed that was how the code is installed is exactly as how i have it in my development directory as shown above. your hint here says that it probably isn't so... heh... thanks, these are things a newbie doesn't know, but someone experienced just considers common sense. for me coming from C code, packed and unpacked via tarballs and such exactly where i want them to go is the normal and it se
[SOLVED] Re: ah, progress...
dieter wrote: ... thank you for your help. :) i finally worked through the changes needed at last. my current package in testing PyPI is at: https://test.pypi.org/project/ngfp/ which uses my code from: https://salsa.debian.org/ant-guest/gfpoken-in-python i ended up needing to do two things. to get all the import statements changed to include the package/module name and to change all my image load statements to include the full path as determined at runtime. this may not work on a non-linux or non-posix system from the binary distribution but the source code distribution may work ok. if anyone wants to try it out. feedback is appreciated in any case. :) now the next fun steps (figuring out the windows, other versions and then the Debian packaging for it). since i don't have windows or other machines to test on i don't know how that will go. cheers and thanks again, ant -- https://mail.python.org/mailman/listinfo/python-list
Re: Why am I getting Error 405 while uploading my package to https://test.pypi.org/legacy?
sntshkm...@gmail.com wrote: > I'm trying to upload my package to PyPi, but before that I wanted to upload > my package to TestPyPi. > > I'm following https://packaging.python.org/guides/using-testpypi/ > > I'm issuing this command: `twine upload --repository-url > https://test.pypi.org/legacy/ dist/*` > > Here is the output: > > ``` > Enter your username: sntshkmr60 > Enter your password: > Uploading distributions to https://test.pypi.org/ > Uploading mysecretpackage-0.0.1a0-py3-none-any.whl > 100%|| > 16.7k/16.7k [00:06<00:00, 2.50kB/s] > NOTE: Try --verbose to see response content. > HTTPError: 405 Client Error: Method Not Allowed for url: > https://test.pypi.org/ > ``` > > Here is the extra output from --verbose flag: > > ``` > Content received from server: > > > 405 Method Not Allowed > > > 405 Method Not Allowed > The method POST is not allowed for this resource. > > > > HTTPError: 405 Client Error: Method Not Allowed for url: > https://test.pypi.org/ > ``` > > What am I doing wrong? well, i used it earlier so here is how i have it set up as follows. because this is testing repository i am not worried about password being stored in a file: .pypirc = [distutils] index-servers= testpypi [testpypi] repository: https://test.pypi.org/legacy/ username: UserName password: Password = and my upload command is: = #!/bin/sh # # upload ngfp to test pypi NGFP_SRC_HOME="/home/me/src/salsa/ngfp" if test ! -d "$NGFP_SRC_HOME" ; then echo " directory $NGFP_SRC_HOME does not exist!" exit fi cd $NGFP_SRC_HOME twine upload --repository testpypi dist/* = note: it may take a while for what you uploaded to be made available for download again even if it reflected in your project page. sometimes it has been as long as a half hour or more before it comes down. other times it has only been a few minutes. hope this helps... ant -- https://mail.python.org/mailman/listinfo/python-list
Re: Why am I getting Error 405 while uploading my package to https://test.pypi.org/legacy?
ant wrote: ... > .pypirc >= > [distutils] > index-servers= > testpypi > > [testpypi] > repository: https://test.pypi.org/legacy/ > username: UserName > password: Password >= > > > and my upload command is: > > >= > #!/bin/sh > # > # upload ngfp to test pypi > > NGFP_SRC_HOME="/home/me/src/salsa/ngfp" > if test ! -d "$NGFP_SRC_HOME" ; then > echo " directory $NGFP_SRC_HOME does not exist!" > exit > fi > > cd $NGFP_SRC_HOME > > twine upload --repository testpypi dist/* >= since i just went through this myself i thought i'd add a bit more. when you change over to post to the main pypi.org you need to do it to upload.pypi.org like: = [pypi] repository: https://upload.pypi.org/legacy/ = as i was getting 405 errors when using just pypi.org i suspect your error above is that you're not using the /legacy/ part at the end... good luck! ant -- https://mail.python.org/mailman/listinfo/python-list
Re: Why am I getting Error 405 while uploading my package to https://test.pypi.org/legacy?
sntshkm...@gmail.com wrote: >> .pypirc >>= >> [distutils] >> index-servers= >> testpypi >> >> [testpypi] >> repository: https://test.pypi.org/legacy/ >> username: UserName >> password: Password > > >> twine upload --repository testpypi dist/* > > Tried your suggestion for .pypirc file. Still the same error. > > Has something changed and is not reflected in the documentation? did you check your dists via twine? $ twine check --verbose dist/* also make sure you are using up to date versions of setuptools, wheel and twine $ pip install --upgrade setuptools $ pip install --upgrade wheel $ pip install --upgrade twine i just used it earlier today... worked fine for me. is your account there and verified? ant -- https://mail.python.org/mailman/listinfo/python-list
Re: Why am I getting Error 405 while uploading my package to https://test.pypi.org/legacy?
sntshkm...@gmail.com wrote: >> did you check your dists via twine? >> >> $ twine check --verbose dist/* > > I checked with `twine check dist/*` (there's no --verbose option though). > I only get warning about markdown right, i see later in my history i used the twine check without the verbose option. you have to fix that for it to upload... $ pip install readme_renderer[md] >> also make sure you are using up to date versions of >> setuptools, wheel and twine >> >> >> $ pip install --upgrade setuptools >> $ pip install --upgrade wheel >> $ pip install --upgrade twine > > [65]> import setuptools, wheel, twine > [66]> print(setuptools.__version__) > 40.6.3 > [67]> print(wheel.__version__) > 0.32.3 > [68]> print(twine.__version__) > 1.12.1 > > >> i just used it earlier today... worked fine for me. >> is your account there and verified? > > Yes, I have an account on TestPyPI and is verified. try the above... ant -- https://mail.python.org/mailman/listinfo/python-list
Re: Why am I getting Error 405 while uploading my package to https://test.pypi.org/legacy?
sntshkm...@gmail.com wrote: >> $ pip install readme_renderer[md] > > Thanks a lot for this, I wasn't able to figure it out earlier. does it work ok now? got the upload to go to pypitest? ant -- https://mail.python.org/mailman/listinfo/python-list
next steps to make a python program more available
ok, i have the program going and installable for my local Linux distribution. aka i have it packaged and on PyPI and it runs from my local terminal command line. i have people available for testing it out on Windows and a Mac, but i don't have any way of knowing what to do to make the program appear in their Menu system to make it clickable. in MATE (which is what i run here) i can figure it out as something to do with the desktop, so somehow i have to do that, but i don't know what keywords to even search for to do that, desktop icons i guess and menu, but is there a more global way to set that up? XDG seems to be a part of that. i'm figuring if i get it working for MATE that it should also work for Gnome. i haven't touched KDE in quite a few years. Windows i'll figure out after the more Linux/Posix systems. so my priorties are about like: 1. posix/other linux sytems (perhaps Macs fit into this anways) 2. Macs 3. Windows luckily i do have other examples of python 3 programs which seem to be multiple platform oriented that i can look at and see what they've done. so i won't be forever lost in the wilderness... thanks for help, :) ant -- https://mail.python.org/mailman/listinfo/python-list
Re: advice needed for simple python web app
> You might also look at the docs for HTML::Mason (www.masonhq.com) to > get a look at a reasonably mature template system, even if you don't > plan to use it (because it's in Perl and not Python). I'm not sure if > CherryPy is directly comparable. I haven't yet used any of the Python > template systems. Mason is available for python http://www.myghty.org/ -- http://mail.python.org/mailman/listinfo/python-list
Re: Abuse of the object-nature of functions?
> Is there a way in python to say, "hey, catch everything but these two"? >>> try: ... raise AttributeError ... except Exception, ex: ... if isinstance(ex, AttributeError): ... print "Won't catch it!" ... raise ex ... Won't catch it! Traceback (most recent call last): File "", line 7, in ? AttributeError Or that sort of thing. -- http://mail.python.org/mailman/listinfo/python-list
Re: hash of hashes
sfo wrote: > how do i create a hash of hash similar to perl using dict in python > $x{$y}{z}=$z Haven't done any Perl in a long while (thankfully ;-) ) so I'm not quite sure on your syntax there, but here's how to do it in Python: >>> x = {'y': {'z': 'My value'}} >>> x['y']['z'] 'My value' Much easier to understand than that crazy perl syntax! -- http://mail.python.org/mailman/listinfo/python-list
Re: Abuse of the object-nature of functions?
> > try: > > # Do some stuff > > except Exception, err: > > if err not in (DontCatchMe1, DontCatchMe2): > > # Handle err > > > > HTH, > > ~Simon > > Dang! not only did somebody else beat me to it, but my code is wrong > and theirs correct. Ignoring the fact you haven't re-raised the exception (as we can ignore the fact mine doesn't handle the others ;-) ), it does show a different valid approach: mine has an advantage if the exceptions you don't want to handle inherit from a small number of base classes; yours has the advantage if there are a large number of unrelated exceptions that need ignoring. -- http://mail.python.org/mailman/listinfo/python-list
Re: How to display name of elements in list?
> I'm using a badly documented module and therefore need to find out > about how to access the elements in a list. > (I need to do this in Python 1.5.3) I presume this is the same in 1.5 use dir(): >>> import os >>> dir(os) ['F_OK', 'O_APPEND', 'O_BINARY', 'O_CREAT', 'O_EXCL', 'O_NOINHERIT', 'O_RANDOM', 'O_RDONLY', 'O_RDWR', 'O_SEQUENTIAL', 'O_SHORT_LIVED', 'O_TEMPORARY', 'O_TEXT', (etc) -- http://mail.python.org/mailman/listinfo/python-list
Re: Accessors in Python (getters and setters)
> Yes, it is possible to name crappy accessors too (e.g set_tmp/get_tmp). > But developers tend to pay more attention to given methods/functions > less crappy names, at least when compared to data attributes. This In my experience of getters and setters in Java, most developers choose attribute names first, and then use the IDE (Java without an IDE *really* sucks) to auto-generate the getters and setters. So most Java developers I have worked with pay more attention to attributes than accessor names as these are derived anyway. So I guess it depends on who the developers are ;-) -- http://mail.python.org/mailman/listinfo/python-list
Re: testing array of logicals
John Henry wrote: > Hi list, > > Is there a more elagant way of doing this? > > # logflags is an array of logicals > test=True > for x in logflags: >test = test and x > print test There's reduce, but it's not as explicit, and see F's post RE efficiency: >>> x = [True, True, True] >>> y = [True, False, True] >>> print reduce(lambda a, b: a and b, x) True >>> print reduce(lambda a, b: a and b, y) False >>> -- http://mail.python.org/mailman/listinfo/python-list
Re: Accessors in Python (getters and setters)
We seem to be flogging a dead horse now. Is the following a fair summary: Q. What is the Pythonic way of implementing getters and setters? A. Use attributes. Quote: "I put a lot more effort into choosing method and function names" Wisdom: Python is a different paradigm from (e.g.) Java w.r.t. accessors: Put the effort you would have put into choosing accessor names into choosing attribute names. Anything else to add to this? Or can it be put to bed? -- http://mail.python.org/mailman/listinfo/python-list
Re: Regular Expression problem
> So What should I do to get the exact value(here the value after > 'href=') in any case even if the > > tags are like these? >> > > > -OR- > > -OR- > The following should do it: expr = r'http://mail.python.org/mailman/listinfo/python-list
Re: Coding style
Christophe wrote: > ... you haven't beed using enouth generator expressions ... You should get yourself to the doctors about that cold dude. :-) -- http://mail.python.org/mailman/listinfo/python-list
Re: Simple file writing techiques ...
> fout = open('somefile','w') > for line in convertedData: > fout.write("%s\n" % line) > fout.close() > > -- or -- > > fout = open('somefile','w') > fout.write("%s" % '\n'.join(convertedData)) > fout.close() I shouldn't think it matters too much which of these you use - time them and see what happens. > An issue that I'm probably most concerned with is scalabitiy, what if > the file was huge, like some sort of log file. Sucking in the entire file into a list won't scale well, as a huge log file could quickly eat all of your available memory. You'd be better off processing each line as you go, and writing it to a temp file, renaming the temp file once you have finished. Something like: in_f = "access.log" out_f = "access.log.tmp" infile = open(in_f) outfile = open(out_f) for line in infile: outfile.write(process(line)) infile.close() outfile.close() os.remove(in_f) os.rename(out_f, in_f) (Not tested, but you get the idea...) -- http://mail.python.org/mailman/listinfo/python-list
Re: Simple file writing techiques ...
Whoops: > outfile = open(out_f) outfile = open(out_f, 'w') may be better ;-) -- http://mail.python.org/mailman/listinfo/python-list
Re: Accessors in Python (getters and setters)
Ed Jensen wrote: > > where the compiler prevents you from accessing > > private variables, but the runtime allows access to these very variables > > via reflection? > > Java does not allow access to private members via reflection. Yes it does. You can call setAccessible(true) on the Method object to override the privateness. -- http://mail.python.org/mailman/listinfo/python-list
Re: Accessors in Python (getters and setters)
Came across this article this afternoon - thought it may be of interest to some of those following this thread... http://www.devx.com/opensource/Article/31593/0/page/2 -- http://mail.python.org/mailman/listinfo/python-list
Re: Since there was talk of if-then-else not being allowed in lambda expressions, the following is from "Dive into Python"
> # python 2.5 > >>> a, b = "", 0 > >>> a if False else b > 0 > >>> a if True else b > '' > > Time to tear out that page. Really. Not quite - 2.5 hasn't been released in its final version yet, and many projects I should imagine will take a while to upgrade. -- http://mail.python.org/mailman/listinfo/python-list
Re: Smaple of recursive directory walker
> At work I have a directory of about 50 large text files and i need to > search thru them for 10 separate words and print how many were found > in total. > > I am new to python so could somebody please show me some sample code > that would help me get this done and i will work from that. Assuming it's primarily the directory walk you need help with, something like the following should help: for root, dirs, files in os.walk('~/mydir'): for file in [f for f in files if f.endswith(".txt")]: fh = open(file) for line in fh: # Search for words. fh.close() -- http://mail.python.org/mailman/listinfo/python-list
Re: Smaple of recursive directory walker
Traveler wrote: > yes this is great i will work from that but how can i use say a list > to pass 10 words? > > mylist = ['word1','word2','word3','word4'] ... > >for root, dirs, files in os.walk('~/mydir'): > >for file in [f for f in files if f.endswith(".txt")]: > >fh = open(file) > >for line in fh: > ># Search for words. > >fh.close() The following will allow you to search a line of text for one of a list of words. import re line = "line of text" mylist = ["bogus", "text", "here"] p = re.compile(r"\b(%s)\b" % '|'.join(mylist)) m = p.search(line) if m: print "Found %s" % m.group(1) Alternatively, the following will give a list of all words in a string that appear in the list: words_found = [word for word in re.split(r"\W+", line) if word in mylist] -- http://mail.python.org/mailman/listinfo/python-list
Re: Zipping files/zipfile module
Enabling directory recursion: > from os import listdir, mkdir > from os.path import join, basename, isfile > from zipfile import ZipFile > > def zip_dir(path, output_path, include_hidden=True): > try: > mkdir(output_path) > except OSError, e: > if e.errno == 17: # Path exists > pass > zip_file = ZipFile(join(output_path, 'temp.zip'), 'w') for root, dirs, files in os.walk(dir): for f in files: fp = path.join(root, f) zip_file.write(fp, fp[len(dir):]) # Write to zip as a path relative to original dir. > zip_file.close() -- http://mail.python.org/mailman/listinfo/python-list
Re: looking for a regular expression
> But what if there's not only commas, but also periods and semicolons? I > want to find words between 2 near by punctuations. I think it would make > it difficult to use split instead of regular expression. You could use re.split(r"\W", text) instead of the string split method to split on all non-word characters. Alternatively the regex you are looking for is probably r"\bjustice\b". -- http://mail.python.org/mailman/listinfo/python-list
Re: need help of regular expression genius
GHUM wrote: > I need to split a text at every ; (Semikolon), but not at semikolons > which are "escaped" within a pair of $$ or $_$ signs. Looking at you example SQL code, it probably isn't possible with regexes. Consider the code: $$ blah blah ... $$ blah; xxx $$ blah blah $$ Regexes aren't clever enough to count the number of backreferences, and so won't help in the above case. You'd be better off creating a custom parser using a stack or counter of some sort to decide whether or not to split the text. -- http://mail.python.org/mailman/listinfo/python-list
Re: Python Projects Continuous Integration
Harry George wrote: > [snip stuff about how to set emacs up as your IDE] Not sure which post you read, but the OP of this thread was asking about continuous integration, not integrated development environments. i.e. tools to *automatically* check out code when the repository has changed, build it if necessary (perhaps if there are C modules in the case of python) and run the unit tests. To the OP: you could of course simply continue to use cruise - it's only a tool after all, and won't require any additional learning if you are already having to learn a new language with all the associated libraries and idioms. -- http://mail.python.org/mailman/listinfo/python-list
Re: How to get hours and minutes from 'datetime.timedelta' object?
John Machin wrote: > Lad wrote: > > Hello, > > what is the best /easest way how to get number of hours and minutes > > from a timedelta object? ... > >>> diff.days > 0 > >>> diff.seconds > 52662 > >>> diff.microseconds > 922000 > >>> minutes = (diff.seconds + diff.microseconds / 100.0) / 60.0 > >>> minutes > 877.715368 I suspect what Lad wanted was something more like: >>> def secs_mins_hours(timed): ... total_secs = timed.seconds ... secs = total_secs % 60 ... total_mins = total_secs / 60 ... mins = total_mins % 60 ... hours = total_mins / 60 ... return (secs, mins, hours) >>> aa=datetime.datetime(2006, 7, 29, 16, 13, 56, 609000) >>> bb=datetime.datetime(2006, 8, 3, 17, 59, 36, 46000) >>> td = aa - bb >>> secs_mins_hours(td) (39, 45, 1) I'm surprised that the timedelta class hasn't got secs, mins and hours properties - they could be generated on the fly in a similar way. -- http://mail.python.org/mailman/listinfo/python-list
Re: How to get hours and minutes from 'datetime.timedelta' object?
John Machin wrote: ... > 1. If that's what he wanted, it was a very peculiar way of asking. Do > you suspect that he needs to be shown how to conver 877.7... minutes > into hours, minutes and seconds??? Chill dude, It wasn't an attack :-) The datetime class has hour, minute and second attributes that give the values of each as being in range(24) (hours) and range(60). i.e. integers. So an educated guess leads me to the conclusion that it is similar functionality that he wants from the timedelta class. > 2. Please consider that the order of the result would be more > conventionally presented as (hours, minutes, seconds) -- or do you Very good point. That would have been a tricky issue for the OP, and for that I apologise. > suspect that the OP needs it presented bassackwards? I think that you have that last word muddled. Not quite ass-backward, but close ;-) -- http://mail.python.org/mailman/listinfo/python-list
Re: is it possible to dividing up a class in multiple files?
Martin Höfling wrote: > Hi there, > > is it possible to put the methods of a class in different files? I just > want to order them and try to keep the files small. The editor leo (http://webpages.charter.net/edreamleo/front.html) gives you a way of handling large files in this way without actually having to split the class between files. A bit like a more powerful form of folding and narrowing text that some other editors have. But like others have noted, it is probably an indication that the class could use a bit of refactoring... -- http://mail.python.org/mailman/listinfo/python-list
Re: screensaver in Python
daniel Van der Borght wrote: > Programming a screensaver in Python, where and/or how di I start ? Google for "python screensaver". The first link has a module to use... -- http://mail.python.org/mailman/listinfo/python-list
Re: screensaver in Python
daniel Van der Borght wrote: > are you Chris ? anyway : thank you... No - I really am Ant. :-) -- http://mail.python.org/mailman/listinfo/python-list
Re: need an alternative to getattr()
> getattr(heading, "process")(file_ptr) ... > Is there an alternatice to getattr() that will solve my problem, or is > there another way to do it. How about: eval("%s.process(%s)" % (heading, file_ptr)) -- http://mail.python.org/mailman/listinfo/python-list
Re: Two Classes In Two Files
[EMAIL PROTECTED] wrote: > Yes, I have been ruined for the last 5 years with Java and C#. Perl was > my only salvation, but now I can't read the programs I wrote. ROFL! That's got to be a contender for Quote of the week. -- http://mail.python.org/mailman/listinfo/python-list
Re: keep a list of read and unread items
a wrote: > i m building an rss reader and i want you suggestions for datastructure > for keeping read and unread list for each use > i m assuming it will be very sparse A dictionary for each site seems to be the obvious choice, mapping the article ID to True or False. -- http://mail.python.org/mailman/listinfo/python-list
Re: Memory usage of an 'empty' python interpreter
[EMAIL PROTECTED] wrote: > I was wondering what the approximate amount of memory needed to load a > Python interpreter (only, no objects, no scripts, no nothing else) in a > Linux 2.6 environment. According to ps, it appears to be 3312 bytes, > which seems absurdly low to me. However, when I check the size of my > Python executable, it looks like it is only about 5600 bytes in size, > so maybe this is reasonable? Are you sure ps is reporting in bytes not KB? The bare interpreter in Windows is 3368KB. On my Gentoo server ps reports 2788KB for the bare interpreter. -- http://mail.python.org/mailman/listinfo/python-list
Re: Memory usage of an 'empty' python interpreter
> > Are you sure ps is reporting in bytes not KB? The bare interpreter in > > Windows is 3368KB. > > Where did you get that from? With Python 2.4.3, on my machine (Win XP > SP2): > > C:\junk>dir \python24\python* > [snip] > 29/03/2006 05:35 PM 4,608 python.exe > 29/03/2006 05:35 PM 1,871,872 python24.dll > 29/03/2006 05:35 PM 5,120 pythonw.exe He's asking for the memory required, not the disk space used by the exe. The 3368KB is reported by the Task Manager. -- http://mail.python.org/mailman/listinfo/python-list
Re: trouble understanding inheritance...
Try running the following example - it should help clear up what is going on: class Base: def __init__(self): print "Initializing base" def shouldBeImplemented(self): raise NotImplementedError def hasDefaultImplementation(self): print "Wey Hey!" class A(Base): def shouldBeImplemented(self): print "Has been implemented!" class B(Base): def __init__(self): Base.__init__(self) print 'Initializing B' class C(Base): def __init__(self): print "Initializing C" def hasDefaultImplementation(self): print "Boo Hoo!" base = Base() print "\n--- A " a = A() a.shouldBeImplemented() print "\n--- B " b = B() b.hasDefaultImplementation() print "\n--- C " c = C() c.hasDefaultImplementation() c.shouldBeImplemented() -- http://mail.python.org/mailman/listinfo/python-list
Re: text editor suggestion?
John Salerno wrote: > I'd really like to learn vim, but I spent days just trying to figure out > how to get the syntax highlighting and indentation working, where these > settings are and how to edit them, and it still doesn't work for me. It > just feels so insurmountable that I can't even start working with it yet > because I don't know how to tailor the settings. FWIW I started to use vim 2 years ago, and hated every minute of it. However, it was installed on every unix/linux box I have known, and so I gradually learned the most common commands. Recently I have been using gvim on windows, which comes pre-configured to syntax highlight etc. It isn't very good at running the current buffer as far as I can tell though, so I still have a command line open currently. jEdit is also a very good editor with the same sort of feature set as vim. Bit slower to load, but much more user freindly and a very powerful editor core. With a few extra plugins (console and Jython interpreter for example) it has all of the features you want, including he ability to write macros in python. (Note vim is also customisable using python). -- http://mail.python.org/mailman/listinfo/python-list
Re: trouble using "\" as a string
> such as tempname="\"..it says that the line is single qouted. The others have addressed the escape issue I think. However it looks like you want the funtionality of the os.path module. For example: >>> import os.path as path >>> filename = "/home/ant/test.sh" >>> filename2 = r"c:\python24\scripts\test.py" >>> path.split(filename) ('/home/ant', 'test.sh') >>> path.split(filename2) ('c:\\python24\\scripts', 'test.py') -- http://mail.python.org/mailman/listinfo/python-list
Bug in re module?
Look at the following minimal example: >>> import re >>> p = re.compile(r"(:?Test) (String)") >>> m = p.search("This is a Test String OK?") >>> m.groups() ('Test', 'String') I would have expected this to produce: ('String') since (:?...) should be a non-capturing group. From the module reference: (?:...) A non-grouping version of regular parentheses. Matches whatever regular expression is inside the parentheses, but the substring matched by the group cannot be retrieved after performing a match or referenced later in the pattern. -- http://mail.python.org/mailman/listinfo/python-list
Re: Bug in re module?
Ant wrote: > Look at the following minimal example: ... (snip example that shows non-capturing group capturing) Note I get the same results from python versions 2.4 and 2.5. -- http://mail.python.org/mailman/listinfo/python-list
Re: Bug in re module?
Just wrote: > Try ?: instead of :? Duh. Put it down to Friday afternoon! :-\ Don't know what I was thinking that something as high profile as that could slip through the net!! -- http://mail.python.org/mailman/listinfo/python-list
Re: Bug in re module?
John Machin wrote: > Now quick kill your post before the effbot spots it :-) Too late - the post was 3 minutes ago you know ;-) -- http://mail.python.org/mailman/listinfo/python-list
Re: humble coin head or tail game script I wrote
> # Get a list which contains 10 values from the user > # let them predict Head Or Tail in ten times coin thrown > # and then prdict the list by a fixed rule > > > list = [] > > print 'Predict Head or Tail in ten times coin thrown\nJust input \'h\' > or \'t\' please\n' > > count = 0 > while True: > count += 1 > print '\nNo.', count, ', h or t? ' > pre_user = raw_input() > while pre_user != 'h' and pre_user != 't': > print '\njust enter \'h\' or \'t\' please' > print '\nNo.', count, ', h or t? ' > pre_user = raw_input() > list.append(pre_user) > if count == 10: > break You can simplify this considerably: user_input = [] # list is a keyword so makes a bad variable name. while len(user_input) < 10: print '\nNo.', len(user_input) + 1, ', h or t? ' pre_user = raw_input() if pre_user not in ["h","t"]: # Note you can also use not in "ht" print '\njust enter \'h\' or \'t\' please' continue user_input.append(pre_user) print user_input HTH. -- http://mail.python.org/mailman/listinfo/python-list
Re: Is there an alternative to os.walk?
The idiomatic way of doing the tree traversal is: def search(a_dir): valid_dirs = [] for dirpath, dirnames, filenames in os.walk(a_dir): if dirtest(filenames): valid_dirs.append(dirpath) return valid_dirs Also since you are given a list of filenames in the directory, then why not just check the list of those files for your test files: def dirtest(filenames): testfiles = ['a','b','c'] for f in testfiles: if not f in filenames: return False return False You'd have to test this to see if it made a difference in performance, but it makes for more readable code -- http://mail.python.org/mailman/listinfo/python-list
Re: recommendations for personal journaling application
Donnie Rhodes wrote: ... > > Thank you all and I hope I'm not biting off too much at once... Not if you break it up into pieces. Look at the things you want to do, and in the first instance, create a function for each. Then you can start to fill in the blanks, and if neccessary ask back here for advice on each bit. For example, your skeleton script may look something like: def main(): options = get_options() text = fetch_body() entry_data = parse_text(text, options) store_entry(entry_data) def get_options(): pass def fetch_body() pass ... if __name__ == "__main__": main() Ideas for the various parts: get_options() - getopt or optparse modules (the former is simpler to start with); fetch_body() - just read from sys.stdin (that way you can also pipe text into it from a file or the output from another program as well); parse_text() - regexes could suffice if the flags and what they are supposed to do is simple, otherwise a grammar parsing module could be useful such as pyparsing (http://pyparsing.wikispaces.com/); store_entry() - I'd probably go with XML for the storage format if you really want to store the entries in a single text file, as you can structure it well, there are good tools in python 2.5 for building xml (xml.etree.ElementTree) and you could implement a fast search engine using SAX. Otherwise a database may be a better option (e.g. sqlite). -- http://mail.python.org/mailman/listinfo/python-list
Re: OT: Sarcasm and irony
Brian van den Broek wrote: ... > A quick check with the on-line text of the second edition of the > Oxford English Dictionary (sadly, a link only available by > subscription) gives as the first meaning: If we're going to start using dictionary definitions, then I claim that the following joke is truly ironic: An old blacksmith relized he was soon going to quit working so hard. He picked out a strong young man to become his apprentice. The old fellow was crabby and exacting. "Don't ask me a lot of questions," he told the boy. "Just do whatever I tell you to do." One day the old blacksmith took an iron out of the forge and laid it on the anvil. "Get the hammer over there," he said. "When I nod my head, hit it real good and hard." Now the town is looking for a new blacksmith. i‧ron‧ic  /aɪˈrɒnɪk/ Pronunciation[ahy-ron-ik] –adjective 1. containing or exemplifying irony: an ironic novel; an ironic remark. i‧ron‧y2  /ˈaɪərni/ Pronunciation[ahy-er-nee] –adjective consisting of, containing, or resembling iron. ;-) (In fact it is ironic in another more conventional sense: i‧ro‧ny1  /ˈaɪrəni, ˈaɪər-/ Pronunciation[ahy-ruh-nee, ahy-er-] –noun, plural -nies. ... 5. an outcome of events contrary to what was, or might have been, expected. ) -- http://mail.python.org/mailman/listinfo/python-list
Re: Converting MSWord Docs to PDF
Theerasak Photha wrote: > On 10/11/06, [EMAIL PROTECTED] <[EMAIL PROTECTED]> wrote: ... > (La)TeX is the king of document processing, and does PDF. Except that the OP want's to print Word documents as PDF. LaTeX is good, granted, but just try converting LaTeX documents to Word or vice versa... And if you want a good laugh, try telling a manager to write all his documentation in LaTeX. :-) -- http://mail.python.org/mailman/listinfo/python-list
Re: a little about regex
Rob Wolfe wrote: ... > def filter(adr):# note that "filter" is a builtin function also > import re > > allow = re.compile(r'.*(?|$)') # negative lookbehind > deny = re.compile(r'.*\.com\.my(>|$)') > cnt = 0 > if deny.search(adr): cnt += 1 > if allow.search(adr): cnt += 1 > return cnt Which makes the 'deny' code here redundant so in this case the function could be reduced to: import re def allow(adr):# note that "filter" is a builtin function also allow = re.compile(r'.*(?|$)') # negative lookbehind if allow.search(adr): return True return False Though having the explicit allow and deny expressions may make what's going on clearer than the fairly esoteric negative lookbehind. -- http://mail.python.org/mailman/listinfo/python-list
Re: I like python.
Fidel wrote: > Renaming the file doesn't work. I am on windows... There is a specific > line of code that tells python not to bother even opening a window. Seriously, renaming the script to .pyw should work from a standard python install. If it doesn't then the file handler for that extension must have got messed up somewhere along the way. You can fix this by right-clicking the renamed (*.pyw) file and selecting "open with..." -> "Choose Program" and finding the pythonw.exe. -- http://mail.python.org/mailman/listinfo/python-list
Re: FOR statement
Jordan Greenberg wrote: ... > >>> def printreverse(lst): > if lst: > printreverse(lst[1:]) > print lst[:1][0] Convoluted way of writing "print lst[0]" ! > >>> printreverse([1,2,3,4]) > > No good reason at all to do it this way. But recursion is fun. But there's a good reason not to. Try: printreverse(range(1000)) Recursion has a maximum depth (of 1000 by default) in Python. -- http://mail.python.org/mailman/listinfo/python-list
Re: sending vim text through Python filter
BartlebyScrivener wrote: > Hello, > > I'm sure this is my fault or some Windows snafu. But using gvim 7.0 on It's a bug in Windows. Try doing "sort.py < test.txt" from the command line, and you'll get the same error. Try "python sort.py < test.txt" and it should work fine. Apparently cmd.exe can't pick up the registered file extensions. > I'm sending standard text with the vim command :%!sort.py You'll have to use :%!python sort.py to get the filter to work. -- http://mail.python.org/mailman/listinfo/python-list
Re: Using Python scripts in Windows Explorer
Ben Sizer wrote: > > Create a shortcut and drop the file over it. ... > That is what I meant by 'the usual steps'. :) It doesn't work. Alter the target of the shortcut to something like: C:\Python25\python.exe C:\0\sort_test.py and drag and drop should work, with the filename of the dragged file being sent as a script argument. -- http://mail.python.org/mailman/listinfo/python-list
Re: The Python Journal
[EMAIL PROTECTED] wrote: ... > We'd love it if you could have a look at our first issue, and let us > know what you think! On the layout of the site: 1) I have to hit two separate Download buttons to get the PDF, and then the PDF must be viewed in an external reader rather than the browser plugin. 2) It appears that there are 3 publications to read, where in fact the third (The Python Journal itself) is an amalgam of the previous two documents. There should be some indication of this in the abstract (e.g. either a note in the Journal abstract that it consists of the previous articles, or a note in the article abstracts that it is a chapter in the journal) 3) I have to scan past two PDF pages to get to any actual content in the PDF's. 4) It would be nice to have an HTML view of these PDFs to save the irritation of waiting for the document to download and the PDF Reader from starting up. I realise that many of these issues could be limitations of the cgpublisher website, but those are my gripes with the site in any case. On the content of the first article: 1) There's a typo (well, indentation error) in the very first example (Text 2). 2) The title of the first article seems to promise more than it delivers. There are stacks of Python idioms, and this is merely one of them. If this is the first of many 'Idiom ' articles, it would be better to have a subtitle as to the nature of this specific idiom, something like "Access control in Python" or similar, with a mention of the actual name-mangling that goes on on 'private' attributes. HTH. Is the intention that this will be a free or non-free (or somewhere in between) journal BTW? -- http://mail.python.org/mailman/listinfo/python-list
Re: creating new objects with references to them
On Nov 2, 3:15 pm, "JohnJSal" <[EMAIL PROTECTED]> wrote: > It seems like what I want to do is something that programmers deal with > everyday, but I just can't think of a way to do it. Basically, I am > writing a DB front-end and I want a new "Researcher" object to be > created whenever the user presses the "New Record" button. He can open > as many new records at a time as he wants and fill in the information. > > What I don't know how to do is handle this arbitrary number of objects. > When it comes time to save the data to the DB, how do I access each > object (to get the text fields associated with each)? Won't I have to > have given each instance some name? It all depends on what UI you are using (Web frontend? GUI such as Tkinter?) and what your use case is. What is it exactly that you want to do? Create a bunch of Researcher objects and then save them in a single hit? Create a list of Researchers and then choose which to save to the db? Populate a list of researchers from the db, and have the option of editing or adding new ones? Should the new Researcher objects be committed to the db as soon as they are saved? Anyway, a simple list of Researchers should suffice for any of these purposes, and assuming you want to commit them all in one hit, you have a list of objects ready to iterate over. -- http://mail.python.org/mailman/listinfo/python-list
Problem exiting application in Windows Console.
Hi all, I'm putting together a simple help module for my applications, using html files stored in the application directory somewhere. Basically it sets up a basic web server, and then uses the webbrowser module to display it in the users browser. I have it set up to call sys.exit(0) if the url quit.html is called, but for some reason (on Windows XP via the cmd.exe shell) python won't let me have the console back. I get the System Exit stack trace OK: Exiting... Exception happened during processing of request from ('127.0.0.1', 3615) Traceback (most recent call last): ... File "C:\Documents and Settings\aroy\My Do... sys.exit(0) SystemExit: 0 However, at this point instead of getting back to a command prompt, I get an unresponsive console. Hitting CTRL-Break gets me the command prompt back, but I would have expected to get back the command prompt as soon as the sys.exit(0) had completed. Here's the code: import webbrowser, os, sys from threading import Thread from BaseHTTPServer import HTTPServer from SimpleHTTPServer import SimpleHTTPRequestHandler class HelpHTTPRequestHandler(SimpleHTTPRequestHandler): def do_GET(self): print "PATH: ", self.path if self.path.endswith("quit.html"): print "Exiting..." sys.exit(0) else: return SimpleHTTPRequestHandler.do_GET(self) def help(base_dir, server_class=HTTPServer, handler_class=HelpHTTPRequestHandler): os.chdir(base_dir) server_address = ('', 8000) httpd = server_class(server_address, handler_class) server_thread = Thread(target=httpd.serve_forever) server_thread.start() webbrowser.open("http://localhost:8000/index.html";) print "Hit CTRL-Break or CTRL-C to exit server" def main(): current_dir = os.path.split(sys.argv[0])[0] help(current_dir) if __name__ == "__main__": main() -- http://mail.python.org/mailman/listinfo/python-list
Re: Need help
Srinivasa wrote: > Hai friends, > I wrote a programme to display a window (ui) using Python. I renamed it > as .pyw to avoid popping-up the dos window while running it. It worked > fine. But when i converted it to executable file using py2exe; the dos > window appears. Can anybody tell me what the solution is. Would be easier to give you a proper example if you posted some code, esp the setup.py script. I'm guessing though that you are using the 'console' option to the setup function in your script rather than the 'windows' option. See help(py2exe) in the python console for more information. -- http://mail.python.org/mailman/listinfo/python-list
Exception Handling in TCPServer (was; Problem exiting application in Windows Console.)
Ant wrote: ... > However, at this point instead of getting back to a command prompt, I > get an unresponsive console. Hitting CTRL-Break gets me the command > prompt back, but I would have expected to get back the command prompt > as soon as the sys.exit(0) had completed. ... > class HelpHTTPRequestHandler(SimpleHTTPRequestHandler): > def do_GET(self): > print "PATH: ", self.path > if self.path.endswith("quit.html"): > print "Exiting..." > sys.exit(0) > else: > return SimpleHTTPRequestHandler.do_GET(self) > > def help(base_dir, server_class=HTTPServer, > handler_class=HelpHTTPRequestHandler): ... OK, I've narrowed the problem back to the way HTTPServer (actually its TCPServer parent) handles exceptions thrown by the process_request method by catching them all, and then calling a handle_error method. There doesn't seem to be a way of getting at the exception thrown however - does anyone know how I can get this information? The default handle_error method in the TCPServer uses the traceback module to print the stacktrace, but I can't find anything in that module to get the actual exception object (or string) - is there an alternative trick? Incidentally, this seems to me to be a pretty drastic try: except: block, catching *everything* and then (essentially) discarding the exception. Wouldn't it be better to either catch only the exceptions that are expected (presumably IO errors, Socket exceptions, HTTP error code exceptions etc), and let others pass through, or alternatively pass the exception through to the handle_error() method since this is where it should be dealt with? Thanks. -- http://mail.python.org/mailman/listinfo/python-list
Re: Exception Handling in TCPServer (was; Problem exiting application in Windows Console.)
Ant wrote: ... > OK, I've narrowed the problem back to the way HTTPServer (actually its > TCPServer parent) handles exceptions thrown by the process_request > method by catching them all, and then calling a handle_error method. > There doesn't seem to be a way of getting at the exception thrown > however - does anyone know how I can get this information? Hmm. Lonely topic ;-) I've found a way to solve the problem, by creating a subclass of HTTPServer which overrides the handle_error method: class HelpServer(HTTPServer): def handle_error(self, request, client_address): exception_line = inspect.trace()[-1][-2][0] if "sys.exit" in exception_line: print "Trying to exit again!" sys.exit(0) else: HTTPServer.handle_error(self, request, client_address) This seems a really nasty hack though - any ideas for a cleaner way to do it? -- http://mail.python.org/mailman/listinfo/python-list
Re: Exception Handling in TCPServer (was; Problem exiting application in Windows Console.)
Steve Holden wrote: ... > First of all, five hour response time is a high expectation, you must be > a Platinum customer :-) I'm in the last week of my current job - start a new one on Monday, and so I haven't got a great deal to do at the moment. Five hours is a lifetime when you're staring at a newsgroup waiting for it to change ;-) > Secondly, while a try/except catching all exceptions *is* unusual it's > justifiable in a server context (though some logging and/or analysis > certainly wouldn't go amiss). True. And I'd expected that the exception ought to be passed to the handle_error method so that something could be done with it there. This morning however I discovered that Guido's been in his time machine since yesterday and provided the very useful sys.exc_info() function for this very purpose! > Thirdly your "ugly hack" *could* be replaced by something cleaner with > more analysis of the trace structure, but given how infrequently this > code is going to run and the low probability that anything else will > trigger the hook I'd be happy with it as it is. But that's just me ... The sys.exc_info() was what I was looking for it turns out, I can get the exception type from that instead of trying to parse the stack trace. However: Gabriel Genellina wrote: ... > Replace serve_forever with your own loop checking an exit flag, and > set the flag in your "quit.html" handler instead of sys.exit(0) This is the approach I decided to go for. I found that RequestHandler objects have a reference to its server that I can use to control the server. Thanks for the input chaps! -- http://mail.python.org/mailman/listinfo/python-list
Re: Can not download plugins for jEdit (help!!)
On Nov 9, 6:52 am, "BillJosephson" <[EMAIL PROTECTED]> wrote: ... > If anyone knows why i can't connect, in info about it would be > a big help. Sounds like the default mirror is down. Go to Utilities -> Global Options -> Plugin Manager and click on "Update mirror list". Choose a new mirror and try again. If that still fails, it may be that you need to set up your proxy server properly. -- http://mail.python.org/mailman/listinfo/python-list
Re: Can not download plugins for jEdit (help!!)
> Vim, it can handle all the things.http://www.vim.org/ I'm not convinced of that quite yet. jEdit's syntax highlighting seems more robust (see SocketServer.py in the standard library for an example - vim gets the highlighting of the first doc-comment wrong). I've also not found anything like jEdit's Console plugin for vim. It's interactive. You can run the current buffer in the console window. The console interacts with an error reporting pane which allows you to click on the error and it will take you to the appropriate line in the code that the stack trace indicates. The only thing I have found for vim (and it is specific to running Python code) is the runscript.vim plugin, which merely allows you to view the stdout from the script in a different buffer. It doesn't allow you to interact (say you are writing a console based interactive application for example) nor does it have all of those other features I mentioned. I'm very interested in other people's vim setup for Python coding however, as I do use vim more than Python these days, mainly because I have to use it a great deal on headless servers (jEdit would be no good here of course) and I got fed up with adding artefacts such as extraneous i's, o's and :w's into my code when working with jEdit ;-) And it starts faster of course. -- http://mail.python.org/mailman/listinfo/python-list
Re: Can not download plugins for jEdit (help!!)
On Nov 9, 2:11 pm, Neil Cerutti <[EMAIL PROTECTED]> wrote: ... > """ never are. Then I changed the synchronize declarations in > /syntax/python.vim to the following: > > syn sync match pythonSync grouphere NONE '"""$' > syn sync maxlines=300 > > The above is no good for random Python code, though. > > But in random Python code you can simply increase the number of > lines inspected as high as your machine can handle, as documented > in python.vim. I commented out the maxlines line, and uncommented the syn sync minlines=2000 line, and that seems to have worked. > The reason I haven't adapted quickfix mode to Python is that Vim > would hide most of the Traceback. I'd constantly be executing the > command to show the entire error message, so I've chosen to > eschew quickfix mode for Python code. Sounds interesting - I may take a look at it. > ... default Python plugin provides the [[, ]] and [m, ]m commands > for jumping to the next function or method respectively. They are > a nice help once you're aware of them. There's a more powerful > plugin available on the internet, but it the code-jumps were the > main commands I wanted. They sound good. I've been using the taglist.vim plugin for code browsing which is pretty good for that sort of thing. > pytags should be in your Tools directory. I haven't found it > terribly useful, but in theory it's invaluable (my project is > piddly in size at the moment). I'll take a look. A code navigation tool I presume? > If you set shiftwidth to your preferred Python indent, then > indenting and unindenting code blocks is as easy as the < and > > commands Yes - I have that set up. Thanks for the tips :-) -- http://mail.python.org/mailman/listinfo/python-list
Re: Question regarding lists and regex
On Nov 9, 6:29 am, Prabhu Gurumurthy <[EMAIL PROTECTED]> wrote: ... > regex: I presume this is rather a dumb question, anyways here it comes! as you > can see from my program, pattIp = r\d{1,3}\ etc, is there any other easy > way > to group the reptitions, instead of typing the same regex 4 times. ... >pattIp = r'\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}' pattIp = r"\d{1,3}(\.\d{1,3}){3}" Is the best you can get using pure regexes (rather than something like Paul's solution). -- http://mail.python.org/mailman/listinfo/python-list
Re: Can not download plugins for jEdit (help!!)
> May I ask another quetion? I don't want to mess with lots of plugins > at this point. What are the minimum plugins to get a typical looking > IDE with a code window and output window and that lets me set > breakpoints and step through code? The Console plugin is a must (I think it depends on the ErrorList plugin which is also very useful as I noted above). JPyDebug is an interactive debugger - I have never used it mind, I usually rely on print statements to debug stuff. -- http://mail.python.org/mailman/listinfo/python-list
Re: Can not download plugins for jEdit (help!!)
On Nov 9, 3:27 pm, John Salerno <[EMAIL PROTECTED]> wrote: > Ant wrote: > > I do use vim more than Python these daysWhat does that mean? Are you > > referring to all the setup involved with vim? Whoops! I mean I use vim more than jEdit these days! -- http://mail.python.org/mailman/listinfo/python-list
Re: Newb: installing Jython on Windows XP ...
On 10 Nov, 10:29, Marcus Bajohr <[EMAIL PROTECTED]> wrote: > donkeyboy wrote: > > All, > > > I'm having issues installing Jython on Windows XP. I've looked on the ... > > Any help would be of great use!!! > Try it from cmd, not from the cygwin shell. > The environments differ ! Looking at the command prompt, he's not in the cygwin shell. I think that there's a bug in the latest jre from sun. I installed the 1.5.0_09 update a few days ago, and I can't get any apps to run with it (including irritatingly the Java Control Panel application so I can't change the default back easily...) The problems are NoClassDefFoundError's as well, so I wonder if the class loading is broken in that release... Anyway the solution seems to be to use an earlier jre explicitly: "c:\Program Files\Java\jdk1.5.0_06\bin\java.exe" jython-21 1.5.0_06 seems to work fine for me. HTH -- http://mail.python.org/mailman/listinfo/python-list
Selection in Tkinter Text widget.
Hi all, I have been trying to select text in a Text widget programmatically. I have been trying the following minimal example: #= from Tkinter import * def showgui(): win = Tk() area = Text(win, width = 50, height = 20) area.pack() new = """Lots of text here and here and here...""" area.insert("1.0", new) area.tag_add(SEL, "1.0", END) win.mainloop() if __name__ == "__main__": showgui() #== The area.tag_add(...) line should - from what I have read in Frederik's Intro to Tkinter guide - select all of the text in the text area. It doesn't however... Does anyone have any idea how to get this to work? Or tell me what I am doing wrong. Cheers, -- Ant... -- http://mail.python.org/mailman/listinfo/python-list
Re: Selection in Tkinter Text widget.
Fredrik Lundh wrote: ... > it does, but by default, the selection is only shown for widgets that has the > key- > board focus. if you add an explicit focus_set() call, you'll see the > selection. > > Perfect! Thanks Fredrik. Strange behaviour though (IMHO), that the selection is only shown if the widget has focus. I just tried adding another component to the test, and switching from widget to widget does indeed stop the selection showing! Cheers, -- Ant... -- http://mail.python.org/mailman/listinfo/python-list
Re: How do you practice Python?
> In our field, we don't always get to program in the language we'd like For sure! > to program. So... how do you practice Python in this case? Say you're > doing J2EE right now. How do you practice Python to keep your skills > sharp? Well, we have to use J2EE at work. I keep my Python skills going by playing the puzzles like the PythonChallenge you mentioned and the Maths Challenge Euler project (http://mathschallenge.net/index.php?section=project) They are good for getting the Python idioms and shortcuts nailed, such as list comprehensions, generators etc that aren't available in Java. I also use Python almost exclusively at home for my website, Wiki, Photo gallery etc - all of which I hand rolled partly for the experience, and partly to get them exactly the way I want them :-) At work I use Python for all my scripting needs. I also use it to automate running through our web-applications for test purposes, using a framework I wrote around the urllib2 module. So, there are plenty of opportunities to use it if you keep your eyes open. Unfortunately for me, using Python so much has made using J2EE very painful. Not so good seeing as it's my day job! -- http://mail.python.org/mailman/listinfo/python-list
Re: C# equivalent to range()
> That's because many of them have killfiled you. I actually wonder whether anyone does use killfiles. I just can''t imagine the sort of person who writes "plonk" or "welcome to my killfile" etc could bear to miss out on a reply to such a post in case there's more to get angry about! And people who are not riled enough by a post to comment on it probably would'nt be fired up enough to bother killfiling either... -- http://mail.python.org/mailman/listinfo/python-list
Re: XML, JSON, or what?
> to use? I could go back to XML, or I could switch to JSON - I have read I'd favour JSON if the data structures are simple personally. XML is comparatively speaking a pain to deal with, where with JSON you can simply eval() the data and you have a Python dictionary at your disposal. I recently used JSON as a way of passing data from a Java backend to a web page for Javascript to deal with, with the added side effect that my Python testing scripts could also easily read the same data. -- http://mail.python.org/mailman/listinfo/python-list
Re: XML, JSON, or what?
> Yes, evaling JSON, or any other text coming from the web, is definitely > a bad idea. > > But there's no need for eval: there are safe JSON codecs for python, Fair enough. And I should imagine that the codecs are still much faster and easier to use than XML for the same purpose. For my purposes, the JSON is pushed out to the web from our Java web-app, and eval'd in the test scripts which screen scrape the JSON structure from the web page - no danger in this case for me. But yes - I wouldn't be eval'ing random 'JSON' code from the web :-) -- http://mail.python.org/mailman/listinfo/python-list
Python Video processing.
Hi all, I have a specific task I want to automate - rotating a video file through 90 degrees. I've used the PIL library quite a bit to perform batch processing on images, and would like to do similar on video. Can anyone see a problem with the following: 1) Use pymedia to convert the video into a sequence of stills (e.g. http://pymedia.org/tut/src/dump_video.py.html) 2) Use PIL to process each still 3) Use pymedia to re-pack the still into video format. In particular, does anyone know whether the data obtained from decoding the video frame as in the following snippet from http://pymedia.org/tut/src/dump_video.py.html: dd= d.convert( fmt ) img= pygame.image.fromstring( dd.data, dd.size, "RGB" ) can be directly loaded into a PIL Image object (and back again?)? -- http://mail.python.org/mailman/listinfo/python-list
Re: Python Video processing.
> im = Image.fromstring("RGB", dd.size, dd.data) > > instead of doing that pygame.image call (not that the argument order is > different). > > for details, see the pygame tostring/fromstring docs, and the corresponding > PIL > methods: That's starting to look promising, yes - thanks! I'll give it a shot this evening and see what happens... -- http://mail.python.org/mailman/listinfo/python-list
Is there a better way of accessing functions in a module?
I have the following code which works fine for running some tests defined within a module: def a_test(): print "Test A" def b_test(): print "Test B" if __name__ == "__main__": tests = ["%s()" % x for x in dir() if x.endswith("test")] for test in tests: eval(test) But this feels like a hack... Is there a cleaner way for accessing the functions of the current module similar to the __dict__ attribute of classes? i.e. a way to access the local symbol table? -- http://mail.python.org/mailman/listinfo/python-list
Re: Is there a better way of accessing functions in a module?
Ant wrote: ... > But this feels like a hack... Is there a cleaner way for accessing the > functions of the current module similar to the __dict__ attribute of > classes? i.e. a way to access the local symbol table? Sorry - posted too soon. Found the globals() built-in... -- http://mail.python.org/mailman/listinfo/python-list
Re: Regular Expression pattern group
> I am a fussy learner. Could someone explain to me why the following > inconsistency exists between methods? How can it be justified if it is > considered all right? It's the standard way of accessing groups from regex matches in pretty much all languages that support them. In most modern languages, I believe regexes are generally designed to be compatible with Perl regexes - but these I guess derive from earlier languages like awk and sed. So it's not an inconsistency. Think of 0 as being an implicit group around the entire expression. In addition, if the match around the entire group was only accessible via a different method, then how would you refer to the entire match in back-references/substitutions? -- http://mail.python.org/mailman/listinfo/python-list
Re: a good programming text editor (not IDE)
> I've tried a few others, like TextPad and Crimson, and right now I use > UltraEdit, which I love actually, except for minor issues here and > there. But it'd be nice to make the move, as much as possible, to free, > open-source, cross-platform software. Vim is great if you have a good memory... Otherwise you end up trawling through the help to find out how to do stuff that would in another IDE be just a few menu clicks away. jEdit is for me still the best text editor available. Very extensible with macros (which can be written in Jython with the appropriate plugin installed). Full mapping of key-bindings to built in commands, plugin commands and macros a-la Vim, emacs etc. Also comes with a range of plugins if you do want that little bit more power... Java based so cross platform. -- http://mail.python.org/mailman/listinfo/python-list
urllib2 problem with ports.
Hi all, I have just moved to a new machine, and so have installed the latest version of Python (2.4.3 - previously I believe I was running 2.4.2). Unfortunately this seems to have broken urllib2... An app I wrote for testing our web application makes heavy use of urllib2 against the localhost, and I am getting the following problem (minimal code sample): #/usr/bin/python import urllib2 urllib2.urlopen("http://localhost:8080/";) This gives the output (Stack trace snipped - can post if required): urllib2.HTTPError: HTTP Error 502: Proxy Error ( The Uniform Resource Locator (U RL) does not use a recognized protocol. Either the protocol is not supported or the request was not typed correctly. Confirm that a valid protocol is in use (fo r example, HTTP for a Web request). ) This I believe is a problem with the addition of a non-default port. Replace the url with "http://google.com"; for example, and all is well. Any ideas for workarounds? Or do I roll back to 2.4.2? -- http://mail.python.org/mailman/listinfo/python-list
Re: a good programming text editor (not IDE)
John Salerno wrote: > Ant wrote: > > > jEdit is for me still the best text editor available. Very extensible > > with macros (which can be written in Jython with the appropriate plugin > > installed). > > I like the idea of being extensible, but of course I can only write in > Python. Are there any editors that support that? Jython is python (running on the Java platform) - just not a very recent version (2.1 currently IIRC). -- http://mail.python.org/mailman/listinfo/python-list
Re: a good programming text editor (not IDE)
John Salerno wrote: > Larry Bates wrote: > > > Nope, no Java knowledge necessary. Jython just compiles Python code > > to java bytecode instead of python bytecode. Once it is in java bytecode > > the JVM doesn't know where it came from. > > Well that's good to know. I guess there's not much of a point in writing > pure Python code with Jython, but at least now I know it works that way! That's right - but it's very useful for scripting Java (for example in jEdit) as you have access not only to the Python library, but to the Java API (which you just use as if they were Python modules). -- http://mail.python.org/mailman/listinfo/python-list
Re: urllib2 problem with ports.
Fredrik Lundh wrote: > check your proxy configuration. most likely, your new machine is set up > to route all requests via a remote proxy. Here's me looking like a fool :-) The parts of the machine (eg Firefox, GAIM etc) that I'd set up use a direct connection - it looks like the guy who'd had the machine before me used a proxy with IE... > (on windows, you'll find the proxy configuration under "Internet > Options" - "Connections" - "LAN Settings". before you do anything else, > make sure "Bypass proxy server for local addresses" is checked) Interestingly the "Bypass proxy server for local addresses" was checked. Anyway, I've changed it to a direct connection, and all seems well. Thanks. -- http://mail.python.org/mailman/listinfo/python-list
Re: a good programming text editor (not IDE)
> > Vim is great if you have a good memory... Otherwise you end up trawling > > through the help to find out how to do stuff that would in another IDE > > be just a few menu clicks away. > > Mental memory (the painful kind of memory) rapidly turns into muscle > memory (the fun kind of memory) and all of those mind boggling > keystrokes begin to feel like an artistic expression reminiscent of > playing the piano. That's probably true. I still have a long way to go to get the most out of vim though, because it takes a certain amount of mental memory to be able to use the keystrokes often enough to turn them into muscle memory... I tend to use gvim on my PC if I'm also working on my server via ssh - that way it doesn't matter if I type a few ESC :w's or :dd's. Otherwise I tend to use jEdit. -- http://mail.python.org/mailman/listinfo/python-list
Re: Python is fun (useless social thread) ;-)
> No, I learned it because Perl was too dirty and Java to complicated. > Now it is part of my daily job. Ditto. I was fed up of writing, compiling and running a java application just in order to do a quick script. I'd used perl, but quite frankly perl's a ridiculous language. Ruby looked promising, but perl had already poisoned my mind against the syntax... Python was clean, object oriented if you want it, and mature. > > Also, how did you go about learning it? > > Programming, reading this newsgroup, reading the python cookbook, > reading python source files of the standard library. > > Are there still some things you feel you need to learn or improve? I feel like I've got a pretty good grasp of the core language now, but there's always something that pops up in the group that I find I've not dabbled in (such as the itertools, which I discovered on the group last week(?) and have been using to great effect already!). -- http://mail.python.org/mailman/listinfo/python-list
Re: Extracting values from text file
> What I first though was if there was possible to make a filter such as: > > Apples (apples) > (ducks) Ducks > (butter) g butter Try something like: import re text = """> Some text that can span some lines. Apples 34 56 Ducks Some more text. """ filters = {"apples": re.compile(r"Apples\s+(\d+)"), "ducks": re.compile(r"(\d+)\s+Ducks"), "butter": re.compile(r"([0-9.]+)\s+g\s+butter")} out = [] for k,v in filters.iteritems(): matches = v.findall(text) for match in matches: out.append((k, match)) print out -- http://mail.python.org/mailman/listinfo/python-list
Re: Simple script to make .png thumbnails from .zip archive...
Try adapting the other posters example with something like: import Image, StringIO zip=zipfile.ZipFile(inURL,mode="r") picture=zip.read("00.jpg") image = Image.open(StringIO(picture)) image.thumbnail ((128,128), Image.ANTIALIAS) image.save (file + '.thumb.png') I haven't tested it, but something like this should work. -- http://mail.python.org/mailman/listinfo/python-list
urllib2 OpenerDirector question on usage.
Hello all, I am using urllib2 as a part of a web testing tool. One of the things I am testing is the effect of two different people perforing the same actions on the website - do they interfer with each other or not. So to emulate this, I essentially have the following function: def get_opener(): policy = cookielib.DefaultCookiePolicy(rfc2965=True) cj = cookielib.CookieJar(policy) return urllib2.build_opener(urllib2.HTTPCookieProcessor(cj)) Now am I right in thinking that since I am creating a separate CookieJar for each opener, then creating two openers and using one for each hypothetical user will achieve this effect - i.e. sessions will be separate from each other in the app, since the openers will return different session cookies back to the server. Cheers, Ant... -- http://mail.python.org/mailman/listinfo/python-list
Re: Chapter 9 Tutorial for Classes Not Working
> class MyClass: > "A simple example class" > i = 12345 > def f(self): > return 'hello world' Nothing wrong with this. > From here I run: > x = MyClass Here's your problem - x is a class, *not* an instance of a class (i.e. an object). Methods operate on objects, not classes. Try x = MyClass() instead to get an instance of the class. > xf = x.f > while True: >print xf() Why not try something simpler - it doesn't look like you really know what you are doing here. The while True is going to print your "hello world" until the end of time, and there is no value in assigning the function to a variable at this stage - its adding complexity which you don't seem to need at the moment... Try: x = MyClass() x.f() -- http://mail.python.org/mailman/listinfo/python-list
Re: Easier way to save result of a function?
> Thanks, that's awesome! Definitely not something I'd have ever been able > to work out myself - I think I need to learn more about nested functions > and introspection. I've recently found nested functions incredibly useful in many places in my code, particularly as a way of producing functions that are pre-set with some initialization data. I've written a bit about it here: http://antroy.blogspot.com/ (the entry about Partial Functions) > > def memoizeMethod(cls, n, m): > > def decorated(self): > > if n in self._memo: return self._memo[n] > > result = self._memo[n] = m(self) > > return result > > decorated.__name__ = n > > setattr(cls, n, decorated) Couldn't this be more simply written as: def memoizeMethod(cls, n, m): def decorated(self): if not n in self._memo: self._memo[n] = m(self) return self._memo[n] decorated.__name__ = n setattr(cls, n, decorated) I've not seen the use of chained = statements before. Presumably it sets all variables to the value of the last one? -- http://mail.python.org/mailman/listinfo/python-list
Re: eval to dict problems NEWB going crazy !
> [('recId', 3), ('parse', {'pos': u'np', 'gen': u'm'})] > [('recId', 5), ('parse', {'pos': u'np', 'gen': u'm'})] > # line injected by a malicious user > "__import__('os').system('echo if I were bad I could do worse')" > [('recId', 7 ), ('parse', {'pos': u'np', 'gen': u'm'})] I'm curious, if you disabled import, could you make eval safe? For example: >>> eval("__import__('os').system('echo if I were bad I could do worse')") if I were bad I could do worse 0 >>> eval("__import__('os').system('echo if I were bad I could do worse')", >>> {'__import__': lambda x:None}) Traceback (most recent call last): File "", line 1, in ? File "", line 0, in ? AttributeError: 'NoneType' object has no attribute 'system' So, it seems to be possible to disable access to imports, but is this enough? Are there other ways to access modules, or do damage via built-in commands? It seems that there must be a way to use eval safely, as there are plenty of apps that embed python as a scripting language - and what's the point of an eval function if impossible to use safely, and you have to write your own Python parser!! -- http://mail.python.org/mailman/listinfo/python-list