Re: [Tutor] passing arguments to functions - problem with argument order
Alan Gauld schrieb: Fine, but I would seriously consider learning dicts as a prioritry. Certainly way higher that learning OOP. In fact being comfortable with dictionaries will make understanding OOP much easier since a class is really just a special type of dictionary!. Alan G. ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor Well then I guess I will. I appreciate this kind of advice - thanks a lot :) Paul ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] identifying the calling module/function
Kent Johnson schrieb: [EMAIL PROTECTED] wrote: in dgf.py: (hope the formatting gets good for you, t-bird breaks the lines badly on my machine...) def csvwriter(*column_definitions): Edit Me! if sys.argv[0] == /usr/local/bin/xyz.py: output_csv_filename = xyz.csv else: output_csv_filename = raw_input(Name of the file to produce? (ATTENTION: WILL BE OVERWRITTEN!) ) If you can change xyz.py you can avoid this by adding an optional filename parameter to the function. first_row = ;.join(*column_definitions) try: file = open(output_csv_filename, w) file.write(first_row) file.close() except: print(Couldn't open %s for writing. % output_csv_filename) It's not such a good idea to hide the exception like this; you might at least want to print the actual exception. Or just let it propagate. sys.exit(1) You can use the csv module to write the header row, too. I would write this as def csvwriter(*column_definitions, filename=None): if filename is none: filename = raw_input(Name of the file to produce? (ATTENTION: WILL BE OVERWRITTEN!) ) try: out = csv.writer(open(filename, ab), delimiter=;, quoting=csv.QUOTE_NONE) out.writerow(column_definitions) except Exception, e print(Couldn't open %s for writing. % output_csv_filename) print e return out Kent Yeah, I think I get it, thanks! Paul ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
[Tutor] passing arguments to functions - problem with argument order
I don't get this - what is the clean way of the order of passing arguments to functions? The called function goes like this: def csvwriter(output_csv_filename=None, *coloumn_definitions): Edit Me! if output_csv_filename == None: output_csv_filename = raw_input(Name der zu erzeugenden Datei (vollständiger Pfad)? (ACHTUNG: WIRD ÜBERSCHRIEBEN, FALLS VORHANDEN!) ) first_row = ;.join(coloumn_definitions) print(first_row) try: file = open(output_csv_filename, w) file.writerow(first_row) file.close() except Exception, e: print(Konnte %s nicht zum Schreiben öffnen.) sys.exit(e) return csv.writer(open(output_csv_filename, ab), delimiter=;, quoting=csv.QUOTE_NONE) The call to the function seems impossible to do. When I say: writer = dgf.csvwriter(output_csv_filename=None, kundennummer, anrede, vorname, nachname, plz, ort, adresse, kontoinhaber, blz, kto, bankname, status, spielbeginn, letzte_aenderung, importdatum, briefdatum, buchungsdatum, stornodatum) I get: SyntaxError: non-keyword arg after keyword arg - So I guess I have to put keyword arg at the end... When I put output_csv_writer at the end: writer = dgf.csvwriter(kundennummer, anrede, vorname, nachname, plz, ort, adresse, kontoinhaber, blz, kto, bankname, status, spielbeginn, letzte_aenderung, importdatum, briefdatum, buchungsdatum, stornodatum, output_csv_filename=None) I get: TypeError: csvwriter() got multiple values for keyword argument 'output_csv_filename' - Am I right that output_csv_filename now becomes kundennummer at first? Also, changing the function definition gives me syntax error: def csvwriter(*coloumn_definitions, output_csv_filename=None): ^ SyntaxError: invalid syntax What's going on here? I'm confused... Paul ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] passing arguments to functions - problem with argument order
And I thought I might get away without using dicts... Thanks, Greg Greg Graham schrieb: Paul, Python does not allow mixing variable length arguments and keyword arguments in that way. To accomplish what you want, you must add an argument preceded by a ** which will be a dict containing all of the keyword arguments as key, value pairs. You then have to retrieve the arguments from the dict by name. When called, the keyword arguments must be last. Here is a little example: def test(*column_definitions, **options): print Column Definitions: + , .join(column_definitions) output_csv_filename = options.get('output_csv_filename', None) print Output csv filename: + str(output_csv_filename) test(kundennummer, anrede, vorname, nachname, plz, ort, adresse, kontoinhaber, blz, kto, bankname, status, spielbeginn, letzte_aenderung, importdatum, briefdatum, buchungsdatum, stornodatum, output_csv_filename=None) Column Definitions:kundennummer, anrede, vorname, nachname, plz, ort, adresse, kontoinhaber, blz, kto, bankname, status, spielbeginn, letzte_aenderung, importdatum, briefdatum, buchungsdatum, stornodatum Output csv filename: None Greg ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] passing arguments to functions - problem with argument order
That's it! Paul Andreas Kostyrka schrieb: What you probably want is to pass: writer(None, field1, field2) Andreas Am Montag, den 10.03.2008, 16:28 +0100 schrieb [EMAIL PROTECTED]: And I thought I might get away without using dicts... Thanks, Greg Greg Graham schrieb: Paul, Python does not allow mixing variable length arguments and keyword arguments in that way. To accomplish what you want, you must add an argument preceded by a ** which will be a dict containing all of the keyword arguments as key, value pairs. You then have to retrieve the arguments from the dict by name. When called, the keyword arguments must be last. Here is a little example: def test(*column_definitions, **options): print Column Definitions: + , .join(column_definitions) output_csv_filename = options.get('output_csv_filename', None) print Output csv filename: + str(output_csv_filename) test(kundennummer, anrede, vorname, nachname, plz, ort, adresse, kontoinhaber, blz, kto, bankname, status, spielbeginn, letzte_aenderung, importdatum, briefdatum, buchungsdatum, stornodatum, output_csv_filename=None) Column Definitions:kundennummer, anrede, vorname, nachname, plz, ort, adresse, kontoinhaber, blz, kto, bankname, status, spielbeginn, letzte_aenderung, importdatum, briefdatum, buchungsdatum, stornodatum Output csv filename: None Greg ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] identifying the calling module/function
Jeff Younker schrieb: Telling us your goal might allow us to recommend a better and faster way of accomplishing it. - Jeff Younker - [EMAIL PROTECTED] - in dgf.py: (hope the formatting gets good for you, t-bird breaks the lines badly on my machine...) def csvwriter(*column_definitions): Edit Me! if sys.argv[0] == /usr/local/bin/xyz.py: # sorry, I am not allowed to give you guys xyz.py here, or the real name of the script - my boss is a bit paranoid o.O output_csv_filename = xyz.csv else: output_csv_filename = raw_input(Name of the file to produce? (ATTENTION: WILL BE OVERWRITTEN!) ) first_row = ;.join(*column_definitions) try: file = open(output_csv_filename, w) file.write(first_row) file.close() except: print(Couldn't open %s for writing. % output_csv_filename) sys.exit(1) return csv.writer(open(output_csv_filename, ab), delimiter=;, quoting=csv.QUOTE_NONE) Well, I am starting to realize that the whole thing might be a bit unnecessary... Just keeps me from creating csv.writer objects everywhere when the dialect is always the same. Also, I don't know if I understood the *argument_list thing correctly... ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
[Tutor] identifying the calling module/function
Hello Tutor! I am building a couple of scripts to manage a database for our company. The projects name is 'dgf'. As a lot of the functionality is used in more than one of these scripts, I moved this functionality to a module (dgf.py). It has several functions now. Question: Is there an easy way to determine inside dgf.py where the function call came from? dgf.py: def yetanotherfunction(): Edit Me! if function call came from script.py: ... Something like that? Regards -paul ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] MySQLdb for Python 2.5? (Debian related)
Michael Langford schrieb: However, Debian is known for stability and security, right? I don't know if I should install things without apt in a production environment, so I first have to ask my guru if it's alright. The *point* of buildout is that the entire installation is *local* to the application. There is no change system wide, just for the application that is running. This is *much* safer than using the system package manager. Its like running a standalone exe like putty on windows, versus installing a microsoft product. --Michael We're already considering to use the cheese shop for for all our Python stuff! Again, thanks a lot for the hint! - Paul Michael Langford schrieb: It's a distribution issue. As far as what I've found as having cutting edge (or even reasonably fresh) python packages in your package manager is dictated by the distro, who vary wildly in this. Debian SID at times All the Ubuntus Debian SID at times Fedora Core Debian testing Debian stable This is the #1 reason I use ubuntu on servers right now. And if the above is wrong now, these are generally feelings about a small sample set over a period of time. I really have just gone all Kubuntu/Xubuntu where I can these days. I will suggest you look into learning eggs, cheese shop and easy_install as an alternative to OS based package management for python. I was an awesome presentation by Brandon Rhodes Mill about Buildout at PyAtl a couple weeks ago. It automagically downloads all the eggs you need. You just create a setup.py and a quick config file, and check those in with your source. When you run a command when you start developing on a checkout, it pulls down all the eggs you need to work with that checkout from the cheeshop, putting them in a project local directory, which is then prepended to the python search path for that project. This means site-packages and you don't have fights when you install on multiple system who may need other past versions of modules. Buildout also gets the right version of python on the machine ( in a local directory again ) and is compatible with system where you don't have root access. Buildout was originally written by the Zope people I believe, but has been made independent of zope so all of us non-zope people can use it. --Michael Cheese Shop: www.python.org/pypi Monty Python Cheese Shop Skit: www.youtube.com/watch?v=B3KBuQHHKx0 Buildout: www.python.org/pypi/zc.buildout More about Eggs: http://peak.telecommunity.com/DevCenter/PythonEggs PyAtl (where presumably his talk will be posted): http://pyatl.org/ On Jan 23, 2008 11:01 AM, [EMAIL PROTECTED] wrote: I decided to install Python2.5 on the server machine to save me the time for low-level debugging ;) but it doesn't find the MySQLdb module... I searched through aptitude - the only thing I find is MySQLdb for Py2.4 ... What's happening here? I have to say that the client PC (on which my script runs fine with 2.5) has Ubuntu installed - can it be that the MySQLdb module is behind in Debian? Sorry for going off topic - if you guys don't want that here can move the problem to the Debian list - but maybe someone here knows about the status of the packages...? - Paul ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] csv.reader: bad argument type
Kent Johnson schrieb: [EMAIL PROTECTED] wrote: import csv import MySQLdb import sys try: datei = sys.argv[1] except: print(Usage: insert_into_db .csv-file) # convert csv to list reader = csv.reader(open(datei, rb), delimiter = ;, quotechar = , quoting = csv.QUOTE_NONE) /snip After copying it to the server, it says: server1:/usr/local/sbin# ./insert_dgf_customers.py /usr/local/sbin/my.csv Traceback (most recent call last): File ./insert_dgf_customers.py, line 27, in ? reader = csv.reader(open(datei, rb), delimiter = ;, quotechar = , quoting = csv.QUOTE_NONE) TypeError: bad argument type for built-in operation The file my.csv is a test file and correctly formatted (it's the same file that worked on the other machine). It doesn't seem to get as far as actually reading the file. I thought the built-in operation meant here is open(), but without putting csv.reader() around it everything works fine... And I guess csv.reader is _not_ built-in, for it from the module - is that correct? csv.reader is not built-in, that is correct. But it is implemented in C so you won't get a good traceback into where the failure is actually occurring. What version of Python is on the two machines? I guess that's it - my client has 2.5 while the server runs 2.4 ... I don't think any differences are mentioned in the library reference for csv but I'll have a second look. A couple of things to try, maybe some hints will come out: - print the value of datei just to make sure nothing funny there Filename is alright - my.csv - split the open to a separate line to make sure it is the call to reader that is the problem: f = open(datei, rb) reader = csv.reader(f, delimiter = ;, quotechar = , quoting = csv.QUOTE_NONE) Same result. Thanks nonetheless :) Kent - Paul ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
[Tutor] MySQLdb for Python 2.5? (Debian related) (was: csv.reader: bad argument type)
I decided to install Python2.5 on the server machine to save me the time for low-level debugging ;) but it doesn't find the MySQLdb module... I searched through aptitude - the only thing I find is MySQLdb for Py2.4 ... What's happening here? I have to say that the client PC (on which my script runs fine with 2.5) has Ubuntu installed - can it be that the MySQLdb module is behind in Debian? Sorry for going off topic - if you guys don't want that here can move the problem to the Debian list - but maybe someone here knows about the status of the packages...? - Paul ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor