Re: len() should always return something
Dr. Phillip M. Feldman schrieb: Some aspects of the Python design are remarkably clever, while others leave me perplexed. Here's an example of the latter: Why does len() give an error when applied to an int or float? len() should always return something; in particular, when applied to a scalar, it should return a value of 1. Of course, I can define my own function like this: def mylen(x): if isinstance(x,int) or isinstance(x,float): return 1 return len(x) But, this shouldn't be necessary. Can you show some example of where that is actually making a piece of code more elegant? Diez -- http://mail.python.org/mailman/listinfo/python-list
Re: len() should always return something
On Thu, Jul 23, 2009 at 11:35 PM, Dr. Phillip M. Feldman wrote: > > Some aspects of the Python design are remarkably clever, while others leave > me perplexed. Here's an example of the latter: Why does len() give an error > when applied to an int or float? len() should always return something; in > particular, when applied to a scalar, it should return a value of 1. Of > course, I can define my own function like this: > > def mylen(x): > if isinstance(x,int) or isinstance(x,float): return 1 > return len(x) > > But, this shouldn't be necessary. The problem is that redefining len()/length/size that way would violate several principles of Python's design (The "Zen" of Python - http://www.python.org/dev/peps/pep-0020/). Specifically: - Explicit is better than implicit. - Special cases aren't special enough to break the rules. - Errors should never pass silently. - In the face of ambiguity, refuse the temptation to guess. If you'd explain the situation that prompts you to find this redefinition necessary, I'm sure someone can suggest a better approach. Cheers, Chris -- http://blog.rebertia.com -- http://mail.python.org/mailman/listinfo/python-list
Re: len() should always return something
Dr. Phillip M. Feldman wrote: > Some aspects of the Python design are remarkably clever, while others > leave me perplexed. Here's an example of the latter: Why does len() give > an error when applied to an int or float? len() should always return > something; in particular, when applied to a scalar, it should return a > value of 1. Of course, I can define my own function like this: > > def mylen(x): >if isinstance(x,int) or isinstance(x,float): return 1 >return len(x) > > But, this shouldn't be necessary. Python should not blur the distinction between vectors an scalars like that. Instead of trying to be clever you should pass a vector with a single item and send mylen() to /dev/null. On a general note, I think one of Python's strengths is that it consistently /avoids/ this kind of cleverness. A prominent example is the handling of "1" + 1. Peter -- http://mail.python.org/mailman/listinfo/python-list
Re: Looking for os.listdir() generator
Christian Heimes wrote: > Nick Craig-Wood wrote: > > Christian Heimes wrote: > >> I'm looking for a generator version of os.listdir() for Python 2.5 and > >> newer. I know somebody has worked on it because I've seen a generator > >> version in a posting on some list or blog a while ago. I can't find it > >> anymore. It seems my Google fu is lacking today. All I can find is a > >> very old version of xlistdir. A Cython based solution is appreciated but > >> please no ctypes version. > > > > I posted exactly that yesterday I think ;-) > > > > Note that this has a python part as well as a ctypes part because the > > version of ctypes I used doesn't support generators. I think the > > development version does though. > > Thanks Nick! > > ctypes? I'm sure you wanted to say Cython :) Er, yes! > If you don't mind I'm going to wrap it up into a nice package and > release it on PyPI. Yes thats fine with me! -- Nick Craig-Wood -- http://www.craig-wood.com/nick -- http://mail.python.org/mailman/listinfo/python-list
Re: ANN: psyco V2
On 23-07-2009, Christian Tismer wrote: > On 7/17/09 4:11 AM, Bearophile wrote: >> Very good, thank you. I'll try it when I can. >> >> Is Psyco3 going to borrow/steal some ideas/code from Unladen Swallow? > > Psyco3: nice typo! :-) > > Well, I haven't so far found a new idea there that I'd want > to borrow and did not know from PyPy, before. > Wasn't the project plan saying the opposite, borrowing > some ideas from psyco? :-) > http://code.google.com/p/unladen-swallow/wiki/ProjectPlan How do you see the futur of psyco when unladen-swallow will grab the best of psyco (if they can !) ? Wait and see ? Anyway, thanks a lot for your work that we can use NOW ! -- William Dodé - http://flibuste.net Informaticien Indépendant -- http://mail.python.org/mailman/listinfo/python-list
Re: If Scheme is so good why MIT drops it?
On 2009-07-23 13:15:00 -0400, Isaac Gouy said: I get the feeling I'm missing the joke? Yes, you are missing the joke. The point is that if python is 60x slower than C, even if there were not a GIL, it would require running the python program on a 60 core machine just reach parity with C. The existence of the GIL means that in reality you'd probably need a several hundred core machine running python just to equal what C can do on one core. Hence the 13375p34k pseudo quote - "teh slowness on all ur cores!" -- Raffael Cavallaro -- http://mail.python.org/mailman/listinfo/python-list
Re: integer square roots
Thanks to all! Tim -- http://mail.python.org/mailman/listinfo/python-list
Predefined Variables
Is there a pre-defined variable that returns the GET line (http://www.php.net/index.php?everythingafterthequestionmark) as a single variable (rather than individual variables)? Regards, Fred -- http://mail.python.org/mailman/listinfo/python-list
Re: Predefined Variables
os.environment('QUERY_STRING')
"Fred Atkinson" wrote in message
news:[email protected]...
> Is there a pre-defined variable that returns the GET line
> (http://www.php.net/index.php?everythingafterthequestionmark) as a
> single variable (rather than individual variables)?
>
> Regards,
>
>
>
> Fred
--
http://mail.python.org/mailman/listinfo/python-list
Re: Predefined Variables
$_SERVER['QUERY_STRING']; (if it's PHP) "Fred Atkinson" wrote in message news:[email protected]... > Is there a pre-defined variable that returns the GET line > (http://www.php.net/index.php?everythingafterthequestionmark) as a > single variable (rather than individual variables)? > > Regards, > > > > Fred -- http://mail.python.org/mailman/listinfo/python-list
Re: Gedcom and Genealogy
On Friday 24 July 2009 00:14:19 Gordon wrote: > We have many small libraries in JAVA or Ruby that need to be ported to > Python. Actually it's so simple a rewrite is possible too. Is this: 1 - A question? 2 - A job offer? 3 - A piece of random news? - Hendrik -- http://mail.python.org/mailman/listinfo/python-list
Adding method from one class to another class or to instance of another class
Hi, I have one class (A) that has defined method createVars. I would like to add that method to class B The code looks like this: class A(object): def createVars(self): self.v1 = 1 self.v2 = 3 pass class B(object): pass I don't want to use inheritance (because class A has many methods defined that class B doesn't need). When I try the folloowing: B.createVars = C.createVars B().createVars() then the following error occurs: Traceback (most recent call last): File "", line 1, in TypeError: unbound method createVars() must be called with A instance as first argument (got nothing instead) When I try to add the createVars method to instance of B: >>> b=B() >>> b.createVars = new.instancemethod(A.createVars, b, B) >>> b.createVars > >>> b.createVars() Then the following error raises: Traceback (most recent call last): File "", line 1, in TypeError: unbound method createVars() must be called with A instance as first argument (got B instance instead) How can I solve this problem? Regards, Marek -- http://mail.python.org/mailman/listinfo/python-list
mmap 2GB allocation limit on Win XP, 32-bits, Python 2.5.4
OS: Win XP SP3, 32 bit Python 2.5.4 Hi I have run into some problems with allocating numpy.memmaps exceeding and accumulated size of about 2 GB. I have found out that the real problem relates to numpy.memmap using mmap.mmap I've written a small test program to illustrate it: import itertools import mmap import os files = [] mmaps = [] file_names= [] mmap_cap=0 bytes_per_mmap = 100 * 1024 ** 2 try: for i in itertools.count(1): file_name = "d:/%d.tst" % i file_names.append(file_name) f = open(file_name, "w+b") files.append(f) mm = mmap.mmap(f.fileno(), bytes_per_mmap) mmaps.append(mm) mmap_cap += bytes_per_mmap print "Created %d writeable mmaps containing %d MB" % (i, mmap_cap/(1024**2)) #Clean up finally: print "Removing mmaps..." for mm, f, file_name in zip(mmaps, files, file_names): mm.close() f.close() os.remove(file_name) print "Done..." which creates this output Created 1 writeable mmaps containing 100 MB Created 2 writeable mmaps containing 200 MB Created 17 writeable mmaps containing 1700 MB Created 18 writeable mmaps containing 1800 MB Removing mmaps... Done... Traceback (most recent call last): File "C:\svn-sandbox\research\scipy\scipy\src\com\terma\kha \mmaptest.py", line 16, in mm = mmap.mmap(f.fileno(), bytes_per_mmap) WindowsError: [Error 8] Not enough storage is available to process this command There is more than 25 GB of free space on drive d: at this stage. Is it a bug or a "feature" of the 32 bit OS? I am surprised about it as I have not found any notes about these kinds of limitations in the documentation. I am in dire need of these large memmaps for my task, and it is not an option to change OS due to other constraints in the system. Is there anything I can do about it? Best wishes, Kim -- http://mail.python.org/mailman/listinfo/python-list
Re: What is file.encoding convention?
On Jul 24, 4:10 am, Naoki INADA wrote: > > Yes! I confused by it. > > s/I confused/I am confused/ > > > "Writing unicode to a file(-like)" is a simple requirement. > > Does python have any simple resolution for it? > > s/resolution/solution/ > Of course, Python 3 has much better Unicode support: - C:\Users\Vinay>chcp 1251 Active code page: 1251 C:\Users\Vinay>\python31\python ActivePython 3.1.0.1 (ActiveState Software Inc.) based on Python 3.1 (r31:73572, Jun 28 2009, 19:55:39) [MSC v.1500 32 bit (Intel)] on win 32 Type "help", "copyright", "credits" or "license" for more information. >>> import sys >>> sys.stdout.encoding 'cp1251' >>> u = '\u0434\u043e \u0441\u0432\u0438\u0434\u0430\u043d\u0438\u044f' >>> print(u) до свидания >>> n = sys.stdout.write(u) до свидания>>> ^Z - Regards, Vinay Sajip -- http://mail.python.org/mailman/listinfo/python-list
Re: mmap 2GB allocation limit on Win XP, 32-bits, Python 2.5.4
Slaunger schrieb: OS: Win XP SP3, 32 bit Python 2.5.4 Hi I have run into some problems with allocating numpy.memmaps exceeding and accumulated size of about 2 GB. I have found out that the real problem relates to numpy.memmap using mmap.mmap I've written a small test program to illustrate it: import itertools import mmap import os files = [] mmaps = [] file_names= [] mmap_cap=0 bytes_per_mmap = 100 * 1024 ** 2 try: for i in itertools.count(1): file_name = "d:/%d.tst" % i file_names.append(file_name) f = open(file_name, "w+b") files.append(f) mm = mmap.mmap(f.fileno(), bytes_per_mmap) mmaps.append(mm) mmap_cap += bytes_per_mmap print "Created %d writeable mmaps containing %d MB" % (i, mmap_cap/(1024**2)) #Clean up finally: print "Removing mmaps..." for mm, f, file_name in zip(mmaps, files, file_names): mm.close() f.close() os.remove(file_name) print "Done..." which creates this output Created 1 writeable mmaps containing 100 MB Created 2 writeable mmaps containing 200 MB Created 17 writeable mmaps containing 1700 MB Created 18 writeable mmaps containing 1800 MB Removing mmaps... Done... Traceback (most recent call last): File "C:\svn-sandbox\research\scipy\scipy\src\com\terma\kha \mmaptest.py", line 16, in mm = mmap.mmap(f.fileno(), bytes_per_mmap) WindowsError: [Error 8] Not enough storage is available to process this command There is more than 25 GB of free space on drive d: at this stage. Is it a bug or a "feature" of the 32 bit OS? It's a limitation, yes. That's what 64-bit-OSes are for. I am surprised about it as I have not found any notes about these kinds of limitations in the documentation. I am in dire need of these large memmaps for my task, and it is not an option to change OS due to other constraints in the system. Is there anything I can do about it? Only by partitioning data yourself, and accessing these partitions. Like in the good old days of DOS-programming. Diez -- http://mail.python.org/mailman/listinfo/python-list
Re: What is file.encoding convention?
> Naoki INADA (NI) wrote: >NI> "Writing unicode to a file(-like)" is a simple requirement. >NI> Does python have any simple resolution for it? Yes, Python 3 will do this. For Python < 3.0 you will have to use a codecs wrapper or explicitely do the encoding. -- Piet van Oostrum URL: http://pietvanoostrum.com [PGP 8DAE142BE17999C4] Private email: [email protected] -- http://mail.python.org/mailman/listinfo/python-list
Re: effbot.org broken (WAS: Problems in commands.getoutput(cmd) with sox)
> Chris Rebert (CR) wrote: >CR> On Thu, Jul 23, 2009 at 12:42 PM, Chris Rebert wrote: >>> You can use tabnanny to help diagnose the problem: >>> http://74.125.155.132/search?q=cache:QtxvZm3QDLsJ:effbot.org/librarybook/tabnanny.htm+tabnanny&cd=3&hl=en&ct=clnk&gl=us&client=firefox-a >CR> Anyone know what's the deal with effbot.org? It seems to be broken at >CR> present, forcing me to use Google's cached version. >CR> It's a real shame since it has lots of handy Python info. Just try again. -- Piet van Oostrum URL: http://pietvanoostrum.com [PGP 8DAE142BE17999C4] Private email: [email protected] -- http://mail.python.org/mailman/listinfo/python-list
Re: Adding method from one class to another class or to instance of another class
marekw2143 wrote: > Hi, > > I have one class (A) that has defined method createVars. I would like > to add that method to class B > The code looks like this: > > > class A(object): >def createVars(self): > self.v1 = 1 > self.v2 = 3 > pass > > class B(object): >pass > > > I don't want to use inheritance (because class A has many methods > defined that class B doesn't need). You can move createVars() into a mixin or common base class: class M(object): def createVars(self): ... class A(M): ... class B(M) ... > When I try the folloowing: > > > B.createVars = C.createVars > B().createVars() > > > then the following error occurs: > Traceback (most recent call last): > File "", line 1, in > TypeError: unbound method createVars() must be called with A instance > as first argument (got nothing instead) > > When I try to add the createVars method to instance of B: > b=B() b.createVars = new.instancemethod(A.createVars, b, B) b.createVars > > b.createVars() > > > > Then the following error raises: > > > Traceback (most recent call last): > File "", line 1, in > TypeError: unbound method createVars() must be called with A instance > as first argument (got B instance instead) > > > > How can I solve this problem? >>> class A(object): ... def create_vars(self): ... self.x = 42 ... >>> class B(object): pass ... >>> B.create_vars = A.create_vars.im_func >>> b = B() >>> b.create_vars() >>> b.x 42 An alternative I find a bit cleaner: >>> def create_vars(self): self.x = 42 ... >>> class A(object): ... create_vars = create_vars ... >>> class B(object): ... create_vars = create_vars ... >>> b = B() >>> b.create_vars() >>> b.x 42 Peter -- http://mail.python.org/mailman/listinfo/python-list
Re: available formats and params for Image.save()
En Thu, 23 Jul 2009 06:56:45 -0300, News123 escribió: Somehow I have difficulties reading the documentation for PIL (Image) Is there an easy way to know which formats are supported and what their names are? py> import PIL py> from PIL import Image py> Image.ID [] py> Image.init() py> Image.ID ['PNG', 'ARG', 'BMP', 'BUFR', 'CUR', 'PCX', 'DCX', 'EPS', 'FITS', 'FLI', 'FPX', 'GBR', 'GIF', 'GRIB', 'HDF5', 'ICNS', 'ICO', 'IM', 'IMT', 'IPTC', 'JPEG', 'MCIDA S', 'TIFF', 'MIC', 'MPEG', 'MSP', 'PCD', 'PIXAR', 'PPM', 'PSD', 'SGI', 'SPIDER', 'SUN', 'TGA', 'WBMP', 'WMF', 'XBM', 'XPM', 'XVTHUMB'] py> Image.OPEN.keys() ['PCX', 'ICNS', 'HDF5', 'SUN', 'MIC', 'EPS', 'MSP', 'FLI', 'FITS', 'GBR', 'WBMP' , 'PCD', 'PIXAR', 'BUFR', 'PPM', 'WMF', 'SGI', 'BMP', 'TGA', 'DCX', 'ICO', 'CUR' , 'XPM', 'TIFF', 'JPEG', 'SPIDER', 'GIF', 'GRIB', 'IM', 'IMT', 'IPTC', 'FPX', 'X BM', 'MPEG', 'PSD', 'ARG', 'XVTHUMB', 'PNG', 'MCIDAS'] py> Image.SAVE.keys() ['XBM', 'PCX', 'SPIDER', 'HDF5', 'TIFF', 'BUFR', 'EPS', 'JPEG', 'MSP', 'GRIB', ' GIF', 'BMP', 'IM', 'PPM', 'PDF', 'FITS', 'PALM', 'WBMP', 'WMF', 'PNG'] Is there an easy way to know which parameters are supported by Image.save(). How can I list them where are they documented? That depends on the format being used. The PIL handbook lists the standard formats used and their parameters: http://www.pythonware.com/library/pil/handbook/index.htm I'm at a complete loss at finding out what parameters the save function accepts for saving a JPG file or a PNG file http://www.pythonware.com/library/pil/handbook/format-jpeg.htm -- Gabriel Genellina -- http://mail.python.org/mailman/listinfo/python-list
Re: invoke method on many instances
En Thu, 23 Jul 2009 21:27:35 -0300, Aahz escribió: In article , Gabriel Genellina wrote: NLMPI What? IHNFI -- Gabriel Genellina -- http://mail.python.org/mailman/listinfo/python-list
how to get no value
Hi,
I have a file having lines:-
48 ALA H = 8.33 N = 120.77 CA = 55.18 HA = 4.12 C = 181.50
104 ALA H = 7.70 N = 121.21 CA = 54.32 HA = 4.21 C =
85 ALA H = 8.60 N = CA = HA = 4.65 C =
Now i want to make two another file in which i want to put those lines for
which C is missing and another one for which N,CA and C all are missing,
I tried in this way:
import re
f = open('chem.txt')
for line in f:
if re.search('C = ',''):
print line
but i am not getting the desired output.
Amrita Kumari
Research Fellow
IISER Mohali
Chandigarh
INDIA
--
http://mail.python.org/mailman/listinfo/python-list
Re: OverflowError in RLock.acquire()
En Fri, 24 Jul 2009 01:27:10 -0300, David Roberts escribió: I'm trying to port a Python application to Windows, and I'm getting the following error (which did not occur when running on Linux): Exception in thread Thread-4: File "C:\Python26\lib\threading.py", line 803, in currentThread return _active[_get_ident()] OverflowError: can't convert negative value to unsigned long Looks like a bug in the thread module - you should report it at http://bugs.python.org -- Gabriel Genellina -- http://mail.python.org/mailman/listinfo/python-list
Convert points to polygon shapefile
Dear all, I would like to know how to convert a list of points into a polygon shapefile (esri). Thanks! Best regards, Luis Pedro Almeida -- http://mail.python.org/mailman/listinfo/python-list
Re: mmap 2GB allocation limit on Win XP, 32-bits, Python 2.5.4
> Slaunger (S) wrote: >S> OS: Win XP SP3, 32 bit >S> Python 2.5.4 >S> Hi I have run into some problems with allocating numpy.memmaps >S> exceeding and accumulated size of about 2 GB. I have found out that >S> the real problem relates to numpy.memmap using mmap.mmap On Windows XP the virtual address space of a process is limited to 2 GB unless the /3GB switch is used in the Boot.ini file. http://www.microsoft.com/whdc/system/platform/server/PAE/PAEmem.mspx -- Piet van Oostrum URL: http://pietvanoostrum.com [PGP 8DAE142BE17999C4] Private email: [email protected] -- http://mail.python.org/mailman/listinfo/python-list
Re: how to get no value
On Fri, 24 Jul 2009 15:50:11 +0530 (IST) [email protected] wrote: > but i am not getting the desired output. Show us what output you got and what you desired. -- D'Arcy J.M. Cain | Democracy is three wolves http://www.druid.net/darcy/| and a sheep voting on +1 416 425 1212 (DoD#0082)(eNTP) | what's for dinner. -- http://mail.python.org/mailman/listinfo/python-list
Re: how to get no value
[email protected] schrieb: Hi, I have a file having lines:- 48 ALA H = 8.33 N = 120.77 CA = 55.18 HA = 4.12 C = 181.50 104 ALA H = 7.70 N = 121.21 CA = 54.32 HA = 4.21 C = 85 ALA H = 8.60 N = CA = HA = 4.65 C = Now i want to make two another file in which i want to put those lines for which C is missing and another one for which N,CA and C all are missing, I tried in this way: import re f = open('chem.txt') for line in f: if re.search('C = ',''): print line but i am not getting the desired output. Gosh. Must be groundhog-day. Again. And there is me thinking that my job could be endangered by cheap & qualified indian soft-workers - can't be to many of them around if the OP doesn't get a hold of one for the better part of a month now. Must be one of those management myths they tell you to scare you into a less well paid contract... Diez -- http://mail.python.org/mailman/listinfo/python-list
Re: Help understanding the decisions *behind* python?
On Jul 22, 9:36 am, Hendrik van Rooyen wrote: > On Tuesday 21 July 2009 15:49:59 Inky 788 wrote: > > > My guess is that it was probably for optimization reasons long ago. > > I've never heard a *good* reason why Python needs both. > > The good reason is the immutability, which lets you use > a tuple as a dict key. > On Jul 22, 9:36 am, Hendrik van Rooyen wrote: > On Tuesday 21 July 2009 15:49:59 Inky 788 wrote: > > > My guess is that it was probably for optimization reasons long ago. > > I've never heard a *good* reason why Python needs both. > > The good reason is the immutability, which lets you use > a tuple as a dict key. > The *technical* reason is immutability for dict keys. Dict could allow mutable objects as keys by comparing *by value*, making a copy on insertion and hashing the current value on lookup. Prior art: the 2.3 sets module allows mutable Sets as elements in Sets, by making ImmutableSet copies on insertion, and hashing Sets as if they are temporarily immutable on lookup. This inspired PEP 351 and ambitious proposals to expand the approach to all Python with a copy-on-write scheme. But these ideas were rejected, and the 2.4 builtin sets only allow frozenset elements. Half the reason is technical: copy-on-write and harder than it sounds, and without it you pay a performance price. But the deeper reason is style: immutable types are convenient! The allow a pure-functional style of code, which can be simpler. Of course, sometimes an imperative style is simpler. Depends on the problem. My rule of thumb: - Use mutable lists when you are likely to modify individual elements. - Use immutable tuples when you are likely to replace the whole thing. (In practice, this boils down to similar usage to the "official" rule of thumb that lists are for homogenous data, and tuples for heterogenous records.) -- http://mail.python.org/mailman/listinfo/python-list
Re: OverflowError in RLock.acquire()
Done: http://bugs.python.org/issue6562 -- David Roberts http://da.vidr.cc/ On Fri, Jul 24, 2009 at 20:24, Gabriel Genellina wrote: > En Fri, 24 Jul 2009 01:27:10 -0300, David Roberts escribió: > >> I'm trying to port a Python application to Windows, and I'm getting >> the following error (which did not occur when running on Linux): >> >> Exception in thread Thread-4: >> File "C:\Python26\lib\threading.py", line 803, in currentThread >> return _active[_get_ident()] >> OverflowError: can't convert negative value to unsigned long > > Looks like a bug in the thread module - you should report it at > http://bugs.python.org > > -- > Gabriel Genellina > > -- > http://mail.python.org/mailman/listinfo/python-list > -- http://mail.python.org/mailman/listinfo/python-list
Re: how to get no value
> [email protected] (a) a écrit: >a> Hi, >a> I have a file having lines:- >a> 48 ALA H = 8.33 N = 120.77 CA = 55.18 HA = 4.12 C = 181.50 >a> 104 ALA H = 7.70 N = 121.21 CA = 54.32 HA = 4.21 C = >a> 85 ALA H = 8.60 N = CA = HA = 4.65 C = >a> Now i want to make two another file in which i want to put those lines for >a> which C is missing and another one for which N,CA and C all are missing, >a> I tried in this way: >a> import re >a> f = open('chem.txt') >a> for line in f: >a> if re.search('C = ',''): >a> print line >a> but i am not getting the desired output. You never look in the lines you read. if re.search('C = ',''): should be if re.search('C = ', line): Do you really think before you write your code? Or look at it after you have written it? Sorry if I offend you but you give the impression of just trying some more or less random stuff and then asking here if it doesn't give the required result. That's not the attitude of a researcher, unless your definition of research is 'asking on Usenet'. -- Piet van Oostrum URL: http://pietvanoostrum.com [PGP 8DAE142BE17999C4] Private email: [email protected] -- http://mail.python.org/mailman/listinfo/python-list
Re: Convert points to polygon shapefile
On Jul 24, 11:21 am, Luis Pedro Almeida wrote: > Dear all, > > I would like to know how to convert a list of points into a polygon > shapefile (esri). > > Thanks! > > Best regards, > > Luis Pedro Almeida I think you'd be better served by asking this question in a newsgroup dedicated to GIS software (I'm guessing that's the ESRI you meant). Still, what you want is to find the file format specification, and implement it. -- http://mail.python.org/mailman/listinfo/python-list
Re: Convert points to polygon shapefile
> Luis Pedro Almeida (LPA) wrote: >LPA> Dear all, >LPA> I would like to know how to convert a list of points into a >LPA> polygon shapefile (esri). http://lmgtfy.com/?q=esri+shapefile+Python -- Piet van Oostrum URL: http://pietvanoostrum.com [PGP 8DAE142BE17999C4] Private email: [email protected] -- http://mail.python.org/mailman/listinfo/python-list
Re: mmap 2GB allocation limit on Win XP, 32-bits, Python 2.5.4
Slaunger wrote: OS: Win XP SP3, 32 bit Python 2.5.4 Hi I have run into some problems with allocating numpy.memmaps exceeding and accumulated size of about 2 GB. I have found out that the real problem relates to numpy.memmap using mmap.mmap I've written a small test program to illustrate it: import itertools import mmap import os files = [] mmaps = [] file_names= [] mmap_cap=0 bytes_per_mmap = 100 * 1024 ** 2 try: for i in itertools.count(1): file_name = "d:/%d.tst" % i file_names.append(file_name) f = open(file_name, "w+b") files.append(f) mm = mmap.mmap(f.fileno(), bytes_per_mmap) mmaps.append(mm) mmap_cap += bytes_per_mmap print "Created %d writeable mmaps containing %d MB" % (i, mmap_cap/(1024**2)) #Clean up finally: print "Removing mmaps..." for mm, f, file_name in zip(mmaps, files, file_names): mm.close() f.close() os.remove(file_name) print "Done..." which creates this output Created 1 writeable mmaps containing 100 MB Created 2 writeable mmaps containing 200 MB Created 17 writeable mmaps containing 1700 MB Created 18 writeable mmaps containing 1800 MB Removing mmaps... Done... Traceback (most recent call last): File "C:\svn-sandbox\research\scipy\scipy\src\com\terma\kha \mmaptest.py", line 16, in mm = mmap.mmap(f.fileno(), bytes_per_mmap) WindowsError: [Error 8] Not enough storage is available to process this command There is more than 25 GB of free space on drive d: at this stage. Is it a bug or a "feature" of the 32 bit OS? I am surprised about it as I have not found any notes about these kinds of limitations in the documentation. I am in dire need of these large memmaps for my task, and it is not an option to change OS due to other constraints in the system. Is there anything I can do about it? Best wishes, Kim It's not a question of how much disk space there is, but how much virtual space 32 bits can address. 2**32 is about 4 gig, and Windows XP reserves about half of that for system use. Presumably a 64 bit OS would have a much larger limit. Years ago I worked on Sun Sparc system which had much more limited shared memory access, due to hardware limitations. So 2gig seems pretty good to me. There is supposed to be a way to tell the Windows OS to only use 1 gb of virtual space, leaving 3gb for application use. But there are some limitations, and I don't recall what they are. I believe it has to be done globally (probably in Boot.ini), rather than per process. And some things didn't work in that configuration. DaveA -- http://mail.python.org/mailman/listinfo/python-list
Re: doted filenames in import statements
Terry Reedy wrote:
Jean-Michel Pichavant wrote:
Piet van Oostrum wrote:
[snip]
JP> file = "/home/dsp/4.6.0.0/test.py"
JP> test = __import__(file)
JP> => no module name blalalal found.
JP> Any suggestion ? I tried multiple escape technics without any
success.
Rightly so.
I think the best would be to add the directory to sys.path
sys.path.add('/home/dsp/4.6.0.0')
and then
__import__('test', ... )
I see. My problem is that a have to import 2 different files having
de same name. In the same name space I have 2 objects from 2
different software branches, let's say 4.6.0 and 4.6.1.
The first object shall import 4.6.0/orb.py and the second one
4.6.1/orb.py.
If I add 4.6.1 to sys.path, the import statement will look like:
self._orb = __import__('orb')
The problem is, python wil assume orb is already imported and will
assign the module from the 4.6.0 branch to my 4.6.1 object.
Do I have to mess up with sys.modules keys to make python import the
correct file ? Is there a standard/proper way to do that ?
If you make the directory names into proper identifiers like v460 and
v461 and add __init__.py to each to make them packages and have both
on search path, then
import v460.orb #or import v460.orb as orb460
import v461.orb #or import v460.orb as orb461
will get you both. One way or another, they have to get different
names within Python.
Terry Jan Reedy
I finally had to write my own import statement as I prefered to
manipulate the python objects instead of manipulating third party files.
Basically when importing 'file.py' it records it in sys.modules as
sys.modules['__magic_word_file''] and then I remove the standard
reference. This allows me to import file.py again, but with a totally
different path. (path is temporarily added to sys.path)
JM
--
http://mail.python.org/mailman/listinfo/python-list
non-owning references?
Hi, I'm not sure the subject describes what I'm looking for, but the question is the following: Is there a way I can tell a variable that the object it is pointing too is not owned by it, in the sense that if it is the only reference to the object it can be garbage collected? I want this for what is essentially a singleton class, so that on first instantiation the object is created and a reference is kept in the class, that is used to return the same object in subsequent instantiations. When all instances go out of scope, the reference in the class is still there, preventing it from being garbage collected, but since the instance can be huge, I would like it to be. Thanks, Utpal -- http://mail.python.org/mailman/listinfo/python-list
trouble with wrapping a c program
Hi Mailing, I am using a c program, which first initializes for some seconds and then waits for user input (keyboard) to type something. When enter is pressed the c program continues. I have wrapped this program in a python script, which starts the c program. To start the c program, there are many options in python e.g. Os.system os.popen or subprocess.popen To me there does not seem to be a difference for starting the program. They all work well. However the problem occurs when I want to input data during this c program. Using the keyboard and then enter in the c program prompt works, but I wish to do this from the python script by sending the string from the python script. I am able to print the string from python with a print command or with a stdout.write command. But this simply prints it to the prompt, the c program does nothing with this printed string. Is there a way to pipe, stream, or send this string to the running c program? All tutorials about piping I have read, seem to have to wait for the process to finish. Or need the stdin string before starting the program. Note that the c program is not finished when the input is needed, so I cannot use subprocess.call or wait. It is possible to give the user input in advance, but I do not want this, because this makes everything much slower! I really would like to do this during/while the c program is running. I have created a thread and a small xmlrpc server for this and this works fine. However I cannot seem to pass the string. Here is a small fragment of my code: #initialization cmd = [a list of my program and arguments] subprocess.Popen(cmd) #starts the c program #somewhere along the way Send_string(str): #Sys.stdin = str #subprocess.stdin = str I have spent the entire night trying to get this to work, but I can't seem to get it right. Any help is much appreciated. Also, if anybody could explain me where the fuck-up in my brain is, I would be very happy.. Sanne -- http://mail.python.org/mailman/listinfo/python-list
Re: non-owning references?
Utpal Sarkar wrote: > Is there a way I can tell a variable that the object it is pointing > too is not owned by it, in the sense that if it is the only reference > to the object it can be garbage collected? http://docs.python.org/library/weakref.html -- http://mail.python.org/mailman/listinfo/python-list
Re: non-owning references?
Utpal Sarkar wrote: > Hi, > [...] You're looking for the weakref module. What you're describing there sounds like a nice exercise, but I cannot imagine why you'd really need to clean it up, if it really is a singleton. -- Gerhard -- http://mail.python.org/mailman/listinfo/python-list
Re: non-owning references?
Utpal Sarkar writes: > Is there a way I can tell a variable that the object it is pointing > too is not owned by it, in the sense that if it is the only reference > to the object it can be garbage collected? Python doesn't have “pointers”, and doesn't really have “variables” either, at least not how many other languages use that term. What it does have is references to objects http://effbot.org/zone/python-objects.htm>. > on first instantiation the object is created and a reference is kept > in the class, that is used to return the same object in subsequent > instantiations. When all instances go out of scope, the reference in > the class is still there, preventing it from being garbage collected, > but since the instance can be huge, I would like it to be. What you are asking for is called a “weak reference” and is provided by the ‘weakref’ module http://docs.python.org/library/weakref>. -- \ “Patience, n. A minor form of despair, disguised as a virtue.” | `\ —Ambrose Bierce, _The Devil's Dictionary_, 1906 | _o__) | Ben Finney -- http://mail.python.org/mailman/listinfo/python-list
Re: non-owning references?
On Jul 24, 3:06 pm, Ben Finney wrote: > Utpal Sarkar writes: > > Is there a way I can tell a variable that the object it is pointing > > too is not owned by it, in the sense that if it is the only reference > > to the object it can be garbage collected? > > Python doesn't have “pointers”, and doesn't really have “variables” > either, at least not how many other languages use that term. > > What it does have is references to objects > http://effbot.org/zone/python-objects.htm>. > > > on first instantiation the object is created and a reference is kept > > in the class, that is used to return the same object in subsequent > > instantiations. When all instances go out of scope, the reference in > > the class is still there, preventing it from being garbage collected, > > but since the instance can be huge, I would like it to be. > > What you are asking for is called a “weak reference” and is provided > by the ‘weakref’ module http://docs.python.org/library/weakref>. > > -- > \ “Patience, n. A minor form of despair, disguised as a virtue.” | > `\ —Ambrose Bierce, _The Devil's Dictionary_, 1906 | > _o__) | > Ben Finney Thanks to the three of you. This is precisely what I needed! Gerhard, the reason I need to clean it up is that it is a lazy data structure that can grow to arbitrary size. When it is not needed anymore it would still remain in memory. Utpal -- http://mail.python.org/mailman/listinfo/python-list
Re: how to get no value
[email protected] wrote: Hi, I have a file having lines:- 48 ALA H = 8.33 N = 120.77 CA = 55.18 HA = 4.12 C = 181.50 104 ALA H = 7.70 N = 121.21 CA = 54.32 HA = 4.21 C = 85 ALA H = 8.60 N = CA = HA = 4.65 C = Now i want to make two another file in which i want to put those lines for which C is missing and another one for which N,CA and C all are missing, I tried in this way: import re f = open('chem.txt') for line in f: if re.search('C = ',''): print line but i am not getting the desired output. Amrita Kumari Research Fellow IISER Mohali Chandigarh INDIA Try writing correct rather than incorrect code. Or as has been repeatedly stated get somone from your CS department to help. -- Kindest regards. Mark Lawrence. -- http://mail.python.org/mailman/listinfo/python-list
Re: len() should always return something
On 2009-07-24, Dr. Phillip M. Feldman wrote: > > Some aspects of the Python design are remarkably clever, while > others leave me perplexed. Here's an example of the latter: > Why does len() give an error when applied to an int or float? > len() should always return something; in particular, when > applied to a scalar, it should return a value of 1. If len(7) returned a value of 1, then wouldn't one expect 7[0] to be valid? It isn't, so you'd then have to redefine all types so that they are sequences that can be indexed. Sounds like a big mess to me... [Are there types for which len() returns a value that can't be indexed?] -- Grant Edwards grante Yow! It's the RINSE CYCLE!! at They've ALL IGNORED the visi.comRINSE CYCLE!! -- http://mail.python.org/mailman/listinfo/python-list
Popen
Hi, I wonder if I use Popen, the parent process will wait for the child process to finish or continue without waiting? Thanks and regards! -- http://mail.python.org/mailman/listinfo/python-list
Re: non-owning references?
Ben Finney writes: > Utpal Sarkar writes: > >> Is there a way I can tell a variable that the object it is pointing >> too is not owned by it, in the sense that if it is the only reference >> to the object it can be garbage collected? > > Python doesn't have “pointers”, and doesn't really have “variables” > either, at least not how many other languages use that term. The OP didn't use the term "pointer", but the word "pointing", which makes sense in the context. The term "variable" is used in the Python language reference and elsewhere, and is quite compatible with how other popular languages (Java, PHP, Lisp, ...) use it. Please stop complaining about valid terminology; it is not helpful. -- http://mail.python.org/mailman/listinfo/python-list
Re: len() should always return something
On Fri, 24 Jul 2009 14:57:02 +0100, Grant Edwards wrote: On 2009-07-24, Dr. Phillip M. Feldman wrote: Some aspects of the Python design are remarkably clever, while others leave me perplexed. Here's an example of the latter: Why does len() give an error when applied to an int or float? len() should always return something; in particular, when applied to a scalar, it should return a value of 1. If len(7) returned a value of 1, then wouldn't one expect 7[0] to be valid? It isn't, so you'd then have to redefine all types so that they are sequences that can be indexed. Sounds like a big mess to me... [Are there types for which len() returns a value that can't be indexed?] Dictionaries. Which doesn't make your point less valid. In fact I'd go so far as to argue that what len() gives you is the number of items in a container, so len(7) should return 0. -- Rhodri James *-* Wildebeest Herder to the Masses -- http://mail.python.org/mailman/listinfo/python-list
Re: how to get no value
On 2009-07-24, [email protected] wrote: > > Hi, > > I have a file having lines:- > > 48 ALA H = 8.33 N = 120.77 CA = 55.18 HA = 4.12 C = 181.50 > 104 ALA H = 7.70 N = 121.21 CA = 54.32 HA = 4.21 C = > 85 ALA H = 8.60 N = CA = HA = 4.65 C = > > Now i want to make two another file in which i want to put > those lines for which C is missing and another one for which > N,CA and C all are missing, > > I tried in this way: > import re > f = open('chem.txt') > for line in f: > if re.search('C = ',''): > print line > > but i am not getting the desired output. I've told you before: don't use regular expressions (e.g. the "re" module). Stop using regular expressions now. Regular expressions are way beyond your capabilities. Use simple operations like split() and "in": f = open('chem.txt') for line in f: if "C = " in line: print line You really need to work through a Python tutorial or two: http://docs.python.org/tutorial/ http://www.greenteapress.com/thinkpython/thinkpython.html Better yet, take an couple introductory programming courses. I'm a bit surprised that one could become a "Research Fellow" in a scientific field without taking any programming courses. -- Grant Edwards grante Yow! I'm also against at BODY-SURFING!! visi.com -- http://mail.python.org/mailman/listinfo/python-list
Re: non-owning references?
On Fri, 24 Jul 2009 14:55:45 +0100, Hrvoje Niksic wrote: Ben Finney writes: Utpal Sarkar writes: Is there a way I can tell a variable that the object it is pointing too is not owned by it, in the sense that if it is the only reference to the object it can be garbage collected? Python doesn't have “pointers”, and doesn't really have “variables” either, at least not how many other languages use that term. The OP didn't use the term "pointer", but the word "pointing", which makes sense in the context. The term "variable" is used in the Python language reference and elsewhere, and is quite compatible with how other popular languages (Java, PHP, Lisp, ...) use it. Only superficially. Treating Python variables the same as C variables (say) is one of the classic ways that newbies come unstuck when mutable objects appear on the scene. While the OP appears to have the right idea, your "correction" here could be quite misleading. -- Rhodri James *-* Wildebeest Herder to the Masses -- http://mail.python.org/mailman/listinfo/python-list
Re: strange python scripting error
On Jul 23, 7:03 pm, Dave Angel wrote: > Mark Tarver wrote: > > I have a very strange error. I have two test python files test.py and > > python.py which contain the following code > > > #!/usr/bin/python > > print "Content-type: text/html" > > print > > print "" > > print "Hello, Linux.com!" > > print "" > > > One file (test.py) works; you call it up and it shows a web page with > > > Hello, Linux.com > > > The other fails with a server configuration error. Both are running > > under Linux, same server, same permissions. Running a character scan > > shows that both files contain the same printable characters and are > > therefore typographically identical. They are absolutely the same. > > > The only hint at a difference I can see is that my ftp program says > > the files are of unequal lengths. test.py is 129 bytes long. > > python.py 134 bytes long. > > > A zipped folder containing both files is at > > >www.lambdassociates.org/weird.zip > > > Any ideas welcome. > > > Mark > > Easiest explanation is that python.py has Windows-style newlines. In > other words, each line ends with 0d0a, rather than the Unix convention > of 0a. > > If your server is Unix-based, it can't handle that first line, since it > has an illegal character (0d) following the > > #!/usr/bin/python > > line. Convert it to Unix line-endings. > > DaveA Use dos2unix for conversion of the longer file and try again: http://linux.about.com/od/commands/l/blcmdl1_dos2uni.htm -- http://mail.python.org/mailman/listinfo/python-list
Re: len() should always return something
On Jul 24, 3:11 pm, "Rhodri James" wrote: > Which doesn't make your point less valid. In fact I'd go so > far as to argue that what len() gives you is the number of > items in a container, so len(7) should return 0. Nah. 7 contains three bits, so len(7) should *clearly* return 3. Mark -- http://mail.python.org/mailman/listinfo/python-list
Re: len() should always return something
Mark Dickinson wrote:
On Jul 24, 3:11 pm, "Rhodri James"
wrote:
Which doesn't make your point less valid. In fact I'd go so
far as to argue that what len() gives you is the number of
items in a container, so len(7) should return 0.
Nah. 7 contains three bits, so len(7) should *clearly* return 3.
and len("7") must return 8, by the same token... but wait!
>>> len("7")
1
>>>
my python installation must me outdated ;-)
bye
--
http://mail.python.org/mailman/listinfo/python-list
Re: len() should always return something
Mark Dickinson schrieb: On Jul 24, 3:11 pm, "Rhodri James" wrote: Which doesn't make your point less valid. In fact I'd go so far as to argue that what len() gives you is the number of items in a container, so len(7) should return 0. Nah. 7 contains three bits, so len(7) should *clearly* return 3. But it contains a minimum of 32 bits! And why are you treating ones as special over zeros? I thought the times of BitRacism were finally over... Diez -- http://mail.python.org/mailman/listinfo/python-list
Re: Popen
On Fri, Jul 24, 2009 at 7:33 PM, Tim wrote: > > Hi, > I wonder if I use Popen, the parent process will wait for the child process > to finish or continue without waiting? > Thanks and regards! > Assuming you mean subprocess.Popen, the child is executed asynchronously. You can use the wait() method on the Popen object if you want the parent to wait for the child to finish. -- kushal -- http://mail.python.org/mailman/listinfo/python-list
Re: non-owning references?
> "Rhodri James" (RJ) wrote: >RJ> On Fri, 24 Jul 2009 14:55:45 +0100, Hrvoje Niksic >wrote: >>> Ben Finney writes: >>> Utpal Sarkar writes: > Is there a way I can tell a variable that the object it is pointing > too is not owned by it, in the sense that if it is the only reference > to the object it can be garbage collected? Python doesn't have “pointers”, and doesn't really have “variables” either, at least not how many other languages use that term. >>> >>> The OP didn't use the term "pointer", but the word "pointing", which >>> makes sense in the context. The term "variable" is used in the Python >>> language reference and elsewhere, and is quite compatible with how other >>> popular languages (Java, PHP, Lisp, ...) use it. >RJ> Only superficially. Treating Python variables the same as C variables >RJ> (say) is one of the classic ways that newbies come unstuck when mutable >RJ> objects appear on the scene. While the OP appears to have the right idea, >RJ> your "correction" here could be quite misleading. If you read the OP, it is clear that he talked about a class variable, which is a perfectly legal notion in Python, and is mentioned as such in the language reference manual: `Variables defined in the class definition are class variables' And who was talking about C variables? -- Piet van Oostrum URL: http://pietvanoostrum.com [PGP 8DAE142BE17999C4] Private email: [email protected] -- http://mail.python.org/mailman/listinfo/python-list
Re: Help understanding the decisions *behind* python?
On Jul 23, 3:42 am, Hendrik van Rooyen wrote: > On Wednesday 22 July 2009 16:36:51 Inky 788 wrote: > > > On Jul 22, 2:36 am, Hendrik van Rooyen > > > wrote: > > > The good reason is the immutability, which lets you use > > > a tuple as a dict key. > > > Thanks for the reply Hendrik (and Steven (other reply)). Perhaps I'm > > just not sophisticated enough, but I've never wanted to use a list/ > > tuple as a dict key. This sounds like obscure usage, and a bit > > contrived as a reason for having *both* lists and tuples. > > Steven showed why you cannot have a mutable thing > as a key in a dict. > > if you think it is contrived, then please consider how you would > keep track of say the colour of a pixel on a screen at position > (x,y) - this is about the simplest "natural" tuple format and > example. My guess is that this is probably the way most people do it: #!/usr/bin/env python import sys import random if len( sys.argv ) != 3: print "Please pass exactly 2 ints. Exiting." sys.exit(1) NUM_COLUMNS = int( sys.argv[1] ) NUM_ROWS= int( sys.argv[2] ) print "Making array of %s columns by %s rows." % (NUM_COLUMNS, NUM_ROWS) def rand(): return int( 255 * random.random()) def make_a_pixel(): # red green blue return [rand(), rand(), rand()] def make_a_row(num_columns): temp_row = [] for i in range(num_columns): temp_row.append( make_a_pixel() ) return temp_row def make_array_of_pixels(num_columns, num_rows): rows = [] for i in range(num_rows): rows.append( make_a_row(num_columns) ) return rows def show_pixels(pixel_array): for row in pixel_array: for pixel in row: print pixel, ' ', print rows_of_pixels = make_array_of_pixels(NUM_COLUMNS, NUM_ROWS) show_pixels(rows_of_pixels) -- http://mail.python.org/mailman/listinfo/python-list
Re: len() should always return something
> "Rhodri James" (RJ) wrote: >RJ> On Fri, 24 Jul 2009 14:57:02 +0100, Grant Edwards wrote: >>> On 2009-07-24, Dr. Phillip M. Feldman wrote: Some aspects of the Python design are remarkably clever, while others leave me perplexed. Here's an example of the latter: Why does len() give an error when applied to an int or float? len() should always return something; in particular, when applied to a scalar, it should return a value of 1. >>> >>> If len(7) returned a value of 1, then wouldn't one expect 7[0] >>> to be valid? It isn't, so you'd then have to redefine all >>> types so that they are sequences that can be indexed. Sounds >>> like a big mess to me... >>> >>> [Are there types for which len() returns a value that can't be >>> indexed?] >>> >RJ> Dictionaries. >RJ> Which doesn't make your point less valid. In fact I'd go so >RJ> far as to argue that what len() gives you is the number of >RJ> items in a container, so len(7) should return 0. But len(7) could as well be defined as 3, 1, 32, or 64 (depending on the implementation). Therefore it doesn't make much sense. -- Piet van Oostrum URL: http://pietvanoostrum.com [PGP 8DAE142BE17999C4] Private email: [email protected] -- http://mail.python.org/mailman/listinfo/python-list
Re: non-owning references?
Hrvoje Niksic writes: > The term "variable" is used in the Python language reference and > elsewhere Yes. It should also be abundantly clear from the constant stream of confused newbies on this point that its usage of that term is different to what many expect from usage elsewhere. > and is quite compatible with how other popular languages (Java, PHP, > Lisp, ...) use it. Please stop complaining about valid terminology; it > is not helpful. I disagree with your assertions. Rather than yet another round of this tedious debate, I merely point interested readers to http://effbot.org/zone/python-objects.htm> and ask them to draw their own conclusion on how compatible their pre-existing “variable” concept is with Python's object reference model. -- \ “Too many pieces of music finish too long after the end.” —Igor | `\ Stravinskey | _o__) | Ben Finney -- http://mail.python.org/mailman/listinfo/python-list
Re: Popen
> Tim (T) wrote: >T> Hi, >T> I wonder if I use Popen, the parent process will wait for the child process >to finish or continue without waiting? >T> Thanks and regards! Only if you use Popen.wait(), Popen.communicate() or something similar like os.waitpid(), subprocess.call() -- Piet van Oostrum URL: http://pietvanoostrum.com [PGP 8DAE142BE17999C4] Private email: [email protected] -- http://mail.python.org/mailman/listinfo/python-list
Re: import vs imp and friends.
Christian, Robert, thank you both for the replies, much appreciated. Manu -- http://mail.python.org/mailman/listinfo/python-list
Re: how to get no value
Well actually your subject is `how to get no value'. Your code does that perfectly. :=) -- Piet van Oostrum URL: http://pietvanoostrum.com [PGP 8DAE142BE17999C4] Private email: [email protected] -- http://mail.python.org/mailman/listinfo/python-list
Re: difference in printing to screen Mac / Windows
On Jul 18, 7:03 am, Tim Chase wrote: > Lastly, you can force all standard-output in your program to be > unbuffered without the "-u" parameter: And if you're using -u a lot, the PYTHONUNBUFFERED environment variable can also be set (but not empty), so that python adds the option automatically. -- http://mail.python.org/mailman/listinfo/python-list
Re: regex: multiple matching for one string
[email protected] wrote: Nick Dumas wrote: On 7/23/2009 9:23 AM, Mark Lawrence wrote: [email protected] wrote: For example, I have a string "#a=valuea;b=valueb;c=valuec;", and I will like to take out the values (valuea, valueb, and valuec). How do I do that in Python? The group method will only return the matched part. Thanks. p = re.compile('#a=*;b=*;c=*;') m = p.match(line) if m: print m.group(), IMHO a regex for this is overkill, a combination of string methods such as split and find should suffice. You're saying that something like the following is better than the simple regex used by the OP? [untested] values = [] parts = line.split(';') if len(parts) != 4: raise SomeError() for p, expected in zip (parts[-1], ('#a','b','c')): name, x, value = p.partition ('=') if name != expected or x != '=': raise SomeError() values.append (value) print values[0], values[1], values[2] I call straw man: [tested] line = "#a=valuea;b=valueb;c=valuec;" d = dict(single.split('=', 1) for single in line.split(';') if single) d['#a'], d['b'], d['c'] If you want checking code, add: if len(d) != 3: raise ValueError('Too many keys: %s in %r)' % ( sorted(d), line)) Blech, not in my book. The regex checks the format of the string, extracts the values, and does so very clearly. Further, it is easily adapted to other similar formats, or evolutionary changes in format. It is also (once one is familiar with regexes -- a useful skill outside of Python too) easier to get right (at least in a simple case like this.) The posted regex doesn't work; this might be homework, so I'll not fix the two problems. The fact that you did not see the failure weakens your claim of "does so very clearly." --Scott David Daniels [email protected] -- http://mail.python.org/mailman/listinfo/python-list
Re: Predefined Variables
Stephen Cuppett (should have written in this order):
"Fred Atkinson" wrote ...
Is there a pre-defined variable that returns the GET line
(http://www.php.net/index.php?everythingafterthequestionmark) as a
single variable (rather than individual variables)?
> os.environment('QUERY_STRING')
Maybe you mean:
os.environ['USER']
--Scott David Daniels
[email protected]
--
http://mail.python.org/mailman/listinfo/python-list
Re: Popen
Thanks! Yes I mean subprocess.Popen. I was wondering the meaning of "asynchronously" Here is some code I am reading recently: " result = Popen(cmdline,shell=True,stdout=PIPE).stdout for line in result.readlines(): if find(line,"Cross") != -1: return float(split(line)[-1][0:-1]) " The computation in the program "cmdline" takes a long time, at the end of which the results will be output to stdout. "asynchronous" seems to mean Popen returns to the parent process immediately and the parent and child processes continue to be executed. However, if Popen returns immediately to the parent process, then there will be nothing in "result", not to mention extracting information from the output. Thus it seems to me the parent process has to wait till the child process finish. So how to understand the meaning of "asynchronous"? Thanks and regards! --- On Fri, 7/24/09, Kushal Kumaran wrote: > From: Kushal Kumaran > Subject: Re: Popen > To: "Tim" > Cc: [email protected] > Date: Friday, July 24, 2009, 10:58 AM > On Fri, Jul 24, 2009 at 7:33 PM, > Tim > wrote: > > > > Hi, > > I wonder if I use Popen, the parent process will wait > for the child process to finish or continue without > waiting? > > Thanks and regards! > > > > Assuming you mean subprocess.Popen, the child is executed > asynchronously. You can use the wait() method on the > Popen object if > you want the parent to wait for the child to finish. > > -- > kushal > -- http://mail.python.org/mailman/listinfo/python-list
Re: Popen
Tim schrieb: Thanks! Yes I mean subprocess.Popen. I was wondering the meaning of "asynchronously" Here is some code I am reading recently: " result = Popen(cmdline,shell=True,stdout=PIPE).stdout for line in result.readlines(): if find(line,"Cross") != -1: return float(split(line)[-1][0:-1]) " The computation in the program "cmdline" takes a long time, at the end of which the results will be output to stdout. "asynchronous" seems to mean Popen returns to the parent process immediately and the parent and child processes continue to be executed. However, if Popen returns immediately to the parent process, then there will be nothing in "result", not to mention extracting information from the output. Thus it seems to me the parent process has to wait till the child process finish. So how to understand the meaning of "asynchronous"? "Asynchronous" means asynchronous - the parent is *not* waiting. Which is the reason that there is (amongst other things) the "wait"-method you can call to wait for the child to be terminated. Diez -- http://mail.python.org/mailman/listinfo/python-list
Re: Popen
On Jul 24, 6:24 pm, Tim wrote: > Thanks! > Yes I mean subprocess.Popen. > > I was wondering the meaning of "asynchronously" > Here is some code I am reading recently: > " > result = Popen(cmdline,shell=True,stdout=PIPE).stdout > for line in result.readlines(): > if find(line,"Cross") != -1: > return float(split(line)[-1][0:-1]) > " > The computation in the program "cmdline" takes a long time, at the end of > which the results will be output to stdout. > > "asynchronous" seems to mean Popen returns to the parent process immediately > and the parent and child processes continue to be executed. This is correct > However, if Popen returns immediately to the parent process, then there will > be nothing in "result", not to mention extracting information from the > output. Thus it seems to me the parent process has to wait till the child > process finish. > Calling result.readlines() the parent process attempts to read from stdout until end of file. For a pipe, end of file means that the other end is closing its connection, which - unless the child process closes stdout explicitely - means that the child process is terminated. So the end effect is the almost like using 'communicate' on the result of Popen, except that communicates returns both standard output and standard error contents at the same time. > So how to understand the meaning of "asynchronous"? > > Thanks and regards! HTH Ciao - FB -- http://mail.python.org/mailman/listinfo/python-list
SONAME for python modules is bad? (aka multiple module version)
As far as I know there has not been any consensus on how to install multiple version of a same module in python ? What are the recommended mechanism ? I could not find any documentation on the subject. Does anyone sees any issue with using standard SONAME mechanism when installing a python module ? Thanks, ref: http://mail.python.org/pipermail/pythonmac-sig/2009-January/020936.html -- http://mail.python.org/mailman/listinfo/python-list
Re: Popen
Thanks! If that is the case, i.e. the parent doesn't wait, is the code in my last post wrong? "result" could be nothing. --- On Fri, 7/24/09, Diez B. Roggisch wrote: > From: Diez B. Roggisch > Subject: Re: Popen > To: [email protected] > Date: Friday, July 24, 2009, 12:35 PM > Tim schrieb: > > Thanks! Yes I mean subprocess.Popen. > > > > I was wondering the meaning of "asynchronously" > > Here is some code I am reading recently: > > " > > result = Popen(cmdline,shell=True,stdout=PIPE).stdout > for line in result.readlines(): > > if find(line,"Cross") != -1: > > return > float(split(line)[-1][0:-1]) " > > The computation in the program "cmdline" takes a long > time, at the end of which the results will be output to > stdout. > > > > "asynchronous" seems to mean Popen returns to the > parent process immediately and the parent and child > processes continue to be executed. > > However, if Popen returns immediately to the parent > process, then there will be nothing in "result", not to > mention extracting information from the output. Thus it > seems to me the parent process has to wait till the child > process finish. > > > > So how to understand the meaning of "asynchronous"? > > "Asynchronous" means asynchronous - the parent is *not* > waiting. > > Which is the reason that there is (amongst other things) > the "wait"-method you can call to wait for the child to be > terminated. > > Diez > -- http://mail.python.org/mailman/listinfo/python-list > -- http://mail.python.org/mailman/listinfo/python-list
Re: Re: len() should always return something
I've read the "Zen of Python", but most of these aphorisms are vague and could be understood differently by different readers. In particular, I don't understand the statement that "explicit is better than implicit". Some examples of this would be helpful.I've been converting Matlab codes to Python. In Matlab, a scalar is just a one-by-one matrix and has a length of 1. This convention seems no less arbitrary to me than Python's convention that the concept of length is not applicable to ints and floats. My workaround was to write the following function:def is_scalar(x): """Return True if x is an instance of int, float, or complex. Otherwise, return False. Note: If x is a length-1 list or array containing an int, float, or complex value, False is returned.""" if isinstance(x,int) or isinstance(x,float) or isinstance(x,complex): return True return FalseThe application is the following: In various types of scientific applications, one operates on a list of measurements. If there is only a single measurement, it is reasonable to allow the calling program to pass a scalar without wrapping it up into a list or array.PhillipJul 24, 2009 07:02:29 AM, [email protected] wrote:On Thu, Jul 23, 2009 at 11:35 PM, Dr. Phillip M.Feldmanwrote:>> Some aspects of the Python design are remarkably clever, while others leave> me perplexed. Here's an example of the latter: Why does len() give an error> when applied to an int or float? len() should always return something; in> particular, when applied to a scalar, it should return a value of 1. Of> course, I can define my own function like this:>> def mylen(x):> if isinstance(x,int) or isinstance(x,float): return 1> return len(x)>> But, this shouldn't be necessary.The problem is that redefining len()/length/size that way wouldviolate several principles of Python's design (The "Zen" of Python -http://www.python.org/dev/peps/pep-0020/).Specifically:- Explicit is better than implicit.- Special cases aren't special enough to break the rules.- Errors should never pass silently.- In the face of ambiguity, refuse the temptation to guess.If you'd explain the situation that prompts you to find thisredefinition necessary, I'm sure someone can suggest a betterapproach.Cheers,Chris-- http://blog.rebertia.com -- http://mail.python.org/mailman/listinfo/python-list
Re: any suggestions to synchronize typed text and speech ?
On 7/21/2009 12:13 PM, Stef Mientki wrote: hi Marcus, That sounds like a very specialized type of thing, Well from an application point of view, with the current netbooks, this looks like a perfect tool for any conversation or meeting. which only the few people with experience with wxPython, PyAudio, and Scintilla could help you with... I was afraid of that too, so I dropped the question in several places, and the writer of Scintilla himself came with the perfect answer. cheers,Stef But you might try having a dictionary with notes and associated times, or just give each note a four-digit ID number at the beginning of it when it's entered and use that in the dictionary (to keep keys shorter). Or you could just do a little hack and increase the number of bookmarks allowed (seeing as source is available) :p Marcus Glad you got a good answer from somebody. Sounds like an interesting project. About when would this be headed for a release? Could you post a link to a googlecode or sourceforge project or something so I can follow and/or help with development? For the moment it's just an idea, so no line of code yet. I first like to tackle all the problems, at least to the level I think I can handle them. So first solve the next problem, before I start coding: automatic synchronization (file uploading and deleting) between EEE-pc and desktop PC over bluetooth. And another problem, as my customers are physicians, both the text and audio need to be stored encrypted. cheers, Stef Marcus I would recommend pybluez and http://www.google.com/search?q=python+aes+encryption Marcus -- http://mail.python.org/mailman/listinfo/python-list
Re: effbot.org broken (WAS: Problems in commands.getoutput(cmd) with sox)
On Fri, Jul 24, 2009 at 2:38 AM, Piet van Oostrum wrote: >> Chris Rebert (CR) wrote: > >>CR> On Thu, Jul 23, 2009 at 12:42 PM, Chris Rebert wrote: You can use tabnanny to help diagnose the problem: http://74.125.155.132/search?q=cache:QtxvZm3QDLsJ:effbot.org/librarybook/tabnanny.htm+tabnanny&cd=3&hl=en&ct=clnk&gl=us&client=firefox-a > >>CR> Anyone know what's the deal with effbot.org? It seems to be broken at >>CR> present, forcing me to use Google's cached version. >>CR> It's a real shame since it has lots of handy Python info. > > Just try again. Yup, whatever the problem was, seems to be fixed now. Interesting. - Chris -- http://mail.python.org/mailman/listinfo/python-list
Re: Help understanding the decisions *behind* python?
On Jul 20, 12:27 pm, Phillip B Oldham wrote: > My colleagues and I have been working with python for around 6 months > now, and while we love a lot of what python has done for us and what > it enables us to do some of the decisions behind such certain > data-types and their related methods baffle us slightly (when compared > to the decisions made in other, similarly powerful languages). > > Specifically the "differences" between lists and tuples have us > confused and have caused many "discussions" in the office. We > understand that lists are mutable and tuples are not, but we're a > little lost as to why the two were kept separate from the start. They > both perform a very similar job as far as we can tell. > > Consider the following: > > >>> x = [2,1,3] > >>> x.sort() > >>> print x > > [1, 2, 3] > > Now, if the sort operations were unable to affect the original > structure of the list (as in JavaScript) you'd effectively have a > tuple which you could add/remove from, and the example above would > look more like: > > >>> x = [2,1,3] > >>> print x.sort() > [1, 2, 3] > >>> print x > > [2,1,3] > > This make a lot more sense to us, and follows the convention from > other languages. It would also mean chaining methods to manipulate > lists would be easier: > > >>> x = [2,1,3] > >>> print x.sort()[0] > 3 > >>> print x > > [2,1,3] > > We often find we need to do manipulations like the above without > changing the order of the original list, and languages like JS allow > this. We can't work out how to do this in python though, other than > duplicating the list, sorting, reversing, then discarding. > > We're not looking to start any arguments or religious wars and we're > not asking that python be changed into something its not. We'd simply > like to understand the decision behind the lists and tuple structures. > We feel that in not "getting" the difference between the two types we > may be missing out on using these data structures to their full > potential. A tuple can be used like a struct in C - the number of fields is meant to be fixed and should not be dynamically changed. -- http://mail.python.org/mailman/listinfo/python-list
cgi.fieldstorage()
this is a non standard way to store multi part post data on disk
def application(environ, response):
with open('/usr/httpd/var/wsgiTemp','w') as f:
while True:
chunk = environ['wsgi.input'].read(8192).decode('latin1')
if not chunk: break
f.write(chunk)
response('200 OK',[])
return ['complete']
my question is how do i handle the file, so i can shuffle it into a db
using small chunks of memorie ?
--
http://mail.python.org/mailman/listinfo/python-list
Re: Popen
> --- On Fri, 7/24/09, Diez B. Roggisch wrote: > >> From: Diez B. Roggisch >> Subject: Re: Popen >> To: [email protected] >> Date: Friday, July 24, 2009, 12:35 PM >> Tim schrieb: >> > Thanks! Yes I mean subprocess.Popen. >> > >> > I was wondering the meaning of "asynchronously" >> > Here is some code I am reading recently: >> > " >> > result = Popen(cmdline,shell=True,stdout=PIPE).stdout >> for line in result.readlines(): >> > if find(line,"Cross") != -1: >> > return >> float(split(line)[-1][0:-1]) " >> > The computation in the program "cmdline" takes a long >> time, at the end of which the results will be output to >> stdout. >> > >> > "asynchronous" seems to mean Popen returns to the >> parent process immediately and the parent and child >> processes continue to be executed. >> > However, if Popen returns immediately to the parent >> process, then there will be nothing in "result", not to >> mention extracting information from the output. Thus it >> seems to me the parent process has to wait till the child >> process finish. >> > >> > So how to understand the meaning of "asynchronous"? >> >> "Asynchronous" means asynchronous - the parent is *not* >> waiting. >> >> Which is the reason that there is (amongst other things) >> the "wait"-method you can call to wait for the child to be >> terminated. On Fri, Jul 24, 2009 at 9:52 AM, Tim wrote: > > Thanks! If that is the case, i.e. the parent doesn't wait, is the code in my > last post wrong? "result" could be nothing. No, it will be a subprocess.Popen object representing the spawned process. Both the child process and the parent Python process will then be running simultaneously.This allows the Python program to interact with the subprocess via the Popen object while the subprocess is executing in parallel with Python. The asynchronicity means that the call to the Popen constructor does not wait for the spawned subprocess to terminate before returning the new Popen object to the parent Python program. Cheers, Chris -- http://blog.rebertia.com -- http://mail.python.org/mailman/listinfo/python-list
Re: SONAME for python modules is bad? (aka multiple module version)
mathieu schrieb: As far as I know there has not been any consensus on how to install multiple version of a same module in python ? What are the recommended mechanism ? I use virtualenvs for everything. Especially on unixish OSes this usually works without problems. On windows, things are a bit different, as sometimes you only get binary installers that insist on installing into the base-installation. I could not find any documentation on the subject. Does anyone sees any issue with using standard SONAME mechanism when installing a python module ? I don't understand that. You mean the .so.X.Y.Z-thingy under *ix? That would essentially be the pkg_resources.require-road, yes. But as it's not widely adopted, it will cause you troubles because some packages won't declare their dependencies properly. Diez -- http://mail.python.org/mailman/listinfo/python-list
Re: cgi.fieldstorage()
gert schrieb:
this is a non standard way to store multi part post data on disk
def application(environ, response):
with open('/usr/httpd/var/wsgiTemp','w') as f:
while True:
chunk = environ['wsgi.input'].read(8192).decode('latin1')
if not chunk: break
f.write(chunk)
response('200 OK',[])
return ['complete']
my question is how do i handle the file, so i can shuffle it into a db
using small chunks of memorie ?
I don't think that's possible with the current DB-API. There is no
stream-based BLOB-interface (as e.g. JDBC offers).
So the answer certainly depends on your used RDBMS. For oracle, you
would be lucky:
http://cx-oracle.sourceforge.net/html/lob.html
Other adapters I don't know about.
Diez
--
http://mail.python.org/mailman/listinfo/python-list
Re: trouble with wrapping a c program
On Fri, Jul 24, 2009 at 5:34 AM, Sanne Korzec wrote:
> Hi Mailing,
>
> I am using a c program, which first initializes for some seconds and then
> waits for user input (keyboard) to type something. When enter is pressed the
> c program continues.
> Using the keyboard and then enter in the c program prompt works, but I wish
> to do this from the python script by sending the string from the python
> script.
>
> I am able to print the string from python with a print command or with a
> stdout.write command. But this simply prints it to the prompt, the c program
> does nothing with this printed string.
>
> Is there a way to pipe, stream, or send this string to the running c
> program?
> Here is a small fragment of my code:
>
> #initialization
>
> cmd = [a list of my program and arguments]
>
> subprocess.Popen(cmd) #starts the c program
import subprocess
cmd = [a list of my program and arguments]
process = subprocess.Popen(cmd, stdin=subprocess.PIPE) #starts the c program
line = raw_input("Please enter a line of input for the C program:")
process.stdin.write(line)
process.stdin.write("\n")
You might want to study the docs for the subprocess module:
http://docs.python.org/library/subprocess.html
Cheers,
Chris
--
http://blog.rebertia.com
--
http://mail.python.org/mailman/listinfo/python-list
Re: len() should always return something
On Fri, 24 Jul 2009 16:10:07 +0100, Piet van Oostrum wrote: "Rhodri James" (RJ) wrote: RJ> On Fri, 24 Jul 2009 14:57:02 +0100, Grant Edwards wrote: On 2009-07-24, Dr. Phillip M. Feldman wrote: Some aspects of the Python design are remarkably clever, while others leave me perplexed. Here's an example of the latter: Why does len() give an error when applied to an int or float? len() should always return something; in particular, when applied to a scalar, it should return a value of 1. If len(7) returned a value of 1, then wouldn't one expect 7[0] to be valid? It isn't, so you'd then have to redefine all types so that they are sequences that can be indexed. Sounds like a big mess to me... [Are there types for which len() returns a value that can't be indexed?] RJ> Dictionaries. RJ> Which doesn't make your point less valid. In fact I'd go so RJ> far as to argue that what len() gives you is the number of RJ> items in a container, so len(7) should return 0. But len(7) could as well be defined as 3, 1, 32, or 64 (depending on the implementation). Therefore it doesn't make much sense. Quite. -- Rhodri James *-* Wildebeest Herder to the Masses -- http://mail.python.org/mailman/listinfo/python-list
Re: trouble with minidom
On Tue, Jul 21, 2009 at 7:32 PM, Gabriel Genellina
wrote:
> En Tue, 21 Jul 2009 21:08:57 -0300, Ronn Ross
> escribió:
>
>
> Hello I'm trying to read an xml file using minidome. The xml looks like:
>>
>>
>> myProj
>> /here/
>>
>>
>>
>> My code looks like so:
>> from xml.dom.minidom import parse
>>
>> dom = parse("myfile.xml")
>>
>> for node in dom.getElementsByTagName("project'):
>> print('name: %s, path: %s \n') % (node.childNodes[0].nodeValue,
>> node.childNodes[1])
>>
>> Unfortunately, it returns 'nodeValue as none. I'm trying to read the value
>> out of the node fir example name: myProj. I haven't found much help in the
>> documentation. Can someone point me in the right direction?
>>
>
> Unless you have a specific reason to use the DOM interface (like having a
> masochistic mind), working with ElementTree usually is a lot easier:
>
> py> import xml.etree.ElementTree as ET
> py> xml = """
> ...
> ...myProj
> .../here/
> ...
> ... """
> py> doc = ET.fromstring(xml)
> py> for project in doc.findall('project'):
> ... for child in project.getchildren():
> ... print child.tag, child.text
> ...
> name myProj
> path /here/
>
> --
> Gabriel Genellina
>
> --
> http://mail.python.org/mailman/listinfo/python-list
>
I have used the loop below and it works great, but I need get both child
elements or 'project' per iteration. I want to build a dictionary that
resemble this:
my_dict = {'myProj':'/here/', 'anothername':'anotherpath'}
I couldn't find how to do with in the element tree docs. Can you point me in
the right direction?
--
http://mail.python.org/mailman/listinfo/python-list
ElementTree's Element substitution in Python 3
I have a function to replace the content of an ElementTree Element by
that of another one which works using Python 2 but not with Python 3.
I get an assertion error. The function is as follows:
def replace_element(elem, replacement):
'''replace the content of an ElementTree Element by that of
another
one.
'''
elem.clear()
elem.text = replacement.text
elem.tail = replacement.tail
elem.tag = replacement.tag
elem.attrib = replacement.attrib
elem[:] = replacement[:]
The last line is problematic. For example, if I do the following
program with Python2.5
###
from xml.etree import ElementTree as et
a = et.Element('a')
b = et.SubElement(a, 'b')
c = et.Element('c')
a[:] = c[:]
###
nothing of note happens - however, doing the same with Python 3.1, I
get the following traceback:
Traceback (most recent call last):
File "test.py", line 7, in
a[:] = c[:]
File "/usr/local/py3.1/lib/python3.1/xml/etree/ElementTree.py", line
210, in __setitem__
assert iselement(element)
AssertionError
==
I would gladly welcome any suggestion for writing a replace_element()
function that works with Python 3.1
André
--
http://mail.python.org/mailman/listinfo/python-list
Re: Re: len() should always return something
> Jul 24, 2009 07:02:29 AM, [email protected] wrote: > > On Thu, Jul 23, 2009 at 11:35 PM, Dr. Phillip M. > Feldman wrote: >> >> Some aspects of the Python design are remarkably clever, while others >> leave >> me perplexed. Here's an example of the latter: Why does len() give an >> error >> when applied to an int or float? len() should always return something; in >> particular, when applied to a scalar, it should return a value of 1. Of >> course, I can define my own function like this: >> >> def mylen(x): >> if isinstance(x,int) or isinstance(x,float): return 1 >> return len(x) >> >> But, this shouldn't be necessary. > > The problem is that redefining len()/length/size that way would > violate several principles of Python's design (The "Zen" of Python - > http://www.python.org/dev/peps/pep-0020/). > > Specifically: > - Explicit is better than implicit. > - Special cases aren't special enough to break the rules. > - Errors should never pass silently. > - In the face of ambiguity, refuse the temptation to guess. > > If you'd explain the situation that prompts you to find this > redefinition necessary, I'm sure someone can suggest a better > approach. On Fri, Jul 24, 2009 at 8:58 AM, Phillip M. Feldman wrote: > I've read the "Zen of Python", but most of these aphorisms are vague and > could be understood differently by different readers. In particular, I > don't understand the statement that "explicit is better than implicit". > Some examples of this would be helpful. > > I've been converting Matlab codes to Python. In Matlab, a scalar is just a > one-by-one matrix and has a length of 1. This convention seems no less > arbitrary to me than Python's convention that the concept of length is not > applicable to ints and floats. My workaround was to write the following > function: > > def is_scalar(x): >"""Return True if x is an instance of int, float, or complex. >Otherwise, return False. Note: If x is a length-1 list or array >containing an int, float, or complex value, False is returned.""" >if isinstance(x,int) or isinstance(x,float) or isinstance(x,complex): > return True >return False > > The application is the following: In various types of scientific > applications, one operates on a list of measurements. If there is only a > single measurement, it is reasonable to allow the calling program to pass a > scalar without wrapping it up into a list or array. You could use Python's extended call syntax when defining your function: def average(*args): return sum(args) / len(args) average(7) #==> 7 average(2,3,4,5,6) #==> 4 average(*[2,3,4,5,6]) #==> 4 average([2,3,4,5,6]) #==> error Cheers, Chris -- http://blog.rebertia.com -- http://mail.python.org/mailman/listinfo/python-list
Re: non-owning references?
On Fri, 24 Jul 2009 16:03:58 +0100, Piet van Oostrum wrote: "Rhodri James" (RJ) wrote: RJ> On Fri, 24 Jul 2009 14:55:45 +0100, Hrvoje Niksic wrote: Ben Finney writes: Utpal Sarkar writes: Is there a way I can tell a variable that the object it is pointing too is not owned by it, in the sense that if it is the only reference to the object it can be garbage collected? Python doesn't have “pointers”, and doesn't really have “variables” either, at least not how many other languages use that term. The OP didn't use the term "pointer", but the word "pointing", which makes sense in the context. The term "variable" is used in the Python language reference and elsewhere, and is quite compatible with how other popular languages (Java, PHP, Lisp, ...) use it. RJ> Only superficially. Treating Python variables the same as C variables RJ> (say) is one of the classic ways that newbies come unstuck when mutable RJ> objects appear on the scene. While the OP appears to have the right idea, RJ> your "correction" here could be quite misleading. If you read the OP, it is clear that he talked about a class variable, which is a perfectly legal notion in Python, and is mentioned as such in the language reference manual: `Variables defined in the class definition are class variables' Yes. I didn't think I needed to say that explicitly. And who was talking about C variables? Hrvoje, implicitly. 'The term "variable" is used in the Python language reference and elsewhere, and is quite compatible with how other popular languages (Java, PHP, Lisp, ...) use it.' I listed C as another example of a popular language because I am very familiar with how C's variables work; I don't know Java, I've never programmed PHP in anger and it's twenty years since I last touched Lisp. The point was, and remains, that this newsgroup gets regular traffic from people who expect Python's variables to act like C's variables, demonstrating that describing them as "quite compatible" is somewhat misleading. -- Rhodri James *-* Wildebeest Herder to the Masses -- http://mail.python.org/mailman/listinfo/python-list
Re: If Scheme is so good why MIT drops it?
On 2009-07-23 23:51:02 -0400, Carl Banks said: On Jul 23, 5:52 pm, Rui Maciel wrote: fft1976 wrote: How do you explain that something as inferior as Python beat Lisp in the market place despite starting 40 years later. Probably due to similar reasons that lead php to become remotely relevant . Well, the only reason PHP became relevant because it was an easy (emphasis added) to deploy solution in a single application domain, the web, that happened to explode. i.e., Python "beat" lisp because it is ~70% of lisp in a form that is much more palatable to the average programmer, just as php became popular because it is powerful enough to do websites and, most importantly, apprehensible to mediocre programmers and even some non-programmers. -- Raffael Cavallaro -- http://mail.python.org/mailman/listinfo/python-list
Re: cgi.fieldstorage()
On Jul 24, 7:32 pm, "Diez B. Roggisch" wrote:
> gert schrieb:
>
> > this is a non standard way to store multi part post data on disk
>
> > def application(environ, response):
> > with open('/usr/httpd/var/wsgiTemp','w') as f:
> > while True:
> > chunk = environ['wsgi.input'].read(8192).decode('latin1')
> > if not chunk: break
> > f.write(chunk)
> > response('200 OK',[])
> > return ['complete']
>
> > my question is how do i handle the file, so i can shuffle it into a db
> > using small chunks of memorie ?
>
> I don't think that's possible with the current DB-API. There is no
> stream-based BLOB-interface (as e.g. JDBC offers).
>
> So the answer certainly depends on your used RDBMS. For oracle, you
> would be lucky:
>
> http://cx-oracle.sourceforge.net/html/lob.html
>
> Other adapters I don't know about.
sqlite :) ok let say for now it would be impossible on a db level, but
before i reach the impossible, i still need to parse the file to
prepare the chunks. How do i do that ? How do i get the chunks without
loading the hole file into memorie ?
b = environ['CONTENT_TYPE'].split('boundary=')[1]
data = search(b+r'.*?Content-Type: application/octet-stream\r\n\r
\n
(.*?)\r\n--'+b,t,DOTALL).group(1)
data = data.encode('latin1')
--
http://mail.python.org/mailman/listinfo/python-list
exceptions.TypeError an integer is required
I am trying to do a post to a REST API over HTTPS and requires the
script to pass a cert to the server. I am getting
"exceptions.TypeError an integer is required" error and can't find the
reason. I commenting out the lines of code, it is happening on the
connection.request() line. Here is the problem code. Would love some
help if possible.
head = {"Content-Type" : "application/x-www-form-urlencoded",
"Accept" : "text/plain"}
parameters = urlencode({"collection" : collection, "entryxml" : open
(file,'r').read()})
try:
connection = httplib.HTTPSConnection(host, port, key_file,
cert_file)
connection.request('POST', path, parameters, head)
response = connection.getresponse()
print response.status, response.reason
except:
print sys.exc_type, sys.exc_value
connection.close()
--
http://mail.python.org/mailman/listinfo/python-list
Re: Predefined Variables
> Scott David Daniels (SDD) wrote:
>SDD> Stephen Cuppett (should have written in this order):
>>> "Fred Atkinson" wrote ...
Is there a pre-defined variable that returns the GET line
(http://www.php.net/index.php?everythingafterthequestionmark) as a
single variable (rather than individual variables)?
>>> os.environment('QUERY_STRING')
>SDD> Maybe you mean:
>SDD> os.environ['USER']
Let's take the best of both:
os.environ['QUERY_STRING']
--
Piet van Oostrum
URL: http://pietvanoostrum.com [PGP 8DAE142BE17999C4]
Private email: [email protected]
--
http://mail.python.org/mailman/listinfo/python-list
Re: len() should always return something
Phillip M. Feldman wrote: I've been converting Matlab codes to Python. In Matlab, a scalar is just a one-by-one matrix and has a length of 1. This convention seems no less arbitrary to me than Python's convention that the concept of length is not applicable to ints and floats. Multiplication of a vector/matrix by a scalar always defined and commutative. Multiplication of a vector/matrix by a 1x1 matrix is not always even defined. So not having scalars in a matrix package strikes me as a bit odd. > My workaround was to write the following function: def is_scalar(x): """Return True if x is an instance of int, float, or complex. Otherwise, return False. Note: If x is a length-1 list or array containing an int, float, or complex value, False is returned.""" if isinstance(x,int) or isinstance(x,float) or isinstance(x,complex): Better:if isinstance(x, (int, float, complex)): but you forgot decimals and fractions and any other possible number modules. In 3.1, >>> from numbers import Number >>> from decimal import Decimal >>> from fractions import Fraction >>> for x in 1, 1.0, (1+0j), Decimal(1), Fraction(1,1): isinstance(x, Number) True True True True True and the same for any other module that registers a class as a Number return True return False The application is the following: In various types of scientific applications, one operates on a list of measurements. If there is only a single measurement, it is reasonable to allow the calling program to pass a scalar without wrapping it up into a list or array. If you want to do that, start with def f(x): try: len(x) except TypeError: x = x, or in 3.1 use Number test above. Terry Jan Reedy -- http://mail.python.org/mailman/listinfo/python-list
How can I get the line number ?
Hello I am writing some Python code that runs in another application(has wrapper functions). Due to lack of debugging I am printing out alot of outputs and manual messages. I want to be able to create a function that would let me print the current line number that is called from. This is not for debugging exceptions it is rather to simplify my debug messages, at least I can trace my debug messages. thanks -- http://mail.python.org/mailman/listinfo/python-list
Re: How can I get the line number ?
On Fri, Jul 24, 2009 at 2:51 PM, kk wrote:
> Hello
>
> I am writing some Python code that runs in another application(has
> wrapper functions). Due to lack of debugging I am printing out alot of
> outputs and manual messages. I want to be able to create a function
> that would let me print the current line number that is called from.
> This is not for debugging exceptions it is rather to simplify my debug
> messages, at least I can trace my debug messages.
>
> thanks
Modify the following as needed:
from inspect import currentframe, getframeinfo
def caller_info(depth=0):
"""
Get file, line number, and name of the calling function.
"""
if depth < 0:
raise ValueError('invalid stack depth')
caller = frame = currentframe()
try:
for i in xrange(-1, depth):
caller = caller.f_back
if caller is None:
return (None, None, None)
return getframeinfo(caller, 0)[:3]
finally:
del caller, frame
- Max
--
http://mail.python.org/mailman/listinfo/python-list
Re: len() should always return something
On Fri, 24 Jul 2009 16:50:03 +0200, superpollo wrote:
>> Nah. 7 contains three bits, so len(7) should *clearly* return 3.
>
> and len("7") must return 8, by the same token... but wait!
>
> >>> len("7")
> 1
> >>>
> >>>
> my python installation must me outdated ;-)
No no no, you're obviously using an awesome version of Python that can
compress single-character strings to a single bit!
--
Steven
--
http://mail.python.org/mailman/listinfo/python-list
Re: Convert points to polygon shapefile
On 2009-07-24 05:21, Luis Pedro Almeida wrote: Dear all, I would like to know how to convert a list of points into a polygon shapefile (esri). shapelib has Python bindings. http://shapelib.maptools.org/ -- Robert Kern "I have come to believe that the whole world is an enigma, a harmless enigma that is made terrible by our own mad attempt to interpret it as though it had an underlying truth." -- Umberto Eco -- http://mail.python.org/mailman/listinfo/python-list
Re: non-owning references?
On Fri, 24 Jul 2009 15:55:45 +0200, Hrvoje Niksic wrote: > The term "variable" is used in the Python > language reference and elsewhere, and is quite compatible with how other > popular languages (Java, PHP, Lisp, ...) use it. Please stop > complaining about valid terminology; it is not helpful. No, the use of the single term "variable" to describe two distinct program models is not helpful. Whether other languages muddy the water between memory-location based variables and name-binding is irrelevant to whether we should do so. And quite a few of us are disappointed that the Python language reference should confuse the issue by using misleading terminology. Unfortunately, the use of "variable" is so ingrained, and so simple compared to name binding terminology, that I fear we'll never eradicate it. I know sometimes I use it myself, but always with a little shiver of shame that I'm misusing terminology. -- Steven -- http://mail.python.org/mailman/listinfo/python-list
list vs. tuple [Re: len() should always return something]
In article , Terry Reedy wrote: > Better:if isinstance(x, (int, float, complex)): I never noticed this before, but it seems odd that the second argument to isinstance() should be a tuple. Using the normal arguments made about tuples vs. lists, it seems like a list would be the right data structure here. I suppose a set would be even more right, but (I'm pretty sure) isinstance() predates sets. I'm curious why a tuple was chosen. -- http://mail.python.org/mailman/listinfo/python-list
Re: len() should always return something
In article <[email protected]>, Steven D'Aprano wrote: > On Fri, 24 Jul 2009 16:50:03 +0200, superpollo wrote: > > >> Nah. 7 contains three bits, so len(7) should *clearly* return 3. > > > > and len("7") must return 8, by the same token... but wait! > > > > >>> len("7") > > 1 > > >>> > > >>> > > my python installation must me outdated ;-) > > No no no, you're obviously using an awesome version of Python that can > compress single-character strings to a single bit! Compressing strings to a single bit is easy. It's the uncompressing that's tricky. -- http://mail.python.org/mailman/listinfo/python-list
Re: len() should always return something
On Fri, 24 Jul 2009 00:02:28 -0700, Chris Rebert wrote: > On Thu, Jul 23, 2009 at 11:35 PM, Dr. Phillip M. > Feldman wrote: >> >> Some aspects of the Python design are remarkably clever, while others >> leave me perplexed. Here's an example of the latter: Why does len() >> give an error when applied to an int or float? len() should always >> return something; in particular, when applied to a scalar, it should >> return a value of 1. Of course, I can define my own function like this: >> >> def mylen(x): >> if isinstance(x,int) or isinstance(x,float): return 1 return len(x) >> >> But, this shouldn't be necessary. > > The problem is that redefining len()/length/size that way would violate > several principles of Python's design (The "Zen" of Python - > http://www.python.org/dev/peps/pep-0020/). > > Specifically: > - Explicit is better than implicit. > - Special cases aren't special enough to break the rules. > - Errors should never pass silently. > - In the face of ambiguity, refuse the temptation to guess. Chris, I'm curious why you think that these Zen are relevant to the OP's complaint. Re explicit vs implicit, len(42) is just as explicit as len([42, 23]). Arguably (I wouldn't argue this, but some people might) ints aren't "special enough" to break the rule that len(obj) should always return something. (I don't actually agree, but some people might be able to produce a coherent argument why len() should apply equally to all objects.) Re errors passing silently, the OP doesn't believe that len(42) should be an error, so that's not relevant. And there's nothing ambiguous about len(42). I agree with the current Python behaviour, but I don't think there's anything in the Zen to support it. As far as I know, there is no programming language which treats scalars like ints as if they were vectors of length 1, which makes Python's choice to make ints unlengthed a no-brainer. -- Steven -- http://mail.python.org/mailman/listinfo/python-list
Re: ElementTree's Element substitution in Python 3
Sorry for replying to myself ... the following seems to be a working solution to my original problem. On Jul 24, 2:54 pm, André wrote: > I have a function to replace the content of an ElementTree Element by > that of another one which works using Python 2 but not with Python 3. > I get an assertion error. The function is as follows: > > def replace_element(elem, replacement): > '''replace the content of an ElementTree Element by that of > another > one. > ''' > elem.clear() > elem.text = replacement.text > elem.tail = replacement.tail > elem.tag = replacement.tag > elem.attrib = replacement.attrib > elem[:] = replacement[:] > Use instead: def replace_element(elem, replacement): '''replace the content of an ElementTree Element by that of another one. ''' elem.clear() elem.text = replacement.text elem.tail = replacement.tail elem.tag = replacement.tag elem.attrib = replacement.attrib try: elem[:] = replacement[:] # works with Python 2.x (fast) but not 3.x except AssertionError: del elem[:] for child in replacement: elem.append(child) André -- http://mail.python.org/mailman/listinfo/python-list
Re: exceptions.TypeError an integer is required
On Fri, 24 Jul 2009 11:24:58 -0700, jakecjacobson wrote: > I am trying to do a post to a REST API over HTTPS and requires the > script to pass a cert to the server. I am getting "exceptions.TypeError > an integer is required" error and can't find the reason. I commenting > out the lines of code, it is happening on the connection.request() line. > Here is the problem code. Would love some help if possible. Please post the traceback that you get. My guess is that you are passing a string instead of an integer, probably for the port. [...] > except: > print sys.exc_type, sys.exc_value As a general rule, a bare except of that fashion is bad practice. Unless you can explain why it is normally bad practice, *and* why your case is an exception (no pun intended) to the rule "never use bare except clauses", I suggest you either: * replace "except:" with "except Exception:" instead. * better still, re-write the entire try block as: try: [code goes here] finally: connection.close() and use the Python error-reporting mechanism instead of defeating it. -- Steven -- http://mail.python.org/mailman/listinfo/python-list
Eclipse Pydev update error ?
Does anyone know why this error is occurring in my Eclipse Pydev update ? An error occurred while collecting items to be installed No repository found containing: org.python.pydev/osgi.bundle/1.4.7.2843 No repository found containing: org.python.pydev.ast/osgi.bundle/1.4.7.2843 No repository found containing: org.python.pydev.core/osgi.bundle/1.4.7.2843 No repository found containing: org.python.pydev.debug/osgi.bundle/1.4.7.2843 No repository found containing: org.python.pydev.feature/org.eclipse.update.feature/1.4.7.2843 No repository found containing: org.python.pydev.help/osgi.bundle/1.4.7.2843 No repository found containing: org.python.pydev.jython/osgi.bundle/1.4.7.2843 No repository found containing: org.python.pydev.parser/osgi.bundle/1.4.7.2843 No repository found containing: org.python.pydev.refactoring/osgi.bundle/1.4.7.2843 No repository found containing: org.python.pydev.templates/osgi.bundle/1.4.7.2843 No repository found containing: org.python.pydev.customizations/osgi.bundle/1.4.7.2843 Thanks -- http://mail.python.org/mailman/listinfo/python-list
Re: Adding method from one class to another class or to instance of another class
marekw2143 wrote: Hi, I have one class (A) that has defined method createVars. I would like to add that method to class B The code looks like this: class A(object): def createVars(self): self.v1 = 1 self.v2 = 3 pass class B(object): pass I don't want to use inheritance (because class A has many methods defined that class B doesn't need). When I try the folloowing: B.createVars = C.createVars you meant A.createVars B().createVars() then the following error occurs: Traceback (most recent call last): File "", line 1, in TypeError: unbound method createVars() must be called with A instance as first argument (got nothing instead) In 3.1, your example works fine. The difference is that in 2.x, B.createVars is a method wrapperthat wraps the function, whereas in 3.1, it is the function itself. For 2.x, you need to extract the function from the wrapper. It is im_func or something like that. Use dir(B.createVars) to check for sure. How can I solve this problem? Terry Jan Reedy -- http://mail.python.org/mailman/listinfo/python-list
Re: list vs. tuple [Re: len() should always return something]
On Fri, 24 Jul 2009 15:03:29 -0400, Roy Smith wrote: > In article , > Terry Reedy wrote: > >> Better:if isinstance(x, (int, float, complex)): > > I never noticed this before, but it seems odd that the second argument > to isinstance() should be a tuple. Using the normal arguments made > about tuples vs. lists, it seems like a list would be the right data > structure here. What would be the point of using a list? You're never going to sort it, or append items to it, or otherwise mutate it. You build it, pass it to a function which doesn't modify it in any fashion, then it gets garbage collected. > I suppose a set would be even more right, but (I'm > pretty sure) isinstance() predates sets. Yes. [st...@sylar ~]$ python1.5 Python 1.5.2 (#1, Apr 1 2009, 22:55:54) [GCC 4.1.2 20070925 (Red Hat 4.1.2-27)] on linux2 Copyright 1991-1995 Stichting Mathematisch Centrum, Amsterdam >>> isinstance >>> set Traceback (innermost last): File "", line 1, in ? NameError: set > I'm curious why a tuple was chosen. Tuples are smaller and faster to build than lists -- they're the most lightweight sequence type in Python. You don't need all the extra functionality of lists, so why go to the time and effort of building a list? -- Steven -- http://mail.python.org/mailman/listinfo/python-list
Re: ElementTree's Element substitution in Python 3
> André (A) a écrit:
>A> I have a function to replace the content of an ElementTree Element by
>A> that of another one which works using Python 2 but not with Python 3.
>A> I get an assertion error. The function is as follows:
>A> def replace_element(elem, replacement):
>A> '''replace the content of an ElementTree Element by that of
>A> another
>A>one.
>A> '''
>A> elem.clear()
>A> elem.text = replacement.text
>A> elem.tail = replacement.tail
>A> elem.tag = replacement.tag
>A> elem.attrib = replacement.attrib
>A> elem[:] = replacement[:]
>A> The last line is problematic. For example, if I do the following
>A> program with Python2.5
>A> ###
>A> from xml.etree import ElementTree as et
>A> a = et.Element('a')
>A> b = et.SubElement(a, 'b')
>A> c = et.Element('c')
>A> a[:] = c[:]
>A> ###
>A> nothing of note happens - however, doing the same with Python 3.1, I
>A> get the following traceback:
>A> Traceback (most recent call last):
>A> File "test.py", line 7, in
>A> a[:] = c[:]
>A> File "/usr/local/py3.1/lib/python3.1/xml/etree/ElementTree.py", line
>A> 210, in __setitem__
>A> assert iselement(element)
>A> AssertionError
This is a Python bug. Please report it. The problem is that in Python 3
slice assignments are done with __setitem__ rather than __setslice__ but
ElementTree has not been adapted to that.
--
Piet van Oostrum
URL: http://pietvanoostrum.com [PGP 8DAE142BE17999C4]
Private email: [email protected]
--
http://mail.python.org/mailman/listinfo/python-list
Re: Eclipse Pydev update error ?
On Fri, Jul 24, 2009 at 4:17 PM, Linuxguy123 wrote: > Does anyone know why this error is occurring in my Eclipse Pydev > update ? > > An error occurred while collecting items to be installed > No repository found containing: > org.python.pydev/osgi.bundle/1.4.7.2843 > No repository found containing: > org.python.pydev.ast/osgi.bundle/1.4.7.2843 > No repository found containing: > org.python.pydev.core/osgi.bundle/1.4.7.2843 > No repository found containing: > org.python.pydev.debug/osgi.bundle/1.4.7.2843 > No repository found containing: > org.python.pydev.feature/org.eclipse.update.feature/1.4.7.2843 > No repository found containing: > org.python.pydev.help/osgi.bundle/1.4.7.2843 > No repository found containing: > org.python.pydev.jython/osgi.bundle/1.4.7.2843 > No repository found containing: > org.python.pydev.parser/osgi.bundle/1.4.7.2843 > No repository found containing: > org.python.pydev.refactoring/osgi.bundle/1.4.7.2843 > No repository found containing: > org.python.pydev.templates/osgi.bundle/1.4.7.2843 > No repository found containing: > org.python.pydev.customizations/osgi.bundle/1.4.7.2843 > This usually happens if there was some connection error during the update. You can try other mirrors (see: http://pydev.blogspot.com/2009/07/pydev-147-released.html for the new added mirrors). Cheers, Fabio -- http://mail.python.org/mailman/listinfo/python-list
