Re: [Tutor] How to import python dictionary into MySQL table?
Sean Carolan wrote: >> I have a database with a table called "systems" that contains an >> auto-increment id field, as well as fields for each of the keys in >> mydata.keys(). But I can't seem to get the syntax to import >> mydata.values() into the table. I think the problem may be that some >> of the items in my list are dictionaries or lists... >> >> What would be the quickest and most straightforward way to do this? > > I got this working in case anyone else comes across the same problem. > This function will pull cobbler *.json data into a MySQL database > table. The function assumes that you already have a database set up, > and that you are dumping the data into the "systems" table: > > def importJSON(targetfile): > ''' > Imports JSON data from targetfile into MySQL database table. > ''' > value_list = [] > rawdata = json.load(open(targetfile)) > for key in rawdata.keys(): > strvalue = str(rawdata[key]).replace("'",'"') Is the above line really needed? > value_list.append(strvalue) > valtuple = tuple(value_list) > sql = "INSERT INTO systems (comment, kickstart, > name_servers_search, ks_meta, kernel_options_post, image, > redhat_management_key, power_type, power_user, kernel_options, vi > rt_file_size, mtime, template_files, gateway, uid, virt_cpus, > hostname, virt_type, mgmt_classes, power_pass, netboot_enabled, > profile, virt_bridge, parent, virt_path, interfaces, power_address, > name_servers, name, owners, ctime, virt_ram, power_id, random_id, > server, redhat_management_server, depth) VALUES (%s, %s, %s, %s, %s, > %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, > %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s);" > cursor.execute(sql, valtuple) The order of key/value pairs in a dictionary is an implementation detail. I think it's dangerous to assume they will always be in sync with the column names as provided in the string constant. Here's what I came up with when I tried to make a generalized version of the above: def add_row(cursor, tablename, rowdict): # XXX tablename not sanitized # XXX test for allowed keys is case-sensitive # filter out keys that are not column names cursor.execute("describe %s" % tablename) allowed_keys = set(row[0] for row in cursor.fetchall()) keys = allowed_keys.intersection(rowdict) if len(rowdict) > len(keys): unknown_keys = set(rowdict) - allowed_keys print >> sys.stderr, "skipping keys:", ", ".join(unknown_keys) columns = ", ".join(keys) values_template = ", ".join(["%s"] * len(keys)) sql = "insert into %s (%s) values (%s)" % ( tablename, columns, values_template) values = tuple(rowdict[key] for key in keys) cursor.execute(sql, values) filename = ... tablename = ... db = MySQLdb.connect(...) cursor = db.cursor() with open(filename) as instream: row = json.load(instream) add_row(cursor, tablename, row) Peter ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
[Tutor] Looking for a tutor to review my code and provide constructive feedback.
Hello, How to prevent shadows a built-in in code. Please if you are kind enougth to take the time to provide feedback I would appreciate that it is constructive. The script is here: http://bpaste.net/show/12364/ ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Looking for a tutor to review my code and provide constructive feedback.
zicu radu wrote: Hello, How to prevent shadows a built-in in code. Please if you are kind enougth to take the time to provide feedback I would appreciate that it is constructive. The script is here: http://bpaste.net/show/12364/ Are you sure you have posted the right link? This is a function called "fixXML". It doesn't seem to have anything to do with preventing the shadowing of built-ins. -- Steven ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] vim as a python editor; FOLLOW-UP QUESTION
Thanks for all the good advice guys! Joel > -Original Message- > From: tutor-bounces+joel=joelschwartz@python.org > [mailto:tutor-bounces+joel=joelschwartz@python.org] On > Behalf Of Alan Gauld > Sent: Thursday, December 16, 2010 4:27 PM > To: tutor@python.org > Subject: Re: [Tutor] vim as a python editor; FOLLOW-UP QUESTION > > "Joel Schwartz" wrote > > you experts whether it makes sense (or is even possible) to > use vim in > > Windows (I use Windows XP-SP3). > > Yes, vim is great on Windows. > When i used unix primarily I was an emacs fan, but emacs just > doesn't feel right on windows to me so I moved to vim which > works hust as well in both OS. > > > One more question: IDLE does not appear to have a way to > review your > > command history, e.g., by hitting the up arrow. > > IDLE borrows from emacs and uses ALT-P and ALT-N. > (FWIW Pythonwin uses Ctrl-UP/Ctrl-Down for the same purpose) > > > set up IDLE to do this? Alternatively, is there another > basic Python > > GUI that has the up-arrow command history feature and is > also good for > > Python coding in general? > > Pythonwin is better than IDLE for Windows users. It has > integration with COM for object browsing and also with the > Windows help system. > And the debugger is much better than the IDLE one too. > > And it users the same editor control as Scite. > Speaking of Scite, it is a pretty useful editor in its own > right and adds tabbed editing which Pythonwin doesn't have, > but OTOH it loses all the Python specific tools. my ideal > Windows text editor would be a combination of Scite and vim... > > Finally, if you have a PC big enough to run it well, Eclipse > and Pydev are a good combination. Especially if you also work > in Java or C++ etc > > And there are a bunch of others that folks like. IDEs and > text editors are very personal choices. Wingz, SPE, > PyCrust/PyAlaMode, and more. > > HTH, > > -- > Alan Gauld > Author of the Learn to Program web site > http://www.alan-g.me.uk/ > > > ___ > 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
[Tutor] os.system
I want to pass a variable to os.system which is dir >>> dir=raw_input ("Dir Please ") Dir Please / ok dir = / >>> os.system ("ls" + dir) sh: ls/: No such file or directory 32512 any advice signature.asc Description: This is a digitally signed message part ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] os.system
On 12/17/2010 4:07 PM jtl999 said... I want to pass a variable to os.system which is dir dir=raw_input ("Dir Please ") Dir Please / ok dir = / os.system ("ls" + dir) sh: ls/: No such file or directory this has concatenated "ls" and dir try with "ls " Emile 32512 any advice ___ 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
[Tutor] calling setters of superclasses
Hi all, Consider the following classes where PositiveX should constrain the attribute _x to be positive and SmallX should further constrain the attribute _x to be less than 10. class PositiveX(object): def __init__(self): self._x = 1 @property def x(self): return self._x @x.setter def x(self, val): if val < 0: raise ValueError('Negatives not allowed') self._x = val class SmallX(PositiveX): @property def x(self): return self._x @x.setter def x(self, val): # How do I call the superclass' @x.setter super(SmallX, self).__setattr__('x', val) if val > 10: raise ValueError('Big values not allowed') self._x = val I thought I could call the superclass setter first to ensure the value was positive, but I'm getting an infinite recursion. I also tried: super(SmallX, self).x = val but I get this: AttributeError: 'super' object has no attribute 'x' I'm fully confused and, therefore, likely doing something stupid. thanks, matt ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] os.system
On Fri, Dec 17, 2010 at 4:17 PM, Emile van Sebille wrote: > On 12/17/2010 4:07 PM jtl999 said... >> >> I want to pass a variable to os.system which is dir > dir=raw_input ("Dir Please ") > os.system ("ls" + dir) >> sh: ls/: No such file or directory > > this has concatenated "ls" and dir > try with "ls " in addition to emile's obvious suggestion, i have a few minor ones: 1) don't use a built-in function as a variable name (dir) 2) os.system() only sends off the request. if the cmd has output, you get it too. you can't capture it like you can with os.popen*() 3) you should consider using subprocess... it obsoletes all the os.*() functions cheers, -- wesley - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - "Core Python Programming", Prentice Hall, (c)2007,2001 "Python Fundamentals", Prentice Hall, (c)2009 http://corepython.com wesley.j.chun :: wescpy-at-gmail.com python training and technical consulting cyberweb.consulting : silicon valley, ca http://cyberwebconsulting.com ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] os.system
jtl999 wrote: I want to pass a variable to os.system which is dir dir=raw_input ("Dir Please ") Dir Please / ok dir = / os.system ("ls" + dir) sh: ls/: No such file or directory 32512 any advice Yes -- learn to read the error messages you get. The error you get is a shell error, not a Python error. It is *exactly* the same error you would get if you do this by hand: sh-3.2$ ls/ sh: ls/: No such file or directory This tells you that the shell can't find a file called "ls/". No surprises there -- you want "ls /". So you need to include a space in the shell command that you pass to os.system. -- Steven ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
[Tutor] unit testing - Separate methods or group tests together?
List, When you create unit tests, do you group tests for a given function together into one unit test method, or do you prefer to have separate test methods for every assert statement? Thanks! -Modulok- ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] calling setters of superclasses
"Gregory, Matthew" wrote class PositiveX(object): def __init__(self): @property def x(self): @x.setter def x(self, val): I don't use properties in Python very often (hardly ever in fact) and I've never used @setter so there may be naming requirements I'm not aware of. But in general I'd avoid having two methods with the same name. Try renaming the setter to setX() or somesuch and see if you get the same error. Just a thought. Alan G. ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] unit testing - Separate methods or group tests together?
Modulok wrote: List, When you create unit tests, do you group tests for a given function together into one unit test method, or do you prefer to have separate test methods for every assert statement? Each test method should test one thing. That doesn't necessarily mean one assert, but one conceptual thing. Here's an example from one of my test suites: class QuartileSkewnessTest(unittest.TestCase): def testFailure(self): # Test that function correctly raises an exception if the # arguments are out of order. self.assertRaises(ValueError, stats.quartile_skewness, 2, 3, 1) self.assertRaises(ValueError, stats.quartile_skewness, 9, 8, 7) def testNan(self): # Test that the degenerate case where all three arguments are # equal returns NAN. self.assertTrue(math.isnan(stats.quartile_skewness(1, 1, 1))) self.assertTrue(math.isnan(stats.quartile_skewness(5, 5, 5))) def testSkew(self): # Test skew calculations. self.assertEqual(stats.quartile_skewness(3, 5, 7), 0.0) self.assertEqual(stats.quartile_skewness(0, 1, 10), 0.8) self.assertEqual(stats.quartile_skewness(0, 9, 10), -0.8) In this particular case, I happen to have one test class for this function, but that's not always the case. Each test class is for a set of related tests (e.g. "test everything about this function", or "test that this same property holds for all eight of these functions", or similar). Within the test class, each test method is for one test, not necessarily one assert. -- Steven ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] The Template Pattern
Hello Steven, Could take some time when possible to answer to my follow-up question? Thank you! Cheers Karim On 12/14/2010 12:19 AM, Karim wrote: On 12/13/2010 11:47 PM, Steven D'Aprano wrote: Karim wrote: Hello all, I am seeking for information about the template pattern applied to python. Could you explain some implementation or anything else? it would be helpful. Design patterns are means to an end, not an end in themselves. You shouldn't say "I want to use the template pattern", you should say "I want to solve this problem, what solution is appropriate?" and then only use the template pattern if it is appropriate. Steven, thank you for answering ;o) I don't want to use this pattern absolutely. But I discussed this topic with a C++ programmer. He wanted to translate part of C++ code in python that implements this pattern in C++. I already have implemented a Strategy pattern in python to read different file format into my database and generate different output format file like the code below: class Input(object): """The basic Input class, leaving out the details of the algorithm.""" def __init__( self, strategy ): self.strategy = strategy self.lastRead = None def read(self, filePath): """Load into database memory an input file. @arg: filePath - string - The input file path. @return: The created database object. """ self.lastRead = self.strategy.read() return self.lastRead class InputStrategy( object ): """This InputStrategy class is an abstract interface to various read strategy objects. """ def read(self, filePath): """Abstract method to load into database memory an input file. @arg: filePath - string - The input file path. @return: The created database object. """ raise NotImplementedError By the way, I use NotImplementedError in my abstract method of the abstract class. You are using TypeError exception. Is there a general rule for that? NotImplementedError is ok? I knew from Java experience that template method pattern is only a kind of particular case of Stategy where you delegate parts of algorithm by abstract methods overriding. Indeed the base class implements some common features and let derived classes implement particular parts by polymorphism. Now with your example, I don't understand why he had problems to implement from C++ ? Or perhaps is he mixing it with C++ feature template ?! In any case, the template design pattern is one of the simpler design patterns. Here's an example, adapted from Wikipedia's article on the Template Method Pattern: class AbstractGame: """An abstract class that is common to several games in which players play against the others, but only one is playing at a given time. """ def __init__(self, *args, **kwargs): if self.__class__ is AbstractGame: raise TypeError('abstract class cannot be instantiated') def playOneGame(self, playersCount): self.playersCount = playersCount self.initializeGame() j = 0 while not self.endOfGame(): self.makePlay(j) j = (j + 1) % self.playersCount self.printWinner() def initializeGame(self): raise TypeError('abstract method must be overridden') def endOfGame(self): raise TypeError('abstract method must be overridden') def makePlay(self, player_num): raise TypeError('abstract method must be overridden') def printWinner(self): raise TypeError('abstract method must be overridden') Now to create concrete (non-abstract) games, you subclass AbstractGame and override the abstract methods. class Chess(AbstractGame): def initializeGame(self): # Put the pieces on the board. ... def makePlay(player): # Process a turn for the player ... etc. One more Thanks for you example Steven! Regards Karim ___ 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] Writing to the terminal?
Hello Steven, I added the pipe char '|' to have a complete spinner! This would be set as a function for my wait routine installer. Thanks to share! Karim On 12/10/2010 09:51 PM, Steven D'Aprano wrote: Modulok wrote: List, Forgive me if I don't describe this well, I'm new to it: [snip description of a progress bar] Here's one way: import time, sys f = sys.stdout for i in range(20): time.sleep(1) # do some work f.write('=') f.flush() # make the change visible immediately else: f.write('\n') You might be able to get the same effect with print, but it's probably easier using sys.stdout directly. Here's another way, which *must* use stdout and not print. for i in range(20): percentage = i/20.0 spinner = '/-\\-'[i % 4] f.write("Progress: %5.2f%% %s %s>\r" % (percentage, spinner, '='*(i+1))) f.flush() time.sleep(1) else: f.write('\n') ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Writing to the terminal?
I suppose we can do something like that (kind of pseudo code) to fully configure it: def doSomeStuff(*args): ... def progressBar( func, *args) import time, sys f = sys.stdout for i in range(20): func.__call__(args) f.write('=') f.flush() # make the change visible immediately else: f.write('\n') progressBar( doSomeStuff ) Cheers Karim On 12/18/2010 08:05 AM, Karim wrote: Hello Steven, I added the pipe char '|' to have a complete spinner! This would be set as a function for my wait routine installer. Thanks to share! Karim On 12/10/2010 09:51 PM, Steven D'Aprano wrote: Modulok wrote: List, Forgive me if I don't describe this well, I'm new to it: [snip description of a progress bar] Here's one way: import time, sys f = sys.stdout for i in range(20): time.sleep(1) # do some work f.write('=') f.flush() # make the change visible immediately else: f.write('\n') You might be able to get the same effect with print, but it's probably easier using sys.stdout directly. Here's another way, which *must* use stdout and not print. for i in range(20): percentage = i/20.0 spinner = '/-\\-'[i % 4] f.write("Progress: %5.2f%% %s %s>\r" % (percentage, spinner, '='*(i+1))) f.flush() time.sleep(1) else: f.write('\n') ___ 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