python-colormath 1.0.5 Released
An error in the CIE2000 Delta E equation has been found and corrected, necessitating the immediate release of python-colormath 1.0.5. All users of the 1.x series are encouraged to upgrade to avoid this mathematical error. What new in 1.0.5? === * The examples and unit tests may be ran directly from their directories now, without installing the package. * Updated the setup.py file to include the examples and LICENSE.txt file in the source distribution. * Fixed a small error in the CIE2000 Delta E formula. This could had resulted in some minor skew in calculations. What is python-colormath? python-colormath is a developer-oriented module that abstracts a number of color math operations behind a small set of classes representing color spaces (IE: RGB, CIE Lab, XYZ, and LCH, etc.). Color conversions, delta E comparisons, and density calculations are all relatively involved, but are hid behind the simple API. Where is python-colormath? = Getting Started/Website/Development: http://code.google.com/p/python-colormath/ Documentation: http://code.google.com/p/python-colormath/w/list Download: http://pypi.python.org/pypi/colormath/ -- Gregory Taylor -- http://mail.python.org/mailman/listinfo/python-announce-list Support the Python Software Foundation: http://www.python.org/psf/donations/
Re: Global array in python
2009/9/29 rantingrick rantingr...@gmail.com: On Sep 28, 8:04 pm, Chris Rebert c...@rebertia.com wrote: On Mon, Sep 28, 2009 at 5:48 PM, Rudolf yellowblueyel...@gmail.com wrote: How can i declare a global array in python? Python has no concept of declarations. And it doesn't have arrays, it has dynamically-resizing lists. What version are you using, i must have py4000 alpha? import array a = array.array('i') a.append(1) a array('i', [1]) hmm? ;-) Heh. I must remember to never state python does not have X - it almost always has X, I just don't know where yet ;) -- http://mail.python.org/mailman/listinfo/python-list -- twitter.com/olofb olofb.wordpress.com olofb.wordpress.com/tag/english -- http://mail.python.org/mailman/listinfo/python-list
Re: Global array in python
On Mon, Sep 28, 2009 at 6:55 PM, rantingrick rantingr...@gmail.com wrote: On Sep 28, 8:04 pm, Chris Rebert c...@rebertia.com wrote: On Mon, Sep 28, 2009 at 5:48 PM, Rudolf yellowblueyel...@gmail.com wrote: How can i declare a global array in python? Python has no concept of declarations. And it doesn't have arrays, it has dynamically-resizing lists. What version are you using, i must have py4000 alpha? import array a = array.array('i') a.append(1) a array('i', [1]) hmm? ;-) I should have qualified that statement... :) Cheers, Chris -- http://blog.rebertia.com -- http://mail.python.org/mailman/listinfo/python-list
Re: Why the file mode of .pyc files has x?
Peng Yu wrote: What python/OS are you using? python 2.6.2 and CentOS Just tried 2.6 on Darwin, and it does happen. So looks like 2.6 has been changed to inherit the permission bits from the .py. Makes sense, except that the x bits should really be turned off. -- Greg -- http://mail.python.org/mailman/listinfo/python-list
os.listdir unwanted behaviour
Hello, I am writing code that cycles through files in a directory and for each file it writes out another file with info in it. It appears that as I am iterating through the list returned by os.listdir it is being updated with the new files that are being added to the directory. This occurs even if I reassign the list to another variable. Here is my code: fileList = os.listdir(temporaryDirectory) for curFile in fileList: # print the file list to see if it is indeed growing print FileList fp = file(os.path.join(temporaryDirectory, . + curFile), 'w') # write stuff fp.close() Here is the output: ['a', 'b', 'c'] ['a', 'b', 'c', '.a'] ['a', 'b', 'c', '.a', '.b'] ['a', 'b', 'c', '.a', '.b', '.c'] So the list is growing and eventually curFile iterates through the list of files that were created. I don't want this to happen and it seems like a bug because the fileList variable should be static, i.e. not updated after being assigned. Even if I assign fileList to another variable this still happens. Any ideas? Chris. -- http://mail.python.org/mailman/listinfo/python-list
Re: os.listdir unwanted behaviour
On Mon, Sep 28, 2009 at 11:03 PM, Chris Adamson chris.adam...@mcri.edu.au wrote: Hello, I am writing code that cycles through files in a directory and for each file it writes out another file with info in it. It appears that as I am iterating through the list returned by os.listdir it is being updated with the new files that are being added to the directory. This occurs even if I reassign the list to another variable. Here is my code: fileList = os.listdir(temporaryDirectory) for curFile in fileList: # print the file list to see if it is indeed growing print FileList fp = file(os.path.join(temporaryDirectory, . + curFile), 'w') # write stuff fp.close() Here is the output: ['a', 'b', 'c'] ['a', 'b', 'c', '.a'] ['a', 'b', 'c', '.a', '.b'] ['a', 'b', 'c', '.a', '.b', '.c'] So the list is growing and eventually curFile iterates through the list of files that were created. I don't want this to happen and it seems like a bug because the fileList variable should be static, i.e. not updated after being assigned. Even if I assign fileList to another variable this still happens. Any ideas? Copy the list instead? Python uses call-by-object, so assignment to a variable doesn't cause copying, you must do so explicitly: fileList = os.listdir(temporaryDirectory)[:] Although this behavior (bug?) you're running into definitely seems like ought to be mentioned in the docs. File a bug perhaps? Cheers, Chris -- http://blog.rebertia.com -- http://mail.python.org/mailman/listinfo/python-list
Re: Repeated output when logging exceptions
On Sep 28, 9:38 pm, John Gordon gor...@panix.com wrote: If I didn't do all that in a class, where would I do it? You could, for example, use the basicConfig() function to do it all for you. import logging logging.basicConfig(filename='/path/to/my/log',level=logging.DEBUG) logging.debug('This message should go to the log file') Here's the documentation link for simple examples: http://docs.python.org/library/logging.html#simple-examples Here's the link for basicConfig: http://docs.python.org/library/logging.html#logging.basicConfig Regards, Vinay Sajip -- http://mail.python.org/mailman/listinfo/python-list
Re: Why the file mode of .pyc files has x?
On Tue, 29 Sep 2009 18:21:09 +1200, greg wrote: Peng Yu wrote: What python/OS are you using? python 2.6.2 and CentOS Just tried 2.6 on Darwin, and it does happen. So looks like 2.6 has been changed to inherit the permission bits from the .py. Makes sense, except that the x bits should really be turned off. Bug reported. http://bugs.python.org/issue7016 -- Steven -- http://mail.python.org/mailman/listinfo/python-list
Re: os.listdir unwanted behaviour
Chris Adamson wrote: I am writing code that cycles through files in a directory and for each file it writes out another file with info in it. It appears that as I am iterating through the list returned by os.listdir it is being updated with the new files that are being added to the directory. This occurs even if I reassign the list to another variable. My guess is that this has nothing to do with os.listdir(): import os files = os.listdir(.) files ['b', 'a'] os.system(touch c) 0 files ['b', 'a'] # look Ma, no automatic updates! os.listdir(.) ['b', 'c', 'a'] It is normal Python behaviour that assignment doesn't copy a list; it just creates another reference: a = [1] b = a id(a) == id(b) True b.append(2) a [1, 2] Use slicing to make an actual copy: b = a[:] # b = list(a) would work, too id(a) == id(b) False b.append(3) a [1, 2] b [1, 2, 3] Here is my code: No, it's not. If you post a simplified version it is crucial that you don't remove the parts that actually cause the undesired behaviour. In your case there has to be a mutating operation on the list like append() or extend(). Peter -- http://mail.python.org/mailman/listinfo/python-list
Re: os.listdir unwanted behaviour
On Tue, 29 Sep 2009 16:03:46 +1000, Chris Adamson wrote: Hello, I am writing code that cycles through files in a directory and for each file it writes out another file with info in it. It appears that as I am iterating through the list returned by os.listdir it is being updated with the new files that are being added to the directory. This occurs even if I reassign the list to another variable. Here is my code: fileList = os.listdir(temporaryDirectory) for curFile in fileList: # print the file list to see if it is indeed growing print FileList fp = file(os.path.join(temporaryDirectory, . + curFile), 'w') # write stuff fp.close() Are you sure this is your code you're using? Where is FileList defined? It's not the same as fileList. What you describe is impossible -- os.listdir() returns an ordinary list, it isn't a lazy iterator that updates automatically as the directory changes. (At least not in Python2.5 -- I haven't checked Python 3.1.) This is what happens when I try it: import os os.listdir('.') ['a', 'c', 'b'] filelist = os.listdir('.') for curFile in filelist: ... print filelist ... fp = file(os.path.join('.', .+curFile), 'w') ... fp.close() ... ['a', 'c', 'b'] ['a', 'c', 'b'] ['a', 'c', 'b'] I think the bug is in your code -- you're probably inadvertently updating fileList somehow. -- Steven -- http://mail.python.org/mailman/listinfo/python-list
[off-topic] Pessimal (was: Detecting changes to a dict)
If you can enumerate the language of possible inputs you could generate a unique binary representation. Against a language of size l that would only take you O(l*n) to build the repr for a dict and for certain repr sizes the comparison could be O(1), making the entire operation O(l*n+l*m) vs O(n*m). Geremy, I can't comment on the *content* of your observation on performance, but ... This sentence reminds me of the mid-1980s, when I was learning C in order to program the very first font cartridge for the HP LaserJet printer. The escape sequences seemed to consist entirely of ones and small ells and zeros and capital ohs -- apparently designed for maximal confusion. Was it around that time that the word pessimal was coined? -John -- http://mail.python.org/mailman/listinfo/python-list
Re: Most active coroutine library project?
On 2009-09-28, Hendrik van Rooyen hend...@microcorp.co.za wrote: On Saturday, 26 September 2009 16:55:30 Grant Edwards wrote: On 2009-09-26, Dave Angel da...@ieee.org wrote: Actually even 64k looked pretty good, compared to the 1.5k of RAM and 2k of PROM for one of my projects, a navigation system for shipboard use. I've worked on projects as recently as the past year that had only a couple hundred bytes of RAM, and most of it was reserved for a message buffer. There is little reason to do that nowadays - one can buy a single cycle 8032 running at 30 MHz with 16/32/64k of programming flash and ik of RAM, as well as some bytes of eeprom for around US$10-00. - in one off quantities. $10 is pretty expensive for a lot of applications. I bet that processor also uses a lot of power and takes up a lot of board space. If you've only got $2-$3 in the money budget, 200uA at 1.8V in the power budget, and 6mm X 6mm of board-space, your choices are limited. Besides If you can get by with 256 or 512 bytes of RAM, why pay 4X the price for a 1K part? Besides which, the 8032 instruction set and development tools are icky compared to something like an MSP430 or an AVR. ;) [The 8032 is still head and shoulders above the 8-bit PIC family.] -- Grant -- http://mail.python.org/mailman/listinfo/python-list
Re: custom data warehouse in python vs. out-of-the-box ETL tool
On this post, you are talking about Talend and Informatica like both are comparable... indeed both tools are comparable in power and functionality. But keep in mind Informatica is a proprietary solution, with no access to the code. You have a license fee + other costs associated to the use of Infa. Talend is an open source ETL able to perform data migration and synchronization. It's code is open source. You can download the free version of Talend, Talend Open Studio on the website. Being an open source tool, you can adapt the software to your organization and therefore spend less time and money on the development of your companies' own software. Download it here: http://www.talend.com/download.php Just my 2 cents about Talend and Informatica... -- http://mail.python.org/mailman/listinfo/python-list
Re: Global array in python
On Sep 29, 5:48 am, Rudolf yellowblueyel...@gmail.com wrote: How can i declare a global array in python? As others have mentioned, you do not have concept of declaration. But if you are looking for creating a global variable, it is like any other language. Declare the same in a module, outside any procedures or classes. But, please note that it is not completely global. You have to go inside the modules namespace to get it. So, it will be like Module A: l = [] Module B: import A A.l == This is the global variable you are looking for. To explain it further, every variable lives in a namespace. The namespace can be that of a module (which is the global thing you are looking for, I guess), or a class/object or even a procedure. There is also a global keyword etc if that is what you are looking for. Check out python documentation for more details. HTH K -- http://mail.python.org/mailman/listinfo/python-list
MySQL Matrix manipulation in Python
Hello Everybody, My doubt is about matrix data manipulation in python - I hope someone can point me some direction. I googled around, but there is not much good examples about playing with matrix in python on internet. My following function works pretty well, and gives me the outup from my MySQL db: * Code: def employee(self, verifyName): runQuery= = %s self.cursor.execute(runQuery,(verifyName,)) for row in self.cursor.fetchall(): print row print Number of lines returned: %d % self.cursor.rowcount print tell column numbers: %d % len(row) * output ('John', 'Plumber') ('Bill', 'Driver') ('Mark', 'Lawyer') Number of lines returned: 3 tell column numbers: 2 Now, how can I transfer this output to provide a matrix with a content like this: my_array = [['John','Plumber'], ['Bill','Driver'], ['Mark','Lawyer']] All comments and suggestions are highly appreciated! -- http://mail.python.org/mailman/listinfo/python-list
Re: os.listdir unwanted behaviour
Steven D'Aprano ste...@remove.this.cybersource.com.au (SD) wrote: SD What you describe is impossible -- os.listdir() returns an ordinary list, SD it isn't a lazy iterator that updates automatically as the directory SD changes. (At least not in Python2.5 -- I haven't checked Python 3.1.) He's not using Python3, see the print statement and the file function. But even with the appropriate changes the behaviour will be the same in 3.1 as in 2.x. -- Piet van Oostrum p...@cs.uu.nl URL: http://pietvanoostrum.com [PGP 8DAE142BE17999C4] Private email: p...@vanoostrum.org -- http://mail.python.org/mailman/listinfo/python-list
Re: SQLObject
On Sep 28, 9:41 pm, Daniel Fetchinson fetchin...@googlemail.com wrote: I'm new to using SQLObject, and having some problems with getting it to recognise my current MySQL database. I've set up my connection fine, but it won't recognise the names of the columns (presumably because they're not written using the default naming convention?). For example, one of my columns is an acronym, so is 3 uppercase letters. I've tried the following: class Table1(sqlobject.SQLObject): _connection = conn _fromDatabase = True class sqlmeta: table = 'Table1' idName = 'Table1ID' BOB = StringCol() print Table1.get(1) this gives the result Unknown column 'bo_b' in 'field list' So, specifically a few questions: I've seen the attribute in class sqlmeta of 'columns' - will this find my column names automatically, or do I still need to input them manually? If the latter..: I assume I set the names of each column in the instance of sqlmeta (like I have done with the table name) - how do I do this?! Do I do this before or after I've told SQLObject that the BOB column is a String column? Is there a published list of the default naming convention that SQLObject follows? I couldn't find it on the website. The current maintainer of sqlobject is Oleg Broytmann and he frequently answers support questions on the sqlobject mailing list so it's best to ask questions such as these over there:https://lists.sourceforge.net/lists/listinfo/sqlobject-discuss HTH, Daniel -- Psss, psss, put it down! -http://www.cafepress.com/putitdown Thanks for this - have reposted there. Chris -- http://mail.python.org/mailman/listinfo/python-list
Storing a C pointer in a Python class instance
Hello From my C extension module I want to store a C pointer in a given PyObject. The only way I figure how to do it is to use Py_BuildValues and store the poiner casted to Py_ssize_t, thus: Py_BuildValues(n, (Py_ssize_t)my_ptr) Can it be done differently? Regards, Elias -- http://mail.python.org/mailman/listinfo/python-list
Re: Passing tuples of tuples to sqlite
On 28 Sep, 17:49, Dennis Lee Bieber wlfr...@ix.netcom.com wrote: On Mon, 28 Sep 2009 03:35:44 -0700 (PDT), xera121 lolmcbr...@googlemail.com declaimed the following in gmane.comp.python.general: Hi I have tuples in the format shown below: (u('2','3','4'),u('5','6','7')u('20','21','22')) What are they? The syntax shown is that of a function named u taking three string arguments, not a tuple. but they are not being accepted into sqlite - due I think to the excessive quote marks which are confusing the issue when converting to a string to send it to sqlite. Can someone advise me on how I should format the strings so the whole tuple of tuples will be accepted in the sqlite insert query? It would help to know the SQL statement you are trying to use to insert the data. Note that in proper relational databases, repeated groups are not permitted, so your tuple should be split into three discrete columns (call them t0, t1, t2 G). insert into table (..., t0, t1, t2, ...) values (..., ?, ?, ?, ...) and invoked as cur.execute(theSQL, (..., tuple[0], tuple[1], tuple[2], ...) ) letting the DB-API adapter handle the proper quoting/conversion. On retrieval, you'd then have to recreate the tuple from the components. -- Wulfraed Dennis Lee Bieber KD6MOG wlfr...@ix.netcom.com HTTP://wlfraed.home.netcom.com/ The SQL is not very mysterious: The sqlite database table is created as follows: CREATE TABLE my_table (mytuples TEXT,myInteger INTEGER); INSERT INTO table VALUES(mytuples,anInteger) What I'm after as an end point is for each row in the db to have a unique entry of tuples and it just occurred to me after looking at the sqlite3 module documentation in python is that I can do an MD5 sum of the tuples and have that as a INTEGER PRIMARY KEY field. I could then split my list of tuples up into individual fields for each tuple which would make it bulky to look at on screen but totally workable in the retrieval/testing for uniqueness that I want to my with my data. Also as an aside the u is not a call to a function it stands for unicode and is put there by Python itself to denote the string is in unicode format. I'm going to do trials with the MD5 thing to prove it out. Thanks for your input, Lol Mc -- http://mail.python.org/mailman/listinfo/python-list
Re: MySQL Matrix manipulation in Python
Threader Slash wrote: Hello Everybody, My doubt is about matrix data manipulation in python - I hope someone can point me some direction. I googled around, but there is not much good examples about playing with matrix in python on internet. My following function works pretty well, and gives me the outup from my MySQL db: * Code: def employee(self, verifyName): runQuery= = %s self.cursor.execute(runQuery,(verifyName,)) for row in self.cursor.fetchall(): print row print Number of lines returned: %d % self.cursor.rowcount print tell column numbers: %d % len(row) * output ('John', 'Plumber') ('Bill', 'Driver') ('Mark', 'Lawyer') Number of lines returned: 3 tell column numbers: 2 Now, how can I transfer this output to provide a matrix with a content like this: my_array = [['John','Plumber'], ['Bill','Driver'], ['Mark','Lawyer']] All comments and suggestions are highly appreciated! my_array = [list(row) for row in self.cursor.fetchall()] -- http://mail.python.org/mailman/listinfo/python-list
Re: os.listdir unwanted behaviour
Piet van Oostrum wrote: Steven D'Aprano ste...@remove.this.cybersource.com.au (SD) wrote: SD What you describe is impossible -- os.listdir() returns an ordinary list, SD it isn't a lazy iterator that updates automatically as the directory SD changes. (At least not in Python2.5 -- I haven't checked Python 3.1.) He's not using Python3, see the print statement and the file function. But even with the appropriate changes the behaviour will be the same in 3.1 as in 2.x. I think Steven may be remembering the conversation here on c.l.p a month or two back where folks were asking to turn os.listdir() into an iterator (or create an os.xlistdir() or os.iterdir() function) because directories with lots of files were causing inordinate slowdown. Yes, listdir() in both 2.x and 3.x both return lists while such a proposed iterator version could be changed on the fly by interim file/directory creation. -tkc -- http://mail.python.org/mailman/listinfo/python-list
Re: Global array in python
Rudolf wrote: How can i declare a global array in python? import __builtin__ __builtin__.myList = [] Now you can use myList everywhere in your code. Note that modifying __builtin__ is **not** recommended at all. I don't have time to detail that point, google it if you want the answer. I warned you :o) JM -- http://mail.python.org/mailman/listinfo/python-list
different behaviour for user defined exception with attribute args
Hi all For an exception defined as below class OptionError(Exception): def __init__(self, args): self.args = args def __str__(self): return repr(self.v) an iteration is happening when the exception is raised Meanwhile for almost the same structured exception replacing the attribute 'args' with say 'value' it is not a probs. class OptionError(Exception): def __init__(self, args): self.value = args def __str__(self): return repr(self.value) This was frustrating because for a st. OptionError('Error') for exception 1 output will be OptionError: ('E', 'r', 'r', 'o', 'r') Meanwhile for exception 2 output will be OptionError: 'Error' which is desired..Why this behaviour? Regards Visco -- http://mail.python.org/mailman/listinfo/python-list
Re: different behaviour for user defined exception with attribute args
Visco Shaun wrote: Hi all For an exception defined as below class OptionError(Exception): def __init__(self, args): self.args = args This should read: def __init__(self, *args): self.args = args (notice the * !) self.args holds the constructor argument tuple or list, not a single value. def __str__(self): return repr(self.v) an iteration is happening when the exception is raised Meanwhile for almost the same structured exception replacing the attribute 'args' with say 'value' it is not a probs. class OptionError(Exception): def __init__(self, args): self.value = args def __str__(self): return repr(self.value) This was frustrating because for a st. OptionError('Error') for exception 1 output will be OptionError: ('E', 'r', 'r', 'o', 'r') Meanwhile for exception 2 output will be OptionError: 'Error' which is desired..Why this behaviour? -- Marc-Andre Lemburg eGenix.com Professional Python Services directly from the Source (#1, Sep 29 2009) Python/Zope Consulting and Support ...http://www.egenix.com/ mxODBC.Zope.Database.Adapter ... http://zope.egenix.com/ mxODBC, mxDateTime, mxTextTools ...http://python.egenix.com/ ::: Try our new mxODBC.Connect Python Database Interface for free ! eGenix.com Software, Skills and Services GmbH Pastor-Loeh-Str.48 D-40764 Langenfeld, Germany. CEO Dipl.-Math. Marc-Andre Lemburg Registered at Amtsgericht Duesseldorf: HRB 46611 http://www.egenix.com/company/contact/ -- http://mail.python.org/mailman/listinfo/python-list
Re: variable scope
Mark Dickinson a écrit : On Sep 28, 9:37 am, Bruno Desthuilliers bruno. 42.desthuilli...@websiteburo.invalid wrote: Joel Juvenal Rivera Rivera a écrit : Yeah i forgot the self an try the code then i see an error that it was not defines _uno__a so that's where i define the global and see that behavior. (snip) Joel Juvenal Rivera Rivera wrote: Hi i was playing around with my code the i realize of this ### _uno__a = 1 class uno(): __a = 2 def __init__(self): print __a uno() ### and prints 1 Looks like a bug to me. I Think you should fill a ticket... I don't think it's a bug. Unless I'm missing something, it's the 'names in class scope are not accessible' gotcha, I would of course expect the class level name __a to be unreachable from within __init__. What puzzle me is that local name __a (*not* self.__a) in the __init__ resolves to the module level name _uno__a. The double underscores and name mangling are a red herring: I beg to disagree. The problem (well... what I think is a problem, actually) IS that name mangling is applied to a method *local* variable. -- http://mail.python.org/mailman/listinfo/python-list
Re: variable scope
On Sep 29, 11:11 am, Bruno Desthuilliers bruno. 42.desthuilli...@websiteburo.invalid wrote: Mark Dickinson a écrit : On Sep 28, 9:37 am, Bruno Desthuilliers bruno. 42.desthuilli...@websiteburo.invalid wrote: Looks like a bug to me. I Think you should fill a ticket... I don't think it's a bug. Unless I'm missing something, it's the 'names in class scope are not accessible' gotcha, I would of course expect the class level name __a to be unreachable from within __init__. What puzzle me is that local name __a (*not* self.__a) in the __init__ resolves to the module level name _uno__a. Ah. So I was, of course, missing something. :) Apologies. The double underscores and name mangling are a red herring: I beg to disagree. The problem (well... what I think is a problem, actually) IS that name mangling is applied to a method *local* variable. Hmm. The description of name mangling at: http://docs.python.org/reference/expressions.html#atom-identifiers indicates (if I'm reading it right) that this behaviour is at least intentional: it appears that name mangling occurs at an early stage of source processing. Yet another reason to avoid name mangling Mark -- http://mail.python.org/mailman/listinfo/python-list
Restarting IDLE without closing it
Hi I was wondering if there exists somme way to clear memory of all objects created during a current IDLE session (with the same effect as if one starts an IDLE session). Thanks. -- http://mail.python.org/mailman/listinfo/python-list
How to use variable in a file name
if myFile variable contains the actual filename then how can i open a file for wirte so that complete filename appears as actualname_kks.txt -- http://mail.python.org/mailman/listinfo/python-list
Re: How to use variable in a file name
On Tue, Sep 29, 2009 at 3:35 PM, kks kkis.thek...@gmail.com wrote: if myFile variable contains the actual filename then how can i open a file for wirte so that complete filename appears as actualname_kks.txt open(myFile + _kks.txt, w) -- André Engels, andreeng...@gmail.com -- http://mail.python.org/mailman/listinfo/python-list
Re: Using String for new List name
On Monday, 28 September 2009 18:54:09 Scott wrote: I am new to Python but I have studied hard and written a fairly big (to me) script/program. I have solved all of my problems by Googling but this one has got me stumped. I want to check a string for a substring and if it exists I want to create a new, empty list using that substring as the name of the list. For example: Let's say file1 has line1 through line100 as the first word in each line. for X in open(file1): Do a test. If true: Y = re.split( , X) Z = Y[0] # This is a string, maybe it is Line42 Z = [] # This doesn't work, I want a new, empty list created called Line42 not Z. Is there any way to do this? Yes Look at exec and eval But also look at using the string as a key in a dict. - Hendrik -- http://mail.python.org/mailman/listinfo/python-list
Re: Using String for new List name
Hendrik van Rooyen wrote: On Monday, 28 September 2009 18:54:09 Scott wrote: I am new to Python but I have studied hard and written a fairly big (to me) script/program. I have solved all of my problems by Googling but this one has got me stumped. I want to check a string for a substring and if it exists I want to create a new, empty list using that substring as the name of the list. For example: Let's say file1 has line1 through line100 as the first word in each line. for X in open(file1): Do a test. If true: Y = re.split( , X) Z = Y[0] # This is a string, maybe it is Line42 Z = [] # This doesn't work, I want a new, empty list created called Line42 not Z. Is there any way to do this? Yes Look at exec and eval Look. But don't touch ;) -- http://mail.python.org/mailman/listinfo/python-list
Re: Idiom for last word in a string
Grant Edwards wrote: I recently ran across this construct for grabbing the last (whitespace delimited) word in a string: s.rsplit(None,1)[1] ... I've always done this: s.split()[-1] I was wondering what the advantage of the rsplit(None,1)[1] approach would be ... Others have pointed out the efficiency reason (asking the machine to do a pile of work that you intend to throw away). But nobody warned you: s.rsplit(None, 1)[-1] would be better in the case of 'single_word'.rsplit(None, 1) --Scott David Daniels scott.dani...@acm.org -- http://mail.python.org/mailman/listinfo/python-list
Re: Using String for new List name
On Sep 28, 7:37 pm, Scott scott.freem...@gmail.com wrote: On Sep 28, 2:00 pm, Dave Angel da...@ieee.org wrote: Scott wrote: Thank you fine folks for getting back with your answers! So down the road I do dictname[line42].append(new stuff). (or [var] if I'm looping through the dict) Nope, you still haven't gotten it. Of course, I really don't know where you're going wrong, since you didn't use the same symbols as any of the responses you had gotten. I suspect that you meant dictname[] to be the dictionary that Duncan called values[]. On that assumption, in order to append, you'd want something like: values[line42].append(new stuff) or values[var].append(new stuff) if you happen to have a variable called var with a value of line42. You will need to get a firm grasp on the distinctions between symbol names, literals, and values. And although Python lets you blur these in some pretty bizarre ways, you haven't a chance of understanding those unless you learn how to play by the rules first. I'd suggest your first goal should be to come up with better naming conventions. And when asking questions here, try for more meaningful data than Line42 to make your point. Suppose a text file called customers.txt has on each line a name and some data. We want to initialize an (empty) list for each of those customers, and refer to it by the customer's name. At first glance we might seem to want to initialize a variable for each customer, but our program doesn't know any of the names ahead of time, so it's much better to have some form of collection. We choose a dictionary. transactions = {} with open(customers.txt) as infile: for line in infile: fields = line.split() customername = fields[0] #customer is first thing on the line transactions[customername] = [] #this is where we'll put the transactions at some later point, for this customer Now, if our program happens to have a special case for a single customer, we might have in our program something like: transactions[mayor].append(boots) But more likely, we'll be in a loop, working through another file: . for line in otherfile: fields = line.split() customername = fields[0] transaction = fields[1] transactions[customername].append(transaction) #append one transaction or interacting: name = raw_input(Customer name) trans = raw_input(transaction for that customer) transactions[name].append(trans) Dave, I'm amazed at everyone's willingness to share and teach! I will sure do the same once I have the experience. I think that one of the problems here is that I tried to make my initial question as bone simple as possible. When I first tried to explain what I was doing I was getting up to 2 pages and I thought I bet these folks don't need to read my program. They probably just need to know the one bit I'm looking for. So I deleted it all and reduced it to the 10 line example that I posted. It was then suggested that I eschew using regular expressions when not required because I used Y = re.split( , X) in my example. In my program it is actually aclLs = re.split(\s|:|/, aclS) which I think requires a regex. I just didn't want anyone to waste their time parsing the regex when it was not really germane to my actual question. The same applies to the suggestion for using meaningful variables. In the above aclLs represents (to me) access control list List-Split and aclS represents access control list String. Again, I thought X and Y, like foo and bar or spam and eggs would do for a simple example. Of course I then went and forgot the quotes around line42 and really looked confused. I was so excited to have an answer that I typed the reply without thinking it through. Not good. Don't worry though, I take no offense. I understand and welcome the advice. I don't have anyone to work with and this post is my first interaction with any person who knows programming and Python. I am but a network engineer (Cisco, Lan/Wan, firewalls, security, monitoring (this is the connection), etc.) who has never programmed. I will work on clearer communications in future posts. I'm happy for a chance to share what I am actually trying to accomplish here. I have a firewall with a static list of access-control-list (ACL) rules (about 500 rules). I also have a directory with one week of syslog output from the firewall. About 100 text files that are each about 10 to 30 MB in size. My quest, if you will, is to create a list of syslog entries, each representing a successful network connection, with each syslog entry listed under the access-list rule that allowed it. Since ACL rules can be written with a range of granularity, i.e. loose or tight, with or without Port Number, etc., their order is
Re: os.listdir unwanted behaviour
On Tuesday, 29 September 2009 11:03:17 Tim Chase wrote: I think Steven may be remembering the conversation here on c.l.p a month or two back where folks were asking to turn os.listdir() into an iterator (or create an os.xlistdir() or os.iterdir() function) because directories with lots of files were causing inordinate slowdown. Yes, listdir() in both 2.x and 3.x both return lists while such a proposed iterator version could be changed on the fly by interim file/directory creation. Is os.walk not the right thing to use for this kind of stuff? - Hendrik -- http://mail.python.org/mailman/listinfo/python-list
Re: variable scope
On Sep 29, 3:11 am, Bruno Desthuilliers bruno. 42.desthuilli...@websiteburo.invalid wrote: Mark Dickinson a écrit : On Sep 28, 9:37 am, Bruno Desthuilliers bruno. 42.desthuilli...@websiteburo.invalid wrote: Joel Juvenal Rivera Rivera a écrit : Yeah i forgot the self an try the code then i see an error that it was not defines _uno__a so that's where i define the global and see that behavior. (snip) Joel Juvenal Rivera Rivera wrote: Hi i was playing around with my code the i realize of this ### _uno__a = 1 class uno(): __a = 2 def __init__(self): print __a uno() ### and prints 1 Looks like a bug to me. I Think you should fill a ticket... I don't think it's a bug. Unless I'm missing something, it's the 'names in class scope are not accessible' gotcha, I would of course expect the class level name __a to be unreachable from within __init__. What puzzle me is that local name __a (*not* self.__a) in the __init__ resolves to the module level name _uno__a. The double underscores and name mangling are a red herring: I beg to disagree. The problem (well... what I think is a problem, actually) IS that name mangling is applied to a method *local* variable. It's not (__a is a global), and I'll opine that the behavior is more consistent and more easily explained the way it is. Consider the following: import foo class uno(object): def __init__(self): __bar = 1 foo.__bar = 1 Now you have to explain why __bar would be name-mangled when set in the foo module, but not when set in the current module. Carl Banks -- http://mail.python.org/mailman/listinfo/python-list
Re: Restarting IDLE without closing it
candide wrote: Hi I was wondering if there exists somme way to clear memory of all objects created during a current IDLE session (with the same effect as if one starts an IDLE session). Thanks. Different than Shell / Restart Shell (Ctrl+F6) ? Of course this doesn't work if you started Idle ith the -n switch. -- http://mail.python.org/mailman/listinfo/python-list
Split string but ignore quotes
I'm attempting to reformat an apache log file that was written with a custom output format. I'm attempting to get it to w3c format using a python script. The problem I'm having is the field-to-field matching. In my python code I'm using split with spaces as my delimiter. But it fails when it reaches the user agent because that field itself contains spaces. But that user agent is enclosed with double quotes. So is there a way to split on a certain delimiter but not to split within quoted words. i.e. a line might look like 2009-09-29 12:00:00 - GET / Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 6.0; GTB5; SLCC1; .NET CLR 2.0.50727; Media Center PC 5.0; .NET CLR 3.0.04506; .NET CLR 3.5.21022) http://somehost.com 200 1923 1360 31715 - -- http://mail.python.org/mailman/listinfo/python-list
Re: variable scope
Carl Banks a écrit : On Sep 29, 3:11 am, Bruno Desthuilliers bruno. 42.desthuilli...@websiteburo.invalid wrote: Mark Dickinson a écrit : (snip) The double underscores and name mangling are a red herring: I beg to disagree. The problem (well... what I think is a problem, actually) IS that name mangling is applied to a method *local* variable. It's not (__a is a global), Hmmm, yes, indeed. and I'll opine that the behavior is more consistent and more easily explained the way it is. As long as this behaviour is known and intentional, I won't discuss the point any further as far as I'm concerned !-) -- http://mail.python.org/mailman/listinfo/python-list
Re: Split string but ignore quotes
2009/9/29 Scooter slbent...@gmail.com: I'm attempting to reformat an apache log file that was written with a custom output format. I'm attempting to get it to w3c format using a python script. The problem I'm having is the field-to-field matching. In my python code I'm using split with spaces as my delimiter. But it fails when it reaches the user agent because that field itself contains spaces. But that user agent is enclosed with double quotes. So is there a way to split on a certain delimiter but not to split within quoted words. i.e. a line might look like 2009-09-29 12:00:00 - GET / Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 6.0; GTB5; SLCC1; .NET CLR 2.0.50727; Media Center PC 5.0; .NET CLR 3.0.04506; .NET CLR 3.5.21022) http://somehost.com 200 1923 1360 31715 - Try shlex: import shlex s = '2009-09-29 12:00:00 - GET / Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 6.0; GTB5; SLCC1; .NET CLR 2.0.50727; Media Center PC 5.0; .NET CLR 3.0.04506; .NET CLR 3.5.21022) http://somehost.com 200' shlex.split(s) ['2009-09-29', '12:00:00', '-', 'GET', '/', 'Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 6.0; GTB5; SLCC1; .NET CLR 2.0.50727; Media Center PC 5.0; .NET CLR 3.0.04506; .NET CLR 3.5.21022)', 'http://somehost.com', '200'] -- mvh Björn -- http://mail.python.org/mailman/listinfo/python-list
Re: create a class instance from C API?
Thanks Carl, that does it! -- Elias Carl Banks pavlovevide...@gmail.com wrote in message news:48ce343a-36ef-406f-bea3-851444785...@b18g2000vbl.googlegroups.com... On Sep 28, 8:19 am, lallous lall...@lgwm.org wrote: Hello How to programmatically create a class instance of a given Python class? For example to create a new list there is the PyObject *PyList_New() but suppose the user already defined a class: class X: pass How to create an instance of it from my C extension module? Same way you'd do it in Python: call it. Use PyObject_Call or any of it's convenient variants. Example (leaving off all the error-checking stuff): mod = PyImport_ImportModule(modname); cls = PyObject_GetAttrStr(mod,classname); inst = PyObject_CallFunctionObjArgs(cls,NULL); Carl Banks -- http://mail.python.org/mailman/listinfo/python-list
print object attributes recursively
Hello Suppose I have this code: class X: def __init__(self, n): self.L = [x for x in xrange(0, n+1)] class Y: def __init__(self, n): self.M = [X(x) for x in xrange(0, n)] t = Y(5) How can I easily print t and all its nested attributes? (Something like PHP's print_r()) Thanks, Elias -- http://mail.python.org/mailman/listinfo/python-list
two html pages from twisted server
Could soneone show me a simple example of a twisted server that loads 2 html pages www..com:7899/page1/ www..com:7899/page2/ Thanx -- http://mail.python.org/mailman/listinfo/python-list
Simple if-else question
Hi all, A simple and silly if-else question. I saw some code that has the following structure. My question is why else is used there though removing else has the same result. More important, is it not syntactically wrong :-( for i in xrange(8): if i 4: print i else: print i Cheers, dksr -- http://mail.python.org/mailman/listinfo/python-list
Re: Split string but ignore quotes
Björn Lindqvist wrote: 2009/9/29 Scooter slbent...@gmail.com: I'm attempting to reformat an apache log file that was written with a custom output format. I'm attempting to get it to w3c format using a python script. The problem I'm having is the field-to-field matching. In my python code I'm using split with spaces as my delimiter. But it fails when it reaches the user agent because that field itself contains spaces. But that user agent is enclosed with double quotes. So is there a way to split on a certain delimiter but not to split within quoted words. i.e. a line might look like 2009-09-29 12:00:00 - GET / Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 6.0; GTB5; SLCC1; .NET CLR 2.0.50727; Media Center PC 5.0; .NET CLR 3.0.04506; .NET CLR 3.5.21022) http://somehost.com 200 1923 1360 31715 - Try shlex: import shlex s = '2009-09-29 12:00:00 - GET / Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 6.0; GTB5; SLCC1; .NET CLR 2.0.50727; Media Center PC 5.0; .NET CLR 3.0.04506; .NET CLR 3.5.21022) http://somehost.com 200' shlex.split(s) ['2009-09-29', '12:00:00', '-', 'GET', '/', 'Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 6.0; GTB5; SLCC1; .NET CLR 2.0.50727; Media Center PC 5.0; .NET CLR 3.0.04506; .NET CLR 3.5.21022)', 'http://somehost.com', '200'] The regex solution is: import re s = '2009-09-29 12:00:00 - GET / Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 6.0; GTB5; SLCC1; .NET CLR 2.0.50727; Media Center PC 5.0; .NET CLR 3.0.04506; .NET CLR 3.5.21022) http://somehost.com 200' re.findall(r'.*?|\S+', s) ['2009-09-29', '12:00:00', '-', 'GET', '/', 'Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 6.0; GTB5; SLCC1; .NET CLR 2.0.50727; Media Center PC 5.0; .NET CLR 3.0.04506; .NET CLR 3.5.21022)', 'http://somehost.com', '200'] -- http://mail.python.org/mailman/listinfo/python-list
Re: Simple if-else question
Sandy wrote: Hi all, A simple and silly if-else question. I saw some code that has the following structure. My question is why else is used there though removing else has the same result. More important, is it not syntactically wrong :-( for i in xrange(8): if i 4: print i else: print i Cheers, dksr else needs to be indented like if -- http://mail.python.org/mailman/listinfo/python-list
Re: Simple if-else question
Sandy wrote: Hi all, A simple and silly if-else question. I saw some code that has the following structure. My question is why else is used there though removing else has the same result. More important, is it not syntactically wrong :-( for i in xrange(8): if i 4: print i else: print i Cheers, dksr See http://docs.python.org/reference/compound_stmts.html#the-for-statement for an explanation of the for statement, and the meaning of it's else suite. In this particular instance, the else adds nothing, but there are instances where it does provide a useful functionality. Gary Herron -- http://mail.python.org/mailman/listinfo/python-list
Re: Simple if-else question
Sandy wrote: Hi all, A simple and silly if-else question. I saw some code that has the following structure. My question is why else is used there though removing else has the same result. More important, is it not syntactically wrong :-( for i in xrange(8): if i 4: print i else: print i Cheers, dksr The else is not tied to the if, it is tied to the for. The statements in a for-else (and while-else, and if-else) only execute if the control expression becomes False. If you want to avoid executing the else clause, you have to break out of the loop. Some examples: In [1]: for i in xrange(8): ...: if i 4: ...: print i ...: 0 1 2 3 In [2]: for i in xrange(8): ...: if i 4: ...: print i ...: else: ...: print i ...: 0 1 2 3 7 In [3]: for i in xrange(8): ...: if i 4: ...: print i ...: if i == 1: ...: break ...: else: ...: print i ...: 0 1 Hope this helps! ~Ethan~ -- http://mail.python.org/mailman/listinfo/python-list
Re: Storing a C pointer in a Python class instance
On Sep 29, 2:27 am, lallous lall...@lgwm.org wrote: Hello From my C extension module I want to store a C pointer in a given PyObject. The only way I figure how to do it is to use Py_BuildValues and store the poiner casted to Py_ssize_t, thus: Py_BuildValues(n, (Py_ssize_t)my_ptr) Can it be done differently? Regards, Elias You can use a PyCObject_FromVoidPtr http://docs.python.org/c-api/cobject.html PyArg_ParseTuple(args, O, pyVoidPointer); castPointer = (type *) PyCObject_AsVoidPtr(pyVoidPointer); return PyCObject_FromVoidPtr((void *) castPointer, NULL); -- http://mail.python.org/mailman/listinfo/python-list
Cannot get POST to work
Hi, I'm trying to post data to a short test script in php I wrote. The python code to do the post is import httplib #server address conn = httplib.HTTPConnection(localhost) #file location conn.request(POST, /programming/bots/test.php,ted=fred) r1 = conn.getresponse() print r1.status, r1.reason data1 = r1.read() print data1 conn.close() print new ok The PHP script is printhello br; print $_POST[ted]; Ted post -- http://mail.python.org/mailman/listinfo/python-list
Re: Cannot get POST to work
On Sep 29, 10:24 am, tedpot...@gmail.com tedpot...@gmail.com wrote: Hi, I'm trying to post data to a short test script in php I wrote. The python code to do the post is import httplib #server address conn = httplib.HTTPConnection(localhost) #file location conn.request(POST, /programming/bots/test.php,ted=fred) r1 = conn.getresponse() print r1.status, r1.reason data1 = r1.read() print data1 conn.close() print new ok The PHP script is printhello br; print $_POST[ted]; Ted post I can't speak to what is wrong with your current script - instead I would recommend the higher level urllib libraries: (Untested) import urllib2, urllib response = urllib2.open(http://localhost/programming/bots/test.php;, urllib.urlencode({ted: fred})) print response.read() response.close() ~Garrick -- http://mail.python.org/mailman/listinfo/python-list
How to pass a global variable to a module?
Dear Python users, I just start to use python and love this language. I met this problem when I try to save my functions in a separate file. The question is how I can pass a global variable to a function which is saved in another file. If I save the function I defined in the same file with the main program, there is no problem after I declare the global variable. But problem comes out when I save all the function is a separate file. Help is very much appreciated! Thanks! Jinbo -- http://mail.python.org/mailman/listinfo/python-list
Re: Storing a C pointer in a Python class instance
On 29 Sep, 10:27, lallous lall...@lgwm.org wrote: Hello From my C extension module I want to store a C pointer in a given PyObject. The only way I figure how to do it is to use Py_BuildValues and store the poiner casted to Py_ssize_t, Formally, you should cast the pointer to Py_intptr_t, as it has the same size as void*. Py_ssize_t has the same size as size_t, but the C standard does not mandate that sizeof(void*) == sizeof(size_t). In fact there are segment and offset architectures where this is not true. Casting a pointer to Py_ssize_t accidentally works if you have a flat address space. Can it be done differently? You can use PyCObject, or write your own extension type that wraps the pointer (very easy to to with Cython or Pyrex). The advantage of using an extension type is you have a guarantee from Python on the deallocator method being called (cdef __dealloc__ in Cython). If the pointer references a resource that needs to be closed, this is safer than using a __del__ method in a Python class. -- http://mail.python.org/mailman/listinfo/python-list
Re: Simple if-else question
If I'm reading the indentation correctly, the else is applying to the for loop, not the if statement. When used in this way, the else occurs only if the for loop exits due to completion (aka, the for loop does not exit due to a break or return statement). I would expect the output from that code to be: 0 1 2 3 7 Any values in the range under 4 should be printed (the if), as well as the last value in the range (7 in the case given). Chris On Tue, Sep 29, 2009 at 8:50 AM, Sandy dksre...@gmail.com wrote: Hi all, A simple and silly if-else question. I saw some code that has the following structure. My question is why else is used there though removing else has the same result. More important, is it not syntactically wrong :-( for i in xrange(8): if i 4: print i else: print i Cheers, dksr -- http://mail.python.org/mailman/listinfo/python-list -- http://mail.python.org/mailman/listinfo/python-list
Re: How to pass a global variable to a module?
Hi, Am Tue, 29 Sep 2009 09:40:29 -0700 (PDT) schrieb Mars creature jin...@gmail.com: I just start to use python and love this language. I met this problem when I try to save my functions in a separate file. The question is how I can pass a global variable to a function which is saved in another file. If I save the function I defined in the same file with the main program, there is no problem after I declare the global variable. But problem comes out when I save all the function is a separate file. Help is very much appreciated! Thanks! Jinbo http://mail.python.org/pipermail/tutor/2002-November/018353.html -- Gregor http://gregor-horvath.com -- http://mail.python.org/mailman/listinfo/python-list
Re: Storing a C pointer in a Python class instance
On Sep 29, 9:42 am, sturlamolden sturlamol...@yahoo.no wrote: You can use PyCObject, or write your own extension type that wraps the pointer (very easy to to with Cython or Pyrex). The advantage of using an extension type is you have a guarantee from Python on the deallocator method being called (cdef __dealloc__ in Cython). CObjects can be passed a C function as a deallocator; this should work as reliably as a custom class deallocator. Carl Banks -- http://mail.python.org/mailman/listinfo/python-list
Re: os.listdir unwanted behaviour
a month or two back where folks were asking to turn os.listdir() into an iterator (or create an os.xlistdir() or os.iterdir() function) because directories with lots of files were causing inordinate slowdown. Yes, listdir() in both 2.x and 3.x both return lists while such a proposed iterator version could be changed on the fly by interim file/directory creation. Is os.walk not the right thing to use for this kind of stuff? Behind the scenes os.walk() calls listdir() which has the same problems in directories with large files. But yes, I believe there was discussion in that thread of having a generator that behaved like os.walk() but called the proposed xlistdir() or iterdir() function instead. However, no such beast exists yet (in stock Python). -tkc -- http://mail.python.org/mailman/listinfo/python-list
Re: Simple if-else question
On Sep 29, 9:08 am, Gary Herron gher...@islandtraining.com wrote: Sandy wrote: Hi all, A simple and silly if-else question. I saw some code that has the following structure. My question is why else is used there though removing else has the same result. More important, is it not syntactically wrong :-( for i in xrange(8): if i 4: print i else: print i Cheers, dksr Seehttp://docs.python.org/reference/compound_stmts.html#the-for-statement for an explanation of the for statement, and the meaning of it's else suite. In this particular instance, the else adds nothing, but there are instances where it does provide a useful functionality. Hmm, I wonder if Python should emit a warning if an else is used on a for block with no break inside. I don't think the else can be invoked in any other way. As a bonus it could catch some cases where people mistakenly use it thinking it will execute when there are no iterations. Carl Banks -- http://mail.python.org/mailman/listinfo/python-list
Re: How python source code in large projects are organized?
On Sep 20, 8:19 am, Peng Yu pengyu...@gmail.com wrote: I am wondering what is the best way of organizing python source code in a large projects. There are package code, testing code. I'm wondering if there has been any summary on previous practices. (Sorry for the late reply.) My advice: Don't write big projects. Not that Python can't do it, but because it's a bad idea in general. Instead, write lots of small, independent projects that have well-defined, simple interfaces and work together. First step is to map out what components you have and what their interfaces are. (The interface is what functions and classes exist and what their behavior is.) Nail this down early on. Try to reuse components from other projects. If something exists but it isn't up to par with what you need, adapt your interface requirements or consider contributing to the project. Next step is to build your scaffolding. For each component you'll be building, build up the interface and leave out the implementation. If possible, put in something reasonable but incorrect. For instance: def twice(x): return 1 If that's not possible: def twice(x): raise NotImplementedError (If you think of 'twice' as something complicated like 'standard_deviation', then you'll appreciate this more.) Next, get the stack working. Although it will be incorrect, it should do something visible. Finally, flesh out the scaffolding by replacing the wrong implementations with correct ones, working from basic components to higher level components. Keep the stack working at all times. When writing unit tests, test only the code in the component and not code from another component. The structure of individual components or modules should be standard, not just for your project but for the vast majority of projects out there. Use paster to get your code setup. This will give you a good standard template to work from. Create a virtualenv to get your working environment. Then run python setup.py develop from each of the modules to install them in the working environment. If you have the dependencies mapped out, it should install all the stuff you need. In the end, you should get something that looks more like Pylons and less like Django. Projects built in this way tend to have more replaceable components and the components tend to be more useful outside of the project. That means you write less code and get more work done. -- http://mail.python.org/mailman/listinfo/python-list
Re: Simple if-else question
Ethan Furman wrote: Sandy wrote: Hi all, A simple and silly if-else question. I saw some code that has the following structure. My question is why else is used there though removing else has the same result. More important, is it not syntactically wrong :-( for i in xrange(8): if i 4: print i else: print i Cheers, dksr The else is not tied to the if, it is tied to the for. The statements in a for-else (and while-else, and if-else) only execute if the control expression becomes False. If you want to avoid executing the else clause, you have to break out of the loop. Some examples: In [1]: for i in xrange(8): ...: if i 4: ...: print i ...: 0 1 2 3 In [2]: for i in xrange(8): ...: if i 4: ...: print i ...: else: ...: print i ...: 0 1 2 3 7 In [3]: for i in xrange(8): ...: if i 4: ...: print i ...: if i == 1: ...: break ...: else: ...: print i ...: 0 1 Hope this helps! The example that makes it clearest for me is searching through a list for a certain item and breaking out of the 'for' loop if I find it. If I get to the end of the list and still haven't broken out then I haven't found the item, and that's when the else statement takes effect: for item in my_list: if item == desired_item: print Found it! break else: print It's not in the list -- http://mail.python.org/mailman/listinfo/python-list
Re: How to pass a global variable to a module?
Mars creature wrote: Dear Python users, I just start to use python and love this language. I met this problem when I try to save my functions in a separate file. The question is how I can pass a global variable to a function which is saved in another file. If I save the function I defined in the same file with the main program, there is no problem after I declare the global variable. But problem comes out when I save all the function is a separate file. Help is very much appreciated! Thanks! Jinbo Do not use global variable, that's evil ! in file1.py: myVar = 'foo' in file2.py: import file1 print file1.myVar 'foo' file1.myVar = 'bar' print file1.myVar 'bar' Keep your variables ordered on their shelf. JM -- http://mail.python.org/mailman/listinfo/python-list
Re: Simple if-else question
Carl Banks pavlovevide...@gmail.com wrote: Hmm, I wonder if Python should emit a warning if an else is used on a for block with no break inside. I don't think the else can be invoked in any other way. As a bonus it could catch some cases where people mistakenly use it thinking it will execute when there are no iterations. It will execute when there are no iterations. Did you mean to say people think it will execute *only* when there are no iterations? -- http://mail.python.org/mailman/listinfo/python-list
Re: How to pass a global variable to a module?
On Tue, 29 Sep 2009 09:40:29 -0700, Mars creature jin...@gmail.com wrote: Dear Python users, I just start to use python and love this language. I met this problem when I try to save my functions in a separate file. The question is how I can pass a global variable to a function which is saved in another file. If I save the function I defined in the same file with the main program, there is no problem after I declare the global variable. But problem comes out when I save all the function is a separate file. Help is very much appreciated! Thanks! Jinbo In Python, as in many other languages, I'd advise that you think about whether your variable needs to be global, or whether you could (or should) simply pass the variable to the function as a parameter. HTH, Rami -- Rami Chowdhury Never attribute to malice that which can be attributed to stupidity -- Hanlon's Razor 408-597-7068 (US) / 07875-841-046 (UK) / 0189-245544 (BD) -- http://mail.python.org/mailman/listinfo/python-list
Remote uptime with win32pdh
I'm new to python and I'm trying to write a script which takes the computer name from the variable 'name' and gets the uptime. What I have so far: query = win32pdh.OpenQuery() counter = win32pdh.AddCounter(query, r'\System\System Up Time') win32pdh.CollectQueryData(query) var1, val = win32pdh.GetFormattedCounterValue(counter, win32pdh.PDH_FMT_LONG) uptime = val / 3600 print -- Uptime: %s hours % uptime That works just fine for the local system, but I'm at a loss figuring out how to make it gater that data from a remote system. Any help would be much appreciated! -- http://mail.python.org/mailman/listinfo/python-list
Re: Remote uptime with win32pdh
On Sep 29, 1:08 pm, Jamie jamie.iva...@gmail.com wrote: I'm new to python and I'm trying to write a script which takes the computer name from the variable 'name' and gets the uptime. What I have so far: query = win32pdh.OpenQuery() counter = win32pdh.AddCounter(query, r'\System\System Up Time') win32pdh.CollectQueryData(query) var1, val = win32pdh.GetFormattedCounterValue(counter, win32pdh.PDH_FMT_LONG) uptime = val / 3600 print -- Uptime: %s hours % uptime That works just fine for the local system, but I'm at a loss figuring out how to make it gater that data from a remote system. Any help would be much appreciated! Oh, and I can't use WMI because WMI doesn't seem to work in Portable Python (long story) -- http://mail.python.org/mailman/listinfo/python-list
Re: Storing a C pointer in a Python class instance
On 29 Sep, 19:11, Carl Banks pavlovevide...@gmail.com wrote: CObjects can be passed a C function as a deallocator; this should work as reliably as a custom class deallocator. Carl Banks Except that __del__ prevents cyclic GC. -- http://mail.python.org/mailman/listinfo/python-list
Re: How to pass a global variable to a module?
On Sep 29, 12:49 pm, Rami Chowdhury rami.chowdh...@gmail.com wrote: On Tue, 29 Sep 2009 09:40:29 -0700, Mars creature jin...@gmail.com wrote: Dear Python users, I just start to use python and love this language. I met this problem when I try to save my functions in a separate file. The question is how I can pass a global variable to a function which is saved in another file. If I save the function I defined in the same file with the main program, there is no problem after I declare the global variable. But problem comes out when I save all the function is a separate file. Help is very much appreciated! Thanks! Jinbo In Python, as in many other languages, I'd advise that you think about whether your variable needs to be global, or whether you could (or should) simply pass the variable to the function as a parameter. HTH, Rami -- Rami Chowdhury Never attribute to malice that which can be attributed to stupidity -- Hanlon's Razor 408-597-7068 (US) / 07875-841-046 (UK) / 0189-245544 (BD) Thank you guys for the prompt and helpful response. From the link Gregor posted, it seems no way to share variable between modules. I can understand the point that global variables tends to mess up programs. Assume that I have 10 parameters need to pass to the function. If these parameters are fixed, I can use another module to store these 10 parameters, and import to the module, as suggested by jean-michel. But what if these 10 parameters will be changed in the main program? Passing the variable to the function as a parameter suggested by Rami will certainly do, but I am wondering weather there are other ways. What you'd like to code it? Thank you very much! Jinbo -- http://mail.python.org/mailman/listinfo/python-list
Re: Split string but ignore quotes
On Tue, Sep 29, 2009 at 11:11 AM, Scooter slbent...@gmail.com wrote: I'm attempting to reformat an apache log file that was written with a custom output format. I'm attempting to get it to w3c format using a python script. The problem I'm having is the field-to-field matching. In my python code I'm using split with spaces as my delimiter. But it fails when it reaches the user agent because that field itself contains spaces. But that user agent is enclosed with double quotes. So is there a way to split on a certain delimiter but not to split within quoted words. i.e. a line might look like 2009-09-29 12:00:00 - GET / Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 6.0; GTB5; SLCC1; .NET CLR 2.0.50727; Media Center PC 5.0; .NET CLR 3.0.04506; .NET CLR 3.5.21022) http://somehost.com 200 1923 1360 31715 - -- http://mail.python.org/mailman/listinfo/python-list s = '''2009-09-29 12:00:00 - GET / Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 6.0; GTB5; SLCC1; .NET CLR 2.0.50727; Media Center PC 5.0; .NET CLR 3.0.04506; .NET CLR 3.5.21022) http://somehost.com 200 1923 1360 31715 -''' initial, user_agent, trailing = s.split('') # Then depending on what you want to do with them... foo = initial.split() + [user_agent] + trailing.split() -- http://mail.python.org/mailman/listinfo/python-list
Re: How to pass a global variable to a module?
Mars creature wrote: On Sep 29, 12:49 pm, Rami Chowdhury rami.chowdh...@gmail.com wrote: On Tue, 29 Sep 2009 09:40:29 -0700, Mars creature jin...@gmail.com wrote: Dear Python users, I just start to use python and love this language. I met this problem when I try to save my functions in a separate file. The question is how I can pass a global variable to a function which is saved in another file. If I save the function I defined in the same file with the main program, there is no problem after I declare the global variable. But problem comes out when I save all the function is a separate file. Help is very much appreciated! Thanks! Jinbo In Python, as in many other languages, I'd advise that you think about whether your variable needs to be global, or whether you could (or should) simply pass the variable to the function as a parameter. HTH, Rami -- Rami Chowdhury Never attribute to malice that which can be attributed to stupidity -- Hanlon's Razor 408-597-7068 (US) / 07875-841-046 (UK) / 0189-245544 (BD) Thank you guys for the prompt and helpful response. From the link Gregor posted, it seems no way to share variable between modules. I can understand the point that global variables tends to mess up programs. Assume that I have 10 parameters need to pass to the function. If these parameters are fixed, I can use another module to store these 10 parameters, and import to the module, as suggested by jean-michel. But what if these 10 parameters will be changed in the main program? Passing the variable to the function as a parameter suggested by Rami will certainly do, but I am wondering weather there are other ways. What you'd like to code it? If there are a lot of them then an alternative is to pass them in some sort of contains, such as a dict or an object: class Params(object): pass params = Params() params.x = 'foo' params.x 'foo' -- http://mail.python.org/mailman/listinfo/python-list
Re: How to use variable in a file name
On Sep 29, 9:43 am, Andre Engels andreeng...@gmail.com wrote: On Tue, Sep 29, 2009 at 3:35 PM, kks kkis.thek...@gmail.com wrote: if myFile variable contains the actual filename then how can i open a file for wirte so that complete filename appears as actualname_kks.txt open(myFile + _kks.txt, w) -- André Engels, andreeng...@gmail.com Thank You, it works -- http://mail.python.org/mailman/listinfo/python-list
Re: Remote uptime with win32pdh
On Sep 29, 1:19 pm, Jamie jamie.iva...@gmail.com wrote: On Sep 29, 1:08 pm, Jamie jamie.iva...@gmail.com wrote: I'm new to python and I'm trying to write a script which takes the computer name from the variable 'name' and gets the uptime. What I have so far: query = win32pdh.OpenQuery() counter = win32pdh.AddCounter(query, r'\System\System Up Time') win32pdh.CollectQueryData(query) var1, val = win32pdh.GetFormattedCounterValue(counter, win32pdh.PDH_FMT_LONG) uptime = val / 3600 print -- Uptime: %s hours % uptime That works just fine for the local system, but I'm at a loss figuring out how to make it gater that data from a remote system. Any help would be much appreciated! Oh, and I can't use WMI because WMI doesn't seem to work in Portable Python (long story)- Hide quoted text - - Show quoted text - path = win32pdh.MakeCounterPath((name, r'System', None, None, 0, System Up Time)) query = win32pdh.OpenQuery() handle = win32pdh.AddCounter(query, path) win32pdh.CollectQueryData(query) seconds = win32pdh.GetFormattedCounterValue(handle, win32pdh.PDH_FMT_LONG | win32pdh.PDH_FMT_NOSCALE )[ 1 ] uptime = seconds / 3600 that works! ;) -- http://mail.python.org/mailman/listinfo/python-list
Re: Simple if-else question
On Sep 29, 10:38 am, Duncan Booth duncan.bo...@invalid.invalid wrote: Carl Banks pavlovevide...@gmail.com wrote: Hmm, I wonder if Python should emit a warning if an else is used on a for block with no break inside. I don't think the else can be invoked in any other way. As a bonus it could catch some cases where people mistakenly use it thinking it will execute when there are no iterations. It will execute when there are no iterations. Did you mean to say people think it will execute *only* when there are no iterations? No but that's what I should have meant. Carl Banks -- http://mail.python.org/mailman/listinfo/python-list
Re: How to pass a global variable to a module?
Mars creature wrote: I just start to use python and love this language. I met this problem when I try to save my functions in a separate file. The question is how I can pass a global variable to a function which is saved in another file. This question is somewhat mis-phrased. In Python, one uses names -- local, nonlocal, global, and dotted -- as well as other expressions, to pass objects to functions as arguments that get bound to the parameter names of the function. Objects are neither local or global; they just are. Thinking this way will help your use of Python. If I save the function I defined in the same file with the main program, there is no problem after I declare the global variable. But problem comes out when I save all the function is a separate file. Help is very much appreciated! Thanks! Specific answers require specific examples and either a clear description of actual versus expected behavior or a complete copy of an error traceback. Good luck. tjr -- http://mail.python.org/mailman/listinfo/python-list
Re: Restarting IDLE without closing it
Scott David Daniels a écrit : Different than Shell / Restart Shell (Ctrl+F6) ? Of course this doesn't work if you started Idle ith the -n switch. Thanks for the info : by default on my Ubuntu distrib, IDLE is launched with the -n option ;) Now, all is clear ! -- http://mail.python.org/mailman/listinfo/python-list
ARP code
Hello. I have a problem can not run. I'm not understand. Help me please. How to use and detail ? Thanks. #!/usr/bin/python2.5 import py_net_libs class dissect: def __init__(self,pkt,offset): self.pkt, self.offset = py_net_libs.arp_dissect(pkt,offset) self.next = None def format(self): mac_s, mac_d, ip_src, ip_dst = '','','','' for n in (self.pkt[5]): mac_s = mac_s + '%02x:' % n for n in (self.pkt[7]): mac_d = mac_d + '%02x:' % n for n in (self.pkt[6]): ip_src = ip_src + '%d.' % n for n in (self.pkt[8]): ip_dst = ip_dst + '%d.' % n return [ 'HW Type = 0x%02x' % self.pkt[0], 'Protocol Type = 0x%x' % self.pkt[1], 'HW Address Lenght = %d' % self.pkt[2], 'Procol Address Length = %d' % self.pkt[3], 'OP code = %x' % self.pkt[4], 'Sender HW Addr = %s'% mac_s [:-1], 'Sender IP Addr = %s'% ip_src[:-1], 'Target HW Addr = %s'% mac_d [:-1], 'Target IP Addr = %s'% ip_dst[:-1] ] _ แสดงเส้นทาง! เพิ่มแผนที่และทิศทางไปสู่งานปาร์ตี้ของคุณ http://www.microsoft.com/windows/windowslive/products/events.aspx-- http://mail.python.org/mailman/listinfo/python-list
Re: setting up dynamic descriptors in Python
I created a new class for each instance as you suggested and added the descriptor to the class. It worked great. Thanks for the help. Brian Huggins -- http://mail.python.org/mailman/listinfo/python-list
Re: different behaviour for user defined exception with attribute args
Visco Shaun wrote: Hi all For an exception defined as below class OptionError(Exception): def __init__(self, args): self.args = args def __str__(self): return repr(self.v) an iteration is happening when the exception is raised snip What is self.v intended to produce? Perhaps you meant self.args ?? That doesn't explain your error though. I suspect you didn't quote your code accurately. The other problem is that args is intended to be a tuple. So you want to use *args for your formal parameter. DaveA -- http://mail.python.org/mailman/listinfo/python-list
Re: Re: variable scope
Bruno Desthuilliers wrote: (snip) Joel Juvenal Rivera Rivera wrote: Hi i was playing around with my code the i realize of this ### _uno__a = 1 class uno(): __a = 2 def __init__(self): print __a uno() ### and prints 1 snip I beg to disagree. The problem (well... what I think is a problem, actually) IS that name mangling is applied to a method *local* variable. It is not a local variable, because there's no assignment within that same method. That decision happens at compile time of the definition. Once it's not a local, then it needs to get mangled, per the rules for double-underscore. Try adding a __a = 49 before or after the line in question. Either one will change the behavior (and/or the error message) to indicate that it's a local. DaveA -- http://mail.python.org/mailman/listinfo/python-list
Serial connections and threads in a GUI
I have a wx-based GUI application that I'd like to have read streaming data from a serial port and plot that data using matplotlib and a figure canvas. It's a robotic application where the workflow goes something like this: 1) Connect to robot 2) Configure robot settings 3) Command robot to do something for time, t - While robot does something, read sensor data and plot 4) End of t seconds, resume at (2) I have the application reading from the serial port in a separate thread, but my problem is this - I'd like the user to be able to write to the serial port by interacting with the GUI between runs. However, the infinite loop running inside the thread responsible for receiving data has exclusive access to the serial connection object. Because of how the application works, I know that I won't need to write to the serial port when I'm in the middle of receiving data, so that simplifies things somewhat. But, how can I allow the thread access to the connection for reading, but then have it give up control over the connection when it's done so the main GUI thread can access the connection again for writing? Currently, I have the thread's run() method exit when it's done receiving data. But, that means that I can only receive data again using the same thread by calling run(), blocking the main GUI thread from updating the plot until it completes. This is my first go at using threads in Python, so I'm sure there's a straightforward way to do this that I'm just missing. At any rate, thanks in advance for your patience and assistance. Thanks, Aaron -- http://mail.python.org/mailman/listinfo/python-list
Re: How to pass a global variable to a module?
Mars creature wrote: snip I can understand the point that global variables tends to mess up programs. Assume that I have 10 parameters need to pass to the function. If these parameters are fixed, I can use another module to store these 10 parameters, and import to the module, as suggested by jean-michel. But what if these 10 parameters will be changed in the main program? Passing the variable to the function as a parameter suggested by Rami will certainly do, but I am wondering weather there are other ways. What you'd like to code it? Thank you very much! Jinbo If we're just talking generalities, we can give you general advice. Avoid globals like the plague. Except for constants, each global should require a lot of justification to permit its use. There's no harm in passing 10 parameters to a function. And if some of them are related to each other, group them in a tuple, or an object. If two functions seem to have a need to share data without passing it back and forth, they probably belong in a class. Most of the justifiable globals are already there in the standard libraries, or at least a good analogy. For example, stdout is used by print, wherever it occurs. Likewise you may want a global logging object. These are things which act a lot like constants, even though they have internal state. DaveA -- http://mail.python.org/mailman/listinfo/python-list
Re: Serial connections and threads in a GUI
On 29 Sep, 23:56, Aaron Hoover ahoo...@eecs.berkeley.edu wrote: how can I allow the thread access to the connection for reading, but then have it give up control over the connection when it's done so the main GUI thread can access the connection again for writing? Protect read/write access to the serial port with a lock. Or use Candygram to pass Erlang-style messages to and from the thread owning the serial port. http://candygram.sourceforge.net/ -- http://mail.python.org/mailman/listinfo/python-list
Re: How to pass a global variable to a module?
Mars creature jin...@gmail.com wrote: Assume that I have 10 parameters need to pass to the function. If these parameters are fixed, I can use another module to store these 10 parameters, and import to the module, as suggested by jean-michel. But what if these 10 parameters will be changed in the main program? With Python, for the duration of program execution a module is (generally) only ever imported by an import statement once. Any other imports will instead look up the result of the first import, and will refer to that module. So you can use modules to stash variables for the life of the program: a.py: import globals globals.a = 1 globals.b = 2 b.py: import globals globals.b = 77 c.py: import globals print globals.a, globals.b # will be '1, 77' Passing the variable to the function as a parameter suggested by Rami will certainly do, but I am wondering weather there are other ways. What you'd like to code it? While you can use globals to avoid having to pass functions around, it's generally a sign of bad code. You're tightly binding the behaviour of the function to the presence of the globals, which makes the function a lot harder to re-use elsewhere. Depending on the functions, I'd tend to use either a list or a dict: def positional_args_func(a, b, c, d, e): ... pass ... params = [1, 3, 5, 7, 9] positional_args_func(*params) # the * unpacks the list def keyword_args_func(a=None, b=None, c=None, d=None, e=None): ... pass ... params = dict(a=1, b=3, c=5, d=7, e=9) keyword_args_func(**params) # the ** unpacks the dict If you're using Python 2.6/3.x, you might find namedtuple handy: from collections import namedtuple Parameters = namedtuple('Parameters', 'a b c d e') params = Parameters(1, 3, 5, 7, 9) params Parameters(a=1, b=3, c=5, d=7, e=9) params.a, params.e (1, 9) def singular_arg_func(params): ... # access through params.a, params.b etc ... pass ... singular_arg_func(params) Or course, any combination of these can be used in the same functions. Hope this helps. -- http://mail.python.org/mailman/listinfo/python-list
Re: Split string but ignore quotes
Would the csv module be appropriate? On 9/29/09, Scooter slbent...@gmail.com wrote: I'm attempting to reformat an apache log file that was written with a custom output format. I'm attempting to get it to w3c format using a python script. The problem I'm having is the field-to-field matching. In my python code I'm using split with spaces as my delimiter. But it fails when it reaches the user agent because that field itself contains spaces. But that user agent is enclosed with double quotes. So is there a way to split on a certain delimiter but not to split within quoted words. i.e. a line might look like 2009-09-29 12:00:00 - GET / Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 6.0; GTB5; SLCC1; .NET CLR 2.0.50727; Media Center PC 5.0; .NET CLR 3.0.04506; .NET CLR 3.5.21022) http://somehost.com 200 1923 1360 31715 - -- http://mail.python.org/mailman/listinfo/python-list -- To argue that honorable conduct is only required against an honorable enemy degrades the Americans who must carry out the orders. -- Charles Krulak, Former Commandant of the Marine Corps We are all slave to our own paradigm. -- Joshua Williams If the letters PhD appear after a person's name, that person will remain outdoors even after it's started raining. -- Jeff Kay -- http://mail.python.org/mailman/listinfo/python-list
Re: two html pages from twisted server
On Sep 30, 1:30 am, Mikie thephantom6...@hotmail.com wrote: Could soneone show me a simple example of a twisted server that loads 2 html pages www..com:7899/page1/ www..com:7899/page2/ Thanx Hey Mikie, Jp Calderone has been writing a very handy 'Twisted Web in 60 seconds' series, I think part 3 on static dispatch is what you're after: http://jcalderone.livejournal.com/48795.html (I'm not a Twisted user, sorry, just been finding this series interesting...) -- http://mail.python.org/mailman/listinfo/python-list
Re: Most active coroutine library project?
On Monday, 28 September 2009 16:44:48 Grant Edwards wrote: $10 is pretty expensive for a lot of applications. I bet that processor also uses a lot of power and takes up a lot of board space. If you've only got $2-$3 in the money budget, 200uA at 1.8V in the power budget, and 6mm X 6mm of board-space, your choices are limited. Besides If you can get by with 256 or 512 bytes of RAM, why pay 4X the price for a 1K part? Besides which, the 8032 instruction set and development tools are icky compared to something like an MSP430 or an AVR. ;) [The 8032 is still head and shoulders above the 8-bit PIC family.] I am biased. I like the 8031 family. I have written pre-emptive multitasking systems for it, as well as state-machine round robin systems. In assembler. Who needs tools if you have a half decent macro assembler? And if the macro assembler is not up to much, then you write your own pre processor using python. The 803x bit handling is, in my arrogant opinion, still the best of any processor. - jump if bit set then clear as an atomic instruction rocks. :-) Where do you get such nice projects to work on? - Hendrik -- http://mail.python.org/mailman/listinfo/python-list
Partial directory search question
I have a directory that contains the following login rc sum _1 _2 _3 _4 What's the sanest way to print out all the files in the directory that start with the underscore? Ie, I just want to list _1, _2, _3, _4. -- http://mail.python.org/mailman/listinfo/python-list
Re: Most active coroutine library project?
On 2009-09-29, Hendrik van Rooyen hend...@microcorp.co.za wrote: On Monday, 28 September 2009 16:44:48 Grant Edwards wrote: $10 is pretty expensive for a lot of applications. I bet that processor also uses a lot of power and takes up a lot of board space. If you've only got $2-$3 in the money budget, 200uA at 1.8V in the power budget, and 6mm X 6mm of board-space, your choices are limited. Besides If you can get by with 256 or 512 bytes of RAM, why pay 4X the price for a 1K part? Besides which, the 8032 instruction set and development tools are icky compared to something like an MSP430 or an AVR. ;) [The 8032 is still head and shoulders above the 8-bit PIC family.] I am biased. I like the 8031 family. I have written pre-emptive multitasking systems for it, as well as state-machine round robin systems. In assembler. Who needs tools if you have a half decent macro assembler? Assembler macros are indeed a lost art. Back in the day, I remember seeing some pretty impressive macro libraries layered 2-3 deep. I've done assember macros as recently as about 2-3 years go because it was the easiest way to auto-magically generate lookup tables for use by C programs (macro assemblers always have a repeat directive, and cpp doesn't). The 803x bit handling is, in my arrogant opinion, still the best of any processor. - jump if bit set then clear as an atomic instruction rocks. The bit-addressing mode was (and still is) cool. However, the stack implementation hurts pretty badly now that memory is cheap. I shouldn't criticize the 8051. I remember switching from the 8048 to the 8051 (8751 actually, at about $300 each) and thinking it was wonderful. [Anybody who remembers fighting with the 8048 page boundaries knows what I mean.] :-) Where do you get such nice projects to work on? Just lucky. :) -- Grant -- http://mail.python.org/mailman/listinfo/python-list
Re: Partial directory search question
What's the sanest way to print out all the files in the directory that start with the underscore? Ie, I just want to list _1, _2, _3, _4. I'd use a string's join() method to combine the results of a list-comprehension or generator that filtered the output of os.listdir() based on the startswith() method of the strings. Left intentionally oblique and code-free because this sounds a bit like a home-work problem. If you're a python coder, that should make pretty decent sense and be a one-liner to implement. -tkc -- http://mail.python.org/mailman/listinfo/python-list
Can I make these getters/setters?
Hi, As part of my GUI, I have lots of fields that people can fill in, defined like this: self.selection = Pmw.EntryField(group.interior(), labelpos='w', label_text='Selection to use: ', value='(polymer)', ) I then use self.selection.get_value() and self.selection.set_value(), and those two functions are the only ways in which I care about self.selection. I've never really used properties, getters or setters before. I tried this, but it didn't work: def __init__(self): self._selection = Pmw.EntryField(group.interior(), labelpos='w', label_text='Selection to use: ', value='(polymer)', ) self.selection = property(self._selection.get_value (),self._selection.set_value()) Of course, I really have ~40 things that I'd like to do this for, not just one, so I'd like to find a fairly concise syntax. In case it helps, here's a complete example of me failing. I'd like it to print out 2 instead of property object at 0xe763f0 class Foo(object): def __init__(self,val): self._val = val def get(self): return self._val def set(self,val): self._val = val class Bar(object): def __init__(self): self._v = Foo(2) self.v = property(self._v.get,self._v.set) b = Bar() print b.v -- http://mail.python.org/mailman/listinfo/python-list
Re: Partial directory search question
On Sep 29, 7:20 pm, Tim Chase python.l...@tim.thechases.com wrote: What's the sanest way to print out all the files in the directory that start with the underscore? Ie, I just want to list _1, _2, _3, _4. I'd use a string's join() method to combine the results of a list-comprehension or generator that filtered the output of os.listdir() based on the startswith() method of the strings. Left intentionally oblique and code-free because this sounds a bit like a home-work problem. If you're a python coder, that should make pretty decent sense and be a one-liner to implement. -tkc Okay, sorry for the delay to the response. I got side tracked trying to stalk, I mean talk to the 59 year old neighbor girl. Anyways, I couldn't get it to one in one line. Here is what I did... % more rec.py #!/usr/local/bin/python import os import time for filename in os.listdir(/usr/bbs/confs/september): #stat = os.stat(filename) if filename.startswith(_): print filename ./rec.py _1 _2 _3 _4 _5 _6 _7 _8 It correctly prints out all the files in the directory that start with an underscore. -- http://mail.python.org/mailman/listinfo/python-list
Re: Partial directory search question
On Sep 29, 7:52 pm, chad cdal...@gmail.com wrote: On Sep 29, 7:20 pm, Tim Chase python.l...@tim.thechases.com wrote: What's the sanest way to print out all the files in the directory that start with the underscore? Ie, I just want to list _1, _2, _3, _4. I'd use a string's join() method to combine the results of a list-comprehension or generator that filtered the output of os.listdir() based on the startswith() method of the strings. Left intentionally oblique and code-free because this sounds a bit like a home-work problem. If you're a python coder, that should make pretty decent sense and be a one-liner to implement. -tkc Okay, sorry for the delay to the response. I got side tracked trying to stalk, I mean talk to the 59 year old neighbor girl. Anyways, I couldn't get it to one in one line. Here is what I did... % more rec.py #!/usr/local/bin/python import os import time for filename in os.listdir(/usr/bbs/confs/september): #stat = os.stat(filename) if filename.startswith(_): print filename ./rec.py _1 _2 _3 _4 _5 _6 _7 _8 It correctly prints out all the files in the directory that start with an underscore. er *couldn't get it into a one liner*. -- http://mail.python.org/mailman/listinfo/python-list
Re: Serial connections and threads in a GUI
Aaron Hoover wrote: I have a wx-based GUI application that I'd like to have read streaming data from a serial port and plot that data using matplotlib and a figure canvas. It's a robotic application where the workflow goes something like this: 1) Connect to robot 2) Configure robot settings 3) Command robot to do something for time, t - While robot does something, read sensor data and plot 4) End of t seconds, resume at (2) I have the application reading from the serial port in a separate thread, but my problem is this - I'd like the user to be able to write to the serial port by interacting with the GUI between runs. However, the infinite loop running inside the thread responsible for receiving data has exclusive access to the serial connection object. Because of how the application works, I know that I won't need to write to the serial port when I'm in the middle of receiving data, so that simplifies things somewhat. But, how can I allow the thread access to the connection for reading, but then have it give up control over the connection when it's done so the main GUI thread can access the connection again for writing? Currently, I have the thread's run() method exit when it's done receiving data. But, that means that I can only receive data again using the same thread by calling run(), blocking the main GUI thread from updating the plot until it completes. This is my first go at using threads in Python, so I'm sure there's a straightforward way to do this that I'm just missing. At any rate, thanks in advance for your patience and assistance. Thanks, Aaron Use a few queue objects (see the queue module). Your serial port thread can have an rx and a tx queue. Your gui program just checks the rx queue whenever it wants and puts data into the tx queue whenever it wants, and the queue objects do all the worrying about synchronizing and thread safety and such. Works like magic! Paul Probert -- http://mail.python.org/mailman/listinfo/python-list
Re: Can I make these getters/setters?
On Tue, 29 Sep 2009 19:48:51 -0700, Michael George Lerner wrote: I then use self.selection.get_value() and self.selection.set_value(), and those two functions are the only ways in which I care about self.selection. I've never really used properties, getters or setters before. I tried this, but it didn't work: def __init__(self): self._selection = Pmw.EntryField(group.interior(), labelpos='w', label_text='Selection to use: ', value='(polymer)', ) self.selection = property(self._selection.get_value (),self._selection.set_value()) property() only works if the property is defined in the *class*, not the instance. In other words, this will work: class K(object): def _parrot_getter(self): return Norwegian %s % self._colour parrot = property(_parrot_getter) def __init__(self, colour=Blue): self._colour = colour But this will not: class K(object): def _parrot_getter(self): return Norwegian %s % self._colour def __init__(self, colour=Blue): self._colour = colour parrot = property(_parrot_getter) -- Steven -- http://mail.python.org/mailman/listinfo/python-list
UnboundLocalError with extra code after return
I'm trying to write a decorator that causes a function to do nothing if called more than once- the reason for this is trivial and to see if I can (Read- I'm enjoying the challenge, please don't ruin it for me =] ) However I'm getting strange results with Python 2.6.2 on win32. With this code: def callonce(func): def nullmethod(): pass def __(): return func() return __ @callonce def t2(): print T2 called t2() It does exactly what you'd expect, prints T2 called However: def callonce(func): def nullmethod(): pass def __(): return func() func = nullmethod return ret return __ @callonce def t2(): print T2 called t2() Gives me: C:\tmp\calloncecallonce.py Traceback (most recent call last): File C:\tmp\callonce\callonce.py, line 27, in module t2() File C:\tmp\callonce\callonce.py, line 12, in __ return func() UnboundLocalError: local variable 'func' referenced before assignment Any ideas on why? This looks like a bug to me, but I'm quite new to this style of programming so it may be some nuance I'm not aware of. Thanks in advance. Rich Healey -- http://mail.python.org/mailman/listinfo/python-list
Re: Partial directory search question
On Tue, 29 Sep 2009 19:09:16 -0700 (PDT), chad cdal...@gmail.com wrote: I have a directory that contains the following login rc sum _1 _2 _3 _4 What's the sanest way to print out all the files in the directory that start with the underscore? Ie, I just want to list _1, _2, _3, _4. I don't know what you mean by sanest, but this is one way: import glob for f in glob.glob(_*): print f Martien -- | Martien Verbruggen | You can't have everything, where would first.l...@heliotrope.com.au | you put it? | -- http://mail.python.org/mailman/listinfo/python-list
Re: UnboundLocalError with extra code after return
On Sep 30, 2:15 pm, Rich Healey healey.r...@gmail.com wrote: I'm trying to write a decorator that causes a function to do nothing if called more than once- the reason for this is trivial and to see if I can (Read- I'm enjoying the challenge, please don't ruin it for me =] ) However I'm getting strange results with Python 2.6.2 on win32. With this code: def callonce(func): def nullmethod(): pass def __(): return func() return __ @callonce def t2(): print T2 called t2() It does exactly what you'd expect, prints T2 called However: def callonce(func): def nullmethod(): pass def __(): return func() func = nullmethod return ret return __ @callonce def t2(): print T2 called t2() Gives me: C:\tmp\calloncecallonce.py Traceback (most recent call last): File C:\tmp\callonce\callonce.py, line 27, in module t2() File C:\tmp\callonce\callonce.py, line 12, in __ return func() UnboundLocalError: local variable 'func' referenced before assignment Any ideas on why? This looks like a bug to me, but I'm quite new to this style of programming so it may be some nuance I'm not aware of. Thanks in advance. Rich Healey In case anyone was paying attention I've now gotten this working- and raised a whole bunch more questions! def callonce(func): func.__RECALL = True def __(): if func.__RECALL: func.__RECALL = False return func() else: return return __ @callonce def t2(): print T2 called t2() t2() t2() Works as expected- the last two t2() calls do nothing. It seems that my problem was that I can't assign a new function to the name func within the callonce() function. I can however interact with the func object (in this case storing information about whether or not I'd called it in it's __RECALL item. Is there a cleaner solution? I'd originally wanted to define a function that does nothing, and then point the function to that for subsequent calls (this seems cleaner overall, if a little longwidned to write) ie: def onlyCalledOnce(): global onlyCalledOnce def nullfunc(): pass onlyCalledOnce = nullfunc # Do stuff here return Something I'll keep plugging at this for a little while I think- thoughts suggestions welcome! -- http://mail.python.org/mailman/listinfo/python-list
Re: UnboundLocalError with extra code after return
On Tue, Sep 29, 2009 at 9:15 PM, Rich Healey healey.r...@gmail.com wrote: However: def callonce(func): def nullmethod(): pass def __(): return func() func = nullmethod When Python sees this assignment to func as it compiles the __() method, it marks func as a local variable and will not consult nested function scopes when looking it up at runtime. Hence, when the function is executed, Python does a fast lookup of func in the local scope, finds it has not been assigned to, and raises the error you're seeing, not falling back to and examining the outer function variable scope. Additionally, the __() method does not make sense as written, for its body will stop executing as soon as it hits the first `return` statement. Cheers, Chris -- http://blog.rebertia.com -- http://mail.python.org/mailman/listinfo/python-list
Re: UnboundLocalError with extra code after return
On Tue, Sep 29, 2009 at 9:41 PM, Chris Rebert c...@rebertia.com wrote: On Tue, Sep 29, 2009 at 9:15 PM, Rich Healey healey.r...@gmail.com wrote: However: def callonce(func): def nullmethod(): pass def __(): return func() func = nullmethod Additionally, to rebind a variable in an outer nested function scope like you tried to do, you'd need a `nonlocal` statement. See http://www.python.org/dev/peps/pep-3104/ Cheers, Chris -- http://blog.rebertia.com -- http://mail.python.org/mailman/listinfo/python-list
Re: UnboundLocalError with extra code after return
On Tue, 2009-09-29 at 21:15 -0700, Rich Healey wrote: However: def callonce(func): def nullmethod(): pass def __(): return func() func = nullmethod return ret return __ @callonce def t2(): print T2 called t2() Gives me: C:\tmp\calloncecallonce.py Traceback (most recent call last): File C:\tmp\callonce\callonce.py, line 27, in module t2() File C:\tmp\callonce\callonce.py, line 12, in __ return func() UnboundLocalError: local variable 'func' referenced before assignment Any ideas on why? This looks like a bug to me, but I'm quite new to this style of programming so it may be some nuance I'm not aware of. I'm not following your logic. There is no check to see if func is already called. Moreover, you are replacing func which is not recommended. A decorator is supposed to decorate func, not replace it. I think what is happening here is func = nullmethod is being assigned at definition time, not at runtime, so by the time you've defined __() func is no longer there, so Secondly, if nullmethod returns nothing, why not just return nothing in __() instead of creating a new function that does nothing. Thirdly, 'return ret' is never called. Because you return out of __() in the first line of the function. Fourthly, 'ret' is never defined, so even if it were run you would get an undefined error. But what I think is happening is since you have effectively overriden func (t2) with nullmethod it gets 'lost' by the time the __() is actually called. I haven't looked closely but you should be able to see what's happening in a debugger. What you really want to do is something like this: def callonce(func): func.called = False def dec(*args, **kwargs): if func.called: return func.called=True return func(*args, **kwargs) return dec @callonce def t2(): print 't2() Called' t2() t2() Called t2() -- http://mail.python.org/mailman/listinfo/python-list
Re: Simple if-else question
On Sep 29, 1:15 pm, Carl Banks pavlovevide...@gmail.com wrote: Hmm, I wonder if Python should emit a warning if an else is used on a for block with no break inside. I don't think the else can be invoked in any other way. As a bonus it could catch some cases where people mistakenly use it thinking it will execute [only] when there are no iterations. [Edit from Duncan Booth] I would definitely be in favor of a warning. Yes, people should read the docs more carefully, and yes, it would cost a certain amount of annoyance to implement this. But I don't think it would get in people's way if they do know how to use else, and I think it would cut down on the number of questions from mystified beginners, some of whom are much more aggressive than this particular OP about claiming that Python is broken (when it's actually behaving as designed). John -- http://mail.python.org/mailman/listinfo/python-list
Re: Simple if-else question
On Sep 29, 1:25 pm, MRAB pyt...@mrabarnett.plus.com wrote: The example that makes it clearest for me is searching through a list for a certain item and breaking out of the 'for' loop if I find it. If I get to the end of the list and still haven't broken out then I haven't found the item, and that's when the else statement takes effect: for item in my_list: if item == desired_item: print Found it! break else: print It's not in the list To me, this is the quintessential use case and clearest example of the for-else. John -- http://mail.python.org/mailman/listinfo/python-list