Jython 2.5.1 Release Candidate 3 is out
On behalf of the Jython development team, I'm pleased to announce that Jython 2.5.1rc3 is available for download here: https://sourceforge.net/projects/jython/files/jython-dev/2.5.1rc3/jython_installer-2.5.1rc3.jar/download - See http://wiki.python.org/jython/InstallationInstructions for installation instructions. I didn't really want to have an RC3, I was hoping to have a final by now, but a data loss bug was discovered in RC2 and that prompted one more RC. I'm hoping that this is the last one and that we'll have a final shortly. Please see the NEWS file for detailed release notes. Please report any bugs that you find here: http://bugs.jython.org Thanks, -Frank -- http://mail.python.org/mailman/listinfo/python-announce-list Support the Python Software Foundation: http://www.python.org/psf/donations/
Try CodeInvestigator version 0.16.0
CodeInvestigator version 0.16.0 was released on Sept 23. It requires Python version 2.6 and a Firefox browser. Bug fixes: Indentation of comments and triple quoted strings. Triple quoted strings could embed additional spaces. Additional __str__ calls were made for an instance. Record scripts that don't have a .py extension. ELIF sometimes fails. Docstrings passed over to generated program Issue with importing 64 bit modules Initial syntax check sometimes rejected too much. Thanks Tim! Changes: Remarks. If you click in the right margin you can enter remarks. Remarks can be dragged to another location if you use Firefox 3.5. They are intended to work like poste-it notes attached to a printout of the code. Warning: The database will be initialized and you will lose your runs. CodeInvestigator is a tracing tool for Python programs. Running a program through CodeInvestigator creates a recording. Program flow, function calls, variable values and conditions are all stored for every line the program executes. The recording is then viewed with an interface consisting of the code. The code can be clicked: A clicked variable displays its value, a clicked loop displays its iterations. You read code, and have at your disposal all the run time details of that code. A computerized desk check tool and another way to learn about your program. http://sourceforge.net/project/showfiles.php?group_id=183942 -- http://mail.python.org/mailman/listinfo/python-announce-list Support the Python Software Foundation: http://www.python.org/psf/donations/
Sneaky web server 0.1
A fast portable pure-python multithreaded experimental WSGI web server in 300 lines of code: Python 2.4,2.5,2.6 version: http://code.google.com/p/web2py/source/browse/trunk/gluon/sneaky.py Python 3.0,3.1 version: http://code.google.com/p/web2py/source/browse/trunk/gluon/ sneaky3.py 1) Some third party benchmarks are in the doc strings but you should run them yourself. These are benchmarks for the servers, not the frameworks. 2) We are not sure it is perfectly WSGI compliant but it is very similar to Cherrypy wsgiserver 3.2. 3) Experimental means we are still ironing down WSGI compliance and we have not tested chunked uploads (only regular uploads). 4) This is distributed with web2py, it works with web2py, but until we have more tests done, web2py still uses cherrypy's wsgi server in production. 5) The license is GPL but we are going change it to MIT of BSD or Apache when done. 6) We are posting here because we hope you will give it a try and report compatibility issues with your favorite framework and browser. This will help us fix problems and turn into a fast production server. -- http://mail.python.org/mailman/listinfo/python-announce-list Support the Python Software Foundation: http://www.python.org/psf/donations/
Re: Finding application data after install - a solution?
En Tue, 22 Sep 2009 13:47:09 -0300, Wolodja Wentland wentl...@cl.uni-heidelberg.de escribió: On Tue, Sep 22, 2009 at 07:42 -0700, Aahz wrote: I want to: 1. Give administrators the freedom to install the data wherever they want 2. Adhere to the FHS (installing data within modules breaks it) 3. Be able to find that data again regardless of the installation scheme used 1 and 2 are easily solved... It was just not possible to find the data again. The snippet in the original code solves that. Given your mention of FHS, it sounds like you are focused on Unix-like systems, in which case why not rely on the standard mechanisms for config files? I do not intentionally focus on UNIX type systems, but I have grown up with *nix and I rather follow one scheme than none at all. But the proposed way works on Windows as well, although the users might find previously unseen directories like 'PREFIX/share/foo/doc' and the like on their system. I think many Windows users would say WTF!? when seeing those directories - and send cordial greetings to you, your parents and your whole family :) Instead of ending with, e.g., a directory like c:\usr\share\foo\doc, your program should ask the OS for the special folder CSIDL_COMMON_APPDATA and add the foo\doc part. Or any other suitable standard folder. A directory like c:\usr\share\foo on Windows is as ridiculous as /Documents\ and\ Settings/All\ Users/Application\ Data/foo on any unix like system. As $DATA_PREFIX is only known at build time there was (until now) no reliable way to find the data if the only information one can get is $LIB_PREFIX, because these two might be totally unrelated. You should probably raise this issue at the distutils-sig mailing list: http://www.python.org/community/sigs/current/distutils-sig/ -- Gabriel Genellina -- http://mail.python.org/mailman/listinfo/python-list
Re: Twisted: 1 thread in the reactor pattern
On Sep 23, 5:57 pm, exar...@twistedmatrix.com wrote: On 06:08 am, jacopo.pe...@gmail.com wrote: I am diving into Twisted and Perspective Broker (PB) in particular and I would like to understand more about what happens behind the curtains. Say I have a client and a server on two different machines, the server gets callRemote() 19s in an asynchronous way, these requests are parked in a queue and then served sequentially (not in parallel 13 correct me if I am wrong). Since, as you point out below, there is only one thread, the remote methods can only be invoked one at a time. However, rather central to the asynchronous operation of Twisted libraries and applications, the remote method itself may return before the remote call has been completely serviced. So while only one remote method will run at a time, each of the two remote calls may run concurrently at some point before they are responded to. If everything is implemented in a single thread, how is it possible that while the processor is engaged in the processing triggered by callRemote() 19s at the same time the reactor is ready to listen/accept new events and put them in a queue? To me it looks like there should be at least 2 processes, one for the reactor and on for the rest. It isn't possible. While the remote methods are running, other events are not being serviced. This is what is meant when people describe Twisted as a *cooperative* multitasking system. Event handlers (such as remote methods) run for a short enough period of time that it doesn't matter that the reactor is prevented from accepting new connections (or what have you) for the duration of their execution. Jean-Paul Jean -Paul, not sure I have understood. Say I have one server S and two clients C1 and C2 (all on separate machines). (a) C1 requests a remote call of f1() to S, f1() requires 5 minutes of processing. (b) S puts f1() in a queue and returns immediately a Deferred to C1. (c) Now f1() starts and keeps S’s processor busy for 5 mins (d) after few seconds C2 requests a remote call f2() to S. (e) On S the processor is already engaged with f1() but still “someone” on S is able to accept the request from C2, put it in a queue (after f1()) and return a Deferred to C2. (f) At some point after f1() is finished f2() will start I believe (b) is what you say “run for a short enough period of time that it doesn't matter that the reactor is prevented from accepting new connections (or what have you) for the duration of their execution.” ?! Don’t we have (c) and (e) running at the same time here? How is it possible to have only one Thread? Thanks, Jacopo -- http://mail.python.org/mailman/listinfo/python-list
Re: logging.handlers.SMTPHandler question
On Sep 24, 4:14 am, akonsu ako...@gmail.com wrote: hello, SMTPHAndler seems to email every single record separately. is there a way to collect all log output and then send it in a single email message? or do i have to do it manually? You can use a buffering SMTP handler, see: http://gist.github.com/192587 I haven't tested this code - I got it from this page (Polish language): http://python.pinger.pl/m/552751/bufferingsmtphandler -- http://mail.python.org/mailman/listinfo/python-list
Screencapture of a video
Hi, I was trying to capture the video which I was viewing using Imagegrab module in PIL (windows). But, I found that the video was not captured at all. A quick googling tells me that it is because the windows does not have access to the viewing area. Is there any way I can capture the video? It will be very helpful. -- http://mail.python.org/mailman/listinfo/python-list
Re: logging.handlers.SMTPHandler question
On Sep 24, 8:37 am, Vinay Sajip vinay_sa...@yahoo.co.uk wrote: On Sep 24, 4:14 am, akonsu ako...@gmail.com wrote: http://python.pinger.pl/m/552751/bufferingsmtphandler Looking back at it, it looks as if it's adapted from some test code I wrote back in 2002, see here: http://opensolaris.org/sc/src/xen-gate/xvm-3.3+xen.hg/tools/python/logging/logging-0.4.9.2/test/log_test11.py -- http://mail.python.org/mailman/listinfo/python-list
Average of entries in a list of lists
Hello, I'm a bit of a newbie to python, so this may very well be a basic question: I have a list of lists, with around 1000 lists looking like this: ['0.000744', '0.480106', 'B']. I need the average of the first to entries of all the lists, can anybody help how me to do this? Thanks in advance, Evora -- View this message in context: http://www.nabble.com/Average-of-entries-in-a-list-of-lists-tp25543334p25543334.html Sent from the Python - python-list mailing list archive at Nabble.com. -- http://mail.python.org/mailman/listinfo/python-list
Re: Average of entries in a list of lists
On Thu, 24 Sep 2009 01:30:10 -0700, Evora wrote: Hello, I'm a bit of a newbie to python, so this may very well be a basic question: I have a list of lists, with around 1000 lists looking like this: ['0.000744', '0.480106', 'B']. I need the average of the first to entries of all the lists, can anybody help how me to do this? This sounds rather like homework, so here are some clues rather than a working solution. Let's start with a simple function to calculate the average of some numbers. This is so trivial you should already have this, or something like it: def average(numbers): return sum(numbers)/float(len(numbers)) The call to float is to avoid a difficulty with division in versions of Python before 3.0. If you're using Python 3.0 or 3.1, you can leave it out. If you can guarantee that your numbers will already be floats, you can leave it out too. Or you can put this line at the top of your program: from __future__ import division Now you need another function which extracts all the numbers from your list-of-lists and converts them from strings to floats. To extract the first N items from a list L, you use: L[:N] For example, if you have L = ['0.1', '0.2', 1234, 'C'] and you want the first three items, you would say: L[:3] and get: ['0.1', '0.2', 1234] Secondly, if you have a list of numbers in the form of strings: L = ['0.1', '0.2'] and you want to get a list of actual floats, you can do this: a, b = L numbers = [float(a), float(b)] but probably faster is: numbers = map(float, L) Thirdly, if you have a list-of-lists, and you want to combine values taken from each sub-list into a single summary list, you can use a variation on the following: list_of_lists = [ [1, 'a', 'b'], [2, 'c', 'd'], [3, 'e', 'f'] ] summary = [] for sublist in list_of_lists: # extract the first item x = sublist[0] # add it to the end of the summary summary.append(x) # summary should now be [1, 2, 3] Finally, if you find yourself writing something like: x, y = sublist L.append(x) L.append(y) you can save two lines by doing this: L.extend(sublist) Put all those pieces together, make a few minor modifications, and you will get a function that extracts the first two items from each sub-list, converts them from strings to floats, and consolidates them into a single summary list. Then you can pass that summary list to the average function, and Bob's your uncle. -- Steven -- http://mail.python.org/mailman/listinfo/python-list
Re: Intercepting binding?
On Wed, 23 Sep 2009 18:10:10 -0700, andrew cooke wrote: these are valid points, but in practice the main use (for the restricted application i care about) is si,ple variables, and this is an optional extra to help the user, so it's OK if it only works sometimes. at the moment i'd be happy with any half-baked unreliable solution that is transparent... Speaking as a user (although not of Andrew's domain specific language), I'd like to say to developers PLEASE PLEASE PLEASE don't try to help me with half-baked unreliable solutions that only work sometimes. There's few things worse than unreliable tools that break just when you've come to rely on them. -- Steven -- http://mail.python.org/mailman/listinfo/python-list
Re: custom data warehouse in python vs. out-of-the-box ETL tool
@Martin: Thanks for your great feedback. So do you think it would be very beneficial for me to start with an Inman or Kimball book? Or do you think it would be just leisure reading and not very practical at best - fill my head with needless jargon and inflexible dogmas, at worst? I took a database class in college, understand the basic principals of normalisation, and have built a few complicated RDBMS schemas from the ground up. On Sep 23, 12:28 pm, Martin P. Hellwig martin.hell...@dcuktec.org wrote: snfctech wrote: @Martin: I originally thought that there was nothing magical about building a datawarehouse, but then I did a little research and received all sorts of feedback about how datawarehouseprojects have notorious failure rates, that datawarehousedesign IS different than normal RDBMS - and then there's the whole thing about data marts vs. warehouses, Kimball vs. Inmon, star schemas, EAV tables, and so on. So I started to think that maybe I needed to get a little better read on the subject. Yes failure rate for datawarehouseprojects is quite high, so are other IT projects without data warehouses. Datawarehousedesign is not that much different than 'normal' RDBMS, you are following the same decisions for example: - Do I rather have multiple copies of data than slow or complicated access? If yes how do I ensure integrity? - How do I do access control on value level? - Do I need fail over or load balancing, if yes how much of it can I do on the application level? The thing is if you never have designed a database from a database point of view because you used abstractions that hide these ugly details then yes, Data warehouses are different than normal RDBMS. Yes you can make it all sound very complicated by throwing PHB words in like meta schema's, dimensional approach, subject orientated, etc. I like to see it more like I have a couple of data sources, I want to combine them in a way that I can do neat stuff with it. The 'combine them' part is the datawarehousedesign, the 'neat stuff' part are the clients that use that datawarehouse. If you wish you can start making it more complicated by saying but my data sources are also my clients, but that is another step. I guess you can sum up all this by saying a datawarehouseis a method of gathering already existing data and present them in a different way, the concept is simple, the details can be complicated if you want/need it to be. -- MPHhttp://blog.dcuktec.com 'If consumed, best digested with added seasoning to own preference.' -- http://mail.python.org/mailman/listinfo/python-list
Re: IDE for python similar to visual basic
In article pan.2009.09.11.12.02.03.656...@nowhere.com, Nobody nob...@nowhere.com wrote: On Mon, 07 Sep 2009 23:56:17 +, Albert van der Horst wrote: SNIP In view of the above this is not quite the correct way to put it. What I resent is that it leads to a non-professional attitude of the graphical part. Programming is over, lets now kludge some screens together. No. The graphics part has to be carefully designed, carefully tested, and carefully written, even if it is using a graphical tool. So, yes please, *do* create a GUI programmatically. My view is that the program should provide functionality without unnecessarily dictating the way in which that functionality is used. Essentially, it's an issue of loose coupling. The interface really should be configurable by the user according to their needs. The code doesn't need to *know* the position or dimensions of a widget, or its label or colour or spacing, let alone dictate them. In most cases, the code shouldn't even get to dictate that specific widgets even exist. Rather, it should provide actions which can be bound to buttons, menu items and/or accelerators as the user chooses. I don't necessarily disagree. But how does this work in practice? I have a totally programmable editor (I do, I'm using it right no.) I'm able to redefine commands to the point that I have no longer a command to quit the program, and even have no longer a possibility to define a new key-combination to quit the program. The hacker who wrote it would say: don't do that. Combined with my habit to switch the Caps lock and control keys and use the editor full-screen, someone else really gets nowhere. What if I prefer to have the gaz throttle and the clutch pedal of my car switched. Is that a good idea? Bottomline, let the user choose must not be an excuse for us, where we are not able to propose a good choice. (You may read the subject note-eater on my website.) Groetjes Albert -- -- Albert van der Horst, UTRECHT,THE NETHERLANDS Economic growth -- being exponential -- ultimately falters. alb...@spearc.xs4all.nl =n http://home.hccnet.nl/a.w.m.van.der.horst -- http://mail.python.org/mailman/listinfo/python-list
Date using input
Sorry to ask a simple question but I am a little confused how to combine the input function and the date time module. Simply at the start of the program I want to prompt the user to enter the date, desirably in the format dd/mm/year. However I want to make sure that python understands the time format because first the date will form part of the name of the output file so dd/mm/year as 1st September 2009, secondly if I have multiple output files saved in a directory I may need to search later on the files and contents of files by date range. So can I timestamp the file? I know this is a simple question but it eludes me exactly how to do it. I have the basics from http://docs.python.org/library/datetime.html from datetime import date date = input(type date dd/mm/year: ) datetime(day,month,year) # some program blocks #print to file(name = date) or apphend if it exists -- http://mail.python.org/mailman/listinfo/python-list
very weird python bug..
Dear List, Maybe someone could help out with this mysterious bug.. starting a python program in a directory named python, makes the importation of modules sometimes impossible with cryptic error messages.. sam...@linux-912g:~ mkdir paska2 sam...@linux-912g:~ sam...@linux-912g:~ echo import scipy paska2/testi.py sam...@linux-912g:~ sam...@linux-912g:~ export PYTHONPATH= sam...@linux-912g:~ sam...@linux-912g:~ python paska2/testi.py sam...@linux-912g:~ sam...@linux-912g:~ echo import scipy python/testi.py sam...@linux-912g:~ sam...@linux-912g:~ python python/testi.py Traceback (most recent call last): File python/testi.py, line 1, in module import scipy File /usr/local/lib/python2.6/site-packages/scipy/__init__.py, line 18, in module from numpy import oldnumeric File /usr/lib/python2.6/site-packages/numpy/oldnumeric/__init__.py, line 14, in module from compat import * File /usr/lib/python2.6/site-packages/numpy/oldnumeric/compat.py, line 97, in module import pickle, copy File /usr/lib/python2.6/pickle.py, line 171, in module class Pickler: File /usr/lib/python2.6/pickle.py, line 250, in Pickler def put(self, i, pack=struct.pack): AttributeError: 'module' object has no attribute 'pack' --- = If I start the program in directory paska2, everythings OK, but if the directory name happens to be python, the importation of the modules goes nuts! Very Weird. Kind Regards, Sampsa -- Niin tärkeä ihmisoikeus kuin sanavapaus onkin, täytyy harkita ja punnita kommentteja, kommentoi asiaa Sananvapausjärjestö Suomen Penin varapuheenjohtaja Jarkko Tontti Uudelle Suomelle. (Uusi Suomi, 25.8.2009) -- http://mail.python.org/mailman/listinfo/python-list
Re: Creating a local variable scope.
In article 65e8a017-abcb-49ad-8867-bc473f83e...@s39g2000yqj.googlegroups.com, Bearophile bearophileh...@lycos.com wrote: Steven D'Aprano: (3) Create an inner function, then call that. Several people after someone gives this anwer. My personal opinion is that if you really need a local scope inside a function, the function is doing too much and should be split up. I agree. And a way to split a function is to define an inner function, that's one of their main purposes. No need to add other things to the language as the OP suggests. There are exceptions. E.g. implementations of quicksort have a recursive inner function, that you may prefer to not have exposed. Also there may be data to communicate to or between instances of the inner function. At least that is the situation in most languages. I would be interested to learn if and how Python gets around that. Bye, bearophile Groetjes Albert -- -- Albert van der Horst, UTRECHT,THE NETHERLANDS Economic growth -- being exponential -- ultimately falters. alb...@spearc.xs4all.nl =n http://home.hccnet.nl/a.w.m.van.der.horst -- http://mail.python.org/mailman/listinfo/python-list
Average of entries in a list of lists
Hello, I'm a bit of a newbie to python, so this may very well be a basic question: I have a list of lists, with around 1000 lists looking like this: ['0.000744', '0.480106', 'B']. I need the average of the first to entries of all the lists, can anybody help how me to do this? Thanks in advance, Evora -- View this message in context: http://www.nabble.com/Average-of-entries-in-a-list-of-lists-tp2090p2090.html Sent from the Python - python-list mailing list archive at Nabble.com. -- http://mail.python.org/mailman/listinfo/python-list
Re: easy question, how to double a variable
On Sep 23, 7:36 pm, David C Ullrich dullr...@sprynet.com wrote: On Tue, 22 Sep 2009 02:34:53 +, Steven D'Aprano wrote: On Mon, 21 Sep 2009 13:50:23 -0500, David C Ullrich wrote: But you actually want to return twice the value. I don't see how to do that. What? Seriously? You're saying it _can_ be done in Python? They must have added something to the standard library again. I mean how can you return twice a value without a twice function to start with? I've tried. You'd think def twice(n): return twice(n) would work, but I get this really long error message. You're not just yanking the OP's chain??? That would be cruel. I mean the guy has enough problems already... Sorry, there is no 'twice' builtin. I think what you are looking for is: def twice(n): return return n Iain -- http://mail.python.org/mailman/listinfo/python-list
Re: Download and save a picture - urllib
MRAB pyt...@mrabarnett.plus.com (M) wrote: M mattia wrote: You were right, the problem was with the print function, using a normal write everythong works fine. M You should open the output file as binary (it doesn't matter on M Linux/Unix, but is a good idea anyway for portability). It probably DOES matter also on Linux/Unix, as the OP is using Python3. -- Piet van Oostrum p...@cs.uu.nl URL: http://pietvanoostrum.com [PGP 8DAE142BE17999C4] Private email: p...@vanoostrum.org -- http://mail.python.org/mailman/listinfo/python-list
Re: very weird python bug..
Sampsa Riikonen wrote: = If I start the program in directory paska2, everythings OK, but if the directory name happens to be python, the importation of the modules goes nuts! What's inside the python/ subdirectory? Do you happen to have a file called struct.py inside it? -- http://mail.python.org/mailman/listinfo/python-list
Re: custom data warehouse in python vs. out-of-the-box ETL tool
snfctech wrote: @Lemburg: Thanks for the suggestion. I'm sure you make a fine product, but my development platform is Linux, and I don't want any additional Windows servers to deal with (than the ones I'm already stuck with.) Strange, EasySoft used to support their product on Linux as well... http://download.freshmeat.net/projects/easysoftsqlengine Looks like they stopped with supporting it on Linux and now only have Windows downloads available. mxODBC and mxODBC Connect work just fine on Linux, so you could use them to build a custom joining solution in Python. It really depends on whether you need to run those joins on an on-demand basis or not: AFAIK, Talend can only run jobs which then apply the JOINs and put the resulting data somewhere, e.g. into a file or another database table. With Python, you could do the join straight away and use the data directly without the Python script - without having to go through an external temporary resource. Anyway, just a suggestion. On Sep 23, 2:02 am, M.-A. Lemburg m...@egenix.com wrote: snfctech wrote: Does anyone have experience building a data warehouse in python? Any thoughts on custom vs using an out-of-the-box product like Talend or Informatica? I have an integrated system Dashboard project that I was going to build using cross-vendor joins on existing DBs, but I keep hearing that a data warehouse is the way to go. e.g. I want to create orders and order_items with relations to members (MS Access DB), products (flat file) and employees (MySQL). Thanks in advance for any tips. You might want to look at this solution for doing cross-database JOINs: EasySoft ODBC Join-Engine: http://www.easysoft.com/products/data_access/odbc_odbc_join_engine/in... and then use our mxODBC to access EasySoft's Engine: http://www.egenix.com/products/python/mxODBC/ or mxODBC Connect, if you have a client-server setup: http://www.egenix.com/products/python/mxODBCConnect/ Some database engines also allow integrating external ODBC data sources - the external tables then look like normal database tables and can be used in JOINs as well. I know that DB2 and Oracle support this kind of setup. You can access those using mxODBC as well. -- Marc-Andre Lemburg eGenix.com Professional Python Services directly from the Source (#1, Sep 23 2009) Python/Zope Consulting and Support ...http://www.egenix.com/ mxODBC.Zope.Database.Adapter ...http://zope.egenix.com/ mxODBC, mxDateTime, mxTextTools ...http://python.egenix.com/ ::: Try our new mxODBC.Connect Python Database Interface for free ! eGenix.com Software, Skills and Services GmbH Pastor-Loeh-Str.48 D-40764 Langenfeld, Germany. CEO Dipl.-Math. Marc-Andre Lemburg Registered at Amtsgericht Duesseldorf: HRB 46611 http://www.egenix.com/company/contact/ -- Marc-Andre Lemburg eGenix.com Professional Python Services directly from the Source (#1, Sep 24 2009) Python/Zope Consulting and Support ...http://www.egenix.com/ mxODBC.Zope.Database.Adapter ... http://zope.egenix.com/ mxODBC, mxDateTime, mxTextTools ...http://python.egenix.com/ ::: Try our new mxODBC.Connect Python Database Interface for free ! eGenix.com Software, Skills and Services GmbH Pastor-Loeh-Str.48 D-40764 Langenfeld, Germany. CEO Dipl.-Math. Marc-Andre Lemburg Registered at Amtsgericht Duesseldorf: HRB 46611 http://www.egenix.com/company/contact/ -- http://mail.python.org/mailman/listinfo/python-list
Re: Intercepting binding?
On Sep 23, 5:49 pm, Rhodri James rho...@wildebst.demon.co.uk wrote: On Thu, 24 Sep 2009 01:34:35 +0100, andrew cooke and...@acooke.org wrote: For example, I assume it's possible to somehow access the dictionary for the current block, but I can't see how to do this after assignment. If I do it in the Foo constructor, for example, a will not yet be bound. I apologise for failing to notice earlier that you know what you're talking about. I blame the hour :-) I'm not sure you can access the namespace dictionary of the current block (module?), that's the problem. Oh, except via locals(), which might do exactly what you're after depending. Excuse me, I'm being very dim tonight. Hmmm. @contextlib.contextmanager def capture_changed_bindings(): before = sys._getframe(2).f_locals.copy() changed = {} yield changed after = sys._getframe(2).f_locals for key,value in after.iteritems(): if value is changed: continue if key not in before or value is not before[key]: changed[key] = value def test(): a = 2 b = 3 c = 4 with capture_changed_bindings() as changed: b = 5 c = 4 d = 6 print changed test() Quick and dirty, not robust at all. But you get the idea. Carl Banks -- http://mail.python.org/mailman/listinfo/python-list
Re: Average of entries in a list of lists
On Thu, Sep 24, 2009 at 8:54 PM, Evora lasse_lorent...@hotmail.com wrote: Hello, I'm a bit of a newbie to python, so this may very well be a basic question: I have a list of lists, with around 1000 lists looking like this: ['0.000744', '0.480106', 'B']. I need the average of the first to entries of all the lists, can anybody help how me to do this? Say if we have: data = [['1', '2', 'B'], ['2', '3', 'B'], ['3', '4', 'B']] The easier way is to iterate through the big list, and get out the first item and add them. Once you've added them all, divide by the number of lists in the big list. total = 0 for list in data: total += int(list[0]) print(total/len(data)) # OUTPUT 2.0 Or there is some ... not so obvious way, but same idea: print(sum(int(numbers[0]) for numbers in data) / len(data)) # OUTPUT 2.0 (I'm using Python 3.1 by the way. You might have to change the print function.) Cheers, Xav -- http://mail.python.org/mailman/listinfo/python-list
Re: Finding application data after install - a solution?
On Thu, Sep 24, 2009 at 04:07 -0300, Gabriel Genellina wrote: I do not intentionally focus on UNIX type systems, but I have grown up with *nix and I rather follow one scheme than none at all. But the proposed way works on Windows as well, although the users might find previously unseen directories like 'PREFIX/share/foo/doc' and the like on their system. I think many Windows users would say WTF!? when seeing those directories - and send cordial greetings to you, your parents and your whole family :) That is probably true, but Windows has a 'etc' directory (c:\windows\system32\drivers\etc) which AFAIK contains the hosts file and/or other goodies from *NIX. Instead of ending with, e.g., a directory like c:\usr\share\foo\doc, your program should ask the OS for the special folder CSIDL_COMMON_APPDATA and add the foo\doc part. Or any other suitable standard folder. Is CSIDL_COMMON_APPDATA and environment variable set on all Windows flavours? Are there versions that relying on this env var (if it is one) will cause havoc and the death of millions of kittens? (ie. is it available on all versions) Where can I find Information on these other standard folders? Sorry, but I have not used a Windows machine in ages. A directory like c:\usr\share\foo on Windows is as ridiculous as /Documents\ and\ Settings/All\ Users/Application\ Data/foo on any unix like system. Agreed! Although I find the *NIX flavour more palatable to my eye ;-) As $DATA_PREFIX is only known at build time there was (until now) no reliable way to find the data if the only information one can get is $LIB_PREFIX, because these two might be totally unrelated. You should probably raise this issue at the distutils-sig mailing list: http://www.python.org/community/sigs/current/distutils-sig/ I already did [1] but the distutils developers are quite busy discussing a the new format for setup.cfg. I did not got any feedback on the solution I developed and thought that other Python users might have a better solution to this problem. How do *you* deal with application data in your programs? Is there a way that works on as many platforms as possible? so long Wolodja [1] http://mail.python.org/pipermail/distutils-sig/2009-September/013238.html signature.asc Description: Digital signature -- http://mail.python.org/mailman/listinfo/python-list
Re: Intercepting binding?
On Sep 23, 7:41 pm, andrew cooke and...@acooke.org wrote: On Sep 23, 10:11 pm, Dave Angel da...@ieee.org wrote: This comes up periodically in this list, and the answer is always something like: you can't get there from here. Well, I'm both flexible and desperate, so this is a possible route (perhaps near enough): import sys class Foo(object): def __rlshift__(self, name): try: raise Exception() except: locals = sys.exc_traceback.tb_frame.f_back.f_locals locals[name] = self if __name__ == '__main__': foo = Foo() 'a' foo print(a) Did you try this inside a function? (Hint: it won't work in a function.) BTW, if you are desperate to do this then I'd say you lack good perspective. You are subverting some of the most basic behavior of Python here for something of marginal and only occasional usefulness. If you are that desperate just retype the name, it won't be the end of the world. a = Foo('a') Carl Banks -- http://mail.python.org/mailman/listinfo/python-list
Re: Finding application data after install - a solution?
Wolodja Wentland wrote: I think many Windows users would say WTF!? when seeing those directories - and send cordial greetings to you, your parents and your whole family :) That is probably true, but Windows has a 'etc' directory (c:\windows\system32\drivers\etc) which AFAIK contains the hosts file and/or other goodies from *NIX. True, but it's little known and -- as you show -- buried deep in the innards. I assume that it's the residue (or a symptom) of the Posix compatibility layer. Is CSIDL_COMMON_APPDATA and environment variable set on all Windows flavours? Certainly all those which Python currently supports. There are some small subtleties which have changed between older and newer versions. Are there versions that relying on this env var (if it is one) will cause havoc and the death of millions of kittens? (ie. is it available on all versions) Where can I find Information on these other standard folders? Sorry, but I have not used a Windows machine in ages. (drops CSIDL_COMMON_APPDATA into Google and...) http://msdn.microsoft.com/en-us/library/bb762494%28VS.85%29.aspx A directory like c:\usr\share\foo on Windows is as ridiculous as /Documents\ and\ Settings/All\ Users/Application\ Data/foo on any unix like system. Agreed! Although I find the *NIX flavour more palatable to my eye ;-) And they say familiarity breeds contempt! ;) TJG -- http://mail.python.org/mailman/listinfo/python-list
Re: Intercepting binding?
On Sep 24, 7:12 am, Carl Banks pavlovevide...@gmail.com wrote: with capture_changed_bindings() as changed: b = 5 c = 4 d = 6 print changed test() Quick and dirty, not robust at all. But you get the idea. Carl Banks brilliant. using the with context is an excellent idea. thanks very much. andrew -- http://mail.python.org/mailman/listinfo/python-list
Re: Date using input
flebber wrote: Sorry to ask a simple question but I am a little confused how to combine the input function and the date time module. Simply at the start of the program I want to prompt the user to enter the date, desirably in the format dd/mm/year. However I want to make sure that python understands the time format because first the date will form part of the name of the output file so dd/mm/year as 1st September 2009, secondly if I have multiple output files saved in a directory I may need to search later on the files and contents of files by date range. So can I timestamp the file? I know this is a simple question but it eludes me exactly how to do it. I have the basics from http://docs.python.org/library/datetime.html from datetime import date date = input(type date dd/mm/year: ) datetime(day,month,year) # some program blocks #print to file(name = date) or apphend if it exists What version of python is your class, instructor, and text book using? If you want to learn fastest, you probably need to be using the same, or nearly same environment. The input() function is one place where it matters whether it's Python 2.x or Python 3.x. While you're at it, you should give the rest of your environment, such as which OS. The doc page you pointed us to is for Python 2.6.2, but the input function on that version returns an integer. Perhaps you want raw_input() ? What code have you written, and what about it doesn't work? Have you added print statements before the line that causes the error to see what the intermediate values are? To try to anticipate some of your problems, you should realize that in most file systems, the slash is a reserved character, so you can't write the date that way. I'd suggest using dashes. I put dates in directory names, and I always put year, then month, then day, because then sorting the filenames also sorts the dates. I'm not in a country that sorts dates that way, but it does make things easier. So directories for the last few days would be: 2009-09-22 2009-09-23 2009-09-24 When asking the user for a date, or telling him a date, by all means use your country's preferred format, as you say. You mention timestamping the file. While that can be done (Unix touch, for example), I consider it a last resort for keeping track of useful information. At best, it should be an extra reminder of something that's already in the file contents. And since many programs make the assumption that if the timestamp doesn't change, the contents haven't changed, you can only reasonably do this on a file whose contents are fixed when first created. If you control the internal format of the file, put the date there, perhaps right after the header which defines the data type and version. -- http://mail.python.org/mailman/listinfo/python-list
Re: Average of entries in a list of lists
Evora wrote: Hello, I'm a bit of a newbie to python, so this may very well be a basic question: I have a list of lists, with around 1000 lists looking like this: ['0.000744', '0.480106', 'B']. I need the average of the first to entries of all the lists, can anybody help how me to do this? Thanks in advance, Evora first to entries probably is intended to be first two entries This is the same typo you made when you cross-posted on python-tutor. Have you made a sample program (script) that partially works? Show us what you've done, and explain what's wrong with it. Usually by that time, you'll figure it out yourself, and learn more. But if you're really stuck, and not just wanting someone else to do tonight's homework, show us what you've got. Start with a program that defines a list of maybe 4 lists (you can change it to 1000 later). Just enough data that you can see the algorithm working. Then write some form of loop that you think might accomplish the result, or at least a subset of the result. Show us that code, and its output, and explain why it falls short. And of course, tell us the Python version and OS environment you're running in. You'd be surprised how often that matters. DaveA -- http://mail.python.org/mailman/listinfo/python-list
Re: Re: Date using input
I am using python 2.6.2, I haven't updated to 3.0 yet. No I have no class or instructor, I am learning this myself. I have Hetlands book Beginning Python Novice to Professional and online documentation books so Dive into Python, python.org etc. Using the SPE editor. I have currently only fully written basic psuedocode to give me a basic framework to guide myself. #Basic pseudocode #Purpose to get raw input and calculate a score for a field of options and return that #score in a set in descending order. #Multiple sets sould be written to the doc #Obtain date #Check if txt file with same date exists. If yes apphend to results to file. #Obtain location #Set Dictionary #Event number #Obtain set size #Prompt first entry #First Entry Number #First Entry Name #Set Blocks to obtain and calculate data #Block 1 example - Placings Block #Obtain number of events competed in #Obtain how many times finished first #Ensure this value is not greater than Number of Events #Number of Firsts divide by Events * total by 15. #Obtain Second finishes #Ensure this value is not greater than Number of Events #Number of Seconds divide by Events * total by 10. #Continue On with this #Block 2 - Lookup coach Dict and apply value. #Obtain Surname of Coach #Lookup Coach File and Match Name and get value. #Blocks continue gaining and calculating values. #create txt file named using date #Sum Values Block1 + Block2 etc #Print to file event number and field with name number individual Block totals and Sum Total #Arranged in descending Sum Total. #Prompt are there any more events? Yes return to start #Apphend all additional events to same day file seperated by blank line. On Sep 24, 2009 9:59pm, Dave Angel da...@ieee.org wrote: flebber wrote: Sorry to ask a simple question but I am a little confused how to combine the input function and the date time module. Simply at the start of the program I want to prompt the user to enter the date, desirably in the format dd/mm/year. However I want to make sure that python understands the time format because first the date will form part of the name of the output file so dd/mm/year as 1st September 2009, secondly if I have multiple output files saved in a directory I may need to search later on the files and contents of files by date range. So can I timestamp the file? I know this is a simple question but it eludes me exactly how to do it. I have the basics from http://docs.python.org/library/datetime.html from datetime import date date = input(type date dd/mm/year: ) datetime(day,month,year) # some program blocks #print to file(name = date) or apphend if it exists What version of python is your class, instructor, and text book using? If you want to learn fastest, you probably need to be using the same, or nearly same environment. The input() function is one place where it matters whether it's Python 2.x or Python 3.x. While you're at it, you should give the rest of your environment, such as which OS. The doc page you pointed us to is for Python 2.6.2, but the input function on that version returns an integer. Perhaps you want raw_input() ? What code have you written, and what about it doesn't work? Have you added print statements before the line that causes the error to see what the intermediate values are? To try to anticipate some of your problems, you should realize that in most file systems, the slash is a reserved character, so you can't write the date that way. I'd suggest using dashes. I put dates in directory names, and I always put year, then month, then day, because then sorting the filenames also sorts the dates. I'm not in a country that sorts dates that way, but it does make things easier. So directories for the last few days would be: 2009-09-22 2009-09-23 2009-09-24 When asking the user for a date, or telling him a date, by all means use your country's preferred format, as you say. You mention timestamping the file. While that can be done (Unix touch, for example), I consider it a last resort for keeping track of useful information. At best, it should be an extra reminder of something that's already in the file contents. And since many programs make the assumption that if the timestamp doesn't change, the contents haven't changed, you can only reasonably do this on a file whose contents are fixed when first created. If you control the internal format of the file, put the date there, perhaps right after the header which defines the data type and version. -- http://mail.python.org/mailman/listinfo/python-list
Re: Intercepting binding?
On Sep 24, 5:20 am, Steven D'Aprano ste...@remove.this.cybersource.com.au wrote: Speaking as a user (although not of Andrew's domain specific language), I'd like to say to developers PLEASE PLEASE PLEASE don't try to help me with half-baked unreliable solutions that only work sometimes. There's few things worse than unreliable tools that break just when you've come to rely on them. The context is that I am looking at how best to provide debugging support for a recursive descent parser. If the actions are pure then the whole thing is deterministic, so it might be possible to do something like buddha (a declarative debugger for haskell). I'm also looking for a paper that was linked to somewhere this last month or so on one of the popular sites/blogs about debugging prolog (since the two are closely related - depth first search etc) - I know this is vague, but if it rings a bell with anyone... Anyway, doing this in pure Python makes risks losing the information associated with variable names. I was wondering how I might reduce that. The reason I asked for unreliable half-baked solutions is that I am exploring what might be possible and, in my experience, this group prefers to lecture me on what they think I should do rather than answer the damn question. I was hoping that by explicitly saying that reliability is not important, people might feel more free to give wild ideas that I could learn from and improve on. It's significant, depressing, and not at all surprising that every person who replied to this thread told me, in one way or another, that was I was asking was wrong or impossible or foolhardy. If I want your advice on how to write popular tools, you can be sure I will come and ask you for it. That is not what I asked for here. But since we're all passing round unsolicited advice, here's some from me. Being an anal retentive will get you a long way in programming. Dotting the is and crossing the ts is 99% of what it's all about. I agree. But the last 1% requires a little bit of imagination. You should try it one day. Andrew -- http://mail.python.org/mailman/listinfo/python-list
Re: Solved - Python: automate input to MySQL query
On Wed, Sep 23, 2009 at 4:21 PM, D'Arcy J.M. Cain da...@druid.net wrote: In any case, I have a strong philosophical objection to using the same name to refer to two different things regardless of any operational issues. The manager.firstname and employee.firstname are not the same thing and should have different names to reflect that. They are in separate tables, that should be enough to reflect the fact that they're not the same thing. A similar issue comes up in the classic situation of the inventory table price field and the sale item price field. They sound like the same thing but they are not. One is the current price in the catalogue and the other is the price it was sold for at a particular time. They need different names for that reason. I am not sure if you're talking about two different tables (inventory and sale?). If you meant a single table; I would hope you don't have two columns, with the same name, in a single table. If you were talking about two different tables, see my above comment. -- http://mail.python.org/mailman/listinfo/python-list
Re: Date using input
flebber.c...@gmail.com wrote: I am using python 2.6.2, I haven't updated to 3.0 yet. No I have no class or instructor, I am learning this myself. I have Hetlands book Beginning Python Novice to Professional and online documentation books so Dive into Python, python.org etc. Using the SPE editor. I have currently only fully written basic psuedocode to give me a basic framework to guide myself. #Basic pseudocode #Purpose to get raw input and calculate a score for a field of options and return that #score in a set in descending order. #Multiple sets sould be written to the doc #Obtain date #Check if txt file with same date exists. If yes apphend to results to file. #Obtain location #Set Dictionary #Event number #Obtain set size #Prompt first entry #First Entry Number #First Entry Name #Set Blocks to obtain and calculate data #Block 1 example - Placings Block #Obtain number of events competed in #Obtain how many times finished first #Ensure this value is not greater than Number of Events #Number of Firsts divide by Events * total by 15. #Obtain Second finishes #Ensure this value is not greater than Number of Events #Number of Seconds divide by Events * total by 10. #Continue On with this #Block 2 - Lookup coach Dict and apply value. #Obtain Surname of Coach #Lookup Coach File and Match Name and get value. #Blocks continue gaining and calculating values. #create txt file named using date #Sum Values Block1 + Block2 etc #Print to file event number and field with name number individual Block totals and Sum Total #Arranged in descending Sum Total. #Prompt are there any more events? Yes return to start #Apphend all additional events to same day file seperated by blank line. How many of these steps have you attempted actually coding? Seems to me your first two steps are just string manipulation, and you only need to use the datetime module if you need to validate. In other words, if the user specifies the date as 31/09/2009, you might want to later bounce back to him with a complaint that September only has 30 days. So the first task is to accept input in the form ab/cd/efgh and produce a string efgh-cd-ab.log which you will then create as a text file. And if the file exists, you'll append to it instead of overwriting it. Can you do that much? Make sure each of these is in a function with a good name, so you can later refine the behavior, like adding error checking. So right now, you might code the function without error checking, and later add some validation, with error messages and new input query. DaveA -- http://mail.python.org/mailman/listinfo/python-list
Re: Finding application data after install - a solution?
On Thu, Sep 24, 2009 at 12:51 +0100, Tim Golden wrote: Wolodja Wentland wrote: Is CSIDL_COMMON_APPDATA and environment variable set on all Windows flavours? Certainly all those which Python currently supports. There are some small subtleties which have changed between older and newer versions. Great! Where can I find Information on these other standard folders? Sorry, but I have not used a Windows machine in ages. http://msdn.microsoft.com/en-us/library/bb762494%28VS.85%29.aspx Thank you! I will definitely consider adding better Windows installation support in one of the next releases. so long Wolodja signature.asc Description: Digital signature -- http://mail.python.org/mailman/listinfo/python-list
Re: Solved - Python: automate input to MySQL query
Hi, ... code, even that is not needed, as the columns are returned in the order specified so code /knows/ that the first column isname from /this/ table and the other column withname is from /that/ table). Unless you get a dictionary return. In any case, I have a strong philosophical objection to using the same name to refer to two different things regardless of any operational issues. The manager.firstname and employee.firstname are not the same thing and should have different names to reflect that. This seems to be a shortcome in your datamodel, in fact I'd see a firstname of an employee as the same type of information as the firstname of a manager. So what you really would have is a table with all the people and another which declares who is manager of who. recommended of course is to name different data differently and also to keep the name of the foreign keys the same on every table so you can use: SELECT a,b,c FROM tablea JOIN tableb USING (ab_id) since this avoids duplicate columns in the result. A similar issue comes up in the classic situation of the inventory table price field and the sale item price field. They sound like the same thing but they are not. One is the current price in the catalogue and the other is the price it was sold for at a particular time. They need different names for that reason. Yes and you should name them different in your query when they appear in the same result set. Regards Tino smime.p7s Description: S/MIME Cryptographic Signature -- http://mail.python.org/mailman/listinfo/python-list
Re: Date using input
On Sep 24, 10:58 pm, Dave Angel da...@ieee.org wrote: flebber.c...@gmail.com wrote: I am using python 2.6.2, I haven't updated to 3.0 yet. No I have no class or instructor, I am learning this myself. I have Hetlands book Beginning Python Novice to Professional and online documentation books so Dive into Python, python.org etc. Using the SPE editor. I have currently only fully written basic psuedocode to give me a basic framework to guide myself. #Basic pseudocode #Purpose to get raw input and calculate a score for a field of options and return that #score in a set in descending order. #Multiple sets sould be written to the doc #Obtain date #Check if txt file with same date exists. If yes apphend to results to file. #Obtain location #Set Dictionary #Event number #Obtain set size #Prompt first entry #First Entry Number #First Entry Name #Set Blocks to obtain and calculate data #Block 1 example - Placings Block #Obtain number of events competed in #Obtain how many times finished first #Ensure this value is not greater than Number of Events #Number of Firsts divide by Events * total by 15. #Obtain Second finishes #Ensure this value is not greater than Number of Events #Number of Seconds divide by Events * total by 10. #Continue On with this #Block 2 - Lookup coach Dict and apply value. #Obtain Surname of Coach #Lookup Coach File and Match Name and get value. #Blocks continue gaining and calculating values. #create txt file named using date #Sum Values Block1 + Block2 etc #Print to file event number and field with name number individual Block totals and Sum Total #Arranged in descending Sum Total. #Prompt are there any more events? Yes return to start #Apphend all additional events to same day file seperated by blank line. How many of these steps have you attempted actually coding? Seems to me your first two steps are just string manipulation, and you only need to use the datetime module if you need to validate. In other words, if the user specifies the date as 31/09/2009, you might want to later bounce back to him with a complaint that September only has 30 days. So the first task is to accept input in the form ab/cd/efgh and produce a string efgh-cd-ab.log which you will then create as a text file. And if the file exists, you'll append to it instead of overwriting it. Can you do that much? DaveA Trying but haven't got it working, thats why I started to try and use datetime module. -- http://mail.python.org/mailman/listinfo/python-list
Re: Finding application data after install - a solution?
Wolodja Wentland wrote: On Thu, Sep 24, 2009 at 12:51 +0100, Tim Golden wrote: Wolodja Wentland wrote: Is CSIDL_COMMON_APPDATA and environment variable set on all Windows flavours? Just to clarify, now that I read your post more carefully, there *is* an environment variable APPDATA which is the user-specific Application Data (probably what you want). If you need the common appdata (as in the question above), this isn't exposed as an env var and you'll need to use the shell API to query Windows for it. Be warned: altho' it might appear that there is a common pattern to these locations, they're not guaranteed, especially in the face of i18n issues and XP-Vista changes etc. TJG -- http://mail.python.org/mailman/listinfo/python-list
Re: Solved - Python: automate input to MySQL query
On Thu, 24 Sep 2009 08:49:02 -0400 Joe Riopel goo...@gmail.com wrote: On Wed, Sep 23, 2009 at 4:21 PM, D'Arcy J.M. Cain da...@druid.net wrote: In any case, I have a strong philosophical objection to using the same name to refer to two different things regardless of any operational issues. The manager.firstname and employee.firstname are not the same thing and should have different names to reflect that. They are in separate tables, that should be enough to reflect the fact that they're not the same thing. But manager_id (to join the employee to their manager) is in both tables and it does refer to the same thing. A similar issue comes up in the classic situation of the inventory table price field and the sale item price field. They sound like the same thing but they are not. One is the current price in the catalogue and the other is the price it was sold for at a particular time. They need different names for that reason. I am not sure if you're talking about two different tables (inventory and sale?). If you meant a single table; I would hope you don't have two columns, with the same name, in a single table. If you were talking about two different tables, see my above comment. I'm talking about two different tables. Consider an inventory table that includes the current price of an item. The invoice line item table references the inventory table for details of the item. A classic mistake is to use the price from the inventory table to determine the total price (item.quantity * invent.price). The problem is that the price may change tomorrow but the price it was sold for yesterday does not. Hence, invent.price and item.price are not the same thing and I would name one of them differently. -- D'Arcy J.M. Cain da...@druid.net | Democracy is three wolves http://www.druid.net/darcy/| and a sheep voting on +1 416 425 1212 (DoD#0082)(eNTP) | what's for dinner. -- http://mail.python.org/mailman/listinfo/python-list
Looger object only prints ERRORs
hi everybody, I took your adviced and used the logging object. I copied the example in 16.6.15.2 - using logging in multiple modules from http://docs.activestate.com/activepython/3.1/python/library/logging.html. unfortunattly it only prints to file the ERROR level's messages and ignore the others. I tried to change the level with SETLEVEL but it didn't help. Any idea? thanks, DaveD -- http://mail.python.org/mailman/listinfo/python-list
Re: easy question, how to double a variable
On Sep 24, 5:51 am, Iain King iaink...@gmail.com wrote: On Sep 23, 7:36 pm, David C Ullrich dullr...@sprynet.com wrote: On Tue, 22 Sep 2009 02:34:53 +, Steven D'Aprano wrote: On Mon, 21 Sep 2009 13:50:23 -0500, David C Ullrich wrote: But you actually want to return twice the value. I don't see how to do that. What? Seriously? You're saying it _can_ be done in Python? They must have added something to the standard library again. I mean how can you return twice a value without a twice function to start with? I've tried. You'd think def twice(n): return twice(n) would work, but I get this really long error message. You're not just yanking the OP's chain??? That would be cruel. I mean the guy has enough problems already... Sorry, there is no 'twice' builtin. I think what you are looking for is: def twice(n): return return n Iain Actually, what he wants is: (def twice (x) (+ (once x) (once x))) -- http://mail.python.org/mailman/listinfo/python-list
Re: Solved - Python: automate input to MySQL query
On Thu, 24 Sep 2009 15:03:26 +0200 Tino Wildenhain t...@wildenhain.de wrote: In any case, I have a strong philosophical objection to using the same name to refer to two different things regardless of any operational issues. The manager.firstname and employee.firstname are not the same thing and should have different names to reflect that. This seems to be a shortcome in your datamodel, in fact I'd see a firstname of an employee as the same type of information as the firstname of a manager. So what you really would have is a table with all the people and another which declares who is manager of who. Sure, I was trying to show a hypothetical situation. In my real databases I don't do that and in fact I point the manager_id field to another entry in the same table. Obviously in that case I am forced to make it a different name even though it references the same column as employee_id. recommended of course is to name different data differently and also to keep the name of the foreign keys the same on every table so you can use: SELECT a,b,c FROM tablea JOIN tableb USING (ab_id) since this avoids duplicate columns in the result. Exactly. A similar issue comes up in the classic situation of the inventory table price field and the sale item price field. They sound like the same thing but they are not. One is the current price in the catalogue and the other is the price it was sold for at a particular time. They need different names for that reason. Yes and you should name them different in your query when they appear in the same result set. I want to name them differently even if they aren't in the same result set because they are different things that just happen to have the same type - money. I wouldn't name every int column int and I wouldn't name every price column price. -- D'Arcy J.M. Cain da...@druid.net | Democracy is three wolves http://www.druid.net/darcy/| and a sheep voting on +1 416 425 1212 (DoD#0082)(eNTP) | what's for dinner. -- http://mail.python.org/mailman/listinfo/python-list
Re: Most active coroutine library project?
Grant Edwards invalid at invalid.invalid writes: Back when I worked on one of the first hand-held cellular mobile phones, it used co-routines where the number of coroutines was fixed at 2 (one for each register set in a Z80 CPU). Gotta love the lightning-fast EXX instruction. :-) Regards Antoine. -- http://mail.python.org/mailman/listinfo/python-list
Re: Looger object only prints ERRORs
daved170 wrote: hi everybody, I took your adviced and used the logging object. I copied the example in 16.6.15.2 - using logging in multiple modules from http://docs.activestate.com/activepython/3.1/python/library/logging.html. unfortunattly it only prints to file the ERROR level's messages and ignore the others. I tried to change the level with SETLEVEL but it didn't help. Any idea? thanks, DaveD Diffcult to say without a bunch of code. I would say that your FileHandler formatter has something wrong. Please provide the code where you're adding this handler to you logger object. Try to change the format to %(asctime)s - %(levelname)s - %(message)s for testing purpose and tell us if you still have issues. /logging/ is the way to go, keep going :o) Jean-Michel -- http://mail.python.org/mailman/listinfo/python-list
Re: Looger object only prints ERRORs
Jean-Michel Pichavant wrote: daved170 wrote: hi everybody, I took your adviced and used the logging object. I copied the example in 16.6.15.2 - using logging in multiple modules from http://docs.activestate.com/activepython/3.1/python/library/logging.html. unfortunattly it only prints to file the ERROR level's messages and ignore the others. I tried to change the level with SETLEVEL but it didn't help. Any idea? thanks, DaveD Diffcult to say without a bunch of code. I would say that your FileHandler formatter has something wrong. Please provide the code where you're adding this handler to you logger object. Try to change the format to %(asctime)s - %(levelname)s - %(message)s for testing purpose and tell us if you still have issues. /logging/ is the way to go, keep going :o) Jean-Michel Ignore my post, I just misread your description. Verify you don't have filters attached to your handlers. If you have a hierarchy of loggers, make sure their level are all correctly set For instance if you have a 'foo.bar' logger, you may set 'foo.bar' level to DEBUG, logs can still be filtered by 'foo' if this logger level is still ERROR. JM -- http://mail.python.org/mailman/listinfo/python-list
Regex trouble
I am trying to loop over a dictionary of phone numbers and using a python regex to determine if they are long distance or local and then adding them to their appropriate dictionary, My regex doesn't appear to be working though. My regex's are these international__iregex=r'^1?(011|001)' local__iregex=r'^1?(281|832|713|800)' #long distance ld_regex=r'^1?(281|832|713|800|866|877|011|001|888)' long_distance= {} My loop: for key1,value1 in x.items(): if key1 == 'dest': if re.search(ld_regex,value1): long_distance[key1] = value1 print long_distance -- http://mail.python.org/mailman/listinfo/python-list
Re: Date using input
On Sep 24, 11:10 pm, flebber flebber.c...@gmail.com wrote: On Sep 24, 10:58 pm, Dave Angel da...@ieee.org wrote: flebber.c...@gmail.com wrote: I am using python 2.6.2, I haven't updated to 3.0 yet. No I have no class or instructor, I am learning this myself. I have Hetlands book Beginning Python Novice to Professional and online documentation books so Dive into Python, python.org etc. Using the SPE editor. I have currently only fully written basic psuedocode to give me a basic framework to guide myself. #Basic pseudocode #Purpose to get raw input and calculate a score for a field of options and return that #score in a set in descending order. #Multiple sets sould be written to the doc #Obtain date #Check if txt file with same date exists. If yes apphend to results to file. #Obtain location #Set Dictionary #Event number #Obtain set size #Prompt first entry #First Entry Number #First Entry Name #Set Blocks to obtain and calculate data #Block 1 example - Placings Block #Obtain number of events competed in #Obtain how many times finished first #Ensure this value is not greater than Number of Events #Number of Firsts divide by Events * total by 15. #Obtain Second finishes #Ensure this value is not greater than Number of Events #Number of Seconds divide by Events * total by 10. #Continue On with this #Block 2 - Lookup coach Dict and apply value. #Obtain Surname of Coach #Lookup Coach File and Match Name and get value. #Blocks continue gaining and calculating values. #create txt file named using date #Sum Values Block1 + Block2 etc #Print to file event number and field with name number individual Block totals and Sum Total #Arranged in descending Sum Total. #Prompt are there any more events? Yes return to start #Apphend all additional events to same day file seperated by blank line. How many of these steps have you attempted actually coding? Seems to me your first two steps are just string manipulation, and you only need to use the datetime module if you need to validate. In other words, if the user specifies the date as 31/09/2009, you might want to later bounce back to him with a complaint that September only has 30 days. So the first task is to accept input in the form ab/cd/efgh and produce a string efgh-cd-ab.log which you will then create as a text file. And if the file exists, you'll append to it instead of overwriting it. Can you do that much? DaveA Trying but haven't got it working, thats why I started to try and use datetime module. Surely getting it tottally mixed up from datetime import date def ObtainDate(params): date = raw_input(Type Date dd/mm/year: %2.0r%2.0r/%2.0r%2.0r/%4.0r %4.0r%4.0r%4.0r) print date.datetime(year-month-day) #Check if txt file with same date exists. If yes apphend to results to file. date.append(datetime and def ObtainDate(params): date = raw_input(Type Date dd/mm/year: ) date.format = (%4.0s%4.0s%4.0s%4.0s-%2.0s%2.0s-%2.0s) print date.format -- http://mail.python.org/mailman/listinfo/python-list
Regex trouble
I am trying to loop over a dictionary of phone numbers and using a python regex to determine if they are long distance or local and then adding them to their appropriate dictionary, My regex doesn't appear to be working though. My regex's are these international__iregex=r'^1?(011|001)' #Anything starting with these prefixes is International local__iregex=r'^1?(281|832|713|800)' #anything starting with these are local #long distance ld_regex=r'^1?(281|832|713|800|866|877|011|001|888)' #any number not starting with these prefixes is long distance long_distance= {} My loop: for key1,value1 in x.items(): if key1 == 'dest': if not re.search(ld_regex,value1): long_distance[key1] = value1 print long_distance -- http://mail.python.org/mailman/listinfo/python-list
Re: Dynamic Form
in line... On Wed, Sep 23, 2009 at 9:13 PM, BJ Swope bigbluesw...@gmail.com wrote: What is your code not doing? see below Are you winding up with duplicate data in the DB? yes Is your web browser re-submitting the form with the same data if you refresh the screen? yes Is your web browser pre-filling the fields of the form due to caching? no If I must use cookies, do I want cookielib/cookiejar, or just Cookie? TIA. V I don't understand what's not working On Wed, Sep 23, 2009 at 12:40 PM, Victor Subervi victorsube...@gmail.comwrote: I've been trying the hidden field, but the problem is that when I set the variable flag, it stays in memory. I would rather just pass a var like I've been trying, but I don't think it's possible. Any ideas? Is a session cookie the only way? Here's more simplified code: #!/usr/bin/python import cgitb; cgitb.enable() import cgi import sys,os sys.path.append(os.getcwd()) import MySQLdb import string, re def client(): form = cgi.FieldStorage() client = string.replace(string.replace(form.getfirst('client', ''), ', '#39;'), '', '#34;') flag = form.getfirst('flag', '') print Content-Type: text/html print print !DOCTYPE html PUBLIC -//W3C//DTD XHTML 1.0 Frameset//EN http://www.w3.org/TR/xhtml1/DTD/xhtml1-frameset.dtd; head xmlns=http://www.w3.org/1999/xhtml; body if flag == '': print form method=post action=client.py Company Name: input type='text' value='' size='20' maxlength='100' name='client' /br / input type='hidden' name='flag' value='y' / div align='center' input type='submit' value=' Send ' / /div /form /body/html else: host = 'h' db = 'db' user = 'u' passwd = '1' database = MySQLdb.connect(host, user, passwd, db) cursor = database.cursor() cursor.execute('insert into companies (client);' % (client)) cursor.close() print '/body/html' client() TIA, V On Tue, Sep 22, 2009 at 1:43 PM, Dennis Lee Bieber wlfr...@ix.netcom.com wrote: On Tue, 22 Sep 2009 12:50:31 -0400, Victor Subervi victorsube...@gmail.com declaimed the following in gmane.comp.python.general: Well it's Web stuff, sure, but it's written in python :) The code follows. The problem is that I haven't figured out how to tell the program that the user has entered data and to clear the cache of that data so that it's not re-entered. How do I do that? Remember, HTTP is a stateless protocol. EACH submission is considered a totally new transaction with no memory of the previous processing. Possible solutions... * Use a session cookie that identifies what phase in the multistep processing you are in... * Use a hidden field in the form that defaults to, say False, when you first display the form, but then gets set to True during the first response process (and is then sent back out with True so the second response takes a different branch). -- Wulfraed Dennis Lee Bieber KD6MOG wlfr...@ix.netcom.com HTTP://wlfraed.home.netcom.com/http://wlfraed.home.netcom.com/ -- http://mail.python.org/mailman/listinfo/python-list -- http://mail.python.org/mailman/listinfo/python-list -- To argue that honorable conduct is only required against an honorable enemy degrades the Americans who must carry out the orders. -- Charles Krulak, Former Commandant of the Marine Corps We are all slave to our own paradigm. -- Joshua Williams If the letters PhD appear after a person's name, that person will remain outdoors even after it's started raining. -- Jeff Kay -- http://mail.python.org/mailman/listinfo/python-list -- http://mail.python.org/mailman/listinfo/python-list
Re: Date using input
Surely getting it tottally mixed up from datetime import date def ObtainDate(params): date = raw_input(Type Date dd/mm/year: %2.0r%2.0r/%2.0r%2.0r/%4.0r %4.0r%4.0r%4.0r) print date.datetime(year-month-day) By setting date = raw_input(...), you mask the datetime.date object preventing you from using it in the next print line. Additionally, the - aren't used to separate parameters...you want commas. You also haven't split out the year/month/day bits to pass to the date.datetime() constructor. So immediate corrections involve: 1) choose a name other than date for the value returned from raw_input() 2) take that resulting value from step #1 and split it up so you have the constituent parts. This is a wonderful use for tuple assignment. 3) After splitting parts up, you still have strings, so you need to convert them to numbers. (for advanced users, I'd use map() to do the conversion in step #2) 4) once you have the year, month, and day values as integers, you can pass them to the datetime.date constructor (instead of the datetime.date.datetime constructor which is a little weird). #Check if txt file with same date exists. If yes apphend to results to file. date.append(datetime You'd then want to create a file-name to open, based on the date object. The strftime() method will help you here (see the docs on the format string). Once you have the filename, you'll want to open a file with that name, appending to it if it already exists (see the docs on the file() object) -tkc -- http://mail.python.org/mailman/listinfo/python-list
Re: logging.handlers.SMTPHandler question
On Sep 24, 4:14 am, akonsu ako...@gmail.com wrote: hello, SMTPHAndler seems to email every single record separately. is there a way to collect all log output and then send it in a single email message? or do i have to do it manually? thanks konstantin See also http://pypi.python.org/pypi/mailinglogger -- http://mail.python.org/mailman/listinfo/python-list
setting up dynamic descriptors in Python
Hello, I want to set up descriptors at runtine, but it isn't working the way i would expect. Does anybody know if this is possible? Here is an example: class Descriptor(object): def __init__(self, name) : self.val=0 self.name = name def __get__(self, obj, objtype): print 'Retrieving', self.name return self.val def __set__(self, obj, val): print 'Updating' , self.name self.val = val class TestDesc (object): x=Descriptor (x) def __init__ (self): self.y=Descriptor(y) And Usage: test=TestDesc() test.x=0 Updating x test.x=4 Updating x test.x Retrieving x 4 test.y descriptor.Descriptor object at 0x00B3A5F0 When i access y, it appears to be a regular object and not a descriptor type object like x. Is there a way to make y work the same as x but setting it up during object creation? thanks, brian -- http://mail.python.org/mailman/listinfo/python-list
Re: Most active coroutine library project?
On Aug 23, 5:02 pm, Phillip B Oldham phillip.old...@gmail.com wrote: I've been taking a look at the multitude of coroutine libraries available for Python, but from the looks of the projects they all seem to be rather quiet. I'd like to pick one up to use on a current project but can't deduce which is the most popular/has the largest community. Libraries I looked at include: cogen, weightless, eventlet and circuits (which isn't exactly coroutine-based but it's event-driven model was intriguing). Firstly, are there any others I've missed? And what would the consensus be on the which has the most active community behind it? The newest one seems to be diesel, by Simon Willison Co: http://dieselweb.org/lib -- http://mail.python.org/mailman/listinfo/python-list
Re: Regex trouble
Support Desk wrote: I am trying to loop over a dictionary of phone numbers and using a python regex to determine if they are long distance or local and then adding them to their appropriate dictionary, My regex doesn't appear to be working though. My regex's are these international__iregex=r'^1?(011|001)' local__iregex=r'^1?(281|832|713|800)' #long distance ld_regex=r'^1?(281|832|713|800|866|877|011|001|888)' long_distance= {} My loop: for key1,value1 in x.items(): if key1 == 'dest': if re.search(ld_regex,value1): long_distance[key1] = value1 print long_distance Define not working. BTW, x.items() will give the key/value pairs, but key1 == 'dest' will be true for only one of the keys, so you're really only looking at x['dest']. Please provide a simple though complete example showing the problem, stating what you expected and what you actually got, including any tracebacks if an exception occurred. -- http://mail.python.org/mailman/listinfo/python-list
Re: IDE for python similar to visual basic
You can also try Eclipse + PyDev. It's not the same as Visual Studio, and I am not sure about the GUI builder, but I think it's what you want. I really like Eclipse + Pydev. It is not a GUI builder at all but it has a nice debugger, code completion and that kind of thing. And its free! Brian -- http://mail.python.org/mailman/listinfo/python-list
Re: Dynamic Form
1. Did you try the headers for no-caching of the page? 2. If you wish to avoid dupes in a DB, Primary Keys are the tool to prevent duplicates. On Thu, Sep 24, 2009 at 11:01 AM, victorsubervi victorsube...@gmail.comwrote: in line... On Wed, Sep 23, 2009 at 9:13 PM, BJ Swope bigbluesw...@gmail.com wrote: What is your code not doing? see below Are you winding up with duplicate data in the DB? yes Is your web browser re-submitting the form with the same data if you refresh the screen? yes Is your web browser pre-filling the fields of the form due to caching? no If I must use cookies, do I want cookielib/cookiejar, or just Cookie? TIA. V I don't understand what's not working On Wed, Sep 23, 2009 at 12:40 PM, Victor Subervi victorsube...@gmail.com wrote: I've been trying the hidden field, but the problem is that when I set the variable flag, it stays in memory. I would rather just pass a var like I've been trying, but I don't think it's possible. Any ideas? Is a session cookie the only way? Here's more simplified code: #!/usr/bin/python import cgitb; cgitb.enable() import cgi import sys,os sys.path.append(os.getcwd()) import MySQLdb import string, re def client(): form = cgi.FieldStorage() client = string.replace(string.replace(form.getfirst('client', ''), ', '#39;'), '', '#34;') flag = form.getfirst('flag', '') print Content-Type: text/html print print !DOCTYPE html PUBLIC -//W3C//DTD XHTML 1.0 Frameset//EN http://www.w3.org/TR/xhtml1/DTD/xhtml1-frameset.dtd; head xmlns=http://www.w3.org/1999/xhtml; body if flag == '': print form method=post action=client.py Company Name: input type='text' value='' size='20' maxlength='100' name='client' /br / input type='hidden' name='flag' value='y' / div align='center' input type='submit' value=' Send ' / /div /form /body/html else: host = 'h' db = 'db' user = 'u' passwd = '1' database = MySQLdb.connect(host, user, passwd, db) cursor = database.cursor() cursor.execute('insert into companies (client);' % (client)) cursor.close() print '/body/html' client() TIA, V On Tue, Sep 22, 2009 at 1:43 PM, Dennis Lee Bieber wlfr...@ix.netcom.com wrote: On Tue, 22 Sep 2009 12:50:31 -0400, Victor Subervi victorsube...@gmail.com declaimed the following in gmane.comp.python.general: Well it's Web stuff, sure, but it's written in python :) The code follows. The problem is that I haven't figured out how to tell the program that the user has entered data and to clear the cache of that data so that it's not re-entered. How do I do that? Remember, HTTP is a stateless protocol. EACH submission is considered a totally new transaction with no memory of the previous processing. Possible solutions... * Use a session cookie that identifies what phase in the multistep processing you are in... * Use a hidden field in the form that defaults to, say False, when you first display the form, but then gets set to True during the first response process (and is then sent back out with True so the second response takes a different branch). -- Wulfraed Dennis Lee Bieber KD6MOG wlfr...@ix.netcom.com HTTP://wlfraed.home.netcom.com/http://wlfraed.home.netcom.com/ -- http://mail.python.org/mailman/listinfo/python-list -- http://mail.python.org/mailman/listinfo/python-list -- To argue that honorable conduct is only required against an honorable enemy degrades the Americans who must carry out the orders. -- Charles Krulak, Former Commandant of the Marine Corps We are all slave to our own paradigm. -- Joshua Williams If the letters PhD appear after a person's name, that person will remain outdoors even after it's started raining. -- Jeff Kay -- http://mail.python.org/mailman/listinfo/python-list -- To argue that honorable conduct is only required against an honorable enemy degrades the Americans who must carry out the orders. -- Charles Krulak, Former Commandant of the Marine Corps We are all slave to our own paradigm. -- Joshua Williams If the letters PhD appear after a person's name, that person will remain outdoors even after it's started raining. -- Jeff Kay -- http://mail.python.org/mailman/listinfo/python-list
Re: custom data warehouse in python vs. out-of-the-box ETL tool
Hi, Marc-Andre - well, so far you seem to be the only one suggesting that cross-database joins is the way to go - everyone else has been telling me to build a warehouse. I initially was trying to avoid the warehouse idea to avoid going through the external temporary resource, as you say. But then the situation came up where the data warehouse would give me another benefit by enabling power users with tools like OOBase to do their own reporting. So I started to re- consider it, and since then, no one has advised the direct approach with connections/joins on multiple databases. Have you had much luck circumventing the need for a data warehouse in this way? Thanks. On Sep 24, 4:10 am, M.-A. Lemburg m...@egenix.com wrote: snfctech wrote: @Lemburg: Thanks for the suggestion. I'm sure you make a fine product, but my development platform is Linux, and I don't want any additional Windows servers to deal with (than the ones I'm already stuck with.) Strange, EasySoft used to support their product on Linux as well... http://download.freshmeat.net/projects/easysoftsqlengine Looks like they stopped with supporting it on Linux and now only have Windows downloads available. mxODBC and mxODBC Connect work just fine on Linux, so you could use them to build a custom joining solution in Python. It really depends on whether you need to run those joins on an on-demand basis or not: AFAIK, Talend can only run jobs which then apply the JOINs and put the resulting data somewhere, e.g. into a file or another database table. With Python, you could do the join straight away and use the data directly without the Python script - without having to go through an external temporary resource. Anyway, just a suggestion. On Sep 23, 2:02 am, M.-A. Lemburg m...@egenix.com wrote: snfctech wrote: Does anyone have experience building a datawarehousein python? Any thoughts on custom vs using an out-of-the-box product like Talend or Informatica? I have an integrated system Dashboard project that I was going to build using cross-vendor joins on existing DBs, but I keep hearing that a datawarehouseis the way to go. e.g. I want to create orders and order_items with relations to members (MS Access DB), products (flat file) and employees (MySQL). Thanks in advance for any tips. You might want to look at this solution for doing cross-database JOINs: EasySoft ODBC Join-Engine: http://www.easysoft.com/products/data_access/odbc_odbc_join_engine/in... and then use our mxODBC to access EasySoft's Engine: http://www.egenix.com/products/python/mxODBC/ or mxODBC Connect, if you have a client-server setup: http://www.egenix.com/products/python/mxODBCConnect/ Some database engines also allow integrating external ODBC data sources - the external tables then look like normal database tables and can be used in JOINs as well. I know that DB2 and Oracle support this kind of setup. You can access those using mxODBC as well. -- Marc-Andre Lemburg eGenix.com Professional Python Services directly from the Source (#1, Sep 23 2009) Python/Zope Consulting and Support ... http://www.egenix.com/ mxODBC.Zope.Database.Adapter ... http://zope.egenix.com/ mxODBC, mxDateTime, mxTextTools ... http://python.egenix.com/ ::: Try our new mxODBC.Connect Python Database Interface for free ! eGenix.com Software, Skills and Services GmbH Pastor-Loeh-Str.48 D-40764 Langenfeld, Germany. CEO Dipl.-Math. Marc-Andre Lemburg Registered at Amtsgericht Duesseldorf: HRB 46611 http://www.egenix.com/company/contact/ -- Marc-Andre Lemburg eGenix.com Professional Python Services directly from the Source (#1, Sep 24 2009) Python/Zope Consulting and Support ... http://www.egenix.com/ mxODBC.Zope.Database.Adapter ... http://zope.egenix.com/ mxODBC, mxDateTime, mxTextTools ... http://python.egenix.com/ ::: Try our new mxODBC.Connect Python Database Interface for free ! eGenix.com Software, Skills and Services GmbH Pastor-Loeh-Str.48 D-40764 Langenfeld, Germany. CEO Dipl.-Math. Marc-Andre Lemburg Registered at Amtsgericht Duesseldorf: HRB 46611 http://www.egenix.com/company/contact/ -- http://mail.python.org/mailman/listinfo/python-list
Re: Dynamic Form
victorsubervi wrote: On Wed, Sep 23, 2009 at 9:13 PM, BJ Swope bigbluesw...@gmail.com wrote: Is your web browser re-submitting the form with the same data if you refresh the screen? yes I'm surprised no one has mentioned this before, but the standard approach to this problem is to redirect after a successful POST. That way refreshing the browser won't try to re-POST the form. http://en.wikipedia.org/wiki/Post/Redirect/Get -- Brian -- http://mail.python.org/mailman/listinfo/python-list
Re: Twisted: 1 thread in the reactor pattern
On 07:10 am, jacopo.pe...@gmail.com wrote: On Sep 23, 5:57�pm, exar...@twistedmatrix.com wrote: [snip] It isn't possible. �While the remote methods are running, other events are not being serviced. �This is what is meant when people describe Twisted as a *cooperative* multitasking system. Event handlers (such as remote methods) run for a short enough period of time that it doesn't matter that the reactor is prevented from accepting new connections (or what have you) for the duration of their execution. Jean-Paul Jean -Paul, not sure I have understood. Say I have one server S and two clients C1 and C2 (all on separate machines). (a) C1 requests a remote call of f1() to S, f1() requires 5 minutes of processing. (b) S puts f1() in a queue and returns immediately a Deferred to C1. (c) Now f1() starts and keeps S 19s processor busy for 5 mins (d) after few seconds C2 requests a remote call f2() to S. (e) On S the processor is already engaged with f1() but still 1Csomeone 1D on S is able to accept the request from C2, put it in a queue (after f1()) and return a Deferred to C2. (f) At some point after f1() is finished f2() will start I believe (b) is what you say 1Crun for a short enough period of time that it doesn't matter that the reactor is prevented from accepting new connections (or what have you) for the duration of their execution. 1D ?! If you have a function that takes 5 minutes to run, then you're blocking the reactor thread for 5 minutes and no other events are serviced until the function finishes running. You have to avoid blocking the reactor thread if you want other events to continue to be serviced. There are various strategies for avoiding blocking. Different strategies are appropriate for different kinds of blocking code. Jean-Paul -- http://mail.python.org/mailman/listinfo/python-list
Doubley imported module caused devastating bug
Currently it is possible to import a file of one path to more than one 'instance' of a module. One notable example is import __init__ from a package. See http://stackoverflow.com/questions/436497/python-import-the-containing-package This recently caused a devastating bug in some of my code. What I have is support for the Perforce global options as a context for a perforce module. http://www.perforce.com/perforce/doc.072/manuals/cmdref/o.gopts.html#1040647 This way one can call functions that call many perforce command and have them execute on a different client for example. So, in module A and module B both imported the Perforce module, but they turned out not to be the same module. Module A did with Perforce.GlobalOptions(client=client): B.function() B.function did not receive the new GlobalOptions because of this problem. As a result important files on the original client were overwritten (OUCH). I would like to propose that it be made impossible in the Python source to import two instances of the same module. -- Zachary Burns (407)590-4814 Aim - Zac256FL Production Engineer (Digital Overlord) Zindagi Games -- http://mail.python.org/mailman/listinfo/python-list
Re: Date using input
flebber wrote: On Sep 24, 11:10 pm, flebber flebber.c...@gmail.com wrote: On Sep 24, 10:58 pm, Dave Angel da...@ieee.org wrote: flebber.c...@gmail.com wrote: I am using python 2.6.2, I haven't updated to 3.0 yet. No I have no class or instructor, I am learning this myself. I have Hetlands book Beginning Python Novice to Professional and online documentation books so Dive into Python, python.org etc. Using the SPE editor. I have currently only fully written basic psuedocode to give me a basic framework to guide myself. #Basic pseudocode #Purpose to get raw input and calculate a score for a field of options and return that #score in a set in descending order. #Multiple sets sould be written to the doc #Obtain date #Check if txt file with same date exists. If yes apphend to results to file. #Obtain location #Set Dictionary #Event number #Obtain set size #Prompt first entry #First Entry Number #First Entry Name #Set Blocks to obtain and calculate data #Block 1 example - Placings Block #Obtain number of events competed in #Obtain how many times finished first #Ensure this value is not greater than Number of Events #Number of Firsts divide by Events * total by 15. #Obtain Second finishes #Ensure this value is not greater than Number of Events #Number of Seconds divide by Events * total by 10. #Continue On with this #Block 2 - Lookup coach Dict and apply value. #Obtain Surname of Coach #Lookup Coach File and Match Name and get value. #Blocks continue gaining and calculating values. #create txt file named using date #Sum Values Block1 + Block2 etc #Print to file event number and field with name number individual Block totals and Sum Total #Arranged in descending Sum Total. #Prompt are there any more events? Yes return to start #Apphend all additional events to same day file seperated by blank line. How many of these steps have you attempted actually coding? Seems to me your first two steps are just string manipulation, and you only need to use the datetime module if you need to validate. In other words, if the user specifies the date as 31/09/2009, you might want to later bounce back to him with a complaint that September only has 30 days. So the first task is to accept input in the form ab/cd/efgh and produce a string efgh-cd-ab.log which you will then create as a text file. And if the file exists, you'll append to it instead of overwriting it. Can you do that much? DaveA Trying but haven't got it working, thats why I started to try and use datetime module. Surely getting it tottally mixed up from datetime import date def ObtainDate(params): date =aw_input(Type Date dd/mm/year: %2.0r%2.0r/%2.0r%2.0r/%4.0r %4.0r%4.0r%4.0r) print date.datetime(year-month-day) #Check if txt file with same date exists. If yes apphend to results to file. date.append(datetime and def ObtainDate(params): date =aw_input(Type Date dd/mm/year: ) date.format =%4.0s%4.0s%4.0s%4.0s-%2.0s%2.0s-%2.0s) print date.format As Tim says, first thing you want to do is rename that variable. You've defined two symbols with the same name. Then I'd ask what that %2.0r stuff is inside the prompt to the user. Do you understand what kind of data is returned by raw_input() ? If so, look at the available methods of that type, and see if there's one called split() that you can use to separate out the multiple parts of the user's response. You want to separate the dd from the mm and from the year. Once you've split the text, then you want to recombine it in a different order, and with dashes between the parts. If I were at your stage of experience, I'd not bother with the datetime module at all. Just see if you can rebuild the string you need, assuming the user has entered a valid 10-character string. Later you can go back and figure out the datetime logic. DaveA -- http://mail.python.org/mailman/listinfo/python-list
test_sys failed
On a new python 3 installation on a fedora 7 box, I get a test_sys failure. Here is the console spew. Couldn't find any discussion about this. Suspect I have fouled up but I don' t know how to proceed. Ideas? 853 test_sys 854 test test_sys failed -- Traceback (most recent call last): 855 File /home/eno/Python-3.1.1/Lib/test/test_sys.py, line 379, in test_43581 856 self.assertEqual(sys.__stdout__.encoding, sys.__stderr__.encoding) 857 AssertionError: 'ascii' != 'UTF-8' -- http://mail.python.org/mailman/listinfo/python-list
Re: Regex trouble
On Thu, Sep 24, 2009 at 10:43 AM, Support Desk m...@ipglobal.net wrote: I am trying to loop over a dictionary of phone numbers and using a python regex to determine if they are long distance or local and then adding them to their appropriate dictionary, My regex doesn't appear to be working though. doesn't appear to be working is a useless way to characterize the problem you're having. Always tell us what you expected and what you got instead. Preferably with the actual traceback if any. Also, post a /runnable/ code fragment. That way we can try it out for ourselves. FWIW this problem is too simple (IMHO) for regular expressions. Simply carve off the first three digits and check against sets of the prefixes you're interested in: #any number starting with these prefixes is not long distance local_prefixes = set(['832', '877', '888', '713', '866', '011', '001', '281', '800']) long_distance= {} for key1, value1 in x.iteritems(): if key1 == 'dest': if value1[:3] not in local_prefixes: long_distance[key1] = value1 HTH, ~Simon My regex's are these international__iregex=r'^1?(011|001)' #Anything starting with these prefixes is International local__iregex=r'^1?(281|832|713|800)' #anything starting with these are local #long distance ld_regex=r'^1?(281|832|713|800|866|877|011|001|888)' #any number not starting with these prefixes is long distance long_distance= {} My loop: for key1,value1 in x.items(): if key1 == 'dest': if not re.search(ld_regex,value1): long_distance[key1] = value1 print long_distance Out of curiosity, why are you printing the whole dict for every key1 == 'dest' even when you're not modifying the output dict? -- http://mail.python.org/mailman/listinfo/python-list -- http://mail.python.org/mailman/listinfo/python-list
Re: Python-URL! - weekly Python news and links (Sep 17)
In article h8tf2d$95...@lairds.us, Gabriel Genellina python-...@phaseit.net wrote (but I edited): . . . More ways to define an empty function that you ever imagined: http://groups.google.com/group/comp.lang.python/t/c9f494b6745c7d74/ . . . Oops; this should have been URL: http://groups.google.com/group/comp.lang.python/browse_thread/thread/eddc376716d f2029/ . My apology, all. -- http://mail.python.org/mailman/listinfo/python-list
Re: DBHandler class for logging?
In f8a64e65-d13c-410f-abf8-fa5fadcb9...@f10g2000vbf.googlegroups.com Vinay Sajip vinay_sa...@yahoo.co.uk writes: See my answer to a question on Stack Overflow, which has the source code for a simple handler which writes to a database using the Python DB-API 2.0: http://stackoverflow.com/questions/935930/creating-a-logging-handler-to-connect-to-oracle/1014450#1014450 Although the question relates to Oracle, my answer is not so specific. It's not production quality code but it should get you started. Many thanks, to both you and Diez. kynn -- http://mail.python.org/mailman/listinfo/python-list
Re: pycopg2 build problems
On Sep 24, 2:14 am, phi...@semanchuk.com wrote: Quoting Wolodja Wentland wentl...@cl.uni-heidelberg.de: On Wed, Sep 23, 2009 at 12:24 -0700, devaru wrote: I'm trying to install psycopg2 on my system. I followed the instruction in INSTALL file and gave the command python setup.py build running build running build_py running build_ext error: No such file or directory I ran into this some days ago. The problem is not related to the distribution you downloaded, but to missing information about PostgreSQL itself. IIRC the file in question is /usr/bin/pg_config. The file is probably packaged in some lib*-dev package on your distribution. That's the most common install problem with psycopg2 -- setup.py can't find (or execute) pg_config which it needs to decide how to talk to Postgres. I remember it giving a different (and more descriptive) error but I haven't used psycopg2 recently. To the OP: if executing pg_config fails from the same command line in which you're running setup.py, then Wolodja is absolutely correct. You need to get pg_config on your path somewhere, or there might be an environment variable you can set to tell setup where to find it if you don't want it in your path. Good luck Philip --- Debian example --- $ apt-file search /usr/bin/pg_config libpq-dev: /usr/bin/pg_config --- snip --- thanks for all the fish Wolodja Thank you all. Yes, as Philip and Wolodja have said, we need pg_config in the PATH variable for psycopg2 to build. Then I ran into a problem where I had to install the dependency package python-devel. Now psycopg2 installed fine. -- http://mail.python.org/mailman/listinfo/python-list
PExpect on Windows System Using Cygwin
Hello, I downloaded and installed the pexpect module and wrote a script. All is well and good, but the script proved to be pretty useful and now I was asked to run it as a scheduled task up on a server to run periodically. I was intending on simply packaging it up with Py2Exe and moving it to the server that way. However, when I went to test it, I received this error Traceback (most recent call last): File script.py, line 17, in module import pexpect File lib\site-packages\pexpect.py, line 85, in module support it. Pexpect is intended for UNIX-like operating systems.) ImportError: No module named resource A critical module was not found. Probably this operating system does not support it. Pexpect is intended for UNIX-like operating systems. So now I realize that when I was testing on my system, I run cygwin and so everything worked fine Is there a .dll that I can copy with it or anything so that it works without having to install cygwin on the server that will be hosting this scheduled task? Thanks for any help. Kevin -- http://mail.python.org/mailman/listinfo/python-list
When is divmod(a,b)[0] == floor(a/b)-1 ?
The docs for divmod include the following: divmod(a, b) ...For floating point numbers the result is (q, a % b), where q is usually math.floor(a / b) but may be 1 less than that. ... I know that floating point math can sometimes produce unexpected results, so the above caveat is not entirely surprising. Still, I would find it helpful to see a specific example where divmod(a, b)[0] is equal to math.floor(a/b)-1. Does anybody know one? Thanks! kynn -- http://mail.python.org/mailman/listinfo/python-list
Repeated output when logging exceptions
I wrote some code to handle and log exceptions in my application. It works well, but it produces double output for each exception. How can I fix this? Here's the pared-down code: - main.py import exceptionLogger import doStuff exlog = exceptionLogger.exceptionLogger() stuff = doStuff.doStuff() try: stuff.doit() except Exception,msg: exlog.logException() - exceptionLogger.py import traceback import logging import os class exceptionLogger: loggingLevel = logger = None def __init__(self): self.logger = logging.getLogger(foo) self.loggingLevel = DEBUG if self.loggingLevel == DEBUG: self.logger.setLevel(logging.DEBUG) handler = logging.FileHandler(error.txt) format = %(asctime)s %(levelname)-8s %(message)s formatter = logging.Formatter(format) handler.setFormatter(formatter) self.logger.addHandler(handler) def logMsg(self,message): try: if self.loggingLevel == DEBUG: self.logger.debug(message) except: self.stopLogging() def logException(self): Write an exception traceback to the logfile. # save the stack trace to a file and then copy the contents of that # file to the log. (traceback.format_exc would allow us to avoid # using a temporary file, but it is not available in python 2.3) TMPFILE = os.tmpfile() traceback.print_exc(None, TMPFILE) TMPFILE.flush() self.logMsg( ** EXCEPTION BEGINS **) self.logMsgFromFile(TMPFILE) self.logMsg( ** EXCEPTION ENDS **) TMPFILE.close() def logMsgFromFile(self,file): Read the contents of a file into a string and log the string. file.seek(0) msg = for line in file: msg = msg + line self.logMsg(msg) def stopLogging(self): logging.shutdown() - doStuff.py import exceptionLogger class doStuff: def doit(self): exlog = exceptionLogger.exceptionLogger() try: raise ValueError, hi there except Exception: exlog.logException() And here's the contents of error.txt: - errror.txt 2009-09-24 14:22:23,288 DEBUG ** EXCEPTION BEGINS ** 2009-09-24 14:22:23,288 DEBUG ** EXCEPTION BEGINS ** 2009-09-24 14:22:23,288 DEBUGTraceback (most recent call last): File /home/gordonj/exception/doStuff.py, line 8, in doit raise ValueError, hi there ValueError: hi there 2009-09-24 14:22:23,288 DEBUGTraceback (most recent call last): File /home/gordonj/exception/doStuff.py, line 8, in doit raise ValueError, hi there ValueError: hi there 2009-09-24 14:22:23,288 DEBUG ** EXCEPTION ENDS ** 2009-09-24 14:22:23,288 DEBUG ** EXCEPTION ENDS ** As you can see, all of the output is doubled. Header lines, footer lines, and body text. Why is this happening? I suspect it's because I'm declaring two instances of the exceptionLogger class, which ends up calling logger.addHandler() twice. Is that right? What would be a better way to handle this? Thanks. -- John Gordon A is for Amy, who fell down the stairs gor...@panix.com B is for Basil, assaulted by bears -- Edward Gorey, The Gashlycrumb Tinies -- http://mail.python.org/mailman/listinfo/python-list
Re: Doubley imported module caused devastating bug
Zac Burns wrote: Currently it is possible to import a file of one path to more than one 'instance' of a module. One notable example is import __init__ from a package. See http://stackoverflow.com/questions/436497/python-import-the-containing-package This recently caused a devastating bug in some of my code. What I have is support for the Perforce global options as a context for a perforce module. http://www.perforce.com/perforce/doc.072/manuals/cmdref/o.gopts.html#1040647 This way one can call functions that call many perforce command and have them execute on a different client for example. So, in module A and module B both imported the Perforce module, but they turned out not to be the same module. Module A did with Perforce.GlobalOptions(client=client): B.function() B.function did not receive the new GlobalOptions because of this problem. As a result important files on the original client were overwritten (OUCH). I would like to propose that it be made impossible in the Python source to import two instances of the same module. -- Zachary Burns (407)590-4814 Aim - Zac256FL Production Engineer (Digital Overlord) Zindagi Games I believe that modules are imported only once, and my toy example demonstrates that (python 2.5): test_import.py: testing multiple imports CONSTANT = 928 version = (2, 0, 9) plug_ins = [] random_text = 'some text here' def set_text(new_text): global random_text random_text = new_text plug_ins.append(new_text) A.py: import test_import print test_import.version test_import.set_text('hello!') print test_import.plug_ins print test_import.CONSTANT test_import.CONSTANT = 'changed!' B.py: import test_import print test_import.version test_import.set_text('world!') print test_import.plug_ins print test_import.CONSTANT Running... In [1]: import A (2, 0, 9) ['hello!'] 928 In [2]: import B (2, 0, 9) ['hello!', 'world!'] changed! As you can see, module A made a change to test_import.CONSTANT, and if they were different things then B would not have seen it, yet B *did* see it. This makes me wonder if A) Perforce.GlobalOptions isn't actually setting module level variables, or B) B.function is using copies of those variables that were set when B was originally imported, so is not seeing the changes or C) there is yet another wrinkle here that I don't know about. ;-) ~Ethan~ -- http://mail.python.org/mailman/listinfo/python-list
Evaluate coding and style
I recently started playing with Python about 3 days now (Ex Perl guy) and wanted some input on style and structure of what I'm doing before I really start picking up some bad habits here is a simple test tool I wrote to validate home dirs on my system. Please evaluate and let me know what could have been done better. Once again this is really my first time using python. $ ./homedir_exists.py root mqm pcap root successful! Directory: /var/arpwatch not found! pcap successful! mqm successful! $ cat homedir_exists.py #!/usr/bin/env python import sys, os from re import match userlist = [] filename = '/etc/passwd' for user in sys.argv[1:]: userlist.append(user) try: fh = open(filename) except IOError: print No such filename: %s % (filename) def checkDir(username): data = fh.readlines() for line in data: for user in username: if match(user,line): s = line.split(':') if not os.path.isdir(s[5]): print Directory: %s not found! % (s[5]) print s[0] + successful! checkDir(userlist) -- http://mail.python.org/mailman/listinfo/python-list
Re: Looger object only prints ERRORs
On Sep 24, 2:27 pm, daved170 daved...@gmail.com wrote: hi everybody, I took your adviced and used theloggingobject. I copied the example in 16.6.15.2 - usingloggingin multiple modules fromhttp://docs.activestate.com/activepython/3.1/python/library/logging.html. unfortunattly it only prints to file the ERROR level's messages and ignore the others. I tried to change the level with SETLEVEL but it didn't help. Any idea? thanks, DaveD You need to post some code. I just copied and pasted that example into app.py and auxiliary_module.py, and running it produced a spam.log file with the expected results. So your copy must be different from that example, and the problem will be somehow related to something you've changed. Regards, Vinay Sajip -- http://mail.python.org/mailman/listinfo/python-list
help wanted with list
list = [ 'a', '1', 'b', '2'] what would be the logic, if I input a to get output 1. -- http://mail.python.org/mailman/listinfo/python-list
Re: help wanted with list
You should use a dictionary. dic = {'a':1, 'b':2} Use dic['a'] to get output 1. []s iurisilvio On Thu, Sep 24, 2009 at 5:31 PM, Ahmed Shamim partha.sha...@gmail.comwrote: list = [ 'a', '1', 'b', '2'] what would be the logic, if I input a to get output 1. -- http://mail.python.org/mailman/listinfo/python-list -- http://mail.python.org/mailman/listinfo/python-list
Re: Doubley imported module caused devastating bug
On Sep 24, 10:26 am, Zac Burns zac...@gmail.com wrote: Currently it is possible to import a file of one path to more than one 'instance' of a module. One notable example is import __init__ from a package. Seehttp://stackoverflow.com/questions/436497/python-import-the-containin... This recently caused a devastating bug in some of my code. What I have is support for the Perforce global options as a context for a perforce module.http://www.perforce.com/perforce/doc.072/manuals/cmdref/o.gopts.html#... This way one can call functions that call many perforce command and have them execute on a different client for example. So, in module A and module B both imported the Perforce module, but they turned out not to be the same module. Module A did with Perforce.GlobalOptions(client=client): B.function() B.function did not receive the new GlobalOptions because of this problem. As a result important files on the original client were overwritten (OUCH). I would like to propose that it be made impossible in the Python source to import two instances of the same module. Impossible's a pretty strong word. It's a reasonable request, but with Python's importing the way it is it'd be kind of hard to do. A Python file can be visible in multiple ways. However, anyone who does import __init__ (or from . import __init__ with relative import) is asking for trouble, I can't think of any valid reason to do it, and I wouldn't mind seeing that forbidden, but it's simple to avoid. Someone probably did that because they didn't know how to import a containing package from one of its modules, failing to realize that it created a new module. Carl Banks -- http://mail.python.org/mailman/listinfo/python-list
iterate over list while changing it
Hello, a = [1, 2, 3, 4, 5, 6] for i, x in enumerate(a): if x == 3: a.pop(i) continue if x == 4: a.push(88) print i, i, x, x I'd like to iterate over a list and change that list while iterating. I'd still like to work on all items in that list, which is not happening in the example above. The conditions in the example are not real but much more complex in reality. Can anybody tell me how to do this? Thanks for any hints, Torsten. -- http://mail.python.org/mailman/listinfo/python-list
Re: help wanted with list
2009/9/24 Ahmed Shamim partha.sha...@gmail.com: list = [ 'a', '1', 'b', '2'] what would be the logic, if I input a to get output 1. Turn it into a dictionary first: mylist = [ 'a', '1', 'b', '2'] mydict = dict(zip(mylist[::2], mylist[1::2])) mydict['a'] '1' -- Cheers, Simon B. -- http://mail.python.org/mailman/listinfo/python-list
help wanted with list
Not sure I understand well, but let say I did.. list[list.index('a')+1] Le 14:59, Ahmed Shamim a écrit : list = [ 'a', '1', 'b', '2'] what would be the logic, if I input a to get output 1. -- http://mail.python.org/mailman/listinfo/python-list
Re: Doubley imported module caused devastating bug
On Thu, Sep 24, 2009 at 1:38 PM, Carl Banks pavlovevide...@gmail.com wrote: On Sep 24, 10:26 am, Zac Burns zac...@gmail.com wrote: Currently it is possible to import a file of one path to more than one 'instance' of a module. One notable example is import __init__ from a package. Seehttp://stackoverflow.com/questions/436497/python-import-the-containin... This recently caused a devastating bug in some of my code. What I have is support for the Perforce global options as a context for a perforce module.http://www.perforce.com/perforce/doc.072/manuals/cmdref/o.gopts.html#... This way one can call functions that call many perforce command and have them execute on a different client for example. So, in module A and module B both imported the Perforce module, but they turned out not to be the same module. Module A did with Perforce.GlobalOptions(client=client): B.function() B.function did not receive the new GlobalOptions because of this problem. As a result important files on the original client were overwritten (OUCH). I would like to propose that it be made impossible in the Python source to import two instances of the same module. Impossible's a pretty strong word. It's a reasonable request, but with Python's importing the way it is it'd be kind of hard to do. A Python file can be visible in multiple ways. However, anyone who does import __init__ (or from . import __init__ with relative import) is asking for trouble, I can't think of any valid reason to do it, and I wouldn't mind seeing that forbidden, but it's simple to avoid. Someone probably did that because they didn't know how to import a containing package from one of its modules, failing to realize that it created a new module. Carl Banks -- http://mail.python.org/mailman/listinfo/python-list There are corner cases. The corner case that I ran into was that there were two ways to find the module on PATH because one value of PATH was over another. Since then this problem has been removed and it wasn't too much trouble to work around - but finding the problem was a real pain. I am not intimately familiar with the import code and trust your judgment that it is difficult. If people are in agreement that this should be changed though it could be put in a list somewhere waiting for some ambitious person to figure out the implementation, no? Personally I think it would be worthwhile. -- Zachary Burns (407)590-4814 Aim - Zac256FL Production Engineer (Digital Overlord) Zindagi Games -- http://mail.python.org/mailman/listinfo/python-list
Re: When is divmod(a,b)[0] == floor(a/b)-1 ?
On 2009-09-24 14:40 PM, kj wrote: The docs for divmod include the following: divmod(a, b) ...For floating point numbers the result is (q, a % b), where q is usually math.floor(a / b) but may be 1 less than that. ... I know that floating point math can sometimes produce unexpected results, so the above caveat is not entirely surprising. Still, I would find it helpful to see a specific example where divmod(a, b)[0] is equal to math.floor(a/b)-1. Does anybody know one? In [21]: a = 10.0 In [22]: b = 10.0 / 3.0 In [24]: divmod(a, b)[0] Out[24]: 2.0 In [25]: math.floor(a / b) - 1.0 Out[25]: 2.0 -- Robert Kern I have come to believe that the whole world is an enigma, a harmless enigma that is made terrible by our own mad attempt to interpret it as though it had an underlying truth. -- Umberto Eco -- http://mail.python.org/mailman/listinfo/python-list
Re: iterate over list while changing it
Torsten Mohr wrote: Hello, a = [1, 2, 3, 4, 5, 6] for i, x in enumerate(a): If you change a list while iterating over, start at the tail. ...reversed(enumerate(a)) if x == 3: a.pop(i) del a[i] # you already have the item continue if x == 4: a.push(88) no such list method, which mean you did not run the above before posting. Boo! print i, i, x, x I'd like to iterate over a list and change that list while iterating. I'd still like to work on all items in that list, which is not happening in the example above. The conditions in the example are not real but much more complex in reality. Can anybody tell me how to do this? Thanks for any hints, Torsten. -- http://mail.python.org/mailman/listinfo/python-list
Re: Evaluate coding and style
Brown, Rodrick wrote: I recently started playing with Python about 3 days now (Ex Perl guy) and wanted some input on style and structure of what I'm doing before I really start picking up some bad habits here is a simple test tool I wrote to validate home dirs on my system. Please evaluate and let me know what could have been done better. Once again this is really my first time using python. All in all it looks pretty good. Some minor enhancements... $ ./homedir_exists.py root mqm pcap root successful! Directory: /var/arpwatch not found! false negative? pcap successful! mqm successful! $ cat homedir_exists.py #!/usr/bin/env python import sys, os from re import match userlist = [] filename = '/etc/passwd' for user in sys.argv[1:]: userlist.append(user) since sys.argv is already a list, you can do userlist = sys.argv[1:] try: fh = open(filename) except IOError: print No such filename: %s % (filename) def checkDir(username): data = fh.readlines() for line in data: for user in username: if match(user,line): s = line.split(':') if not os.path.isdir(s[5]): print Directory: %s not found! % (s[5]) print s[0] + successful! checkDir(userlist) passwd has a well defined layout... instead of using re, I would split each line into it's component fields, then just match the username fields against the usernames passed in... this also avoids the false negative shown in your example, and gets rid of one unnecessary loop. for line in data: fields = line.split(':') user = fields[0] path = fields[5] if user in userlist: if os.path.isdir(path): print %s successfull! % user else: print Directory %s for user %s not found! % (path, user) Cheers! ~Ethan~ -- http://mail.python.org/mailman/listinfo/python-list
Re: When is divmod(a,b)[0] == floor(a/b)-1 ?
In mailman.429.1253825725.2807.python-l...@python.org Robert Kern robert.k...@gmail.com writes: On 2009-09-24 14:40 PM, kj wrote: The docs for divmod include the following: divmod(a, b) ...For floating point numbers the result is (q, a % b), where q is usually math.floor(a / b) but may be 1 less than that. ... I know that floating point math can sometimes produce unexpected results, so the above caveat is not entirely surprising. Still, I would find it helpful to see a specific example where divmod(a, b)[0] is equal to math.floor(a/b)-1. Does anybody know one? In [21]: a = 10.0 In [22]: b = 10.0 / 3.0 In [24]: divmod(a, b)[0] Out[24]: 2.0 In [25]: math.floor(a / b) - 1.0 Out[25]: 2.0 Wow. To me this stuff is just black magic, with a bit of voodoo added for good measure... Maybe some day I'll understand it. Thanks! kynn -- http://mail.python.org/mailman/listinfo/python-list
urllib, can't seem to get form post right
I'm trying to scrape some historical data from NOAA's website, but I can't seem to feed it the right form values to get the data out of it. Heres the code: import urllib import urllib2 ## The source page http://www.erh.noaa.gov/bgm/climate/bgm.shtml url = 'http://www.erh.noaa.gov/bgm/climate/pick.php' values = {'month' : 'July', 'year' : '1988'} user_agent = 'Mozilla/4.0 (compatible; MSIE 5.5; Windows NT)' headers = { 'User-Agent' : user_agent } data = urllib.urlencode(values) req = urllib2.Request(url, data, headers) response = urllib2.urlopen(req) the_page = response.read() print the_page -- http://mail.python.org/mailman/listinfo/python-list
Re: Doubley imported module caused devastating bug
Zac Burns wrote: On Thu, Sep 24, 2009 at 1:38 PM, Carl Banks pavlovevide...@gmail.com wrote: On Sep 24, 10:26 am, Zac Burns zac...@gmail.com wrote: Currently it is possible to import a file of one path to more than one 'instance' of a module. One notable example is import __init__ from a package. Seehttp://stackoverflow.com/questions/436497/python-import-the-containin... This recently caused a devastating bug in some of my code. What I have is support for the Perforce global options as a context for a perforce module.http://www.perforce.com/perforce/doc.072/manuals/cmdref/o.gopts.html#... This way one can call functions that call many perforce command and have them execute on a different client for example. So, in module A and module B both imported the Perforce module, but they turned out not to be the same module. Module A did with Perforce.GlobalOptions(client=client): B.function() B.function did not receive the new GlobalOptions because of this problem. As a result important files on the original client were overwritten (OUCH). I would like to propose that it be made impossible in the Python source to import two instances of the same module. Impossible's a pretty strong word. It's a reasonable request, but with Python's importing the way it is it'd be kind of hard to do. A Python file can be visible in multiple ways. However, anyone who does import __init__ (or from . import __init__ with relative import) is asking for trouble, I can't think of any valid reason to do it, and I wouldn't mind seeing that forbidden, but it's simple to avoid. /__init__.py is basically an implementation hack to make a directory also 'be' a file. Use at one own risk, I say. There are corner cases. The corner case that I ran into was that there were two ways to find the module on PATH because one value of PATH was over another. Since then this problem has been removed and it wasn't too much trouble to work around - but finding the problem was a real pain. I am not intimately familiar with the import code and trust your judgment that it is difficult. If people are in agreement that this should be changed though it could be put in a list somewhere waiting for some ambitious person to figure out the implementation, no? 1. It would slow down all imports, at least a bit. 2. It would kill code that intentionally makes use of duplicate modules (but this could be considered exploitation of a bug, perhaps). It would also make forced module reloads harder, it not impossible. Currently, just delete the entry in sys.modules. 3. The language itself does not specify how and where from an implementation 'initializes' a module on first import. Indeed, CPython has at least three options (.py, .zip, and .dll or .pyd (Windows)), with hooks for more. Lets a take the request as specifically preventing the creation of duplicate module objects from a particular .py file. One implementatin *might* be add a set to sys, say sys.mod_files for x.py or x.pyc files already used to initialize a module. The .py or .pyc or .pyo would be stripped but the name otherwise should be the absolute path. (Including drive letter, on Windows). This would not cover the case when files are symlinked (or copied). For *nix, a set of inode numbers could be used, but not for Windows. I suspect there might be other system-specific problems I have not thought of. Terry Jan Reedy -- http://mail.python.org/mailman/listinfo/python-list
Re: Evaluate coding and style
On 24 Sep, 21:11, Brown, Rodrick rodrick.br...@citi.com wrote: I recently started playing with Python about 3 days now (Ex Perl guy) and wanted some input on style and structure of what I'm doing before I really start picking up some bad habits here is a simple test tool I wrote to validate home dirs on my system. Please evaluate and let me know what could have been done better. Once again this is really my first time using python. $ ./homedir_exists.py root mqm pcap root successful! Directory: /var/arpwatch not found! pcap successful! mqm successful! $ cat homedir_exists.py #!/usr/bin/env python import sys, os from re import match Imports are typically one per line. Personally I'd just use import re, as re.match isn't too much typing and makes it fairly clear to any Python programmer it's a regex match (as opposed to a filename match or wildcard match or other such thing) userlist = [] filename = '/etc/passwd' I'd probably have filename as FILENAME as it's almost a constant. for user in sys.argv[1:]: userlist.append(user) You don't really need to build userlist like this, try: userlist = sys.argv[1:] try: fh = open(filename) except IOError: print No such filename: %s % (filename) If the file doesn't exist then print it doesn't exist and carry on regardless anyway? Either re-raise another exception, exit the program, or don't bother with the try/except and just open the file. If it doesn't exist, the exception will go unhandled and the program will stop with the IOError exception. def checkDir(username): data = fh.readlines() for line in data: for user in username: if match(user,line): s = line.split(':') if not os.path.isdir(s[5]): print Directory: %s not found! % (s[5]) print s[0] + successful! Conventionally the function would be called checkdir or check_dir. I'd also pass the file object and userlist as parameters instead of referring to an outer scope. You don't need the .readlines, just iterate over the fh object as 'for line in fh'. 'match' is completely the wrong function to use here. A user of 'r' will match root, roger, etc... etc... Also, as python supports the in operator, looping each user is unnecessary, something like: for line in fh: tokens = line.split(':') if tokens[0] in userlist: if not os.path.isdir(tokens[5]): print 'Directory %s not found' % tokens[5] else: print 'User %s successful' % tokens[0] checkDir(userlist) It's fairly traditional to wrap the 'main' function inside a block to check if this script is the main one that's executing as such: if __name__ == '__main__': checkdir(userlist) This enables the program to be used in an import so that other programs can use its functions, but will only execute the check function, if it's the sole script. hth a bit, Jon -- http://mail.python.org/mailman/listinfo/python-list
Re: urllib, can't seem to get form post right
On 24 Sep, 22:18, Adam W. awasile...@gmail.com wrote: I'm trying to scrape some historical data from NOAA's website, but I can't seem to feed it the right form values to get the data out of it. Heres the code: import urllib import urllib2 ## The source pagehttp://www.erh.noaa.gov/bgm/climate/bgm.shtml url = 'http://www.erh.noaa.gov/bgm/climate/pick.php' values = {'month' : 'July', 'year' : '1988'} user_agent = 'Mozilla/4.0 (compatible; MSIE 5.5; Windows NT)' headers = { 'User-Agent' : user_agent } data = urllib.urlencode(values) req = urllib2.Request(url, data, headers) response = urllib2.urlopen(req) the_page = response.read() print the_page Hint: select name=month option value=/janJanuary/option option value=/febFebruary/option option value=/marMarch/option option value=/aprApril/option option value=/mayMay/option option value=/junJune/option option value=/julJuly/option option value=/augAugust/option option value=/sepSeptember/option option value=/octOctober/option option value=/novNovember/option option value=/decDecember/option /select Jon. -- http://mail.python.org/mailman/listinfo/python-list
Re: Evaluate coding and style
On Thu, 24 Sep 2009 21:11:36 +0100, Brown, Rodrick rodrick.br...@citi.com wrote: I recently started playing with Python about 3 days now (Ex Perl guy) and wanted some input on style and structure of what I'm doing before I really start picking up some bad habits here is a simple test tool I wrote to validate home dirs on my system. Ethan's made some good points. Here are a couple more. [snip] try: fh = open(filename) except IOError: print No such filename: %s % (filename) IOError could be raised for a couple of reasons, not just because the file doesn't exist, so your printed message is a bit misleading. Worse, because you've caught the exception you'll carry on executing, but with no open file! checkDir will then complain, and it will all fall over in one big, misleading mess. On the whole, it's probably better not bothering with the try...except since the default behaviour is likely to be what you want anyway. def checkDir(username): PEP 8 (the style guide) suggests that function names should be lowercase_with_underscores. It's only a suggestion, mind you. data = fh.readlines() Tsk. Using the fh as a global variable, rather than passing it as a parameter? Slapped wrists all round! In fact the division between what's in the function and what's not is rather arbitrary at the moment. I'd be tempted to move the file opening into the function it would make it a little easier to convert this script into a module if you ever wanted to. for line in data: for user in username: if match(user,line): s = line.split(':') if not os.path.isdir(s[5]): print Directory: %s not found! % (s[5]) print s[0] + successful! Apart from Ethan's fix, there's another issue here. You don't catch the case of a username given on the command line not actually existing in the password file. That probably means updating the username list (which is hard if you're iterating over it at the time) or creating a list of what's been found as you go, and being aware of the possibility of duplicates. checkDir(userlist) If you're going to stuff everything into a function and then execute it (not a bad idea at all), then there's another idiom you should know about. if __name__ == '__main__': checkDir(userlist) (though in reality again you'd think about what you wanted in checkDir and bung everything else inside the 'if') The special variable __name__ contains the name of the current module. For a script (executing directly from the command line), the module name is always __main__. So when you run homedir_exists.py from the command line, the condition is true and your script runs exactly as it does now. If however you tweak it along the lines I've suggested, you can use it as a module as well: -=-=-=- $ cat trivial_example.py from homedir_exists import checkDir # Do something important checkDir(fred) # Do something else important -=-=-=- In this case, when homedir_exists.py is read in, __name__ will be the name of the module (i.e. homedir_exists) instead of __main__, so the original checkDir(userlist) won't be executed. -- Rhodri James *-* Wildebeest Herder to the Masses -- http://mail.python.org/mailman/listinfo/python-list
Re: iterate over list while changing it
On Thu, Sep 24, 2009 at 4:32 PM, Torsten Mohr tm...@s.netic.de wrote: Hello, a = [1, 2, 3, 4, 5, 6] for i, x in enumerate(a): if x == 3: a.pop(i) continue if x == 4: a.push(88) print i, i, x, x I'd like to iterate over a list and change that list while iterating. This is generally a bad idea, but you can do it if you're careful. Let's check that enumerate() works when you append to the underlying list: In [1]: n = range(3) In [2]: for i, x in enumerate(n): ...: print i, '=', x ...: if i 3: ...: n.append(23) ...: ...: 0 = 0 1 = 1 2 = 2 3 = 23 4 = 23 5 = 23 So far so good. But notice if you delete an item from the list while iterating over the enumerate object the index returned will still be incremented on the next iteration and you'll skip the item after the one you just deleted: In [1]: n = range(3) In [2]: for i, x in enumerate(n): ...: print i, '=', x, '\t', n ...: if i == 1: ...: del n[i] ...: ...: 0 = 0 [0, 1, 2] 1 = 1 [0, 1, 2] In [3]: n Out[3]: [0, 2] In [4]: n = range(6) In [5]: for i, x in enumerate(n): ...: print i, '=', x, '\t', n ...: if i == 3: ...: del n[i] ...: ...: 0 = 0 [0, 1, 2, 3, 4, 5] 1 = 1 [0, 1, 2, 3, 4, 5] 2 = 2 [0, 1, 2, 3, 4, 5] 3 = 3 [0, 1, 2, 3, 4, 5] 4 = 5 [0, 1, 2, 4, 5] I'd still like to work on all items in that list, which is not happening in the example above. The conditions in the example are not real but much more complex in reality. Can anybody tell me how to do this? You could manage the index yourself, like so: def f(n): i = 0 while True: try: x = n[i] except IndexError: break print i, '=', x if x == 3: del n[i] continue if x == 4: n.append(23) # Increment the index # only if you didn't # delete anything during # this iteration. i += 1 N = range(6) f(N) print N This prints: 0 = 0 1 = 1 2 = 2 3 = 3 3 = 4 4 = 5 5 = 23 [0, 1, 2, 4, 5, 23] HTH, ~Simon Thanks for any hints, Torsten. -- http://mail.python.org/mailman/listinfo/python-list -- http://mail.python.org/mailman/listinfo/python-list
Re: Intercepting binding?
On Thu, 24 Sep 2009 13:39:57 +0100, andrew cooke and...@acooke.org wrote: On Sep 24, 5:20 am, Steven D'Aprano ste...@remove.this.cybersource.com.au wrote: Speaking as a user (although not of Andrew's domain specific language), I'd like to say to developers PLEASE PLEASE PLEASE don't try to help me with half-baked unreliable solutions that only work sometimes. There's few things worse than unreliable tools that break just when you've come to rely on them. [snip context] The reason I asked for unreliable half-baked solutions is that I am exploring what might be possible and, in my experience, this group prefers to lecture me on what they think I should do rather than answer the damn question. I was hoping that by explicitly saying that reliability is not important, people might feel more free to give wild ideas that I could learn from and improve on. It's significant, depressing, and not at all surprising that every person who replied to this thread told me, in one way or another, that was I was asking was wrong or impossible or foolhardy. I did apologise. I'm now going to recant, because (a) you're being an arse, and (b) I agree 100% with Steven here. If a tool is going to break on my when I need it (i.e. in the complicated cases), I don't bother using that tool again. -- Rhodri James *-* Wildebeest Herder to the Masses -- http://mail.python.org/mailman/listinfo/python-list
Re: iterate over list while changing it
On Thu, 24 Sep 2009 21:32:53 +0100, Torsten Mohr tm...@s.netic.de wrote: Hello, a = [1, 2, 3, 4, 5, 6] for i, x in enumerate(a): if x == 3: a.pop(i) continue if x == 4: a.push(88) print i, i, x, x I'd like to iterate over a list and change that list while iterating. I'd still like to work on all items in that list, which is not happening in the example above. The conditions in the example are not real but much more complex in reality. Can anybody tell me how to do this? Generally, don't. It's much less dangerous to create a new list as you go. new_a = [] for x in a: if x != 3: new_a.append(x) if x == 4: new_a.append(88) # or whatever you intended push to mean If you weren't doing the insertion, you could have used a list comprehension and neatened things up a bit. -- Rhodri James *-* Wildebeest Herder to the Masses -- http://mail.python.org/mailman/listinfo/python-list
Re: Regex trouble
On Thu, 24 Sep 2009 19:45:37 +0100, Simon Forman sajmik...@gmail.com wrote: FWIW this problem is too simple (IMHO) for regular expressions. Simply carve off the first three digits and check against sets of the prefixes you're interested in: #any number starting with these prefixes is not long distance local_prefixes = set(['832', '877', '888', '713', '866', '011', '001', '281', '800']) long_distance= {} for key1, value1 in x.iteritems(): if key1 == 'dest': if value1[:3] not in local_prefixes: long_distance[key1] = value1 You need to allow for the potential leading 1, which can be done with a bit of straightforward string slicing but still puts REs more sensibly in the running. -- Rhodri James *-* Wildebeest Herder to the Masses -- http://mail.python.org/mailman/listinfo/python-list
Re: Re: Date using input
Okay, thanks for the advice that sounds a good place to start. I used %2.os was an attempt to define width and precision to stop typo errors eg the user accidentally inputing 101/09/2009 or similar error. So that the __/__/ was adhered to. I will go back to the start get the basics happening and then figure out a way to catch errors with format and correctness. Thanks On Sep 25, 2009 3:57am, Dave Angel da...@ieee.org wrote: flebber wrote: On Sep 24, 11:10 pm, flebber flebber.c...@gmail.com wrote: On Sep 24, 10:58 pm, Dave Angel da...@ieee.org wrote: flebber.c...@gmail.com wrote: I am using python 2.6.2, I haven't updated to 3.0 yet. No I have no class or instructor, I am learning this myself. I have Hetlands book Beginning Python Novice to Professional and online documentation books so Dive into Python, python.org etc. Using the SPE editor. I have currently only fully written basic psuedocode to give me a basic framework to guide myself. #Basic pseudocode #Purpose to get raw input and calculate a score for a field of options and return that #score in a set in descending order. #Multiple sets sould be written to the doc #Obtain date #Check if txt file with same date exists. If yes apphend to results to file. #Obtain location #Set Dictionary #Event number #Obtain set size #Prompt first entry #First Entry Number #First Entry Name #Set Blocks to obtain and calculate data #Block 1 example - Placings Block #Obtain number of events competed in #Obtain how many times finished first #Ensure this value is not greater than Number of Events #Number of Firsts divide by Events * total by 15. #Obtain Second finishes #Ensure this value is not greater than Number of Events #Number of Seconds divide by Events * total by 10. #Continue On with this #Block 2 - Lookup coach Dict and apply value. #Obtain Surname of Coach #Lookup Coach File and Match Name and get value. #Blocks continue gaining and calculating values. #create txt file named using date #Sum Values Block1 + Block2 etc #Print to file event number and field with name number individual Block totals and Sum Total #Arranged in descending Sum Total. #Prompt are there any more events? Yes return to start #Apphend all additional events to same day file seperated by blank line. How many of these steps have you attempted actually coding? Seems to me your first two steps are just string manipulation, and you only need to use the datetime module if you need to validate. In other words, if the user specifies the date as 31/09/2009, you might want to later bounce back to him with a complaint that September only has 30 days. So the first task is to accept input in the form ab/cd/efgh and produce a string efgh-cd-ab.log which you will then create as a text file. And if the file exists, you'll append to it instead of overwriting it. Can you do that much? DaveA Trying but haven't got it working, thats why I started to try and use datetime module. Surely getting it tottally mixed up from datetime import date def ObtainDate(params): date =aw_input(Type Date dd/mm/year: %2.0r%2.0r/%2.0r%2.0r/%4.0r %4.0r%4.0r%4.0r) print date.datetime(year-month-day) #Check if txt file with same date exists. If yes apphend to results to file. date.append(datetime and def ObtainDate(params): date =aw_input(Type Date dd/mm/year: ) date.format =%4.0s%4.0s%4.0s%4.0s-%2.0s%2.0s-%2.0s) print date.format As Tim says, first thing you want to do is rename that variable. You've defined two symbols with the same name. Then I'd ask what that %2.0r stuff is inside the prompt to the user. Do you understand what kind of data is returned by raw_input() ? If so, look at the available methods of that type, and see if there's one called split() that you can use to separate out the multiple parts of the user's response. You want to separate the dd from the mm and from the year. Once you've split the text, then you want to recombine it in a different order, and with dashes between the parts. If I were at your stage of experience, I'd not bother with the datetime module at all. Just see if you can rebuild the string you need, assuming the user has entered a valid 10-character string. Later you can go back and figure out the datetime logic. DaveA -- http://mail.python.org/mailman/listinfo/python-list
Re: Regex trouble
On Thu, Sep 24, 2009 at 6:31 PM, Rhodri James rho...@wildebst.demon.co.uk wrote: On Thu, 24 Sep 2009 19:45:37 +0100, Simon Forman sajmik...@gmail.com wrote: FWIW this problem is too simple (IMHO) for regular expressions. Simply carve off the first three digits and check against sets of the prefixes you're interested in: #any number starting with these prefixes is not long distance local_prefixes = set(['832', '877', '888', '713', '866', '011', '001', '281', '800']) long_distance= {} for key1, value1 in x.iteritems(): if key1 == 'dest': if value1[:3] not in local_prefixes: long_distance[key1] = value1 You need to allow for the potential leading 1, which can be done with a bit of straightforward string slicing but still puts REs more sensibly in the running. -- Rhodri James *-* Wildebeest Herder to the Masses -- http://mail.python.org/mailman/listinfo/python-list Wow, I was looking right at it ('1?') and I missed it. I need more sleep. This is slightly less win: for key1, value1 in x.iteritems(): if key1 == 'dest': n = value1[0] == '1' if value1[n:n + 3] not in local_prefixes: long_distance[key1] = value1 (MRAB's comment about the foolishness of iterating through the dict but testing for key == string_literal still applies, of course.) Depending on what the OP's doing I might recommend just normalizing the phone numbers before processing them, (i.e. stripping off the '1's and possible breaking them up into tuples ('nnn', 'nnn', ''). ~Simon -- http://mail.python.org/mailman/listinfo/python-list
Re: PExpect on Windows System Using Cygwin
Kevin Holleran wrote: Hello, I downloaded and installed the pexpect module and wrote a script. All is well and good, but the script proved to be pretty useful and now I was asked to run it as a scheduled task up on a server to run periodically. I was intending on simply packaging it up with Py2Exe and moving it to the server that way. However, when I went to test it, I received this error Traceback (most recent call last): File script.py, line 17, in module import pexpect File lib\site-packages\pexpect.py, line 85, in module support it. Pexpect is intended for UNIX-like operating systems.) ImportError: No module named resource A critical module was not found. Probably this operating system does not support it. Pexpect is intended for UNIX-like operating systems. So now I realize that when I was testing on my system, I run cygwin and so everything worked fine Is there a .dll that I can copy with it or anything so that it works without having to install cygwin on the server that will be hosting this scheduled task? Thanks for any help. Kevin Why not just use the subprocess module? It's built into the Windows distribution, and doesn't need cygwin. DaveA -- http://mail.python.org/mailman/listinfo/python-list
Re: Doubley imported module caused devastating bug
On Thu, Sep 24, 2009 at 2:51 PM, Ethan Furman et...@stoneleaf.us wrote: I believe that modules are imported only once That's *mostly* true, but try this one: A.py: print 'Importing A' import B B.py: print 'Importing B' import A Cashew:/tmp$ python2.5 B.py Importing B Importing A Importing B -- Daniel Stutzbach, Ph.D. President, Stutzbach Enterprises, LLC http://stutzbachenterprises.com -- http://mail.python.org/mailman/listinfo/python-list
Re: IDE for python similar to visual basic
On Sun, Sep 13, 2009 at 6:25 AM, Nobody nob...@nowhere.com wrote: On Fri, 11 Sep 2009 05:27:59 -0700, r wrote: Sounds like somebody failed to get input from their users at design time. Or somebody has the inability to relate to their end users. You're assuming that there is some right answer which is appropriate for all users. There isn't. I worked for a company that had a team composed of graphic artists, QA types, etc...that did nothing but draw up GUI's, show them to customers, revise them, write up runnable dummies of the approved GUI's, performed usability studies with our customers using the dummy GUI's, and finally handed the GUI's over to dev so they could put in the guts to make it do stuff. Bugs or Cases involving the GUI needing revision because a button needed to be moved for usability were *extremely* rare, and the GUI didn't require an additional toolset that allowed end users to tweak them. -- Computers are like air conditioners... They quit working when you open Windows. -- http://mail.python.org/mailman/listinfo/python-list
Re: Date using input
On Sep 24, 7:49 am, flebber flebber.c...@gmail.com wrote: On Sep 24, 11:10 pm, flebber flebber.c...@gmail.com wrote: On Sep 24, 10:58 pm, Dave Angel da...@ieee.org wrote: flebber.c...@gmail.com wrote: I am using python 2.6.2, I haven't updated to 3.0 yet. No I have no class or instructor, I am learning this myself. I have Hetlands book Beginning Python Novice to Professional and online documentation books so Dive into Python, python.org etc. Using the SPE editor. I have currently only fully written basic psuedocode to give me a basic framework to guide myself. #Basic pseudocode #Purpose to get raw input and calculate a score for a field of options and return that #score in a set in descending order. #Multiple sets sould be written to the doc #Obtain date #Check if txt file with same date exists. If yes apphend to results to file. #Obtain location #Set Dictionary #Event number #Obtain set size #Prompt first entry #First Entry Number #First Entry Name #Set Blocks to obtain and calculate data #Block 1 example - Placings Block #Obtain number of events competed in #Obtain how many times finished first #Ensure this value is not greater than Number of Events #Number of Firsts divide by Events * total by 15. #Obtain Second finishes #Ensure this value is not greater than Number of Events #Number of Seconds divide by Events * total by 10. #Continue On with this #Block 2 - Lookup coach Dict and apply value. #Obtain Surname of Coach #Lookup Coach File and Match Name and get value. #Blocks continue gaining and calculating values. #create txt file named using date #Sum Values Block1 + Block2 etc #Print to file event number and field with name number individual Block totals and Sum Total #Arranged in descending Sum Total. #Prompt are there any more events? Yes return to start #Apphend all additional events to same day file seperated by blank line. How many of these steps have you attempted actually coding? Seems to me your first two steps are just string manipulation, and you only need to use the datetime module if you need to validate. In other words, if the user specifies the date as 31/09/2009, you might want to later bounce back to him with a complaint that September only has 30 days. So the first task is to accept input in the form ab/cd/efgh and produce a string efgh-cd-ab.log which you will then create as a text file. And if the file exists, you'll append to it instead of overwriting it. Can you do that much? DaveA Trying but haven't got it working, thats why I started to try and use datetime module. Surely getting it tottally mixed up from datetime import date def ObtainDate(params): date = raw_input(Type Date dd/mm/year: %2.0r%2.0r/%2.0r%2.0r/%4.0r %4.0r%4.0r%4.0r) print date.datetime(year-month-day) #Check if txt file with same date exists. If yes apphend to results to file. date.append(datetime and def ObtainDate(params): date = raw_input(Type Date dd/mm/year: ) date.format = (%4.0s%4.0s%4.0s%4.0s-%2.0s%2.0s-%2.0s) print date.format I think you are looking for datetime.strptime() input = raw_input(Type Date dd/mm/year: ) d = datetime.strptime(input, %d/%m/%y) print d ~Sean -- http://mail.python.org/mailman/listinfo/python-list
Anyone with Experience Using WinTV Capture Cards?
A friend is looking for some help with how to use Python to access a WinTV (Go Plus) capture card, and how to display an image from it. Is there some facility that might help him, or does anyone have experience with such use that might suggest sources? -- http://mail.python.org/mailman/listinfo/python-list
Re: Evaluate coding and style
Brown, Rodrick wrote: I recently started playing with Python about 3 days now (Ex Perl guy) and wanted some input on style and structure of what I'm doing before I really start picking up some bad habits here is a simple test tool I wrote to validate home dirs on my system. Please evaluate and let me know what could have been done better. Once again this is really my first time using python. You may want to check out pylint ( pychecker) Esmail -- http://mail.python.org/mailman/listinfo/python-list
Re: Re: Re: Date using input
I don't think I am using re.compile properly, but thought as this would make my output an object it would be better for later, is that correct? #Obtain date def ObtainDate(date): date = raw_input(Type Date dd/mm/year: ) re.split('[/]+', date) date year = date[-1] month = date[1] day = date[0] re.compile('year-month-day') On Sep 25, 2009 8:32am, flebber.c...@gmail.com wrote: Okay, thanks for the advice that sounds a good place to start. I used %2.os was an attempt to define width and precision to stop typo errors eg the user accidentally inputing 101/09/2009 or similar error. So that the __/__/ was adhered to. I will go back to the start get the basics happening and then figure out a way to catch errors with format and correctness. Thanks On Sep 25, 2009 3:57am, Dave Angel da...@ieee.org wrote: flebber wrote: On Sep 24, 11:10 pm, flebber flebber.c...@gmail.com wrote: On Sep 24, 10:58 pm, Dave Angel da...@ieee.org wrote: flebber.c...@gmail.com wrote: I am using python 2.6.2, I haven't updated to 3.0 yet. No I have no class or instructor, I am learning this myself. I have Hetlands book Beginning Python Novice to Professional and online documentation books so Dive into Python, python.org etc. Using the SPE editor. I have currently only fully written basic psuedocode to give me a basic framework to guide myself. #Basic pseudocode #Purpose to get raw input and calculate a score for a field of options and return that #score in a set in descending order. #Multiple sets sould be written to the doc #Obtain date #Check if txt file with same date exists. If yes apphend to results to file. #Obtain location #Set Dictionary #Event number #Obtain set size #Prompt first entry #First Entry Number #First Entry Name #Set Blocks to obtain and calculate data #Block 1 example - Placings Block #Obtain number of events competed in #Obtain how many times finished first #Ensure this value is not greater than Number of Events #Number of Firsts divide by Events * total by 15. #Obtain Second finishes #Ensure this value is not greater than Number of Events #Number of Seconds divide by Events * total by 10. #Continue On with this #Block 2 - Lookup coach Dict and apply value. #Obtain Surname of Coach #Lookup Coach File and Match Name and get value. #Blocks continue gaining and calculating values. #create txt file named using date #Sum Values Block1 + Block2 etc #Print to file event number and field with name number individual Block totals and Sum Total #Arranged in descending Sum Total. #Prompt are there any more events? Yes return to start #Apphend all additional events to same day file seperated by blank line. How many of these steps have you attempted actually coding? Seems to me your first two steps are just string manipulation, and you only need to use the datetime module if you need to validate. In other words, if the user specifies the date as 31/09/2009, you might want to later bounce back to him with a complaint that September only has 30 days. So the first task is to accept input in the form ab/cd/efgh and produce a string efgh-cd-ab.log which you will then create as a text file. And if the file exists, you'll append to it instead of overwriting it. Can you do that much? DaveA Trying but haven't got it working, thats why I started to try and use datetime module. Surely getting it tottally mixed up from datetime import date def ObtainDate(params): date =aw_input(Type Date dd/mm/year: %2.0r%2.0r/%2.0r%2.0r/%4.0r %4.0r%4.0r%4.0r) print date.datetime(year-month-day) #Check if txt file with same date exists. If yes apphend to results to file. date.append(datetime and def ObtainDate(params): date =aw_input(Type Date dd/mm/year: ) date.format =%4.0s%4.0s%4.0s%4.0s-%2.0s%2.0s-%2.0s) print date.format As Tim says, first thing you want to do is rename that variable. You've defined two symbols with the same name. Then I'd ask what that %2.0r stuff is inside the prompt to the user. Do you understand what kind of data is returned by raw_input() ? If so, look at the available methods of that type, and see if there's one called split() that you can use to separate out the multiple parts of the user's response. You want to separate the dd from the mm and from the year. Once you've split the text, then you want to recombine it in a different order, and with dashes between the parts. If I were at your stage of experience, I'd not bother with the datetime module at all. Just see if you can rebuild the string you need, assuming the user has entered a valid 10-character string. Later you can go back and figure out the datetime logic. DaveA -- http://mail.python.org/mailman/listinfo/python-list