[Tutor] Question regarding win32com getting ldap object
Hi List, I am running following code to get ldap com object but the result I am getting is unknown I am using Python 25 and win32 bindings available at http://downloads.sourceforge.net/pywin32/pywin32-212.win32-py2.2.exe?modtime=1217535908&big_mirror=0 *adsi = win32com.client.Dispatch('ADsNameSpaces') print adsi ldap = adsi.getobject('', 'LDAP:') print ldap* OutPut: > Any ideas what I am doing wrong ? -- Cheers, Vishwajeet http://www.singhvishwajeet.com ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] RE Silliness
Omer wrote: Bob, I tried your way. >>> import re >>> urlMask = r"http://[\w\Q./\?=\R]+()?" >>> text=u"Not working examplehttp://this.is.a/url?header=nullAnd another linehttp://and.another.url"; >>> re.findall(urlMask,text) [u'', u''] Oops I failed to notice you were using findall. Kent explained it. Another way to fix it is to make () a non-group: (?:) -- Bob Gailer Chapel Hill NC 919-636-4239 ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] extreme basics
Mr Gerard Kelly wrote: This is extremely weird, I think. Here is a tiny program: from math import * from Numeric import * x=[0]*10 for counter in rangelen((x)): x[counter]=counter*0.1 print x Here is what I get: [0.0, 0.10001, 0.20001, 0.30004, 0.40002, 0.5, 0.60009, 0.70007, 0.80004, 0.90002] What on Earth is going on with those decimals at the end? Is this a floating point thing? You can find the answer in the Python FAQ (although it is not a Python thing): http://www.python.org/doc/faq/general/#why-are-floating-point-calculations-so-inaccurate Sincerely, Albert ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
[Tutor] Usage of for loop
Hi all, i am beginner to this python language and slowing learning the language by referring docs.I am trying to understand the for loop i.e., usage of for loop in python,unlike c where i can give condition in python it is simple iterating over sequence. I am trying tounderstand the below lines of code but of no avail. a = ["cat", "window","defenestrate"] for x in a: print x, len(x) i cant understand what x is doing here and what is the purpose of it can anyone help me out here? -- Raghavendra Vanam ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] RE Silliness
Bob, I tried your way. >>> import re >>> urlMask = r"http://[\w\Q./\?=\R]+()?" >>> text=u"Not working examplehttp://this.is.a/url?header=nullAnd another linehttp://and.another.url"; >>> re.findall(urlMask,text) [u'', u''] spir, I did understand it. What I'm not understanding is why isn't this working. (Whereas, >>> OldurlMask = r"http://[\w\Q./\?=\R]+"; #Not f-ing working. >>> re.findall(OldurlMask,text) ['http://this.is.a/url?header=null', 'http://and.another.url'] does work. Which is what had me frowning. Also, this ugly url mask is working: >>> UglyUrlMask = r"(http://[\w\Q./\?=\R]+|http://[\w\Q./\?=\R]+)" >>> re.findall(UglyUrlMask,text) ['http://this.is.a/url?header=null', 'http://and.another.url'] Anyone?) On Mon, Jan 5, 2009 at 12:08 AM, spir wrote: > On Sun, 04 Jan 2009 14:09:53 -0500 > bob gailer wrote: > > > Omer wrote: > > > I'm sorry, burrowed into the reference until my eyes bled. > > > > > > What I want is to have a regular expression with an optional ending of > > > "" > > > > > > (For those interested, > > > urlMask = r"http://[\w\Q./\?=\R]+"; > > > is ther version w/o the optional ending.) > > > > > > I can't seem to make a string optional- only a single character via > > > []s. I for some reason thuoght it'll be ()s, but no help there- it > > > just returns only the . Anybody? > > > > > urlMask = r"http://[\w\Q./\?=\R]+()?" > > > > From the docs: ? Causes the resulting RE to match 0 or 1 repetitions of > > the preceding RE. ab? will match either 'a' or 'ab'. > > > > > > Maybe Omer had not noted that a sub-expression can be grouped in () so that > an operator (?+*) applies on the whole group. > Denis > > -- > la vida e estranya > ___ > Tutor maillist - Tutor@python.org > http://mail.python.org/mailman/listinfo/tutor > ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] extreme basics
"Mr Gerard Kelly" wrote This is extremely weird, I think. No, its normal and you got the right reason its due to floating point binary representation issues. Here is a tiny program: from math import * from Numeric import * This is probably a bad idea here (in fact its usually a bad idea!) since both math and numeric are likely to have similar names in them By importing all names from the modules there is a high chance of name clashes causing strange behaviour. Better to use import math, Numeric Or, if that's too much typing, use import math as M import Numeric as N x=[0]*10 for counter in rangelen((x)): x[counter]=counter*0.1 print x Although of course you are not using anything from either math or Numeric in this example, but I assume you intend to!? HTH, -- Alan Gauld Author of the Learn to Program web site http://www.alan-g.me.uk ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Wayne's waning list.
"WM." wrote As a BASIC, hobby programmer, (long since), I get so jargonized here that I seldom ask about anything any more. Have you tried my tutorial, it compares VBScript (somewhat like BASIC) with Python. It might help. gleaned from reading many, many posts is the URL for projecteuler. Maybe, after I get past the baby steps and start using the libraries, I will benefit more. Now, the opaque questions and bewildering responses leave me in a very passive state. As somebody else said we cover everything from newbies to programming through to experts ion other languages whjo are new to Python and even some experts(ish) in Python who are new to a specific area(like GUI or web programming) As a result the questions range from the trivial to the esoteric. If you think you should know whats being discussed don't be afraid to jump in and ask for clarification in plain English. Either in the same thread or if its looking like a biggie as a separate thread. We aim to help and a background in BASIC should help not hinder! :-) -- Alan Gauld Author of the Learn to Program web site http://www.freenetpages.co.uk/hp/alan.gauld ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] RE Silliness
On Mon, Jan 5, 2009 at 11:16 AM, Omer wrote: > Bob, I tried your way. > import re urlMask = r"http://[\w\Q./\?=\R]+()?" text=u"Not working examplehttp://this.is.a/url?header=nullAnd another linehttp://and.another.url"; re.findall(urlMask,text) > [u'', u''] > > spir, I did understand it. What I'm not understanding is why isn't this > working. There is a bit of a gotcha in re.findall() - its behaviour changes depending on whether there are groups in the re. If the re contains groups, re.findall() only returns the matches for the groups. If you enclose the entire re in parentheses (making it a group) you get a better result: In [2]: urlMask = r"(http://[\w\Q./\?=\R]+()?)" In [3]: text=u"Not working examplehttp://this.is.a/url?header=nullAnd another linehttp://and.another.url"; In [4]: re.findall(urlMask,text) Out[4]: [(u'http://this.is.a/url?header=null', u''), (u'http://and.another.url', u'')] You can also use non-grouping parentheses around the : In [5]: urlMask = r"http://[\w\Q./\?=\R]+(?:)?" In [6]: re.findall(urlMask,text) Out[6]: [u'http://this.is.a/url?header=null', u'http://and.another.url'] Kent ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
[Tutor] Wayne's waning list.
As a BASIC, hobby programmer, (long since), I get so jargonized here that I seldom ask about anything any more. The only useful bit I have gleaned from reading many, many posts is the URL for projecteuler. Maybe, after I get past the baby steps and start using the libraries, I will benefit more. Now, the opaque questions and bewildering responses leave me in a very passive state. ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Wayne's waning list.
Le Mon, 05 Jan 2009 16:01:56 +0100, "A.T.Hofkamp" a écrit : > > (And if it is any comfort to you, it happens to me too. I have read several > discussions about the "@" operator, and still don't understand why you'd want > to have it. No doubt it is a fantastic operator with many uses, but > apparently, not for me.) There may be a question of personal programmer style, and a question of personal history as programmer. I know people for whom OO is weird, useless, and... abstract (which is very true imo -- and even: the better you use it, the more abstract it is). There have learnt without it. When they must use it, e.g. for linking to modules, it's painful. I have learnt without OO (C,pascal) but it seems to fits my brain's "Weltanschauung" (way of watching the world?), as to say my style. But I had hard time with it a start, especially because of the high inconstency of all the various systems that wish to be called OO, and the inconstency of the OO lexicon. About @, it seems not to be style at all, as for you. Ditto for meta-classes (the few I have tried lead me to endless MRO problems). > Sincerely, > Albert > > ___ > Tutor maillist - Tutor@python.org > http://mail.python.org/mailman/listinfo/tutor > -- la vida e estranya ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Usage of for loop
vanam wrote: Hi all, i am beginner to this python language and slowing learning the language by referring docs.I am trying to understand the for loop i.e., usage of for loop in python,unlike c where i can give condition in python it is simple In C, the for-loop is just a hidden 'while'. You can think of (I dropped the variable declaration stuff, and the cumbersome ";" and printf() syntax for clarity) a = ["cat", "window","defenestrate"] for (i=0; i < len(a) ; i = i + 1) { x = a[i] print x } as the equivalent of a = ["cat", "window","defenestrate"] i = 0 while i < len(a): x = a[i] print x i = i + 1 Now take a step back. What are you aiming to achieve here? If you look carefully, the whole i variable is not relevant. The aim of the code is to get each element of the list a in x (one element with each iteration), and do something with it, in this case, printing it. To demonstrate, let's sum all values of list b: b = [1, 2, 3] total = 0 i = 0 while i < len(b): y = b[i] total = total + y i = i + 1 If you compare both pieces, you see the same pieces of boilerplate code appear: i = 0 while i < len(): = [i] and i = i + 1 This happens at every loop that you write. The Python developers decided that Python should take care of the boilerplate code. Of the first 3 lines, the interesting pieces are only (the list you get values from), and (the name of variable where you put each element in with each iteration). The line at the bottom is always the same, no need to enter that each time. What you are left with after throwing out the boilerplate code (in pseudo english/Python), for your original problem, is something like "for each element" x "from the list" a "do" print x which is shortened in Python to for x in a: print x ('in' is ASCII for the 'element of' symbol U+2208). Not only is this much shorter, it is also more powerful. Python has many rich data structures (such as lists, tuples, sets, dictionaries), and the above construct can be used for all of them. Sincerely, Albert ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Usage of for loop
On Mon, Jan 5, 2009 at 14:19, vanam wrote: > I am trying tounderstand the below lines of code but of no avail. Python can loop over many types of sequences.. This can be a list, tuple or string and in your example a list. > a = ["cat", "window","defenestrate"] > for x in a: > print x, len(x) > i cant understand what x is doing here and what is the purpose of it > can anyone help me out here? In the example for every loop x becomes 1 of the values in the list (3 in total). So in the first loop it has value "cat", second "window" and third "defenestrate". How loops work in python is part of most python beginners guides, for example "Dive into Python" [1]. Greets Sander [1] http://diveintopython.org/toc/index.html ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Question regarding win32com getting ldap object
On Mon, Jan 5, 2009 at 8:04 PM, vishwajeet singh wrote: > Hi List, > > I am running following code to get ldap com object but the result I am > getting is unknown > I am using Python 25 and win32 bindings available at > http://downloads.sourceforge.net/pywin32/pywin32-212.win32-py2.2.exe?modtime=1217535908&big_mirror=0 > > *adsi = win32com.client.Dispatch('ADsNameSpaces') > print adsi > ldap = adsi.getobject('', 'LDAP:') > print ldap* > > OutPut: > > > > > Any ideas what I am doing wrong ? > > -- > Cheers, > Vishwajeet > http://www.singhvishwajeet.com > > ___ > Tutor maillist - Tutor@python.org > http://mail.python.org/mailman/listinfo/tutor > > Hi, try the following from the archive: http://mail.python.org/pipermail/python-list/1999-December/018594.html Cheers, Jervis ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Wayne's waning list.
WM. wrote: As a BASIC, hobby programmer, (long since), I get so jargonized here that I seldom ask about anything any more. The only useful bit I have gleaned from reading many, many posts is the URL for projecteuler. Maybe, after I get past the baby steps and start using the libraries, I will benefit more. Now, the opaque questions and bewildering responses leave me in a very passive state. Sorry to hear that. We do not intend to scare you away, on the contrary. It is just that there is a wide range of people posting here with all kinds of different backgrounds with questions on widely different topics. When answering a question (in a single post), one often has to hook into terms and bodies of knowledge that the questioner seems to understand judging by phrases that are used in the question. Given the wide range of background and experience of the readers, several readers will not or only partly understand what is being written. While that is regrettable, I believe it is unavoidable. (And if it is any comfort to you, it happens to me too. I have read several discussions about the "@" operator, and still don't understand why you'd want to have it. No doubt it is a fantastic operator with many uses, but apparently, not for me.) However, you are free to ask questions here, and that includes questions about answers. (It is not until you ask, that we become aware of your need for further clarification.) Sincerely, Albert ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] extreme basics
Le Mon, 05 Jan 2009 17:59:06 +1000, Mr Gerard Kelly a écrit : > This is extremely weird, I think. > > Here is a tiny program: > > from math import * > from Numeric import * > > x=[0]*10 > > > for counter in rangelen((x)): > x[counter]=counter*0.1 > > print x > > Here is what I get: > > [0.0, 0.10001, 0.20001, 0.30004, > 0.40002, 0.5, 0.60009, 0.70007, > 0.80004, 0.90002] > > What on Earth is going on with those decimals at the end? Is this a > floating point thing? I will take the opportunity to try and explain that "weird" issue, because I explored the topic a long time ago. If you /really/ know what "base n", "floating point", and "scientific notation" mean, just skip first section. denis -1- bases We are so used to the decimal notation that we do not think anymore what it actually means. In fact, "decimal" both means in base 10 (from latin root dec- = ten) and fractional (from decim- = tenth). A decimal notation like "1.2" represents 12/10, where /10 expresses the base number and the fractional feature. in that case, "10" mean ten only because the base is ten: "10" mean sixteen in base sixteen, three in base three, two in base two. To generalize the decimal pattern, consider 123.45678 This expresses a number equal to 12345678/10 = 12345678/(10^5) = 12345678*(10^-5). The so-called scientific notation represents this as 12345678e-5, or else 1.2345678e2. "e^n" mean "*base^n". Now, if we change to another base b, the pattern is exactly the same, except that: * each digit is a digit in base b * b is exponentiated For instance: base 16:123af.59= 123af59 / (16^2) = 123af59e-2 base 3: 123.31 = 12331 / (3^2) = 12331e-2 base 2: 101.01 = 10101 / (2^2) = 10101e-2 (=21/4=5.25) -2- exactitude When we write down a number, whatever the notation, it can only be an exact number in the sense that it has a finite number of digits. The point is that an exact number in decimal base must not be exact (in that sense) in another base -- and conversely. This may seem weird indeed (I had a hard time understanding it). Below, the first version of each number representation (silently given by repr()) shows a real internal value, while the second (str()) is nicely arranged for normal humans. >>> 0.5; print 0.5 0.5 0.5 >>> 0.7; print 0.7 0.69996 0.7 Python computes in base 2, so that each non-integer number is represented as mantissa / 2^n For a number to be exact for python, this number must then be so that we can find a proper mantissa and a proper exponent n that satisfies number = mantissa / 2^n or mantissa/number = 2^n As 2^n can only only be 1,2,4,8... there is no garanty that it is possible at all (in a reasonable number of digits -- because both mantissa and n must be ints). The output of Gerard's code indeed shows that a/10 will be an exact number in base 2 only for a=5. Why 0.5? 0.5 = 5/10 ==> base 2: 101/1010 We can see above that the denominator is a multiple of the numerator, so that we can simplify: 101/1010 = 1/10 (base10:1/2) = 1/2^1 = 1e-1 = 0.1 So that 0.5 in base 10 can be expressed as the exact number 0.1 in base 2. Now, let us try 0.7: 0.7 = 7/10 ==> base 2: 111/1010 too bad! It won't match so easily. To express 0.7, we must try and find a proper (mantissa,exponent) pair that together express a result of 0.7. Good luck! Conversely, as I see it, there is no exact binary number which can't be exactly expressed in decimal base. The reason simply is, I guess, that 10 is a multiple of 2. Fractions based on 2 (0.1, 0.01, 0.001...) are not as simple in base 10, but they can be written: >>> for e in range(1,10): print repr(1.0 * 2**(-e)), ... ... 0.5 0.25 0.125 0.0625 0.03125 0.015625 0.0078125 0.00390625 0.001953125 On the contrary, we find problems at once when starting from base 3, as a base three decimal number expression means mantissa/3^n and 1/3 is already inexact in base 10: base3: 0.1 = 1/10==> base10: 1/3 = 0.33... base3: 1.2 = 12/10 ==> base10: 5/3 = 1.66... -- la vida e estranya ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Usage of for loop
On Mon, 2009-01-05 at 18:49 +0530, vanam wrote: > Hi all, > i am beginner to this python language and slowing learning the > language by referring docs.I am trying to understand the for loop > i.e., usage of for loop in python,unlike c where i can give condition > in python it is simple iterating over sequence. > > I am trying tounderstand the below lines of code but of no avail. > > a = ["cat", "window","defenestrate"] > for x in a: > print x, len(x) > i cant understand what x is doing here and what is the purpose of it > can anyone help me out here? > > -- > Raghavendra Vanam > > ___ > Tutor maillist - Tutor@python.org > http://mail.python.org/mailman/listinfo/tutor x is a new variable and when used this way it holds one of the members of "a" for each trip through the loop. After your for loop is done there will be two new names in a "dir()" listing if you're using the interactive python shell. There will be a name "a" and a name "x". If you type "print x" it will output "defenestrate" because it still holds the last value of a from the for loop. John Purser ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
[Tutor] Convert values in a list back and forth from ints and time
Hello Tutors, I use the csv module to read and write a csv file. When I read the file into a new list I convert the ints and the dates to int and time objects so I can do calculations. I use the below function which works. def convertValue(value, dateformat, reverse=False): if reverse: try: return strftime(dateformat, value) except TypeError: return str(value) else: try: return int(float(value)) except ValueError: try: return strptime(value, dateformat) except: return value When writing the lines back to a csv file I loop over the lists and convert the values back with the same function but in reverse but I was just wondering if there is another way of dealing with this. Should I pass the strings to the calculation function (not written yet), do my calculations and have it return strings? Or do you have another way? Thanks Sander ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Convert values in a list back and forth from ints and time
Sander Sweers wrote: Hello Tutors, I use the csv module to read and write a csv file. When I read the file into a new list I convert the ints and the dates to int and time objects so I can do calculations. I use the below function which works. def convertValue(value, dateformat, reverse=False): if reverse: try: return strftime(dateformat, value) except TypeError: return str(value) else: try: return int(float(value)) except ValueError: try: return strptime(value, dateformat) except: return value When writing the lines back to a csv file I loop over the lists and convert the values back with the same function but in reverse but I was just wondering if there is another way of dealing with this. Should I pass the strings to the calculation function (not written yet), do my calculations and have it return strings? I prefer separation of tasks. Let the calculation routines expect and return numbers. I also suggest splitting convertValue into two functions, one that takes strings and one that takes numbers. A lot easier to read and maintain. FWIW you could dispense with reverse in convertValue by testing the type of value for int or str. -- Bob Gailer Chapel Hill NC 919-636-4239 ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Question regarding win32com getting ldap object
Thanks for the help. It worked for me. On Tue, Jan 6, 2009 at 1:28 AM, Jervis Whitley wrote: > > > On Mon, Jan 5, 2009 at 8:04 PM, vishwajeet singh wrote: > >> Hi List, >> >> I am running following code to get ldap com object but the result I am >> getting is unknown >> I am using Python 25 and win32 bindings available at >> http://downloads.sourceforge.net/pywin32/pywin32-212.win32-py2.2.exe?modtime=1217535908&big_mirror=0 >> >> *adsi = win32com.client.Dispatch('ADsNameSpaces') >> print adsi >> ldap = adsi.getobject('', 'LDAP:') >> print ldap* >> >> OutPut: >> >> > >> >> Any ideas what I am doing wrong ? >> >> -- >> Cheers, >> Vishwajeet >> http://www.singhvishwajeet.com >> >> ___ >> Tutor maillist - Tutor@python.org >> http://mail.python.org/mailman/listinfo/tutor >> >> Hi, > try the following from the archive: > > http://mail.python.org/pipermail/python-list/1999-December/018594.html > > Cheers, > > Jervis > -- Cheers, Vishwajeet http://www.singhvishwajeet.com ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor