Re: [Tutor] How to load a dict into a dict subclass?
... >> Assume I have a dict, 'foo'. I also have my own class, 'Bar', which >> subclasses (i.e. is a derived class) of a dict. How do I eloquently >> get foo into an instace of Bar? Example: >> >> >> ### BEGIN CODE: >> class Bar(dict): >>pass # Act like a dict for now. >> >> foo = {'a': 100, 'b': 200, 'c': 300} # This could be a function return >> value. >> myvar = Bar() >> # The hacky feeling part: >> for k,v in foo.items(): myvar[k] = v >> >> ### END CODE >> > You can use the built-in function for dictionaries called update. So > > >>> class Bar(dict): > >>> pass > > >>> foo = {'a':100, 'b':200, 'c': 300} > >>> myvar = Bar() > >>> myvar.update(foo) > >>> myvar > {'a': 100, 'c': 300, 'b': 200} ... Thanks guys! Christian, that's just what I was looking for. Thank you! -Modulok- ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
[Tutor] Fwd: New to Python
Remember to hit Reply to all - Forwarding your message onto the list -- Forwarded message -- From: Date: Wed, Oct 28, 2009 at 1:00 AM Subject: Re: [Tutor] New to Python To: sri...@gmail.com Hi Wayne, I will try as best to explain what I need to do. I have a log file. In that log file thre contains text. Something along the lines of this. 115=WAS 115=GAD 115=TRE I need to search that log file (thought of using grep) and write each text after the "=" sign to another text file. So the other text file (call it "* List*") will contain a list of all the differernt values for 115. After I have created that, i then need to run a script that will run through the latest log file and compare the *List *from the log file and any new 115=... values that are not in the* List *text file must be added, vice versa. End result is that I want to track and see how many values I can get in the 115=... The log file is siuated on a different server/machine on my network, so I want the script to be able to copy the log file from the server to my machine. I hope that makes sense. Many thanks for your help. -Original Message- From: Wayne To: asteri...@petlover.com Cc: tutor@python.org Sent: Tue, Oct 27, 2009 6:53 pm Subject: Re: [Tutor] New to Python On Tue, Oct 27, 2009 at 10:35 AM, wrote: > Hi there, > > How do you connect to a different server using python? (I have not used > python before) > What kind of server? What do you ultimately want to do? see: http://catb.org/~esr/faqs/smart-questions.html#beprecise for more info about asking good questions. We can't help you if we don't know what you want. -Wayne -- To be considered stupid and to be told so is more painful than being called gluttonous, mendacious, violent, lascivious, lazy, cowardly: every weakness, every vice, has found its defenders, its rhetoric, its ennoblement and exaltation, but stupidity hasn’t. - Primo Levi ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
[Tutor] Access PostGreSQL 8.3 by python 2.5
Hi , I'm try to access PostGreSQL 8.3 by python 2.5. I'm working on winXP. I use pg module. At the first time i get error that no found dll. when i add C:\Program Files\PostgreSQL\8.3\bin to the path variable i able to fixed it. After that i get following error. >>> import pg Traceback (most recent call last): File "", line 1, in import pg File "C:\Python25\Lib\site-packages\pg.py", line 21, in from _pg import * ImportError: DLL load failed with error code 182 Can any one tell me how I fixed this error? Thank in advance. Mahasen ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] New to Python
Forwarding to the list with my reply (please use Reply All to reply to the list): On Wed, Oct 28, 2009 at 2:01 AM, wrote: > Hi Kent, > > Thank you for replying to my request, really appreciate it. > > I am not familiar with all your connections that you have mentioned below. > > What I can tell you which I hope can help is that I have a log file on a > different server/machine on a network which I would like to copy and paste > on my machine? How would you copy the file if you did it by hand? Is the server file system accessible to your machine? If so, you can copy it with shutil.copyfile() or just open it and process it directly. Kent ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Access PostGreSQL 8.3 by python 2.5
On Wed, Oct 28, 2009 at 1:44 AM, Mahasen Dehideniya wrote: > Hi , > > I'm try to access PostGreSQL 8.3 by python 2.5. I'm working on winXP. > > I use pg module. > > At the first time i get error that no found dll. when i add C:\Program > Files\PostgreSQL\8.3\bin to the path variable i able to fixed it. > > After that i get following error. > > > import pg > > Traceback (most recent call last): > File "", line 1, in > import pg > File "C:\Python25\Lib\site-packages\pg.py", line 21, in > from _pg import * > ImportError: DLL load failed with error code 182 > > > > Can any one tell me how I fixed this error? Here is someone with the same error who traced it to conflicting versions of a supporting DLL: http://mailman.vex.net/pipermail/pygresql/2008-March/001942.html ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] New to Python
Remember to hit Reply to all - Forwarding your message onto the list -- Forwarded message -- From: Date: Wed, Oct 28, 2009 at 1:00 AM Subject: Re: [Tutor] New to Python To: sri...@gmail.com Hi Wayne, I will try as best to explain what I need to do. I have a log file. In that log file thre contains text. Something along the lines of this. 115=WAS 115=GAD 115=TRE I need to search that log file (thought of using grep) and write each text after the "=" sign to another text file. So the other text file (call it "* List*") will contain a list of all the differernt values for 115. After I have created that, i then need to run a script that will run through the latest log file and compare the *List *from the log file and any new 115=... values that are not in the* List *text file must be added, vice versa. End result is that I want to track and see how many values I can get in the 115=... The log file is siuated on a different server/machine on my network, so I want the script to be able to copy the log file from the server to my machine. I hope that makes sense. Many thanks for your help. -Original Message- From: Wayne To: asteri...@petlover.com Cc: tutor@python.org Sent: Tue, Oct 27, 2009 6:53 pm Subject: Re: [Tutor] New to Python On Tue, Oct 27, 2009 at 10:35 AM, wrote: Hi there, How do you connect to a different server using python? (I have not used python before) What kind of server? What do you ultimately want to do? see: http://catb.org/~esr/faqs/smart-questions.html#beprecise for more info about asking good questions. We can't help you if we don't know what you want. -Wayne Did you look up the smart-questions url? Since you're trying to connect from one Windows XP machine to another Windows XP or Server 2003 machine on the same network, the simplest answer is to use UNC names, which are machine-name, share-name, and path, all rolled into one string with two leading backslashes. In that case, you can do an open something like the following: infile = open(r"\\myserver\share3\system\logs\mylog.txt", "r") Notice the r" which indicates a raw string, so you wouldn't need to double the backslashes. DaveA ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] New to Python
Well I would have to remote to the machine that contains the log file and copy it from there. -Original Message- From: Kent Johnson To: asteri...@petlover.com Cc: *tutor python Sent: Wed, Oct 28, 2009 1:18 pm Subject: Re: [Tutor] New to Python Forwarding to the list with my reply (please use Reply All to reply to he list): On Wed, Oct 28, 2009 at 2:01 AM, wrote: Hi Kent, Thank you for replying to my request, really appreciate it. I am not familiar with all your connections that you have mentioned below. What I can tell you which I hope can help is that I have a log file on a different server/machine on a network which I would like to copy and paste on my machine? How would you copy the file if you did it by hand? Is the server file ystem accessible to your machine? If so, you can copy it with hutil.copyfile() or just open it and process it directly. Kent ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] New to Python
On Wed, Oct 28, 2009 at 11:16 AM, wrote: > Well I would have to remote to the machine that contains the log file and > copy it from there. You are not being very helpful. What does "remote to the machine" mean? What OS are you running? Some details would help. Kent PS Please don't top-post. > > > -Original Message- > From: Kent Johnson > To: asteri...@petlover.com > Cc: *tutor python > Sent: Wed, Oct 28, 2009 1:18 pm > Subject: Re: [Tutor] New to Python > > Forwarding to the list with my reply (please use Reply All to reply to > the list): > > On Wed, Oct 28, 2009 at 2:01 AM, wrote: >> Hi Kent, >> >> Thank you for replying to my request, really appreciate it. >> >> I am not familiar with all your connections that you have mentioned below. >> >> What I can tell you which I hope can help is that I have a log file on a >> different server/machine on a network which I would like to copy and paste >> on my machine? > > How would you copy the file if you did it by hand? Is the server file > system accessible to your machine? If so, you can copy it with > shutil.copyfile() or just open it and process it directly. > > Kent > ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
[Tutor] PyQT forum?
I'm starting to learn PyQt. Can anyone recommend a good mailing list or forum? Thanks. "I'm the last person to pretend that I'm a radio. I'd rather go out and be a color television set." -David Bowie "Who dares wins" -British military motto "There is no such thing as luck; there is only adequate or inadequate preparation to cope with a statistical universe." -Robert A. Heinlein, "Have Space Suit - Will Travel" ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
[Tutor] Generating unique ID
Hello, I'm writing an application which will hold a database of people. Ofcourse, people can have the same name, so I want to stock them with an unique ID. I've searched and found some things: - uuid.uuid4() - id(name) - os.urandom(n) But they seem overkill to me. Well, maybe not id(). What should I use the best for this? Maybe examples of other programs that do something alike? Thanks, Timo ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Generating unique ID
On Wed, Oct 28, 2009 at 1:52 PM, Timo List wrote: > Hello, > > I'm writing an application which will hold a database of people. Ofcourse, > people can have the same name, so I want to stock them with an unique ID. > > I've searched and found some things: > - uuid.uuid4() > - id(name) > - os.urandom(n) > > But they seem overkill to me. Well, maybe not id(). > > What should I use the best for this? Maybe examples of other programs that > do something alike? You could use SQLite... I believe it supports autoincrement. If you just want a random number, you can use the random library. HTH, Wayne ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Generating unique ID
> I'm writing an application which will hold a database of people. Ofcourse, > people can have the same name, so I want to stock them with an unique ID. > If you're using an actual database rather than a plain old text file, you can let the database do the work for you. Most flavors (sqlite, postgresql, mysql, oracle) have an autoincrement option that will generate unique IDs for you when you insert new records. Below are the docs for sqlite that explain the feature: http://www.sqlite.org/autoinc.html ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
[Tutor] Can python determine Battery or AC Power?
Is there a way in python to tell if the power source for a laptop is ac or battery? I am trying to write a small script to print out my system stats and would like to know if the laptop is on battery power or using ac power. I just dont know enough python to know where to look for that info. Thanks! -Nick ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Can python determine Battery or AC Power?
If you're on Windows and you can find an example using the Win32 api (in C++ for example) you can use pywin32 module to do the same thing through python. It's a little complicated sometimes though. So what O.S. are you on? On Wed, Oct 28, 2009 at 4:15 PM, Nick Hird wrote: > Is there a way in python to tell if the power source for a laptop is > ac or battery? I am trying to write a small script to print out my > system stats and would like to know if the laptop is on battery power > or using ac power. I just dont know enough python to know where to > look for that info. > > Thanks! > -Nick > ___ > Tutor maillist - Tutor@python.org > To unsubscribe or change subscription options: > http://mail.python.org/mailman/listinfo/tutor > ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Can python determine Battery or AC Power?
I am running Windows XP. On Wed, Oct 28, 2009 at 5:19 PM, Luke Paireepinart wrote: > If you're on Windows and you can find an example using the Win32 api (in C++ > for example) you can use pywin32 module to do the same thing through > python. It's a little complicated sometimes though. So what O.S. are you > on? > > On Wed, Oct 28, 2009 at 4:15 PM, Nick Hird wrote: >> >> Is there a way in python to tell if the power source for a laptop is >> ac or battery? I am trying to write a small script to print out my >> system stats and would like to know if the laptop is on battery power >> or using ac power. I just dont know enough python to know where to >> look for that info. >> >> Thanks! >> -Nick >> ___ >> Tutor maillist - tu...@python.org >> To unsubscribe or change subscription options: >> http://mail.python.org/mailman/listinfo/tutor > > -- --Nick ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Can python determine Battery or AC Power?
On Wed, Oct 28, 2009 at 4:28 PM, Nick Hird wrote: > I am running Windows XP. > My advice applies then ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
[Tutor] Sorting points on a 2D plane
Hi, I am working on a practice problem called 'POINTS' on the CodeChef site:http://www.codechef.com/problems/POINTS/. This simply wants the sum of the distances between a number of points on a 2 dimensional plane. Looking at the problem, the algorithm to define the sum of the distances between all the points is not really difficult.The challenge is in ordering the points given the following rules: 1) You cannot move to a point with a lesser X value as compared to the X value of the point you are on. 2) For points having the same X value you need to visit the point with the greatest Y value before visiting the new point with the same X value. The least X takes precedence over the greatest Y. In any given test case there can be from 2 to 10 points. The X and Y coordinates of each point will be between 0 and 1 both inclusive. OK. The rules are established. A properly constructed algorithm will work for 10 points as well as 1 points given it is properly constructed. My point of confusion is in ordering the points. Given a very simple example set of points: 0 5 0 1 0 2 2 1 2 3 5 2 5 1 The arrangement should be, 0 5 0 2 0 1 2 3 2 1 5 2 5 1 My solution is to first sort all the X values which would give me 0,0,0,2,2,5,5. I then will look at the Y values associated with the X value so I know that I am going to sort 3 Y values in reverse order for X = 0 and then append the Y value to each X point. What I am looking for is a better, faster approach. I think there would be a way to build a dictionary but you can't use the X values as keys since keys must be unique. So, any help to point me in a better faster direction will be truly appreciated. Thanks, Robert ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Sorting points on a 2D plane
Oops I replied off list twice. (i wish they would just munge the addresses *grumble**grumble*) On Wed, Oct 28, 2009 at 11:34 AM, Robert Berman wrote: Hi, What I am looking for is a better, faster approach. I think there would be a way to build a dictionary but you can't use the X values as keys since keys must be unique. Why can't you use the X values? Just key to a list. Eg. for the data 0 5 0 2 0 1 2 3 2 1 5 2 5 1 Your dictionary would look like: {0:[5,2,1], 2:[3,1], 5:[2,1]} After sorting the sublists you could use itertools to reassemble the original coordinates fairly easily but I still don't think this will be the most optimal way. You can't get around sorting to sort values. So perhaps you could give us the exact code you did (on pastebin if it's long, try to cut out unnecessary cruft before posting) so we can help you make it more efficient. This should be extremely fast if you're doing the sorting correctly. Python's sort is really really optimized. Also, if you can upload a sample input file with a large dataset and indicate your current running time on the data set and what you're trying to achieve it will be easier to figure out how to improve your algorithm. -Luke > ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Can python determine Battery or AC Power?
"Nick Hird" wrote I am running Windows XP. On Wed, Oct 28, 2009 at 4:15 PM, Nick Hird wrote: Is there a way in python to tell if the power source for a laptop is ac or battery? Search Microsoft Developer site (MSDN) for GetSystemPowerStatus which is the function that tests for battery or AC among other things. HTH, Alan G ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Sorting points on a 2D plane
"Robert Berman" wrote constructed. My point of confusion is in ordering the points. Given a very simple example set of points: The arrangement should be, 0 5 0 2 0 1 2 3 2 1 5 2 5 1 You might find the itertools.groupby function does most of what you need? HTH, Alan G. ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Sorting points on a 2D plane
On Wed, Oct 28, 2009 at 1:34 PM, Robert Berman wrote: > Hi, > > I am working on a practice problem called 'POINTS' on the CodeChef > site:http://www.codechef.com/problems/POINTS/. This simply wants the sum > of the distances between a number of points on a 2 dimensional plane. > Looking at the problem, the algorithm to define the sum of the distances > between all the points is not really difficult.The challenge is in > ordering the points given the following rules: > 1) You cannot move to a point with a lesser X value as compared to > the X value of the point you are on. > 2) For points having the same X value you need to visit the point with > the greatest Y value before visiting the new point with the same X > value. The least X takes precedence over the greatest Y. > > In any given test case there can be from 2 to 10 points. The X and Y > coordinates of each point will be between 0 and 1 both inclusive. > > OK. The rules are established. A properly constructed algorithm will > work for 10 points as well as 1 points given it is properly > constructed. My point of confusion is in ordering the points. Given a > very simple example set of points: > > 0 5 > 0 1 > 0 2 > 2 1 > 2 3 > 5 2 > 5 1 > > > The arrangement should be, > > 0 5 > 0 2 > 0 1 > 2 3 > 2 1 > 5 2 > 5 1 > > My solution is to first sort all the X values which would give me > 0,0,0,2,2,5,5. I then will look at the Y values associated with the X > value so I know that I am going to sort 3 Y values in reverse order for > X = 0 and then append the Y value to each X point. There are two easy ways to do this. One is similar to what you describe, but you sort first by Y value (descending) then sort by X value ascending. Python sort will preserve the order of equal items so the X sort preserves the descending Y sort and you get what you want. operator.itemgetter() is a convenience function for generating a key extraction function. In [3]: data Out[3]: [[0, 5], [0, 1], [0, 2], [2, 1], [2, 3], [5, 2], [5, 1]] In [4]: from operator import itemgetter In [5]: data.sort(key=itemgetter(1), reverse=True) In [6]: data Out[6]: [[0, 5], [2, 3], [0, 2], [5, 2], [0, 1], [2, 1], [5, 1]] In [7]: data.sort(key=itemgetter(0)) In [8]: data Out[8]: [[0, 5], [0, 2], [0, 1], [2, 3], [2, 1], [5, 2], [5, 1]] The other way to do this is to make a key that negates Y: In [10]: data.sort(key=lambda (x,y): (x, -y)) In [11]: data Out[11]: [[0, 5], [0, 2], [0, 1], [2, 3], [2, 1], [5, 2], [5, 1]] Kent ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Generating unique ID
> I'm writing an application which will hold a database of people. Ofcourse, > people can have the same name, so I want to stock them with an unique ID. > > I've searched and found some things: > - uuid.uuid4() > - id(name) > - os.urandom(n) > > But they seem overkill to me. Well, maybe not id(). > > What should I use the best for this? Maybe examples of other programs that > do something alike? Use the auto-increment feature of your database database management backend. (Assuming you're using a database backend like MySQL, postgresql, etc.) In MySQL your database description would look something like this (with any other fields you need): # MySQL table description: CREATE TABLE IF NOT EXISTS mytable ( `uid`BIGINT unsigned NOT NULL auto_increment unique, `uname` CHAR(32) NOT NULL default "guest", PRIMARY KEY (`uid`)); You could use MySQLdb (third party python module) to talk to the MySQL process with Python. Other database managers have similar abilities. >> os.urandom(n) Random numbers are random, NOT unique. If you're using your own backend, like a text file or whatever, stop. Take the time to learn to use a database manager like postgresql or MySQL or whatever. They have already solved many of the problems you're now facing. It will be well worth the time and frustration. Otherwise, you'll have to parse your database and get the previously used value and then increment that. However, this solution will fail if there are multiple processes, or threads accessing the data concurrently. To solve that problem you'll have to introduce some manner of mutex to gurantee that only one process has access to the unique data at any given time. Things get complicated. All of these problems have already been solved with other database managers. Use them! In a pinch, with a low volume database for non-critical data, you could probably get away with using a Unix epoch style timestamp with sufficient granularity. But even this is in no way, "guaranteed" to be unique. It's just, "probably unique". It would look like this: >>> import time >>> time.time() 1256796357.661967 If it absolutely must be unique, use a database manager that can make that guarantee. Best of luck! -Modulok- ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor