[Tutor] Syntax Error? Variable Scope?
Hi Folks, I am new to Python, but I have many years experience in software development. I have a question about variable scope. I'm having a problem that I suspect is merely a syntax error or something of that nature. I'm not real clear on variable scoping rules, either. I can't figure out how to declare a variable containing a file pointer outside a class and then access the variable from inside a method inside the class. I could be creating errors when trying to use the "global" keyword due to not fully understanding the scope rules. The examples I found so far in books and on the web are to simplistic for what I want to do. I am using a load-testing app named The Grinder (version 3, beta 30) and Python 2.5. I want my script to open an external CSV (comma-separated values) file containing x number of records with people's names, addresses, etc. I want the file opened outside the TestRunner class (the main class for The Grinder) when the script is first started. Then I want each instance of the TestRunner class to read one line from the external CSV file. After it gets the line from the file, I want to split the line at the commas into components and do the processing stuff. After the last instance of TestRunner finishes, I want the script to close the file and exit nicely. My reasoning for opening and closing the CSV file outside the class is that I want the file to stay open and have the file cursor maintain its location after each read. For example, if I am running 2,000 threads, the file gets opened, whatever thread gets there first reads line 1, the next thread that gets there reads line 2, and so on until all 2,000 threads have had a chance to get a different line from the file. Then I want the file to close. Here are some code snippets: -- from net.grinder.script import Test from net.grinder.script.Grinder import grinder from net.grinder.plugin.http import HTTPPluginControl, HTTPRequest from HTTPClient import NVPair connectionDefaults = HTTPPluginControl.getConnectionDefaults() httpUtilities = HTTPPluginControl.getHTTPUtilities() [... snip ...] fileNewUserInfo = 'new-user-info.csv' fileNewUserInfoIsOpen = 0 [... snip ...] class TestRunner: """A TestRunner instance is created for each worker thread.""" # The instance initialization method. def __init__(self): print 'We are in TestRunner.__init__()' global infile global fileNewUserInfoIsOpen if (fileNewUserInfoIsOpen == 0): infile = open(fileNewUserInfo, "r") fileNewUserInfoIsOpen += 1; print 'We are inside the open if statement' #global infile self._userTokenCtx = '' self._userTokenRi = '' self._userInfo = [] for line in infile.readlines(): self._userInfo.append(string.split((line),',')) print line print self._userInfo def nextMethod(self): [... snip ...] -- The script blows up at this line inside the for loop: self._userInfo.append(string.split((line),',')) with this error message in my error log: 11/2/06 7:12:20 PM (thread 0): Aborting thread due to Jython exception "NameError: string" whilst creating per-thread test runner object NameError: string File "new-user-registration.py", line 356, in __init__ I've tried using "import string" and it doesn't like that. I'm sure I'm missing something very basic here due to my newness with Python. Can anyone offer any insights? Thanks, Chuck -- == Chuck Coker, Software Developer[EMAIL PROTECTED] Tyrell Software Corporation http://www.tyrell.com Office: +1 949 458 1911 x 203Cell: +1 714 326 5939 == ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Starting over with Python
JC> If anyone could direct me to some site where python is associated JC> with Cryptography I would be very grateful. John, I did a quick Google search using +"python" +"cryptography" as the search term and Google reported Results 1 - 10 of about 1,030,000 for +"python" +"cryptography". (0.09 seconds) Chuck -- ====== Chuck Coker, Software Developer[EMAIL PROTECTED] Tyrell Software Corporation http://www.tyrell.com Office: +1 949 458 1911 x 203Cell: +1 714 326 5939 == ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Is Python the language for me?
Andy, Disclaimer: I'm a Python newbie, although I've been developing software for many years. (In my humble opinion,) Luke makes some critically important points here. The first one has to do with programming ~concepts~. LP> As an introductory language to Computer Science I'd recommend LP> Python. You spend less time trying to understand the language LP> itself and more time learning abstract concepts like how a merge LP> sort works, inheritance, classes, 'self', and things like that. LP> Once you understand all of these things, C++ will be greatly LP> easier to pick up, as should most Object-Oriented languages be. I LP> think if you start with C++, you'll get discouraged. Perhaps not, LP> but that's how it worked with me. ... LP> As it turns out, all of the classes at my school are in C++. But LP> despite the fact that I came here with barely any C++ experience, LP> I had _programming_ experience, and that, as they say, made all LP> the difference. I went from C to C++ and had an extremely difficult time with shifting my thinking to object orientation. Once I learned the basics of O-O programming, I've found it very easy to shift from one language to another. As a contract developer for hire, and in my work as an employee at Tyrell Software, I frequently run into situations where, for whatever reason, I need to use a language that I know nothing about. Example: On my current project at Tyrell Software, the decision was made to use a load-testing package named The Grinder. (I highly recommend The Grinder: http://grinder.sourceforge.net/) The load-testing part of the project was handed to me. I knew I would have some questions about Python, so I asked who our resident Python Guru was. They told me, "You are, now." Sometimes you need to learn fast. Having learned the basic concepts, when going to a new language, I have to devote much of my learning time to language syntax issues, rather than learning the basics all over again. Don't let anyone tell you that language X is superior to language Y. In one particular instance that may be so, but in the grand scheme of things it all evens out. Different languages do different things better and worse than other languages, but the O-O concepts remain the same. (This week, I'm being paid to write Python code, so Python is clearly the superior language. :-) ) LP> ... you can use packages and such to keep your code manageable. LP> It's probably a good idea to keep your code separated from the LP> get-go when you reach the time to make your game, because it'll LP> eventually get to the point where you'll need to break it apart, LP> so you'd be doing yourself a favor. Packages, modules, libraries -- whatever you want to call them -- are the way to go. If you put a little bit of thought into them when writing them, most things can be reused at some future time. Most code you write will be code that you will write over and over. Try to keep your pieces small and generic, except, or course, where you need to do a specific step that applies to your current project only. Good luck on your project. Chuck -- == Chuck Coker, Software Developer[EMAIL PROTECTED] Tyrell Software Corporation http://www.tyrell.com Office: +1 949 458 1911 x 203Cell: +1 714 326 5939 == ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Optimal solution in dealing with huge databases in python
This page has some info on C/C++ for PostgreSQL: http://www.postgresql.org/docs/8.0/interactive/xfunc-c.html Shadab Sayani wrote: > Do have any idea about the C api for Postgresql and > some documentation to use it? -- == Chuck Coker, Software Developer[EMAIL PROTECTED] Tyrell Software Corporation http://www.tyrell.com Office: +1 949 458 1911 x 203Cell: +1 714 326 5939 == ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] How many loops does "break" jump out of?
Bob, Watch your code blocks to make sure you don't change your logic inadvertently. The code starting with "if (test_2)" and continuing to the end of the snippet is inside the perpetual for loop which is inside the "if (test_1)" condition. Chuck Bob Gailer wrote: > [EMAIL PROTECTED] wrote: > >> Am trying to convert a C program with a GOTO in it to Python and was >> wondering >> how many loops a Python "break" jumps out of. Here is the C pseudo code >> >> if (test_1) { >> for (;;) { >> if (test_2) { >> do_stuff(); >> } else if (test_2) { >> for (ip=m1+m2+1;ip<=m;ip++) { >> if (test_3) { >> do_more_stuff(); >> if (test_4) >> goto one; >> } >> } >> for (i=m1+1;i<=m1+m2;i++) >> do_even_more_stuff(); >> >> do_yet_more_stuff(); >> } >> >> do_final_stuff(); >> >> one: continue_program(); > > I'd put this code in a function, replace the goto one with return, and > put a call to the function in place of the code. > > def tests(): > if test_1: > etc etc > if test_4: return > > Some may take issue with that. You can, as Alan says, restructure the > code, or use intermediate variables. -- == Chuck Coker, Software Developer[EMAIL PROTECTED] Tyrell Software Corporation http://www.tyrell.com Office: +1 949 458 1911 x 203Cell: +1 714 326 5939 == ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor