Dict Help
Hello Guys, I'm looking for some help expanding on a dictionary I've got and storing multiple values per key and the best way to do it. I'm guessing that I need to store a list inside the value of the dictionary, but I'm not quite sure how that can be achieved, and also how to check for values in that list. Here is a brief example of what I want to be able to build: Key Value 00:0F:DE:A8:AB:6F 6C66F14B-FED6-D1A4-62EAAD881E9133F0.jpg,01DBB592-F7EB-B000-7F250FD8A2CE158F.gif,533EAE0F-B211-B2D8-4C2DB662CCECFBD7.3gp 00:17:B0:A0:E7:09 6C66F14B-FED6-D1A4-62EAAD881E9133F0.jpg,01DBB592-F7EB-B000-7F250FD8A2CE158F.gif 00:1B:98:16:21:E4 6C66F14B-FED6-D1A4-62EAAD881E9133F0.jpg Now I really need to have two functions, one of which appends a value to the list for a specified key, so I have the key and the new value as strings, I then want to locate that key and append the list with the new value. The second function I need, is to check for the existence of a value in the list of a particular key, again I have the key and the value and I want to do something like: if value in list of values for key: do something here... The final function that would be helpful to me would to be able to remove a value from the list for a specific key. I'm not sure if a list as the value in a dict is possible, or if its the best way to achieve this, It just made logic sense to me so thought I'd come and get your thoughts on this. If anyone has any better suggestions about how to attach mutiple values to a single key, I would love to hear it. Other than that I just need some code examples of those append/existence functions. Can anyone offer some help? Thanks guys, Rob _ 100’s of Music vouchers to be won with MSN Music https://www.musicmashup.co.uk/index.html-- http://mail.python.org/mailman/listinfo/python-list
Cant run application as ./myapp.py
Hello Guys, I've got an application here which for some reason won't start using the following syntax from the command line: Cd /mydirectory ./MyApplication.py I have to run this as a fully qualified python launch such as: Cd /mydirectory python MyApplication.py This is a little bit confusing to me as I have other applications which run just fine using the ./somthing.py syntax. Is there any reason why this doesn't work? Cheers, Robert -- http://mail.python.org/mailman/listinfo/python-list
Logging Date/Time Format
Hello Guys, I'm using the python logging module, however I'm not happy with the current date/time format which is used to write the timestamp into the log file. I need the logger to write the stamp without the milliseconds appended too it. This is because I use a 3rd party application to parse the logs at a later date as a CSV and the comma separating the milliseconds throws things out. I've been looking through the API documentation but couldn't see any decent method for doing so. Any ideas? Robert -- http://mail.python.org/mailman/listinfo/python-list
MySQL DB-Api
Good morning list. I'm in the process of learning my way around the DB-API module for MySQL and wanted to come and get some advice on how you all manage your database connections and cursors. Within my applications I'll have many classes which access the database, I'm wondering to what level I should extract the database connection. Should I create a new database connection and close it for every method which calls the database? Should I create the connection/cursor to the DB when I construct the class and place the cursor in the self scope? Or should I create a application wide connection/cursor to the database and inject the cursor into all the classes which require it? All classes within the application access the same database so at the moment I'm leaning towards creating an application wide connection and cursor and then injecting it into classes which require database access, does that sound like a fair plan? I'm just interested to learn how you are managing this. Thanks guys, Rob -- http://mail.python.org/mailman/listinfo/python-list
Check For SELF Variable Existance
Hello Guys, I want to be able to check if a class has a certain property in its 'self' scope, what's the best way to do this? I've seen a few examples of people using a try: block to test if the variable exists but to be honest, this seems a little bit verbose, do we not have a better method of checking for its existence? Cheers, Rob -- http://mail.python.org/mailman/listinfo/python-list
RE: Check For SELF Variable Existance
Thank you Simon, I was hoping there would be something as simple as that :-) Rob -Original Message- From: [EMAIL PROTECTED] [mailto:[EMAIL PROTECTED] On Behalf Of Simon Brunning Sent: 11 March 2008 13:21 To: python-list@python.org Subject: Re: Check For SELF Variable Existance On Tue, Mar 11, 2008 at 11:00 AM, Robert Rawlins <[EMAIL PROTECTED]> wrote: > I want to be able to check if a class has a certain property in its 'self' > scope, what's the best way to do this? >>> class Spam(object): ... def egg(self): ... if hasattr(self, 'chips'): print 'got chips!' ... >>> spam = Spam() >>> spam.egg() >>> spam.chips = 'beans' >>> spam.egg() got chips! -- Cheers, Simon B. [EMAIL PROTECTED] http://www.brunningonline.net/simon/blog/ -- http://mail.python.org/mailman/listinfo/python-list -- http://mail.python.org/mailman/listinfo/python-list
RE: SQLObject 0.10.0
Excellent stuff Oleg, I've been looking for an ORM framework for a while and hadn't settled on one, I'll give this a look through later today. Thanks, Robert -Original Message- From: [EMAIL PROTECTED] [mailto:[EMAIL PROTECTED] On Behalf Of Oleg Broytmann Sent: 11 March 2008 13:40 To: Python Announce Mailing List; Python Mailing List Subject: SQLObject 0.10.0 Hello! I'm pleased to announce version 0.10.0, the first stable release of 0.10 branch of SQLObject. What is SQLObject = SQLObject is an object-relational mapper. Your database tables are described as classes, and rows are instances of those classes. SQLObject is meant to be easy to use and quick to get started with. SQLObject supports a number of backends: MySQL, PostgreSQL, SQLite, and Firebird. It also has newly added support for Sybase, MSSQL and MaxDB (also known as SAPDB). Where is SQLObject == Site: http://sqlobject.org Development: http://sqlobject.org/devel/ Mailing list: https://lists.sourceforge.net/mailman/listinfo/sqlobject-discuss Archives: http://news.gmane.org/gmane.comp.python.sqlobject Download: http://cheeseshop.python.org/pypi/SQLObject/0.10.0 News and changes: http://sqlobject.org/News.html What's New == News since 0.9 -- Features & Interface * Dropped support for Python 2.2. The minimal version of Python for SQLObject is 2.3 now. * Removed actively deprecated attributes; lowered deprecation level for other attributes to be removed after 0.10. * SQLBuilder Select supports the rest of SelectResults options (reversed, distinct, joins, etc.) * SQLObject.select() (i.e., SelectResults) and DBConnection.queryForSelect() use SQLBuilder Select queries; this make all SELECTs implemented internally via a single mechanism. * SQLBuilder Joins handle SQLExpression tables (not just str/SQLObject/Alias) and properly sqlrepr. * Added SQLBuilder ImportProxy. It allows one to ignore the circular import issues with referring to SQLObject classes in other files - it uses the classregistry as the string class names for FK/Joins do, but specifically intended for SQLBuilder expressions. See tests/test_sqlbuilder_importproxy.py. * Added SelectResults.throughTo. It allows one to traverse relationships (FK/Join) via SQL, avoiding the intermediate objects. Additionally, it's a simple mechanism for pre-caching/eager-loading of later FK relationships (i.e., going to loop over a select of somePeople and ask for aPerson.group, first call list(somePeople.throughTo.group) to preload those related groups and use 2 db queries instead of N+1). See tests/test_select_through.py. * Added ViewSQLObject. * Added sqlmeta.getColumns() to get all the columns for a class (including parent classes), excluding the column 'childName' and including the column 'id'. sqlmeta.asDict() now uses getColumns(), so there is no need to override it in the inheritable sqlmeta class; this makes asDict() to work properly on inheritable sqlobjects. * Allow MyTable.select(MyTable.q.foreignKey == object) where object is an instance of SQLObject. * Added rich comparison methods; SQLObjects of the same class are considered equal is they have the same id; other methods return NotImplemented. * RowDestroySignal is sent on destroying an SQLObject instance; postfunctions are run after the row has been destroyed. * Changed the implementation type in BoolCol under SQLite from TINYINT to BOOLEAN and made fromDatabase machinery to recognize it. * MySQLConnection (and DB URI) accept a number of SSL-related parameters: ssl_key, ssl_cert, ssl_ca, ssl_capath. * Use sets instead of dicts in tablesUsed. Dropped tablesUsedDict function; instead there is tablesUsedSet that returns a set of strings. * SQLBuilder tablesUsedSet handles sqlrepr'able objects. * Under MySQL, PickleCol no longer uses TEXT column types; the smallest column is now BLOB - it is not possible to create TINYBLOB column. For a more complete list, please see the news: http://sqlobject.org/News.html Oleg. -- Oleg Broytmannhttp://phd.pp.ru/[EMAIL PROTECTED] Programmers don't die, they just GOSUB without RETURN. -- http://mail.python.org/mailman/listinfo/python-list -- http://mail.python.org/mailman/listinfo/python-list
RE: MySQL DB-Api
Hello guys, sorry for dragging this to the top of the list again but I really am intrigued to get your feedback. I'm looking for a clean and efficient way to implement the DB-API into my application without having to create database connections for every instance of objects which require it. I'd love to create some form of cached instance of the connection within the application and then provide new cursor instances for objects that need to query the database but I'm not sure of the best way to deal with this to ensure the cursors are closed when the instances destruct, or just as importantly, if I even need to close the cursor. I'd be really interested to know how you are implementing this kind of thing, Cheers, Robert From: [EMAIL PROTECTED] [mailto:[EMAIL PROTECTED] On Behalf Of Robert Rawlins Sent: 11 March 2008 11:24 To: python-list@python.org Subject: MySQL DB-Api Good morning list. I'm in the process of learning my way around the DB-API module for MySQL and wanted to come and get some advice on how you all manage your database connections and cursors. Within my applications I'll have many classes which access the database, I'm wondering to what level I should extract the database connection. Should I create a new database connection and close it for every method which calls the database? Should I create the connection/cursor to the DB when I construct the class and place the cursor in the self scope? Or should I create a application wide connection/cursor to the database and inject the cursor into all the classes which require it? All classes within the application access the same database so at the moment I'm leaning towards creating an application wide connection and cursor and then injecting it into classes which require database access, does that sound like a fair plan? I'm just interested to learn how you are managing this. Thanks guys, Rob -- http://mail.python.org/mailman/listinfo/python-list
RE: agg (effbot)
Haha, Tim, that cracks me up! lol Bring forth the Holy Hand Grenade of Antioch Rob -Original Message- From: [EMAIL PROTECTED] [mailto:[EMAIL PROTECTED] On Behalf Of Tim Chase Sent: 12 March 2008 15:04 To: python-list@python.org Subject: Re: agg (effbot) Importance: Low Gerhard Häring wrote: > [EMAIL PROTECTED] wrote: >> aggdraw-1.2a3-20060212.tar.gz > > Try shegabittling the frotz first. If that doesn't help, please post the > output of the compile command that threw the error. Maynard: He who is valiant and pure of spirit may find the compiling instructions in the Drawing Program of Agg Arthur: what?! Maynard: the Drawing Program of Agg. Bedevere: What's that? Maynard: he must of died while typing it Launcelaot: Oh, come on! Maynard: well, that's what it's called Artuhur: Look, if he was dying, he wouldn't bother to name it "agg" Maynard: Well, that's what's written in the URL Galahad: Perhaps he was dictating... Arthur: Oh, shut up. ... -tkc -- http://mail.python.org/mailman/listinfo/python-list -- http://mail.python.org/mailman/listinfo/python-list
"Attribute Doesnt Exist" ... but.... it does :-s
Hello Guys, I've got an awfully aggravating problem which is causing some substantial hair loss this afternoon J I want to get your ideas on this. I am trying to invoke a particular method in one of my classes, and I'm getting a runtime error which is telling me the attribute does not exist. I'm calling the method from within __init__ yet it still seems to think it doesn't exist. Code: # Define the RemoteDevice class. class remote_device: # I'm the class constructor method. def __init__(self, message_list=""): self.set_pending_list(message_list) def set_pending_list(self, pending_list): # Set the message list property. self.pending_list = message_list And the error message which I receive during the instantiation of the class: File: "/path/to/my/files/remote_device.py", line 22, in __init__ self.set_pending_list(message_list) AttributeError: remote_device instance has no attribute 'set_pending_list' Does anyone have the slightest idea why this might be happening? I can see that the code DOES have that method in it, I also know that I don't get any compile time errors so that should be fine. I know it mentions line 22 in the error, but I've chopped out a load of non relevant code for the sake of posting here. Perhaps I'm missing something really simple, but it's got my head spinning. Thanks, Robert -- http://mail.python.org/mailman/listinfo/python-list
RE: "Attribute Doesnt Exist" ... but.... it does :-s
Hi Guys, Well thanks for the response, I followed your advice and chopped out all the crap from my class, right down to the bare __init__ and the setter method, however, the problem continued to persist. However, Robert mentioned something about unindented lines which got me thinking so I deleted my tab indents on that method and replaces them with standard space-bar indents and it appears to have cured the problem. Usually my Eclipse IDE throws up an error about this but for some reason decided not too this time around, what a PITA. Thanks for the ideas guys, I appreciate it. Robert -Original Message- From: [EMAIL PROTECTED] [mailto:[EMAIL PROTECTED] On Behalf Of Robert Bossy Sent: 13 March 2008 15:51 To: python-list@python.org Subject: Re: "Attribute Doesnt Exist" ... but it does :-s Robert Rawlins wrote: > > Hello Guys, > > I've got an awfully aggravating problem which is causing some > substantial hair loss this afternoon J I want to get your ideas on > this. I am trying to invoke a particular method in one of my classes, > and I'm getting a runtime error which is telling me the attribute does > not exist. > > I'm calling the method from within __init__ yet it still seems to > think it doesn't exist. > > Code: > > # Define the RemoteDevice class. > > class *remote_device*: > > # I'm the class constructor method. > > def *__init__*(/self/, message_list=/""/): > > /self/.set_pending_list(message_list) > > def *set_pending_list*(/self/, pending_list): > > # Set the message list property. > > /self/.pending_list = message_list > > And the error message which I receive during the instantiation of the > class: > > File: "/path/to/my/files/remote_device.py", line 22, in __init__ > > self.set_pending_list(message_list) > > AttributeError: remote_device instance has no attribute 'set_pending_list' > > Does anyone have the slightest idea why this might be happening? I can > see that the code DOES have that method in it, I also know that I > don't get any compile time errors so that should be fine. I know it > mentions line 22 in the error, but I've chopped out a load of non > relevant code for the sake of posting here. > Hi, I don't get this error if I run your code. Maybe the irrelevant code causes the error: my guess is that there's a parenthesis mismatch or an undeindented line. Btw, calls to set_pending_list will fail since the name "message_list" is not defined in its scope. Please follow Chris Mellon's advice. Cheers, RB -- http://mail.python.org/mailman/listinfo/python-list -- http://mail.python.org/mailman/listinfo/python-list
RE: "Attribute Doesnt Exist" ... but.... it does :-s
Haha, I could use a stiff whisky myself after the stress that caused me :-) Robert -Original Message- From: [EMAIL PROTECTED] [mailto:[EMAIL PROTECTED] On Behalf Of Robert Bossy Sent: 13 March 2008 16:10 To: python-list@python.org Subject: Re: "Attribute Doesnt Exist" ... but it does :-s Robert Rawlins wrote: > Hi Guys, > > Well thanks for the response, I followed your advice and chopped out all the > crap from my class, right down to the bare __init__ and the setter method, > however, the problem continued to persist. > > However, Robert mentioned something about unindented lines which got me > thinking so I deleted my tab indents on that method and replaces them with > standard space-bar indents and it appears to have cured the problem. > Aha! Killed the bug at the first guess! You owe me a beer, mate. RB -- http://mail.python.org/mailman/listinfo/python-list -- http://mail.python.org/mailman/listinfo/python-list
RE: Huge problem gettng MySQLdb to work on my mac mini running Macosx 10.5 Leopard
Geert, I've not seen this issue myself, however, you might get a little more luck asking over on the MySQLdb mailing list as this seems to be more an issue with the db than your python code. It might be worth posting your question there too as it'll heighten your chances of finding someone who knows MySQLdb inside out. http://mail.python.org/mailman/listinfo/db-sig Cheers, Robert -Original Message- From: [EMAIL PROTECTED] [mailto:[EMAIL PROTECTED] On Behalf Of geert Sent: 14 March 2008 10:36 To: python-list@python.org Subject: Huge problem gettng MySQLdb to work on my mac mini running Macosx 10.5 Leopard Hi all, I have a mac mini running maocosx 10.5 leopard I want to deploy a django project on. My backend is MySQL, and I have it running as a 64- bit app. Of course, apache2 is also running as 64-bit. MySQLdb installs with the usual warnings after applying the various patches I found here and there. These patches consist of altering _mysql.c and site.cfg. Basically, my problem boils down to this: File "", line 1, in File "build/bdist.macosx-10.5-i386/egg/MySQLdb/__init__.py", line 19, in File "build/bdist.macosx-10.5-i386/egg/_mysql.py", line 7, in File "build/bdist.macosx-10.5-i386/egg/_mysql.py", line 6, in __bootstrap__ ImportError: dynamic module does not define init function (init_mysql) Has anyone solved this? I been hunting around for 2 weeks now, and my deadline is looming grimly on the horizon :) Geert -- http://mail.python.org/mailman/listinfo/python-list -- http://mail.python.org/mailman/listinfo/python-list
Element Tree Help
Hello Guys,I have little to no experiance with element tree and I'm struggling to find a way to parse the details from the XML document (attached) into my application. Essentialy I'm looking to take the following document and turn it into a dict of tuples, each dict element defines a datasource with the key to the element being the 'name' which is defined in the XML and then the value of the pair is a tuple which contains the details of the datasource, like the host and port etc.I've attached a copy of the example XML to this email.Can anyone offer some advice on how to get started with this? I've spent a little time looking over the documentation of element tree and have struggled to break the ice and parse this document.Thanks guys,Robert _ The next generation of Windows Live is here http://www.windowslive.co.uk/get-live a name localhost 3306 database1 someusername somepassword another name localhost 3306 database2 itsusername andthepassword -- http://mail.python.org/mailman/listinfo/python-list
Logger Configuration
Hello guys, I've attached an example of my logging configuration file for you to look at. The problem I'm experiencing is that the log files are not rotating as I would expect them to, they just keep growing and growing. Can you see any reason for this to happen? This is the first time I've used a config file and not just configured the logger programmatically. Thanks guys, Robert logging.conf Description: Binary data -- http://mail.python.org/mailman/listinfo/python-list
No Exceptions
Morning Guys, This morning I've been having a strange issue where my application doesn't appear to raise any exceptions. Even when manually placing code in the application which raises an exception explicitly like: Raise Exception, "This is a test exception" The application doesn't appear to throw it, it will fail to execute any code in the block following the exception but does not throw any exceptions to the command prompt. Any ideas what might be causing this? I've just sent another support request to this list about logging configuration, could the two issues be related? Thank you, Robert -- http://mail.python.org/mailman/listinfo/python-list
RE: Logger Configuration
Ok, I've managed to resolve this issue, it seems it was because I had the file size and number of archives to keep in "" and was thus setting them as strings instead of numerical values. Robert From: [EMAIL PROTECTED] [mailto:[EMAIL PROTECTED] On Behalf Of Robert Rawlins Sent: 08 July 2008 13:18 To: python-list@python.org Subject: Logger Configuration Hello guys, I've attached an example of my logging configuration file for you to look at. The problem I'm experiencing is that the log files are not rotating as I would expect them to, they just keep growing and growing. Can you see any reason for this to happen? This is the first time I've used a config file and not just configured the logger programmatically. Thanks guys, Robert -- http://mail.python.org/mailman/listinfo/python-list
Logging in __del__()
Guys, I'm trying to help trace when instances of particular classes are being destroyed by the garbage collector and thought the cleanest way would be to implement a logging call in __del__() on the class. However, I'm having an issue. I inject a logger instance into my class upon construction and set it to self.logger, I then use this within the class to log actions going on within, works fine. Now, when I make the call in the __del__() method like so: def __del__(self): # Log the classes destruction. self.logger.debug("Class Instance Destroyed") I then get the following exception thrown when running my code: Traceback (most recent call last): File "/usr/lib/python2.5/logging/handlers.py", line 73, in emit if self.shouldRollover(record): File "/usr/lib/python2.5/logging/handlers.py", line 147, in shouldRollover self.stream.seek(0, 2) #due to non-posix-compliant Windows feature ValueError: I/O operation on closed file Does anyone have any ideas as to what I'm doing wrong here? Is this a known issue which has a neat little work around? Cheers, Robert -- http://mail.python.org/mailman/listinfo/python-list
RE: Logging in __del__()
Hi Fredrik, > When the application is running, or when it is shutting down? This is interesting, I did a test where I explicitly destroyed the instance using 'del my_instance' while the application was running and no error was thrown. It would see you are right, when the application ends it kills the logging module before my classes. I think you're on the right approach just try/except it and leave it be. Am I right in thinking that Python destroys instances of classes when it deems they are no longer needed? I shouldn't have to explicitly delete the classes, right? Thanks Fredrik, Robert -- http://mail.python.org/mailman/listinfo/python-list
RE: Logging in __del__()
Hi Vinay, > Python uses reference counting with a cycle detector, but the > detector's behaviour is different if there are finalizers (__del__) - > see > > http://www.python.org/doc/ext/refcounts.html > Thank you for the link, that certainly explains a great deal. So, am I right to assume that python will still handle its garbage disposal if I implement __del__(), it just handles circular references in a slightly different way, but to the same effect. Right? Cheers, Robert -- http://mail.python.org/mailman/listinfo/python-list
Get current class namespace.
Guys, What's the simplest way to access a classes namespace from within itself. I want to use it in a custom __repr__() method so it prints the current namespace for the class like package.module.class. Suggestions? I'm sure there is a simple enough method built in to help me here, I've just not seen it before. Cheers, Robert -- http://mail.python.org/mailman/listinfo/python-list
Unusual Exception Behaviour
Hello Chaps, I have an unusual situation with my application which I've also seen once or twice in the past but never found a solution too. Basically the application stops properly reporting Exceptions when they are thrown. My application logs extensively to a file using the python logging module, when an exception is throw all the log data starts being thrown to the command prompt instead of the file, however, I don't get any actual exception information output. I have looked through the application for any unusual or bare try/except blocks that don't actually do anything with the except just in case any of them are causing the issue but can't seem to see any. This little issue makes debugging my application VERY hard, when running the app I can see it crash, but I get no information as to where the exception is being thrown from, as you can imagine, quite frustrating. Even when throwing my own exceptions just for testing like so: Raise Exception, "This is a test exception." In certain parts of the application it doesn't print the exception to screen, but all subsequent requests to the logger seem to print out at the command prompt. I would really love some suggestions on what might be causing this. Cheers, Robert -- http://mail.python.org/mailman/listinfo/python-list
RE: Unusual Exception Behaviour
Hi Mk, > Why not capture exceptions themselves to a log file? > > http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/466332 Thanks for the reply mate, I appreciate you getting back to me so quickly. I certainly like that implementation for logging the exceptions, however, at the moment I don't even know where the exceptions are occurring, or what type they are, could I still use this method to log any and all exceptions raised in the application? I'm a little confused as to how I can modify that implementation to do so. Robert -- http://mail.python.org/mailman/listinfo/python-list
RE: Unusual Exception Behaviour
Hi MK, >>Robert Rawlins wrote: >> >> I certainly like that implementation for logging the exceptions, however, at >> the moment I don't even know where the exceptions are occurring, or what >> type they are, could I still use this method to log any and all exceptions >> raised in the application? > Remember, Google is your friend, here's the crux of the method without > the fancy schmancy sugar coating: > > http://linux.byexamples.com/archives/365/python-convey-the-exception-traceba ck-into-log-file/ > > if __name__=="__main__": > try: > main() > except: > print "Trigger Exception, traceback info forward to log file." > traceback.print_exc(file=open("errlog.txt","a")) > sys.exit(1) > I've just given this solution a shot but I still seem to get the same result, it suddenly starts dumping the log data to the command line, and nothing gets printed in error.txt apart from the keyboard interrupt from when I kill the application. For some reason the exceptions don't seem to be raised properly so obviously aren't being caught at this higher level. So confusing. -- http://mail.python.org/mailman/listinfo/python-list
RE: Unusual Exception Behaviour
> That's seriously weird. What's your Python version and platform? On my > Windows and Linux machines, with more recent Python versions the above > trick works flawlessly. > > Check your environment, namely PYTHON* variables. There may be something > causing this behaviour. Unset them. > > Check the first line of your scripts. If you're calling wrong Python > interpreter (there may be more than one in the system for some reason), > this may cause it. > > You could also try setting up PYTHONINSPECT environment variable or run > the python interpreter with -i option before program filename, which > drops you into an interactive shell upon exception or termination of a > program. > > This behavior is seriously unusual for Python. Maybe you have some old / > buggy version? Thanks for that MK. I'm using Debian with Python 2.5 from the stable apt repository, installed but a couple of days ago. I'll be sure to look into those other elements you suggested also. I'm not sure if it bares any resemblance but this application runs a gobject mainloop and uses dbus quite extensively. Don't think this might have something to do with the way I have my loggers configured do you? For some reason it sits in my mind that this issue started when I moved my logging configuration from programmatic into a config file, I can't be totally sure of that though. I've attached the config file that I use, does it all look ok to you? I wonder if the way I've not added any handles/formatters to my root logger might be causing beef? This is certainly a strange one. Robert simple_loggingconf.conf Description: Binary data -- http://mail.python.org/mailman/listinfo/python-list
RE: Unusual Exception Behaviour
>> That's seriously weird. What's your Python version and platform? On my >> Windows and Linux machines, with more recent Python versions the above >> trick works flawlessly. >> >> Check your environment, namely PYTHON* variables. There may be >> something causing this behaviour. Unset them. >> >> Check the first line of your scripts. If you're calling wrong Python >> interpreter (there may be more than one in the system for some >> reason), this may cause it. >> >> You could also try setting up PYTHONINSPECT environment variable or >> run the python interpreter with -i option before program filename, >> which drops you into an interactive shell upon exception or >> termination of a program. >> >> This behavior is seriously unusual for Python. Maybe you have some old >> / buggy version? > > Thanks for that MK. I'm using Debian with Python 2.5 from the stable apt repository, installed > but a couple of days ago. I'll be sure to look into those other elements you suggested also. > > I'm not sure if it bares any resemblance but this application runs a gobject mainloop and uses > > dbus quite extensively. > > Don't think this might have something to do with the way I have my loggers configured do you? > > For some reason it sits in my mind that this issue started when I moved my logging > > > > > > > configuration from programmatic into a config file, I can't be totally sure of that though. > > I've attached the config file that I use, does it all look ok to you? I wonder if the way I've > not added any handles/formatters to my root logger might be causing beef? > > This is certainly a strange one. Ok, Just to add a little interest, when I comment out the configuration line for my logging, like so: #logging.config.fileConfig("/myapp/configuration/logging.conf") It appears to throw the exceptions as normal :-) :-s Sounds as if it's a conflict with my logging configuration, I wonder what though. -- http://mail.python.org/mailman/listinfo/python-list
RE: Unusual Exception Behaviour
Hi Mk, > To tell the truth I have never used logging module extensively, so I'm > not expert in this area and can't help you there. > > However, it seems to me that you may have stumbled upon some subtle bug > / side effect of logging module that could cause some side effects in > exceptions. Or perhaps it surfaces only in combination with glib? > > If you discover the root cause, please let us know on this ng, I'm also > using Python extensions and bindings to other libraries and this could > be of interest at least to me. Yeah it's got me a little bemused to be honest, I've tried playing around with configuration options this morning and not been able to achieve anything that works properly. I'll keep testing though and as soon as I have a root cause to the problem I'll be sure to let the list know. Thanks mate, Robert -- http://mail.python.org/mailman/listinfo/python-list
RE: Unusual Exception Behaviour
> The logging configuration functionality provided by fileConfig is all- > or-nothing, i.e. it does not support incremental configuration. > > Do you know if any libraries you depend on use fileConfig? > > If you use programmatic configuration only, and don't use fileConfig > at all, does everything work as expected? Vinay, I changed this over to programmatic configuration this afternoon and it works just great, all the logging I require, and my exceptions get thrown. :-D I have no idea if any of the librarys I use work from a file config, I'm certainly not aware of them doing so. This is really quite frustrating as I'd much rather use a conf file than work this programmatically. I get the feeling that it's because in the config file I was not attaching any handlers to the root logger, but I don't know. Robert -- http://mail.python.org/mailman/listinfo/python-list
RE: __del__ methods
> Yes. > > "Objects that have __del__() methods and are part of a reference cycle > cause the entire reference cycle to be uncollectable, including > objects not necessarily in the cycle but reachable only from it. > Python doesn't collect such cycles automatically because, in general, > it isn't possible for Python to guess a safe order in which to run the > __del__() methods." > > The uncollectable objects are stored in gc.garbage and will not be > freed until their reference cycles are broken and they are removed > from that list. Ok, guys, I've just recently (in the past week) started using the __del__ method to log class instance destruction so I can keep a track of when objects are created and destroyed, in order to help me trace and fix memory leaks. Are you saying that on the adverse side to this, __del__ may in fact be the CAUSE of a memory leak within my application? If this is the case and __del__ creates such a vulnerability within the application, and apparently isn't all that reliable anyway, why is it still part of the python platform? Cheers, Robert -- http://mail.python.org/mailman/listinfo/python-list
RE: __del__ methods
Hi Duncan, > That sounds like an appropriate use for __del__: it won't matter that it > may not be called when your app exits. Ok, well that's good to know. :-) > Yes, but there is an easy work-around. If you want to track destruction of > objects of type C then don't add a __del__ method to the C objects. Instead > create a separate class which does nothing but track it's own desctruction > and reference that from the class which may be leaking. > > >>> class Track(object): > def __init__(self, parent): > self.parentid = id(parent) > self.parenttype = type(parent).__name__ > def __del__(self): > print "Destroyed <%s object at %s>" % (self.parenttype, > self.parentid) > > > >>> class C(object): > def __init__(self): > self._track = Track(self) > I like this idea, I can definitely see the benefits to working with this concept. One things I will take this quick opportunity to ask, even though it's a little OT: What is the benefit of extending the base 'object' class? What does that give me that en empty, non subclassed object doesn't? > However you should also consider that __del__ only lets you log when > objects are destroyed. Using weak references may be a more useful option as > it will let you track which objects are not being destroyed: you can easily > keep a dictionary of weak references to all existing objects of interest. > Check its length periodically to see whether objects are being leaked and > then inspect the objects themselves to see which ones have leaked. > > You can use gc.get_referrers() to find everything that references a > particular objects and gradually trace backwards until you find the problem > reference (it is tricky though as any code which does this needs to ignore > its own references to the object in question). Yes, that's a very nice concept and like you say gives you quite a nice visual reference of what objects are and aren't being destroyed. Cheers Duncan, Robert -- http://mail.python.org/mailman/listinfo/python-list
RE: __del__ methods
> In Python 2.x, "classic" classes (which are not part of the unified > type hierarchy) are deprecated, and exist only for backward > compatibility with old code. > > You need to create "new-style" classes > http://www.python.org/doc/newstyle/> by inheriting from some > class that is part of the unified type hierarchy; if there is no > obvious candidate, 'object' is the recommended choice. Thanks Ben, This isn’t something I'd seen before (god that makes me feel stupid). I've always based my code off the odd example that's dotted around and hadn’t ever done any proper reading on these new type classes. I've done a little reading this morning and really love a great deal of the concepts, I'll be upgrading my app to this spec in the next few days. Robert -- http://mail.python.org/mailman/listinfo/python-list
RE: __del__ methods
> Time to fix that, then, with some documentation > http://www.python.org/doc/>, and by working through the Python > tutorial http://www.python.org/doc/tut/>. Thanks Ben, I'll be sure to read through these. I also read through this http://www.geocities.com/foetsch/python/new_style_classes.htm earlier this morning which was also a nice little resource. Just about all of it makes sense at the moment, apart from the new constructor types which are constructors at a class level opposed to at instance level. I found this a little confusing but who knows. Presumably this is where you would deal with setting things which exist in every instance of a class, and properties that can vary from instance to instance go into the standard __init_(), have I got that right? Cheers Ben, Robert -- http://mail.python.org/mailman/listinfo/python-list
RE: __del__ methods
> Are you talking about the __new__ method ? Or about metaclasses ? Sorry Bruno, I should have made that a little clearer. I was talking about the __new__ method. Cheers mate, Robert -- http://mail.python.org/mailman/listinfo/python-list
Odd math related issue.
Guys, I've got what seems to me to be a totally illogical math issue here which I can't figure out. Take a look at the following code: self.__logger.info("%i / %i" % (bytes_transferred, self.__sessions[path].total_bytes)) percentage = bytes_transferred / self.__sessions[path].total_bytes * 100 self.__logger.info("%i" % percentage) Seems fairly straight forward, you would think. It takes two values and calculates the percentage of one from the other, however, percentage always comes back as '0' for some reason, look at this log output. 2008-07-21 08:39:05,465 manager.py 78 INFO 290112 / 442435 2008-07-21 08:39:05,466 manager.py 80 INFO 0 Any suggestions as to why this might be the case? I'm sure it's something dumb but I can't spot it. Robert -- http://mail.python.org/mailman/listinfo/python-list
RE: Odd math related issue.
> if you divide two integers, you'll get an integer back (in Python 2.X, > at least). quick fix: > > percentage = bytes_transferred * 100 / total_bytes > > Hey That worked a charm mate, thanks for the info. -- http://mail.python.org/mailman/listinfo/python-list
Daemonize an application.
Guys, I've been looking to run one of my applications as a Deamon on a linux based system. I've been looking at this recipe this morning and it all looks relatively fine: http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/278731 I just noted that the recipe was written 5 years ago and thought I would see if there have been any progressions in Python which allow a more native approach to forking my application as a daemon? I essentially want to add a command line parameter "--as-daemon" to my application which would fork it as a daemon instead of running it as a standard python script, I also want to ensure that stdin/out/err are all configured in a way which means any exceptions thrown by the application or data output to the command line, which I haven't foreseen will be stored in a log file for review. I'd really appreciate your advice and experience. Cheers, Robert -- http://mail.python.org/mailman/listinfo/python-list
Get dict value but not as reference.
Guys, This feels like a strange question but it's not something I've done before, I'm sure it's quite simple. I have a dictionary, and I want to get one of the values from it, but rather than returning it as a reference I want to actually detach/remove the element from the dictionary. Like so: Def get_something(): Something = self.my_dict["keythatIwanttoredetach"] Return something I can then use something, however, it no longer exists in the dictionary. Sorry, I couldn't really explain this very well. How can I achieve this? Cheers, Robert -- http://mail.python.org/mailman/listinfo/python-list
RE: Get dict value but not as reference.
> Use the pop() method of the dictionary, like this: Ah, of course! Pop! I have seen this before I think Jerry, seems to do the trick nicely. Thank you. Robert -- http://mail.python.org/mailman/listinfo/python-list
Gracefull application exit.
Chaps, I'm looking to implement an exit/termination process for an application which can be triggered by A) a keyboard interrupt or B) termination of the application as a Daemon using a signal. I have a whole bunch of tasks I want to perform as a cleanup before the application is terminated, cleaning files, database calls, closing open connections and a few other things. I know we have: # I'm the main application started method. if __name__ == "__main__": For starting an application, but do we have an equivalent methods for when an application is terminated which we can use to close down any current internal processes before the application exits? I'd really love to have your thoughts and experience on gracefully killing an application. Robert -- http://mail.python.org/mailman/listinfo/python-list
RE: Gracefull application exit.
> but mind the caveats: Thanks Tim, the main caveat which worries me is the fact that it doesn't get called when being killed by SIGTERM. When I look at implementing the application as Daemon will cause complications. The implementation looks nice and is certainly the way I would like to go, but unless it'll work when killed by sigterm it likely won't be for me. Thanks Tim, if you're aware of any other methods then give me a shout. Rob -- http://mail.python.org/mailman/listinfo/python-list
GUID Generation
Chaps, I've been looking at the following recipe for generating a GUID http://code.activestate.com/recipes/163604/ however I note that its about 6 years old. Has python since adopted an internal module for GUID generation? Or is this 3rd part module still the best option? Cheers, Robert -- http://mail.python.org/mailman/listinfo/python-list
RE: GUID Generation
> Not only is the answer, Yes: > > http://docs.python.org/lib/module-uuid.html > > but it's just featured as Doug Hellmann's module of the Week: > > http://blog.doughellmann.com/2008/07/pymotw-uuid.html > > TJG Thanks Tim, Perfect! Robert -- http://mail.python.org/mailman/listinfo/python-list
Setting my Locale
Good morning Guys, I'm running python 2.5 on a Debian based system and I'm looking for your advice on how to set the locale for my application. I've read through the locale module documentation http://docs.python.org/lib/module-locale.html and tried a couple of the examples but can't seem to get it working. For instance, when I attempt to run the following code: import locale locale.setlocale(locale.LC_ALL, 'de_DE') I get an exception raised by the application which says: Traceback (most recent call last): File "locale_test.py", line 2, in locale.setlocale(locale.LC_ALL, 'de_DE') File "/usr/lib/python2.5/locale.py", line 478, in setlocale return _setlocale(category, locale) locale.Error: unsupported locale setting My questions are; what would be causing this error? Do I need any additional packages installed on the system for this locale support to work properly? How can I get a list of available locales? Many thanks for any advice guys, Robert -- http://mail.python.org/mailman/listinfo/python-list
XML Processing
Guys, I'm running python 2.5 and currently using ElementTree to perform my XML parsing and creation. ElementTree really is a great package for doing this, however, I've been tasked by our deployment guys to try and move away from external libraries where possible as it makes their job easier. Simple question I suppose to start with, does Python have any inbuilt XML processing modules? If the answer is no then I'll stick with eTree, if python does have one, then I'll look at some migration steps. Many thanks All, Robert -- http://mail.python.org/mailman/listinfo/python-list
RE: XML Processing
> Some is going to kick themselves when they realise > that ElementTree *is* built in to Python 2.5 > > http://docs.python.org/whatsnew/modules.html#SECTION000142 Tim, Andrii, Thanks for the heads up on that! I hadn't noticed they're made it part of the platform modules, that's excellent news. In theory I should just be able to amend my import paths and we'll be good to go, no install external modules. Thanks for this, Robert -- http://mail.python.org/mailman/listinfo/python-list
Minimize Bandwidth
Hello Chaps, I have a python application that hits a web service on a regular basis to post a string of CSV log data and I'm looking to minimize the amount of bandwidth that the application uses to send the log data. Is there any way to encode the string into base64 or something, that I can then post and decode at the other end? Is that likely to save me bandwidth perhaps? I don't really know much about this encoding stuff, but the smaller I can compress the string the better. Thanks, Rob -- http://mail.python.org/mailman/listinfo/python-list
Binary / SOAPpy
Hello Guys, I have a WebService call which returns an array, the first element in that array is the binary for a zip file, however I'm having trouble writing that binary string into an actual file when it arrives, I've tried the following method. Result = call to the webservice that returns the array. file = open("Zips/1.zip", "wb") file.write(result[0]) file.close() But this throws the error message: file.write(result[0]) TypeError: argument 1 must be string or read-only buffer, not instance Does anyone know what I might be doing wrong? Once I've resaved this file can then unzip it and get at all its lovely content. Thanks for any input guys, Rob -- http://mail.python.org/mailman/listinfo/python-list
Strange Thread Issue
Hello Guys, I'm having an issue with a thread which I've not come across before and it has be baffled. The thread doesn't really do a lot, it simple contains a popen command to run something from cmd, now then i trigger the thread form my main application using the .start() method nothing happens, the command prompt program isn't triggered, yet as soon as a ctrl+c to close my application the thread then seems to kick into life and work. Any ideas what is causing this? Thanks guys, Rob -- http://mail.python.org/mailman/listinfo/python-list
Zip File Woes
Hello Guys, I'm having a MASSIVE headache today with zip files, I had it working a while ago but it all seems to have stopped in the past 30 minutes and I can't figure out why. I'm simply trying to write a function that will unzip a file, simple as that. I've currently got this code: Import zipfile zip = zipfile.ZipFile('Media/Media.zip', 'r') unzip(zip) zip.close() def unzip(zip): for name in zip.namelist(): newname = 'Media/%s' % (name) file(newname, 'wb').write(zip.read(name)) Now when I try and run this i get the following error message: File "/usr/lib/python2.4/zipfile.py", line 242, in _RealGetContents raise BadZipfile, "File is not a zip file" zipfile.BadZipfile: File is not a zip file However, if I copy the zip file off the unit client onto my windows box and unzip it and it works absolutely perfectly, all the files are extracted as I would expect, which leads me to think the zip file is fine, and i must just be missing something in my code. Any ideas guys? I'm tearing my hair out here. Rob -- http://mail.python.org/mailman/listinfo/python-list
RE: Zip File Woes
Just as an update guys: Before my zip code featured below I have another piece of code that creates the zip file from a binary string, like this: #f3 = open("Media/Media.zip", "wb") #f3.write(base64.decodestring(MediaBinary)) #f3.close Now, with that code commented out like that so the unzip code is running on the file generated last time the code was run it works fine, but if I try and unzip after the file has been freshly created I get that error, I've even tried placing a 2 second sleep command in between them and still get the problems. Thanks guys, Rob From: [EMAIL PROTECTED] [mailto:[EMAIL PROTECTED] On Behalf Of Robert Rawlins - Think Blue Sent: 27 June 2007 15:10 To: python-list@python.org Subject: Zip File Woes Hello Guys, I'm having a MASSIVE headache today with zip files, I had it working a while ago but it all seems to have stopped in the past 30 minutes and I can't figure out why. I'm simply trying to write a function that will unzip a file, simple as that. I've currently got this code: Import zipfile zip = zipfile.ZipFile('Media/Media.zip', 'r') unzip(zip) zip.close() def unzip(zip): for name in zip.namelist(): newname = 'Media/%s' % (name) file(newname, 'wb').write(zip.read(name)) Now when I try and run this i get the following error message: File "/usr/lib/python2.4/zipfile.py", line 242, in _RealGetContents raise BadZipfile, "File is not a zip file" zipfile.BadZipfile: File is not a zip file However, if I copy the zip file off the unit client onto my windows box and unzip it and it works absolutely perfectly, all the files are extracted as I would expect, which leads me to think the zip file is fine, and i must just be missing something in my code. Any ideas guys? I'm tearing my hair out here. Rob -- http://mail.python.org/mailman/listinfo/python-list
Threads Dying?
Hello Guys, I've got an application that seems to be a little bit unstable and freezes quite a bit, and I'm suspecting it's something in one of my threads that's causing the problem, when does a thread die? And how can I be sure that its dyeing when its mean to be? Thanks guys, Rob -- http://mail.python.org/mailman/listinfo/python-list
XML Parsing Help,
Hello Chaps, I'm looking for some help with XML parsing, I've been playing around with this over the past few days and the only solution I can come up with seems to be a little slow and also leaves what I think is a memory leak in my application, which causes all kinds of problems. I have a very simple XML file which I need to loop over the elements and extract the attribute information from, but the loop must be conditional as the attributes must meet a certain criteria. My current solution is using minidom, which I've read isn't one of the better parsers, if anyone knows of any that are better for the task I would love to hear it, the content is extracted regularly so whatever we chose needs to be quick. Take a look at this brief example of the XML we're dealing with: Now what I need to do is loop through the 'event' elements and locate the first one which has a type of '2' and return the name and location, if it is unable to find any events with the type of 2 then I want it to return the default event which is defined by the attributes of the schedules element. I'm pretty inexperienced with parsing XML in python and could really use some help selecting a parser and writing the code, I'm sure it's all quite simple I'm just looking for the best solution. Thanks guys, Rob -- http://mail.python.org/mailman/listinfo/python-list
Application Crashing
Hello Guys, I find my application is freezing/crashing every now and then and it becoming a bit of a frustration, so this morning I sat down to try and locate the problem. After doing some debugging I think I've found the line of code that causes my problem. Print 'Read Results' New = e.read() Print 'Results Read' The last thing the application does is print the words 'Read Results' which leads me to think that the troublesome line is e.read(). This line is reading the contents of a file object created by a popen command. This runs without fail most of the time, but on a reasonably regular occurrence it seems to crash my app. I've tried wrapping it in a try/except, which I don't know much about and that doesn't appear to make any difference, the app still crashes. print 'Reading Push' try: new = e.read() except: new = 'fault' Print 'Results Read' I'm not sure if this makes any difference, but this is all being run from within a thread. I need some help on handling this a little better to avoid my application from dying on me, if anyone can offer some advice on what can be done it would be greatly appreciated, I'm hoping just a small code solution will be available. Thanks guys, I look forward to hearing from you, Rob -- http://mail.python.org/mailman/listinfo/python-list
DatePart From String
Hello Guys, I have a date/time as a string which looks like this: 2007-02-01 00:00:00 I'm trying to get my hands on the different date parts of that string in the following formats: Time Only: 00:00:00 Day As Number: 01 Month As Number: 02 Day As Word: Monday I've tried using the time.strptime() function without much success so thought I'd come and ask your advice. Thanks guys for any advice, Rob -- http://mail.python.org/mailman/listinfo/python-list
RE: DatePart From String
Hello Dave, Thanks for getting back to me, I had been trying to use it like this: time.strptime('2007-01-01 00:00:00','%H:%M:%S') I was getting an error thrown at me: ValueError: time data did not match format: data=2007-01-01 00:00:00 fmt=%H:%M:%S I see how your solution works, but I'm hoping to trip is down a little bit as this has to be used in a conditional statement such as the one below: If time.strftime('%H:%M:%S') > time.strptime('2007-01-01 00:00:00','%H:%M:%S') Print 'Later In The Day' You see how that works? I'm basically trying to check if the current time is later that the one defined in my string. Are you able to give me a working example of how I might do this with my conditional? Thanks Dave, Rob -Original Message- From: [EMAIL PROTECTED] [mailto:[EMAIL PROTECTED] On Behalf Of Dave Sent: 03 July 2007 10:46 To: python-list@python.org Subject: Re: DatePart From String Robert Rawlins - Think Blue thinkbluemedia.co.uk> writes: > I’ve tried using the time.strptime() function without much success so > thought I’d come and ask your advice. > How exactly does it not work?? This works for me: from time import mktime, strptime from datetime import datetime datetime_string = '2007-02-01 00:00:00' datetime_object = datetime.fromtimestamp( mktime(strptime(datetime_string,'%Y-%m-%d %H:%M:%S'))) print datetime_object.year print datetime_object.month print datetime_object.day print datetime_object.hour print datetime_object.minute print datetime_object.second HTH, Dave -- http://mail.python.org/mailman/listinfo/python-list -- http://mail.python.org/mailman/listinfo/python-list
POpen - Wait For Process To Complete
Hello guys, Quite a simple one I'm hoping. I've got a process that I run using popen which writes to a file, I'm then going to read the file and parse its contents. However, getting the application to wait until the process is complete and finished writing before it reads and parses the file is becoming complicated. Popen is meant to have a wait() function along with a poll() function (according to the docs), both of which can be used to make the app wait for the process to finished, but for some reason I can't get them working, when I try this: Import popen2 Process = popen2.popen4('This is my command to run from cmd') Process.wait() f = open('path/to/output/file.txt') new = f.read() f.close() It throws back the following error to me: push.wait() AttributeError: 'tuple' object has no attribute 'wait' Which is fair enough as popen4 returns a tuple containing the output streams from the cmd command, but how the hell can I have it wait so I can read the physical files? I'll be interested to hear your thoughts guys, Rob -- http://mail.python.org/mailman/listinfo/python-list
Which Python Version
Hello Chaps, Is there a command I can run to confirm which version of python I'm running? Another thing I've always wondered, should i be running my applications using './MyFile.py' or 'Python MyFile.Py' what are the benefits of each method? One thing I have noticed is that when I used 'Python MyFile.Py' my processor usage was a lot higher, is that normal? Thanks guys, Rob -- http://mail.python.org/mailman/listinfo/python-list
Which Python Version
Hello Chaps, Is there a command I can run to confirm which version of python I'm running? Another thing I've always wondered, should i be running my applications using './MyFile.py' or 'Python MyFile.Py' what are the benefits of each method? One thing I have noticed is that when I used 'Python MyFile.Py' my processor usage was a lot higher, is that normal? Thanks guys, Rob -- http://mail.python.org/mailman/listinfo/python-list
RE: Which Python Version
Thanks Tim, Greatly appreciated, I've been having a few problems with one of my apps recently crashing at all sorts of odd intervals without throwing an error or anything like that, So I'm upgrading to 2.5 to see if they'll make life any simpler. Thanks mate, Rob -Original Message- From: [EMAIL PROTECTED] [mailto:[EMAIL PROTECTED] On Behalf Of Tim Golden Sent: 04 July 2007 11:36 Cc: python-list@python.org Subject: Re: Which Python Version Robert Rawlins - Think Blue wrote: > Is there a command I can run to confirm which version of python I'm running? From outside Python: python -V (that's a capital V) From inside Python: import sys print sys.version (and a couple of more easily parseable alternatives; look at the sys module docs) TJG -- http://mail.python.org/mailman/listinfo/python-list -- http://mail.python.org/mailman/listinfo/python-list
Which Python Version
Hello Chaps, Is there a command I can run to confirm which version of python I'm running? Another thing I've always wondered, should i be running my applications using './MyFile.py' or 'Python MyFile.Py' what are the benefits of each method? One thing I have noticed is that when I used 'Python MyFile.Py' my processor usage was a lot higher, is that normal? Thanks guys, Rob -- http://mail.python.org/mailman/listinfo/python-list
Which Python Version
Hello Chaps, Is there a command I can run to confirm which version of python I'm running? Another thing I've always wondered, should i be running my applications using './MyFile.py' or 'Python MyFile.Py' what are the benefits of each method? One thing I have noticed is that when I used 'Python MyFile.Py' my processor usage was a lot higher, is that normal? Thanks guys, Rob -- http://mail.python.org/mailman/listinfo/python-list
Which Python Version
Hello Chaps, Is there a command I can run to confirm which version of python I'm running? Another thing I've always wondered, should i be running my applications using './MyFile.py' or 'Python MyFile.Py' what are the benefits of each method? One thing I have noticed is that when I used 'Python MyFile.Py' my processor usage was a lot higher, is that normal? Thanks guys, Rob -- http://mail.python.org/mailman/listinfo/python-list
GObject and Python 2.5
Hello Guys, Firstly I should apologise for all the mails that keep landing on the list, my SMTP server (supplied by my ISP) seems to be playing silly buggers and sending multiples. I've just installed Python 2.5 on my Debian system and I'm trying to run my application and I get 'ImportError: No module named gobject' thrown back at me. I think I've successfully install the glib library using apt-get but the problem still persists. There is obviously a module I'm missing, any ideas what it might be? Thanks guys, Rob -- http://mail.python.org/mailman/listinfo/python-list
Parsing Help
Hello Guys, I'm looking for some help building a function which can parse some XML for me using ElementTree. The document is of a very consistent format and I've copied an example of the document below. Now, the piece of information I'm looking to retrieve is inside the element and is, in this example , however I want the function to return the standard integer value and not the unit8 encoded version, so instead of my function returning '0x05' it just needs to return '5' which is the standard integer version. I will be passing this XML into the function as a string, so the function will be formed something like this: Def myFunction(XmlAsString): Pass the xml and exract my value Return the value as an integer... I'm not sure on the best method to do this, I just want something nice and quick, lightweight and that's not resource hungry. Can anyone offer some advice on this? Thanks guys, Rob -- http://mail.python.org/mailman/listinfo/python-list
Time A Script
Hello Guys, What's the best way to time how long it takes a script to run, from top to bottom and then have it print that execution time at the end? Thanks guys, Rob -- http://mail.python.org/mailman/listinfo/python-list
Launch One Application From Another
Hello Guys, I'm looking for the best method to launch one Python application from another? Is something like sub process going to be my best bet? Or is there something more specific for launching one python app from another? Thanks guys, Rob -- http://mail.python.org/mailman/listinfo/python-list
RE: Dict Help
Thanks Gabriel, That all works a charm, Iteration on this isnt important for me so the SET will work much better I think, I had read about them before but have never used them, I'm glad to see it works so simply. Thanks, Rob -Original Message- From: [EMAIL PROTECTED] [mailto:[EMAIL PROTECTED] On Behalf Of Gabriel Genellina Sent: 15 July 2007 20:33 To: python-list@python.org Subject: Re: Dict Help En Sun, 15 Jul 2007 11:39:24 -0300, Robert Rawlins <[EMAIL PROTECTED]> escribió: > I'm looking for some help expanding on a dictionary I've got and storing > multiple values per key and the best way to do it. I'm guessing that I > need to store a list inside the value of the dictionary, but I'm not > quite sure how that can be achieved, and also how to check for values in > that list. > Here is a brief example of what I want to be able to build: > Key Value > 00:0F:DE:A8:AB:6F > 6C66F14B-FED6-D1A4-62EAAD881E9133F0.jpg,01DBB592-F7EB-B000-7F250FD8A2CE158F. gif,533EAE0F-B211-B2D8-4C2DB662CCECFBD7.3gp > 00:17:B0:A0:E7:09 > 6C66F14B-FED6-D1A4-62EAAD881E9133F0.jpg,01DBB592-F7EB-B000-7F250FD8A2CE158F. gif > 00:1B:98:16:21:E4 > 6C66F14B-FED6-D1A4-62EAAD881E9133F0.jpg There are a few ways to do it. Just translating your own words into Python, use a dictionary with a list of values. > Now I really need to have two functions, one of which appends a value to > the list for a specified key, so I have the key and the new value as > strings, I then want to locate that key and append the list with the new > value. The simplest way: d = {} # add a pair key, value if key not in d: d[key] = [value] else: d[key].append(value) > The second function I need, is to check for the existence of a value in > the list of a particular key, again I have the key and the value and I > want to do something like: > if value in list of values for key: >do something here... # check for a key, value exists = key in d and value in d[key] > The final function that would be helpful to me would to be able to > remove a value from the list for a specific key. # remove value from key's values if key in d and value in d[key]: d[key].remove(value) > I'm not sure if a list as the value in a dict is possible, or if its the > best way to achieve this, It just made logic sense to me so thought I'd > come and get your thoughts on this. If anyone has any better suggestions It's perfectly possible as you can see. Now, depending on how many values you have, or how often do you test, you might replace the list with a set. Sets are unordered collections (you will loose the insertion order) but are better suited for a "contains" test (O(1) instead of O(n) for a list) And you might replace the dictionary with a defaultdict. The insertion would become: from collections import defaultdict d = defaultdict(set) # add a pair key, value d[key].add(value) The existence check and remove method do not change. Dicts, lists and sets are documented here: <http://docs.python.org/lib/types.html> and defaultdict <http://docs.python.org/lib/defaultdict-objects.html> -- Gabriel Genellina -- http://mail.python.org/mailman/listinfo/python-list -- http://mail.python.org/mailman/listinfo/python-list
Access Object From 2 Applications or Fix Scheduler
Hello Guys, I have two applications which I need to get talking and sharing an object, what's the best way to do this? Basically my first application parses an XML document into a bunch of lists and tuples etc, and I need to access the data in these lists and tuples from my second application. Is this possible? And what's the simplest method? This problem all evolves from a hack/work around I've had to place together as I have been having problems with sched, these two applications used to be a single one to start with but for some reason when scheduling tasks using the sched module they wouldn't ever run when supposed too, so I'd schedule it to run every 2 minutes and it would run between 3 and 15 minutes :-s so I ripped it out into its own application and it now runs every 2 minutes exactly. I either need to fix my sched issues or the memory share, either way I'm quite happy, just need to get it working smoothly. Any ideas? Thanks guys, Rob -- http://mail.python.org/mailman/listinfo/python-list
RE: Access Object From 2 Applications or Fix Scheduler
Thanks Hendrik & Alex for your suggestions, I'm glad to see that there are lots of options for me here, and I'll give them all of a decent exploration to see what my best option is, starting the second app in a named pipe sounds like a fair solution, but I've not don't this before, how easy is it to have the second application ready to receive the new dictionary data? Just out of interest though I'd still like to know why my first application schedules didn't work properly, should I be starting them all in their own thread? Would that work better? Thanks Guys, Rob -Original Message- From: [EMAIL PROTECTED] [mailto:[EMAIL PROTECTED] On Behalf Of Hendrik van Rooyen Sent: 16 July 2007 07:29 To: python-list@python.org Subject: Re: Access Object From 2 Applications or Fix Scheduler Robert Rawlins - Think Blue wrote: >Hello Guys, > >I have two applications which I need to get talking and sharing an >object, what s the best way to do this? Basically my >first application parses an XML document into a bunch of lists and tuples etc, and I need to access the data in these lists >and tuples from my second application. Is this possible? And whats the simplest method? > >This problem all evolves from a hack/work around Ive had to place >together as I have been having problems with >sched, these two applications used to be a single one to start with but for some reason when scheduling tasks using the >sched module they wouldnt ever run when supposed too, so Id schedule >it to run every 2 minutes and it would run >between 3 and 15 minutes :-s so I ripped it out into its own application and it now runs every 2 minutes exactly. > >I either need to fix my sched issues or the memory share, either way >Im quite happy, just need to get it working >smoothly. > >Any ideas? > You have already split the thing, so I would: schedule the XML parser and make it do its stuff. then pickle the results using cPickle then from this first thing, either: write the pickles to temp files and start the second thing using eg os.system() or start the second thing and use a named pipe to pass the pickles over for unpickling and processing, or use one of the popens, or have a look at Pyro, or start the second thing as a thread and use a Queue.. of course this whole scheme will fall apart if the combined processing takes longer than the scheduling interval. HTH - Hendrik -- http://mail.python.org/mailman/listinfo/python-list
RE: Access Object From 2 Applications or Fix Scheduler
Also Hendrik, I should probably mention that the second application is a constant running application, it's nothing something that can just be 'started' by the first one, its running non-stop and just needs data passed into it regularly when the first application finds it. Application 1 Application 2 Work with dict Work with dict Work with dict Work with dict New XML Found, Parse Into Dict --> Work with new dict Work with new dict Work with new dict Work with new dict You see how that works? Application 1 has a function that runs every minute and _may_ find a new xml file, if it does then I need it to parse that file into a list of dictionary and then pass that into application 2, which then starts using it :-) Now we may be able to avoid this if there is some type of file watcher function available in python, my second application could then just watch the XML file and as soon as a new one is available parse it itself. Is that something you've heard of? Thanks, Rob -Original Message- From: [EMAIL PROTECTED] [mailto:[EMAIL PROTECTED] On Behalf Of Hendrik van Rooyen Sent: 16 July 2007 07:29 To: python-list@python.org Subject: Re: Access Object From 2 Applications or Fix Scheduler Robert Rawlins - Think Blue wrote: >Hello Guys, > >I have two applications which I need to get talking and sharing an >object, what s the best way to do this? Basically my >first application parses an XML document into a bunch of lists and tuples etc, and I need to access the data in these lists >and tuples from my second application. Is this possible? And whats the simplest method? > >This problem all evolves from a hack/work around Ive had to place >together as I have been having problems with >sched, these two applications used to be a single one to start with but for some reason when scheduling tasks using the >sched module they wouldnt ever run when supposed too, so Id schedule >it to run every 2 minutes and it would run >between 3 and 15 minutes :-s so I ripped it out into its own application and it now runs every 2 minutes exactly. > >I either need to fix my sched issues or the memory share, either way >Im quite happy, just need to get it working >smoothly. > >Any ideas? > You have already split the thing, so I would: schedule the XML parser and make it do its stuff. then pickle the results using cPickle then from this first thing, either: write the pickles to temp files and start the second thing using eg os.system() or start the second thing and use a named pipe to pass the pickles over for unpickling and processing, or use one of the popens, or have a look at Pyro, or start the second thing as a thread and use a Queue.. of course this whole scheme will fall apart if the combined processing takes longer than the scheduling interval. HTH - Hendrik -- http://mail.python.org/mailman/listinfo/python-list
RE: Dict Help
Morning Gabriel, I'm looking for a little more advice on this dictionary/list to defaultdict/set conversion that we were talking about, there were a few things I was looking to clarify. Firstly, what is the difference between a standard dict and a default dict? Is it purely a performance issue? This dict is likely to grow pretty large and is read/written on a very regular basis so the better performing one is going to work best for me. Also, am I still able to iterate over a set in the same way I can a list? Here is an example of my add function at the moment, how can that be converted to a defaultdict/set instead of the dict/list approach? self.History = {} def addHistory(self, address, media): if address not in self.History: self.History[address] = [] self.History[address].append(media) Thanks Gabriel, Rob -Original Message- From: [EMAIL PROTECTED] [mailto:[EMAIL PROTECTED] On Behalf Of Robert Rawlins - Think Blue Sent: 15 July 2007 20:47 To: 'Gabriel Genellina'; python-list@python.org Subject: RE: Dict Help Thanks Gabriel, That all works a charm, Iteration on this isnt important for me so the SET will work much better I think, I had read about them before but have never used them, I'm glad to see it works so simply. Thanks, Rob -Original Message- From: [EMAIL PROTECTED] [mailto:[EMAIL PROTECTED] On Behalf Of Gabriel Genellina Sent: 15 July 2007 20:33 To: python-list@python.org Subject: Re: Dict Help En Sun, 15 Jul 2007 11:39:24 -0300, Robert Rawlins <[EMAIL PROTECTED]> escribió: > I'm looking for some help expanding on a dictionary I've got and storing > multiple values per key and the best way to do it. I'm guessing that I > need to store a list inside the value of the dictionary, but I'm not > quite sure how that can be achieved, and also how to check for values in > that list. > Here is a brief example of what I want to be able to build: > Key Value > 00:0F:DE:A8:AB:6F > 6C66F14B-FED6-D1A4-62EAAD881E9133F0.jpg,01DBB592-F7EB-B000-7F250FD8A2CE158F. gif,533EAE0F-B211-B2D8-4C2DB662CCECFBD7.3gp > 00:17:B0:A0:E7:09 > 6C66F14B-FED6-D1A4-62EAAD881E9133F0.jpg,01DBB592-F7EB-B000-7F250FD8A2CE158F. gif > 00:1B:98:16:21:E4 > 6C66F14B-FED6-D1A4-62EAAD881E9133F0.jpg There are a few ways to do it. Just translating your own words into Python, use a dictionary with a list of values. > Now I really need to have two functions, one of which appends a value to > the list for a specified key, so I have the key and the new value as > strings, I then want to locate that key and append the list with the new > value. The simplest way: d = {} # add a pair key, value if key not in d: d[key] = [value] else: d[key].append(value) > The second function I need, is to check for the existence of a value in > the list of a particular key, again I have the key and the value and I > want to do something like: > if value in list of values for key: >do something here... # check for a key, value exists = key in d and value in d[key] > The final function that would be helpful to me would to be able to > remove a value from the list for a specific key. # remove value from key's values if key in d and value in d[key]: d[key].remove(value) > I'm not sure if a list as the value in a dict is possible, or if its the > best way to achieve this, It just made logic sense to me so thought I'd > come and get your thoughts on this. If anyone has any better suggestions It's perfectly possible as you can see. Now, depending on how many values you have, or how often do you test, you might replace the list with a set. Sets are unordered collections (you will loose the insertion order) but are better suited for a "contains" test (O(1) instead of O(n) for a list) And you might replace the dictionary with a defaultdict. The insertion would become: from collections import defaultdict d = defaultdict(set) # add a pair key, value d[key].add(value) The existence check and remove method do not change. Dicts, lists and sets are documented here: <http://docs.python.org/lib/types.html> and defaultdict <http://docs.python.org/lib/defaultdict-objects.html> -- Gabriel Genellina -- http://mail.python.org/mailman/listinfo/python-list -- http://mail.python.org/mailman/listinfo/python-list -- http://mail.python.org/mailman/listinfo/python-list
Subprocess Poll() to sport Application Crash
Hello Guys, I have an application which launches a sub-process using the sub process/Popen module, which seems to work great. However on occasion the launched process will crash, it doesn't appear to full terminate though as when I run a 'top' command from the Linux system the process is still displayed in the list, it just appears to be dormant and the TIME+ for the application stops incrementing. Can I use the poll() method from the sub-process object to test for this? I would love to test it however the crash is so rare it's very difficult to simulate, so thought I'd try and see if anyone has any experience of this. Thanks chaps, Rob -- http://mail.python.org/mailman/listinfo/python-list
Subprocess Poll
Hello Guys, I have an application which launches a sub-process using the sub process/Popen module, which seems to work great. However on occasion the launched process will crash, it doesn't appear to full terminate though as when I run a 'top' command from the Linux system the process is still displayed in the list, it just appears to be dormant and the TIME+ for the application stops incrementing. Can I use the poll() method from the sub-process object to test for this? I would love to test it however the crash is so rare it's very difficult to simulate, so thought I'd try and see if anyone has any experience of this. Thanks chaps, Rob -- http://mail.python.org/mailman/listinfo/python-list
Copy List
Hello Guys, What's the best way to create a copy of a list? I've seen several method and I'm not sure what to use. This will be in a class and one method creates a list which I then want to move to the self scope, like so: __Init__(self): Self.myList = [] regenerateList(self): newList = [] for somthing in somthing: newList.append('SomeStuff') self.Mylist = newList.copy() See, The iteration and rebuilding of the list could be quite slow, during which time the application could be reading from the self.mylist variable so i think simply doing this: regenerateList(self): self.myList = [] for somthing in somthing: self.myList.append('SomeStuff') Might cause a few inconsistencies in the list if it's being read from whilst I'm appending too it, so I'm guessing that the top example is the best method, I'm just looking for the best way to copy() that list into the self scope, should i be using copy(), deepcopy() or simply self.mylist = newlist? Thanks guys, Rob -- http://mail.python.org/mailman/listinfo/python-list
Log Memory Usage
Hello Guys, I have an embedded application with a suspected memory leak which I'm trying to confirm. You see, the application seems to crash unexpectedly and when looking at the resource consumption after the crash system memory has crept up to nearly 100%. However this takes quite a long time to occur, so sitting and watching the application run isn't a very effective way of doing it, so I'm looking for the best way to log the system memory every minute or two. I have a scheduled event which occurs every minute, i just need a code solution to give me the systems current memory consumptions details, is there perhaps something in the os module? Thanks for any suggestions guys, Rob -- http://mail.python.org/mailman/listinfo/python-list
DateTime Differance
Hello Guys, I've used the eGenix date time module to create two date time objects which I'm now looking to compare like so: If date 1 is more than 5 minutes older than date 2: Do something here... After reading through the egenix documentation I'm still a little confused as the best way to do this. Can anyone offer any advice? Thanks guys, Rob -- http://mail.python.org/mailman/listinfo/python-list
Byte-Array to String
Hello Guys, I have a byte array passed to me by dbus and I'm looking to convert it into a string? Is that possible? Sorry for seeming like a putts with these questions, I'm not used to all these complex data types :-D The byte array looks something like this when printed to screen. dbus.Array([dbus.Byte(54), dbus.Byte(0), dbus.Byte(24), dbus.Byte(9), dbus.Byte(0), dbus.Byte(0), dbus.Byte(10), dbus.Byte(0), dbu s.Byte(0), dbus.Byte(0), dbus.Byte(0), dbus.Byte(9), dbus.Byte(0), dbus.Byte(1), dbus.Byte(53), dbus.Byte(3), dbus.Byte(25), dbus. Byte(16), dbus.Byte(0), dbus.Byte(9), dbus.Byte(2), dbus.Byte(0), dbus.Byte(53), dbus.Byte(3), dbus.Byte(9), dbus.Byte(1), dbus.By te(1)], signature=dbus.Signature('y')) Thanks again, Rob -- http://mail.python.org/mailman/listinfo/python-list
RE: Byte-Array to String
Thanks for getting back to me on this Tim, *pauses pulling his hair out for a moment. I'm back on this damn API and its driving me crazy I get three options really for returning a service record from the API, they look like this. array{uint32} GetRemoteServiceHandles(string address, string match) This method will request the SDP database of a remote device and retrieve the service record handles. To request service browse send an empty match string. Possible errors: org.bluez.Error.InvalidArguments org.bluez.Error.InProgress org.bluez.Error.ConnectionAttemptFailed org.bluez.Error.Failed array{byte} GetRemoteServiceRecord(string address, uint32 handle) This method will request the SDP database of a remote device for a service record and return the binary stream of it. Possible errors: org.bluez.Error.InvalidArguments org.bluez.Error.InProgress org.bluez.Error.Failed string GetRemoteServiceRecordAsXML(string address, uint32 handle) This method will request the SDP database of a remote device for a service record and return its data in XML format. Possible errors: org.bluez.Error.InvalidArguments org.bluez.Error.InProgress org.bluez.Error.Failed The first method, when I print its results just gives me dbus.Array([dbus.UInt32(65547L)], signature=dbus.Signature('u')) the method gives me that byte array and the third doesn't appear to work at all :-D Unfortunately nowhere seems to document what exactly those arrays contain, so I'm trying to crack one open and find out, but as you can see, I'm not having much luck. Perhaps this is a DBus thing, they do have a mailing list so I'll give them a go in a while. If you have any brain waves let me know mate, I'll add another beer to the tab, Rob -Original Message- From: [EMAIL PROTECTED] [mailto:[EMAIL PROTECTED] On Behalf Of Tim Golden Sent: 19 April 2007 15:54 Cc: python-list@python.org Subject: Re: Byte-Array to String Robert Rawlins - Think Blue wrote: > I have a byte array passed to me by dbus and I'm looking to convert it into > a string? Is that possible? Sorry for seeming like a putts with these > questions, I'm not used to all these complex data types :-D > > dbus.Array([dbus.Byte(54), dbus.Byte(0), dbus.Byte(24), dbus.Byte(9), > dbus.Byte(0), dbus.Byte(0), dbus.Byte(10), dbus.Byte(0), dbu > s.Byte(0), dbus.Byte(0), dbus.Byte(0), dbus.Byte(9), dbus.Byte(0), > dbus.Byte(1), dbus.Byte(53), dbus.Byte(3), dbus.Byte(25), dbus. > Byte(16), dbus.Byte(0), dbus.Byte(9), dbus.Byte(2), dbus.Byte(0), > dbus.Byte(53), dbus.Byte(3), dbus.Byte(9), dbus.Byte(1), dbus.By > te(1)], signature=dbus.Signature('y')) No idea, but what happens when you iterate over it? for i in array_thingy: print i Or does it support the buffer interface? for i in buffer (array_thingy): print i If it did then you can at least get access to its innards and construct some kind of string representation. I've no idea what the numbers are supposed to represent, so I don't know what "convert it to a string" is likely to imply. This looks rather more like a dbus question than a strictly Python one. Is there a DBus mailing list or what-have-you? TJG -- http://mail.python.org/mailman/listinfo/python-list -- http://mail.python.org/mailman/listinfo/python-list
RE: Byte-Array to String
Morning Steve, That stuff looks mighty promising, I did play around with the toString() function yesterday but couldn't get the damned thing working. The syntax has me a little muddled, perhaps you can help out on this by taking a look at my code. #!/usr/bin/python import dbus bus = dbus.SystemBus() obj = bus.get_object('org.bluez', '/org/bluez') obj = bus.get_object('org.bluez', '/org/bluez/hci0') adapter = dbus.Interface(obj, 'org.bluez.Adapter') # Search For Obex Push Protocol result = adapter.GetRemoteServiceHandles('00:17:B0:A0:E7:09', 'opp') result2 = adapter.GetRemoteServiceRecord('00:17:B0:A0:E7:09', result[0]) Now 'result2' is basically that byte array, Unfortunately the API doesn't give any more information other than that the function returns a array{byte} so I can't really shed any more light on that however if I can just get it to output all the elements from that array then I will know which one it is I'm looking for. I tried to implement your suggestions below as they look pretty safe, but I couldn't get it to work, my syntax is pretty confused as I'm so new to the language. Like I say, there is only one element of the array I'm interested in, I just need to see them all before I know which it is. Thanks again Steve, Rob From: Steven Howe [mailto:[EMAIL PROTECTED] Sent: 19 April 2007 16:51 To: Robert Rawlins - Think Blue Cc: python-list@python.org Subject: Re: Byte-Array to String Robert Rawlins - Think Blue wrote: Hello Guys, I have a byte array passed to me by dbus and I'm looking to convert it into a string? Is that possible? Sorry for seeming like a putts with these questions, I'm not used to all these complex data types :-D The byte array looks something like this when printed to screen. dbus.Array([dbus.Byte(54), dbus.Byte(0), dbus.Byte(24), dbus.Byte(9), dbus.Byte(0), dbus.Byte(0), dbus.Byte(10), dbus.Byte(0), dbu s.Byte(0), dbus.Byte(0), dbus.Byte(0), dbus.Byte(9), dbus.Byte(0), dbus.Byte(1), dbus.Byte(53), dbus.Byte(3), dbus.Byte(25), dbus. Byte(16), dbus.Byte(0), dbus.Byte(9), dbus.Byte(2), dbus.Byte(0), dbus.Byte(53), dbus.Byte(3), dbus.Byte(9), dbus.Byte(1), dbus.By te(1)], signature=dbus.Signature('y')) Thanks again, Rob When reading about array, I wondered what the hell it was good for. Now I see. It's a tool to build objects to pass to the Operating System or other applications. Something like ctypes. The OS might store data from left to right, or right to left, or not use IEEE standards (which VMS certainly doesn't). So the data you give/get from the system call must be massaged by the application before it's usable. python/lib/module-array.html (5.14 array -- Efficient arrays of numeric values) array.tostring( ) Convert the array to an array of machine values and return the string representation (the same sequence of bytes that would be written to a file by the tofile() method.) I wonder if this is the method you are looking for. So you have an object dbus.Array, which, obviously is from a call to the dbus (another application's data) that contains 28 byte arrays. I would assume you know which you want, say the first one. myDbusString01 = dbus.Array[0].tostring() or to get the lot: myDbusStrings = [] #create a new empty list for array in dbus.Array: myDbusStrings.append( array.tostring() ) At this point you should have the array converted. But you will still need a reference as to what you have. The call to the dbus should have some documentation abut what it's returning. Also I'd expect the second example to be very bad programming, as some of the array elements are probably not going to be characters. They could be integers, floats or booleans. So creating a dictionary to handle specific array element handling is probably a better, less error prone, method of attack. Not have the contents and defination of your dbus.array handy, I can't test this, but the approach seems reasonable. Steven Howe -- http://mail.python.org/mailman/listinfo/python-list
RE: Byte-Array to String
Thanks for that Carsten, I've given that a go and I still get similar results to what I've seen in the past whereby it prints a couple of elements I would expect to see but other which I'm not sure about, and what seems to be ALOT of blank characters. 6e 555 5 5 5 en j 5 %OBEX Object Push ÿ That's what your suggestion returns to me, I'm using the API to scan a Bluetooth device for services, one of which is OBEX Object Push, but the rest of that output is apparent junk. Unfortunately the API doesn’t really give any suggestions at all, take a look here http://bluez.cvs.sourceforge.net/*checkout*/bluez/utils/hcid/dbus-api.txt if you look down under the 'adapter' section, I'm running the method called GetRemoteServiceRecord() which returns the byte array but there really isn’t any suggestion as to how you extract the information from what's returned. I have no idea if we're barking up the right tree here or if we're going about this the wrong way. Thanks again for all your helps guys, Rob -Original Message- From: Carsten Haese [mailto:[EMAIL PROTECTED] Sent: 20 April 2007 14:00 To: Robert Rawlins - Think Blue Cc: python-list@python.org Subject: RE: Byte-Array to String On Fri, 2007-04-20 at 09:51 +0100, Robert Rawlins - Think Blue wrote: > Morning Steve, > > > > That stuff looks mighty promising, I did play around with the > toString() function yesterday but couldn’t get the damned thing > working. That's because Steven seems to have given you suboptimal advice. The thing you're working with is a dbus.Array of dbus.Byte, not a Python array, so Python's standard array module is not going to help you. When you work with an API function and you don't know what to do with the results of calling that function, the documentation for that API should be your first place to look for help. I don't know what you're referencing, but Google tells me that the python-dbus API is documented here: http://dbus.freedesktop.org/doc/dbus-python/api/ So, what do you do with a dbus.Array full of instances of dbus.Byte? Let's look at their documentation, http://dbus.freedesktop.org/doc/dbus-python/api/dbus.Array-class.html and http://dbus.freedesktop.org/doc/dbus-python/api/dbus.Byte-class.html, respectively. The page about dbus.Array tells us that it inherits from list, so you should be able to do indexed access into it and iterate over it. dbus.Byte inherits from int, and the documentation says that a dbus.Byte can be converted to a character (string of length 1) by calling str or chr on it. So, to get a string from your dbus.Array, you could do something like this: s = "" for b in myDbusArray: s += chr(b) However, it's better (faster) to use the join idiom for building a string char-by-char: s = "".join(chr(b) for b in myDbusArray) That ought to give you the string you're asking for. HTH, Carsten. -- http://mail.python.org/mailman/listinfo/python-list
Schedule Task
Hello Guys, I've got a method in my app that I want to run every 20 minutes that the application it running, what's the best way of achieving this? At the moment I'm using this kind of method. import sched import time, sys scheduler = sched.scheduler(time.time, time.sleep) scheduler.enter(1.0, 0, sys.stdout.write, ("one\n",)) scheduler.run() Now when the function runs after 20 minutes I just have it schedule itself to run again in 20 minutes time, which seems to work for me quite nicely, it just seems a little 'hacky' to keep the task reoccurring like that. Is there any other method? I've spent some time on Google but couldn't really find anything. Not sure if it makes any difference but I'm running Linux. Thanks, Rob -- http://mail.python.org/mailman/listinfo/python-list
Class Not Auto-Init On Import
Hello Guys, >From my understanding of what I've read, the 'import' is meant to auto Init my class ready for me to access its methods, but it doesn't appear too, I'm having to init them myself before I can access them, like this. import LocationService Location = LocationService.LocationService() LocationService.setIP('192.168.1.1') Why is this the case? Should i not just be able to access the setIP() method by doing LocationService.setIP('192.168.1.1') Without having to create my own inited reference? Thanks, Rob -- http://mail.python.org/mailman/listinfo/python-list
Styled Output
Chaps, Hope you're all having a good weekend, I'm sure it'll only be the more 'hard core' of you reading this, anyone with any sanity would be out in the sunshine right now. I'm running a program of mine from Linux bash script and it currently outputs status messages to the command prompt, but they all look a little boring to say the least, it'll be like this. Application Started Sucess Application Configured Failed Retry Application Configured Success What I'd ideally like to do it give it a little bit of format, so it perhaps looks something like this. Application Started [Success] Application Configured [Success] And perhaps have the word 'success' in green, or red for 'failed' and things like that, just to make it a little more presentable to the user. Any ideas on the best way to achieve this? Thanks, Rob -- http://mail.python.org/mailman/listinfo/python-list
Minidom Help
Hello guys, Not got much experience with working with minidom, but I'm looking to parse this XML and retrieve the 'name' value from the xml. Think Blue 0x1002 1 I've got as far as parsing the XML using self.doc = xml.dom.minidom.parse(self.filepath) but that's as far as I've managed to get. I've spent a little time googling around but it's all double Dutch to me. Any ideas? Thanks, Rob -- http://mail.python.org/mailman/listinfo/python-list
If Dict Contains a particular key
Hello Guys, I'm Looking to build a quick if/else statement that checks a dictionary for a key like follows. If myDict contains ThisKey: Do this... Else Do that... Thats the best way of doing this? Thanks, Rob -- http://mail.python.org/mailman/listinfo/python-list
RE: If Dict Contains a particular key
Thanks guys for this, glad it was so simple. I used mikes solution in the end, and it worked a charm. Thanks again, Rob From: [EMAIL PROTECTED] [mailto:[EMAIL PROTECTED] On Behalf Of Michael Bentley Sent: 24 April 2007 18:37 To: python-list@python.org Subject: Re: If Dict Contains a particular key On Apr 24, 2007, at 12:28 PM, Robert Rawlins - Think Blue wrote: Hello Guys, I'm Looking to build a quick if/else statement that checks a dictionary for a key like follows. If myDict contains ThisKey: Do this... Else Do that... Thats the best way of doing this? if ThisKey in myDict: pass # do this else: pass # do that -- http://mail.python.org/mailman/listinfo/python-list
If Dict Contains...
Hello Guys, Looking to build a quick if/else statement that checks a dictionary for a key like follows. If myDict contains ThisKey: Do this... Else Do that... Thats the best way of doing this? Thanks, Rob -- http://mail.python.org/mailman/listinfo/python-list
popen2 results
Hello guys, I've recently ported my application from bash to python, however there are still a few bash line utilities I -have- to use in the application as there isn't any alternative available to me. In the old days of bash I would have grep'd the output from these commands to determine the outcome. I'm now using popen2 to run the command which works a charm, but I'm struggling to parse the results of the function, has anyone had any experience with this? I've found a few suggested solutions dotted around, such as this one. import os def filtered(command, source): dest, result = os.popen2(command) dest.write(source) dest.close() try: return result.read() finally: result.close() But to be honest I'm struggling to get it to do anything as it doesn't states what the 'source' object is or should be. Thanks for any help guys, I'm just looking to capture the output from the command and then I can go about a little REGEX on it. Thanks, Rob -- http://mail.python.org/mailman/listinfo/python-list
Now()
Hello Guys, I'm using the following function 'str (now)' to place a date time stamp into a log file, which works fine, however it writes the stamp like this. 2007-04-25 11:06:53.873029 But I need to expel those extra decimal places as they're causing problems with the application that parses the log data, all I need is something like this: 2007-04-25 11:06:53 With time depicted to the nearest second, from my background in ColdFusion development we used to have a datetimeformat() function that I could use as DateTimeFormat(now(), "-mm-dd HH:mm:ss") Which would give the current time a mask. Any equivalent for this in python? Thanks, Rob -- http://mail.python.org/mailman/listinfo/python-list
Scheduler Module Help
Hello Guys, I'm using the sched module to create a set of functions that run every 10 or 20 minutes whilst the application is running, however it would seem that the moment I run scheduler.run() it prevents any other operations in my application from running, its sits dormant until it runs the scheduled functions. Any ideas on what might be the root of this issue? Thanks, Rob -- http://mail.python.org/mailman/listinfo/python-list
Re-ocurring Events
Hello Chaps, A bit more of a complex one this time, and I thought I'd get your opinions on the best way to achieve this. Basically I'm looking for a way to describe a re-occurring event, like a calendar event or appointment I guess. I'm likely to use an XML file for the definition of the events, but imagine I've got an event that looks something like this. Now what I want to do is be able to build a class which has a function like 'getCurrentEvent()' which will return any events that should be occurring at that time. So if the current system time and date is 2007-01-03 13:00:00 then it will return THAT event to me, but if it was say 2007-01-03 16:00:00 then it would not, as the event isn't 'due' to occur at that time. Make sense? What's the best way of handling this? I'm really a little lost as to how I might get started, checking a static date time isn't a problem, it's when it comes to these re-occurring events that I struggle a little. The idea is that I would have 5 core repetitions, none, daily, weekly, monthly and annually. Or perhaps you guys have a more full proof method of describing the event in the XML? Any suggestions are more than welcome guys, thanks again for all your help recently. Rob -- http://mail.python.org/mailman/listinfo/python-list
RE:: Re-ocurring Events
Thank you guys for your suggestions. I've been having a look at that launchd stuff from apple but couldnt really see how that applies to my requirements. I've been putting some serious thought into how this should work as its essentially the final part of the puzzle for my application, I'll have a fully working model for my application. I've been thinking about the possibility of using a combination of xpath to search the XML with some loops which change the date. As events won't ever be set to start before 01-01-2007 I can set that as the ceiling for my loop. So when I'm searching for weekly events, I 'simply' take today's date and time and loop from now until 01-01-2007 decrementing the date by a week each iteration of the loop and then search the XML for events in that date, make sense? I know that's a fairly intensive way of doing it, but if it works it works. Now, the loop is where I'm really struggling, I've not done any looping with dates, can anyone give me a hand with this? How can I loop back in time from now to beginning of 07 a week at a time? Do we have some form of dateAdd() I can use with a while loop? Perhaps. Date = (now) While date > 2007-01-01: Date = dateAdd(date, -1, w) Something to that effect? Then I can quickly xpath for every iteration of the loop. Thanks guys for any help. Rob -Original Message- From: [EMAIL PROTECTED] [mailto:[EMAIL PROTECTED] On Behalf Of Laurent Pointal Sent: 26 April 2007 15:33 To: python-list@python.org Subject: Re: Re-ocurring Events Daniel Nogradi a écrit : >> A bit more of a complex one this time, and I thought I'd get your >> opinions >> on the best way to achieve this. Basically I'm looking for a way to >> describe >> a re-occurring event, like a calendar event or appointment I guess. I'm >> likely to use an XML file for the definition of the events, but >> imagine I've >> got an event that looks something like this. >> >> > repeat="daily" >> /> May take a look at launchd (Apple) XML dialect, it may have at least the semantic for your need. For other parts... elementtree, yes. >> Now what I want to do is be able to build a class which has a function >> like >> 'getCurrentEvent()' which will return any events that should be >> occurring at >> that time. So if the current system time and date is 2007-01-03 13:00:00 >> then it will return THAT event to me, but if it was say 2007-01-03 >> 16:00:00 >> then it would not, as the event isn't 'due' to occur at that time. Make >> sense? >> >> What's the best way of handling this? I'm really a little lost as to >> how I >> might get started, checking a static date time isn't a problem, it's >> when it >> comes to these re-occurring events that I struggle a little. The idea is >> that I would have 5 core repetitions, none, daily, weekly, monthly and >> annually. > > This will not solve all your problems, but a very convenient way of > handling XML is the element tree module (that comes with python 2.5): > http://docs.python.org/lib/module-xml.etree.ElementTree.html > > HTH, > Daniel -- http://mail.python.org/mailman/listinfo/python-list -- http://mail.python.org/mailman/listinfo/python-list
RE: : Re-ocurring Events
Just as another quick update, I've found this module for python that may be able to help me, its designed to spot date patterns for things like recurring events I guess, http://www.biostat.wisc.edu/~annis/creations/period.py.html Quite how I can integrate it into my project I'm not sure but if I can loop through each of the events in my XML and see if they are meant to be recurring today then that's fantastic. If anyone has any rough ideas on how this might work for me I'd love to hear some rough concepts. Thanks, Rob -Original Message- From: [EMAIL PROTECTED] [mailto:[EMAIL PROTECTED] On Behalf Of Robert Rawlins - Think Blue Sent: 27 April 2007 08:24 To: python-list@python.org Subject: RE:: Re-ocurring Events Thank you guys for your suggestions. I've been having a look at that launchd stuff from apple but couldnt really see how that applies to my requirements. I've been putting some serious thought into how this should work as its essentially the final part of the puzzle for my application, I'll have a fully working model for my application. I've been thinking about the possibility of using a combination of xpath to search the XML with some loops which change the date. As events won't ever be set to start before 01-01-2007 I can set that as the ceiling for my loop. So when I'm searching for weekly events, I 'simply' take today's date and time and loop from now until 01-01-2007 decrementing the date by a week each iteration of the loop and then search the XML for events in that date, make sense? I know that's a fairly intensive way of doing it, but if it works it works. Now, the loop is where I'm really struggling, I've not done any looping with dates, can anyone give me a hand with this? How can I loop back in time from now to beginning of 07 a week at a time? Do we have some form of dateAdd() I can use with a while loop? Perhaps. Date = (now) While date > 2007-01-01: Date = dateAdd(date, -1, w) Something to that effect? Then I can quickly xpath for every iteration of the loop. Thanks guys for any help. Rob -Original Message- From: [EMAIL PROTECTED] [mailto:[EMAIL PROTECTED] On Behalf Of Laurent Pointal Sent: 26 April 2007 15:33 To: python-list@python.org Subject: Re: Re-ocurring Events Daniel Nogradi a écrit : >> A bit more of a complex one this time, and I thought I'd get your >> opinions >> on the best way to achieve this. Basically I'm looking for a way to >> describe >> a re-occurring event, like a calendar event or appointment I guess. I'm >> likely to use an XML file for the definition of the events, but >> imagine I've >> got an event that looks something like this. >> >> > repeat="daily" >> /> May take a look at launchd (Apple) XML dialect, it may have at least the semantic for your need. For other parts... elementtree, yes. >> Now what I want to do is be able to build a class which has a function >> like >> 'getCurrentEvent()' which will return any events that should be >> occurring at >> that time. So if the current system time and date is 2007-01-03 13:00:00 >> then it will return THAT event to me, but if it was say 2007-01-03 >> 16:00:00 >> then it would not, as the event isn't 'due' to occur at that time. Make >> sense? >> >> What's the best way of handling this? I'm really a little lost as to >> how I >> might get started, checking a static date time isn't a problem, it's >> when it >> comes to these re-occurring events that I struggle a little. The idea is >> that I would have 5 core repetitions, none, daily, weekly, monthly and >> annually. > > This will not solve all your problems, but a very convenient way of > handling XML is the element tree module (that comes with python 2.5): > http://docs.python.org/lib/module-xml.etree.ElementTree.html > > HTH, > Daniel -- http://mail.python.org/mailman/listinfo/python-list -- http://mail.python.org/mailman/listinfo/python-list -- http://mail.python.org/mailman/listinfo/python-list
RE: Re-ocurring Events
Thanks for getting back to me Laurent. I've now made some pretty tidy progress on this and -think- it's going to shape up nicely, I'm just working on converting my date strings from the XML into a date_struct and we should be good to go. I'll keep you all posted. Rob -Original Message- From: Laurent Pointal [mailto:[EMAIL PROTECTED] Sent: 27 April 2007 09:49 To: Robert Rawlins - Think Blue Subject: Re: Re-ocurring Events Robert Rawlins - Think Blue a écrit : > Thank you guys for your suggestions. > > I've been having a look at that launchd stuff from apple but couldnt really > see how that applies to my requirements. I was not thinking about launchd itself, but about its XML configuration files which manage timed events, as you need. > I've been putting some serious thought into how this should work as its > essentially the final part of the puzzle for my application, I'll have a > fully working model for my application. > > I've been thinking about the possibility of using a combination of xpath to > search the XML with some loops which change the date. As events won't ever > be set to start before 01-01-2007 I can set that as the ceiling for my loop. > So when I'm searching for weekly events, I 'simply' take today's date and > time and loop from now until 01-01-2007 decrementing the date by a week each > iteration of the loop and then search the XML for events in that date, make > sense? > > I know that's a fairly intensive way of doing it, but if it works it works. > > Now, the loop is where I'm really struggling, I've not done any looping with > dates, can anyone give me a hand with this? How can I loop back in time from > now to beginning of 07 a week at a time? Do we have some form of dateAdd() I > can use with a while loop? Perhaps. See datetime module, eventually third party mxDatetime. > Date = (now) > While date > 2007-01-01: > Date = dateAdd(date, -1, w) > > Something to that effect? Then I can quickly xpath for every iteration of > the loop. A+ Laurent. -- Laurent POINTAL CNRS-LIMSI dépt. CHM, groupes AMI et PS Courriel: [EMAIL PROTECTED](prof) [EMAIL PROTECTED] (perso) Ouebe: http://www.limsi.fr/Individu/pointal/ Tél. 01 69 85 81 06 (prof) Fax. 01 69 85 80 88 -- http://mail.python.org/mailman/listinfo/python-list
RE: Re-ocurring Events
Ok, Almost done now, it seems to be working a real charm at the moment. I need a little help on performing a quick if statement against a date string. I have couple of date strings returned by s.attributes.getNamedItem("start").nodeValue s.attributes.getNamedItem("end").nodeValue and I need to do and conditional that ensures start is before today's date/time and end is after today's date/time. The string looks like: '2007-01-01 00:00:00' Do I have to convert this to a proper time object to do the comparison? Or can I do it as a string? Thanks, Rob -Original Message- From: [EMAIL PROTECTED] [mailto:[EMAIL PROTECTED] On Behalf Of Robert Rawlins - Think Blue Sent: 27 April 2007 09:55 To: 'Laurent Pointal' Cc: python-list@python.org Subject: RE: Re-ocurring Events Thanks for getting back to me Laurent. I've now made some pretty tidy progress on this and -think- it's going to shape up nicely, I'm just working on converting my date strings from the XML into a date_struct and we should be good to go. I'll keep you all posted. Rob -Original Message- From: Laurent Pointal [mailto:[EMAIL PROTECTED] Sent: 27 April 2007 09:49 To: Robert Rawlins - Think Blue Subject: Re: Re-ocurring Events Robert Rawlins - Think Blue a écrit : > Thank you guys for your suggestions. > > I've been having a look at that launchd stuff from apple but couldnt really > see how that applies to my requirements. I was not thinking about launchd itself, but about its XML configuration files which manage timed events, as you need. > I've been putting some serious thought into how this should work as its > essentially the final part of the puzzle for my application, I'll have a > fully working model for my application. > > I've been thinking about the possibility of using a combination of xpath to > search the XML with some loops which change the date. As events won't ever > be set to start before 01-01-2007 I can set that as the ceiling for my loop. > So when I'm searching for weekly events, I 'simply' take today's date and > time and loop from now until 01-01-2007 decrementing the date by a week each > iteration of the loop and then search the XML for events in that date, make > sense? > > I know that's a fairly intensive way of doing it, but if it works it works. > > Now, the loop is where I'm really struggling, I've not done any looping with > dates, can anyone give me a hand with this? How can I loop back in time from > now to beginning of 07 a week at a time? Do we have some form of dateAdd() I > can use with a while loop? Perhaps. See datetime module, eventually third party mxDatetime. > Date = (now) > While date > 2007-01-01: > Date = dateAdd(date, -1, w) > > Something to that effect? Then I can quickly xpath for every iteration of > the loop. A+ Laurent. -- Laurent POINTAL CNRS-LIMSI dépt. CHM, groupes AMI et PS Courriel: [EMAIL PROTECTED](prof) [EMAIL PROTECTED] (perso) Ouebe: http://www.limsi.fr/Individu/pointal/ Tél. 01 69 85 81 06 (prof) Fax. 01 69 85 80 88 -- http://mail.python.org/mailman/listinfo/python-list -- http://mail.python.org/mailman/listinfo/python-list
RE: what python technology for my app?
Just thought I'd make a little suggestion about this, I dont know how strict you want to be with the web development side of things, but I'm a web developer by trade and have recently started using python for my non-web type applications. If you're looking for a web based server side solution, then you can't go wrong with Adobe ColdFusion. Its and incredibly powerful and scalable development platform and yet supports a simple tag based language which makes life very easy when getting started, and yet will support much more complex OOP methodology with a whole myriad of frameworks, orm's and object factories. For a quick 'run down' of its features take a look at the simple tutorials on the adobe site, they'll demonstrate how easy it is to achieve even quite complex tasks like PDF creation, animated statistical charts an all sorts. As CF is a JAVA based technology you get the power of J2EE platform and its perfectly multi platform playing nicely with linux, unix, sun, windows or whatever server environment you wish to run it on. Just my two pence, I'm a big CF fan so my opinions are no doubt bias, but might be worth you taking a look. http://www.adobe.com/uk/products/coldfusion/ Pay attention to the 'ColdFusion Demos' at the bottom right of the main column. Rob -Original Message- From: [EMAIL PROTECTED] [mailto:[EMAIL PROTECTED] On Behalf Of Bruno Desthuilliers Sent: 27 April 2007 12:19 To: python-list@python.org Subject: Re: what python technology for my app? Chris a écrit : > I have an database containing lots of numerical data. I want to write a > browser based interface that will allow selection of various key > parameters and yield tables, plots and/or printouts of the data > according to the selections. Ultimately I want this to run on an > intranet so that others can get access via their browsers. > > The application is for in-house use only and not likely to have more > than a few users at any one time. I've managed to hack out enough > understanding of sql and sqlAlchemy over the last couple of days to > create and access an sqlite3 based database for my data. This seems to > suit my purposes for now. Now for a front end and some simple > distribution over a network. > > I have written some small Wxpython & matplotlib apps for data analysis > and display but I'm not sure how these would work in a browser based > world, or even if they are appropriate. wxPython being a GUI toolkit, it's of course not really appropriate for a web-based solution (and yes, this is an understatement). I don't have any experience with matplotlib, but according to the project's FAQ, this shouldn't be a problem: http://matplotlib.sourceforge.net/faq.html#BATCHMODE > > Any advice on what technologies I should be looking at for this? Python > based naturally, and hopefully simple and lightweight. I'm not a > programmer by trade and that's not what I really get paid for but I've > learned to love python and its myriad of modules for all the data > analysis work I need to do. If I can throw something moderately > functional together in a week or two (along with all the learning that > entails) If you don't have any experience with web programming, it might take a bit more time. > I'll be happy. Pylons (http://pylonshq.com) and turbogears are two great web frameworks. Both support SQLAlchemy. My own favourite is Pylons, but you should try both and choose the one that better fits your brain. > btw - Platform needs to be windows because that's what on my desk. Python is mostly platform independant. HTH -- http://mail.python.org/mailman/listinfo/python-list -- http://mail.python.org/mailman/listinfo/python-list
RE: Re-ocurring Events
Thanks for that tip Jarek, It worked a charm, I just created a format time string and used that in the compare against my XML and it work perfectly. Thank you, Rob -Original Message- From: [EMAIL PROTECTED] [mailto:[EMAIL PROTECTED] On Behalf Of Jarek Zgoda Sent: 27 April 2007 13:01 To: python-list@python.org Subject: Re: Re-ocurring Events Robert Rawlins - Think Blue napisał(a): > and I need to do and conditional that ensures start is before today's > date/time and end is after today's date/time. > > The string looks like: '2007-01-01 00:00:00' > > Do I have to convert this to a proper time object to do the comparison? Or > can I do it as a string? In the very specific case of string formatted as above, the string comparison will give the same results as in the case of datetime objects comparison. You just have to compare the same kinds of things. ;) -- Jarek Zgoda "We read Knuth so you don't have to." -- http://mail.python.org/mailman/listinfo/python-list -- http://mail.python.org/mailman/listinfo/python-list
RE: what python technology for my app?
Haha, no Troll, just a shameless plug for my life's one true love ;-) Rob -Original Message- From: [EMAIL PROTECTED] [mailto:[EMAIL PROTECTED] On Behalf Of Bruno Desthuilliers Sent: 27 April 2007 14:35 To: python-list@python.org Subject: Re: what python technology for my app? Robert Rawlins - Think Blue a écrit : > Just thought I'd make a little suggestion about this, I dont know how > strict you want to be with the web development side of things, but I'm a web > developer by trade and have recently started using python for my non-web > type applications. > > If you're looking for a web based server side solution, then you can't go > wrong with Adobe ColdFusion. Err... is this a troll ? -- http://mail.python.org/mailman/listinfo/python-list -- http://mail.python.org/mailman/listinfo/python-list
Dict Copy & Compare
Hello Guys, I'm looking for a little advice on dicts, firstly I need to learn how to copy a dict, I suppose I could just something like. Self.newdict = self.olddict But I fear that this only creates a reference rather than an actual copy, this means that as soon as I clear out the old one, the new one will effectively be empty. What's the best way to ACTUALY copy a dict into a new variable? Next up I'm looking to compare two different dictionaries, then loop through the unique results that are in each and print them out. Is there a more efficient way of doing this other than a loop with an if/else statement? For a in self.dict1: If a not in self.dict2: Print 'Found %s' % (a) For b in self.dict2: If b not in self.dict1: Print 'Found %s' % (b) That would firstly loop through the first dict and output any of its unique values, it then loops through the second dict and output any of its unique values, is this the best way of doing this? Or is there something more efficient? Thanks, Rob Rawlins -- http://mail.python.org/mailman/listinfo/python-list
RE: Dict Copy & Compare
Thanks for that Tim, The first part for copying the dict seems to work nicely but I'm struggling to get the second part working properly. Let me explain a little more specifically what I'm trying to do. I have two dicts, one named 'this' and the other named 'that'. I want to get all the unique keys from 'this' and log them into a file, I then want to take all the unique values from 'that' and log them into a separate file. I have functions set up for the logging, so I can call it like logThis(uniquekey) and logThat(uniquekey). So it's just a case of firstly returning a list of all keys that are in 'this' but NOT in 'that' and then visa versa, then loop over them performing the function. Rob -Original Message- From: [EMAIL PROTECTED] [mailto:[EMAIL PROTECTED] On Behalf Of Tim Golden Sent: 30 April 2007 09:41 Cc: python-list@python.org Subject: Re: Dict Copy & Compare Robert Rawlins - Think Blue wrote: > I'm looking for a little advice on dicts, firstly I need to learn how to > copy a dict, I suppose I could just something like. > Self.newdict = self.olddict > But I fear that this only creates a reference rather than an actual copy, > this means that as soon as I clear out the old one, the new one will > effectively be empty. What's the best way to ACTUALY copy a dict into a new > variable? Unless you have specialised needs, you can just say: d2 = dict (d1) which will initialise d2 from d1's key/value pairs: d1 = dict (a=1, b=2) d2 = dict (d1) d2['a'] = 5 print d1 print d2 > Next up I'm looking to compare two different dictionaries, then loop through > the unique results that are in each and print them out. Is there a more > efficient way of doing this other than a loop with an if/else statement? This comes up not infrequently on the list. I think there's even a few recipes in the cookbook. One (fairly recent) technique is to use set versions of your dictionary keys, but it depends on what you want to do next. From my example above: # relies on the fact that dictionary iterators # iterate over the keys of the dict. s1 = set (d1) s2 = set (d2) # do whatever set-ops you want, eg s3 = s1 | s2 for key in s3: print "Key:", key print "d1 =>", d1[key] print "d2 =>", d2[key] TJG -- http://mail.python.org/mailman/listinfo/python-list -- http://mail.python.org/mailman/listinfo/python-list