How to write fast into a file in python?

2013-05-16 Thread lokeshkoppaka


I need to write numbers into a file upto 50mb and it should be fast
can any one help me how to do that?
i had written the following code..
---
def create_file_numbers_old(filename, size):
start = time.clock()

value = 0
with open(filename, "w") as f:
while f.tell()< size:
f.write("{0}\n".format(value))
value += 1

end = time.clock()

print "time taken to write a file of size", size, " is ", (end -start), 
"seconds \n"
--
it takes about 20sec i need 5 to 10 times less than that.
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: How to write fast into a file in python?

2013-05-16 Thread lokeshkoppaka
On Friday, May 17, 2013 8:50:26 AM UTC+5:30, lokesh...@gmail.com wrote:
> I need to write numbers into a file upto 50mb and it should be fast
> 
> can any one help me how to do that?
> 
> i had written the following code..
> 
> ---
> 
> def create_file_numbers_old(filename, size):
> 
> start = time.clock()
> 
> 
> 
> value = 0
> 
> with open(filename, "w") as f:
> 
> while f.tell()< size:
> 
> f.write("{0}\n".format(value))
> 
> value += 1
> 
> 
> 
> end = time.clock()
> 
> 
> 
> print "time taken to write a file of size", size, " is ", (end -start), 
> "seconds \n"
> 
> --
> 
> it takes about 20sec i need 5 to 10 times less than that.
size = 50mb
-- 
http://mail.python.org/mailman/listinfo/python-list


Scope of a class..help???

2013-05-23 Thread lokeshkoppaka
i had written the following code i am unable to create the instance of the 
class "Node" in the method "number_to_LinkedList" can any one help me how to do 
??
and what is the error??
  

class Node:
def __init__(self, value=None):
self.value = value
self.next = None



def number_to_LinkedList(numbers):
  pass
  list_numbers = list(numbers)
  head_node = Node() #unable to create the instance saying UnboundedLocal
  head_node.value = list_numbers[0]
  head_node.next = None
  current_node = head_node
  for i in range(1,len(list_numbers)):
new_node = Node()
new_node.value = list_numbers[i]
new_node.next = current_node
current_node = new_node
  current_node.next = None
  while Node:
print Node.data
Node = Node.next
-- 
http://mail.python.org/mailman/listinfo/python-list


2's Complement in python help.

2013-05-23 Thread lokeshkoppaka
Can anyone give me an idea of how to find the 2's Complement in python with an 
example
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Scope of a class..help???

2013-05-23 Thread lokeshkoppaka
 Thanks Chris Angelico,
i am new to python can you suggest me how to remove the error and solve it.
so,how can i create an instance for "Node" in that function??,is, it not 
possible to create an instance in such a way?

-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Scope of a class..help???

2013-05-23 Thread lokeshkoppaka
ok Peter Otten,
but how to make a Class global??

-- 
http://mail.python.org/mailman/listinfo/python-list


help in obtaining binary equivalent of a decimal number in python

2013-05-23 Thread lokeshkoppaka
i need to get 32 bit binary equivalent of a decimal and need to change the 0's 
to 1's and 1's to 0's
For Example
if the input is 2 
Output should be:
the 32bit equivalent of 2 :       0010
and the 1's compliment is:       1101



is there any pre-defined function to get the above results in python?? 
-- 
http://mail.python.org/mailman/listinfo/python-list


help how to sort a list in order of 'n' in python without using inbuilt functions??

2013-05-24 Thread lokeshkoppaka
i need to write a code which can sort the list in order of 'n' without use 
builtin functions 
can anyone help me how to do?
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: help how to sort a list in order of 'n' in python without using inbuilt functions??

2013-05-24 Thread lokeshkoppaka
On Friday, May 24, 2013 1:34:51 PM UTC+5:30, lokesh...@gmail.com wrote:
> i need to write a code which can sort the list in order of 'n' without use 
> builtin functions 
> 
> can anyone help me how to do?

 Note:
the list only contains 0's,1's,2's
need to sort them in order of 'n'
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: help how to sort a list in order of 'n' in python without using inbuilt functions??

2013-05-24 Thread lokeshkoppaka
On Saturday, May 25, 2013 10:54:01 AM UTC+5:30, Chris Angelico wrote:
> On Sat, May 25, 2013 at 3:15 PM,   wrote:
> 
> > On Friday, May 24, 2013 1:34:51 PM UTC+5:30, lokesh...@gmail.com wrote:
> 
> >> i need to write a code which can sort the list in order of 'n' without use 
> >> builtin functions
> 
> >>
> 
> >> can anyone help me how to do?
> 
> >
> 
> >  Note:
> 
> > the list only contains 0's,1's,2's
> 
> > need to sort them in order of 'n'
> 
> 
> 
> In that case, you're not really ordering them, you're counting them.
> 
> Look at the collections module; you can very easily figure out how
> 
> many of each there are, and then reconstruct the list afterward.
> 
> 
> 
> ChrisA

but i need to do it with out using builtin functions
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: help how to sort a list in order of 'n' in python without using inbuilt functions??

2013-05-24 Thread lokeshkoppaka
On Saturday, May 25, 2013 11:27:38 AM UTC+5:30, Steven D'Aprano wrote:
> On Fri, 24 May 2013 22:39:06 -0700, lokeshkoppaka wrote:
> 
> 
> 
> > On Saturday, May 25, 2013 10:54:01 AM UTC+5:30, Chris Angelico wrote:
> 
> 
> 
> >> In that case, you're not really ordering them, you're counting them.
> 
> >> Look at the collections module; you can very easily figure out how
> 
> >> many of each there are, and then reconstruct the list afterward.
> 
> > 
> 
> > but i need to do it with out using builtin functions
> 
> 
> 
> 
> 
> How would you, an intelligent human being count them?
> 
> 
> 
> Describe how you would count them in English, or whatever your native 
> 
> language is.
> 
> 
> 
> "First I would start a tally for the number of zeroes, tally = 0. Then I 
> 
> look at each item in turn, and if it is 0, I add one to the tally. When I 
> 
> get to the end, I the number of zeroes is equal to the tally.
> 
> 
> 
> Then I do the same for the number of ones, and the number of twos."
> 
> 
> 
> Now turn that into Python code. 
> 
> 
> 
> tally = 0
> 
> for item in list_of_items:
> 
> if item == 0:
> 
> tally = tally + 1
> 
> 
> 
> print "The number of zeroes equals", tally
> 
> 
> 
> 
> 
> 
> 
> -- 
> 
> Steven

ya steven i had done the similar logic but thats not satisfying my professor 
he had given the following constrains
 1. No in-built functions should be used
 2. we are expecting a O(n) solution
 3. Don't use count method
-- 
http://mail.python.org/mailman/listinfo/python-list


help?? on functions

2013-05-26 Thread lokeshkoppaka
def shuffle(input, i, j):
pass
input = input[i:j+1] +input[0:i] + input[j+1:]
def test_shuffle():
input = [1, 2, 3, 4, 5, 6]
shuffle(input, 1, 2)
assert [2, 3, 1, 4, 5, 6] == input


i had done the above code but the problem is i had manipulated the "input" in 
function shuffle(input, i, j) but once i get back to the test_shuffle() 
function again the variable "input" does not reflect the changes made in 
shuffle(input, i, j) why ,please can any one describe why . and help how to 
reflect that change to the variable "input".
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: help?? on functions

2013-05-27 Thread lokeshkoppaka
On Monday, May 27, 2013 11:18:34 AM UTC+5:30, Steven D'Aprano wrote:
> On Sun, 26 May 2013 21:48:34 -0700, lokeshkoppaka wrote:
> 
> 
> 
> > def shuffle(input, i, j):
> 
> > pass
> 
> > input = input[i:j+1] +input[0:i] + input[j+1:]
> 
> 
> 
> "pass" does nothing. Take it out.
> 
> 
> 
> 
> 
> 
> 
> > def test_shuffle():
> 
> > input = [1, 2, 3, 4, 5, 6]
> 
> > shuffle(input, 1, 2)
> 
> > assert [2, 3, 1, 4, 5, 6] == input
> 
> > 
> 
> > 
> 
> > i had done the above code but the problem is i had manipulated the
> 
> > "input" in function shuffle(input, i, j) but once i get back to the
> 
> > test_shuffle() function again the variable "input" does not reflect the
> 
> > changes made in shuffle(input, i, j) why ,please can any one describe
> 
> > why . and help how to reflect that change to the variable "input".
> 
> 
> 
> The line of code:
> 
> 
> 
> input = input[i:j+1] +input[0:i] + input[j+1:]
> 
> 
> 
> 
> 
> takes the input list, makes three slices from that list, creates a new 
> 
> list, and then reassigns the LOCAL variable "input". This does not touch 
> 
> the variable on the outside of the function.
> 
> 
> 
> This will be more clear if you use different names:
> 
> 
> 
> # Outside the function.
> 
> mylist = [1, 2, 3, 4, 5, 6]
> 
> shuffle(mylist, 1, 2)
> 
> 
> 
> 
> 
> Inside the function "shuffle", "input" is a local variable, and when you 
> 
> reassign to it, the variable "mylist" on the outside is not changed. Try 
> 
> this small function to see what I mean:
> 
> 
> 
> def demo(input):
> 
> print('local variable, before:', input)
> 
> input = 100
> 
> print('local variable, after:', input)
> 
> print('non-local variable', mylist)
> 
> 
> 
> 
> 
> mylist = [1, 2, 3, 4, 5, 6]
> 
> demo(mylist)
> 
> 
> 
> 
> 
> 
> 
> So, what can you do to fix this? You have two choices:
> 
> 
> 
> 
> 
> 1) You can return the shuffled list. Add this line to the end of your 
> 
> shuffle function:
> 
> 
> 
> return input
> 
> 
> 
> 
> 
> and then inside the test function, do this:
> 
> 
> 
> def test_shuffle():
> 
> input = [1, 2, 3, 4, 5, 6]
> 
> input = shuffle(input, 1, 2)
> 
> assert [2, 3, 1, 4, 5, 6] == input
> 
> 
> 
> 
> 
> 2) You can modify the input list in place.
> 
> 
> 
> In this case, instead of reassigning the local variable "input" with the 
> 
> new list, you simply tell Python to stuff the new list inside the 
> 
> original list. You do that with a slice:
> 
> 
> 
> 
> 
> input[:] = input[i:j+1] + input[0:i] + input[j+1:]
> 
> 
> 
> 
> 
> That's a small difference from what you wrote, just three characters [:], 
> 
> but it makes a big difference in the effect. Instead of reassigning the 
> 
> local variable to the new list, it takes the existing list, and replaces 
> 
> each value inside it with the values taken from the new list. For example:
> 
> 
> 
> 
> 
> py> mylist = [100, 200, 300, 400, 500, 600]
> 
> py> mylist[3:5] = ['A', 'B', 'C']
> 
> py> mylist
> 
> [100, 200, 300, 'A', 'B', 'C', 600]
> 
> 
> 
> py> mylist[1:] = [99, 98, 97]
> 
> py> mylist
> 
> [100, 99, 98, 97]
> 
> 
> 
> 
> 
> Any questions?
> 
> 
> 
> 
> 
> -- 
> 
> Steven

Steven 
wow, wonderful explanation ,i got it thanks a lot 
-- 
http://mail.python.org/mailman/listinfo/python-list