Re: figuring week of the day....
On Jan 9, 6:05 am, Tim Chase wrote: > Tim Chase wrote: > > tekion wrote: > >> Is there a module where you could figure week of the day, like where > >> it starts and end. I need to do this for a whole year. Thanks. > > > the monthcalendar() call returns the whole month's calendar which > > may be more what you want for the big-picture. > > And if you want a whole year's worth, you can get pretty close with: > > import itertools as i > import calendar as c > for month in range(1,13): > for week in c.monthcalendar(2009, month): > print repr(w) > > You don't detail how you want the month-boundaries to behave, so > this gives "calendar"'s default behavior of filling in zeros on > month-boundaries, so November through the 1st week in Dec 2009 > comes back as > > ... > [0, 0, 0, 0, 0, 0, 1], > [2, 3, 4, 5, 6, 7, 8], > [9, 10, 11, 12, 13, 14, 15], > [16, 17, 18, 19, 20, 21, 22], > [23, 24, 25, 26, 27, 28, 29], > [30, 0, 0, 0, 0, 0, 0], > [0, 1, 2, 3, 4, 5, 6], > ... > > rather than > > ... > [26, 27, 28, 29, 30, 31, 1], > [2, 3, 4, 5, 6, 7, 8], > [9, 10, 11, 12, 13, 14, 15], > [16, 17, 18, 19, 20, 21, 22], > [23, 24, 25, 26, 27, 28, 29], > [30, 1, 2, 3, 4, 5, 6], > ... > > -tkc Thanks, this is exactly what I am looking for. I will give it a try. Do you what argument or documentation I should read up on how to get the month's boundary rather than the default. I would assume it's just an argument I give when creating the month object. -- http://mail.python.org/mailman/listinfo/python-list
help with class
Hello, I am playing with class. Below is the code snippet: #!/usr/bin/python 2 3 class test_class: 4#import gzip 5def __init__(self,file): 6 self.file = file 7def open_file(self): 8 try: 9 print "file: %s" % self.file 10 self.xml_file = gzip.GzipFile(self.file,'r') 11 except: 12 print "an exception has occured" 13 for line in self.xml_file: 14 print "line: %s" % line 15 self.xml_file.close() 16 17 18 if __name__ == '__main__': 19import gzip 20import sys 21t = test_class( sys.argv[1] ) 22t.open_file() My question are: 1. Why do I need to use "import gzip" on main section to get it the script to work? I would assume you need the import of gzip in the class section. 2. What is the proper way of using module in a class you are creating? -- http://mail.python.org/mailman/listinfo/python-list
Re: help with class
Hi, so I am assuming global name space refers to the location of where the module is imported, for example my previous example where I call the gzip module from test_class class, like so: class test_class: import gzip did not work because of scoping. So global name space in this case is declaring the import outside of test_class like so: import gzip class test_class: etc.. Is my assumption correct? Thanks. -- http://mail.python.org/mailman/listinfo/python-list
trapping signal
I have a while iterates forever. I would like to trap a SIGTERM signal and execute some clean up code. How would I do this in python? Thanks. -- http://mail.python.org/mailman/listinfo/python-list
Help with capturing error
Hello, I am getting the following error and my script is bailing out because of it. I have tried capturing it but it does not seem to work. Below is the error: ValueError: I/O operation on closed file the above error is received when, the following code snippet is executed: try: 117 self.xml_file.close() 118 except ValueError: 119 print "file, %s is already closed" % self.seek_file Any idea why line 118, is not capturing the error? Thanks. -- http://mail.python.org/mailman/listinfo/python-list
Re: Help with capturing error
Yes, you are absolutely right. I had open a file earlier and when it reach end of line with no new idea; it seems to have closed the file. I am not sure if this because I am using my own class to open and read a file or just a python behavior. I plan to test this out. On another note, is there a way for python to emulate a "tail -f" in Linux? Thanks. -- http://mail.python.org/mailman/listinfo/python-list
is there a way to determine a relative path to the script?
Hello,
I have a script in /usr/local/app/mypython.py and a configuration file
relative to /usr/local/app/conf. When I call the script with an
absolute path of /usr/local/app/mypthon.py I recieved an error
similar to the below error:
Traceback (most recent call last):
File "script/art/auditlog.py", line 28, in ?
database = Config.get("DB", "user")
File "/System/Library/Frameworks/Python.framework/Versions/2.3/lib/
python2.3/ConfigParser.py", line 505, in get
raise NoSectionError(section)
ConfigParser.NoSectionError: No section: 'DB'
I know why, the configuration which I reference in the script is
relative to "/usr/local/app", when I call the script via an absolute
path, then the relative the configuration file is base on where ever I
call the script from. One way to fix this is to add a path manually
into the variable. But I would like to avoid this hard-coding
parameter into my script. Is there a way to determined the relative
location of the script programatically? FYI, in the end this scrip
would run from CRON.
--
http://mail.python.org/mailman/listinfo/python-list
figuring week of the day....
Is there a module where you could figure week of the day, like where it starts and end. I need to do this for a whole year. Thanks. -- http://mail.python.org/mailman/listinfo/python-list
help with comparison
Hi,
Could some one take a look at the below code snipet which keep
failing:
import optparse
p = optparse.OptionParser(description="script to do stuff",
prog="myscript.py", )
p.add_option("-c" "--compress", help="0 is noncompress")
function1(options.compress)
here's what the what function definition looks like:
function1(zipfile) :
if (zipfile == 1):
do stuff here with for compress file
else
do stuff here
when I call the script "myscript.py 1", the above test keeps falling
to the else clause. I am thinking the object zipfile is not the same
as "1". Any thoughts as how I should test if the argument being pass
in and parse by optparse is 1 or "0"? Thanks.
--
http://mail.python.org/mailman/listinfo/python-list
Re: help with comparison
On Nov 19, 11:36 pm, George Sakkis <[EMAIL PROTECTED]> wrote:
> On Nov 19, 10:21 pm,tekion<[EMAIL PROTECTED]> wrote:
>
>
>
> > Hi,
> > Could some one take a look at the below code snipet which keep
> > failing:
>
> > import optparse
> > p = optparse.OptionParser(description="script to do stuff",
> > prog="myscript.py", )
> > p.add_option("-c" "--compress", help="0 is noncompress")
> > function1(options.compress)
>
> > here's what the what function definition looks like:
> > function1(zipfile) :
> > if (zipfile == 1):
> >do stuff here with for compress file
> > else
> >do stuff here
>
> > when I call the script "myscript.py 1", the above test keeps falling
> > to the else clause. I am thinking the object zipfile is not the same
> > as "1". Any thoughts as how I should test if the argument being pass
> > in and parse by optparse is 1 or "0"? Thanks.
>
> 1 (without quotes) is not the same as "1" (with quotes); the first is
> an integer, the second a string. optparse returns strings by default,
> so the easiest change would be to make the check 'if zipfile == "1"'.
>
> Even better, since it's a boolean option, pass action="store_true" to
> p.add_option(). The test then is reduced to "if zipfile" and the
> program is to be called by "myscript.py -c". Read the docs [1] for
> more details.
>
> HTH,
> George
>
> [1]http://docs.python.org/library/optparse.html#standard-option-actions
Thanks. This fixed it.
--
http://mail.python.org/mailman/listinfo/python-list
tarfiles usage on python 2.4.4
All, I am using tarfile module and my python is version 2.4.4. When I call method extractall, I am getting error method does not exist. Could someone confirm if the method exist on python 2.4.4? Thanks -- http://mail.python.org/mailman/listinfo/python-list
converting string to a date format
All, I know there is a datetime module for converting and manipulate date format. I have this string date format: 24/Nov/2009:10:39:03 -0500 and would like to convert it to a date format of "2009-11-24 10:39:03". At the moment I am reading datetime module trying to find out if I could do it with datetime module. Does any one know of a way besides slashing my way through it using string split function? Thanks. -- http://mail.python.org/mailman/listinfo/python-list
Re: converting string to a date format
Ben, I do not have python 2.6 install, my version of Python is 2.4. Because of my version of Python I believe I have to perform what you have suggested: This should, ideally, consist of two separate operations: * parse the string, using a specific format, to create a ‘datetime’ object * create a string representation of the datetime using your preferred string format So I guess I am stuck on parsing the string "24/Nov/2009:12:00:00 -0500" using regex and or string function to get the output to "2009-11-24 12:00:00". It looks like I may have to use regex to accomplish this and also re-map Nov to "11". Does any one have any idea that would take "24/Nov/2009:HH:MM:SS" and format it to "2009-11-24 HH:MM:SS"? Thanks -- http://mail.python.org/mailman/listinfo/python-list
Re: converting string to a date format
On Dec 21, 3:05 pm, MRAB wrote:
> tekionwrote:
> > Ben,
> > I do not have python 2.6 install, my version of Python is 2.4.
> > Because of my version of Python I believe I have to perform what you
> > have suggested:
>
> > This should, ideally, consist of two separate operations:
>
> > * parse the string, using a specific format, to create a ‘datetime’
> > object
>
> > * create a string representation of the datetime using your
> > preferred
> > string format
>
> > So I guess I am stuck on parsing the string "24/Nov/2009:12:00:00
> > -0500" using regex and or string function to get the output to
> > "2009-11-24 12:00:00". It looks like I may have to use regex to
> > accomplish this and also re-map Nov to "11". Does any one have any
> > idea that would take "24/Nov/2009:HH:MM:SS" and format it to
> > "2009-11-24 HH:MM:SS"? Thanks
>
> If you don't have the 'datetime' module then you can use the 'time'
> instead. Use time.strptime() to parse the string and time.strftime() to
> create the new string.
Thanks. This is what I have:
time.strftime("%Y-%m-%d %H:%M:%S", time.strptime("24/Nov/
2009:12:00:00", "%d/%b/%Y:%H:%M:%S")
and it seems to work.
--
http://mail.python.org/mailman/listinfo/python-list
python 2.x and running shell command
All, some of the servers I have run python 2.2, which is a drag because I can't use subprocess module. My options that I know of is popen2 module. However, it seems it does not have io blocking capabilities. So every time run a command I have open and close a file handle. I have command that requires interactive interaction. I want to be be able to perform following action: fout, fin = popen2.open2(cmd) #open up interactive session fin.write(cmd2); block (input) fout.readline() block output fin.write(cmd2) and so on... is this possible with popen2 or do I have to use pexpect for the job? Thanks. -- http://mail.python.org/mailman/listinfo/python-list
Re: python 2.x and running shell command
On Dec 23, 5:22 pm, Sean DiZazzo wrote: > On Dec 23, 1:57 pm, tekion wrote: > > > > > All, > > some of the servers I have run python 2.2, which is a drag because I > > can't use subprocess module. My options that I know of is popen2 > > module. However, it seems it does not have io blocking > > capabilities. So every time run a command I have open and close a > > file handle. I have command that requires interactive interaction. I > > want to be be able to perform following action: > > fout, fin = popen2.open2(cmd) #open up interactive session > > fin.write(cmd2); > > block (input) > > fout.readline() > > block output > > fin.write(cmd2) > > and so on... > > > is this possible with popen2 or do I have to use pexpect for the job? > > Thanks. > > I've never done that with subprocess, but maybe this will > help:http://www.lysator.liu.se/~astrand/popen5/ > > ~Sean Sean, popen5 is old name for subprocess. -- http://mail.python.org/mailman/listinfo/python-list
deriving MySQLdb class
All, I am trying to write a class which inherits from MySQLdb class. Below is code snippet: import MySQLdb import sys class msql_connect(MySQLdb): def __init__(self): self.host = "hostname" self.user = "user" self.password = "passoword" self.database = "database name" I am running into below error: class msql_connect(MySQLdb): TypeError: Error when calling the metaclass bases module.__init__() takes at most 2 arguments (3 given) Does any one have an idea why? Thanks. -- http://mail.python.org/mailman/listinfo/python-list
Re: deriving MySQLdb class
Sean, I did a little investigation, there are other classes besides Connection. So, could I only set up a derived class from Connection and still be able to use the connection to query database and retrieve data? -- http://mail.python.org/mailman/listinfo/python-list
easy_install error ...
All, I am running into issue with easy install error, see error below: python setup.py easy_install -m 'docutils==0.4' running easy_install error: Not a URL, existing file, or requirement spec: "'docutils==0.4'" any idea as to why? thanks. -- http://mail.python.org/mailman/listinfo/python-list
Re: easy_install error ...
I am also having another issue with easy_install, python setup.py easy_install --find-links thirdparty 'docutils==0.4' running easy_install error: Not a URL, existing file, or requirement spec: "'docutils==0.4'" I think both problem are related. I have setuptool version .6C11 install and I am running python 2.5 on window OS. Is any one else having similar issue with setuptool version .6C11? Thanks. -- http://mail.python.org/mailman/listinfo/python-list
Re: easy_install error ...
FYI,
I figured out what I was doing wrong. After reading the setuptools
docs, I noticed took out the quotes around the package name and it
works, see details below:
python setup.py easy_install -m docutils==0.4
running easy_install
Searching for docutils==0.4
Best match: docutils 0.4
Processing docutils-0.4-py2.6.egg
Removing docutils 0.4 from easy-install.pth file
Installing rst2html.py script to C:\Python26\Scripts
Installing rst2latex.py script to C:\Python26\Scripts
Installing rst2newlatex.py script to C:\Python26\Scripts
Installing rst2pseudoxml.py script to C:\Python26\Scripts
Installing rst2s5.py script to C:\Python26\Scripts
Installing rst2xml.py script to C:\Python26\Scripts
Using c:\python26\lib\site-packages\docutils-0.4-py2.6.egg
Because this distribution was installed --multi-version, before you
can
import modules from this package in an application, you will need to
'import pkg_resources' and then use a 'require()' call similar to one
of
these examples, in order to select the desired version:
pkg_resources.require("docutils") # latest installed version
pkg_resources.require("docutils==0.4") # this exact version
pkg_resources.require("docutils>=0.4") # this version or higher
Processing dependencies for docutils==0.4
Finished processing dependencies for docutils==0.4
I hope this helps other having the similar issue as me.
--
http://mail.python.org/mailman/listinfo/python-list
Re: deriving MySQLdb class
On Jan 21, 11:48 pm, Sean DiZazzo wrote: > On Jan 21, 8:17 pm, tekion wrote: > > > Sean, > > I did a little investigation, there are other classes besides > > Connection. So, could I only set up a derived class from Connection > > and still be able to use the connection to query database and retrieve > > data? > > Im not sure I understand you completely... > > In theory, you could use the C API directly and subclass > _mysql.connection to get at the database. But I think the point of > MySQLdb is that they've done all the hard work. Why not use it? Regarding above statement, Yeah; what you said is true. I am just playing with inheritance to get a better understanding of it and since I am working with database connection, then I might as learn it by doing it. > I think the other stuff in the module is in support of the Connection > () class. ie. You cant get a cursor unless you already have a > connection. Yes, I have confirmed it just by inheriting the connection class, I am able to connect and perform normal database functions. Sweet!!! This is why I like Python. Thanks for your help. -- http://mail.python.org/mailman/listinfo/python-list
python ldap recursive
Hi, I know perl Net::LDAP could do a recursive search call to LDAP. What I am running into with Python LDAP on the search call is that I would l have to wait for the search to complete to get the result. Where as with Perl recursive search call, I would get the result (not the completed result) back while the search is still running. Does any know if we have something like this in Python LDAP module? Thanks. -- http://mail.python.org/mailman/listinfo/python-list
Having problem with subclass
All, I have file name A.py with a class name Aclass. I have a file name B.py with sub class of Aclass, like import A class Bclass(Aclass) ...rest of code When I test it, calling B.py, I am getting: class Bclas(Aclass): NameError: name 'Aclass' is not defined When I move Bclass to A.py, it works. Is there some restriction in python that sub classing a class has be in the same file as the class you're are sub classing? Thanks. -- http://mail.python.org/mailman/listinfo/python-list
ElementTree handling nested tag
All, I have the following xml tag: httpRequest HTTP://cmd.wma.ibm.com:80/ GET 200 I am interested in: httpRequest HTTP://cmd.wma.ibm.com:80/ GET 200 as well as the upper layer tag. How do I get at the nest tag listed above? Thanks. -- http://mail.python.org/mailman/listinfo/python-list
Re: ElementTree handling nested tag
On Oct 2, 5:32 am, [email protected] (Diez B. Roggisch) wrote: > tekion writes: > > All, > > I have the following xml tag: > > > > > > httpRequest > > HTTP://cmd.wma.ibm.com:80/ > > GET > > 200 > > > > > > > I am interested in: > > httpRequest > > HTTP://cmd.wma.ibm.com:80/ > > GET > > 200 > > as well as the upper layer tag. How do I get at the nest tag listed > > above? Thanks. > > What is the "upper layer tag"? And what do you actually want to "get"? > The text-values? Or do you want to just create a document that just > consists of the resource_access tag? > > Then this should help: > > from xml.etree.ElementTree import * > > doc = """ > > > httpRequest > HTTP://cmd.wma.ibm.com:80/ > GET > 200 > > > """ > > doc = fromstring(doc) > > resource_access = doc.find("resource_access") > print tostring(resource_access) > > Diez Diez, This is the sample format from the doc. I the whole log file has this xml formated beginning and ending in the event tag. Is this something ElemenTtree can handle or is it better to user XSLT? Thanks. 2005-10-02-22:01:36.187-04:00I- 1 http 109 1 cmd.wma.ibm.com Unauthenticated 9.54.83.206 IPV4 / HTTP://cmd.wma.ibm.com:80/ httpRequest HTTP://cmd.wma.ibm.com:80/ GET 200 GET HTTP://cmd.wma.ibm.com:80/ HTTP/1.0 1970 Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1) -- http://mail.python.org/mailman/listinfo/python-list
Re: ElementTree handling nested tag
On Oct 3, 2:09 pm, [email protected] (Diez B. Roggisch) wrote: > tekion writes: > > On Oct 2, 5:32 am, [email protected] (Diez B. Roggisch) wrote: > >> tekion writes: > >> > All, > >> > I have the following xml tag: > >> > > >> > > >> > httpRequest > >> > HTTP://cmd.wma.ibm.com:80/ > >> > GET > >> > 200 > >> > > >> > > > >> > I am interested in: > >> > httpRequest > >> > HTTP://cmd.wma.ibm.com:80/ > >> > GET > >> > 200 > >> > as well as the upper layer tag. How do I get at the nest tag listed > >> > above? Thanks. > > >> What is the "upper layer tag"? And what do you actually want to "get"? > >> The text-values? Or do you want to just create a document that just > >> consists of the resource_access tag? > > >> Then this should help: > > >> from xml.etree.ElementTree import * > > >> doc = """ > >> > >> > >> httpRequest > >> HTTP://cmd.wma.ibm.com:80/ > >> GET > >> 200 > >> > >> > >> """ > > >> doc = fromstring(doc) > > >> resource_access = doc.find("resource_access") > >> print tostring(resource_access) > > >> Diez > > > Diez, > > This is the sample format from the doc. I the whole log file has this > > xml formated beginning and ending in the event tag. Is this something > > ElemenTtree can handle or is it better to user XSLT? Thanks. > > Handle *what*? Can it read it? Yes. Can it extract data from it? > Yes. You still haven't said what you actually *want* with all this. > > Diez I wan to get the value of these tags: HTTP://cmd.wma.ibm.com:80/ GET 200 You asked for what the upper layer tags are. The upper layer tags I am referring for below tags are any tag that is above it. HTTP://cmd.wma.ibm.com:80/ GET 200 IE, for the upper upper are: httpRequest and tags. -- http://mail.python.org/mailman/listinfo/python-list
