Gribouillis 1,391 Programming Explorer Team Colleague

I prefer design 1. We don't know anything about the algorithms that you have in mind, so there is no obvious benefit for introducing the classes Search and Edit. With design 1, the Library object appears as a facade behind which you can implement arbitrary complex and flexible algorithms.
Also I don't think it's a good idea to use nested classes in general. There is no gain in nesting classes and they will be more accessible if you implement them linearly at module level.

Gribouillis 1,391 Programming Explorer Team Colleague

I modified your code so that it prints the file name and the destination name instead of renaming the file. Run it and modifiy it until it gives the correct values, then uncomment the 'rename' line.

#!/usr/local/bin/python

import re, os

from os.path import join as pjoin, isdir
while True:
    targetfolder = raw_input('enter a target folder:\n').strip()
    if isdir(targetfolder):
        break
    else:
        print("you must enter an existing folder!")
newname = raw_input('enter a new base name:\n').strip()
newname = pjoin(targetfolder, newname)
rxin = raw_input('enter a regex to search for:\n').strip()
allowed_name = re.compile(rxin).match

a = 0
for fname in os.listdir(os.getcwd()):
    if allowed_name(fname):
        a += 1
        c = os.path.splitext(fname)
        b = (newname + str(a) + c[1])
        #os.rename(fname, b)
        print((fname, b))
Gribouillis 1,391 Programming Explorer Team Colleague

You can write

from os.path import join as pjoin
targetfolder = raw_input('enter a target folder:\n').strip()
newname = raw_input('enter a new base name:\n')
newname = pjoin(targetfolder, newname)
...

If you don't want to nuke your jpegs, make a zip file with the whole folder before doing anything.

Gribouillis 1,391 Programming Explorer Team Colleague

Google found a patch for scapy.all..sniff, wich allows to stop the sniff function programmatically. You should try this http://trac.secdev.org/scapy/wiki/PatchSelectStopperTimeout

Gribouillis 1,391 Programming Explorer Team Colleague

You can write

if colour in ("Green", "Blue", "Red"):
    ... # do something
else:
    ... # do something else
Gribouillis 1,391 Programming Explorer Team Colleague

Note that in python 3, there are no 'old style class', so that your class is implicitely a subclass of object. Here is a small experiment with python 2.6 and python 3.1

Python 2.6.4 (r264:75706, Oct 27 2009, 15:50:27) 
[GCC 4.4.1] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> class A:
...  pass
... 
>>> class B(object):
...  pass
... 
>>> import inspect
>>> inspect.getmro(A) # get the ancestor classes
(<class __main__.A at 0x7f3356d339b0>,)
>>> inspect.getmro(B)
(<class '__main__.B'>, <type 'object'>)
>>> dir(A)
['__doc__', '__module__']
>>> dir(B)
['__class__', '__delattr__', '__dict__', '__doc__', '__format__', '__getattribute__', '__hash__', '__init__', '__module__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__setattr__', '__sizeof__', '__str__', '__subclasshook__', '__weakref__']
>>> issubclass(A, object)
False
>>> a = A()
>>> isinstance(a, object) # look at this !
True
>>> issubclass(B, object)
True
Python 3.1.1 (r311:74480, Aug 17 2009, 21:52:39) 
[GCC 4.4.1] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> class A:
...  pass
... 
>>> class B(object):
...  pass
... 
>>> import inspect
>>> inspect.getmro(A)
(<class '__main__.A'>, <class 'object'>)
>>> inspect.getmro(B)
(<class '__main__.B'>, <class 'object'>)
>>> dir(A)
['__class__', '__delattr__', '__dict__', '__doc__', '__eq__', '__format__', '__ge__', '__getattribute__', '__gt__', '__hash__', '__init__', '__le__', '__lt__', '__module__', '__ne__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__setattr__', '__sizeof__', '__str__', '__subclasshook__', '__weakref__']
>>> dir(B)
['__class__', '__delattr__', '__dict__', '__doc__', '__eq__', '__format__', '__ge__', '__getattribute__', '__gt__', '__hash__', '__init__', '__le__', '__lt__', '__module__', '__ne__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__setattr__', '__sizeof__', '__str__', '__subclasshook__', '__weakref__']
>>> issubclass(A, object)
True
>>> a = A()
>>> isinstance(a, object)
True
Gribouillis 1,391 Programming Explorer Team Colleague

Is this what you mean ?

class Agent:
    def __init__(self):
        exec "import random" in self.__dict__


ag = Agent()

print ag.random.randrange(100) # this works

print random.randrange(100) # this doesn't work (random not in global namespace)

You could even make a class decorator

def withrandom(cls):
    exec "import random" in cls.__dict__
    return cls

@withrandom
class Agent:
    pass


ag = Agent()

or simpler

class Agent:
    import random
Gribouillis 1,391 Programming Explorer Team Colleague

It's true, suppose that mypackage contains __init__.py, foo.py, bar.py and baz.py. You could have this in __init__.py

import foo
import bar
import baz

then, if you simply write import mypackage , it will also import mypackage.foo, mypackage.bar and mypackage.baz.

lllllIllIlllI commented: Thanks +2
Gribouillis 1,391 Programming Explorer Team Colleague

I ran pylint on this code. Here is the result (note that this depends on my personal configuration of pylint, it may give a different result on your system)

$ pylint --disable-msg W0311 bad.py
************* Module bad
C:  6: Line too long (93/80)
C:  8: Line too long (132/80)
C: 12: Line too long (124/80)
C: 13: Line too long (107/80)
C: 14: Line too long (136/80)
C: 15: Line too long (138/80)
C: 16: Line too long (147/80)
C: 17: Line too long (84/80)
C:  1: Missing docstring
C:  1:findInterestRate: Invalid name "findInterestRate" (should match [a-z_][a-z0-9_]{2,30}$)
C:  1:findInterestRate: Missing docstring
C:  2:findInterestRate: Invalid name "loanAmount" (should match (?:(?:[a-k]|[m-z])|[a-z_][a-z_0-9]{2, 30}))
E:  6:findInterestRate: 2 is not callable
E:  7:findInterestRate: Using variable 'zeroFindings' before assignment
E:  9:findInterestRate: Using variable 'ziggy' before assignment
Exception RuntimeError: 'maximum recursion depth exceeded while calling a Python object' in <type 'exceptions.RuntimeError'> ignored
E: 14:findInterestRate: Undefined variable 'finderguess'


Report
======
12 statements analysed.

Duplication
-----------

+-------------------------+------+---------+-----------+
|                         |now   |previous |difference |
+=========================+======+=========+===========+
|nb duplicated lines      |0     |0        |=          |
+-------------------------+------+---------+-----------+
|percent duplicated lines |0.000 |0.000    |=          |
+-------------------------+------+---------+-----------+



Raw metrics
-----------

+----------+-------+------+---------+-----------+
|type      |number |%     |previous |difference |
+==========+=======+======+=========+===========+
|code      |16     |76.19 |16       |=          |
+----------+-------+------+---------+-----------+
|docstring |1      |4.76  |1        |=          |
+----------+-------+------+---------+-----------+
|comment   |1      |4.76  |1        |=          |
+----------+-------+------+---------+-----------+
|empty     |3      |14.29 |3        |=          |
+----------+-------+------+---------+-----------+



Statistics by type
------------------

+---------+-------+-----------+-----------+------------+---------+
|type     |number |old number |difference |%documented |%badname |
+=========+=======+===========+===========+============+=========+
|module   |1      |1          |=          |0.00        |0.00     |
+---------+-------+-----------+-----------+------------+---------+
|class    |0      |0          |=          |0.00        |0.00
lllllIllIlllI commented: Nice :) +2
Gribouillis 1,391 Programming Explorer Team Colleague

You can also use format

message = "z's value is {z:0{digits}n}".format(z=2, digits=3)
print(message)
Gribouillis 1,391 Programming Explorer Team Colleague

Here is a script which should work for 1 .pdb file and many .pd files. It creates the new versions of the .pd files (with less lines) in a separate output directory. See the usage at the end of the script. Let me know if it works !

#!/usr/bin/env python

import os
from os.path import join as pjoin, isdir, isfile

class DataFile(object):
    def __init__(self, path):
        self.path = path

    def lines(self):
        for line in open(self.path):
            yield line 

    @staticmethod
    def key(line):
        return tuple(line.strip().split()[2:6])

class Pdb(DataFile):

    def __init__(self, path):
        DataFile.__init__(self, path)
        self._keys = None

    def keys(self):
        if self._keys is None:
            self._keys = set(self.key(line) for line in open(self.path))
        return self._keys

class Pd(DataFile):
    def __init__(self, path):
        DataFile.__init__(self, path)

    def filtered_lines(self, pdb):
        keys = pdb.keys()
        for line in self.lines():
            if self.key(line) in keys:
                yield line

    def dump(self, output, pdb):
        for line in self.filtered_lines(pdb):
            output.write(line)
        output.close()

class Output(DataFile):
    def __init__(self, path):
        DataFile.__init__(self, path)
        self.ofile = open(path, "w")

    def write(self, *args):
        return self.ofile.write(*args)

    def close(self):
        self.ofile.close()

class Directory(object):
    def __init__(self, path):
        self.path = path

    def names(self, extension=None):
        for name in os.listdir(self.path):
            if extension is None or name.endswith(extension):
                yield name

def process(pdb_file, pd_dir, output_dir):
    if isdir(output_dir):
        raise ValueError("Output dir must not exist")
    else:
        os.mkdir(output_dir)
    pdb = Pdb(pdb_file)
    pd_dir = Directory(pd_dir)
    for name in pd_dir.names(".pd"):
        pd = Pd(pjoin(pd_dir.path, name))
        output = Output(pjoin(output_dir, name))
        pd.dump(output, pdb)

def main():
    from sys import argv
    pdb_file, pd_dir, output_dir = argv[-3:]
    if pdb_file.endswith(".pdb") and isfile(pdb_file) and isdir(pd_dir) and not isdir(output_dir):
        pass
    else:
        raise RuntimeError("""Usage:
<executable> pdb_file pd_dir output_dir
where
  * pdb_file: an existing .pdb …
chavanak commented: One person whom I was able to rely on!! +0
Gribouillis 1,391 Programming Explorer Team Colleague

When you open a file, the cursor is at the beginning of the file. However

myfile.seek(0)

goes to the beginning of the file. Finally, read this.

Gribouillis 1,391 Programming Explorer Team Colleague

Did you read this in your assingment ?

Although it can be helpful to you to discuss your program with other people, and that is a reasonable thing to do
and a good way to learn, the work you hand in must ultimately be your own work. This is essential for you to benefit
from the learning experience, and for the instructors and TAs to grade you fairly. Handing in work that is not your
original work, but is represented as such, is plagiarism and academic misconduct. Penalties for academic misconduct
are outlined in the university calendar.

I'm a teacher, so I can't approve. It's also against the law.

Gribouillis 1,391 Programming Explorer Team Colleague

Some time ago, I was writing a small command line interpreter, with the help of the standard module cmd which offers minimal support for such tasks, and I had the problem that this module doesn't have a function to parse a command line to produce a list argv which can be passed to optparse for example.
I found a first solution with the third party module pygments which contains parsers for different languages, and I parsed the command line using pygment's bash parser.
However, I was not completely happy with this solution and I started looking for the C function wich builds argv for C programs. I finally found such a function in GNU libiberty library which is used by the gcc compiler.
This function was simple enough and I decided to write a pure python implementation of this function, using python's regular expressions and following closely the syntax rules used in libiberty's buildargv function (a 100% compatibility is not at all guaranteed)
This snippet is the result of this coding. The class Argv, which subclasses list, contains methods to transform a command line into a list of arguments with a behaviour similar to that of a C compiler. It also contains methods to write the argument list to a response file and read files to extract command arguments.

Here is the code, enjoy :)

Gribouillis 1,391 Programming Explorer Team Colleague

In the applications menu of your desktop panel, you select install software (you need the root password for this). This opens the software manager, which has a search form. Put libtcl in the search form, then check the package you want in the list. Then click on the button at the bottom of the widget to install the package. Repeat with libtk.
Then go to your python source dir and try to rebuild.

Gribouillis 1,391 Programming Explorer Team Colleague

There are small differences, for example in Pickle, there are Pickler and Unpickler classes which don't exist in cPickle. Often such double implementations exist because someone first wrote a python module and then tried to speed it up by writing a C module. It's a good strategy, because the development time of the pure python version is usually small as compared to the C version.

Gribouillis 1,391 Programming Explorer Team Colleague

mmmm...ok. So you mean that if they call something a build-in type they just want to mention that it's something implemented in C but there are no types that you can only implement by using a C-Extension?

The standard library has examples of modules wich offer 2 implementations, a C and a pure python implementation. For example Pickle and cPickle and StringIO and cStringIO. For most problems, you can imagine a python implementation, but on certain problems, this python implemenation will be very inefficient. For example, if you're writing an editor which manipulates a document as a big tree of lightweight objects, a C implementation will be more efficient, because it's easier to define lightweight objects in C than in python.

Gribouillis 1,391 Programming Explorer Team Colleague

Hi, thanks for your reply. I understand that C will compute some things much faster but I was wondering about the fact that the python documentation says that you CAN NOT implement new build-in objects in python so you have to use C/C++ for that.

I think it's only the terminology of the python documentation. Usually the documentation opposes 'user defined types' implemented in python to 'built in types' implemented in C.

Gribouillis 1,391 Programming Explorer Team Colleague

There are many examples of extensions written in C or C++. For example the numpy package implements classes written in C.
One of the main reasons to do this is that C or C++ run much faster than python, especially in numerical analysis applications.

Gribouillis 1,391 Programming Explorer Team Colleague

It is done by the C runtime. The same code that calls main() collects arguments from the command shell and parses them into an array of char pointers.

I looked in the source code of gcc-4.3.2 and I found this

char **buildargv (const char *input)
{
  char *arg;
  char *copybuf;
  int squote = 0;
  int dquote = 0;
  int bsquote = 0;
  int argc = 0;
  int maxargc = 0;
  char **argv = NULL;
  char **nargv;

  if (input != NULL)
    {
      copybuf = (char *) alloca (strlen (input) + 1);
      /* Is a do{}while to always execute the loop once.  Always return an
	 argv, even for null strings.  See NOTES above, test case below. */
      do
	{
	  /* Pick off argv[argc] */
	  while (ISBLANK (*input))
	    {
	      input++;
	    }
	  if ((maxargc == 0) || (argc >= (maxargc - 1)))
	    {
	      /* argv needs initialization, or expansion */
	      if (argv == NULL)
		{
		  maxargc = INITIAL_MAXARGC;
		  nargv = (char **) malloc (maxargc * sizeof (char *));
		}
	      else
		{
		  maxargc *= 2;
		  nargv = (char **) realloc (argv, maxargc * sizeof (char *));
		}
	      if (nargv == NULL)
		{
		  if (argv != NULL)
		    {
		      freeargv (argv);
		      argv = NULL;
		    }
		  break;
		}
	      argv = nargv;
	      argv[argc] = NULL;
	    }
	  /* Begin scanning arg */
	  arg = copybuf;
	  while (*input != EOS)
	    {
	      if (ISSPACE (*input) && !squote && !dquote && !bsquote)
		{
		  break;
		}
	      else
		{
		  if (bsquote)
		    {
		      bsquote = 0;
		      *arg++ = *input;
		    }
		  else if (*input == '\\')
Gribouillis 1,391 Programming Explorer Team Colleague

try

string2 = "Random Number is\n{value:^16}".format(value = string1)

Also, you can read this :)

Gribouillis 1,391 Programming Explorer Team Colleague
Gribouillis 1,391 Programming Explorer Team Colleague

:) :) :) :) :) :) :)

Gribouillis 1,391 Programming Explorer Team Colleague

This should work

value = min(x for x in temperature_sequence if x > 0)

Note: this raises ValueError if all temperatures are below 0.

gdr969 commented: helped with resolving problem +0
Gribouillis 1,391 Programming Explorer Team Colleague

Displaying tabular data
Here we use format to display tabular data. Consider reusing the Tabular class in your programs, as shown in the function 'test_func' below

#!/usr/bin/env python
from os import linesep as LS 

class Tabular (object ):
  def __init__ (self ,columns ,gen_data ):
    """Create a Tabular object for formatting a table.
    @ columns is a sequence of triples (col_title, col_align, tpdata)
      - col_title is the string at the top of the column. It's width
        determines the column witdth
      - col align is "<" or "^" or ">"
      - tpdata is a template to format the column entries, with a single
        positional argument 0.
    @ gen_data is a generator of the table rows, which are tuples of
      values to be formatted.
    """
    self .gen_data =gen_data 
    columns =list (columns )
    self .titles =[]
    self .templates =[]
    for col_title ,col_align ,tpdata in columns :
      tpcell ="{{0:{a}{w}.{w}}}".format (a =col_align ,w =len (col_title ))
      self .titles .append (col_title )
      self .templates .append ((tpcell ,tpdata ))
    self .width =sum (len (x )for x in self .titles )+len (self .titles )-1 
  def delimit (self ,msg ):
    return "|"+msg +"|"
  def top (self ):
    return "-"*(self .width +2 )
  bottom =top 
  def lines (self ):
    yield self .top ()
    yield self .delimit ("|".join (self .titles ))
    yield self .top ()
    for line_data in self .gen_data :
      line =[]
      for (tpcell ,tpdata ),data in zip (self .templates ,line_data ):
        s =tpcell .format (tpdata .format (data ))
        line .append (s )
      yield self .delimit ("|".join (line ))
Gribouillis 1,391 Programming Explorer Team Colleague

Formatting specifications for datetime.datetime objects differ from the standard ones. They obey the rules of datetime.datetime.strftime. Each class can define its own formatting specifications through its __format__ method.

from datetime import datetime
import sys
from os.path import getmtime

python = sys.executable
pytime = datetime.fromtimestamp(getmtime(python))

print("My python executable was last modified on {t:%b %d %Y} at {t:%H:%M:%S}."
         .format(t=pytime))

"""my output ---->
My python executable was last modified on Aug 22 2009 at 18:38:31.
"""
Gribouillis 1,391 Programming Explorer Team Colleague

For complex templates, we can use functools.partial to set the value of some of the template's arguments. This results in a cleaner code

import functools
rabbits = {
    "flopsy" : 1.0/3, "mopsy" : 576.0/7, "cotton tail": .76/5, "peter": 300000.0/37,
}

template = "{name:{fill}<{namewidth}}{score:{fill}>20.{precision}f}"

nwidth = 1 + max(len(name) for name in rabbits)
# The use of functools.partial allows us to give a value to a subset of
# the template's arguments.
rabbit_line = functools.partial(template.format,
             namewidth = nwidth, precision = 2, fill="-")

for name in sorted(rabbits):
    print(rabbit_line(name = name, score = rabbits[name]))

"""my output ---->
cotton tail-----------------0.15
flopsy----------------------0.33
mopsy----------------------82.29
peter--------------------8108.11
"""
Gribouillis 1,391 Programming Explorer Team Colleague

The floating point precision can be passed as argument too

rabbits = {
    "flopsy" : 1.0/3, "mopsy" : 576.0/7, "cotton tail": .76/5, "peter": 300000.0/37,
}

nwidth = 1 + max(len(name) for name in rabbits)

for name in sorted(rabbits):
    # the floating point precision is passed as argument to format
    print("{name:{namewidth}}:{score:>10.{precision}f}".format(
          name = name, score = rabbits[name],
          namewidth = nwidth, precision = 2))

"""my output ---->
cotton tail :      0.15
flopsy      :      0.33
mopsy       :     82.29
peter       :   8108.11
"""
Gribouillis 1,391 Programming Explorer Team Colleague

Same example with computed field's width:

rabbits = {
    "flopsy" : 1.0/3, "mopsy" : 576.0/7, "cotton tail": .76/5, "peter": 300000.0/37,
}

nwidth = 1 + max(len(name) for name in rabbits)

for name in sorted(rabbits):
    # the name field's width is passed as argument to format
    print("{name:{namewidth}}:{score:>10.4f}".format(
          name = name, score = rabbits[name], namewidth = nwidth))

"""my output ---->
cotton tail :    0.1520
flopsy      :    0.3333
mopsy       :   82.2857
peter       : 8108.1081
"""
Gribouillis 1,391 Programming Explorer Team Colleague

The syntax of the str.format() method described in the python 2.6 documentation looks both powerful and complex. The idea of this thread is to start a collection of nice formatting examples which will ease the task of mastering this function. Please post useful examples, and document them :)

Gribouillis 1,391 Programming Explorer Team Colleague
s='1234444432187667890000568984'
print("\n".join(("I found %d: %d" % (s.count(str(i)), i)) for i in range(10)))
kiddo39 commented: Thanks! +1
Gribouillis 1,391 Programming Explorer Team Colleague

The following benchmark shows that, on my machine, split5() is about 22% faster than splitIt()

# benchmark for split5 and splitIt (tested with python 2.6)
from timeit import Timer
import re
split5 = re.compile(r".{1,5}", re.DOTALL).findall
splitIt =  lambda d, s: [s[i:i+d] for i in range(0, len(s), d)]

if __name__=='__main__':

    t = Timer("split5('Give me bacon and eggs, said the other man.')", "from __main__ import split5")
    print "split5: %.2f microsec"  % t.timeit()
    t = Timer("splitIt(5, 'Give me bacon and eggs, said the other man.')", "from __main__ import splitIt")
    print "splitIt: %.2f microsec"  % t.timeit()

""" my output --->
split5: 2.55 microsec
splitIt: 3.27 microsec
"""

however, you can gain 0.2 microsecs by replacing range with xrange in splitIt()

Gribouillis 1,391 Programming Explorer Team Colleague

I wrote a code snippet for this last year :)

Gribouillis 1,391 Programming Explorer Team Colleague

Ok, you can change the text with

l.setText(str(300)) # the text now shows 300

note that the letter 'l' is a bad variable name. Good variable names have at least 3 characters (except for loop variables like i, j, ...)
For the doc, you could use

from zellegraphics import *
from pydoc import HTMLDoc
textdoc = HTMLDoc()
out = open("mydoc.html", "w")
out.write(textdoc.document(Text))

to get a file mydoc.html to view in your browser.

About the floating point test, you should use if center >= 400.0 rather that == because floating point numbers seldom happen to be equal. If the value is 400.00000001, dx should be set to -10 for example.

A_Dubbs commented: Good help, very knowledgeable +1
Gribouillis 1,391 Programming Explorer Team Colleague

So what you're saying is that as the number of permutations exceeds the period of Python's random number generator, each index has an equally probable chance of being relocated to the beginning? Can you prove this?

I think I understand your point now. There must be a limitation somewhere when n is large. However, note that the builtin shuffle function takes an optional argument 'random' which allows you to pass your own random generator. So I still believe that the best method is to use shuffle, but for large n, write your own random generator with a larger period. An interesting point would be to have an order of magnitude of the n for which this problem arises.

Gribouillis 1,391 Programming Explorer Team Colleague

A shorter method

L1=[[1,2,3],[4,5,6],[7,8,9],[10,11,12]]
L2=[1,2,3,4,11]

set2 = set(L2)
L3 = [x for x in L1 if set2.intersection(x)]
print(L3)
Gribouillis 1,391 Programming Explorer Team Colleague

Strange how I missed that even after looking at the Python documentation for strings.

Type help(str) in a python console.

Gribouillis 1,391 Programming Explorer Team Colleague

thanks for the replies, but unfortunatly none of thee will work.

I am basically trying to return a function from a library if it is not in a class.

import random

class whatever(object):
    def __getattr___(self, name):
        if name not in locals():
            return random.name

you should use getattr(random, name)

scru commented: obviously +4
Gribouillis 1,391 Programming Explorer Team Colleague

I found the corresponding functionality for windows (you need the python for windows extensions)

from win32api import GetCursorPos as mousepos
if __name__ == "__main__":
    print("The mouse position on the screen is {0}".format(mousepos()))
Gribouillis 1,391 Programming Explorer Team Colleague

This snippet defines a function mousepos() which gets the mouse position on the screen. The package python-xlib is required.

Gribouillis 1,391 Programming Explorer Team Colleague

@@pythonuser18 For reference, I got it working, with python 2.6 and this last line:

print DecToRom(int(raw_input("Decimal number: ")))
pythonuser18 commented: Thanks a million! +0
Gribouillis 1,391 Programming Explorer Team Colleague

So what are objects all about anyway? It looks to me like a long complicated way of walling a function or method.

Objects are the result of years of programming practice by thousands of programmers. You need some time to see the benefit. The idea is that an object holds a certain quantity of data. The object's methods are functions which purpose is to manipulate the object's data.

Gribouillis 1,391 Programming Explorer Team Colleague

Another way

lucky = raw_input("Enter an integer: ")
x = "" if lucky.count("7") else "not "
print("%s is %slucky" %(lucky, x))
Gribouillis 1,391 Programming Explorer Team Colleague

You could use HTMLParser like this

import sys
if sys.version_info[0] < 3:
    from HTMLParser import HTMLParser
    from urllib2 import urlopen
else:
    from html.parser import HTMLParser
    from urllib.request import urlopen

class MyParser(HTMLParser):
    def __init__(self):
        HTMLParser.__init__(self)
        self.day = None

    def handle_starttag(self, tag, attrs):
        if tag == 'tr':
            for key, value in attrs:
                if key == 'colData0':
                    self.day = value

def get_day(url):
    parser = MyParser()
    html = urlopen(url).read().decode('utf8')
    parser.feed(html)
    parser.close()
    return parser.day

if __name__ == '__main__':
    print(get_day("http://www.mywebsite.com/py"))
Gribouillis 1,391 Programming Explorer Team Colleague

mylist[i] will raise IndexError unless i is in the interval [-n, n[ where n is the length of mylist. When the list is empty, this interval is empty. You could use

mylist.append("hello")
mylist.insert(0, "hello")
Gribouillis 1,391 Programming Explorer Team Colleague

It should be

def withdraw(self, amount):
        self.balance -= amount

There is no self.amount , only the paramater amount .

Gribouillis 1,391 Programming Explorer Team Colleague

I'm using an online dictionary. This is what I get for potato :)

Gribouillis 1,391 Programming Explorer Team Colleague

Can anyone explain the

x = (math.sqrt(8 * n + 1) - 1) / 2

triangular numbers have sums of consecutive ints like 1+2 = 3, 1+2+3=6 1+2+3+4 = 10 and ect.

A number n is triangular if it has the form n = k(k+1)/2. For example 1+2 = 2*3/2, 1+2+3=3*4/2, 1+2+3+4=4*5/2. If you compute 8n+1, you have 8n+1 = 4k(k+1)+1 = 4 k^2 + 4k + 1 = (2k+1)^2 , so this gives k = (sqrt(8n+1) -1)/2 .

Gribouillis 1,391 Programming Explorer Team Colleague

from __main__ import * must not be written if you want to write maintainable code. What you should do is first see if some parts of your program can be implemented as separate modules, at functional level (for example a module could be devoted to communicate with a database, etc). The variables that you have in __main__ could become members of a single object which could be passed to functions and classes in other modules.

Gribouillis 1,391 Programming Explorer Team Colleague

One way to do it is to use the subprocess module

import subprocess as SP
child_process = SP.Popen(["python", "t.py"])
Dixtosa commented: thx +0