761 Posted Topics
Re: [code=python]def line_sum_and_count(line): """Finds the sum and count of the numbers in the given line of text.""" #----------------------------------------------------- data = line.split() tally = 0 for i in data: tally += float( i ) #------------------------------------------------------- return tally, len( data )[/code] Here's a different way to tackle the first function. I think your … | |
Re: Argh. [url=http://en.wikipedia.org/wiki/Jargon]Jargon[/url] | |
Re: Here's a comparison: [code=python] >>> a = [1,2,3,4,5,6] >>> reduce(lambda y,x: x+y, a) 21 >>> sum = 0 >>> for i in a: ... sum+=i ... >>> sum 21 >>> [/code] Reduce is a way to perform a function cumulatively on every element of a list. It can perform any … | |
Re: sys.argv is used for passing command line arguments to your program. It is a list, and the first value [icode]sys.argv[0][/icode] is always the name of your program. So let's say you open up a terminal and type in [icode]python my_prog.py foo bar[/icode] Then sys.argv's contents would be [0] my_prog.py [1] … | |
Re: Anybody who doesn't know what he's talking about and too lazy to google: [url=http://en.wikipedia.org/wiki/Roguelike]wiki[/url] If I were you I would look into pygame, as that would give you an easy-to-develop GUI with which to interact. The rest is simple python work. | |
Re: PSST! Secret: [code=python] >>> import string >>> string.letters 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ' >>> string.digits '0123456789' [/code] And PSST!!! To turn them into lists use list comprehension: [code=python] >>> [ lett for lett in string.letters ] ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', … | |
First you'll need to learn how to use code tags. As your code stands, it is unreadable because all indentation has been lost. If you use code tags, your code is not only readable, but forum members can easily copy and paste it into their code editors to tinker with. … | |
Re: This is strange. I'm getting [code=Traceback] Traceback (most recent call last): File "<input>", line 1, in <module> File "C:\Python25\lib\pickle.py", line 858, in load dispatch[key](self) File "C:\Python25\lib\pickle.py", line 1198, in load_setitem dict[key] = value TypeError: 'str' object does not support item assignment[/code] The object that you pickled, did it require any … | |
Re: Don't double post... but regardless, check your other post in this same forum. | |
Re: also, which version of Python is this? If you are using the latest you should be using the subprocess module as it's much more efficient and easier to use than the old threading module, which is why it's becoming the new standard ;) | |
Re: You can look into paramiko for FTP use. I've used it before and it's VERY straight forward. The documentation provides plenty of examples of usage for connecting to an FTP server and retrieving files. | |
Re: Couldn't you just read(x); x number of bytes into some garbage variable? | |
Re: Please use code tags. They work like this: [noparse][code=<language>] My Code goes here! [/code][/noparse] Where <language> is any of the languages on DaniWeb (C, Perl, Python, HTML, PHP, etc.) | |
Re: Here's how to ask for input: [url=http://docs.python.org/lib/built-in-funcs.html]Built-in functions[/url] Look down the page... there are two input methods. See if you can find them. | |
Re: Kind of sounds like dependency issues... but seeing as that module is built into Python I don't see how this could happen. I tried importing those modules (which I've never used before) and they worked. Perhaps you need a reinstallation of Python. | |
Re: It's not exactly clear what this example is doing.. Plus this isn't a stand-alone example of the problem that you are seeing since we don't know what possible and matches are (ie, are they lists, tuples, dictionaries, strings?) ... If you could provide us with an idea of exactly what … | |
Re: First of all, please use Code Tags. They make your post readable, and thus will make forum members more likely to read your problem and help you. Secondly, you say that only the last field is "changin", but that is the only variable that changes... [icode]for emailadd in mailing_list:[/icode]... see? … | |
Re: Read the [url=http://docs.python.org/lib/module-time.html]docs[/url] please. | |
Re: Here's one method to open and read a file: [code=python] f = open( 'myfile.txt', 'r' ) # 'r' is for Read Mode lines = f.readlines() f.close() # Now lines contains a list of each line of the file contents for eachline in lines: # Add line to wx.*ctrl [/code] | |
Re: I understand what you're trying to do but where is your question? Do you need to know what modules can be used for this or do you need help implementing a certain portion of this idea? etc... | |
Re: [QUOTE=Ene Uran;691238]For the many of you who still want to know the solution, make variable 'a' a global:[/QUOTE] That or simply pass a to test: [code=python]words = ["aero", "aesthet", "andr", "arch", "arch", "ast", "baro",\ "biblio", "bio", "cardi", "chron", "cosm", "crat", "cycl", "dem", "dont",\ "dogma", "dox", "esth"] answers = ["air", "sense", … | |
Re: You never imported your class... as I'm assuming that 'PathFinder' is not in your Main.py ... let's say it's in PF.py .... [icode]from PF import PathFinder[/icode] That should do it | |
Re: Google is your friend... learn how to use it: [url]http://www-128.ibm.com/developerworks/db2/library/tutorials/db2linux/db2proj/updated/python_db2_interface.htm[/url] | |
Re: Yes. That would simply be a system call, which can be achieved by any number of Python methods (popen, os.system, subprocess, etc.). As far as the GUI portion that can also be achieved by any number of Python GUI toolkits (wxPython, Tkinter, EasyGUI, etc.) I refer you to [url=www.google.com]google[/url] for … | |
Re: For the "GUI" portion of your program you'll likely want to look into wxPython. There are a number of other toolkits for creating GUIs but wx is by far the most native-looking so it'll get you a very clean looking interface that looks like it was built right into Windows. … | |
Re: Yes zachabest is correct. There is nothing special about a csv file. It is literally a text file that uses commas to delimit, or separate, each data cell of a row. Each new line is therefore a new row. I've found that using the csv modules are overkill and it … | |
Re: Here's a snippet of a setup.py that I've used before to grab things that were not in the same directory... [code=python] setup( zipfile = None, # We use os.path.join for portability package_dir = {'': os.path.join('..', 'Common')}, py_modules = ['mylog', 'IPVerify', 'logfile', 'remoteCmd', 'rd_num_gen', 'crc32'], windows = [ { "script": "my_program.py", … | |
Re: Where does execution stop? Does it give an error? Where is this so-called "file open file print" statement? | |
Re: You can launch threads for each new process. Look into the Python subprocess module | |
Re: From the documentation [QUOTE]parse( source) Process an input source, producing SAX events. The source object can be a system identifier (a string identifying the input source - typically a file name or an URL), a file-like object, or an InputSource object. When parse() returns, the input is completely processed, and … | |
Re: Perhaps because you are appending to p1 instead of p ? although even after replacing p1 with p the list shows up empty... what exactly are you trying to do here? I have a feeling it's an indentation error... | |
Re: What have you tried so far? What errors are you running into? What are the rules of said game? | |
Re: I would suggest a nested for loop like: [code=python] for row in xrange( usr_row ): for col in xrange( usr_col ): ## Do my division and printing [/code] | |
Re: I have an inkling that he means reload a module... I too have looked around google and found some sites that insist that it can be done. I just don't know how. To further clarify when I'm developing a module and I import it into my python shell then realize … | |
Re: You're never reading your file... you're literally trying to read from sys.stdin. This is wrong and I think you may have misunderstood your prof. You need to open the file via [icode]open(file_name, mode)[/icode] ... then read from there.... | |
Re: ...Adding on to the previous posts; another thing to keep in mind is that EVERYTHING in python is an object; meaning that the file handle (object) has methods and members that facilitate line-by-line iteration. | |
Re: HEre's a few things to get you on your way... first iterating over a list of files using the os module: [code=python] import os my_dir = '/home/AtomData/' ## path example for Windows: ## my_dir = 'C:\\XXX\\AtomData\' my_files = os.listdir(mydir) for each_file in my_files: ## Here we will do our file … | |
Re: Paulthom also forgot to mention that the replace function has an optional third parameter that will help you greatly: [code=python] >>> t = 'I love cheese; cheese is my favorite thing to eat besides melted cheese.' >>> t.replace('cheese', 'Shredded Gorgonzola', 1) 'I love Shredded Gorgonzola; cheese is my favorite thing … | |
Re: Yes, what vegaseat suggested... either that or use an escape (a la \) before the text as in: [code=python] >>> t = '!,.?'$%' >>> print t !,.?'$% >>> [/code] Same thing goes for double quotes within double-quoted string: [code=python] >>> t = "!,.?\"$%" >>> print t !,.?"$% >>> [/code] | |
![]() | Re: This is similar to someone who wanted to use py2exe on linux ... both of these packages (py2app and py2exe) are somewhat misleading; as they do not simply build an executable 'version' of your program, they literally package your scripts together with an as-needed version of Python. |
Re: One of the values that you are trying to concatenate is an integer. You need to wrap it with str() in order to convert it and concatenate. Also, I don't see you performing any commit() operations. If you do not commit your transactions they will never be posted to the … | |
Re: Heh, whoops. Tequila makes you do funny things. | |
Re: To get a list of the contents of a folder you can use the os module's listdir() function. Then loop over each item, and use split('.') on the name to add '_output' before opening the output file. | |
Re: Your choice of words is very unique and I'm not sure that I understand your dilemma; but perhaps you are asking about passing parameters to a function that you've imported from another module? [code=python] import os os.listdir('/mydir/test/python') [/code] Like that. | |
Re: How about [icode]for num in data[0].split() + 1:[/icode] ? | |
Re: Yes, it is defined within the same class but not in the scope of the function OnRecognition. say() is a member of the ContextEvents class so you'll need to call it as self.say() | |
Re: I think your best bet would be to simply print the value of contents and then ask the user for a new value. Otherwise you will need to figure out how to prompt for input and then subsequently write to the stdin ... you can access stdin/stdout via sys.stdxxx, which … | |
Re: I don't really know what you mean by your explanation of what's happening but I presume that you would want to modify your code to use if/else if structures as such: [CODE=python] while mainloop == 0: for event in pygame.event.get(): if event.type == KEYDOWN: draw_level() if event.key == K_ESCAPE: sys.exit() … | |
Re: Your current problem is probably that the location of easy_install is not on your windows path. For now, try typing in the full path to where you installed easy_install as such: [icode]C:\\Program Files\\<etc, etc>\\easy_install speech[/icode]. | |
![]() | Re: You're missing a parenthesis at the end of the [icode] v = float(raw_input([/icode] line. Also [icode]close = print "thank you for using a George Lee program"[/icode] is incorrect syntax. You can either print that string or save it using [icode]close =[/icode]. Which is it? ![]() |
The End.