880 Posted Topics
Re: It tok my 5sek to display result in notepad ctrl-c and ctrl-v. So to the question why to you need to display source code in notepad? Just to show you one normal to take out som info from a website. [CODE]from BeautifulSoup import BeautifulSoup import urllib url = urllib.urlopen('http://beans.itcarlow.ie/prices.html') soup … | |
Re: [QUOTE]The array module is kind of one of those things that you probably don't have a need for if you don't know why you would use it (and take note that I'm not trying to say that in a condescending manner!). Most of the time, the array module is used … | |
Re: Cgi script is limited and if we compare something like jboss,to same in python it will be one of the many [URL="http://wiki.python.org/moin/WebFrameworks"]Webframework[/URL] for python. Django is the most known. Some sites that run on django. [url]http://www.djangosites.org/[/url] web2py is also very simpel to setup,and is has a lot power. [url]http://www.youtube.com/watch?v=53DF4pkeriU[/url] | |
Re: Like this no need to use time or decimal module. Set upp notepad++ correct or use a good ide editor for python like pyscripter or pydev/SPE IDE [url]http://code.google.com/p/pyscripter/[/url] [url]http://pythonide.blogspot.com/[/url] [url]http://pydev.org/[/url] [CODE]hours = float(raw_input("Enter Hours Worked: ")) gross = hours * 12 netpay = gross - (gross * 0.15) print "Your … | |
Re: You can call file/help or other standar name in a menu,whatever you want. Here the standar way. [CODE]menubar = wx.MenuBar(wx.MB_DOCKABLE) file = wx.Menu() help = wx.Menu() menubar.Append(file, '&File') menubar.Append(help, '&Help')[/CODE] So say that you want other name. [CODE]menubar = wx.MenuBar(wx.MB_DOCKABLE) open_file = wx.Menu() super_help = wx.Menu() menubar.Append(open_file, '&open_File') menubar.Append(super_help, '&super_Help')[/CODE] | |
Re: Like this for python 3,there has been big changes in urllib for python 3(urllib + urllib2 joined together) But i guess you havent used python 2,so the changes dosen`t matter for you. [QUOTE]or is there a way to convert a .html file into a .txt file?[/QUOTE] The code under you … | |
Re: [QUOTE]now i can't figure out what does find+1 means..[/QUOTE] A clue most programming start indexing/counting at 0. [CODE]def find_genes(gene,sequence): my_list= [] #Dont use list as a varible name,it`s a python keyword count = sequence.count(gene) find = sequence.find(gene) my_list.append(find) while find > -1: find = sequence.find(gene,find+1) my_list.append(find) return count,my_list #Move to … | |
Re: Like this it can be done,did some changes in your code that generate a infinity loop. Read stiky post about GUI in top on this forum. [url]http://zetcode.com/[/url] [CODE]import wx class MyFrame(wx.Frame): '''info about class | doc_string''' def __init__(self, parent, mytitle, mysize): wx.Frame.__init__(self, parent, wx.ID_ANY, mytitle, size=mysize) #---|Window color|---# self.SetBackgroundColour('light blue') … | |
Re: [CODE]''' #part.txt This is a test. I want to take out this part,of the text ''' f = open('part.txt') #readlines() return a list,so string slice wont work [start:end] print f.readlines() #--> ['This is a test.\n', 'I want to take out this part,of the text'] f = open('part.txt') #read() return a … | |
Re: Some advice,and no [B]None[/B] as d5ed poinet out. [CODE] import string def read_book(file_in=None): ''' Read in file,strips whitespace-punctuation Returns them in lowercase ''' f = open(file_in) #r is default for l in f: #Dont need readlines book_line = l.strip().translate(None, string.punctuation) for w in book_line.split(" "): if w != "": print … | |
Re: This should help,and try to post some code next time. [CODE]>>> from __future__ import division >>> my_list = [1,2,3,4] >>> avg = sum(my_list)/len(my_list) >>> avg 2.5 >>> [/CODE] | |
Re: [QUOTE]python.org tell people to forget about python 2 and head into python 3.[/QUOTE] No they do not. [QUOTE]For example, python 3 only ship with a tkinter gui module which does not have great reputation. If I am to use python 3, I have no choice but to use tkinter.[/QUOTE] No,you … | |
Re: Just have both installed as most of us have. Wxpyhon is a excellent GUI toolkit use python 2.x if you need to use that. This topic has been upp several times,python is python dont get hang up in python 2.x or 3.x. If you are learning python 3.,you are learing … | |
Re: You have to some reading about dictionary. Some stuff you can look at. [CODE]>>> z = {'a': 1, 'a': 2, 'a': 3, 'b': 4, 'c': 5} >>> z {'a': 3, 'b': 4, 'c': 5} >>> #Keys are unique within a dictionary while values may not be >>> #That`s why only … | |
Re: A rule that can be good to remember. Never delete something from or add something to the list you are iterating over. The solution is: Iterate over your original list, and put everything which passes your test into another list. Some way of doing this. Using List comprehensions. [CODE]>>> l … | |
Re: [QUOTE]Getters and setters are evil. Evil, evil, I say! Python objects are not Java beans. Do not write getters and setters. This is what the ‘property’ built-in is for. And do not take that to mean that you should write getters and setters, and then wrap them in ‘property’. [/QUOTE] … | |
Re: An example i make some code that calulate average. [CODE] #average.py #saved in python dir from __future__ import division def average(average_list): if isinstance(average_list, (list, tuple)): return sum(average_list)/len(average_list) else: return 'Input can only be list or tuple'[/CODE] So import it and test it out. Save it in python sys.path or same … | |
Re: Use parser BeautifulSoup is good. [CODE]from BeautifulSoup import BeautifulSoup html = '''\ <a href="http://www.gumtree.sg/?ChangeLocation=Y" rel="nofollow">Singapore</a>, <a href="http://www.gumtree.com.au/?ChangeLocation=Y" rel="nofollow">Australia</a>, <a href="http://www.gumtree.co.nz/?ChangeLocation=Y" rel="nofollow">New Zealand</a>, <a href="http://www.gumtree.com" rel="nofollow">England</a>, <a href="http://edinburgh.gumtree.com" rel="nofollow">Scotland</a>, <a href="http://cardiff.gumtree.com" rel="nofollow">Wales</a>, <a href="http://www.gumtree.ie" rel="nofollow">Ireland</a>, <a> ''' soup = BeautifulSoup(html) links = soup.findAll('a', href=True) # find <a> with a defined href … | |
Re: Use code tags. A shorter way to average. [CODE]from __future__ import division def getAverage(numList): return sum(numList)/len(numList) print getAverage([1,2,3,4])[/CODE] | |
Re: Some pratice you can try out in IDLE. [CODE]>>> name = raw_input("Enter Name: ") Enter Name: joe >>> name 'joe' >>> list(name) ['j', 'o', 'e'] >>> #This is called list comprehension >>> [i for i in name] ['j', 'o', 'e'] >>> #Written as an ordenarry loop >>> my_list = [] … | |
Re: [QUOTE]ran it and I was so happy with it, but then... why on Earth does it tell me that "5" is higher than "24"? Or why does it tell me that "5" is higher than "45"?? Or why "5" is higher than "13"?[/QUOTE] Because you are comparing string and not … | |
Re: Something you can look that may help you. [CODE]>>> s = '13, 15, 19' >>> s = s.split(',') >>> s ['13', ' 15', ' 19'] >>> [float(item) for item in s] [13.0, 15.0, 19.0] >>> sum([float(item) for item in s]) 47.0 >>>[/CODE] | |
Re: [QUOTE]also i wud like to know about the differences between java and python[/QUOTE] Python vs java. [url]http://pythonconquerstheuniverse.wordpress.com/category/java-and-python/[/url] [QUOTE] Python is a programming language that lets you work more quickly and integrate your systems more effectively. You can learn to use Python and see almost immediate gains in productivity and lower … | |
Re: [CODE]>>> Z = [] >>> X=['a'] >>> Y=['b','c','d','e','f'] >>> for i in Y: ... Z.append(X[0] + i) ... >>> Z ['ab', 'ac', 'ad', 'ae', 'af'] >>> [/CODE] | |
Re: Try to some code something yourself next time,not just ask for a soultion. [CODE]b = 0 a = 5 while b < a: b = b + 1 print b #----------- for i in range(1,6): print i [/CODE] | |
Re: [URL="http://www.daniweb.com/forums/thread20774.html"]This site[/URL] [url="http://www.swaroopch.com/notes/Python"]Byte og python[/url] [url="http://en.wikibooks.org/wiki/Non-Programmer%27s_Tutorial_for_Python"]Non-Programmer's Tutorial for Python 2.6[/url] [url="http://www.tutorialspoint.com/python/index.htm"]Python tutorial[/url] Youtube. [url="http://www.youtube.com/watch?v=4Mf0h3HphEA&feature=channel"]Python basic[/url] [url="http://www.youtube.com/watch?v=RHvhfjVpSdE"]Wxpython[/url] [url="http://www.youtube.com/watch?v=0xgn-HKzZes"]Pygame[/url] [url="http://showmedo.com/"]Show me do[/url] | |
Re: When it comes to parseing xml/html regular expression is not the right tool. Use a parser python has several in standar libary or very good 3 party parser like beautifulsoup and lxml. [url]http://stackoverflow.com/questions/1732348/regex-match-open-tags-except-xhtml-self-contained-tags[/url] | |
Re: [QUOTE]I have to compare 5 words so writing a seperate for each combination would take too long[/QUOTE] I don see why you should use for loop like that for comparing 5 words. Give an example of the words,and how you think the comparing output should look. | |
Re: Another way that i think is ok to read. [CODE]import os filenames = os.listdir(os.curdir) for filename in filenames: for ext in ['txt', 'htm', 'html']: if filename.lower().endswith(ext): print filename [/CODE] | |
Re: I did write about exception handling(try,except)in this post you an look at. [url]http://www.daniweb.com/forums/thread245633.html[/url] | |
Re: Can you post score.dat or a sample of the file. Show what data from the file you will take out,and how the list should look. | |
Re: From looking at your code i have shorten it down. You have to many repating statement,that is not so good in programming. Have not read all rule off game,that you have to work on. [CODE]from random import randint def rollDice(): return [randint(1, 6) for r in range(6)] def user_input(): while … | |
Re: Python has 2 very good 3 party parser BeautifulSoup and lxml. This parser can handle html that is no good,this can be important. An example with BeautifulSoup. We want the price of beans from this site. [url]http://beans.itcarlow.ie/prices.html[/url] [CODE]from BeautifulSoup import BeautifulSoup import urllib2 #Read in website url = urllib2.urlopen('http://beans.itcarlow.ie/prices.html') soup … | |
Re: Your class look strange,dont see a point with __len__ and __str__ in that class. test = zoeken() #calling find method. print test.find(****) #Takes 4 ****arguments or 0 because of default arguments. Maybe is better you explain what you want to do? [B]print test(test2)[/B] You only return test2 in __str__ so … | |
Re: Dont use list as a variable name,it`s a python keyword [CODE]>>> list <type 'list'> >>> a = 'abc' >>> list(a) ['a', 'b', 'c'] [/CODE] Loop over the len() of my_sting,stop at 10 and use slice. [CODE]my_string = 'abcdefghijklmnopqrstuvxyzo' for i in range(len(my_string)): if i == 10: print my_string[:i] #--> abcdefghij … | |
Re: Here something you can look at. [CODE]class testClass(object): def __init__(self): self.my_dict = {} def uppdate(self,key, value): self.my_dict[key] = value def remove(self,del_key): del self.my_dict[del_key] def show_data(self): print self.my_dict if __name__ == '__main__': test = testClass() test.uppdate('a', 1) test.uppdate('b', 2) test.uppdate('c', 3) test.remove('c') test.show_data() #{'a': 1, 'b': 2}[/CODE] Something to think off … | |
Re: One way to make a deck,that is ok to understand. @askrabal remember that import statement shall never be inside a class or function. Import statement shall alway be on top. [CODE]rank = 'A23456789TJQK' suit = 'CDHS' deck = [] for r in list(rank): for s in list(suit): card = r … | |
Re: Make it an integer with int() [CODE]>>> paid_value = '55' >>> type(paid_value) <type 'str'> >>> paid_value = int(paid_value) >>> type(paid_value) <type 'int'> >>> paid_value ** 5 3025[/CODE] | |
Re: Drop all eval,where have you learn this very ugly way to convert string to integer? [CODE]>>> loan_str = raw_input ("Amount of loan:") Amount of loan:20000 >>> #make integer >>> loan = int(loan_str) >>> loan 20000 >>> type(loan) <type 'int'> >>> #Or make input return an integer >>> loan_str = int(raw_input … | |
Re: [QUOTE]In program 1 they are returned by an extra argument calles cls put in program to they are returned directly with the class name. Could someone please help me understand?[/QUOTE] Yes that what a class method do,did you read my link in the other post. [B]Vector2.from_points(A, C)[/B] here you call … | |
Re: [CODE] @classmethod def from_points(cls, P1, P2): return Vector2(P2[0] - P1[0], P2[1] - P1[1]) [/CODE] [url]http://docs.python.org/library/functions.html?highlight=classmethod#classmethod[/url] [QUOTE]could someone please explain the __add__ constructor to me? Thanks.[/QUOTE] [CODE] >>> x = 5 >>> y = 6 >>> x + y 11 >>> dir(x) ['__abs__', '__add__', '__and__', '__class__', '__cmp__', '__coerce__', '__delattr__', '__div__', '__divmod__', … | |
Re: Key in a dictionary can not use list. If you turn it around is ok,and set list as an value. [CODE]>>> codes = {} >>> inpt = raw_input("<<<").split() <<<my car >>> inpt ['my', 'car'] >>> inpt2 = raw_input("?...") ?...black >>> inpt2 'black' >>> codes[inpt] = inpt2 Traceback (most recent call … | |
Re: You dont define x,so then it will of course not have an value. [CODE]>>> x Traceback (most recent call last): File "<pyshell#0>", line 1, in <module> x NameError: name 'x' is not defined >>> def file2Function(): x = 5 print x file2Function() #5 [/CODE] [CODE]#file1.py def file2Function(): x = 5 … | |
Re: [QUOTE]Can't I just access the specific line without loading the file to memory?[/QUOTE] Yes use linecache from standard library. [CODE]'''--> l.txt line 1. line 2. line 3. ''' from linecache import getline #Notice linecache count from 1 print getline('l.txt', 2).strip() #--> line 2.[/CODE] | |
Re: [CODE]>>> xdate = 5 >>> print 'value of xdate is %d' % xdata value of xdate is 5[/CODE] String formatting operations has been in python since day one. [url]http://docs.python.org/library/stdtypes.html#string-formatting-operations[/url] A new and more powerful string formatting operations,from python 2.6--> [url]http://docs.python.org/library/string.html#format-examples[/url] [url]http://www.daniweb.com/code/snippet232375.html[/url] | |
Re: With [URL="http://ironpython.net/"]ironpython[/URL] you can write in python with full acess to the .NET Framework. For a python programmers this is very fine,dont have to learn a more verbose and static types language as C#. You have [URL="http://boo.codehaus.org/"]BOO[/URL] that use a python like way to write code. But are statically typed … | |
Re: Some hint You will not get finish code or so much help if you dont show some effort. [CODE]>>> a = 3 >>> if a <=5 and a >= 1: ... print 'Number are between 1 and 5' ... Number are between 1 and 5 >>> a = 7 >>> … | |
Re: Write takes string as argument,if x is a string is shall be ok. If not [B]''.join() [/B]as tony used be an option. Remeber to [B]close()[/B] the file,if not it will not write anything. [B]with open()[/B] close the file auto,as tony use over. [CODE]if x[0:4]=="ATOM": print type(x) #Check the type >>> … | |
Re: Look into dictionary. Example Banana is the [B]key[/B] and price is the [B]value[/B]. So if you call key you get price. Here is a code example you can look. This code will run in a loop given bye argument number given to function. And save result to a dictionary,that you … | |
Re: For log in use mechanize. for parsing are beautifulSoup or lxml good choice. [CODE]import mechanize browser = mechanize.Browser() browser.open(your_url) browser.select_form(nr=0) #Check form name nr=0 work for many browser['username'] = "xxxxx" browser['password'] = "xxxxx" response = browser.submit() html = response.read() print html[/CODE] |
The End.