Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Python????? The other programming language you never thought of! 2

Status
Not open for further replies.
ahh.. VB and VBScript are two different languages and I haven't seen anything close to what is offered as of yet to array (list in Python) handling in the languages for ASP. I am about to check perlscript though as I'm betting that will be the one to have this functionality.



____________________________________________________
The most important part of your thread is the subject line.
Make it clear and about the topic so we can find it later for reference. Please!! faq333-3811

onpnt2.gif
 
OK Try this (all vbscript - arrays etc..)
<%@language=vbscript %>
<html>
<head>
</head>
<body>
<% = startitall()%>
</body>
</html>

<script language=vbscript runat=server>

Function startitall()

x = array(1,2,3,4,5,6,7,8,9)
arraytest(x)

end Function

function Arraytest(val)
for x = 0 to ubound(val)-1
response.write val(x) & &quot;<br>&quot;
next
end function

</script>
 
Previous should have been

for x = 0 to ubound(val)

(
For x = 1 to 100
Response.write &quot;I shouldn't write code in Notepad <br>&quot;
Next
)

and

(
For x = 1 to 100
Response.write &quot;I should test my code <br>&quot;
Next
)
 
to things I think I need to point out.
a returned value from your example will redim the array to the one value and actuality give a type mismatch unless you loop, which python will not do
eg:
x = array(1,2,3,4,5,6,7,8,9)

function Arraytest(val)
val(0) = 4
end function

startitall(x)
document.write x

that will not work but this will
________________________________________________________
Python
MyNames = [1,2,3,4,5,6,7,8,9]
def Arraytest(val):
val[1] = 4

Arraytest(MyNames)
print MyNames
# here the change is made without errors

____________________________________________________
The most important part of your thread is the subject line.
Make it clear and about the topic so we can find it later for reference. Please!! faq333-3811

onpnt2.gif
 
[lol]

____________________________________________________
The most important part of your thread is the subject line.
Make it clear and about the topic so we can find it later for reference. Please!! faq333-3811

onpnt2.gif
 
If it is really necessary to do the loop rather than just print out the list as done above by onpnt, then I would suggest this:
Code:
def startitall():	#build the list
	return range(1,10)

def Arraytest(val):	#output the list
	for x in val:
		print str(x) + &quot;<br>&quot;

The range function gives us a list of values from 1-9, ie:
Code:
[1,2,3,4,5,6,7,8,9]
the for loop prints each one out with break tags

-Tarwn

01000111 01101111 01110100 00100000 01000011 01101111 01100110 01100110 01100101 01100101 00111111
minilogo.gif alt=tiernok.com
The never-completed website
 
oops, didn;t notice your assignment was inside that function, rewrite:
Code:
def startitall():	#build the list
	x = range(1,10)
	Arraytest(x)

def Arraytest(mylist):	#output the list
	for x in mylist:
		print str(x) + &quot;<br>&quot;

is the same as:
Code:
Sub startitall()
   Dim x
   x = array(1,2,3,4,5,6,7,8,9)
   Arraytest(x)
End Sub

Sub Arraytest(mylist)
   Dim i
   For each i in mylist
      Response.Write i & &quot;<br>&quot;
   Next
End Sub

Although I feel forced to mention that that python example is not really using an array, it is actually using a list.

Now what happens if you want to add an item to a dynamically sized array? Lets rework the vbscript a little:
Code:
Sub startitall(start,count)
   Dim x(), i
   ReDim x(count-start)
   For i = 0 to count - start
      x(i) = i + start
   Next

   Arraytest(x)

   'now lets add an item
   ReDim Preserve x(count-start+1)
   x(UBound(x)) = 12
   
   ArrayTest(x)
End Sub

Sub Arraytest(mylist)
   Dim i
   For each i in mylist
      Response.Write i & &quot;<br>&quot;
   Next
End Sub

startitall 1,10

Ok, now in Python:
Code:
def startitall(start,count):	#build the list
	x = range(start,count+1)
	Arraytest(x)

	x.append(12)
	Arraytest(x)

def Arraytest(mylist):	#output the list
	for x in mylist:
		print str(x) + &quot;<br>&quot;

startitall(1,10)

Note that all it takes to add an item is to append it. I would compare removing an item also, but that requires a lot more work in VBScript than adding it, while it can be done in python in one line again:
Code:
x = x[:2] + x[3:]   #remove the 3rd item from the list

Tada. One line :)

01000111 01101111 01110100 00100000 01000011 01101111 01100110 01100110 01100101 01100101 00111111
minilogo.gif alt=tiernok.com
The never-completed website
 
I really hate you have had more time to play with then me [flame]

____________________________________________________
The most important part of your thread is the subject line.
Make it clear and about the topic so we can find it later for reference. Please!! faq333-3811

onpnt2.gif
 
Here's another one, string manipulation:
VBScript
Code:
Dim aStr
aStr = &quot;****1234567890****&quot;

'extract just the numbers:
Response.Write mid(aStr,5,len(aStr)-8)

Python:
Code:
aStr = &quot;****1234567890****&quot;
print aStr[4:-4]


How about initial assignment?
VBScript:
Code:
Dim i, j, k
i = 0
j = 1
k = 2
Python:
Code:
i, j, k = 0, 1, 2

File reading?
VBScript:
Code:
Dim fso, fil
Set fso = Server.CreateObject(&quot;Scripting.FileSystemObject&quot;)
set fil = fso.OpentextFile(Server.MapPath(&quot;myfile&quot;))
Response.Write fil.ReadAll()
fil.close
Set fil = nothing
Set fso = nothing

Python:
Code:
f = open(&quot;myfile&quot;,&quot;r&quot;)
print f.read()

Create a New Word Document and Save it to the server?
Code:
[b]VBScript[/b]...oooh, scary.

[b]Python:[/b][code]
import codecs,win32com.client

# needed for converting Unicode->Ansi (in local system codepage)
DecodeUnicodeString = lambda x: codecs.latin_1_encode(x)[0]

word = win32com.client.Dispatch(&quot;Word.Application&quot;)
doc = word.Documents.Add()

doc.Content.InsertAfter(&quot;This is my word document!\n&quot;)
doc.Content.InsertAfter(&quot;This entire document was created in Python, ain't life grand?\n&quot;)

# Note that this works, because the style applies to the whole first line.
doc.Range(1,1).Style = win32com.client.constants.wdStyleHeading1

try:
	a = doc.SaveAs(&quot;F:\Programming\Python\pyword.doc&quot;)
except:
	pass

doc.Close()


Get a remote html page?
VBScript
Code:
Dim objhttp
Set objhttp = Server.CreateObject(&quot;Microsoft.XMLHTTP&quot;)
objhttp.Open &quot;GET&quot;, &quot;[URL unfurl="true"]http://www.google.com&quot;,[/URL] False
objhttp.send
Response.Write objhttp.ResponseText
Set objxhttp = Nothing

Python
Code:
import urllib
f = urllib.urlopen(&quot;[URL unfurl="true"]http://www.google.com&quot;)[/URL]
print f.read()

I'll stop for a little bit :)

01000111 01101111 01110100 00100000 01000011 01101111 01100110 01100110 01100101 01100101 00111111
minilogo.gif alt=tiernok.com
The never-completed website
 
e formatting...sorry about that

01000111 01101111 01110100 00100000 01000011 01101111 01100110 01100110 01100101 01100101 00111111
minilogo.gif alt=tiernok.com
The never-completed website
 
Whoops, I forgot to close that file...err
*cough*
Code:
 f.close()

01000111 01101111 01110100 00100000 01000011 01101111 01100110 01100110 01100101 01100101 00111111
minilogo.gif alt=tiernok.com
The never-completed website
 
if I could give you more then one star I would as these have been far more then one valued helpful/expert posts.

besides checking me up.

____________________________________________________
The most important part of your thread is the subject line.
Make it clear and about the topic so we can find it later for reference. Please!! faq333-3811

onpnt2.gif
 
onpnt -- sorry been out but the reason for your type missmatch was that x wasn't set, and passing an &quot;empty&quot; value to the array will cause a missmatch as it isn't an integer..

It seems that the issue is wiht the way VBscript handels passing an array. A good language passes an object by reference, vb script is passing a copy.

When you change the value of an array element inside the called proc, the calling proc won't see the change reflected. It will still only see the original.

.Net etc tend to pass the object and changes would then be reflected outside the proc that changed the array element.

Interesting stuff..

Enjoying the Python tutorial though, this provides some fun exmples..

Rob
 
Just as a note, python does have one issue that has been confusing me. basically it always passes by reference, except when it doesn't :p

From what I gather it passes everything more complex than simple values by reference, whether they be arrays, objects, or even methods (yep, quite nifty).

And now that I mentioned it, say you have a method called idontwanttoforgetthatthismethodisveryveryimportant
Well, thats a real pain to keep typing, so instead of copy and paste, here's another way to handle it:
Code:
i = idontwanttoforgetthatthismethodisveryveryimportant
print i(whatever,arguments)

You can assign variables to methods, or alias if you will, because even methods are considered objects :)

Python also has true OO, by following rules concerning inheritance, constructors, deconstructors, etc.

It also has the nifty ability of being able to tell you what funcions it has inside the code:
Code:
import time
print dir(time)   #prints out all of the methods in the time module

Also, correction to above, there is a del function to remove an item from a list yb it's index.

Want default values? Python allows it.

Want a stack or queue? Python has a pop and append method that allows you to use a list as a stack or queue.

Multililine strings are easy to write:
Code:
print &quot;&quot;&quot;this is on one line
   this is on the next line&quot;&quot;&quot;

And of course, who could forget error handling:
Code:
try:
    f = open(arg, 'r')
except IOError:
    print 'cannot open', arg
else:
    print arg, 'has', len(f.readlines()), 'lines'
    f.close()

Not to mention you can easily override the std error object to create your own customizeable errors :)

Have I mentioned I love this langugae :)
-Tarwn

01000111 01101111 01110100 00100000 01000011 01101111 01100110 01100110 01100101 01100101 00111111
minilogo.gif alt=tiernok.com
The never-completed website
 
The 1200 page McGraw Hill Osborne Python: The Complete Reference from 2001 is selling on one popular auction site for $10, down from the list price of $49. At my local computer store here in the USA it is selling for under $7. I presume a newer edition has been released.

I have not bought one yet, as I'm still using ASP, server-side VBScript and client-side JavaScript. (I've also contributed a couple FAQs over on the dBase forum.)
 
One more thing here.
As far as i see, like you sayed this language seems to get all the good things from each language and that's a good thing but the problem is that my ISP doesnt want to get another script engine. So what should i do?
I'm using JScript code for any complex functions that i need.

Why not <%@ Language = JScript %> ?




________
George, M
 
I currently have this situation myself actually. I persanally called the host that I have the development site under and requested the engine installed. They, of course as I would be also, were reluctant to do it. I talked them into testing it on a mock server first with the current setup and if all is well then they stated I would have it installed.

If they wouldn't have I would have left them. Bottom line I guess. if you want the power of the language then you need to find the resources to support it.
I (and Tarwn I'm sure) think this language is of that great importance to have on your side that causing a service change would be a benifit to the gain.

1H 1K 10 3D 3F 3E 3K 38 3J 10 1T 10 3G 3L 3I 35 10 35 3O 33 35 3C 3C 35 3E 33 35

brickyard.jpeg
Most intense event I ever attended!
 
Exactly same response i got from them. But i'm stick with them for now, moving to another ISP will get same problem here in my country so... maybe when they will use Python another great engine will be used.

One day maybe...

________
George, M
 
It would be nice if one of you Python People would write a FAQ in the Python forum titled: &quot;How To Get Started&quot; or &quot;Python 101: The Basics&quot;.

In a month or two, I'm going to want to get started on trying this all out and I won't be able to figure out where to begin (if history is any indicator, I won't be able to find this thread) [dazed]

Programming today is a race between software engineers striving to build better and bigger idiot-proof programs, and the Universe trying to produce bigger and better idiots. So far, the Universe is winning. - Rick Cook
 
how about a PythonScript 101 FAQ for the ASP forum?

Or are you going to work outside the scripting side of things?
I guess I'm kind of undecided on where it would belong in regards to platforms.

_____________________________________________________________________
onpnt2.gif

[bdaycandle]
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top