This algorithm written in Python solves at least a subset of the Hamilton Circuit problem, which is NP complete, in n^3 time.
This algorithm written in Python solves at least a subset of the Hamilton Circuit problem, which is NP complete, in n^3 time. #!/usr/bin/env python # # hamiltoncircuit.python # # Copyright 2011 Martin Musatov # # This program is free software; you may redistribute it and/or modify # it under the terms of the GNU General Public License as published by # the Free Software Foundation; either version 2 of the License, or # (at your option) any later version. # # This program is distributed in the hope it is useful, # but WITHOUT ANY WARRANTY; without even the implied warranty of # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the # GNU General Public License for more details. # # You may obtain a copy of the GNU General Public License # with this program from the Free Software # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, # MA 02110-1301, USA. // numnodes=10 // // import random // // class allnodes(): // connect = [] // gridpos = [] // initpos = [] // def __init__(self, numnodes): // for i in range(0, numnodes): // self.gridpos.append(i) // self.initpos.append(i) // self.connect.append([]) // // nodes=allnodes(numnodes) // def swapref(n, a, b): // t = nodes.initpos[a] // nodes.initpos[a] = nodes.initpos[b] // nodes.initpos[b] = t // for i in range(0, len(n)): // for j in range(0, len(n[i])): // if n[i][j] == a: // n[i][j] = b // elif n[i][j] == b: // n[i][j] = a // return n // def makeswap(grida, gridb): // ascore = 0 // aswapscore = 0 // bscore = 0 // bswapscore = 0 // if grida > 1 and grida < numnodes-1: // for i in range(0, len(nodes.connect[grida])): // if nodes.connect[grida][i] == grida - 2 or nodes.connect[grida][i] == grida + 2: // ascore+=1 // // elif grida == 0: // for i in range(0, len(nodes.connect[grida])): // if nodes.connect[grida][i] == grida + 1 or nodes.connect[grida][i] == grida + 2: // ascore+=1 // elif grida == 1: // for i in range(0, len(nodes.connect[grida])): // if nodes.connect[grida][i] == grida - 1 or nodes.connect[grida][i] == grida + 2: // ascore+=1 // elif grida == numnodes: // for i in range(0, len(nodes.connect[grida])): // if nodes.connect[grida][i] == grida - 1 or nodes.connect[grida][i] == grida - 2: // ascore+=1 // elif grida == numnodes-1: // for i in range(0, len(nodes.connect[grida])): // if nodes.connect[grida][i] == grida + 1 or nodes.connect[grida][i] == grida - 2: // ascore+=1 // if gridb > 1 and gridb < numnodes-1: // for i in range(0, len(nodes.connect[gridb])): // if nodes.connect[gridb][i] == gridb - 2 or nodes.connect[gridb][i] == gridb + 2: // bscore+=1 // elif gridb == 0: // for i in range(0, len(nodes.connect[gridb])): // if nodes.connect[gridb][i] == gridb + 1 or nodes.connect[gridb][i] == gridb + 2: // bscore+=1 // elif gridb == 1: // for i in range(0, len(nodes.connect[gridb])): // if nodes.connect[gridb][i] == gridb - 1 or nodes.connect[gridb][i] == gridb + 2: // bscore+=1 // elif gridb == numnodes: // for i in range(0, len(nodes.connect[gridb])): // if nodes.connect[gridb][i] == gridb - 1 or nodes.connect[gridb][i] == gridb - 2: // bscore+=1 // elif gridb == numnodes-1: // for i in range(0, len(nodes.connect[gridb])): // if nodes.connect[gridb][i] == gridb + 1 or nodes.connect[gridb][i] == gridb - 2: // bscore+=1 // tempnodes = [] // tempnodes.extend(nodes.connect) // t = tempnodes[grida] // tempnodes[grida]=tempnodes[gridb] // tempnodes[gridb]=t // // if grida > 1 and grida < numnodes-1: // for i in range(0, len(tempnodes[grida])): // if tempnodes[grida][i] == grida - 2 or tempnodes[grida][i] == grida + 2: // aswapscore+=1 // // elif grida == 0: // for i in range(0, len(tempnodes[grida])): // if tempnodes[grida][i] == grida + 1 or tempnodes[grida][i] == grida + 2: // aswapscore+=1 // elif grida == 1: // for i in range(0, len(tempnodes[grida])): // if tempnodes[grida][i] == grida - 1 or tempnodes[grida][i] == grida + 2: // aswapscore+=1 // elif grida == numnodes: //
SLOW AND FAST FUNCTIONS
//\ PROJF //P\ SLOW VER // GDRAW PROJF DEMO P // // P // XEQ GDRAW // //P \ PROJF // // \ FAST VER // @ domain [http://meami.org/fastslow.htm] -- http://mail.python.org/mailman/listinfo/python-list
Any timeline for PIL for Python 3.4
Hi, I am running Python 3.4.4, and would like to use the Python Imaging Library (PIL). This is currently not available for Python Version 3. Does anybody know when it will become available? Plan B is to install Python 2.7.18. I just need an idea of how long I would need to wait for Plan A. -- Regards, Martin Leese E-mail: [email protected] Web: http://members.tripod.com/martin_leese/ -- https://mail.python.org/mailman/listinfo/python-list
Message about not deleted folders using Inno Setup
Hi I would like to place a message in an uninstaller window which will inform the user that some folders haven't been deleted. Is that possible using Inno Setup? -- Thanks in advance Martin -- http://mail.python.org/mailman/listinfo/python-list
Python/ASP local characters and 500 server error
Using Python / ASP on a IIS server with Mark Hammond's win32 extensions, i have the following problem. All occurences of local characters (fx. danish æøå) in comments or in strings result in a HTTP/1.1 500 Server Error. Is there a solution to this problem? /Martin -- http://mail.python.org/mailman/listinfo/python-list
Re: Python/ASP local characters and 500 server error
Fredrik Lundh wrote: > > http://www.python.org/peps/pep-0263.html > Using the info from pep-0263, adding a line in the start for the file: # -*- coding: -*- works for .py files, but the error i get is from an .asp file. The logfile contains this info: ASP_0147|500_Server_Error 500 A solution is to move ALL code from the .asp page to a .py file and import it on the .asp page. But there must be another solution? /Martin -- http://mail.python.org/mailman/listinfo/python-list
Re: Odd behaviour of regexp module
Hi David, b.group() is equivalent to b.group(0), the entire RE match. (^(.*?)\.) will give you 'dfsf.' for that input string. What you want is b.group(1), the subgroup you're looking for inside the main RE. (.*?) which gives you 'dfsf', which is what you're looking for. Cheers, Martin On 13 Jul 2005 14:05:37 -0700, "David Veerasingam" <[EMAIL PROTECTED]> wrote: >Hello > >It seems the grouping feature isn't behaving correctly. > >In [1]: a = 'dfsf.oct.ocfe' > >In [2]: b = re.match(r'^(.*?)\.', a); b.group() >'dfsf.' > >The expected result is 'dfsf'. Why did the regexp grab that period at >the end? > >David -- http://mail.python.org/mailman/listinfo/python-list
Re: Can __new__ prevent __init__ from being called?
Felix Wiemann wrote:
> Sometimes (but not always) the __new__ method of one of my classes
> returns an *existing* instance of the class. However, when it does
> that, the __init__ method of the existing instance is called
> nonetheless, so that the instance is initialized a second time.
[snip]
> How can I prevent __init__ from being called on the
already-initialized
> object?
>
> I do not want to have any code in the __init__ method which checks if
> the instance is already initialized (like "if self.initialized:
return"
> at the beginning) because that would mean I'd have to insert this
> checking code in the __init__ method of every subclass.
>
> Is there an easier way than using a metaclass and writing a custom
> __call__ method?
Yes. You could move all your initalization logic from __init__ to a
separate init method and use the following simple recipe that does not
involve metaclasses and overriding __call__.
Although the base class __init__ does have to check to see if the
instance is initialized, you don't have to repeat the code in derived
classes:
class C(object):
def __new__(cls, *args, **kwds):
it = cls.__dict__.get("__it__")
if it is not None:
return it
cls.__it__ = it = object.__new__(cls)
it.init(*args, **kwds)
return it
def init(self, *args, **kwds): # only called once
print 'In C init.'
pass
def __init__(self): # called each time
print 'In C __init__.'
class D(C):
def init(self, *args, **kwds): # only called once
print 'In D init.'
pass
def __init__(self): # called each time
print 'In D __init__.'
>>> C()
In C init.
In C __init__.
>>> C()
In C __init__.
>>> D()
In D init.
In D __init__.
>>> D()
In D __init__.
-Martin
--
http://mail.python.org/mailman/listinfo/python-list
Re: Can __new__ prevent __init__ from being called?
I meant to say: Although the base class __new__ does have to check to see if the ^^^ instance is initialized, ... not: > Although the base class __init__ does have to check to see if the > instance is initialized, ... -- http://mail.python.org/mailman/listinfo/python-list
asyncio: setting file permissions of a Unix socket?
Hi! :)
I'm using the asyncio.Protocol interface to build a server which binds
to a unix socket file. I want other system users to connect to the
unix socket, so to communicate with the server.
Where should I set the permissions of the file?
The problem is that the socket file is created when the programs
starts listening for the connections. Because of this, I can't change
the file permission before a connection is made. At the same time, a
connection can't happen because of the file permissions.
Currently, I workaround this with os.umask(0o000). But I also want to
make other files later with default permissions. So I have to revert
the umask, but where? I can only do this in the client connection
code, which is out of place.
Bellow is what I've done. Can you suggest a better way? I wish to
avoid permission fixing code like os.umask() for every connection.
Maybe I shouldn't use asyncio.Protocol in the first place?
Thank you! :)
Example code:
#! /usr/bin/env python3
import os
import asyncio
class ExampleServer(asyncio.Protocol):
def __init__(self):
os.umask(0o002)# This is my workaround. Can I avoid this?
# if not umask_restored: # An alternative workaround.
# os.umask(0o002)
def connection_made(self, transport):
self.transport = transport
def data_received(self, data):
print("Data: ", data)
self.transport.write(b"OK, bye!\n")
self.transport.close()
def main():
socket_filepath = "/tmp/example-server.socket"
loop = asyncio.get_event_loop()
server_coroutine = loop.create_unix_server(ExampleServer, socket_filepath)
server = loop.run_until_complete(server_coroutine)
os.umask(0o000) # This is my workaround.
try:
loop.run_forever()
except KeyboardInterrupt:
print("exit")
finally:
server.close()
loop.close()
os.remove(socket_filepath)
if __name__ == "__main__":
main()
--
https://mail.python.org/mailman/listinfo/python-list
15 Exercises to Know A Programming Language
I am trying to improve my Python skills through some exercises. Currently I am working on Larry's "15 exercises to know a programming language " (http://www.knowing.net/ PermaLink,guid,f3b9ba36-848e-43f8-9caa-232ec216192d.aspx). The first exercise is this: "Write a program that takes as its first argument one of the words 'sum,' 'product,' 'mean,' or 'sqrt' and for further arguments a series of numbers. The program applies the appropriate function to the series." My solution so far is this: http://dpaste.com/13469/ I would really like some feedback. Is this a good solution? is it efficient? robust? what could be improved? any not looking for a revised solution, hints on what to improve are also very welcome. Martin -- http://mail.python.org/mailman/listinfo/python-list
Re: 15 Exercises to Know A Programming Language
On Jul 3, 12:25 pm, Marc 'BlackJack' Rintsch <[EMAIL PROTECTED]> wrote: > On Tue, 03 Jul 2007 09:58:16 +, Martin wrote: > > "Write a program that takes as its first argument one of the words > > 'sum,' 'product,' 'mean,' or 'sqrt' and for further arguments a > > series of numbers. The program applies the appropriate function to > > the series." > > > My solution so far is this: > > >http://dpaste.com/13469/ > > > I would really like some feedback. Is this a good solution? is it > > efficient? robust? what could be improved? any not looking for a > > revised solution, hints on what to improve are also very welcome. > > Don't use `eval()` if it is not absolutely necessary. Especially if the > input comes from a user it's a security hole. `float()` is the function > to use here. > > `mean()` does not work as you try to divide a list by a number. > > Ciao, > Marc 'BlackJack' Rintsch Thanks for the feedback. I have posted a revised version here (http:// dpaste.com/13474/) where mean works. The reason I use eval is I want it to work for complex numbers too, but I guess i could check for the presence of a "j" in the arguments instead. Martin -- http://mail.python.org/mailman/listinfo/python-list
Re: 15 Exercises to Know A Programming Language
On Jul 3, 1:47 pm, John Machin <[EMAIL PROTECTED]> wrote: > On Jul 3, 7:58 pm, Martin <[EMAIL PROTECTED]> wrote: > > > > > I am trying to improve my Python skills through some exercises. > > Currently I am working on Larry's "15 exercises to know a programming > > language " (http://www.knowing.net/ > > PermaLink,guid,f3b9ba36-848e-43f8-9caa-232ec216192d.aspx). The first > > exercise is this: > > > "Write a program that takes as its first argument one of the words > > 'sum,' 'product,' 'mean,' or 'sqrt' and for further arguments a > > series of numbers. The program applies the appropriate function to > > the series." > > > My solution so far is this: > > >http://dpaste.com/13469/ > > > I would really like some feedback. Is this a good solution? is it > > efficient? robust? what could be improved? any not looking for a > > revised solution, hints on what to improve are also very welcome. > > > Martin > > sum is a builtin function in Python 2.3 and later. You could do > something like this: > > try: > sum > except NameError: > def sum(args): > return reduce(operator.add, args) > > Tested with 2.5 back to 2.1, and 1.5.2 :-) Thanks John and Daniel. My revised version now uses sum(). I have started to implement some basic error checking and validation. Martin -- http://mail.python.org/mailman/listinfo/python-list
Error when trying to write unicode xml to zipfile
I get below error when trying to write unicode xml to a zipfile.
zip.writestr('content.xml', content.toxml())
File "/usr/lib/python2.4/zipfile.py", line 460, in writestr
zinfo.CRC = binascii.crc32(bytes) # CRC-32 checksum
UnicodeEncodeError: 'ascii' codec can't encode character u'\u25cf' in
position 2848: ordinal not in range(128)
Any ideas?
Martin
--
http://mail.python.org/mailman/listinfo/python-list
Re: Relative import problem
On Apr 19, 6:54 pm, "Jorgen Bodde" <[EMAIL PROTECTED]> wrote:
> Hi all,
>
> I want to structure my app so that I have two dirs like;
>
> obj/{object files}
>
> gui/{gui files}
>
> Here comes the catch. From the GUI dir, I would like to access the obj
> submodule path. I need to go one dir back.. I read there was something
> like from .. import x in python 2.5 so that I could access my obj dir
> from a lower level, but I have problems getting this to work.
>
> Example;
>
> In the main dir I have;
>
> main.py:
>
> import gui
>
> gui.gui.startme()
>
>
> In the obj dir I have;
>
> obj/obj.py
> -
>
> def start():
> print 'Started OBJ'
> -
>
> In the GUI dir I have
>
> gui/gui.py
> -
> from .. import obj
>
> def start():
> obj.obj.start()
> -
>
> This does not work. It gives me;
>
> D:\personal\src\GuitarPortfolio\tmp>python start.py
> Traceback (most recent call last):
> File "start.py", line 4, in
> gui.gui.start()
> AttributeError: 'module' object has no attribute 'gui'
>
> Am I shooting myself in the foot by trying to structure? Or are there
> better more elegant ways?
>
> Regards,
> - Jorgen
You need to add the path to where your files are located since they
are not in any of the "standard" path directories where Python looks.
To do this, you can add the following lines in your files:
import sys
sys.path.append(/path/to/obj)
sys.path.append(/path/to/gui)
import obj
import gui
#etc..
Hope this helps
--
http://mail.python.org/mailman/listinfo/python-list
Access to file dropped on .exe/.app icon
If i create an app using py2exe/py2app is there then a way on windows/ mac to get access to a file dragged and dropped on to the .exe/.app icon? Martin -- http://mail.python.org/mailman/listinfo/python-list
Re: unable to print Unicode characters in Python 3
Hmm this works for me,
it's a self compiled version:
~ $ python3
Python 3.0 (r30:67503, Dec 29 2008, 21:35:15)
[GCC 4.2.4 (Ubuntu 4.2.4-1ubuntu3)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> print("\u20ac")
€
>>> print ("\N{EURO SIGN}")
€
>>>
2009/1/26 jefm :
> What am I doing wrong ?
"\N{EURO SIGN}".encode("ISO-8859-15") ## could be something but I'm
pretty sure I'm totally wrong on this
--
http://soup.alt.delete.co.at
http://www.xing.com/profile/Martin_Marcher
http://www.linkedin.com/in/martinmarcher
You are not free to read this message,
by doing so, you have violated my licence
and are required to urinate publicly. Thank you.
Please avoid sending me Word or PowerPoint attachments.
See http://www.gnu.org/philosophy/no-word-attachments.html
--
http://mail.python.org/mailman/listinfo/python-list
Re: Where to host a (Python) project?
Hi, 2009/1/31 andrew cooke : > However, i am thinking I could really do with: > - a mailing list > - simple bug tracking > - subversion > and am wondering which is the best (free) provider for these (the code > is LGPL open source). I'd prefer a mailing list to something like > google groups (although I guess it may be possible to configure a > gateway) and I could open up my personal subversion server, but that > seems like a lot of work (not really that interested in moving to > something other than svn). Google Groups can be perfectly used as Mailings Lists - while I'm not a huge fan of them that isn't an argument from your feature list :) There's tigris.org, savannah (savannah.gnu.org, nongnu.org), launchpad. All of them are fine to some extent, you might want to read up on PyMotW about how Doug Hellmann decided where to host his stuff. hth Martin -- http://soup.alt.delete.co.at http://www.xing.com/profile/Martin_Marcher http://www.linkedin.com/in/martinmarcher You are not free to read this message, by doing so, you have violated my licence and are required to urinate publicly. Thank you. Please avoid sending me Word or PowerPoint attachments. See http://www.gnu.org/philosophy/no-word-attachments.html -- http://mail.python.org/mailman/listinfo/python-list
Re: Where to host a (Python) project?
2009/1/31 andrew cooke : > On Jan 31, 9:59 am, Martin wrote: >> There's tigris.org, savannah (savannah.gnu.org, nongnu.org), >> launchpad. All of them are fine to some extent, you might want to read >> up on PyMotW about how Doug Hellmann decided where to host his stuff. > > all i can find is that he is writing his own! > http://blog.doughellmann.com/search/label/codehosting > (his reqs are quite different to mine - I am looking for an external > provider because I do not want to host dynamic pages myself). Darn, my bad. I just remembered that something happened in terms of hosting on his site, I thought he had decided on this: http://blog.doughellmann.com/2008/12/moving-pymotw-to-public-repository.html (seems he didn't have any time...) anyway, tigris, nongnu and savannah are hopefully at least useable information :) /Martin -- http://soup.alt.delete.co.at http://www.xing.com/profile/Martin_Marcher http://www.linkedin.com/in/martinmarcher You are not free to read this message, by doing so, you have violated my licence and are required to urinate publicly. Thank you. Please avoid sending me Word or PowerPoint attachments. See http://www.gnu.org/philosophy/no-word-attachments.html -- http://mail.python.org/mailman/listinfo/python-list
Re: database wrapper ?
Hi, 2009/2/1 Stef Mientki : > Googling, I found SQLalchemy, > which looks quit good. sqlalchemy was always enough for my needs, I recently found elixir which is yet another wrapper around sqlalchemy. I haven't played too much with it but it seems there are a couple of nice things, that is elixir makes using sqlalchemy even easier. hth martin -- http://soup.alt.delete.co.at http://www.xing.com/profile/Martin_Marcher http://www.linkedin.com/in/martinmarcher You are not free to read this message, by doing so, you have violated my licence and are required to urinate publicly. Thank you. Please avoid sending me Word or PowerPoint attachments. See http://www.gnu.org/philosophy/no-word-attachments.html -- http://mail.python.org/mailman/listinfo/python-list
Re: [2.5.1] Comparing dates?
Hi,
2009/2/2 Gilles Ganault :
> Thanks guys. For those interested, here's how to perform the
> conversion from DD/MM/ to -MM-DD:
as suggested, the DBA should seriously think about defining the
correct type of the column here, for intermediate use and getting
stuff to work you could use a view and define some stored procedures
on it so that inserting properly works...
---snip plain psql---
test=# CREATE table date_test(
id serial primary key,
the_date timestamp with time zone,
stuff Text
);
NOTICE: CREATE TABLE will create implicit sequence "date_test_id_seq"
for serial column "date_test.id"
NOTICE: CREATE TABLE / PRIMARY KEY will create implicit index
"date_test_pkey" for table "date_test"
CREATE TABLE
test=# INSERT INTO date_test (the_date) VALUES ('20091231');
INSERT 0 1
test=# INSERT INTO date_test (the_date) VALUES ('20081231');
INSERT 0 1
test=# INSERT INTO date_test (the_date) VALUES ('20071231');
INSERT 0 1
test=# SELECT * from date_test;
id |the_date| stuff
++---
1 | 2009-12-31 00:00:00+01 |
2 | 2008-12-31 00:00:00+01 |
3 | 2007-12-31 00:00:00+01 |
(3 rows)
---snap plain psql---
---snip now in python---
from datetime import datetime
d = datetime(day=21, month=21, year=2008) # use a real datetime
without string fiddling
import psycopg2
db = psycopg2.connect(host='localhost', user='test', password='test',
database='test')
cursor = db.cursor()
cursor.execute("select * from date_test where the_date < '20080221'")
# OK simple SELECT
for row in cursor:
print row
# (3, datetime.datetime(2007, 12, 31, 0, 0,
tzinfo=), None)
## kill SQL injection stuff, also personally I find this more
convenient that fiddling with strings...
cursor.execute("select * from date_test where the_date < %s", (d, ))
for row in cursor:
print row
(3, datetime.datetime(2007, 12, 31, 0, 0,
tzinfo=), None)
---snap now in python---
--
http://soup.alt.delete.co.at
http://www.xing.com/profile/Martin_Marcher
http://www.linkedin.com/in/martinmarcher
You are not free to read this message,
by doing so, you have violated my licence
and are required to urinate publicly. Thank you.
Please avoid sending me Word or PowerPoint attachments.
See http://www.gnu.org/philosophy/no-word-attachments.html
--
http://mail.python.org/mailman/listinfo/python-list
Re: Putting asterisks around text
Hi, 2009/2/9 [email protected] : > I'm trying to write a program that puts asterisks around the input text > using while loop. > I can do this without using while loop, but how can you do that using while > loop? > > Example: > > Enter a string: Hello world > > ** > *Hello world* > ** Since others have posted so helpful answer I'm just going to rant: 1) http://docs.python.org/ <- first part (make sure you read the docs -- and tutorial -- for the python version you installed 2) After you know how to store user input in a variable think about what other facts you know (by examining the content of the variable) 2) Think again hard what you need to output and which parts of the output need to match the information you already have (by doing point 2) That shouldn't be too hard hth Martin -- http://soup.alt.delete.co.at http://www.xing.com/profile/Martin_Marcher http://www.linkedin.com/in/martinmarcher You are not free to read this message, by doing so, you have violated my licence and are required to urinate publicly. Thank you. Please avoid sending me Word or PowerPoint attachments. See http://www.gnu.org/philosophy/no-word-attachments.html -- http://mail.python.org/mailman/listinfo/python-list
Re: how to find out vesion of a python module
Hi, 2009/2/10 Atishay : > Now the point is, how do I avoid setting LD_LIBRARY_PATH everytime for > python? I know I can write a bash script, but I am sure python would > have some feature that I can tweak to make it work. On debian you can modify * /etc/ld.so.conf and/or * /etc/ld.so.conf.d/ to include the path you need. hth martin -- http://soup.alt.delete.co.at http://www.xing.com/profile/Martin_Marcher http://www.linkedin.com/in/martinmarcher You are not free to read this message, by doing so, you have violated my licence and are required to urinate publicly. Thank you. Please avoid sending me Word or PowerPoint attachments. See http://www.gnu.org/philosophy/no-word-attachments.html -- http://mail.python.org/mailman/listinfo/python-list
Re: Could you recommend job schedulling solution?
Hi, 2009/2/11 redbaron : > should accept network > connection with new tasks without blocking of client and put it on job > queue. > > What is "task" ? Executing just ordinary python function will be > enough. If solution contain some client library which allow easy task > submit it will be great. I think parallel python will take of that for you (http://www.parallelpython.com/) -- http://soup.alt.delete.co.at http://www.xing.com/profile/Martin_Marcher http://www.linkedin.com/in/martinmarcher You are not free to read this message, by doing so, you have violated my licence and are required to urinate publicly. Thank you. Please avoid sending me Word or PowerPoint attachments. See http://www.gnu.org/philosophy/no-word-attachments.html -- http://mail.python.org/mailman/listinfo/python-list
Re: Match items in large list
Hi, 2009/2/12 Paul Rubin : > Fisherking writes: >> Which are the best way of searching through the list and extract the >> items that are the same. hmmm how about using sqlites in memory database and let SQL take care of finding that for you? hth Martin -- http://soup.alt.delete.co.at http://www.xing.com/profile/Martin_Marcher http://www.linkedin.com/in/martinmarcher You are not free to read this message, by doing so, you have violated my licence and are required to urinate publicly. Thank you. Please avoid sending me Word or PowerPoint attachments. See http://www.gnu.org/philosophy/no-word-attachments.html -- http://mail.python.org/mailman/listinfo/python-list
Re: A little bit else I would like to discuss
Hi, at first I wanted to file this under meta-discussions, but your lost paragraph got me thinking... 2009/2/12 Christian Heimes : > Nobody is going to stop you from creating a large bundle of useful > extensions as long as you follow the licenses. In fact lots of people > may appreciate a bundle. But the Python core package will always stay > small and agile. How does "small and agile" work with "batteries included"? >From my point of view: agile:: Would describe faster extension of the standard lib (rrd, yaml should IMHO already be in the standard lib). I'm pretty sure other people want to see other modules, but that's what agile + standard lib would mean for me. (Also I'm sometimes confused by the naming of modules but that's a different story) small:: just the opposite of "batteries included" My absolute favorite would be * python as just python (no standard lib) * a (rather) fast moving standard lib available as an addon download * possibly for each version I guess that would mean *a lot* maintenance overhead so that will probably never. Finally: Ignore me, Python is still the language that let's me get stuff done in the fastest and most enjoyable way. Thanks to GvR for the initial idea and all the volounteers for making it even better :) -- http://soup.alt.delete.co.at http://www.xing.com/profile/Martin_Marcher http://www.linkedin.com/in/martinmarcher You are not free to read this message, by doing so, you have violated my licence and are required to urinate publicly. Thank you. Please avoid sending me Word or PowerPoint attachments. See http://www.gnu.org/philosophy/no-word-attachments.html -- http://mail.python.org/mailman/listinfo/python-list
Re: A little bit else I would like to discuss
Oh yeah and ignore my typos also :) 2009/2/12 Martin : > Hi, > > at first I wanted to file this under meta-discussions, but your lost > paragraph got me thinking... > > 2009/2/12 Christian Heimes : >> Nobody is going to stop you from creating a large bundle of useful >> extensions as long as you follow the licenses. In fact lots of people >> may appreciate a bundle. But the Python core package will always stay >> small and agile. > > How does "small and agile" work with "batteries included"? > > From my point of view: > > agile:: > Would describe faster extension of the standard lib (rrd, yaml should > IMHO already be in the standard lib). I'm pretty sure other people > want to see other modules, but that's what agile + standard lib would > mean for me. (Also I'm sometimes confused by the naming of modules but > that's a different story) > > small:: > just the opposite of "batteries included" > > My absolute favorite would be > > * python as just python (no standard lib) > * a (rather) fast moving standard lib available as an addon download > * possibly for each version > > I guess that would mean *a lot* maintenance overhead so that will > probably never. > > Finally: Ignore me, Python is still the language that let's me get > stuff done in the fastest and most enjoyable way. Thanks to GvR for > the initial idea and all the volounteers for making it even better :) > > > -- > http://soup.alt.delete.co.at > http://www.xing.com/profile/Martin_Marcher > http://www.linkedin.com/in/martinmarcher > > You are not free to read this message, > by doing so, you have violated my licence > and are required to urinate publicly. Thank you. > > Please avoid sending me Word or PowerPoint attachments. > See http://www.gnu.org/philosophy/no-word-attachments.html > -- http://soup.alt.delete.co.at http://www.xing.com/profile/Martin_Marcher http://www.linkedin.com/in/martinmarcher You are not free to read this message, by doing so, you have violated my licence and are required to urinate publicly. Thank you. Please avoid sending me Word or PowerPoint attachments. See http://www.gnu.org/philosophy/no-word-attachments.html -- http://mail.python.org/mailman/listinfo/python-list
Re: how many databases exists?
Hi, you want to check the postgres documentation for that :) 2009/2/12 Rosell Pupo Polanco : > hello, i am working in pythoncard and i need to know how many databases > exist in my server POstgresql, exists some funcion in python that can help > me?? http://www.postgresql.org/docs/current/static/catalog-pg-database.html hth Martin -- http://soup.alt.delete.co.at http://www.xing.com/profile/Martin_Marcher http://www.linkedin.com/in/martinmarcher You are not free to read this message, by doing so, you have violated my licence and are required to urinate publicly. Thank you. Please avoid sending me Word or PowerPoint attachments. See http://www.gnu.org/philosophy/no-word-attachments.html -- http://mail.python.org/mailman/listinfo/python-list
Re: A little bit else I would like to discuss
2009/2/12 Christian Heimes : > Martin wrote: > [typos igored as requested ;)] > >> How does "small and agile" work with "batteries included"? > > The Python slogan says "batteries included", not "fusion reactor included". I'd be fine with a fusion reactor, my objections would be if skynet was included :) > The rules are: > > ... > > It takes at least 1.5 years to get a new feature into an extension and > at least 3 years to remove or change a feature. That's a major shop > stopper for every fast moving piece of Python software. True, I thought a bit more about it and decided for myself to be happy with the way it is since I prefer quality to features :) /Martin -- http://soup.alt.delete.co.at http://www.xing.com/profile/Martin_Marcher http://www.linkedin.com/in/martinmarcher You are not free to read this message, by doing so, you have violated my licence and are required to urinate publicly. Thank you. Please avoid sending me Word or PowerPoint attachments. See http://www.gnu.org/philosophy/no-word-attachments.html -- http://mail.python.org/mailman/listinfo/python-list
IMAP: How to implement GMail-like threaded conversations view
Currently I am trying to get used to Python's imaplib and email modules. I'like to create a webmail client simmilar to GMail. My Questions: a) Is there any feature hidden in Python's built-in modules (imaplib, email) that already can group all my mails into threads? b) If not a... what would be the best way to implement this? I can think of two approaches: b.1) Use the "References:" field of the messages in order to find out which messages are related to each other. I tried a first implementation which works quite well but I don't know if there can occur situations where one message is related to two parents. Also I don't know what happens if someone is too lazy to type my address. He might click at "Reply", delete topic and old mail-text and compose a new mail. Theoretically his mail client would set the "References:" field accordingly never the less, wouldn't it? Therefore my mail client would consider that completely new mail as part of an older conversation. The thoughts above might lead to the second approach: b.2) Use the "Subject:" field of the messages. I also tried this implementation and it also works (at first glance). I stripped all subjects of all mails so that all those "Re:", "Fw:" tags at the beginning get deleted. Afterwards I grouped those having the same subject and the same participants. Problem: I have no clue what "Re:"-tags might exist around the world. I guess each mail client and each language uses different ones, right? c) Does anyone know good resources to gain more knowledge about imap / mailing? Currently I am using those sites as a reference: http://www.devshed.com/c/a/Python/Python-Email-Libraries-part-2-IMAP/ (as a start *g) http://tools.ietf.org/html/rfc3501 http://tools.ietf.org/html/rfc2822 http://docs.python.org/library/imaplib.html http://docs.python.org/library/email.html Maybe there are other sources of interest on the web? :) This is my first post in this newsgroup. So: "Hello everybody!" :-) I've been reading this group for quite a while and I am really astonished how fast people give valuable answers here. This is a really great community! Many thanks in advance for all ideas! Greetz, Martin -- http://mail.python.org/mailman/listinfo/python-list
Re: Most efficient way to build very large dictionaries
Hi, 2008/12/24 : > Hi Roger, > >> you may want to consider using SQLite > > Thank you for your suggestion about looking at SQLite. I haven't > compared the performance of SQLite to Python dictionaries, but I'm > skeptical that SQLite would be faster than in-memory Python dictionaries > for the type of analysis I'm doing. I'd think he's talking about in memory SQLite Databases, this way you should be quite fast _and_ could dump all that to a persistent storage... regards martin -- http://soup.alt.delete.co.at http://www.xing.com/profile/Martin_Marcher http://www.linkedin.com/in/martinmarcher You are not free to read this message, by doing so, you have violated my licence and are required to urinate publicly. Thank you. Please avoid sending me Word or PowerPoint attachments. See http://www.gnu.org/philosophy/no-word-attachments.html -- http://mail.python.org/mailman/listinfo/python-list
Re: sys.stdout.write()'s bug or doc bug?
Sorry GMAIL got me again, I sent this in private first, apologies.
hi,
2008/12/26 Qiangning Hong :
>>>> sys.stdout.write(u)
> Traceback (most recent call last):
> File "", line 1, in
> UnicodeEncodeError: 'ascii' codec can't encode character u'\u554a' in
> position 0: ordinal not in range(128)
>>>> type(sys.stdout)
>
>>>> sys.stdout.encoding
> 'UTF-8'
Python 2.4.4 (#2, Oct 22 2008, 19:52:44)
[GCC 4.1.2 20061115 (prerelease) (Debian 4.1.1-21)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> u = u"\u554a"
>>> print u
啊
>>> sys.stdout.write(u + "\n")
Traceback (most recent call last):
File "", line 1, in ?
UnicodeEncodeError: 'ascii' codec can't encode character u'\u554a' in
position 0: ordinal not in range(128)
>>> # you are trying to write unicode, you need to encode it to something that
>>> suits your needs
>>> sys.stdout.write(u.encode("UTF-8") + "\n")
啊
>>> # now go and write a hundred times "Unicode is not an encoding" :)
> So, my question is, as sys.stdout IS a file object, why it does not
> use its encoding attribute to convert the given unicode? An
> implementation bug? A documenation bug?
hmm I always thought "sys.stdout" is a "file-like object" not that it IS a file.
/martin
--
http://soup.alt.delete.co.at
http://www.xing.com/profile/Martin_Marcher
http://www.linkedin.com/in/martinmarcher
You are not free to read this message,
by doing so, you have violated my licence
and are required to urinate publicly. Thank you.
Please avoid sending me Word or PowerPoint attachments.
See http://www.gnu.org/philosophy/no-word-attachments.html
--
http://mail.python.org/mailman/listinfo/python-list
Re: [SQL] Right way to set a variable to NULL?
Hi,
I'd create a simple wrapper object
class MailAddress(object):
def __init__(self, address=None):
self.address = address
def __str__(self):
if address:
return self.adress
return "NULL"
you can keep most of your code just replace the original instantiation
of the mail str-instance with the MailAddress
This is just scratched up quickly. Hope it helps
Martin
2008/12/26 Gilles Ganault :
>sql = 'INSERT INTO mytable (name,address,web,mail) VALUES
> ("%s","%s","%s","%s","%s")' % (name,address,web,mail)
> ===
>
> Is there a better way in Python to have a variable match NULL than
> building the SQL query step by step?
>
> Thank you.
> --
> http://mail.python.org/mailman/listinfo/python-list
>
--
http://soup.alt.delete.co.at
http://www.xing.com/profile/Martin_Marcher
http://www.linkedin.com/in/martinmarcher
You are not free to read this message,
by doing so, you have violated my licence
and are required to urinate publicly. Thank you.
Please avoid sending me Word or PowerPoint attachments.
See http://www.gnu.org/philosophy/no-word-attachments.html
--
http://mail.python.org/mailman/listinfo/python-list
Re: Right way to set a variable to NULL?
2008/12/26 John Machin : > The above all have the same characteristic: if the input is a zero- > length string, then NULL is inserted into the database instead of a > zero-length string. Some folks (not just pedants!) regard that as an > important difference. agreed but I understood the OP specifically wanted NULL and not ''. of course for data gathering in web apps I'd personally make the mail attribute a NOT NULL and reject anybody who wouldn't give me their mail address. After all mail is the way to get in touch with my customers/user if they register on my site, and if it's only for a password reset link. /martin -- http://soup.alt.delete.co.at http://www.xing.com/profile/Martin_Marcher http://www.linkedin.com/in/martinmarcher You are not free to read this message, by doing so, you have violated my licence and are required to urinate publicly. Thank you. Please avoid sending me Word or PowerPoint attachments. See http://www.gnu.org/philosophy/no-word-attachments.html -- http://mail.python.org/mailman/listinfo/python-list
Re: multiply each element of a list by a number
you might also want to look into the map(elem), and filter(elem) builtins >>> def multby3(elem): ... return elem * 3 ... >>> map(multby3, (1, 2, 3, )) [3, 6, 9] >>> help(map) >>> def even(elem): ... return not elem % 2 ... >>> filter(even, (1, 2, 3, )) (2,) >>> help(filter) KeyboardInterrupt >>> map(multby3, filter(even, (1, 2, 3, ))) [6] >>> hth martin 2008/12/27 Scott David Daniels : > Tim Chase wrote: >>> >>> What does *not* work is 3 * [0,1,2] >>> As you know, this gives >>>[0,1,2,0,1,2,0,1,2] >>> What I am hoping for is >>>[0,3,6] -- http://soup.alt.delete.co.at http://www.xing.com/profile/Martin_Marcher http://www.linkedin.com/in/martinmarcher You are not free to read this message, by doing so, you have violated my licence and are required to urinate publicly. Thank you. Please avoid sending me Word or PowerPoint attachments. See http://www.gnu.org/philosophy/no-word-attachments.html -- http://mail.python.org/mailman/listinfo/python-list
[2.4.4] creating a datetime.datetime from an XML xs:dateTime
Hi, subject says it all. I'd like to create a python datetime.datetime from an xs:string[1] I know about time.strptime and could get to a datetime.datetime from there, but that would be error prone like: * are there time zones? * how is the year represented * etc, yadda yadda so I'd rather like to find some method (hopefully within the standard lib) that had more peer review than only me :). thanks martin [1] http://www.w3.org/TR/xmlschema-2/#dateTime -- http://soup.alt.delete.co.at http://www.xing.com/profile/Martin_Marcher http://www.linkedin.com/in/martinmarcher You are not free to read this message, by doing so, you have violated my licence and are required to urinate publicly. Thank you. Please avoid sending me Word or PowerPoint attachments. See http://www.gnu.org/philosophy/no-word-attachments.html -- http://mail.python.org/mailman/listinfo/python-list
Re: [2.4.4] creating a datetime.datetime from an XML xs:dateTime
Hi, 2008/12/27 : > Not in the standard lib, but you might want to check out the dateutil > package. Its dateutil.parser module does an excellent job parsing a wide > range of time formats. It's a bit weak in the timezone area though. is that http://labix.org/python-dateutil, I'll have a look at it. -- http://soup.alt.delete.co.at http://www.xing.com/profile/Martin_Marcher http://www.linkedin.com/in/martinmarcher You are not free to read this message, by doing so, you have violated my licence and are required to urinate publicly. Thank you. Please avoid sending me Word or PowerPoint attachments. See http://www.gnu.org/philosophy/no-word-attachments.html -- http://mail.python.org/mailman/listinfo/python-list
[OT] game engine (as in rules not graphics)
Hello, I'd like to get in touch with game development a bit. I'm not talking about graphics but rather the game rules itself. Something like http://en.wikipedia.org/wiki/Monopoly_(game)#Rules, is there even a general approach to that or should I just go sketch up my rules and try to implement them. Being totally new to this topic I don't quite now what to search for to get some decent results that let me make a mental link between game rules and what the best practices are to implement them in python (or any other programming language) thanks, martin -- http://soup.alt.delete.co.at http://www.xing.com/profile/Martin_Marcher http://www.linkedin.com/in/martinmarcher You are not free to read this message, by doing so, you have violated my licence and are required to urinate publicly. Thank you. Please avoid sending me Word or PowerPoint attachments. See http://www.gnu.org/philosophy/no-word-attachments.html -- http://mail.python.org/mailman/listinfo/python-list
Re: Is there a function to remove escape characters from a string ?
2008/12/27 Stef Mientki : > Steven D'Aprano wrote: >> No, that only makes it even more confusing. What does Moore's Law have to >> do with your willful ignorance about the existence of human languages other >> than English? >> > Nothing. > I even don't (want to) see what bits / bytes / escape sequences have to do > with modern programming techniques, > so I certainly don't see any relation between these and human languages. > > But the lack of Moore's law in software explains why we still need to > concern about bits and bytes ;-) http://www.joelonsoftware.com/articles/Unicode.html -- http://soup.alt.delete.co.at http://www.xing.com/profile/Martin_Marcher http://www.linkedin.com/in/martinmarcher You are not free to read this message, by doing so, you have violated my licence and are required to urinate publicly. Thank you. Please avoid sending me Word or PowerPoint attachments. See http://www.gnu.org/philosophy/no-word-attachments.html -- http://mail.python.org/mailman/listinfo/python-list
Re: game engine (as in rules not graphics)
Hi, 2008/12/29 Phil Runciman : > See: Chris Moss, Prolog++: The Power of Object-Oriented and Logic Programming > (ISBN 0201565072) > > This book is a pretty handy intro to an OO version Prolog produced by Logic > Programming Associates. > From: Aaron Brady [mailto:[email protected]] > Sent: Sunday, 28 December 2008 1:22 p.m. > Not my expertise but here are my $0.02. You are looking for ways to > represent rules: buying a house is legal in such and such situation, and the > formula for calculating its price is something. You want "predicates" such > as InJail, OwnedBy, Costs. > > Costs( New York Ave, 200 ) > InJail( player2 ) > OwnedBy( St. Charles Ave, player4 ) > LegalMove( rolldie ) > LegalMove( sellhouse ) I'm not sure I'm looking for prolog, i had an introductory course back at the university but it didn't exactly like it. I'm after some info how such rules would defined in python (specifically python althou logic programming is probably the more appropriate way). I guess I'm missing quite some basics in the design of such concepts, I'll head back to google to find some introductory stuff now :). regards, Martin -- http://soup.alt.delete.co.at http://www.xing.com/profile/Martin_Marcher http://www.linkedin.com/in/martinmarcher You are not free to read this message, by doing so, you have violated my licence and are required to urinate publicly. Thank you. Please avoid sending me Word or PowerPoint attachments. See http://www.gnu.org/philosophy/no-word-attachments.html -- http://mail.python.org/mailman/listinfo/python-list
Extract frame from wmv
Hi, I need to extract a frame from a wmv file and save it as a jpg. In fact I need to extract a frame from each one of a collection of several thousand wmv files, but that's beside the point. I've actually written a script that does exactly this using the pyglet module. But while it works fine on my local machine running Python 2.5, my host is only on 2.4.3. They've installed pyglet for me, but it won't run without ctype, and they don't suport it. Can anyone suggest an alternative way to achieve this using a module that will run on a standard 2.4.3 installation? Thanks, Martin -- http://mail.python.org/mailman/listinfo/python-list
Re: Extract frame from wmv
Tim Golden wrote: Martin wrote: I need to extract a frame from a wmv file and save it as a jpg. In fact I need to extract a frame from each one of a collection of several thousand wmv files, but that's beside the point. I've actually written a script that does exactly this using the pyglet module. But while it works fine on my local machine running Python 2.5, my host is only on 2.4.3. They've installed pyglet for me, but it won't run without ctype, and they don't suport it. Can anyone suggest an alternative way to achieve this using a module that will run on a standard 2.4.3 installation? Pretty sure pymedia can do this kind of thing, possibly with a bit of work. http://pymedia.org/tut/src/dump_video.py.html I used it recentlyish to do the reverse: combine a series of still shots into a time-lapse movie. Alternatively, you might be able to commandline control mencoder or ffmpeg itself to do this. Not sure, but there must be *something* among those millions of command-line options! TJG Hi Tim, I've looked at Pymedia but I have to admit I couldn't work it out. Commandline might be good, but I'm really hoping someone can point me in the right direction, as this is not my area of expertise. Thanks, Martin -- http://mail.python.org/mailman/listinfo/python-list
Re: Extract frame from wmv
Tim Golden wrote: Martin wrote: I've looked at Pymedia but I have to admit I couldn't work it out. Commandline might be good, but I'm really hoping someone can point me in the right direction, as this is not my area of expertise. Nor mine :) Just so people can help you out, can you be more precise in your requirement? Do you want just the first frame (eg to act as an img placeholder for an embedded video). Or do you want to extract one or more arbitrary frames, eg for screen cap purposes? This page: http://stream0.org/2008/02/howto-extract-images-from-a-vi.html looks helpful from the ffmpeg pov, but I assume you're as capable of using Google as I am. I had a very quick go with the copy of pymedia I have installed here, but it couldn't read a (fairly recent) .wmv file so that might be a problem in any case. UPDATE: Just tried: ffmpeg -i bacardi.wmv -r 1 -f image2 images%05d.png from that page and it produced useful stuff. Worth a try? TJG Thanks again Tim, I'm just extracting the first frame to use as a thumbnail. I will try ffmpeg. Thanks for pointer. I didn't really know where to start looking. Martin -- http://mail.python.org/mailman/listinfo/python-list
Re: Extract frame from wmv
Lawrence D'Oliveiro wrote: In message <[EMAIL PROTECTED]>, Tim Golden wrote: Alternatively, you might be able to commandline control mencoder or ffmpeg itself to do this. Not sure, but there must be *something* among those millions of command-line options! FFmpeg--yes! Unfortunately it looks like I have the same problem as I do with pyglet - it's not support by my host. It looks like I might have to process the files locally and then upload them. Painful. Unless anyone else has any suggestions? Martin -- http://mail.python.org/mailman/listinfo/python-list
Re: Extract frame from wmv
Tim Golden wrote: Martin wrote: Lawrence D'Oliveiro wrote: In message <[EMAIL PROTECTED]>, Tim Golden wrote: Alternatively, you might be able to commandline control mencoder or ffmpeg itself to do this. Not sure, but there must be *something* among those millions of command-line options! FFmpeg--yes! Unfortunately it looks like I have the same problem as I do with pyglet - it's not support by my host. It looks like I might have to process the files locally and then upload them. Painful. Unless anyone else has any suggestions? Well, since as far as I can see your host won't let you install anything which isn't there already, you're down to implementing a Python-only module to read a frame from a .wmv. Which is, I would suggest, a not inconsiderable task. Perhaps pre-filtering is the best solution after all. TJG Thanks Lawrence and Tim, The irony is that I already have a Python only solution - it just requires 2.5! I've actually worked out a way to extract and compress just the thumbnails from my local server and upload them without having to re-upload the video files (which took days) so I'm happy. Thanks for the suggestions, Martin -- http://mail.python.org/mailman/listinfo/python-list
Re: binary file compare...
Hi,
On Mon, Apr 13, 2009 at 10:03 PM, Grant Edwards wrote:
> On 2009-04-13, SpreadTooThin wrote:
>
>> I want to compare two binary files and see if they are the same.
>> I see the filecmp.cmp function but I don't get a warm fuzzy feeling
>> that it is doing a byte by byte comparison of two files to see if they
>> are they same.
>
> Perhaps I'm being dim, but how else are you going to decide if
> two files are the same unless you compare the bytes in the
> files?
I'd say checksums, just about every download relies on checksums to
verify you do have indeed the same file.
>
> You could hash them and compare the hashes, but that's a lot
> more work than just comparing the two byte streams.
hashing is not exactly much mork in it's simplest form it's 2 lines per file.
$ dd if=/dev/urandom of=testfile.data bs=1M count=5
5+0 records in
5+0 records out
5242880 bytes (5.2 MB) copied, 1.4491 s, 3.6 MB/s
$ dd if=/dev/urandom of=testfile2.data bs=1M count=5
5+0 records in
5+0 records out
5242880 bytes (5.2 MB) copied, 1.92479 s, 2.7 MB/s
$ cp testfile.data testfile3.data
$ python
Python 2.5.4 (r254:67916, Feb 17 2009, 20:16:45)
[GCC 4.3.3] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import hashlib
>>> sha = hashlib.sha256()
>>> sha.update(file("testfile.data").read())
>>> sha.hexdigest()
'a0a8b5d1fd7b8181e0131fff8fd6acce39917e4498c86704354221fd96815797'
>>> sha2=hashlib.sha256()
>>> sha2.update(file("testfile2.data").read())
>>> sha2.hexdigest()
'25597380f833f287e8dad936b15ddb616669102c38f54dbd60ce57998d99ad3b'
>>> sha3=hashlib.sha256()
>>> sha3.update(file("testfile3.data").read())
>>> sha3.hexdigest()
'a0a8b5d1fd7b8181e0131fff8fd6acce39917e4498c86704354221fd96815797'
>>> sha.hexdigest() == sha2.hexdigest()
False
>>> sha.hexdigest() == sha3.hexdigest()
True
>>> sha2.hexdigest() == sha3.hexdigest()
False
>>>
--
http://soup.alt.delete.co.at
http://www.xing.com/profile/Martin_Marcher
http://www.linkedin.com/in/martinmarcher
You are not free to read this message,
by doing so, you have violated my licence
and are required to urinate publicly. Thank you.
Please avoid sending me Word or PowerPoint attachments.
See http://www.gnu.org/philosophy/no-word-attachments.html
--
http://mail.python.org/mailman/listinfo/python-list
Re: Using Python after a few years of Ruby
Hi, On Tue, Apr 14, 2009 at 9:01 AM, wrote: > 2) Gems - I've seen a bit about Eggs, but they don't seem to have > anywhere near the official status gems do for Ruby. Are there any > "package management" things like this for Python, or do you usually > just grab the code you need as-is? On a side note: Do your friendly SysAdmin Neigboorhood a favor and either: a) Exactly specify which eggs (or gems for ruby) you need (and their version) b) Talk to them about wether you could get your eggs packaged In all cases talk to them ahead of time. (does that sound strange or what?) We have a policy where unpackaged and untesteted software isn't allowed. Unpackaged being no .debs available as we go with debian. There were quite often annoyances when we refused to "just install" stuff from gems or eggs, after quite some fighting (with the project leaders, and C*O mainly, not so much the devs) we managed to get more involved in the process of development and could place some requests. It paid of in being able to keep our service levels more easily, and only being a very minor glitch if the devs get some input beforehand and don't have to plug monitoring features in a product never designed to have that feature... regards, Martin -- http://soup.alt.delete.co.at http://www.xing.com/profile/Martin_Marcher http://www.linkedin.com/in/martinmarcher You are not free to read this message, by doing so, you have violated my licence and are required to urinate publicly. Thank you. Please avoid sending me Word or PowerPoint attachments. See http://www.gnu.org/philosophy/no-word-attachments.html -- http://mail.python.org/mailman/listinfo/python-list
Re: binary file compare...
On Wed, Apr 15, 2009 at 11:03 AM, Steven D'Aprano wrote: > The checksum does look at every byte in each file. Checksumming isn't a > way to avoid looking at each byte of the two files, it is a way of > mapping all the bytes to a single number. My understanding of the original question was a way to determine wether 2 files are equal or not. Creating a checksum of 1-n files and comparing those checksums IMHO is a valid way to do that. I know it's a (one way) mapping between a (possibly) longer byte sequence and another one, how does checksumming not take each byte in the original sequence into account. I'd still say rather burn CPU cycles than development hours (if I got the question right), if not then with binary files you will have to find some way of representing differences between the 2 files in a readable manner anyway. > Hashing is a *lot* more work than just comparing two bytes. The MD5 > checksum has been specifically designed to be fast and compact, and the > algorithm is still complicated: I know that the various checksum algorithms aren't exactly cheap, but I do think that just to know wether 2 files are different a solution which takes 5mins to implement wins against a lengthy discussion which optimizes too early wins hands down. regards, martin -- http://soup.alt.delete.co.at http://www.xing.com/profile/Martin_Marcher http://www.linkedin.com/in/martinmarcher You are not free to read this message, by doing so, you have violated my licence and are required to urinate publicly. Thank you. Please avoid sending me Word or PowerPoint attachments. See http://www.gnu.org/philosophy/no-word-attachments.html -- http://mail.python.org/mailman/listinfo/python-list
Re: print as a function in 2.5 ?
Hi, On Sun, Apr 19, 2009 at 7:13 PM, Stef Mientki wrote: > hello, > > For several reasons I still use Python version 2.5. > I understand that the print-statement will be replaced in Python version > 3.0. > > At the moment I want to extend the print statement with an optional > traceback. > So I've 2 options: > 1- make a new function, like "eprint ()", where "e" stands for extended > print > 2- make a function "print()" that has the extended features I usually have a tools module/package (depending on size of my project, mostly a module) that defines an "emit([object, ...][, sep=' '][, end='\n'][, file=sys.stdout])" -- this corresponds tohttp://docs.python.org/3.0/library/functions.html#print I found that to be the handiest solution, however I'm too much of an idiot to use it as a gobally available module right now :) (Yeah I know DRY) regards, Martin -- http://soup.alt.delete.co.at http://www.xing.com/profile/Martin_Marcher http://www.linkedin.com/in/martinmarcher You are not free to read this message, by doing so, you have violated my licence and are required to urinate publicly. Thank you. Please avoid sending me Word or PowerPoint attachments. See http://www.gnu.org/philosophy/no-word-attachments.html -- http://mail.python.org/mailman/listinfo/python-list
Q: finding distance between 2 time's
I made this little script (below) to look througt a dir to see if
there are any files newer than .e.g. 1 hour.
I have the loop through the dir working and can retreive file time as
well as present time.
both time variables are in the format returned by time.localtime()
My question:
How do I find the difference between such two time variables, to
calculate the 'age' of the file?
:-) Martin
--8<-- Code begin -
import os, time
def buildList(directory):
listing = os.listdir(directory)
for x in listing:
x = os.path.join(directory, x)
if os.path.isdir(x):
print ('dir -> %s') % x
if os.path.isfile(x):
tstF = time.localtime(os.path.getmtime(x))
nSize = os.path.getsize(x)
print ('fil -> %s %d @ %s') % (x, nSize, time.asctime
(tstF))
return 0
tstN = time.localtime()
print tstN
print "Time now: %s" % time.asctime(tstN)
buildList('C:\Martin\Work\Work_Eclipse\.metadata')
--
http://mail.python.org/mailman/listinfo/python-list
Re: Q: finding distance between 2 time's
Thanks both The first answer is quite instuctive, the other one might be the one I'll use in the code, it's nicely compact and clear. So 2x thanks. :-) Martin -- http://mail.python.org/mailman/listinfo/python-list
Re: Q: finding distance between 2 time's
On May 30, 4:10 pm, Steven D'Aprano wrote: > On Sat, 30 May 2009 12:06:55 +0200, jkv wrote: > > I added a few lines to your script, and now it ought to only print files > > newer than 3601 seconds (3600 seconds is one hour). > ... > > #if file newer than one hour print a line > > if time_difference < 3601: > > That's a potential off-by-one error. That may print files that are older > than one hour. Admittedly, they'll be off by less than one second, but if > you're going to write code, write correct code. The right test is: > > if time_difference <= 3600: > > (and you may even want to deal with files that have a *negative* time > difference, e.g. they were created apparently in the future). > > This is particularly necessary if you use time.time() to generate the > current time, since that returns fractions of a second. But even if you > don't, it's still the right thing to do: it's defensive programming. > Rather than assume that all file systems store timestamps accurate only > to a second, assume that some file system, somewhere, will be accurate to > fractions of a second, and code accordingly. > > That way, no matter what the file system does, your code will still do > the right thing, and (in this case) it doesn't even cost you anything. > > -- > Steven Hi Steve thanks for your comment. But I only need it to re-run some test scripts in case there seems to be hick-up in our live download of satellite images. It's not critical within seconds, not even within minutes... But in principel the potential error could of cause be important - in other context. :-) M -- http://mail.python.org/mailman/listinfo/python-list
Re: Q: finding distance between 2 time's
On May 30, 11:37 pm, jkv wrote: > [email protected] wrote: > > Thanks both > > > The first answer is quite instuctive, the other one might be the one > > I'll use in t > > I didn't receive the other answer, could you please forward it to me?> So 2x > thanks. > > You are welcome. > > I took another look at your code, and you can compress it all to a if > "oneliner": > (and thanks to Steven for the <= reminder) > > if os.path.isfile(x): > nSize = os.path.getsize(x) > #if oneliner > if time.mktime(time.localtime()) - > time.mktime(time.localtime(os.path.getmtime(x))) <= 3600: > print ('HT fil -> %s %d @ %s') % (x, nSize, time.asctime) I encurrage both Clarity and Brevity, but usually in that order. In other words, I don't mind to split a statement in two (or three) sub-sentenses, if it increases Clarity. As long as it dosn't so large that I loose the overview, and as long as it dosn't affect runtime performance. -- http://mail.python.org/mailman/listinfo/python-list
Re: Q: finding distance between 2 time's
> > http://en.wikipedia.org/wiki/Subtraction > Only one problem, wise arse: TypeError: unsupported operand type(s) for -: 'time.struct_time' and 'time.struct_time' Like I didn't try that... -- http://mail.python.org/mailman/listinfo/python-list
Re: python custom command interpreter?
I guess you are looking for this: http://docs.python.org/lib/module-cmd.html On Wed, Aug 20, 2008 at 9:19 PM, joey boggs <[EMAIL PROTECTED]> wrote: > I've got a hopefully simple question, maybe I'm just not searching for the > right information. > > I'm working on a project that is using kickstarts to build Fedora/CentOS > variant machines. Within the kickstart in the post section we will be > performing disk space reductions by removing any unnecessary directories on > the machine. > > In the end I'd like to be able to run a custom interpreter and just feed it > one command and a directory. The end result in the kickstart something like > this: > > %post --interpreter #!/usr/bin/myinterpreter > DROP /tmp/directory > DROP /tmp/directory2 > > > How would I setup the interpreter to take the DROP command? I've been > reading and searching all day but I haven't found anything close to what I'm > doing. I realize that using custom commands in this case is overkill but in > the end is used to make the users life easier. If anyone can point me to > some documentation I would be more than grateful. > > Thanks, > Joey > > > > -- > http://mail.python.org/mailman/listinfo/python-list > -- http://www.xing.com/profile/Martin_Marcher You are not free to read this message, by doing so, you have violated my licence and are required to urinate publicly. Thank you. -- http://mail.python.org/mailman/listinfo/python-list
Re: Are dictionaries the same as hashtables?
On Tue, Aug 26, 2008 at 9:52 AM, cnb <[EMAIL PROTECTED]> wrote:
> On Aug 26, 9:43 am, Martin Marcher <[EMAIL PROTECTED]> wrote:
>> On 2008-08-26 00:32:20, cnb wrote:
>>
>> > Are dictionaries the same as hashtables?
>>
>> Yes, but there is nothing in there that does sane collision handling
>> like making a list instead of simply overwriting.
>>
>> PS: your sig was *a bit* longer than you question. please don't do
>> that...
>>
>> signature.asc
>> < 1KViewDownload
>
> my what?
Hehe,
it was *my* sig... i was using some old box with a mut config that
still had the fortune script in it... sorry.
Anyway what I meant by collision handling was:
$ python
Python 2.5.2 (r252:60911, Jul 31 2008, 17:31:22)
[GCC 4.2.3 (Ubuntu 4.2.3-2ubuntu7)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> l = {}
>>> l
{}
>>> l["a"] = 12
>>> l["b"] = 13
>>> l
{'a': 12, 'b': 13}
>>> l["a"] = "This is a collision"
>>> l
{'a': 'This is a collision', 'b': 13}
>>>
As you see position "a" is simply overwritten. Probably "sane" doesn't
really apply because how could the python creators possibly know
whether I just want to overwrite the value or indeed want some form of
collision handling (talk about lists vs. trees with there
subforms...)...
If one would want that he/she/it could still subclass dict and do more magic.
/martin
--
http://www.xing.com/profile/Martin_Marcher
You are not free to read this message,
by doing so, you have violated my licence
and are required to urinate publicly. Thank you.
--
http://mail.python.org/mailman/listinfo/python-list
PIL : How to write array to image ???
Dear group
I'm trying to use PIL to write an array (a NumPy array to be exact) to
an image.
Peace of cake, but it comes out looking strange.
I use the below mini code, that I wrote for the purpose. The print of
a looks like expected:
[[ 200. 200. 200. ...,0.0.0.]
[ 200. 200. 200. ...,0.0.0.]
[ 200. 200. 200. ...,0.0.0.]
...,
[ 0.0.0. ..., 200. 200. 200.]
[ 0.0.0. ..., 200. 200. 200.]
[ 0.0.0. ..., 200. 200. 200.]]
But the image looks nothing like that.
Please see the images on:
http://hvidberg.net/Martin/temp/quat_col.png
http://hvidberg.net/Martin/temp/quat_bw.png
or run the code to see them locally.
Please – what do I do wrong in the PIL part ???
:-? Martin
import numpy as np
from PIL import Image
from PIL import ImageOps
maxcol = 100
maxrow = 100
a = np.zeros((maxcol,maxrow),float)
for i in range(maxcol):
for j in range(maxrow):
if (i<(maxcol/2) and j<(maxrow/2)) or (i>=(maxcol/2) and j>=
(maxrow/2)):
a[i,j] = 200
else:
a[i,j] = 0
print a
pilImage = Image.fromarray(a,'RGB')
pilImage.save('quat_col.png')
pilImage = ImageOps.grayscale(pilImage)
pilImage.save('quat_bw.png')
--
http://mail.python.org/mailman/listinfo/python-list
Re: PIL : How to write array to image ???
On Oct 3, 11:56 pm, Peter Otten <[email protected]> wrote: > Martin wrote: > > Dear group > > > I'm trying to use PIL to write an array (a NumPy array to be exact) to > > an image. > > Peace of cake, but it comes out looking strange. > > > I use the below mini code, that I wrote for the purpose. The print of > > a looks like expected: > > > [[ 200. 200. 200. ..., 0. 0. 0.] > > [ 200. 200. 200. ..., 0. 0. 0.] > > [ 200. 200. 200. ..., 0. 0. 0.] > > ..., > > [ 0. 0. 0. ..., 200. 200. 200.] > > [ 0. 0. 0. ..., 200. 200. 200.] > > [ 0. 0. 0. ..., 200. 200. 200.]] > > > But the image looks nothing like that. > > > Please see the images on: > >http://hvidberg.net/Martin/temp/quat_col.png > >http://hvidberg.net/Martin/temp/quat_bw.png > > > or run the code to see them locally. > > > Please – what do I do wrong in the PIL part ??? > > > :-? Martin > > > import numpy as np > > from PIL import Image > > from PIL import ImageOps > > > maxcol = 100 > > maxrow = 100 > > > a = np.zeros((maxcol,maxrow),float) > > > for i in range(maxcol): > > for j in range(maxrow): > > if (i<(maxcol/2) and j<(maxrow/2)) or (i>=(maxcol/2) and j>= > > (maxrow/2)): > > a[i,j] = 200 > > else: > > a[i,j] = 0 > > > print a > > > pilImage = Image.fromarray(a,'RGB') > > pilImage.save('quat_col.png') > > pilImage = ImageOps.grayscale(pilImage) > > pilImage.save('quat_bw.png') > > The PIL seems to copy the array contents directly from memory without any > conversions or sanity check. In your example The float values determine the > gray value of 8 consecutive pixels. > > If you want a[i,j] to become the color of the pixel (i, j) you have to use > an array with a memory layout that is compatible to the Image. > Here are a few examples: > > >>> import numpy > >>> from PIL import Image > >>> a = numpy.zeros((100, 100), numpy.uint8) > >>> a[:50, :50] = a[50:, 50:] = 255 > >>> Image.fromarray(a).save("tmp1.png") > >>> b = numpy.zeros((100, 100, 3), numpy.uint8) > >>> b[:50, :50, :] = b[50:, 50:, :] = [255, 0, 0] > >>> Image.fromarray(b).save("tmp2.png") > >>> c = numpy.zeros((100, 100), numpy.uint32) > >>> c[:50, :50] = c[50:, 50:] = 0xff808000 > >>> Image.fromarray(c, "RGBA").save("tmp3.png") > > Peter Thanks All - That helped a lot... The working code ended with: imga = np.zeros((imgL.shape[1],imgL.shape[0]),np.uint8) for ro in range(imgL.shape[1]): for co in range(imgL.shape[0]): imga[ro,co] = imgL[ro,co] Image.fromarray(imga).save('_a'+str(lev)+'.png') -- http://mail.python.org/mailman/listinfo/python-list
Re: PIL : How to write array to image ???
On Oct 4, 10:16 pm, "Mart." wrote: > On Oct 4, 9:47 am, Martin wrote: > > > > > On Oct 3, 11:56 pm, Peter Otten <[email protected]> wrote: > > > > Martin wrote: > > > > Dear group > > > > > I'm trying to use PIL to write an array (a NumPy array to be exact) to > > > > an image. > > > > Peace of cake, but it comes out looking strange. > > > > > I use the below mini code, that I wrote for the purpose. The print of > > > > a looks like expected: > > > > > [[ 200. 200. 200. ..., 0. 0. 0.] > > > > [ 200. 200. 200. ..., 0. 0. 0.] > > > > [ 200. 200. 200. ..., 0. 0. 0.] > > > > ..., > > > > [ 0. 0. 0. ..., 200. 200. 200.] > > > > [ 0. 0. 0. ..., 200. 200. 200.] > > > > [ 0. 0. 0. ..., 200. 200. 200.]] > > > > > But the image looks nothing like that. > > > > > Please see the images on: > > > >http://hvidberg.net/Martin/temp/quat_col.png > > > >http://hvidberg.net/Martin/temp/quat_bw.png > > > > > or run the code to see them locally. > > > > > Please – what do I do wrong in the PIL part ??? > > > > > :-? Martin > > > > > import numpy as np > > > > from PIL import Image > > > > from PIL import ImageOps > > > > > maxcol = 100 > > > > maxrow = 100 > > > > > a = np.zeros((maxcol,maxrow),float) > > > > > for i in range(maxcol): > > > > for j in range(maxrow): > > > > if (i<(maxcol/2) and j<(maxrow/2)) or (i>=(maxcol/2) and j>= > > > > (maxrow/2)): > > > > a[i,j] = 200 > > > > else: > > > > a[i,j] = 0 > > > > > print a > > > > > pilImage = Image.fromarray(a,'RGB') > > > > pilImage.save('quat_col.png') > > > > pilImage = ImageOps.grayscale(pilImage) > > > > pilImage.save('quat_bw.png') > > > > The PIL seems to copy the array contents directly from memory without any > > > conversions or sanity check. In your example The float values determine > > > the > > > gray value of 8 consecutive pixels. > > > > If you want a[i,j] to become the color of the pixel (i, j) you have to use > > > an array with a memory layout that is compatible to the Image. > > > Here are a few examples: > > > > >>> import numpy > > > >>> from PIL import Image > > > >>> a = numpy.zeros((100, 100), numpy.uint8) > > > >>> a[:50, :50] = a[50:, 50:] = 255 > > > >>> Image.fromarray(a).save("tmp1.png") > > > >>> b = numpy.zeros((100, 100, 3), numpy.uint8) > > > >>> b[:50, :50, :] = b[50:, 50:, :] = [255, 0, 0] > > > >>> Image.fromarray(b).save("tmp2.png") > > > >>> c = numpy.zeros((100, 100), numpy.uint32) > > > >>> c[:50, :50] = c[50:, 50:] = 0xff808000 > > > >>> Image.fromarray(c, "RGBA").save("tmp3.png") > > > > Peter > > > Thanks All - That helped a lot... > > > The working code ended with: > > > imga = np.zeros((imgL.shape[1],imgL.shape[0]),np.uint8) > > for ro in range(imgL.shape[1]): > > for co in range(imgL.shape[0]): > > imga[ro,co] = imgL[ro,co] > > Image.fromarray(imga).save('_a'+str(lev)+'.png') > > Without knowing how big your image is (can't remember if you said!). > Perhaps rather than looping in the way you might in C for example, the > numpy where might be quicker if you have a big image. Just a > thought... And a good thought too... I use to teach ansi C at Univ. many ears ago, and it's still one of my favorits. But I prefere Python for this type of work. Python have a much faster develop time, is much easier to write (fewer linees, simpler syntax) and is much more 'hot' in these days. Finally my favorit GIS and RS applications support Python out-of-the-box. :-) Martin -- http://mail.python.org/mailman/listinfo/python-list
Re: How to set the range for x-axis
On Nov 9, 8:45 pm, Robert Kern wrote: > On 2009-11-09 10:43 AM, Moses wrote: > > > > > Hi Chris, > > > I am using python 2.6 and am using scipy and pylab. See the code below. > > You will want to ask matplotlib questions on the matplotlib mailing list: > > https://lists.sourceforge.net/lists/listinfo/matplotlib-users > > -- > 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 import matplotlib.pyplot as plt plt.xlim(0., 1.) -- http://mail.python.org/mailman/listinfo/python-list
Re: Python as network protocol
On Tue, Nov 10, 2009 at 16:31, Daniel Fetchinson wrote: >> This is a *really* bad idea. > > How do you know for sure? Maybe the OP wants to use this thing with 3 > known researchers working on a cluster that is not even visible to the > outside world. In such a setup the model the OP suggested is a > perfectly reasonable one. I say this because I often work in such an > environment and security is never an issue for us. And I find it > always amusing that whenever I outline our code to a non-scientist > programmer they always run away in shock and never talk to us again :) > Nevertheless our code works perfectly for our purposes. It is a bad idea because that's exactly why we now have a spam problem. It _was_ a trusted environment once upon a time. Just check your spam messages to see why ignoring security can lead to really bad results. Do you know for sure that in say 3-5 years from now on your software isn't released into the wild and then has no security at all? regards, Martin -- http://www.xing.com/profile/Martin_Marcher http://www.linkedin.com/in/martinmarcher You are not free to read this message, by doing so, you have violated my licence and are required to urinate publicly. Thank you. Please avoid sending me Word or PowerPoint attachments. See http://www.gnu.org/philosophy/no-word-attachments.html -- http://mail.python.org/mailman/listinfo/python-list
Re: Replace various regex
On Feb 12, 7:57 pm, McColgst wrote: > On Feb 12, 2:39 pm, Martin wrote: > > > > > > > Hi, > > > I am trying to come up with a more generic scheme to match and replace > > a series of regex, which look something like this... > > > 19.01,16.38,0.79,1.26,1.00 ! canht_ft(1:npft) > > 5.0, 4.0, 2.0, 4.0, 1.0 ! lai(1:npft) > > > Ideally match the pattern to the right of the "!" sign (e.g. lai), I > > would then like to be able to replace one or all of the corresponding > > numbers on the line. So far I have a rather unsatisfactory solution, > > any suggestions would be appreciated... > > > The file read in is an ascii file. > > > f = open(fname, 'r') > > s = f.read() > > > if CANHT: > > s = re.sub(r"\d+.\d+,\d+.\d+,\d+.\d+,\d+.\d+,\d+.\d+ ! > > canht_ft", CANHT, s) > > > where CANHT might be > > > CANHT = '115.01,16.38,0.79,1.26,1.00 ! canht_ft' > > > But this involves me passing the entire string. > > > Thanks. > > > Martin > > If I understand correctly, there are a couple ways to do it. > One is to use .split() and split by the '!' sign, given that you wont > have more than one '!' on a line. This will return a list of the words > split by the delimiter, in this case being '!', so you should get back > (19.01,16.38,0.79,1.26,1.00 , canht_ft(1:npft) ) and you can do > whatever replace functions you want using the list. > > check out split:http://docs.python.org/library/stdtypes.html#str.split > > Another, is in your regular expression, you can match the first part > or second part of the string by specifying where the '!' is, > if you want to match the part after the '!' I would do something like > r"[^! cahnt_ft]", or something similar (i'm not particularly up-to- > date with my regex syntax, but I think you get the idea.) > > I hope I understood correctly, and I hope that helps. > > -sean Hi I like the second suggestion, so this wouldn't rely on me having to match the numbers only the string canht for example but still allow me to replace the whole line, is that what you mean? I tried it and the expression seemed to replace the entire file, so perhaps i am doing something wrong. But in principle I think that might be a better scheme than my current one. i tried if CANHT: #s = re.sub(r"\d+.\d+,\d+.\d+,\d+.\d+,\d+.\d+,\d+.\d+ ! canht_ft", CANHT, s) s = re.sub(r"[^! canht_ft]", CANHT, s) -- http://mail.python.org/mailman/listinfo/python-list
Re: Replace various regex
On Feb 12, 8:30 pm, MRAB wrote: > McColgst wrote: > > On Feb 12, 2:39 pm, Martin wrote: > >> Hi, > > >> I am trying to come up with a more generic scheme to match and replace > >> a series of regex, which look something like this... > > >> 19.01,16.38,0.79,1.26,1.00 ! canht_ft(1:npft) > >> 5.0, 4.0, 2.0, 4.0, 1.0 ! lai(1:npft) > > >> Ideally match the pattern to the right of the "!" sign (e.g. lai), I > >> would then like to be able to replace one or all of the corresponding > >> numbers on the line. So far I have a rather unsatisfactory solution, > >> any suggestions would be appreciated... > > >> The file read in is an ascii file. > > >> f = open(fname, 'r') > >> s = f.read() > > >> if CANHT: > >> s = re.sub(r"\d+.\d+,\d+.\d+,\d+.\d+,\d+.\d+,\d+.\d+ ! > >> canht_ft", CANHT, s) > > >> where CANHT might be > > >> CANHT = '115.01,16.38,0.79,1.26,1.00 ! canht_ft' > > >> But this involves me passing the entire string. > > >> Thanks. > > >> Martin > > > If I understand correctly, there are a couple ways to do it. > > One is to use .split() and split by the '!' sign, given that you wont > > have more than one '!' on a line. This will return a list of the words > > split by the delimiter, in this case being '!', so you should get back > > (19.01,16.38,0.79,1.26,1.00 , canht_ft(1:npft) ) and you can do > > whatever replace functions you want using the list. > > > check out split:http://docs.python.org/library/stdtypes.html#str.split > > The .split method is the best way if you process the file a line at a > time. The .split method, incidentally, accepts a maxcount argument so > that you can split a line no more than once. > > > Another, is in your regular expression, you can match the first part > > or second part of the string by specifying where the '!' is, > > if you want to match the part after the '!' I would do something like > > r"[^! cahnt_ft]", or something similar (i'm not particularly up-to- > > date with my regex syntax, but I think you get the idea.) > > The regex would be r"(?m)^[^!]*(!.*)" to capture the '!' and the rest of > the line. > > > > > I hope I understood correctly, and I hope that helps. I guess I could read the file a line at a time and try splitting it, though I though it would be better to read it all once then search for the various regex I need to match and replace? I am not sure that regex helps, as that would match and replace every line which had a "!". Perhaps if i explain more thoroughly? So the input file looks something like this... 9*0.0! canopy(1:ntiles) 12.100 ! cs 0.0 ! gs 9*50.0 ! rgrain(1:ntiles) 0.749, 0.743, 0.754, 0.759 ! stheta(1:sm_levels)(top to bottom) 9*0.46 ! snow_tile(1:ntiles) 0.46 ! snow_grnd 276.78,277.46,278.99,282.48 ! t_soil(1:sm_levels)(top to bottom) 9*276.78 ! tstar_tile(1:ntiles) 19.01,16.38,0.79,1.26,1.00 ! canht_ft(1:npft) 200.0, 4.0, 2.0, 4.0, 1.0 ! lai(1:npft) So for each of the strings following the "!" I may potentially want to match them and replace some of the numbers. That is I might search for the expression snow_grnd with the intention of substituting 0.46 for another number. What i came up with was a way to match all the numbers and pass the replacement string. -- http://mail.python.org/mailman/listinfo/python-list
Replace various regex
Hi, I am trying to come up with a more generic scheme to match and replace a series of regex, which look something like this... 19.01,16.38,0.79,1.26,1.00 ! canht_ft(1:npft) 5.0, 4.0, 2.0, 4.0, 1.0 ! lai(1:npft) Ideally match the pattern to the right of the "!" sign (e.g. lai), I would then like to be able to replace one or all of the corresponding numbers on the line. So far I have a rather unsatisfactory solution, any suggestions would be appreciated... The file read in is an ascii file. f = open(fname, 'r') s = f.read() if CANHT: s = re.sub(r"\d+.\d+,\d+.\d+,\d+.\d+,\d+.\d+,\d+.\d+ ! canht_ft", CANHT, s) where CANHT might be CANHT = '115.01,16.38,0.79,1.26,1.00 ! canht_ft' But this involves me passing the entire string. Thanks. Martin -- http://mail.python.org/mailman/listinfo/python-list
Re: Replace various regex
On Feb 15, 2:03 pm, Jean-Michel Pichavant
wrote:
> Martin wrote:
> > Hi,
>
> > I am trying to come up with a more generic scheme to match and replace
> > a series of regex, which look something like this...
>
> > 19.01,16.38,0.79,1.26,1.00 ! canht_ft(1:npft)
> > 5.0, 4.0, 2.0, 4.0, 1.0 ! lai(1:npft)
>
> > Ideally match the pattern to the right of the "!" sign (e.g. lai), I
> > would then like to be able to replace one or all of the corresponding
> > numbers on the line. So far I have a rather unsatisfactory solution,
> > any suggestions would be appreciated...
>
> > The file read in is an ascii file.
>
> > f = open(fname, 'r')
> > s = f.read()
>
> > if CANHT:
> > s = re.sub(r"\d+.\d+,\d+.\d+,\d+.\d+,\d+.\d+,\d+.\d+ !
> > canht_ft", CANHT, s)
>
> > where CANHT might be
>
> > CANHT = '115.01,16.38,0.79,1.26,1.00 ! canht_ft'
>
> > But this involves me passing the entire string.
>
> > Thanks.
>
> > Martin
>
> I remove all lines containing things like 9*0.0 in your file, cause I
> don't know what they mean and how to handle them. These are not numbers.
>
> import re
>
> replace = {
> 'snow_grnd' : (1, '99.99,'), # replace the 1st number by 99.99
> 't_soil' : (2, '88.8,'), # replace the 2nd number by 88.88
> }
>
> testBuffer = """
> 0.749, 0.743, 0.754, 0.759 ! stheta(1:sm_levels)(top to bottom)
> 0.46 ! snow_grnd
> 276.78,277.46,278.99,282.48 ! t_soil(1:sm_levels)(top to bottom)
> 19.01,16.38,0.79,1.26,1.00 ! canht_ft(1:npft)
> 200.0, 4.0, 2.0, 4.0, 1.0 ! lai(1:npft)
> """
>
> outputBuffer = ''
> for line in testBuffer.split('\n'):
> for key, (index, repl) in replace.items():
> if key in line:
> parameters = {
> 'n' : '[\d\.]+', # given you example you have to change
> this one, I don't know what means 9*0.0 in your file
> 'index' : index - 1,
> }
> # the following pattern will silently match any digit before
> the th digit is found, and use a capturing parenthesis for the last
> pattern =
> '(\s*(?:(?:%(n)s)[,\s]+){0,%(index)s})(?:(%(n)s)[,\s]+)(.*!.*)' %
> parameters # regexp are sometimes a nightmare to read
> line = re.sub(pattern, r'\1 '+repl+r'\3' , line)
> break
> outputBuffer += line +'\n'
>
> print outputBuffer
Thanks I will take a look. I think perhaps I was having a very slow
day when I posted and realised I could solve the original problem more
efficiently and the problem wasn't perhaps as I first perceived. It is
enough to match the tag to the right of the "!" sign and use this to
adjust what lies on the left of the "!" sign. Currently I have
this...if anyone thinks there is a neater solution I am happy to hear
it. Many thanks.
variable_tag = 'lai'
variable = [200.0, 60.030, 0.060, 0.030, 0.030]
# generate adjustment string
variable = ",".join(["%s" % i for i in variable]) + ' ! ' +
variable_tag
# call func to adjust input file
adjustStandardPftParams(variable, variable_tag, in_param_fname,
out_param_fname)
and the inside of this func looks like this
def adjustStandardPftParams(self, variable, variable_tag, in_fname,
out_fname):
f = open(in_fname, 'r')
of = open(out_fname, 'w')
pattern_found = False
while True:
line = f.readline()
if not line:
break
pattern = re.findall(r"!\s+"+variable_tag, line)
if pattern:
print 'yes'
print >> of, "%s" % variable
pattern_found = True
if pattern_found:
pattern_found = False
else:
of.write(line)
f.close()
of.close()
return
--
http://mail.python.org/mailman/listinfo/python-list
Re: Replace various regex
On Feb 15, 2:27 pm, Jean-Michel Pichavant
wrote:
> Martin wrote:
> > On Feb 15, 2:03 pm, Jean-Michel Pichavant
> > wrote:
>
> >> Martin wrote:
>
> >>> Hi,
>
> >>> I am trying to come up with a more generic scheme to match and replace
> >>> a series ofregex, which look something like this...
>
> >>> 19.01,16.38,0.79,1.26,1.00 ! canht_ft(1:npft)
> >>> 5.0, 4.0, 2.0, 4.0, 1.0 ! lai(1:npft)
>
> >>> Ideally match the pattern to the right of the "!" sign (e.g. lai), I
> >>> would then like to be able to replace one or all of the corresponding
> >>> numbers on the line. So far I have a rather unsatisfactory solution,
> >>> any suggestions would be appreciated...
>
> >>> The file read in is an ascii file.
>
> >>> f = open(fname, 'r')
> >>> s = f.read()
>
> >>> if CANHT:
> >>> s = re.sub(r"\d+.\d+,\d+.\d+,\d+.\d+,\d+.\d+,\d+.\d+ !
> >>> canht_ft", CANHT, s)
>
> >>> where CANHT might be
>
> >>> CANHT = '115.01,16.38,0.79,1.26,1.00 ! canht_ft'
>
> >>> But this involves me passing the entire string.
>
> >>> Thanks.
>
> >>> Martin
>
> >> I remove all lines containing things like 9*0.0 in your file, cause I
> >> don't know what they mean and how to handle them. These are not numbers.
>
> >> import re
>
> >> replace = {
> >> 'snow_grnd' : (1, '99.99,'), # replace the 1st number by 99.99
> >> 't_soil' : (2, '88.8,'), # replace the 2nd number by 88.88
> >> }
>
> >> testBuffer = """
> >> 0.749, 0.743, 0.754, 0.759 ! stheta(1:sm_levels)(top to bottom)
> >> 0.46 ! snow_grnd
> >> 276.78,277.46,278.99,282.48 ! t_soil(1:sm_levels)(top to bottom)
> >> 19.01,16.38,0.79,1.26,1.00 ! canht_ft(1:npft)
> >> 200.0, 4.0, 2.0, 4.0, 1.0 ! lai(1:npft)
> >> """
>
> >> outputBuffer = ''
> >> for line in testBuffer.split('\n'):
> >> for key, (index, repl) in replace.items():
> >> if key in line:
> >> parameters = {
> >> 'n' : '[\d\.]+', # given you example you have to change
> >> this one, I don't know what means 9*0.0 in your file
> >> 'index' : index - 1,
> >> }
> >> # the following pattern will silently match any digit before
> >> the th digit is found, and use a capturing parenthesis for the last
> >> pattern =
> >> '(\s*(?:(?:%(n)s)[,\s]+){0,%(index)s})(?:(%(n)s)[,\s]+)(.*!.*)' %
> >> parameters # regexp are sometimes a nightmare to read
> >> line = re.sub(pattern, r'\1 '+repl+r'\3' , line)
> >> break
> >> outputBuffer += line +'\n'
>
> >> print outputBuffer
>
> > Thanks I will take a look. I think perhaps I was having a very slow
> > day when I posted and realised I could solve the original problem more
> > efficiently and the problem wasn't perhaps as I first perceived. It is
> > enough to match the tag to the right of the "!" sign and use this to
> > adjust what lies on the left of the "!" sign. Currently I have
> > this...if anyone thinks there is a neater solution I am happy to hear
> > it. Many thanks.
>
> > variable_tag = 'lai'
> > variable = [200.0, 60.030, 0.060, 0.030, 0.030]
>
> > # generate adjustment string
> > variable = ",".join(["%s" % i for i in variable]) + ' ! ' +
> > variable_tag
>
> > # call func to adjust input file
> > adjustStandardPftParams(variable, variable_tag, in_param_fname,
> > out_param_fname)
>
> > and the inside of this func looks like this
>
> > def adjustStandardPftParams(self, variable, variable_tag, in_fname,
> > out_fname):
>
> > f = open(in_fname, 'r')
> > of = open(out_fname, 'w')
> > pattern_found = False
>
> > while True:
> > line = f.readline()
> > if not line:
> > break
> > pattern = re.findall(r"!\s+"+variable_tag, line)
> > if pattern:
> > print 'yes'
> > print >> of, "%s" % variable
> > pattern_found = True
>
> > if pattern_found:
> > pattern_found = False
> > else:
> > of.write(line)
>
> > f.close()
> > of.close()
>
> > return
>
> Are you sure a simple
> if variable_tag in line:
> # do some stuff
>
> is not enough ?
>
> People will usually prefer to write
>
> for line in open(in_fname, 'r') :
>
> instead of your ugly while loop ;-)
>
> JM
My while loop is suitably offended. I have changed it as you
suggested...though if I do: if pattern (variable_tag) in line as you
suggested i would in my example correctly pick the tag lai, but also
one called dcatch_lai, which I wouldn't want. No doubt there is an
obvious solution I am again missing!
of = open(out_fname, 'w')
pattern_found = False
for line in open(in_fname, 'r'):
pattern = re.findall(r"!\s+"+variable_tag, line)
if pattern:
print >> of, "%s" % variable
pattern_found = True
if pattern_found:
pattern_found = False
else:
of.write(line)
of.close()
Many Thanks.
--
http://mail.python.org/mailman/listinfo/python-list
Re: Reading file bit by bit
On Jun 7, 9:57 am, "Alfred Bovin" wrote:
> Hi all.
>
> I'm working on something where I need to read a (binary) file bit by bit and
> do something depending on whether the bit is 0 or 1.
>
> Any help on doing the actual file reading is appreciated.
>
> Thanks in advance
Hi,
Have you looked at the numpy libraries?
It would be very easy to do...
import numpy as np
f = open("something.bin", "rb")
data = np.fromfile(f, np.uint8)
data = np.where(data == 0, data * 5, data)
So in this example I am just saying if data = 0, multiply by 5. This
saves the need for slow loops as well.
Mart.
--
http://mail.python.org/mailman/listinfo/python-list
Re: What's the difference?
On Jun 10, 11:13 pm, Anthony Papillion wrote: > Thank you Emile and Thomas! I appreciate the help. MUCH clearer now. Also at a guess I think perhaps you wrote the syntax slightly wrong (square brackets)...you might want to look up "list comprehension" Martin -- http://mail.python.org/mailman/listinfo/python-list
Re: numpy arrays to python compatible arrays
On Jun 10, 9:02 pm, Philip Semanchuk wrote: > On Jun 10, 2010, at 9:58 AM, Javier Montoya wrote: > > > Dear all, > > > I'm new to python and have been working with the numpy package. I have > > some numpy float arrays (obtained from np.fromfile and np.cov > > functions) and would like to convert them to simple python arrays. > > I was wondering which is the best way to do that? Is there any > > function to do that? > > Hi Javier, > Since you are new to Python I'll ask whether you want to convert Numpy > arrays to Python arrays (as you stated) or Python lists. Python lists > are used very frequently; Python arrays see very little use outside of > numpy. > > If you can use a Python list, the .tolist() member of the numpy array > object should do the trick. > > bye > P as Philip said...though I very much doubt you really want to do this? Why wouldn't you just keep it in a numpy array? -- http://mail.python.org/mailman/listinfo/python-list
Re: efficiently create and fill array.array from C code?
On Jun 13, 6:15 pm, Thomas Jollans wrote: > Hi, > > I'm writing some buffer-centric number-crunching routines in C for > Python code that uses array.array objects for storing/manipulating data. > I would like to: > > 1. allocate a buffer of a certain size > 2. fill it > 3. return it as an array. > > I can't see any obvious way to do this with the array module, but I was > hoping somebody here might be able to help. My best shot would be to: > > 1. create a bytearray with PyByteArray_FromStringAndSize(NULL, byte_len) > 2. fill its buffer > 3. initialize an array from the bytearray. > > The issue I have with this approach is that array will copy the data to > its own buffer. I'd much rather create an array of a certain size, get a > write buffer, and fill it directly -- is that possible? > > I expect that numpy allows this, but I don't really want to depend on > numpy, especially as they haven't released a py3k version yet. > > -- Thomas You want Numpy... e.g. import numpy as np array = np.zeros(100, dtype=np.uint8) then either something like this to fill it for i in xrange(len(100)): array[i] = 2 or array = np.zeros(0) for i in xrange(len(100)): array = np.append(array, 2) Mart -- http://mail.python.org/mailman/listinfo/python-list
Re: a +b ?
On Jun 13, 5:46 pm, [email protected] wrote: > On 04:25 pm, [email protected] wrote: > > > > > > >Steven D'Aprano wrote: > >>No, I think your code is very simple. You can save a few lines by > >>writing > >>it like this: > > >>s = input('enter two numbers: ') > >>t = s.split() > >>print(int(t[0]) + int(t[1])) # no need for temporary variables a and > >>b > > >Not that we're playing a round of code golf here, but this is a > >slightly nicer take on your version: > > >one, two = input('enter two numbers: ').split() > >print(int(one) + int(two)) > > >I like names over subscripts, but that's just me :) > > Fore! > > print(sum(map(int, input('enter two numbers: ').split( > > Jean-Paul Can't beat that for lack of syntax! I'd probably add a check just because the OP did mention only two int's... data = [int(i) for i in raw_input('Enter two integers:\n').split()] if len(data) != 2: print 'Only enter 2 integers!' else: print "\n%d" % sum(data) -- http://mail.python.org/mailman/listinfo/python-list
Re: wikipedia with python
Does this help: http://www.mediawiki.org/wiki/MoinMoin On Mon, Jun 22, 2009 at 6:58 PM, zelegolas wrote: > Let me know if it's the right place to ask. > > I'm looking for wiki writen with python where I can import all > wikipedia site. > If you have any links please let me know. > > Thanks > -- > http://mail.python.org/mailman/listinfo/python-list > -- http://www.xing.com/profile/Martin_Marcher http://www.linkedin.com/in/martinmarcher You are not free to read this message, by doing so, you have violated my licence and are required to urinate publicly. Thank you. Please avoid sending me Word or PowerPoint attachments. See http://www.gnu.org/philosophy/no-word-attachments.html -- http://mail.python.org/mailman/listinfo/python-list
quickly looping over a 2D array?
Hi, I am new to python and I was wondering if there was a way to speed up the way I index 2D arrays when I need to check two arrays simultaneously? My current implementations is (using numpy) something like the following... for i in range(numrows): for j in range(numcols): if array_1[i, j] == some_value or array_2[i, j] >= array_1[i, j] * some_other_value array_1[i, j] = some_new_value Many Thanks, Martin -- http://mail.python.org/mailman/listinfo/python-list
Re: quickly looping over a 2D array?
On Jul 27, 12:42 pm, Peter Otten <[email protected]> wrote: > Martin wrote: > > I am new to python and I was wondering if there was a way to speed up > > the way I index 2D arrays when I need to check two arrays > > simultaneously? My current implementations is (using numpy) something > > like the following... > > > for i in range(numrows): > > for j in range(numcols): > > > if array_1[i, j] == some_value or array_2[i, j] >= array_1[i, > > j] * some_other_value > > array_1[i, j] = some_new_value > > array_1[(array_1 == some_value) | (array_2 >= array_1 * some_other_value)] = > some_new_value > > maybe? So I tried... band_1[(array_1 == 255) or (array_2 >= array_1 * factor)] = 0 which led to ValueError: The truth value of an array with more than one element is ambiguous. Use a.any() or a.all() so not sure that works? -- http://mail.python.org/mailman/listinfo/python-list
Re: quickly looping over a 2D array?
On Jul 27, 1:46 pm, Peter Otten <[email protected]> wrote: > Martin wrote: > > On Jul 27, 12:42 pm, Peter Otten <[email protected]> wrote: > >> Martin wrote: > >> > I am new to python and I was wondering if there was a way to speed up > >> > the way I index 2D arrays when I need to check two arrays > >> > simultaneously? My current implementations is (using numpy) something > >> > like the following... > > >> > for i in range(numrows): > >> > for j in range(numcols): > > >> > if array_1[i, j] == some_value or array_2[i, j] >= array_1[i, > >> > j] * some_other_value > >> > array_1[i, j] = some_new_value > > >> array_1[(array_1 == some_value) | (array_2 >= array_1 * > >> some_other_value)] = some_new_value > > >> maybe? > > > So I tried... > > > band_1[(array_1 == 255) or (array_2 >= array_1 * factor)] = 0 > > > which led to > > > ValueError: The truth value of an array with more than one element is > > ambiguous. Use a.any() or a.all() > > > so not sure that works? > > Copy and paste -- or replace "or" with "|". apologies - I mistook that for a type for "or" I now get the following error... ValueError: shape mismatch: objects cannot be broadcast to a single shape -- http://mail.python.org/mailman/listinfo/python-list
Re: quickly looping over a 2D array?
On Jul 27, 2:17 pm, Peter Otten <[email protected]> wrote: > Martin wrote: > > On Jul 27, 1:46 pm, Peter Otten <[email protected]> wrote: > >> Martin wrote: > >> > On Jul 27, 12:42 pm, Peter Otten <[email protected]> wrote: > >> >> Martin wrote: > >> >> > I am new to python and I was wondering if there was a way to speed > >> >> > up the way I index 2D arrays when I need to check two arrays > >> >> > simultaneously? My current implementations is (using numpy) > >> >> > something like the following... > > >> >> > for i in range(numrows): > >> >> > for j in range(numcols): > > >> >> > if array_1[i, j] == some_value or array_2[i, j] >= > >> >> > array_1[i, > >> >> > j] * some_other_value > >> >> > array_1[i, j] = some_new_value > > >> >> array_1[(array_1 == some_value) | (array_2 >= array_1 * > >> >> some_other_value)] = some_new_value > > >> >> maybe? > > >> > So I tried... > > >> > band_1[(array_1 == 255) or (array_2 >= array_1 * factor)] = 0 > > >> > which led to > > >> > ValueError: The truth value of an array with more than one element is > >> > ambiguous. Use a.any() or a.all() > > >> > so not sure that works? > > >> Copy and paste -- or replace "or" with "|". > > > apologies - I mistook that for a type for "or" > > > I now get the following error... > > > ValueError: shape mismatch: objects cannot be broadcast to a single > > shape > > It seems array_1 and array_2 have a -- dada! -- different shape. > Assuming array_1 is the smaller one: > > numrows, numcols = array_1.shape > array_1[(array_1 == some_value) | (array_2[:numrows,:numcols] >= array_1 * > some_other_value)] = some_new_value > > There may be other options, but I'm not a numpy expert. > > Peter My mistake - the incorrect size in the arrays was my error. The statement works now, but it doesn't give the same results as my original logic, strangely!? in my logic: data = np.zeros((numrows, numcols), dtype = np.uint8, order ='C') for i in range(numrows): for j in range(numcols): if band3[i,j] == 255 or band3[i,j] >= band6[i,j] * factor: data[i,j] = 0 else: data[i,j] = 1 to do the same with what you suggested... data = np.ones((numrows, numcols), dtype = np.uint8, order ='C') data[(band3 == 255) | (band6 >= band3 * factor)] = 0 Thanks -- http://mail.python.org/mailman/listinfo/python-list
Re: quickly looping over a 2D array?
On Jul 27, 4:12 pm, Peter Otten <[email protected]> wrote: > Martin wrote: > > The statement works now, but it doesn't give the same results as my > > original logic, strangely!? > > > in my logic: > > > data = np.zeros((numrows, numcols), dtype = np.uint8, order ='C') > > > for i in range(numrows): > > for j in range(numcols): > > if band3[i,j] == 255 or band3[i,j] >= band6[i,j] * factor: > > data[i,j] = 0 > > else: > > data[i,j] = 1 > > > to do the same with what you suggested... > > > data = np.ones((numrows, numcols), dtype = np.uint8, order ='C') > > data[(band3 == 255) | (band6 >= band3 * factor)] = 0 > > Did you swap band3 and band6? If that's the case, it is an error you should > be able to find yourself. > > Please be a bit more careful. > > Also, it is good practice to write a few test cases where you have > precalculated the result. How would you otherwise know which version is > correct? Writing a third one to break the tie? > > Peter apologies... it was the way I typed it up, I wasn't copying and pasting from my text editor! Thanks Peter. -- http://mail.python.org/mailman/listinfo/python-list
set variable to looping index?
Hi,
I am trying to set the return value from a function to a name which I
grab from the for loop. I can't work out how I can do this without
using an if statement...
for f in var1_fn, var2_fn, var3_fn:
if f.split('.')[0] == 'var1':
var1 = call_some_function(f)
.
.
.
etc
Really I would like to remove the need for this if loop and I am sure
there is a simple way I am missing?
Many thanks
Martin
--
http://mail.python.org/mailman/listinfo/python-list
Re: set variable to looping index?
On Jul 29, 11:02 pm, "Rhodri James"
wrote:
> On Wed, 29 Jul 2009 19:56:28 +0100, Martin wrote:
> > Hi,
>
> > I am trying to set the return value from a function to a name which I
> > grab from the for loop. I can't work out how I can do this without
> > using an if statement...
>
> > for f in var1_fn, var2_fn, var3_fn:
> > if f.split('.')[0] == 'var1':
> > var1 = call_some_function(f)
> > .
> > .
> > .
> > etc
>
> > Really I would like to remove the need for this if loop and I am sure
> > there is a simple way I am missing?
>
> It's a little hard to tell what you actually want from your description,
> but it looks like you're fighting the language unnecessarily here. If
> you have a sequence of functions that you want a sequence of results
> out of, you should be thinking in terms of a sequence type. A list,
> in other words.
>
> results = []
> for f in fn1, fn2, fn3:
> results.append(f())
>
> --
> Rhodri James *-* Wildebeest Herder to the Masses
Hi all,
Thanks and apologises I wasn't trying to be cryptic, like all things
when I wrote it I thought it was quite transparent.
All I was trying to do was call a function and return the result to an
a variable. I could admittedly of just done...
var1 = some_function(var1_fn)
var2 = some_function(var2_fn)
var3 = some_function(var3_fn)
where var1_fn, var2_fn, var3_fn are just filenames, e.g. var1_fn =
'x.txt'. But I figured I would try and make it slightly more generic
whilst I was at it, hence my attempt to use the filenames to create
the variable names (hence the loop). I will as suggested try the
dictionary option. I appreciate all the suggestions.
Thanks
Martin
--
http://mail.python.org/mailman/listinfo/python-list
Re: set variable to looping index?
On Jul 29, 11:57 pm, "Jan Kaliszewski" wrote:
> 30-07-2009 wrote:
> > All I was trying to do was call a function and return the result to an
> > a variable. I could admittedly of just done...
>
> > var1 = some_function(var1_fn)
> > var2 = some_function(var2_fn)
> > var3 = some_function(var3_fn)
>
> > where var1_fn, var2_fn, var3_fn are just filenames, e.g. var1_fn =
> > 'x.txt'. But I figured I would try and make it slightly more generic
> > whilst I was at it, hence my attempt to use the filenames to create
> > the variable names (hence the loop).
>
> Hi,
>
> Then you could also consider using simply lists:
>
> filenames = p'foo', 'bar', baz']
> results = []
> for name in filenames:
> results.append(some_function(name))
>
> If filenames were generated according to a particular pattern, you can
> mimic that pattern and generate filenames list using
> list-comprehension, e.g.:
>
> filenames = ['file{nr}.txt'.format(nr=nr) for nr in range(13)]
>
> Chreers,
>
> *j
>
> --
> Jan Kaliszewski (zuo)
I guess I wanted to keep the function returns in separate arrays in
this case, hence my attempt to make variable names based on the
filenames.
Thanks
Martin
--
http://mail.python.org/mailman/listinfo/python-list
matching patterns after regex?
Hi,
I have a string (see below) and ideally I would like to pull out the
decimal number which follows the bounding coordinate information. For
example ideal from this string I would return...
s = '\nGROUP = ARCHIVEDMETADATA\n
GROUPTYPE= MASTERGROUP\n\n GROUP =
BOUNDINGRECTANGLE\n\nOBJECT =
NORTHBOUNDINGCOORDINATE\n NUM_VAL = 1\n
VALUE= 19.82039\nEND_OBJECT =
NORTHBOUNDINGCOORDINATE\n\nOBJECT =
SOUTHBOUNDINGCOORDINATE\n NUM_VAL = 1\n
VALUE= 9.910197\nEND_OBJECT =
SOUTHBOUNDINGCOORDINATE\n\nOBJECT =
EASTBOUNDINGCOORDINATE\n NUM_VAL = 1\n
VALUE= 10.6506458717851\nEND_OBJECT =
EASTBOUNDINGCOORDINATE\n\nOBJECT =
WESTBOUNDINGCOORDINATE\n NUM_VAL = 1\n
VALUE= 4.3188348375893e-15\nEND_OBJECT
= WESTBOUNDINGCOORDINATE\n\n END_GROUP
NORTHBOUNDINGCOORDINATE = 19.82039
SOUTHBOUNDINGCOORDINATE = 9.910197
EASTBOUNDINGCOORDINATE = 10.6506458717851
WESTBOUNDINGCOORDINATE = 4.3188348375893e-15
so far I have only managed to extract the numbers by doing re.findall
("[\d.]*\d", s), which returns
['1',
'19.82039',
'1',
'9.910197',
'1',
'10.6506458717851',
'1',
'4.3188348375893',
'15',
etc.
Now the first problem that I can see is that my string match chops off
the "e-15" part and I am not sure how to incorporate the potential for
that in my pattern match. Does anyone have any suggestions as to how I
could also match this? Ideally I would have a statement which printed
the number between the two bounding coordinate strings for example
NORTHBOUNDINGCOORDINATE\n NUM_VAL = 1\n
VALUE= 19.82039\nEND_OBJECT =
NORTHBOUNDINGCOORDINATE\n\n
Something that matched "NORTHBOUNDINGCOORDINATE" and printed the
decimal number before it hit the next string
"NORTHBOUNDINGCOORDINATE". But I am not sure how to do this. any
suggestions would be appreciated.
Many thanks
Martin
--
http://mail.python.org/mailman/listinfo/python-list
Re: matching patterns after regex?
On Aug 12, 12:53 pm, Bernard wrote:
> On 12 août, 06:15, Martin wrote:
>
>
>
> > Hi,
>
> > I have a string (see below) and ideally I would like to pull out the
> > decimal number which follows the bounding coordinate information. For
> > example ideal from this string I would return...
>
> > s = '\nGROUP = ARCHIVEDMETADATA\n
> > GROUPTYPE= MASTERGROUP\n\n GROUP =
> > BOUNDINGRECTANGLE\n\nOBJECT =
> > NORTHBOUNDINGCOORDINATE\n NUM_VAL = 1\n
> > VALUE= 19.82039\nEND_OBJECT =
> > NORTHBOUNDINGCOORDINATE\n\nOBJECT =
> > SOUTHBOUNDINGCOORDINATE\n NUM_VAL = 1\n
> > VALUE= 9.910197\nEND_OBJECT =
> > SOUTHBOUNDINGCOORDINATE\n\nOBJECT =
> > EASTBOUNDINGCOORDINATE\n NUM_VAL = 1\n
> > VALUE= 10.6506458717851\nEND_OBJECT =
> > EASTBOUNDINGCOORDINATE\n\nOBJECT =
> > WESTBOUNDINGCOORDINATE\n NUM_VAL = 1\n
> > VALUE= 4.3188348375893e-15\nEND_OBJECT
> > = WESTBOUNDINGCOORDINATE\n\n END_GROUP
>
> > NORTHBOUNDINGCOORDINATE = 19.82039
> > SOUTHBOUNDINGCOORDINATE = 9.910197
> > EASTBOUNDINGCOORDINATE = 10.6506458717851
> > WESTBOUNDINGCOORDINATE = 4.3188348375893e-15
>
> > so far I have only managed to extract the numbers by doing re.findall
> > ("[\d.]*\d", s), which returns
>
> > ['1',
> > '19.82039',
> > '1',
> > '9.910197',
> > '1',
> > '10.6506458717851',
> > '1',
> > '4.3188348375893',
> > '15',
> > etc.
>
> > Now the first problem that I can see is that my string match chops off
> > the "e-15" part and I am not sure how to incorporate the potential for
> > that in my pattern match. Does anyone have any suggestions as to how I
> > could also match this? Ideally I would have a statement which printed
> > the number between the two bounding coordinate strings for example
>
> > NORTHBOUNDINGCOORDINATE\n NUM_VAL = 1\n
> > VALUE= 19.82039\nEND_OBJECT =
> > NORTHBOUNDINGCOORDINATE\n\n
>
> > Something that matched "NORTHBOUNDINGCOORDINATE" and printed the
> > decimal number before it hit the next string
> > "NORTHBOUNDINGCOORDINATE". But I am not sure how to do this. any
> > suggestions would be appreciated.
>
> > Many thanks
>
> > Martin
>
> Hey Martin,
>
> here's a regex I've just tested : (\w+COORDINATE).*\s+VALUE\s+=\s([\d\.
> \w-]+)
>
> the first match corresponds to the whateverBOUNDINGCOORDINATE and the
> second match is the value.
>
> please provide some more entries if you'd like me to test my regex
> some more :)
>
> cheers
>
> Bernard
Thanks Bernard it doesn't seem to be working for me...
I tried
re.findall((\w+COORDINATE).*\s+VALUE\s+=\s([\d\.\w-]+),s)
is that what you meant? Apologies if not, that results in a syntax
error:
In [557]: re.findall((\w+COORDINATE).*\s+VALUE\s+=\s([\d\.\w-]+),s)
File "", line 1
re.findall((\w+COORDINATE).*\s+VALUE\s+=\s([\d\.\w-]+),s)
^
SyntaxError: unexpected character after line continuation character
Thanks
--
http://mail.python.org/mailman/listinfo/python-list
Re: matching patterns after regex?
On Aug 12, 1:23 pm, Steven D'Aprano wrote:
> On Wed, 12 Aug 2009 05:12:22 -0700, Martin wrote:
> > I tried
>
> > re.findall((\w+COORDINATE).*\s+VALUE\s+=\s([\d\.\w-]+),s)
>
> You need to put quotes around strings.
>
> In this case, because you're using regular expressions, you should use a
> raw string:
>
> re.findall(r"(\w+COORDINATE).*\s+VALUE\s+=\s([\d\.\w-]+)",s)
>
> will probably work.
>
> --
> Steven
Thanks I see.
so I tried it and if I use it as it is, it matches the first instance:
I
n [594]: re.findall(r"(\w+COORDINATE).*\s+VALUE\s+=\s([\d\.\w-]+)",s)
Out[594]: [('NORTHBOUNDINGCOORDINATE', '1')]
So I adjusted the first part of the regex, on the basis I could sub
NORTH for SOUTH etc.
In [595]: re.findall(r"(NORTHBOUNDINGCOORDINATE).*\s+VALUE\s+=\s([\d\.
\w-]+)",s)
Out[595]: [('NORTHBOUNDINGCOORDINATE', '1')]
But in both cases it doesn't return the decimal value rather the value
that comes after NUM_VAL = , rather than VALUE = ?
--
http://mail.python.org/mailman/listinfo/python-list
Re: matching patterns after regex?
On Aug 12, 1:42 pm, Martin wrote:
> On Aug 12, 1:23 pm, Steven D'Aprano
>
>
> cybersource.com.au> wrote:
> > On Wed, 12 Aug 2009 05:12:22 -0700, Martin wrote:
> > > I tried
>
> > > re.findall((\w+COORDINATE).*\s+VALUE\s+=\s([\d\.\w-]+),s)
>
> > You need to put quotes around strings.
>
> > In this case, because you're using regular expressions, you should use a
> > raw string:
>
> > re.findall(r"(\w+COORDINATE).*\s+VALUE\s+=\s([\d\.\w-]+)",s)
>
> > will probably work.
>
> > --
> > Steven
>
> Thanks I see.
>
> so I tried it and if I use it as it is, it matches the first instance:
> I
> n [594]: re.findall(r"(\w+COORDINATE).*\s+VALUE\s+=\s([\d\.\w-]+)",s)
> Out[594]: [('NORTHBOUNDINGCOORDINATE', '1')]
>
> So I adjusted the first part of the regex, on the basis I could sub
> NORTH for SOUTH etc.
>
> In [595]: re.findall(r"(NORTHBOUNDINGCOORDINATE).*\s+VALUE\s+=\s([\d\.
> \w-]+)",s)
> Out[595]: [('NORTHBOUNDINGCOORDINATE', '1')]
>
> But in both cases it doesn't return the decimal value rather the value
> that comes after NUM_VAL = , rather than VALUE = ?
I think I kind of got that to work...but I am clearly not quite
understanding how it works as I tried to use it again to match
something else.
In this case I want to print the values 0.00 and 2223901.039333
from a string like this...
YDim=1200\n\t\tUpperLeftPointMtrs=(0.00,2223901.039333)\n\t\t
I tried which I though was matching the statement and printing the
decimal number after the equals sign??
re.findall(r"(\w+UpperLeftPointMtrs)*=\s([\d\.\w-]+)", s)
where s is the string
Many thanks for the help
--
http://mail.python.org/mailman/listinfo/python-list
Re: matching patterns after regex?
On Aug 12, 10:29 pm, Mark Lawrence wrote:
> Bernard wrote:
> > On 12 août, 12:43, Martin wrote:
> >> On Aug 12, 1:42 pm, Martin wrote:
>
> >>> On Aug 12, 1:23 pm, Steven D'Aprano >>> cybersource.com.au> wrote:
> >>>> On Wed, 12 Aug 2009 05:12:22 -0700, Martin wrote:
> >>>>> I tried
> >>>>> re.findall((\w+COORDINATE).*\s+VALUE\s+=\s([\d\.\w-]+),s)
> >>>> You need to put quotes around strings.
> >>>> In this case, because you're using regular expressions, you should use a
> >>>> raw string:
> >>>> re.findall(r"(\w+COORDINATE).*\s+VALUE\s+=\s([\d\.\w-]+)",s)
> >>>> will probably work.
> >>>> --
> >>>> Steven
> >>> Thanks I see.
> >>> so I tried it and if I use it as it is, it matches the first instance:
> >>> I
> >>> n [594]: re.findall(r"(\w+COORDINATE).*\s+VALUE\s+=\s([\d\.\w-]+)",s)
> >>> Out[594]: [('NORTHBOUNDINGCOORDINATE', '1')]
> >>> So I adjusted the first part of the regex, on the basis I could sub
> >>> NORTH for SOUTH etc.
> >>> In [595]: re.findall(r"(NORTHBOUNDINGCOORDINATE).*\s+VALUE\s+=\s([\d\.
> >>> \w-]+)",s)
> >>> Out[595]: [('NORTHBOUNDINGCOORDINATE', '1')]
> >>> But in both cases it doesn't return the decimal value rather the value
> >>> that comes after NUM_VAL = , rather than VALUE = ?
> >> I think I kind of got that to work...but I am clearly not quite
> >> understanding how it works as I tried to use it again to match
> >> something else.
>
> >> In this case I want to print the values 0.00 and 2223901.039333
> >> from a string like this...
>
> >> YDim=1200\n\t\tUpperLeftPointMtrs=(0.00,2223901.039333)\n\t\t
>
> >> I tried which I though was matching the statement and printing the
> >> decimal number after the equals sign??
>
> >> re.findall(r"(\w+UpperLeftPointMtrs)*=\s([\d\.\w-]+)", s)
>
> >> where s is the string
>
> >> Many thanks for the help
>
> > You have to do it with 2 matches in the same regex:
>
> > regex = r"UpperLeftPointMtrs=\(([\d\.]+),([\d\.]+)"
>
> > The first match is before the , and the second one is after the , :)
>
> > You should probably learn how to play with regexes.
> > I personnaly use a visual tool called RX Toolkit[1] that comes with
> > Komodo IDE.
>
> > [1]http://docs.activestate.com/komodo/4.4/regex.html
>
> Haven't tried it myself but how about this?http://re-try.appspot.com/
>
> --
> Kindest regards.
>
> Mark Lawrence.
Thanks Mark and Bernard. I have managed to get it working and I
appreciate the help with understanding the syntax. The web links are
also very useful, I'll give them a go.
Martin
--
http://mail.python.org/mailman/listinfo/python-list
Re: PIL and Python
On Aug 13, 1:55 pm, "[email protected]" wrote: > Hello ! > I want use python to change the "note" from .jpeg files . > What is the functions on PIL how make this ? > Thank you ! What do u mean by the note? -- http://mail.python.org/mailman/listinfo/python-list
Extracting patterns after matching a regex
Hi, I need to extract a string after a matching a regular expression. For example I have the string... s = "FTPHOST: e4ftl01u.ecs.nasa.gov" and once I match "FTPHOST" I would like to extract "e4ftl01u.ecs.nasa.gov". I am not sure as to the best approach to the problem, I had been trying to match the string using something like this: m = re.findall(r"FTPHOST", s) But I couldn't then work out how to return the "e4ftl01u.ecs.nasa.gov" part. Perhaps I need to find the string and then split it? I had some help with a similar problem, but now I don't seem to be able to transfer that to this problem! Thanks in advance for the help, Martin -- http://mail.python.org/mailman/listinfo/python-list
Re: Python und Referenzen auf Variablen?
Hi, this list is english only. I won't translate for you as I think you wouldn't be happy with it - as you can't read the recommendations - if you don't speak english. In that case you might want to try [email protected]) (die Liste ist eigentlich nur english, ich übersetze das mal nicht, da du mit englischen antworten nicht glücklich sein wirst. Kurzes google hat mich zu [email protected] geführt falls du lieber auf Deutsch unterwegs bist) 2009/9/25 Torsten Mohr : > Hallo, > > ich möchte im Speicher eine verschachtelte Struktur aufbauen in der -- http://www.xing.com/profile/Martin_Marcher http://www.linkedin.com/in/martinmarcher You are not free to read this message, by doing so, you have violated my licence and are required to urinate publicly. Thank you. Please avoid sending me Word or PowerPoint attachments. See http://www.gnu.org/philosophy/no-word-attachments.html -- http://mail.python.org/mailman/listinfo/python-list
Unittest fails to import module
I know the answer to this must be trivial but I am stuck... I am starting on a not too complex Python project. Right now the project file structure contains three subdirectories and two files with Python code: code blablabla.py test blablabla_test.py doc (empty for now) blablabla_test.py contains "import unittest" and "import blablabla" $PYTHONPATH points at both the code and the test directories. When I run blablabla_test.py it fails to import blablabla.py I have messed around for oven an hour and get nowhere. I have done unittesting like this with success in the past and I have revisited one of those projects and it still works there. The older project has a slightly flatter structure as it lacks a separate code subdirectory: something.py test something_test.py I have temporarily tried this on the new project but to no avail. Any leads? TIA /Martin -- http://mail.python.org/mailman/listinfo/python-list
Re: Unittest fails to import module
On 2013-06-29, Steven D'Aprano wrote:
> On Sat, 29 Jun 2013 19:13:47 +, Martin Schöön wrote:
>
>> $PYTHONPATH points at both the code and the test directories.
>>
>> When I run blablabla_test.py it fails to import blablabla.py
>
> What error message do you get?
>
>
>> I have messed around for oven an hour and get nowhere. I have done
>> unittesting like this with success in the past and I have revisited one
>> of those projects and it still works there.
> [...]
>> Any leads?
>
> The first step is to confirm that your path is setup correctly. At the
> very top of blablabla_test, put this code:
>
> import os, sys
> print(os.getenv('PYTHONPATH'))
> print(sys.path)
>
Yes, right, I had not managed to make my change to PYTHONPATH stick.
I said the explanation would be trivial, didn't I?
Thanks for the quick replies. I am back in business now.
No, neither English nor Python are native languages of mine but I
enjoy (ab)using both :-)
/Martin
--
http://mail.python.org/mailman/listinfo/python-list
[ANN] pyspread 0.2.4
== pyspread 0.2.4 == Pyspread 0.2.4 is released. Besides Linux, the new version also runs on Windows (Windows 7 64bit and Windows XP 32bit tested). About pyspread == Pyspread is a non-traditional spreadsheet application that is based on and written in the programming language Python. The goal of pyspread is to be the most pythonic spreadsheet application. Pyspread is free software. It is released under the GPL v3. Project website: http://manns.github.com/pyspread/ What is new in 0.2.4 + Windows compatibility + More charts and chart options (box plots, histograms, pie charts) + Export of matplotlib charts to svg, png, pdf, eps and ps + Import from UTF-8 encoded csv files (no other encoding, yet) + More than 64k characters supported in one cell + Insertion and deletion of rows and columns now affect only the current table + High quality in-cell bitmaps images (with transparency) supported + Iterable data can be pasted into multiple cells via Paste As... + Pyspread can now be started from other Python applications + Partly localized in Nowegian Nynorsk and Bokmaal Known issues + Selection mode is disabled in Windows. + "Find & Replace All" is slow in grids with much data unless pyspread is minimized. + Paste As... does not work if dates are present in the data structure. + Sometimes, pressing redo when there is nothing left to redo has undesired effects such as redoing an operation again. + When updating from a non-release version (from git), the file ~/.pyspreadrc (in Windows pyspread's registry entry) may have to be deleted. Enjoy Martin -- http://mail.python.org/mailman/listinfo/python-list
Re: Top-posting &c. (was Re: [ANNC] pybotwar-0.8)
in 679182 20120821 181439 Dennis Lee Bieber wrote: >On Tue, 21 Aug 2012 08:07:33 +0200, Alex Strickland >declaimed the following in gmane.comp.python.general: > >> On 2012/08/17 12:42 AM, Madison May wrote: >> >> > As a lurker, I agree completely with Chris's sentiments. >> >> I too, but I'd prefer something top-posted than have to skip through 38 >> pages of quoted e-mail to get to a (generally) 1 liner at the bottom. > >Doesn't help me though... Agent shows quoted material as blue, fresh >text as black. > >I tend to not see a one-liner at the top (since it is next to the >attribution line) and if the rest of the page is all blue text I hit >page down... and down, down, down... looking for black text... Then end >up going "Wha', where's the new stuff?" and having to scroll back up to >find a one-liner cuddling up with the attribution line. Yep, and the only solution is for everyone to top-post. -- http://mail.python.org/mailman/listinfo/python-list
Re: Article on the future of Python
in 681910 20120927 131113 Devin Jeanpierre wrote: >On Thu, Sep 27, 2012 at 2:13 AM, Steven D'Aprano > wrote: >> On Tue, 25 Sep 2012 09:15:00 +0100, Mark Lawrence wrote: >> And a response: >> >> http://data.geek.nz/python-is-doing-just-fine > >Summary of that article: > >"Sure, you have all these legitimate concerns, but look, cake!" Quote : "This piece argues that Python is an easy-to-learn language that where you can be almost immediately productive in." -- http://mail.python.org/mailman/listinfo/python-list
Re: RE: Unpaking Tuple
in 682592 20121008 232126 "Prasad, Ramit" wrote: >Thomas Bach wrote:=0D=0A> Hi there,=0D=0A> =0D=0A> On Sat, Oct 06, 2012 at = >03:08:38PM +, Steven D'Aprano wrote:=0D=0A> >=0D=0A> > my_tuple =3D my_= >tuple[:4]=0D=0A> > a,b,c,d =3D my_tuple if len(my_tuple) =3D=3D 4 else (my_= >tuple + (None,)*4)[:4]=0D=0A> >=0D=0A> =0D=0A> Are you sure this works as y= >ou expect? I just stumbled over the following:=0D=0A> =0D=0A> $ python=0D= >=0A> Python 3=2E2=2E3 (default, Jun 25 2012, 23:10:56)=0D=0A> [GCC 4=2E7=2E= >1] on linux2=0D=0A> Type "help", "copyright", "credits" or "license" for mo= >re information=2E=0D=0A> >>> split =3D ['foo', 'bar']=0D=0A> >>> head, tail= >=3D split if len(split) =3D=3D 2 else split[0], None=0D=0A> >>> head=0D=0A= >> ['foo', 'bar']=0D=0A> >>> tail=0D=0A> >>>=0D=0A> =0D=0A> I don't get it! = >Could someone help me, please? Why is head not 'foo'=0D=0A> and tail not 'b= >ar'?=0D=0A> =0D=0A> Regards,=0D=0A>Thomas=0D=0A> --=0D=0A=0D=0AI think yo= >u just need to wrap the else in parenthesis so the=0D=0Aelse clause is trea= >ted as a tuple=2E Without the parenthesis =0D=0AI believe it is grouping th= >e code like this=2E=0D=0A=0D=0Ahead, tail =3D (split if len(split) =3D=3D 2= >else split[0] ), None=0D=0A=0D=0AYou want:=0D=0Ahead, tail =3D split if le= >n(split) =3D=3D 2 else (split[0], None )=0D=0A=0D=0A=0D=0ARamit=0D=0AThis e= >mail is confidential and subject to important disclaimers and=0D=0Aconditio= >ns including on offers for the purchase or sale of=0D=0Asecurities, accurac= >y and completeness of information, viruses,=0D=0Aconfidentiality, legal pri= >vilege, and legal entity disclaimers,=0D=0Aavailable at http://www=2Ejpmorg= >an=2Ecom/pages/disclosures/email=2E How does one unpack this post? ;-) -- http://mail.python.org/mailman/listinfo/python-list
Organisation of python classes and their methods
Dear list, I'm relatively new to Python and have googled and googled but haven't found a reasonable answer to this question, so I thought I'd ask it here. I'm beginning a large Python project which contains many packages, modules and classes. The organisation of those is clear to me. Now, the classes can contain many methods (100s of data analysis methods) which operate on instances of the class they belong to. These methods can be long and complex. So if I put these methods all in the module file inside the class, the file will get insanely long. Reading on google, the answer is usually "refactor", but that really doesn't make sense here. It's just that the methods are many, and each method can be a long piece of code. So, is there a way to put these methods in their own files and have them 'included' in the class somehow? I read a little about mixins but all the solutions looked very hacky. Is there an official python way to do this? I don't like having source files with 100's of lines of code in, let alone 1000's. Many thanks, Martin -- http://mail.python.org/mailman/listinfo/python-list
Re: Organisation of python classes and their methods
On 2, Nov, 2012, at 08:38 AM, Paul Rubin wrote: > Martin Hewitson writes: >> So, is there a way to put these methods in their own files and have >> them 'included' in the class somehow? ... Is there an official python >> way to do this? I don't like having source files with 100's of lines >> of code in, let alone 1000's. > > That code sounds kind of smelly... why are there so many methods per > class? Simply because there are many different ways to process the data. The class encapsulates the data, and the user can process the data in many ways. Of course, one could have classes which encapsulate the algorithms, as well as the data, but it also seems natural to me to have algorithms as methods which are part of the data class, so the user operates on the data using methods of that class. > > Python lets you inject new methods into existing classes (this is > sometimes called duck punching) but I don't recommend doing this. Is there not a way just to declare the method in the class and put the actual implementation in another file on the python path so that it's picked up a run time? > > A few hundred lines of code in a file is completely reasonable. Even a > few thousand is ok. IME it starts getting unwieldy at 3000 or so. In all other projects (in other languages) I've always tried to keep individual files short to ease maintainability and search-ability (if there is such a word). Even if one takes reasonable numbers: 20 methods, each method has 20 lines of documentation, then we immediately have 400 lines in the file before writing a line of code. It would seem much more natural to me to have these methods in their own file, grouped nicely in sub-directories. But it seems this is not the python way. Sigh. Thanks for your thoughts, Martin -- http://mail.python.org/mailman/listinfo/python-list
Re: Organisation of python classes and their methods
On 2, Nov, 2012, at 09:00 AM, Peter Otten <[email protected]> wrote: > Martin Hewitson wrote: > >> Dear list, >> >> I'm relatively new to Python and have googled and googled but haven't >> found a reasonable answer to this question, so I thought I'd ask it here. >> >> I'm beginning a large Python project which contains many packages, modules >> and classes. The organisation of those is clear to me. >> >> Now, the classes can contain many methods (100s of data analysis methods) >> which operate on instances of the class they belong to. These methods can >> be long and complex. So if I put these methods all in the module file >> inside the class, the file will get insanely long. Reading on google, the >> answer is usually "refactor", but that really doesn't make sense here. >> It's just that the methods are many, and each method can be a long piece >> of code. So, is there a way to put these methods in their own files and >> have them 'included' in the class somehow? I read a little about mixins >> but all the solutions looked very hacky. Is there an official python way >> to do this? I don't like having source files with 100's of lines of code >> in, let alone 1000's. > > You googled, found the right answer ("refactor"), didn't like it and are now > looking to cure the symptoms of the original problem? > Seriously, a good editor can deal with a long source file, but a class with > hundreds of methods will bring trouble to any old brain. Well, here we disagree. Suppose I have a class which encapsulates time-series data. Below is a list of the absolute minimum methods one would have to process that data. That's close to 100 already before even having any specialised methods for dealing with the data. Each of these methods will have maybe 20 lines of documentation. That's 2000 lines already. And what if someone wants to extend that class to add their own processing methods? It would a maintenance nightmare for them to edit the actual class file, which they would then have to repeat each time a new version of the 'official' class file is released. So maybe some rethinking of this design is needed to handle this 'limitation' of python. Perhaps grouping the processing algorithms into methods of processing classes, then pass the data objects to these methods. But somehow I don't like that. I have the feeling these methods would end up peppered with things like: if this data type, do this else if this data type, do this else normally this would be solved by overloading methods in different data subclasses. More thinking needed, clearly. Martin 'abs' 'acos' 'asin' 'atan' 'atan2' 'average' 'cohere' 'conv' 'corr' 'cos' 'cov' 'cpsd' 'detrend' 'dft' 'diff' 'downsample' 'exp' 'export' 'fft' 'fftfilt' 'filter' 'filtfilt' 'find' 'heterodyne' 'hist' 'imag' 'integrate' 'interp' 'join' 'le' 'lincom' 'ln' 'load' 'log' 'log10' 'lscov' 'max' 'mean' 'median' 'min' 'minus' 'mode' 'mpower' 'mrdivide' 'mtimes' 'ne' 'norm' 'or' 'plot' 'plus' 'polyfit' 'power' 'psd' 'rdivide' 'real' 'resample' 'rms' 'round' 'save' 'scale' 'search' 'select' 'sin' 'smoother' 'sort' 'spectrogram' 'split' 'sqrt' 'std' 'sum' 'sumjoin' 'svd' 'tan' 'tfe' 'timeaverage' 'times' 'timeshift' 'transpose' 'uminus' 'upsample' 'zeropad' -- http://mail.python.org/mailman/listinfo/python-list
Re: Organisation of python classes and their methods
On 2, Nov, 2012, at 09:40 AM, Mark Lawrence wrote: > On 02/11/2012 08:08, Martin Hewitson wrote: >> >> Even if one takes reasonable numbers: 20 methods, each method has 20 lines >> of documentation, then we immediately have 400 lines in the file before >> writing a line of code. It would seem much more natural to me to have these >> methods in their own file, grouped nicely in sub-directories. But it seems >> this is not the python way. Sigh. >> >> Thanks for your thoughts, >> >> Martin >> > > 20 lines of documentation per method? As far as I'm concerned that's not a > smell, that's a stink. Wow, I don't think I've ever been criticised before for writing too much documentation :) I guess we have different end users. This is not a set of classes for other developers to use: it's a set of classes which creates a data analysis environment for scientists to use. They are not programmers, and expect the algorithms to be documented in detail. Martin > > -- > Cheers. > > Mark Lawrence. > > -- > http://mail.python.org/mailman/listinfo/python-list -- http://mail.python.org/mailman/listinfo/python-list
Re: Organisation of python classes and their methods
On 2, Nov, 2012, at 11:49 AM, Ulrich Eckhardt wrote: > Am 02.11.2012 09:20, schrieb Martin Hewitson: >> Well, here we disagree. Suppose I have a class which encapsulates >> time-series data. Below is a list of the absolute minimum methods one >> would have to process that data. > [...] > > 'abs' 'acos' 'asin' 'atan' 'atan2' 'average' 'cohere' 'conv' 'corr' > > 'cos' 'cov' 'cpsd' 'detrend' 'dft' 'diff' 'downsample' 'exp' > > 'export' 'fft' 'fftfilt' 'filter' 'filtfilt' 'find' 'heterodyne' > > 'hist' 'imag' 'integrate' 'interp' 'join' 'le' 'lincom' 'ln' 'load' > > 'log' 'log10' 'lscov' 'max' 'mean' 'median' 'min' 'minus' 'mode' > > 'mpower' 'mrdivide' 'mtimes' 'ne' 'norm' 'or' 'plot' 'plus' > > 'polyfit' 'power' 'psd' 'rdivide' 'real' 'resample' 'rms' 'round' > > 'save' 'scale' 'search' 'select' 'sin' 'smoother' 'sort' > > 'spectrogram' 'split' 'sqrt' 'std' 'sum' 'sumjoin' 'svd' 'tan' 'tfe' > > 'timeaverage' 'times' 'timeshift' 'transpose' 'uminus' 'upsample' > > 'zeropad' > > > Just as a suggestion, you can separate these into categories: > > 1. Things that modify the data, yielding a different (although derived) data > set, e.g. import/load, split, join, plus, minus, zeropad. > 2. Things that operate on the data without modifying it, e.g. export/save, > average, find, plot, integrate. > > The latter can easily be removed from the class. Since they don't touch the > content, they can't invalidate internals and can't break encapsulation. > > For the former, providing general means to construct or modify the data (like > e.g. adding records or joining sequences) is also all that needs to remain > inside the class to ensure internal consistency, everything else can be built > on top of these using external functions. > Thank you all so much for your thoughts and suggestions. I need to absorb all of this and decide on the best approach in this case. Thanks again, Martin > > Uli > > > > -- > http://mail.python.org/mailman/listinfo/python-list -- http://mail.python.org/mailman/listinfo/python-list
Re: Organisation of python classes and their methods
> > > BTW: If you told us which language(s) you have a background in, it could be > easier to help you with identifying the idioms in that language that turn > into misconceptions when applied to Python. I'm considering porting some MATLAB code to python to move away from commercial software. Python seemed like the right choice simply because of the wonderful numpy, scipy and matplotlib. So my project will build on these packages to provide some additional state and functionality. Cheers, Martin > > Greetings! > > Uli > > [1] Actually, modules themselves provide the kind of separation that I think > you are after. Don't always think "class" if it comes to encapsulation and > modularization! > -- > http://mail.python.org/mailman/listinfo/python-list -- http://mail.python.org/mailman/listinfo/python-list
