jlm699 320 Veteran Poster

It is impossible to answer your question without more information. If you are implementing a CGI HTTP server you should reference the following module, which may already provide some of the functions you're looking for:
http://docs.python.org/library/cgihttpserver.html#module-CGIHTTPServer

jlm699 320 Veteran Poster

You could create your own class based on dictionary that checks whether that key already exists in __setattr__ and then add the value to a list/tuple. When you do your get() you can then use isinstance to act accordingly.

jlm699 320 Veteran Poster

Go through the input file line by line writing to an output file - using either regular expression or a simple if 'dtp' in line: expression, which when true skips writing the line to the output file.

jlm699 320 Veteran Poster

Please pardon my lack of networking knowledge.

I am trying to figure out how to setup routing tables to connect to machines that are on a subnet on a different network.

My current machine is sitting on a network, we'll call it Net1: My IP is 10.44.76.X (subnet 255.255.254.0 - gateway 10.44.76.1)

There is a "test" network we'll call Net2. I can ping any machine on Net2, example IP is 10.42.227.48 or 10.42.227.22. Connected to those machines are the machines on the "subnet", we'll call Sub1. These ip's for example are 11.1.97.5, 11.1.99.20.

Is there a way to directly access the Sub1 machines from my machine on Net1 ? I thought perhaps setting up my routing table would be the ticket - but I can't seem to successfully add any entries.

Any suggestions on how to do this, and if routing is the proper way - what do I need to figure out to properly route the traffic? Normally I have to ssh from my Net1 machine to the Net2 machines and then from there ssh to the Sub1 machines.

jlm699 320 Veteran Poster

Yes, use list methods and comprehension.

jlm699 320 Veteran Poster

Use the button_object.config(text=my_text) option. Source

jlm699 320 Veteran Poster

I'm sorry to hear that you have to work with Jython. My condolences. That being said, why don't you insert a print type(strTest) to see if it's actually being presented as a string.

jlm699 320 Veteran Poster

I can't seem to work out the scope of the variables so that they can be seen in the drawline function

The things in drawline that are referenced as being members of self are defined in __init__ without being a member of anything (ie they are objects that are created and subsequently destroyed in the scope of that function only). In order to make them members of self, define them as such!

So:

# panel2 = NavCanvas.NavCanvas(...etc...) 
# Should be:
self.panel2 = NavCanvas.blahblahblah
        # x0=12
        # x1=23
        # y0=13
        # y1=26
# Should be:
        self.x0 = 12
        self.x1 = 23
        self.y0 = 13
        self.y1 = 26

I hope that clears it up.

jlm699 320 Veteran Poster

It would help to know what GUI toolkit you're working with; however a good method (in wx) would be to specify the ID of the window, and then check to see if that ID is already being used or not as the basis of opening the window.

jlm699 320 Veteran Poster

Does anyone have information that will sharpen my python programming knowledge very quickly with some get straight to the point programming tutorials and information?

I suggest reading the book entitled Dive into Python, available for free here

EDIT: Also, it would benefit you to make use of the search capability of this forum and look for all those before you who have asked the same questions. You should really try to make sure a question hasn't already been answered (especially as often as this question gets asked) before posting a new thread, not to mention multiple threads with the same question

jlm699 320 Veteran Poster

sorry.I saw links but i can not solve my problem yet.
...
.but how can i do it?

You should really follow those links that woooee provided. For this type of task you should be either using pexpect or subprocess PIPES, not os.system .

jlm699 320 Veteran Poster

Looks fine to me. Can anyone tell what the problem is?

You never defined t

jlm699 320 Veteran Poster

It depends on how you wrote your scripts; however theoretically, yes it will work. The only thing that would stop it from working is if you used system-specific commands and used a lot of hard-coding.

wxPython is a very cross-platform capable toolkit. I can't speak for biopython.

jlm699 320 Veteran Poster

How about this:

>>> a = [[1,2,3], [4,5,6], [7,8,9]]
>>> new_a = []
>>> for each_a in a:
...     new_a += each_a
...     
>>> new_a
[1, 2, 3, 4, 5, 6, 7, 8, 9]
>>>
jlm699 320 Veteran Poster

Are you asking if your program will work in Unix, or are you literally asking how to transfer a file from one computer to another?

jlm699 320 Veteran Poster

Yes, the hyperlink is present , but I don't to show all the coding behind the hyperlink.
I want to say 'Click Here' and then the 'here' has the hyperlink.

Yes, the html for that would be:

Click <a href="http://www.google.com">Here</a>

However, in order for the hyperlink to display correctly you not only need a program or client that is capable of rendering HTML, you need to make sure you're creating proper HTML code. There are a number of ways to achieve this:

1) Save the output to an html file (.htm, .html) and open that file in a web browser (Internet Explorer, Firefox, Chrome)
2) Send the output in an email using something like SMTP library
3) Implement a GUI (like wxPython) that contains a rich text rendering module capable of displaying HTML

All of the above methods however require proper HTML formatting and syntax. In many cases you want to make sure you're inserting the proper headers, etc.

jlm699 320 Veteran Poster

From your first post, you've already indicated that you inserted the hyperlink into the email message

jlm699 320 Veteran Poster

In order to see a hyperlink you would need something that was capable of rendering HTML. Why don't you try opening the output from Python in your web browser

jlm699 320 Veteran Poster

Maybe it's four 8-bit numbers together?

>>> i = '100010101001000010000011111001010'
>>> first_num = int(i[:8],2)
>>> secnd_num = int(i[8:16],2)
>>> third_num = int(i[16:24],2)
>>> fourt_num = int(i[24:32],2)
>>> first_num
138
>>> secnd_num
144
>>> third_num
131
>>> fourt_num
229
>>>
jlm699 320 Veteran Poster

Are you describing a "race condition"? If you are working with something like this you are not a newbie, this is complex stuff.

try googling "data race condition" and learn about the different methods to solve this fundamental software design issue.

jlm699 320 Veteran Poster

The backslash (\) is used for an "escape character". So \t is the escape character for a tab, \n is for newline, and \" or \' are escapes for a double and single quote respectively.

Let's say we have a string: 'Hi my name is Charlie' . The single quotes are what tells the interpreter that every character in between is part of the string. Now if we wanted to use a contraction inside that string and change it to: 'Hi my name's Charlie' , you can see that the string (ie, what's inside the quotes) is actually only 'Hi my name' and the rest is attempted to be interpreted to Python code.

To solve this there are two methods:

1) Change the type of quotation marks that are used on the string, so we would instead have: "Hi my name's Charlie" 2) Escape the "inner" quotation so that it is interpreted as the character instead of the syntax marker: 'Hi my name\'s Charlie' HTH

jlm699 320 Veteran Poster

y will always be equal to y. I think you meant if y == 'y' . Like wise for the not equals (note you can also use != ). You really don't need to have another clause there, because if y is not equal to the character 'y' you already know it's not equal so you don't need to specifically check again. You can simply use if y == 'y' and else .

Finally, you made a class. That's not the way you're supposed to use a class, you should have defined a function.

jlm699 320 Veteran Poster

You could do a simple while loop:

import subprocess

repeat = 'y'
while repeat == 'y':
    subprocess.call(["programname"])
    repeat = raw_input("Would you like to run again? (y/n) ")
jlm699 320 Veteran Poster

Just for the record.
Do not use window path strings as path strings.
...
The ways to do this:
os.path.join("c:", "Process new","RF","test.dat")

Slate,

Just a minor correction in your example. For some reason on Windows the os.path.join function does not play nice with drive letters. When using a drive letter you still need to have backslashes, ie:

>>> os.path.join("c:", "Process new","RF","test.dat")
'c:Process new\\RF\\test.dat'
>>> os.path.join("c:\\", "Process new","RF","test.dat")
'c:\\Process new\\RF\\test.dat'
jlm699 320 Veteran Poster

This is a very simple way:

#!/usr/ben/python
#Temp conversion 

repeat = 'y'
while repeat == 'y':
    sel = raw_input("select C for celcius to farenheit, or F for farenheit to celcius ? ")
    if sel=="C":
        c = input("what temperature (celius) do you need converted? ")
        print "%s degrees celsius is equal to %s degrees Farenheit" % (c,32+(212-32) / 100 * c)
    elif sel=="F":
        f = input("what temp (farenheit) do you need converted? ")
        print "%s degrees farenheit is equal to %s degrees celcius" % (f,(f-32) * 100 / (212-32))
    repeat = raw_input("Would you like to repeat? (y/n)")
jlm699 320 Veteran Poster

Your code is awaiting your next raw_input at the "C" prompt.... You should be comparing the value of sel . Also, you've done the exact same thing as before. You can't print the value before you get it from the user or your code will complain about that object not existing. You need to modify your code like so:

sel = raw_input("select C for c to f, or F for f to c ? ")
if sel == 'C':
    c = raw_input("what temperature (celius) do you need converted? ")
    print "%s degrees celsius is equal to %s degrees Farenheit" % (c, 32+(212-32) / 100.0 * c)
elif sel == 'F':
    f = raw_input("what temp (farenheit) do you need converted? ")
    print "%s degrees farenheit is equal to %s degrees celcius" % (f,(f-32) * 100.0 / (212-32))
jlm699 320 Veteran Poster

That's because you're trying to print the result before you even ask the user for the number that they want to input.

jlm699 320 Veteran Poster

It looks like you're using a Python version that is not 3.X ... so in that case you should be using raw_input() instead of input().

raw_input stores the user's input as a string, which is the way input() works in Python 3.0 and up

jlm699 320 Veteran Poster

so if I have the variable:
mes = "[ $(date +\%d) -eq $(echo $(cal) | awk {print $NF}) ] && "+buCommand
os.popen("echo '"+mes+"'>>/etc/cron.d/"+mycronfile+";")

how do I KEPP my single quotes?

You put them in the mes variable :P

Above you have:

mes = "[ $(date +\%d) -eq $(echo $(cal) | awk {print $NF}) ] && "+buCommand
os.popen("echo '"+mes+"'>>/etc/cron.d/"+mycronfile+";")

I don't see any single quotes around the awk arguments.

If your real code has them and they still aren't showing up you could try to use a raw string, ie:

mes = r"[ $(date +\%d) -eq $(echo $(cal) | awk '{print $NF}') ] && "+buCommand
os.popen("echo '"+mes+"'>>/etc/cron.d/"+mycronfile+";")
jlm699 320 Veteran Poster

I'm not an expert with different encodings but isn't the "universal" encoding unicode?

jlm699 320 Veteran Poster

This would also likely require that your Python program run in the background as a service. It would need to monitor the presence of a USB flash drive and act accordingly when it detected one. There is information in this forum for setting up a service in Windows if you search for it.

jlm699 320 Veteran Poster

Here, this should give you some insight into how to define an encoding style for your whole python script.

jlm699 320 Veteran Poster

At anytime, dinner... literally anytime.

jlm699 320 Veteran Poster

This time it's because '?' is a special character in regular expressions (you're using it inside your group). The question mark indicates a greedy match of 1 or more (where as the asterick (*) is a greedy match of 0 or more). To match the question mark character itself you need to escape it in your regex like so: \? . The full regular expression then becomes:

>>> c = re.compile('<a href="/List\?ratings=7">(.*?)</a>')
>>> c.findall(t)
['7.2']
jlm699 320 Veteran Poster

So you literally take your object and perform a str(object) ? And that's what you're trying to turn back into a proper python object? For that you would use a simple eval():

to = ['This is my list', 'With Stuff', [0,1,2,3,4], 'Foo', 112, 5, '57 + 1']
my_saved_ver = str(to)
tr = eval(my_saved_ver)
print type(tr)             # Prints <type 'list'>
print type(my_saved_ver)  # Prints <type 'str'>

HTH

jlm699 320 Veteran Poster

Use code tags, Makes it easier for us to read.

Let me explain the code:

[B]import[/B] sys
[B]print [/B]>> sys.stderr, 'Fatal error: invalid input!'

logfile = open('/tmp/mylog.txt', 'a')
[B]print[/B] >> logfile, 'Fatal error: invalid input!'
logfile.close()
  1. We import the sys module documentation here
  2. We print the message 'Fatal error... etc' to the system's standard error pipe

  3. Open a file located at /tmp/mylog.txt in 'append' mode, which means open it for writing but leave the existing contents intact (alternately could say 'w' for 'write' mode, which clears the contents when it opens the file)

  4. Print the message into the logfile
  5. Close the logfile.
jlm699 320 Veteran Poster

hey, I tried it with
mpaaget = re.compile('<div class="info-content">(.*?)</div>')
but then I got something else . Could it be because there is a new line after <div class="info-content"> ? How do I take care of that?

Yes, the white space does not fit into your regular expression. Modify like so to match 0 or any number (*) of white space characters (\s):

>>> m = re.compile('<h5><a href="/mpaa">MPAA</a>:</h5>\s*<div class="info-content">\s*(.*?)\s*</div>')
>>> m.findall(h)
['Rated PG for some scary moments and mild language. (also 2009 extended version)']
>>> m.match(h)
>>>
jlm699 320 Veteran Poster

What about something like this (making use of integer division):

keycode = raw_input()
matrix = [0, 0, 0, 0]
for x in range(0, len(keycode)):
    y = ord(keycode[x])
    factor = x / 4
    x = x - (4 * factor)
    matrix[x] = matrix[x] + y

Not sure if that's what you were shooting for?

jlm699 320 Veteran Poster

ı want to make a loop that wıll change port adress

Here's a simple loop generating your example output strings between 111 and 115. I'm sure you can figure out how to extend up to 800.

>>> for x in xrange(111, 115):
...     print '/sbin/iptables -A INPUT -p tcp --dport %s -j DROP' % x
...     
/sbin/iptables -A INPUT -p tcp --dport 111 -j DROP
/sbin/iptables -A INPUT -p tcp --dport 112 -j DROP
/sbin/iptables -A INPUT -p tcp --dport 113 -j DROP
/sbin/iptables -A INPUT -p tcp --dport 114 -j DROP
>>>

HTH

jlm699 320 Veteran Poster

You can use either a tk or wxPython file dialog. Both should provide a native file chooser window for the user. Plenty of examples on this site or through searching google.

jlm699 320 Veteran Poster

Personally I'd use regular expressions like so:

>>> import re
>>> regex_compiled = re.compile('^<Ranking: (.*) \((.*)\)>$')
>>> input_data = """<Ranking: AA (John)>
... <Ranking: CA (Peter)>
... <Ranking: TA-A (Samantha)>
... """
>>> for each_entry in input_data.split('\n'):
...     regex_match = regex_compiled.match(each_entry)
...     if regex_match:
...         print 'Ranking: % 5s  Name: %s' % (regex_match.group(1), regex_match.group(2))
...     
Ranking:    AA  Name: John
Ranking:    CA  Name: Peter
Ranking:  TA-A  Name: Samantha
>>>

If you need any specific part of that explained I'd be happy to do so.

If you're really in need of using string methods I'd do something like the following:

>>> for each_entry in input_data.split('\n'):
...     rank_search = '<Ranking: '
...     idx = each_entry.find(rank_search)
...     if idx != -1: # string.find returns -1 when not found
...         idx += len(rank_search) # Increase idx to where rank_search ends
...         end_idx = each_entry.find(' (', idx) # idx as starting point for find
...         print 'Ranking: % 5s  Name: %s' % (each_entry[idx:end_idx], each_entry[end_idx+2:-2])
...     
Ranking:    AA  Name: John
Ranking:    CA  Name: Peter
Ranking:  TA-A  Name: Samantha
>>>
jlm699 320 Veteran Poster

This was the first google result for "ubuntu python numpy".

sudo apt-get install python-numpy python-scipy

Also, don't forget to install matplotlib and its dependent files:

sudo apt-get install python-matplotlib python-tk

Also, the author mentions that the scipy website provides pre-built binaries for Ubuntu already.

jlm699 320 Veteran Poster

Okay so returning signals the end of a function as well as confirms the functions ability to pass a variable made in the function?

Yes, a return statement exits the function and passes a value back to the point in code that called it. You can return any object no matter how complex. By omitting a value and just using return you are returning a None type object (ie, nothing).

jlm699 320 Veteran Poster

Typically when you create a module that you want to use in another script you save it as a file (we'll use my_module.py as an example). Then in your script in which you want to use said module you would do import my_module ; however this assumes that my_module is visible to the script either in the PYTHONPATH or system PATH, or the current working directory (ie, directory where the script is running from).

EDIT: Whether this works or not also depends on how you implement your module. In your first code example above you have lines of code in the global space of the script (ie, not indented into a function or class. That code would all run when performing an import. A common practice is to use a main function and protect it from running on import like this:

# Example python module

import sys
# Any other imports... imports should always be first

# Some classes, functions, whatever...
# This is your meat and potatos

# Now we'll define a main function
def main():
    # This is the code that runs when you are running this module alone
    print sys.platform

# This checks whether this file is being run as the main script
#  or if its being run from another script
if __name__ == '__main__':
    main()
# Another script running this script (ie, in an import) would use it's own
#  filename as the value of __name__

Hope that clears it up... …

jlm699 320 Veteran Poster

What about passing the class instance as a parameter to your function?

jlm699 320 Veteran Poster

My second question is:\

def costTotal(self, cost=100):

what exactly is this cost=100?

In a function declaration, the items inside the parenthesis are called parameters. These are basically variables that are "passed" to the function. By assigning a value (of 100) to a parameter you are making it optional. By optional, I mean that you aren't required to pass that value to the function in order for it to work. When making it optional you provide a default value. So you can call the function two ways:

self.costTotal()

Inside the function, if you were to add a print cost as the first line you'd see that cost is equal to 100 (the default value). Now if you called the function like this:

self.costTotal(250)

Your print statement would show that cost is now equal to 250.

Hope that clears it up.

jlm699 320 Veteran Poster
class Cat(object):
    def __init__(self, num=1, age=1):
        self.num = num
        self.age = age
    def costTotal(self, cost=100):
        self.cost = cost
        self.costTotal = self.cost + self.cost * self.num + self.cost * self.age
    def printIt(self):
        print self.num, self.age
        print self.costTotal

cat = Cat(2,3)
cat.printIt()

[1] I cannot get the second printIt worked. It showed <bound method .....
I got self.num, self.age working but not the second line. What did I do wrong?

You need to call the function costTotal and provide the parameters if needed. So your line that says print self.costTotal should actually be print costTotal() ... then in your costTotal function I believe the last line should be return self.cost + self.cost * self.num + self.cost * self.age . The way it is now, you're effectively "erasing" the function costTotal and replacing it with the value of that calculation.

I don't understand your second question.

jlm699 320 Veteran Poster

Yes, you could create a general Student class and then go about creating instances of the class. Each instance would represent a different student and would contain the necessary data to individualize and represent the student's details.

If you look around this forum you'll find examples of "shapes" and "pets" classes that others have provided. They are very basic examples of making a class and then creating instances of said class. That should get you well on your way

jlm699 320 Veteran Poster

I just ran your program verbatim and it ran all the way to the end. How are you running your program?

jlm699 320 Veteran Poster

Here's a regular expression that should work for you: re.split('([.!?] *)', a) If you need it broken down and explained for you I can do that; otherwise, it may be more fun to investigate using the documentation and pick it apart to learn how it works!

Here's me using your test string:

>>> import re
>>> a = "this is the test string! will it work? let's find out. it should work! or should it? oh yes. indeed."
>>> rtn = re.split('([.!?] *)', a)
>>> ''.join([each.capitalize() for each in rtn])
"This is the test string! Will it work? Let's find out. It should work! Or should it? Oh yes. Indeed."
>>>

At the end there I did a list comprehension. To break it out it would be:

>>> str_pieces = []
>>> for each in rtn:
...     str_pieces.append(each.capitalize())
...     
>>> ''.join(str_pieces)
"This is the test string! Will it work? Let's find out. It should work! Or should it? Oh yes. Indeed."
>>>