True Division in Python
For a long time,, There has been a discussion of trueFor division versus integer division in Python. I myslef prefer that / be used for integer division since almost always, I want the result of the division be truncated to integer. However, today I reviewed the method to be used in Python to get true division, and this gave me an idea how true division could be implemented without interferring with the use of / for integer division. Use / for integer division single slash is the operator for integer division. Use ./ for true division. period slash is the operator for true division. Kermit < [EMAIL PROTECTED] > -- http://mail.python.org/mailman/listinfo/python-list
arrays in python
From: Kermit Rose Date: 02/10/06 17:36:34 To: [EMAIL PROTECTED] Subject: Arrays Hello. I want to write a program in python using integer arrays. I wish to calculate formulas using 200 digit integers. I could not find any documentation in python manual about declaring arrays. I searched the internet and found an example that said I must declare from Numeric import * and I downloaded a numerical python extension, but still have not found a way to declare an array of given length. The zeros function always gives me an error message. Kermit <[EMAIL PROTECTED] > -- http://mail.python.org/mailman/listinfo/python-list
Re: 2-dimensional data structures
From: anthonyberet Date: 02/18/06 17:11:01 To: python-list@python.org Subject: Re: 2-dimensional data structures I am not sure how to most efficiently identify which region any given square on the grid is actually in - any thoughts, for those that have done this? - I don't want a massive list of IF conditionals if I can avoid it. Kermit says: There is a MUCH more efficient way to do this! If you are doing a 9 by 9 soduku, Each row and column has exactly the digits 1 through 9 Think of your data as being in a list 81 cells long that is accessed 3 different ways. As a 9 by 9, row wise, As a 9 by 9 column wise As a 3 by 3 by 9 where the first two subscripts identify the region, and the last identify one of the 9 values within the region. And , most important, To search for a value not yet in a row, column, or region, Define holding list of length 9. Suppose a row has 4,2,8 in it, and the corresponding column has 2,3,1 in it and the correspond region has 2, 7, 6 in it. Then you initialize your hold vector to -1, and set hold(4) = 4 hold(2) = 1 hold(8) = 1 hold(2) = 1 hold (3) = 1 hold(1) = 1 hold(2) = 1 hold(7) = 1 hold(6) = 1 Then by scaning only the nine elements of hold, you see that hold(5) = -1, , and hold(9) = -1 are the only values not reset, and only 5 and 9 are possible choices for the target cell. -- http://mail.python.org/mailman/listinfo/python-list
Re: A question about reference in Python.
Chris Rebert wrote: On Sun, Dec 7, 2008 at 9:26 PM, Group <[EMAIL PROTECTED]> wrote: Hello, I'm studying algorithom. For concentrating on the question itself, I intend to use Python to implement the algorithoms. Now, I want to write a Red-Black Tree, and a List structure. In C/C++, I can use pointers to refer to children notes (or next notes). But, in Python, how can I do it? Except the sequence, I know not any way. You'd better help me understan how can I transform the following C code into Python: /* a List */ struct { int data; int *next; int *prev; } Possibly not an exact literal translation, but: class ListNode(object): def __init__(self, data, prev=None, next=None) self.data = data self.prev = prev self.next = next Keep in mind that Python uses call-by-object (google it), so it doesn't have pointers/references per-se. Cheers, Chris Yes, I see. Thank you, very much! -- http://mail.python.org/mailman/listinfo/python-list
How can I understan the "for" here?
Hello all, look at the following sentence: >>>params = {"server":"mpilgrim", "database":"master", "uid":"sa", "pwd":"secret"} >>> ["%s=%s" % (k, v) for k, v in params.items()] ['pwd=secret', 'database=master', 'uid=sa', 'server=mpilgrim'] I can't understand the second sentence because of the "for ... in". I consider that the syntactics of "for" should be: for k,v in params.items(): .. But there's no a colon here, why it can work? Thanks! -- http://mail.python.org/mailman/listinfo/python-list
Re: How can I understan the "for" here?
D'Arcy J.M. Cain wrote: On Thu, 11 Dec 2008 22:44:20 +0800 Kermit Mei <[EMAIL PROTECTED]> wrote: >>> ["%s=%s" % (k, v) for k, v in params.items()] ['pwd=secret', 'database=master', 'uid=sa', 'server=mpilgrim'] I can't understand the second sentence because of the "for ... in". I consider that the syntactics of "for" should be: for k,v in params.items(): I think you are binding ':' here too tightly to the 'for' construct in your mind. The for construct is "for in " which can be used as : in block constructs. The above is not a block construct but a list comprehension so it follows different rules. Yes, I see. Thanks you all. -- http://mail.python.org/mailman/listinfo/python-list
Re: Python interface problem with Windows
On 6/26/2010 5:27 PM, Robert Kern wrote: I do not provide Python support in private email. Please try the python-list: http://mail.python.org/mailman/listinfo/python-list or its USENET gateway: news:comp.lang.python On Sat, Jun 26, 2010 at 16:23, Kermit Rose wrote: Hello Robert. I had a friend download Python in order that he run a python program that I had written. After he had Python 2.6 installed, I copied my Python program to his c-drive. I then opened idle, and then new window. I then used the windows copy and paste to copy the source code into the new window. I then attempted to save the new code. It responded that the Python26 folder did not have permission for me to save the file. It seems to be a problem with the operating system interface with the installation of Python2.6 We checked the properties of the Python26 folder. It had the read only property set. We unchecked the read only property, and then clicked "apply". Then when we checked properties of Python26 folder again, it still had the read only property set. Can you help us? We would like to be able to create and run Python programs on his computer. Kermit Rose -- http://mail.python.org/mailman/listinfo/python-list
2. Re: Python interface problem with Windows (Benjamin Kaplan)
On 6/26/2010 9:30 PM, python-list-requ...@python.org wrote: Today's Topics: 2. Re: Python interface problem with Windows (Benjamin Kaplan) > Message: 2 > Date: Sat, 26 Jun 2010 16:26:47 -0700 > From: Benjamin Kaplan > To: python-list@python.org > Subject: Re: Python interface problem with Windows > Message-ID: > > Content-Type: text/plain; charset=ISO-8859-1 > You don't need to save scripts in C:\Python26. In fact, doing so is a > very bad idea. You should keep the libraries in one place (that would > be C:\Python26 ) and all of your individual programs somewhere else > (like your Documents folder) where you don't have to worry if two > different projects have files of the same name. For security reasons, > you don't have permission to write to the whole hard drive, just your > home directory. I do not know how to import code from any other directory than the default, C:\Python26. And how do I save the code in a different directory than the default, c:\Python26? Kermit Rose -- http://mail.python.org/mailman/listinfo/python-list
Why can't I run this test class?
Dear all, I'm a newbie for python, and I write a program to test how to implement a class: #!/usr/bin/env python class Test: 'My Test class' def __init__(self): self.arg1 = 1 def first(self): return self.arg1 t1 = Test print t1.first() # But when I run it, some errors take: $ ./Test.py Traceback (most recent call last): File "./Test.py", line 13, in print t1.first() TypeError: unbound method first() must be called with Test instance as first argument (got nothing instead) What I want is as the following c++ code do: class Test { public: Test() :arg1(1) {/*Do nothing*/} int first() const { return arg1; } protected: int arg1; }; void main(void) { Test t1; std::cout << t1.first; } Hope your help. Thanks Kermit -- http://mail.python.org/mailman/listinfo/python-list
Re: Why can't I run this test class?
On Fri, 2009-09-11 at 00:33 -0700, Chris Rebert wrote: > On Fri, Sep 11, 2009 at 12:30 AM, Kermit Mei wrote: > > Dear all, > >I'm a newbie for python, and I write a program to test how to > > implement a class: > > > > #!/usr/bin/env > > python > > > > class Test: > >'My Test class' > >def __init__(self): > >self.arg1 = 1 > > > >def first(self): > >return self.arg1 > > > > t1 = Test > > You missed the parentheses to call the constructor. That line should be: > > t1 = Test() > > Cheers, > Chris Yes, that can run. But If I put the following code into Test.py : #!/usr/bin/env python |>>> | class Test: | 'My Test class' | def __init__(self): | self.arg1 = 1 | | def first(self):| return self.arg1| | def setFirst(self,value = 5): | self.arg1 = value But when I want to run it as a module, something also be wrong: $ python Python 2.6.2 (release26-maint, Apr 19 2009, 01:56:41) [GCC 4.3.3] on linux2 Type "help", "copyright", "credits" or "license" for more information. >>> import Test >>> t1 = Test() Traceback (most recent call last): File "", line 1, in TypeError: 'module' object is not callable >>> Thanks Kermit -- http://mail.python.org/mailman/listinfo/python-list
Re: Why can't I run this test class?
On Fri, 2009-09-11 at 00:43 -0700, Chris Rebert wrote: > On Fri, Sep 11, 2009 at 12:40 AM, Kermit Mei wrote: > > On Fri, 2009-09-11 at 00:33 -0700, Chris Rebert wrote: > >> On Fri, Sep 11, 2009 at 12:30 AM, Kermit Mei wrote: > >> > Dear all, > >> >I'm a newbie for python, and I write a program to test how to > >> > implement a class: > >> > > >> > #!/usr/bin/env > >> > python > >> > > >> > class Test: > >> >'My Test class' > >> >def __init__(self): > >> >self.arg1 = 1 > >> > > >> >def first(self): > >> >return self.arg1 > >> > > >> > t1 = Test > >> > >> You missed the parentheses to call the constructor. That line should be: > >> > >> t1 = Test() > >> > >> Cheers, > >> Chris > > > > > > Yes, that can run. But If I put the following code into Test.py : > > #!/usr/bin/env python |>>> > >| > > class Test: | > >'My Test class' | > >def __init__(self): | > >self.arg1 = 1 | > >| > >def first(self):| > >return self.arg1| > >| > >def setFirst(self,value = 5): | > >self.arg1 = value > > > > But when I want to run it as a module, something also be wrong: > > > > $ python > > Python 2.6.2 (release26-maint, Apr 19 2009, 01:56:41) > > [GCC 4.3.3] on linux2 > > Type "help", "copyright", "credits" or "license" for more information. > >>>> import Test > >>>> t1 = Test() > > Traceback (most recent call last): > > File "", line 1, in > > TypeError: 'module' object is not callable > > You've imported the module `Test`, whose name is determined by the > filename (Test.py). > To access the class of the same name (`Test`) that is defined in the > module, you need to use the dot operator: > > >>>> import Test > >>>> t1 = Test.Test() > > You should probably use different names for the module/file and the > class to avoid confusion. > Unlike Java, Python does not observe a direct correspondence between > filenames and classes. > > Cheers, > Chris > -- Oh, yep! Thanks, Cheers. Can you tell me how can I write __init__.py for modules: I have a directory like this: $ tree . `-- main |-- MyTestModules | |-- Test1.py | `-- Test2.py `-- main.py 2 directories, 3 files In main.py, I want to run the following code: #!/usr/bin/env python import MyTestModules t1 = Test1() t2 = Test2() print t1.first() print t2.first() ### The classes Test1 and Test2 just be similar with the Test that I showed before. To run main.py correct, how can I orgnize the code under directory MyTestModules. (May need a __init__.py file under MyTestModules, but I don't know how to write it) Thank you,very much! Regards Kermit -- http://mail.python.org/mailman/listinfo/python-list
How can I use my modules here?
Hello community! I write a modules for testing, and my code is like this(under Linux): $ tree . |-- MyTestModules | |-- Test1.py | |-- Test2.py | `-- __init__.py `-- main.py 1 directory, 4 files $ find . -name '*.py' -print0|xargs -0 cat main.py Begin ## #!/usr/bin/env python from MyTestModules import Test1,Test2 t1 = Test1() t2 = Test2() print t1.first() print t2.first() main.py End ### #Test1.py Begin ## #!/usr/bin/env python class Test1: def first(self): return self.arg1 #Test1.py End #Test1.py Begin ## #!/usr/bin/env python class Test2: def first(self): return self.arg1 #Test1.py End ## __init__.py Begin #!/usr/bin/env python ## __init__.py End When I run the main.py, the following error takes: $ ./main.py from: can't read /var/mail/MyTestModules ./main.py: line 7: syntax error near unexpected token `(' ./main.py: line 7: `t1 = Test1()' Waiting for your help. Thanks. Best Regards Kermit Mei -- http://mail.python.org/mailman/listinfo/python-list
Re: How can I use my modules here?
On Fri, 2009-09-11 at 07:48 -0400, Albert Hopkins wrote: > On Fri, 2009-09-11 at 02:29 -0700, Chris Rebert wrote: > > For some reason, your Python program is being executed by bash as if > > it were a shell script, which it's not. > > No idea what the cause is though. > > Because the first 2 bytes of the file need to be #!/path/to/interpreter, > the OP has: > > main.py Begin ## > > This won't work. Yep, Thanks! -- http://mail.python.org/mailman/listinfo/python-list