118 Posted Topics
Re: Couple of problems. The first is poor class implementation. It's OK to make your class without attribute getters and setters. You can access instance attributes by referring to the attributes themselves. [code]class Car: def __init__(self, year): self.year = year mycar = Car(1999) mycar.year[/code] The second problem with implementation is that … | |
Re: Typecasting the numbers to a string doesn't make much sense to me, but whatever. You are mistakenly using the sample function. Here's what you want to do. [code]import random def make_secret(numdigits): a = [] for i in range(numdigits): a.append(str(random.randrange(0, 10))) return a[/code] | |
Re: It's bad because it isn't friendly to other objects that act like your class. You should use hasattr instead to check that the object you are working with is appropriate. That way as long as the object has the required attribute or method it will work with your code as … | |
Re: [url]http://github.com/pepijndevos/PyMouse[/url] Automated mouse control is a somewhat esoteric programming feature. You may have to experiment, research, and refactor other code to get a solution. | |
Re: If you don't mind me asking, what are the "ircbot" and "irclib" modules or packages you imported, and where could we acquire them or view the source? | |
Re: A technical term for creating a class is 'declare'. When you create a class you are declaring a class, and when you see a class in code it is a class declaration. Just thought you might want to know the lingo. b | |
Re: Seems to me like this would be a great opportunity to apply and use object oriented programming. A card is a type of object. What I would try is to make a card class with rank and suit attributes. Then I'd add comparison methods to compare card rank. (I'm assuming … | |
Re: Are you sure you want to use bubble sort? [url]http://en.wikipedia.org/wiki/Bubble_sort#Performance[/url] According to the Wikipedia article it is one of the worst sorting algorithms. | |
![]() | Re: [url]http://www.devshed.com/c/a/Python/Object-Oriented-Programming-With-Python-part-1/[/url] |
Re: Try relative imports. [url]http://www.python.org/dev/peps/pep-0328/#guido-s-decision[/url] Seem like just the thing. I've used them. They permit you to import modules from a relative parent package. They're hard to figure out though. The documentation is somewhat lacking so you might have to tinker until you get it right. | |
Re: I think the humane treatment of animals - insofar as we should not cause unnecessary distress or pain - is a reasonable standard for law, but otherwise I'm a proponent for the use of animal resources to support and advance human lives. But no to human rights for animals. Basically … | |
Re: [code]import configparser import os import sys config = configparser.ConfigParser() #If the script is executed __name__ will be '__main__' and sys.argv[0] will be the full path of the script. if __name__ == '__main__': path = os.path.dirname(sys.argv[0]) #Else the script was imported as a module and it has a __file__ attribute that … | |
Re: To express my opinion frankly, the instructions are stupid. I'll explain why later. However, let me see if I can help. We're looking at Problem 2? In this problem you are implementing a custom container class to hold shape objects. The main purposes of this container are to ensure that … | |
I know that certain functions will check the script's or module's current directory for the file if no path is specified. [code=PYTHON]open('test.txt')[/code] If that script is run and test.txt is in the same directory that will open the correct file. However, if I try to import that module it doesn't … | |
Re: Try using decorators. They are a language feature in at least Python 3.0 that perform code on a function or method defined after them. Optionally, they can return a new function that will be assigned to the newly defined function. There are function decorators and class decorators. I haven't studied … | |
Re: I'd answer number 1 a little differently than Gribouillis. Technically everything under the hood is entirely object oriented and you can certainly write pure object oriented programs in Python, but it is also possible to write programs in a purely functional style as well. As for number 5? [quote]You can … | |
Re: First, Python doesn't use semicolons to terminate lines of code. P.S. Someone beat me to it, but maybe I can help a little more. A fairly quick way to accomplish what you wish to accomplish is to create a set of the string and then check the length of that. … | |
U.S. Military Weapons Inscribed With Secret 'Jesus' Bible Codes Pentagon Supplier for Rifle Sights Says It Has 'Always' Added New Testament References [URL="http://abcnews.go.com/Blotter/us-military-weapons-inscribed-secret-jesus-bible-codes/story?id=9575794"]http://abcnews.go.com/Blotter/us-military-weapons-inscribed-secret-jesus-bible-codes/story?id=9575794[/URL] I saw this. I think this is wrong. | |
Re: Gribouillis makes some good suggestions. As for storing the data to a file there are really two standard, commonplace options that I think are suited to this. I'd either pickle or marshal the object. pickle and marshal can store and retrieve a Python object on file. Both are similar, but … | |
I'm having a problem with 'os.path.getmtime'. I've been doing some research into generating '.pyc' files and I am able to retrieve the source modification time (in seconds) as recorded in the '.pyc' file. It is merely the second set of four bytes unpacked as a long integer. [code=Python] import random, … | |
![]() | Re: That's going to be a tougher one if I think you are asking for a procedure that can work on lists with mixed items in them down to an arbitrary number of levels. In a real application you could probably find a better way of representing the data and use … |
I was wondering if anyone knows of any resources about building a custom importer. I've found this: [url]http://www.doughellmann.com/PyMOTW/sys/imports.html[/url]. I am also aware of PEP 302 and PEP 369 and imp and importlib. Are there any other resources I might need? | |
Re: Object oriented programming has some advantages in virtually all situations, even if functional programming is sometimes easier to write. That's why I generally tend to encourage its use over functional programming, but make no mistake that a poorly conceptually designed program will be harder to write using object oriented programming … | |
I've seen these in documentation, namely in the "importlib" documentation for Python 3.1, and I'm wondering what the difference is between a "universal newline" and a "standard newline", if any. I know it has to do with the byte sequence that is used to represent newlines, and I think universal … | |
Re: If you find a solution to one of your own problems, it is considered polite to post the solution regardless because other people might also have the same problem. As for my solution to the problem statement? I'd probably just strip the string of any non-alphabetic characters excepting spaces and … | |
Re: The Timer executes the code in a new namespace. This namespace doesn't know lista and listb because they are not in the name space. If you create a module that declares and sets these variables, you could then import it using the Timer's setup statement. (If you need the cell … | |
I'm using the code module to add an interactive interpreter to an object. This object has an interact method that when called creates an interactive interpreter with the object's dictionary as the namespace for the interpreter. When done interacting with the object, I want the "exit" function to return execution … | |
Re: Check out the "code" module. It has functions to emulate a Python interpreter. | |
I'm just wondering if it is possible to import a module as an object attribute. The basic description of what I want to accomplish is that I'm creating a software agent object. This agent will have a set of abilities (functions), but I don't know what these are ahead of … | |
Re: Without supporting or opposing cloud computing and cloud operating and software services, the statement that desktop computers will always exist for resource intensive applications may not be true. The primary factor preventing centralized, networked systems from providing resource intensive services is data transfer width and rate. With modern operating systems … | |
And I can't get anything I write to work. I'm so far off course I think I just need a working example of import hooks in action. I've looked for examples on the Internet, but they only show what looks like correct code. They never show that code in action. … | |
(A quine is a program that produces its own source as its output, typically to a file.) [CODE=Python]import sys ifile = sys.argv[0] counter = 0 istream = open(ifile) source = istream.readlines() source[2] = 'counter = ' + str(counter + 1) + '\n' if counter == 0: original = str(ifile) source.insert(5, … | |
Re: Well. According to the inductive reasoning involving the first law of thermodynamics, we can be fairly confident that the universe has existed in some form or another for eternity. It just may have been in a form we cannot currently learn about or understand. | |
I'm thinking about writing a small command line directory management program in Python. The most basic functionality I want is to be able to compare the the files in a directory and sub-directory, and if there are any duplicates then delete the lower most duplicate. I all ready have a … | |
Re: Edit: I'm sorry. I didn't realize that you wanted the button to set up a condition so that if another button is pressed something would be executed, or do you just want to check which button called the function? | |
Re: 1) Thread titles should be descriptive. A generic request for help is not descriptive. 2) This appears to be homework. Part of homework is to take the initiative to learn for yourself. That means you must complete the essential portions of the assignment for yourself. No one will do it … | |
Lately I've been writing a lot of code involving the creation of new objects, and I quickly come to find that Python almost always creates a reference to an object instead of a new object. I understand the importance of conserving memory usage, but in this instance I actually need … | |
Re: The class if functionally correct. A few things I'd like to suggest. 1) As far as I'm aware, protected names usually begin with one underscore. [CODE]self._name #"Correct" self.__name #"Incorrect"[/CODE] 2) I'd probably put an underscore in front of "get_name" and "set_name" and use the property to handle attribute access. 3) … | |
Re: Whitespace is recommended to make Python code clearer. Traditionally there is no space between related lines of code. A single line is recommended between different code sections and function definitions. Two lines are recommended between class definitions and I use two lines in method declarations if the methods are different … | |
Re: [QUOTE=mahela007;1062614] So how, then, does Tk manage to provide windows which look 'native' for the different operating systems? [/QUOTE] I don't know the way it technically does it, but my best guess is that it uses an API - application programming interface - to standardize how it draws widgets without … | |
I'm wondering if it's possible to change the border color of a tkinter entry widget border. I don't think it is, because no such option exists in relevant documentation. You can change the width, but that just results in the whitespace being recessed - creating a raised 3d border. If … | |
Re: If it interests you, I think that the code for Python's IDLE is available in the Python library. If it is, you can edit it to your liking. | |
Re: There are other options as well. Make both functions methods of a class, and use class attributes to store relevant data. Combine both functions into one. | |
Re: I remember watching a Stargate SG-1 episode called "Torment of Tantalus", and SG-1 find an ancient repository of information created by a council of allied alien races. The first page was a glossary containing basic definitions for the language in the repository, and it was a 3d hologram of the … | |
Re: I'm just wondering why the Library class has a container for book information. Is the book information a temporary container? | |
Re: For Christmas? The meal usually. Next year I'm thinking about celebrating Hanukkah, but I'll have to learn about it first. | |
Hello. I'm lrh9, a new user. This is my first time visiting these forums. I've posted on [url]www.programmingforums.org[/url] under the same user name. I'm a hobbyist and amateur computer programmer. My primary languages are C++, a custom scripting language for Windows named AutoHotkey, and Python which I've just started out … | |
| |
Re: According to your code, you are replacing all periods with spaces. If you want to replace the dots with absolutely nothing, then use two quotes side by side. "" However, as for the script itself. Seems to me if you moved the outfile.write() outside of the loop, and changed it … | |
Re: [QUOTE=gunbuster363;1046880]I do this: link = soup.find(attrs={'class' : re.compile("util2$")}) print dir(link) what appear is: OMG[/QUOTE] You can import the module pprint, and using the function pprint, you can display each function and variable on its own line. [CODE]from pprint import pprint link = soup.find(attrs={'class':re.compile("util2$")}) pprint(dir(link))[/CODE] |
The End.