rrashkin 41 Junior Poster in Training

Here's my 2 cents:
Let's say I have a function that turns Year,month,day into day of year:

def ymd2doy(y,m,d):
    if y<100: y+=2000
    ly=0
    if (y-2000)%4==0: ly=1
    md=(31,28+ly,31,30,31,30,31,31,30,31,30,31)
    for i in xrange(m-1): d+=md[i]
    return d

Instead of if (y-2000)%4==0: ly=1 I want to use a function:

def leapyr (y):
   return (y-2000)%4==0

so:

def ymd2doy(y,m,d):
    if y<100: y+=2000
    ly=0
    if leapyr(y): ly=1
    md=(31,28+ly,31,30,31,30,31,31,30,31,30,31)
    for i in xrange(m-1): d+=md[i]
    return d

Is that what you meant?

rrashkin 41 Junior Poster in Training

It can be done with the eval() builtin function. However!!! it is very dangerous to allow users to input any old thing and then evaluate that input. It would be better to give them a limited set of choices.

rrashkin 41 Junior Poster in Training

Maybe:

with open('c:\FLOUpper.txt', 'r') as infile,open('c:\FLOLower.txt', 'w') as outfile:
    data = infile.readlines()
    data = [i.capitalize() for i in data]
    outfile.write(data)
Shaji_1 commented: Great starting point. Thank you! +0
rrashkin 41 Junior Poster in Training

get rid of "print(input(..."
name1=input("what is your name: ")

Gribouillis commented: indeed +14
rrashkin 41 Junior Poster in Training

Let's say your string, QUESTION DEFINITIONS
var P1_1 = new Array("P1_1", "An audit charter should:", "A. be dynamic and change often to coincide with the changing nature of technology and the audit profession.", "B. clearly state audit objectives for the delegation of authority for the maintenance and review of internal controls.", "C. document the audit procedures designed to achieve the planned audit objectives.", "D. outline the overall authority, scope and responsibilities of the audit function.", "An audit charter should state management's objectives for, and delegation of authority to, IS audit. This charter should not significantly change over time and should be approved at the highest level of management. The audit charter would not be at a detail level and therefore would not include specific audit objectives or procedures.", 4, 1);
, is in a file called "text.txt" (it could be anything).
Run this code:

fid=open("text.txt","r")
strA=fid.read()
fid.close()
strA=strA.split("(")[1].split(")")[0]
strA=strA.replace(",","\n")
print strA

What do you get?

rrashkin 41 Junior Poster in Training

So it seems that "QUESTION DEFINITIONS
var P1_1 = new Array...
level and therefore would not include specific audit objectives or procedures.", 4, 1);" is a string that does indeed look like a javascript array assignment. Let's say you've read that string into a python variable, strA. First, let's get rid of the stuff outside the parentheses:
strA=strA.split("(")[1].split(")")[0]
Now let's replace the commas with linefeeds:
strA=strA.replace(",","\n")
Now I think you have what you want, no?

rrashkin 41 Junior Poster in Training

What you've shown is an array (list), that is, it's already split. What are you starting with?

rrashkin 41 Junior Poster in Training

Not to endorse or refute the underlying concept, if you provided the rules (eg: number of vowels is greater than twice the square root of the number of "s"s) I'm sure you'd get some help.

rrashkin 41 Junior Poster in Training

Everything is a function except cSetInsert(5,4) so that gets executed. cSetInsert tries to use "5" as the argument, "cSet". However the function treats it as a list: cSet[hashChar(e)]

I think you intended to call cSetCreate() first and pass in the corresponding returned cSet. I don't know what you think "5" is for.

rrashkin 41 Junior Poster in Training

Within the function, you do, indeed, need to initialize y before you can start defining elements. Even then, if you try to assign elements out of order, that is, an element, [i], when [i-1] is empty, you will get the out of range error:

>>> y=[]
>>> y[8]=5
Traceback (most recent call last):
  File "/base/data/home/apps/runpythoncode/103.348367701639469596/console.py", line 123, in _execStatement
    exec compiled in statement_module.__dict__
  File "<string>", line 1, in <module>
IndexError: list assignment index out of range

You can initialize the y list with blanks before you start the loop with list comprehension:

y=[" " for i in xrange(len(x))]

or by list multiplication:

y=len(x)*[" "]
rrashkin 41 Junior Poster in Training

I'm guessing you're using Python 3.x where print() takes exactly one argument. Instead of print (x)('x')(y) you need something like ("something like" because I don't have Python 3 to test on): print(x + 'x' + y)

rrashkin 41 Junior Poster in Training

I would start by reading the file into a list:

fid=open('file1.txt')
lstData=fid.readlines()
fid.close()

Now the last record is: lstData[-1]

To loop through the records from the 2nd to last until the first,
for i in lstData[-2::-1]:

rrashkin 41 Junior Poster in Training

I'm not really clear what you're trying to do but os.system has fallen from favor and the subprocess module is recommended. Maybe that will suit your needs better.

james.lu.75491856 commented: WRONG! os.system for all the commands in command line! not for launching programs! +0
Gribouillis commented: indeed +14
rrashkin 41 Junior Poster in Training

sure. It's just a name.

rrashkin 41 Junior Poster in Training

look at this for an example. Basically, the key is in the open():

Modes 'r+', 'w+' and 'a+' open the file for updating (note that 'w+' truncates the file). Append 'b' to the mode to open the file in binary mode, on systems that differentiate between binary and text files; on systems that don’t have this distinction, adding the 'b' has no effect.

rrashkin 41 Junior Poster in Training

Well, when you set enemy equal to goblin, that's really just an association, not a new object. You can see an explanation of the issue and the use of "copy" to deal with it here. Perhaps it is simpler just to instantiate a new obect.

rrashkin 41 Junior Poster in Training

First of all, unzip step doesn't really help you. You can get the ith element of each list with list comprehension:
list=[a[i] for a in column] for a given i.

To get some other set of elements as you describe will require looping through the number of elements and taking each desired element explicitly.

rrashkin 41 Junior Poster in Training

This is sort of buried in the language reference. If you look here: Click Here, it gets explained, but you have to dig. In particular:

or passed as a value in a dictionary preceded by **

That means that the identifier after the "**" is a dictionary with "variable name: value" entries.

rrashkin 41 Junior Poster in Training

It's very ineffient but here goes:

a= [10, 5, 2, 7, 20]
b=[]
for i in xrange(len(a)):
    b.append(min(a))
    a.pop(a.index(b[-1]))
rrashkin 41 Junior Poster in Training

The maximum height is where yvel = 0. In your initialization method you have:
self.yvel=velocity*sin(theta)
You know that yvel goes to zero when 0.98*time equals the initial velocity, or at
velocity*sin(theta)/9.8 seconds
So you can figure out when you get to that time at your interval.
Now since xvel is presumed not to change, the xpos at that time is
(velocity*sin(theta)/9.8)*velocity*cos(theta)

rrashkin 41 Junior Poster in Training

First a list of all the gemstones:

f=open("your file name")
gemstones=[]
for i in f: gemstones.append(i.split(',')[1])
f.close()

now a dictionary where key is gemstone and value is count

gcount={}
for g in gemstones: gcount[g]=gcount.get(g,0)+1
rrashkin 41 Junior Poster in Training

I dont use WX but I don't think that matters. There's a lot of code there and I admit I didn't read it all. So I don't know really what you're doing already, but you have asked a specific question.

I think I would make each sensor a different obect (instance of a sensor class). The instantiation of sensors should be mediated by a dictionary (d[a]=Sensor(initdata)) so you can step through each sensor obect (dictionary entry) and call a get_data() method, appending the appropriate information to a list

rrashkin 41 Junior Poster in Training

To write any 2-d list as a csv formatted file, simply "join" the elements of the list with a comma and write as text:

f=open(<output file>, 'w')
for row in listname:
    f.write(','.join(map(str,row))+'\n')
rrashkin 41 Junior Poster in Training

I would need more information to answer that. What is the structure of the dictionary? Normally, I would key an address book on "name" so something like AddBook={"john doe":("8134789 Zubruzlechester", "Alpha Centauri", "999-555-1234")}. In that case I would get the telephone number, for instance, for John Doe, as: AddBook["john doe"][2]

rrashkin 41 Junior Poster in Training

Well, I'm sure with polynomial recursion you can make it as complicated as you need to but if we take every 2 points on the reference track to define a line, then you can define a parallel line that is offset from that as I described above. Then you just do that for every point pair. It's tedious but that's why we have computers.

Gribouillis commented: I agree with that +13
rrashkin 41 Junior Poster in Training

You've got the right idea but I think you're doing something you don't want to do and I know you're doing something wrong.

First the wrong thing.
When you read each line, for line in f.readlines():, "line" is a string. Strings are iterable but each character is an element. So, line[0] for instance would be "5" in the case of the Bart Simpson record. To get what you want you need to split the line into words (splitting on spaces): line=line.strip('\n').split(), also taking off the linefeed. Now you have another problem, the "name" field is 2 words, line[1] and line[2]. If you know both names are always going to be present then you can use the constructor: Persons(line[0], line[1]+' '+line[2], line[-1]). Otherwise you'll need to check on how many elements line has 4 or 3 and construct the object accordingly.

Now the other thing. In your loop, you keep using the same object name, "pers". Once the loop is done, you'll only have a single object. There are lots of ways to keep all the people "alive" but I think the best is to have a dictionary of people, dctPerson={}, and instead of "pers" use dctPerson[line[0]]=Persons(line[0], line[1]+' '+line[2], line[-1]).

rrashkin 41 Junior Poster in Training

You should, of course post the error but frequently the problem is that the backslash, "\", is used by Python as the escape character. You should either use the forward slash, "/" or escape the backslash, "\" in your paths.