RE: Can you help me with this memoization simple example?

2024-03-31 Thread AVI GROSS via Python-list
I am not sure if it was made clear that there is a general rule in python for 
what is HASHABLE and lists are changeable while tuples are not so the latter 
can be hashed as a simple copy of a list, albeit the contents must also be 
immutable.

The memorize function uses a dictionary to store things and thus the things are 
hashed to decide how to store it in the inner representation of a dictionary 
and anything new that you want to look up in the dictionary has similar 
considerations as it is hashed to see where in the dictionary to look for it.

Of course, if you add enough overhead and the memorize function you make gets 
relatively few requests that are identical, it may not be worthwhile.

-Original Message-
From: Python-list  On 
Behalf Of MRAB via Python-list
Sent: Sunday, March 31, 2024 3:24 PM
To: python-list@python.org
Subject: Re: Can you help me with this memoization simple example?

On 2024-03-31 09:04, marc nicole wrote:
> Thanks for the first comment which I incorporated
>
> but when you say "You can't use a list as a key, but you can use a 
> tuple as a key,
> provided that the elements of the tuple are also immutable."
>
> does it mean  the result of sum of the array is not convenient to use 
> as key as I do?
> Which tuple I should use to refer to the underlying list value as you 
> suggest?
>
I was suggesting using `tuple` on the argument:

def memoize(f):
  cache = {}

  def g(*args):
  key = tuple(args[0]), args[1]

  if key not in cache:
  cache[key] = f(args[0], args[1])

  return cache[key]

  return g

> Anything else is good in my code ?
>
> Thanks
>
> Le dim. 31 mars 2024 à 01:44, MRAB via Python-list 
>  a écrit :
>
> On 2024-03-31 00:09, marc nicole via Python-list wrote:
> > I am creating a memoization example with a function that adds up
> / averages
> > the elements of an array and compares it with the cached ones to
> retrieve
> > them in case they are already stored.
> >
> > In addition, I want to store only if the result of the function
> differs
> > considerably (passes a threshold e.g. 50 below).
> >
> > I created an example using a decorator to do so, the results
> using the
> > decorator is slightly faster than without the memoization which
> is OK, but
> > is the logic of the decorator correct ? anybody can tell me ?
> >
> > My code is attached below:
> >
> >
> >
> > import time
> >
> >
> > def memoize(f):
> >  cache = {}
> >
> >  def g(*args):
> >  if args[1] == "avg":
> >  sum_key_arr = sum(list(args[0])) / len(list(args[0]))
>
> 'list' will iterate over args[0] to make a list, and 'sum' will
> iterate
> over that list.
>
> It would be simpler to just let 'sum' iterate over args[0].
>
> >  elif args[1] == "sum":
> >  sum_key_arr = sum(list(args[0]))
> >  if sum_key_arr not in cache:
> >  for (
> >  key,
> >  value,
> >  ) in (
> >  cache.items()
> >  ):  # key in dict cannot be an array so I use the
> sum of the
> > array as the key
>
> You can't use a list as a key, but you can use a tuple as a key,
> provided that the elements of the tuple are also immutable.
>
> >  if (
> >  abs(sum_key_arr - key) <= 50
> >  ):  # threshold is great here so that all
> values are
> > approximated!
> >  # print('approximated')
> >  return cache[key]
> >  else:
> >  # print('not approximated')
> >  cache[sum_key_arr] = f(args[0], args[1])
> >  return cache[sum_key_arr]
> >
> >  return g
> >
> >
> > @memoize
> > def aggregate(dict_list_arr, operation):
> >  if operation == "avg":
> >  return sum(list(dict_list_arr)) / len(list(dict_list_arr))
> >  if operation == "sum":
> >  return sum(list(dict_list_arr))
> >  return None
> >
> >
> > t = time.time()
> > for i in range(200, 15000):
> >  res = aggregate(list(range(i)), "avg")
> >
> > elapsed = time.time() - t
> > print(res)
> > print(elapsed)
>
>
> -- 
> https://mail.python.org/mailman/listinfo/python-list
>
-- 
https://mail.python.org/mailman/listinfo/python-list

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


Re: Can you help me with this memoization simple example?

2024-03-31 Thread MRAB via Python-list

On 2024-03-31 09:04, marc nicole wrote:

Thanks for the first comment which I incorporated

but when you say "You can't use a list as a key, but you can use a 
tuple as a key,

provided that the elements of the tuple are also immutable."

does it mean  the result of sum of the array is not convenient to use 
as key as I do?
Which tuple I should use to refer to the underlying list value as you 
suggest?



I was suggesting using `tuple` on the argument:

def memoize(f):
 cache = {}

 def g(*args):
 key = tuple(args[0]), args[1]

 if key not in cache:
 cache[key] = f(args[0], args[1])

 return cache[key]

 return g


Anything else is good in my code ?

Thanks

Le dim. 31 mars 2024 à 01:44, MRAB via Python-list 
 a écrit :


On 2024-03-31 00:09, marc nicole via Python-list wrote:
> I am creating a memoization example with a function that adds up
/ averages
> the elements of an array and compares it with the cached ones to
retrieve
> them in case they are already stored.
>
> In addition, I want to store only if the result of the function
differs
> considerably (passes a threshold e.g. 50 below).
>
> I created an example using a decorator to do so, the results
using the
> decorator is slightly faster than without the memoization which
is OK, but
> is the logic of the decorator correct ? anybody can tell me ?
>
> My code is attached below:
>
>
>
> import time
>
>
> def memoize(f):
>      cache = {}
>
>      def g(*args):
>          if args[1] == "avg":
>              sum_key_arr = sum(list(args[0])) / len(list(args[0]))

'list' will iterate over args[0] to make a list, and 'sum' will
iterate
over that list.

It would be simpler to just let 'sum' iterate over args[0].

>          elif args[1] == "sum":
>              sum_key_arr = sum(list(args[0]))
>          if sum_key_arr not in cache:
>              for (
>                  key,
>                  value,
>              ) in (
>                  cache.items()
>              ):  # key in dict cannot be an array so I use the
sum of the
> array as the key

You can't use a list as a key, but you can use a tuple as a key,
provided that the elements of the tuple are also immutable.

>                  if (
>                      abs(sum_key_arr - key) <= 50
>                  ):  # threshold is great here so that all
values are
> approximated!
>                      # print('approximated')
>                      return cache[key]
>              else:
>                  # print('not approximated')
>                  cache[sum_key_arr] = f(args[0], args[1])
>          return cache[sum_key_arr]
>
>      return g
>
>
> @memoize
> def aggregate(dict_list_arr, operation):
>      if operation == "avg":
>          return sum(list(dict_list_arr)) / len(list(dict_list_arr))
>      if operation == "sum":
>          return sum(list(dict_list_arr))
>      return None
>
>
> t = time.time()
> for i in range(200, 15000):
>      res = aggregate(list(range(i)), "avg")
>
> elapsed = time.time() - t
> print(res)
> print(elapsed)


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



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


Re: Can you help me with this memoization simple example?

2024-03-31 Thread marc nicole via Python-list
Thanks for the first comment which I incorporated

but when you say "You can't use a list as a key, but you can use a tuple as
a key,
provided that the elements of the tuple are also immutable."

does it mean  the result of sum of the array is not convenient to use as
key as I do?
Which tuple I should use to refer to the underlying list value as you
suggest?

Anything else is good in my code ?

Thanks

Le dim. 31 mars 2024 à 01:44, MRAB via Python-list 
a écrit :

> On 2024-03-31 00:09, marc nicole via Python-list wrote:
> > I am creating a memoization example with a function that adds up /
> averages
> > the elements of an array and compares it with the cached ones to retrieve
> > them in case they are already stored.
> >
> > In addition, I want to store only if the result of the function differs
> > considerably (passes a threshold e.g. 50 below).
> >
> > I created an example using a decorator to do so, the results using the
> > decorator is slightly faster than without the memoization which is OK,
> but
> > is the logic of the decorator correct ? anybody can tell me ?
> >
> > My code is attached below:
> >
> >
> >
> > import time
> >
> >
> > def memoize(f):
> >  cache = {}
> >
> >  def g(*args):
> >  if args[1] == "avg":
> >  sum_key_arr = sum(list(args[0])) / len(list(args[0]))
>
> 'list' will iterate over args[0] to make a list, and 'sum' will iterate
> over that list.
>
> It would be simpler to just let 'sum' iterate over args[0].
>
> >  elif args[1] == "sum":
> >  sum_key_arr = sum(list(args[0]))
> >  if sum_key_arr not in cache:
> >  for (
> >  key,
> >  value,
> >  ) in (
> >  cache.items()
> >  ):  # key in dict cannot be an array so I use the sum of the
> > array as the key
>
> You can't use a list as a key, but you can use a tuple as a key,
> provided that the elements of the tuple are also immutable.
>
> >  if (
> >  abs(sum_key_arr - key) <= 50
> >  ):  # threshold is great here so that all values are
> > approximated!
> >  # print('approximated')
> >  return cache[key]
> >  else:
> >  # print('not approximated')
> >  cache[sum_key_arr] = f(args[0], args[1])
> >  return cache[sum_key_arr]
> >
> >  return g
> >
> >
> > @memoize
> > def aggregate(dict_list_arr, operation):
> >  if operation == "avg":
> >  return sum(list(dict_list_arr)) / len(list(dict_list_arr))
> >  if operation == "sum":
> >  return sum(list(dict_list_arr))
> >  return None
> >
> >
> > t = time.time()
> > for i in range(200, 15000):
> >  res = aggregate(list(range(i)), "avg")
> >
> > elapsed = time.time() - t
> > print(res)
> > print(elapsed)
>
>
> --
> https://mail.python.org/mailman/listinfo/python-list
>
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Can you help me with this memoization simple example?

2024-03-30 Thread MRAB via Python-list

On 2024-03-31 00:09, marc nicole via Python-list wrote:

I am creating a memoization example with a function that adds up / averages
the elements of an array and compares it with the cached ones to retrieve
them in case they are already stored.

In addition, I want to store only if the result of the function differs
considerably (passes a threshold e.g. 50 below).

I created an example using a decorator to do so, the results using the
decorator is slightly faster than without the memoization which is OK, but
is the logic of the decorator correct ? anybody can tell me ?

My code is attached below:



import time


def memoize(f):
 cache = {}

 def g(*args):
 if args[1] == "avg":
 sum_key_arr = sum(list(args[0])) / len(list(args[0]))


'list' will iterate over args[0] to make a list, and 'sum' will iterate 
over that list.


It would be simpler to just let 'sum' iterate over args[0].


 elif args[1] == "sum":
 sum_key_arr = sum(list(args[0]))
 if sum_key_arr not in cache:
 for (
 key,
 value,
 ) in (
 cache.items()
 ):  # key in dict cannot be an array so I use the sum of the
array as the key


You can't use a list as a key, but you can use a tuple as a key, 
provided that the elements of the tuple are also immutable.



 if (
 abs(sum_key_arr - key) <= 50
 ):  # threshold is great here so that all values are
approximated!
 # print('approximated')
 return cache[key]
 else:
 # print('not approximated')
 cache[sum_key_arr] = f(args[0], args[1])
 return cache[sum_key_arr]

 return g


@memoize
def aggregate(dict_list_arr, operation):
 if operation == "avg":
 return sum(list(dict_list_arr)) / len(list(dict_list_arr))
 if operation == "sum":
 return sum(list(dict_list_arr))
 return None


t = time.time()
for i in range(200, 15000):
 res = aggregate(list(range(i)), "avg")

elapsed = time.time() - t
print(res)
print(elapsed)



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


Can you help me with this memoization simple example?

2024-03-30 Thread marc nicole via Python-list
I am creating a memoization example with a function that adds up / averages
the elements of an array and compares it with the cached ones to retrieve
them in case they are already stored.

In addition, I want to store only if the result of the function differs
considerably (passes a threshold e.g. 50 below).

I created an example using a decorator to do so, the results using the
decorator is slightly faster than without the memoization which is OK, but
is the logic of the decorator correct ? anybody can tell me ?

My code is attached below:



import time


def memoize(f):
cache = {}

def g(*args):
if args[1] == "avg":
sum_key_arr = sum(list(args[0])) / len(list(args[0]))
elif args[1] == "sum":
sum_key_arr = sum(list(args[0]))
if sum_key_arr not in cache:
for (
key,
value,
) in (
cache.items()
):  # key in dict cannot be an array so I use the sum of the
array as the key
if (
abs(sum_key_arr - key) <= 50
):  # threshold is great here so that all values are
approximated!
# print('approximated')
return cache[key]
else:
# print('not approximated')
cache[sum_key_arr] = f(args[0], args[1])
return cache[sum_key_arr]

return g


@memoize
def aggregate(dict_list_arr, operation):
if operation == "avg":
return sum(list(dict_list_arr)) / len(list(dict_list_arr))
if operation == "sum":
return sum(list(dict_list_arr))
return None


t = time.time()
for i in range(200, 15000):
res = aggregate(list(range(i)), "avg")

elapsed = time.time() - t
print(res)
print(elapsed)
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Fwd: Can you help me with this Python question?

2022-10-13 Thread Axy via Python-list
Well, although I never used pandas and never will, if that's about 
artworks, that's mine.


Obviously, you need to iterate columns and sum values returned by the 
snippet you provided. A quick search tells us to use colums property. 
So, it might look like this:


na_sum = sum(df[name].isnull().sum() for name in df.columns)

Axy

On 13/10/2022 13:44, Sarah Wallace wrote:

For a python class I am taking..

In this challenge, you'll be working with a DataFrame that contains data
about artworks, and it contains many missing values.

Your task is to create a variable called na_sum that contains the total
number of missing values in the DataFrame. When that's completed, print out
your answer!

Hint: The code given below will give you the number of missing (NaN) values
for the *Name* column in the DataFrame. How would you edit the code to get
the missing values for every column in the DataFrame?
Extra hint: You'll be returning a single number which is the final sum() of
everything.

df['Name'].isnull().sum()


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


Fwd: Can you help me with this Python question?

2022-10-13 Thread Sarah Wallace
For a python class I am taking..

In this challenge, you'll be working with a DataFrame that contains data
about artworks, and it contains many missing values.

Your task is to create a variable called na_sum that contains the total
number of missing values in the DataFrame. When that's completed, print out
your answer!

Hint: The code given below will give you the number of missing (NaN) values
for the *Name* column in the DataFrame. How would you edit the code to get
the missing values for every column in the DataFrame?
Extra hint: You'll be returning a single number which is the final sum() of
everything.

df['Name'].isnull().sum()

-- 
Thanks!

*Sarah Wallace*
sarah.wallac...@gmail.com




-- 
Thanks!

*Sarah Wallace*
sarah.wallac...@gmail.com
214.300.1064
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Help me with this code

2013-11-07 Thread Piet van Oostrum
chovd...@gmail.com writes:


> Hi friends
>
> help me with the following code. Im able to execute the code but getting 
> wrong output
>
> def sequence_b(N):
> N = 10
> result = 0
> for k in xrange (1,N):
> result  +=  ((-1) ** (k+1))/2*k-1
> print result
> print sequence_b(10)
>
> This is the output which im getting
> -1
> -4
> -5
> -10
> -11
> -18
> -19
> -28
> -29
>
>
> But i want output as
> 1
> -1/3
> 1/5
> -1/7
> 1/9
> -1/11
> 1/13
> -1/15
> 1/17
> -1/19

You probably want this:

N = 10
result = 0
for k in range (1,N):
step = ((-1)**(k+1))/(2*k-1)
result += step
print(step)

Note: 
1. You don't use the parameter N, you immediately change it to 10. Leave the 
line N = 10 out.
2. Your function doesn't return its result, so it returns None. So the print 
sequence_b(10) dosn't make sense.
If the print is only for debugging the use the following:

def sequence_b(N):
result = 0
for k in range (1,N):
step = ((-1)**(k+1))/(2*k-1)
print(step) ## debug output
result += step
return result

print(sequence_b(10)) # print the result of the function call

[I use print() because I use Python 3]
-- 
Piet van Oostrum 
WWW: http://pietvanoostrum.com/
PGP key: [8DAE142BE17999C4]
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Help me with this code PLEASE

2013-11-06 Thread Grant Edwards
On 2013-11-06, Denis McMahon  wrote:
> On Wed, 06 Nov 2013 00:35:56 +0200, Nick the Gr33k wrote:
>
>> Now i realizes i just cannot store lists into it's columns because it
>> does not support a collection datatype.
>
> All databases support storing of collections, but *NOT THE WAY YOU WANT 
> TO DO IT*
>
> You refuse to do it the proper way, so you have to bodge a solution  
> oh wait  I'm having a flashback here 

To quote the great Casey Stengel "it's deja vu all over again".

http://www.bostonbaseball.com/whitesox/baseball_extras/yogi.html

-- 
Grant Edwards   grant.b.edwardsYow! I feel like a wet
  at   parking meter on Darvon!
  gmail.com
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Help me with this code PLEASE

2013-11-06 Thread Joel Goldstick
On Wed, Nov 6, 2013 at 3:49 AM, Mark Lawrence  wrote:
> On 06/11/2013 01:14, Nick the Gr33k wrote:
>>
>>
>> How, do i i proceed?
>
>
> If at first you don't succeed, keep asking on comp.lang.python until someone
> gives me the completely bodged solution that I keep asking for even if it's
> complete nonsense.
>
>
> --
> Python is the second best programming language in the world.
> But the best has yet to be invented.  Christian Tismer
>
> Mark Lawrence
>
> --
> https://mail.python.org/mailman/listinfo/python-list

Nikos,

I believe we are passed the time again where your enquiries slip from
legitimate questions to trolling behavior.  As was pointed out you
aren't close to any solution.  You should go off taking the advice and
tutoring you have had here, do some reading (yes, actually learn and
understand something), perhaps start from scratch reading basics of
computer programming, databases, html, unicode, operating systems, web
servers, etc. and come back when you have a solid basis to ask a
question.

I take my profession seriously.  Its fun, but it takes a lot of
reading and studying and practice to be good at it.  The fact that you
even presume that you are doing what software designers, or engineers
or programmers do to ply the craft is disgusting.  Its not that you
don't know, its that you don't know and are an arrogant idiot to
assume that you do.

Your threads all end up being a very poor man's version of the movie
Ground Hog Day.  The fact that you don't recognize how rude and
insulting you are, and how little you bring to the discussion will
inevitably lead to more personal attacks since you aren't holding up
your end of the technical back and forth.

Your repeated behavior of starting new threads is just a boring
childish method to demand attention.  You have our attention.  I
believe that most of us if not all of us think you are the most
aggravating, incompetent, rude, needy, manipulating,  person we have
ever seen online ever.  Go away, and come back (months from now at
least) when you have some redeeming human qualities.

-- 
Joel Goldstick
http://joelgoldstick.com
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Help me with this code PLEASE

2013-11-06 Thread Mark Lawrence

On 06/11/2013 01:14, Nick the Gr33k wrote:


How, do i i proceed?


If at first you don't succeed, keep asking on comp.lang.python until 
someone gives me the completely bodged solution that I keep asking for 
even if it's complete nonsense.


--
Python is the second best programming language in the world.
But the best has yet to be invented.  Christian Tismer

Mark Lawrence

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


Re: Help me with this code PLEASE

2013-11-06 Thread Antoon Pardon
Op 05-11-13 22:26, Nick the Gr33k schreef:

> I know i'm close to solution, i can feel it but i have some issues.
> The code we arr discussing is the following:

No you are not. You are just doing random changes, without any
understanding. If you had followed my suggestion and actually
read the documentation of fetchone, you would have realised
this wouldn't have worked either.

Your resistance to reading the documentation is making you lose
more time than you hoped to win by just going ahead and trying
things blindly.

> [Tue Nov 05 23:21:52 2013] [error] [client 176.92.96.218]   File
> "/home/nikos/public_html/cgi-bin/metrites.py", line 274, in 
> [Tue Nov 05 23:21:52 2013] [error] [client 176.92.96.218] visit =
> visit.split()
> [Tue Nov 05 23:21:52 2013] [error] [client 176.92.96.218]
> AttributeError: 'NoneType' object has no attribute 'split'

This is essentially the same error you had before. You have an object
of NoneType where you expect something different. Changing your code
to use split instead of iteration won't change the fact that you have
an object of NoneType.

-- 
Antoon Pardon
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Help me with this code

2013-11-05 Thread Dave Angel

On Tue, 5 Nov 2013 17:51:00 -0800 (PST), chovd...@gmail.com wrote:

result  +=  ((-1) ** (k+1))/2*k-1


One of two things are happening here. Maybe both.

You're using Python 2.x (and should havesaid so) where integer 
division is truncated.


You're missing around some part of the intended denominator. Check 
operator precedence rules.


--
DaveA

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


Re: Help me with this code

2013-11-05 Thread Steven D'Aprano
On Tue, 05 Nov 2013 17:51:00 -0800, chovdary wrote:

> Hi friends
> 
> help me with the following code. Im able to execute the code but getting
> wrong output
[snip code]

You have this critical expression in your code:

result  +=  ((-1) ** (k+1))/2*k-1


It looks to me like you are using Python 2. Unfortunately, Python 2 had a 
design flaw that wasn't corrected until Python 3, where division is the 
integer division operator:

1/2 => 0 rather than 0.5.

There are three ways to fix this:

1) convert one of the numbers to a float:

   result  +=  float((-1) ** (k+1))/2*k-1


will probably do the trick. Although I'm not sure if the denominator is 
meant to be just 2, as you have above, or (2*k-1).


2) Put "from __future__ import division" as the very first line of your 
program. It must appear before any other code.

3) Avoid floating point numbers and use actual fractions, which will give 
you exact values instead of approximate. At the beginning of your code, 
put

from fractions import Fraction

and then change the code to look something like this:

result  +=  Fraction((-1) ** (k+1))/Fraction(2*k-1)




-- 
Steven
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Help me with this code

2013-11-05 Thread MRAB

On 06/11/2013 01:51, chovd...@gmail.com wrote:

Hi friends

help me with the following code. Im able to execute the code but getting wrong 
output

def sequence_b(N):
 N = 10
 result = 0
 for k in xrange (1,N):
 result  +=  ((-1) ** (k+1))/2*k-1
 print result
print sequence_b(10)

This is the output which im getting
-1
-4
-5
-10
-11
-18
-19
-28
-29


But i want output as
1
-1/3
1/5
-1/7
1/9
-1/11
1/13
-1/15
1/17
-1/19


1. Multiplication and division take precedence over addition and
subtraction, and both multiplication/division and addition/subtraction
are calculated left-to-right, so an expression like x/2*k-1 is
calculated as ((x/2)*k)-1.

2. In Python 2, dividing an integer by an integer gives an integer
result, e.g. 7/2 gives 3, not 3.5. The simplest way of fixing
that is to introduce a float into either the numerator or denominator.

That means that your expression should be, say:

((-1) ** (k + 1)) / (2.0 * k - 1)

3. It won't print fractions like 1/5, but instead decimals like 0.2.

4. You're adding the result of the expression to 'result' and then 
printing the value of 'result', so your output would be the results of:


-1
-1+(-1/3)
-1+(-1/3)+(1/5)

and so on.

5. Your function prints the result but doesn't return anyhing, so, by
default, it'll return None. Therefore, 'print sequence_b(10)' will
print 'None'. What you'll get is a series of numbers and then a None.

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


Representing fractions (was: Help me with this code)

2013-11-05 Thread Ben Finney
chovd...@gmail.com writes:

> def sequence_b(N):
> N = 10
> result = 0
> for k in xrange (1,N):
> result  +=  ((-1) ** (k+1))/2*k-1

Every number here is an integer. Python 2 will keep all the computations
integers by default::

$ python2
>>> 1 / 2
0

You want to use Python 3, which does “true division” by default::

$ python3
>>> 1 / 2
0.5

> But i want output as
> 1
> -1/3
> 1/5
> -1/7
> 1/9
> -1/11
> 1/13
> -1/15
> 1/17
> -1/19

You're not going to get that output from Python built-in number types.

If you want numbers which represent fractions (as opposed to integers,
or decimal numbers, or floating-point numbers), you want the ‘fractions’
module http://docs.python.org/3/library/fractions.html> which will
represent the number explicitly with numerator and denominator::

$ python3
>>> import fractions
>>> result = fractions.Fraction(1) / fractions.Fraction(2)
>>> result
Fraction(1, 2)
>>> print(result)
1/2

-- 
 \  “A hundred times every day I remind myself that […] I must |
  `\   exert myself in order to give in the same measure as I have |
_o__)received and am still receiving” —Albert Einstein |
Ben Finney

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


Help me with this code

2013-11-05 Thread chovdary
Hi friends

help me with the following code. Im able to execute the code but getting wrong 
output

def sequence_b(N):
N = 10
result = 0
for k in xrange (1,N):
result  +=  ((-1) ** (k+1))/2*k-1
print result
print sequence_b(10)

This is the output which im getting
-1
-4
-5
-10
-11
-18
-19
-28
-29


But i want output as
1
-1/3
1/5
-1/7
1/9
-1/11
1/13
-1/15
1/17
-1/19


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


Re: Help me with this code PLEASE

2013-11-05 Thread Ben Finney
Denis McMahon  writes:

> You have been told several times by several people how to do this 
> properly. You insist on using your bodged up solution instead. OK, we'll 
> all try and help you bodge up a solution[…]

Why? No-one here is obligated to help with implementing a solution we
agree is bad.

Now that helpful suggestions have been offered, and the OP continues to
obstinately refuse to learn, why not leave it there? Why not leave this
person to his own obstinacy and stop giving him the attention he
demands?

-- 
 \ “Computers are useless. They can only give you answers.” —Pablo |
  `\   Picasso |
_o__)  |
Ben Finney

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


Re: Help me with this code PLEASE

2013-11-05 Thread Nick the Gr33k

Στις 6/11/2013 12:54 πμ, ο/η John Gordon έγραψε:

The code i provided only worked once before it failed and managed to
store this:

counterID,host,refs,city,userOS,browser,visits,hits,download
-
1, 176-92-96-218.adsl.cyta.gr, Europe/Athens, Windows, Chrome,
-00-00 00:00:00, 1, ''

You've got a data inconsistancy here.  You give nine column names, but
only eight values.  It's impossible to tell which value belongs to which
column.  Which value is missing?



Yes, you have a point but thats why because 'refs' was an empty string 
for some reason and i have should have made this clear by putting a '' 
like i did with the 'downloads' at the end.


So it is not a matter of inconsistency.
Its weird though why it was stored this way, refs and downloads that is 
as an empty strings.


How, do i i proceed?
--
https://mail.python.org/mailman/listinfo/python-list


Re: Help me with this code PLEASE

2013-11-05 Thread Denis McMahon
On Wed, 06 Nov 2013 00:35:56 +0200, Nick the Gr33k wrote:

> Now i realizes i just cannot store lists into it's columns because it
> does not support a collection datatype.

All databases support storing of collections, but *NOT THE WAY YOU WANT 
TO DO IT*

You refuse to do it the proper way, so you have to bodge a solution  
oh wait  I'm having a flashback here 

You have been told several times by several people how to do this 
properly. You insist on using your bodged up solution instead. OK, we'll 
all try and help you bodge up a solution, but I will tell you now, in 
advance, in very clear terms:

*ONE DAY YOUR BODGED UP SOLUTION WILL BREAK BECAUSE THE TOTAL LIST OF 
TORRENTS THAT A USER HAS DOWNLOADED, WHEN PACKAGED INTO A STRING, WILL BE 
BIGGER THAN THE DATABASE STRING FIELD YOU ASSIGNED*

There may be another issue that you need to consider.

Supposing a torrent file name is 40 characters long. Supposing a user 
downloads up to 20 torrents a day. That means in one year, their list 
will contain 7300 entries, and the character string in the database will 
be 365 * 20 * 40 + 365 * 20 - 1 = 299299 characters.

And you're going to convert this huge string into a list and then back 
into a string 20 times a day for every user just to add another 40 
characters to the end.

Do you expect 100 users? 1,000? 10,000? 100,000?

Let's assume that you have a million users. In a year, every day you're 
reading and writing about 6000 Gbytes a day from and to the database.

Holy smoke batman, that's a lot of data.

Or you could use the database properly, and just write the 40 byte 
torrent file name to the database with a pointer to the user's record 
every time a user downloads another torrent.

-- 
Denis McMahon, denismfmcma...@gmail.com
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Help me with this code PLEASE

2013-11-05 Thread Mark Lawrence

On 05/11/2013 22:31, Cameron Simpson wrote:

On 05Nov2013 20:09, Nikos  wrote:

O even better an rdbms than allows complex data such as tuples,
lists, dicts to be saved into the db as they are so i dont have to
cobvet back and forth each time.


If you're just using the db for storage or adhoc and arbitrary
python objects (and not querying the stored values via SQL - eg
WHERE), then: make a column of type BLOB, convert Python values to
bytes using pickle, store. And of course the reverse.

It is not a great use of an RDB, but it seems to adhere to what you ask.



How do expect the OP to understand a BLOB or pickle or chutney when he 
doesn't understand why he can't iterate around a Nonetype object?


--
Python is the second best programming language in the world.
But the best has yet to be invented.  Christian Tismer

Mark Lawrence

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


Re: Help me with this code PLEASE

2013-11-05 Thread bob gailer

There is also the shelve module.

It uses pickle to marshal a Python object, then stores it in a file 
under a key.


Sample code from the module documentation:

import shelve

d = shelve.open(filename) # open -- file may get suffix added by 
low-level library


d[key] = data   # store data at key (overwrites old data if using an 
existing key)
data = d[key]   # retrieve a COPY of data at key (raise KeyError if no 
such key)

del d[key]  # delete data stored at key (raises KeyError if no such key)
flag = key in d# true if the key exists
klist = list(d.keys()) # a list of all existing keys (slow!)


--
Bob Gailer
919-636-4239
Chapel Hill NC

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


Re: Help me with this code PLEASE

2013-11-05 Thread Cameron Simpson
On 05Nov2013 20:09, Nikos  wrote:
> O even better an rdbms than allows complex data such as tuples,
> lists, dicts to be saved into the db as they are so i dont have to
> cobvet back and forth each time.

If you're just using the db for storage or adhoc and arbitrary
python objects (and not querying the stored values via SQL - eg
WHERE), then: make a column of type BLOB, convert Python values to
bytes using pickle, store. And of course the reverse.

It is not a great use of an RDB, but it seems to adhere to what you ask.
-- 
Cameron Simpson 
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Help me with this code PLEASE

2013-11-05 Thread John Gordon
In  Nick the Gr33k  
writes:

> The code i provided only worked once before it failed and managed to 
> store this:
>   
> counterID,host,refs,city,userOS,browser,visits,hits,download
> -
> 1, 176-92-96-218.adsl.cyta.gr, Europe/Athens, Windows, Chrome, 
> -00-00 00:00:00, 1, ''

You've got a data inconsistancy here.  You give nine column names, but
only eight values.  It's impossible to tell which value belongs to which
column.  Which value is missing?

> 'visit's column is full of zeroes. Perhaps this is considered  as Null 
> for Python?

Python does not consider zero to be a null value.  (It will compare as
equal to False, but that's not the same.)

> But ref is also null, why the code didn't complain for 'ref' which is 
> retrieved just before 'visits'?

Are you sure refs is null?  According to the sample values you gave, refs
is 'Europe/Athens'.

-- 
John Gordon Imagine what it must be like for a real medical doctor to
gor...@panix.comwatch 'House', or a real serial killer to watch 'Dexter'.

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


Re: Help me with this code PLEASE

2013-11-05 Thread Mark Lawrence

On 05/11/2013 22:28, Nick the Gr33k wrote:

Στις 6/11/2013 12:06 πμ, ο/η John Gordon έγραψε:

In  Nick the Gr33k 
writes:


# fetch those columns that act as lists but are stored as
strings
cur.execute('''SELECT refs, visits, downloads FROM visitors
WHERE
counterID = %s and host = %s''', (cID, host) )
data = cur.fetchone()



if cur.rowcount:
# unpack data into variables
(ref, visit, download) = data

visit = visit.split()

[Tue Nov 05 23:21:52 2013] [error] [client 176.92.96.218] visit =
visit.split()
[Tue Nov 05 23:21:52 2013] [error] [client 176.92.96.218]
AttributeError: 'NoneType' object has no attribute 'split'


It appears that in the row you're fetching from the visitors table, the
'visits' column is NULL.

Your next step is track down why.


Indeed.

The code i provided only worked once before it failed and managed to
store this:


counterID,host,refs,city,userOS,browser,visits,hits,download
-
1, 176-92-96-218.adsl.cyta.gr, Europe/Athens, Windows, Chrome,
-00-00 00:00:00, 1, ''

'visit's column is full of zeroes. Perhaps this is considered  as Null
for Python?

But ref is also null, why the code didn't complain for 'ref' which is
retrieved just before 'visits'?

How, i eman what si the way to investigate this further?



Don't worry about it.  Just leave it with the crack team that I've put 
together for you.  We should have some answers some time tomorrow.


--
Python is the second best programming language in the world.
But the best has yet to be invented.  Christian Tismer

Mark Lawrence

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


Re: Help me with this code PLEASE

2013-11-05 Thread Nick the Gr33k

Στις 6/11/2013 12:15 πμ, ο/η Piet van Oostrum έγραψε:

Nick the Gr33k  writes:



IAM STRUGGLING WITH IT 2 DAYS NOW AND I CANNOT GET IT TO WORK.

ALL I WANT IT TO DO IS JUST

1. RETRIEVE 3 COLUMNS THAT CONSIST OF 3 LONG STRINGS
2. CONVERT LONG STRINGS TO LISTS
3. ADD SOME CURRENT VALUES TO THOSE LISTS
4. CONVERT FROM LISTS TO LONG STRINGS SO I CAN STORE SUCCESSFULLY LIST
PYTHON DATATYPE TO MYSQL SCALAR STRING.

EVERYHTIGN I TRIED FAILED.


Then why don't you use the simple solution: use a relational database to store 
the data?




--
How you mean?

Up until now i was using for years MySQL.
Now i realizes i just cannot store lists into it's columns because it 
does not support a collection datatype.


perhaps its time to use postgresql so to avoid these coversions?

does postgresql support "collection" columns?

and is its synta
--
https://mail.python.org/mailman/listinfo/python-list


Re: Help me with this code PLEASE

2013-11-05 Thread Nick the Gr33k

Στις 6/11/2013 12:06 πμ, ο/η John Gordon έγραψε:

In  Nick the Gr33k  
writes:


# fetch those columns that act as lists but are stored as 
strings
cur.execute('''SELECT refs, visits, downloads FROM visitors 
WHERE
counterID = %s and host = %s''', (cID, host) )
data = cur.fetchone()



if cur.rowcount:
# unpack data into variables
(ref, visit, download) = data

visit = visit.split()

[Tue Nov 05 23:21:52 2013] [error] [client 176.92.96.218] visit =
visit.split()
[Tue Nov 05 23:21:52 2013] [error] [client 176.92.96.218]
AttributeError: 'NoneType' object has no attribute 'split'


It appears that in the row you're fetching from the visitors table, the
'visits' column is NULL.

Your next step is track down why.


Indeed.

The code i provided only worked once before it failed and managed to 
store this:



counterID,host,refs,city,userOS,browser,visits,hits,download
-
1, 176-92-96-218.adsl.cyta.gr, Europe/Athens, Windows, Chrome, 
-00-00 00:00:00, 1, ''


'visit's column is full of zeroes. Perhaps this is considered  as Null 
for Python?


But ref is also null, why the code didn't complain for 'ref' which is 
retrieved just before 'visits'?


How, i eman what si the way to investigate this further?

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


Re: Help me with this code PLEASE

2013-11-05 Thread Piet van Oostrum
Nick the Gr33k  writes:

>
> IAM STRUGGLING WITH IT 2 DAYS NOW AND I CANNOT GET IT TO WORK.
>
> ALL I WANT IT TO DO IS JUST
>
> 1. RETRIEVE 3 COLUMNS THAT CONSIST OF 3 LONG STRINGS
> 2. CONVERT LONG STRINGS TO LISTS
> 3. ADD SOME CURRENT VALUES TO THOSE LISTS
> 4. CONVERT FROM LISTS TO LONG STRINGS SO I CAN STORE SUCCESSFULLY LIST
> PYTHON DATATYPE TO MYSQL SCALAR STRING.
>
> EVERYHTIGN I TRIED FAILED.

Then why don't you use the simple solution: use a relational database to store 
the data?
-- 
Piet van Oostrum 
WWW: http://pietvanoostrum.com/
PGP key: [8DAE142BE17999C4]
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Help me with this code PLEASE

2013-11-05 Thread John Gordon
In  Nick the Gr33k  
writes:

>   # fetch those columns that act as lists but are stored as 
> strings
>   cur.execute('''SELECT refs, visits, downloads FROM visitors 
> WHERE 
> counterID = %s and host = %s''', (cID, host) )
>   data = cur.fetchone()

>   if cur.rowcount:
>   # unpack data into variables
>   (ref, visit, download) = data
>   
>   visit = visit.split()
>   
> [Tue Nov 05 23:21:52 2013] [error] [client 176.92.96.218] visit = 
> visit.split()
> [Tue Nov 05 23:21:52 2013] [error] [client 176.92.96.218] 
> AttributeError: 'NoneType' object has no attribute 'split'

It appears that in the row you're fetching from the visitors table, the
'visits' column is NULL.

Your next step is track down why.

-- 
John Gordon Imagine what it must be like for a real medical doctor to
gor...@panix.comwatch 'House', or a real serial killer to watch 'Dexter'.

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


Re: Help me with this code PLEASE

2013-11-05 Thread Chris Angelico
On Wed, Nov 6, 2013 at 8:17 AM, Mark Lawrence  wrote:
> I've taken a different approach. I've put the contract out to tender and
> hereby give you the winners
> http://www.mudefordwoodcommunitycentre.co.uk/playgroup-and-tiny-tots/

Sounds good! But I don't see a list of their technologies - do they
use open source tools like Python, or is it proprietary stuff like
Fisher-Price(tm) "My First Website"? The latter is probably sufficient
for these tasks, but I would hope that your contractors are able to
master real-world problems too.

Nikos, once again you are getting yourself into a tizz over this,
because the problem is urgent to you. You need to stop programming
professionally and make it a hobby, so that you remove the time
pressure. You cope badly with pressure, so why torture yourself? Get
into a completely different industry for your day job, and do all your
programming in the evenings. You'll find lots of things easier and
less stressful.

ChrisA
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Help me with this code PLEASE

2013-11-05 Thread Mark Lawrence

On 05/11/2013 21:17, Mark Lawrence wrote:

On 05/11/2013 20:19, John Gordon wrote:

In  Nick the Gr33k 
writes:


IAM STRUGGLING WITH IT 2 DAYS NOW AND I CANNOT GET IT TO WORK.



ALL I WANT IT TO DO IS JUST



1. RETRIEVE 3 COLUMNS THAT CONSIST OF 3 LONG STRINGS
2. CONVERT LONG STRINGS TO LISTS
3. ADD SOME CURRENT VALUES TO THOSE LISTS
4. CONVERT FROM LISTS TO LONG STRINGS SO I CAN STORE SUCCESSFULLY LIST
PYTHON DATATYPE TO MYSQL SCALAR STRING.



EVERYHTIGN I TRIED FAILED.


How did it fail?

Error message?
No results at all?
Different results than you wanted?  If so, how did they differ?



Dear John,

This approach has already been tried by Dave Angel, Steven D'Aprano and
Chris Angelico amongst others and has failed dismally so why should you
be any different?

I've taken a different approach. I've put the contract out to tender and
hereby give you the winners
http://www.mudefordwoodcommunitycentre.co.uk/playgroup-and-tiny-tots/

Part of the rationale is that they're literally 100 yards from my front
door so communicating with them effectively should be extremely easy.
This will be particularly useful when it comes down to debugging such
difficult issues as "> TypeError: 'NoneType' object is not iterable".

I'm looking forward to working in partnership with them and am convinced
that within a few days all of Nikos' problems will have been solved.



Oh dear it looks as if we might have to renegotiate the contract as I 
understand that we've an almost intractable problem to solve it's 
"AttributeError: 'NoneType' object has no attribute 'split'".


--
Python is the second best programming language in the world.
But the best has yet to be invented.  Christian Tismer

Mark Lawrence

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


Re: Help me with this code PLEASE

2013-11-05 Thread Nick the Gr33k

Στις 5/11/2013 10:19 μμ, ο/η John Gordon έγραψε:

In  Nick the Gr33k  
writes:


IAM STRUGGLING WITH IT 2 DAYS NOW AND I CANNOT GET IT TO WORK.



ALL I WANT IT TO DO IS JUST



1. RETRIEVE 3 COLUMNS THAT CONSIST OF 3 LONG STRINGS
2. CONVERT LONG STRINGS TO LISTS
3. ADD SOME CURRENT VALUES TO THOSE LISTS
4. CONVERT FROM LISTS TO LONG STRINGS SO I CAN STORE SUCCESSFULLY LIST
PYTHON DATATYPE TO MYSQL SCALAR STRING.



EVERYHTIGN I TRIED FAILED.


How did it fail?

Error message?
No results at all?
Different results than you wanted?  If so, how did they differ?




I know i'm close to solution, i can feel it but i have some issues.
The code we arr discussing is the following:


=
# ~ DATABASE INSERTS ~
=
if cookieID != 'nikos' and ( os.path.exists( path + page ) or 
os.path.exists( cgi_path + page ) ) and re.search( 
r'(amazon|google|proxy|cloud|reverse|fetch|msn|who|spider|crawl|ping)', 
host ) is None:


try:
		# if first time for webpage; create new record( primary key is 
automatic, hit is defaulted ), if page exists then update record
		cur.execute('''INSERT INTO counters (url) VALUES (%s) ON DUPLICATE KEY 
UPDATE hits = hits + 1''', page )

cID = cur.lastrowid

# fetch those columns that act as lists but are stored as 
strings
		cur.execute('''SELECT refs, visits, downloads FROM visitors WHERE 
counterID = %s and host = %s''', (cID, host) )

data = cur.fetchone()

ref = visit = download = []
if cur.rowcount:
# unpack data into variables
(ref, visit, download) = data

# retrieve long strings and convert them into lists 
respectively
ref = ref.split()
visit = visit.split()
download = download.split()
else:
# initiate these values
ref = ref
visit = lastvisit
download = ''

refs = visits = downloads = []
# add current values to each list respectively
refs.append( ref )
visits.append( visit )
downloads.append( download )

# convert lists back to longstrings
refs = ', '.join( refs )
visits = ', '.join( visits )
downloads = ', '.join( downloads )

# save this visit as an entry into database
		cur.execute('''INSERT INTO visitors (counterID, refs, host, city, 
useros, browser, visits, downloads) VALUES (%s, %s, %s, %s, %s, %s, %s, %s)
		ON DUPLICATE KEY UPDATE refs = %s, visits = %s, hits = hits + 1, 
downloads = %s''',
		(cID, refs, host, city, useros, browser, visits, downloads, refs, 
visits, downloads) )


con.commit()
except pymysql.ProgrammingError as e:
print( repr(e) )
con.rollback()
sys.exit(0)
===


[Tue Nov 05 23:21:52 2013] [error] [client 176.92.96.218]   File 
"/home/nikos/public_html/cgi-bin/metrites.py", line 274, in 
[Tue Nov 05 23:21:52 2013] [error] [client 176.92.96.218] visit = 
visit.split()
[Tue Nov 05 23:21:52 2013] [error] [client 176.92.96.218] 
AttributeError: 'NoneType' object has no attribute 'split'

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


Re: Help me with this code PLEASE

2013-11-05 Thread Mark Lawrence

On 05/11/2013 20:19, John Gordon wrote:

In  Nick the Gr33k  
writes:


IAM STRUGGLING WITH IT 2 DAYS NOW AND I CANNOT GET IT TO WORK.



ALL I WANT IT TO DO IS JUST



1. RETRIEVE 3 COLUMNS THAT CONSIST OF 3 LONG STRINGS
2. CONVERT LONG STRINGS TO LISTS
3. ADD SOME CURRENT VALUES TO THOSE LISTS
4. CONVERT FROM LISTS TO LONG STRINGS SO I CAN STORE SUCCESSFULLY LIST
PYTHON DATATYPE TO MYSQL SCALAR STRING.



EVERYHTIGN I TRIED FAILED.


How did it fail?

Error message?
No results at all?
Different results than you wanted?  If so, how did they differ?



Dear John,

This approach has already been tried by Dave Angel, Steven D'Aprano and 
Chris Angelico amongst others and has failed dismally so why should you 
be any different?


I've taken a different approach. I've put the contract out to tender and 
hereby give you the winners 
http://www.mudefordwoodcommunitycentre.co.uk/playgroup-and-tiny-tots/


Part of the rationale is that they're literally 100 yards from my front 
door so communicating with them effectively should be extremely easy. 
This will be particularly useful when it comes down to debugging such 
difficult issues as "> TypeError: 'NoneType' object is not iterable".


I'm looking forward to working in partnership with them and am convinced 
that within a few days all of Nikos' problems will have been solved.


--
Python is the second best programming language in the world.
But the best has yet to be invented.  Christian Tismer

Mark Lawrence

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


Re: Help me with this code PLEASE

2013-11-05 Thread John Gordon
In  Nick the Gr33k  
writes:

> IAM STRUGGLING WITH IT 2 DAYS NOW AND I CANNOT GET IT TO WORK.

> ALL I WANT IT TO DO IS JUST

> 1. RETRIEVE 3 COLUMNS THAT CONSIST OF 3 LONG STRINGS
> 2. CONVERT LONG STRINGS TO LISTS
> 3. ADD SOME CURRENT VALUES TO THOSE LISTS
> 4. CONVERT FROM LISTS TO LONG STRINGS SO I CAN STORE SUCCESSFULLY LIST 
> PYTHON DATATYPE TO MYSQL SCALAR STRING.

> EVERYHTIGN I TRIED FAILED.

How did it fail?

Error message?
No results at all?
Different results than you wanted?  If so, how did they differ?

-- 
John Gordon Imagine what it must be like for a real medical doctor to
gor...@panix.comwatch 'House', or a real serial killer to watch 'Dexter'.

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


Re: Help me with this code PLEASE

2013-11-05 Thread Denis McMahon
On Tue, 05 Nov 2013 20:09:42 +0200, Nick the Gr33k wrote:

> Denis, i have already provided my code trying to do what i need and i
> need some commendation on how to make it work.

Nick, you're obviously trying to code way above your abilities.

If you want me to write your code, you will have to pay my day rate, and 
you can't afford it. If you could afford it, you'd be paying someone 
competent already.

Here, have a code example, this works, although the formatting might get 
broken. The database "ntg1" has a single table called str_idx defined as 
follows:

create table idx_str ( idx int primary key, str varchar[1024] );

#!/usr/bin/python

import random
import sqlite3

def make_list( length ):
l = []
if ( length < 1 ):
return l
for count in range( 0, length ):
s = '';
for i in range( 1, random.randrange( 4, 12 ) ):
c = chr( random.randrange( 97, 123 ) )
s += c
l.append( s )
return l

def list_to_string( l ):
return "|".join( l )

def string_to_list( s ):
return s.split( "|" )

l1 = make_list( 10 )
print "l1 -> ", l1
s = list_to_string( l1 )
print "s -> ", s
l2 = string_to_list( s )
print "l2 -> ", l2
print "l1 == l2 -> ", l2 == l1

l2 = make_list( 10 )
l3 = make_list( 10 )
l4 = make_list( 10 )

print "Original Lists"
print "l1 -> ", l1
print "l2 -> ", l2
print "l3 -> ", l3
print "l4 -> ", l4

conn = sqlite3.connect( "ntg1" )
cur = conn.cursor()
cur.execute( "delete from idx_str where idx is not null" )
cur.execute( 
"insert into idx_str values ( 1, '{0}' )".format( 
list_to_string( l1 ) ) )
cur.execute( 
"insert into idx_str values ( 2, '{0}' )".format( 
list_to_string( l2 ) ) )
cur.execute( 
"insert into idx_str values ( 3, '{0}' )".format( 
list_to_string( l3 ) ) )
cur.execute( 
"insert into idx_str values ( 4, '{0}' )".format( 
list_to_string( l4 ) ) )
conn.commit()
conn.close()

print "Lists now in DB"
print "Reading 1 record at a time"

conn2 = sqlite3.connect( "ntg1" )
cur2 = conn2.cursor()

cur2.execute( "select * from idx_str where idx = 1" );
row = cur2.fetchone()
print "stored 1 -> ", row[ 1 ]
print "l1 -> ", string_to_list( row[ 1 ] )

cur2.execute( "select * from idx_str where idx = 2" );
row = cur2.fetchone()
print "stored 2 -> ", row[ 1 ]
print "l2 -> ", string_to_list( row[ 1 ] )

cur2.execute( "select * from idx_str where idx = 3" );
row = cur2.fetchone()
print "stored 3 -> ", row[ 1 ]
print "l3 -> ", string_to_list( row[ 1 ] )

cur2.execute( "select * from idx_str where idx = 4" );
row = cur2.fetchone()
print "stored 4 -> ", row[ 1 ]
print "l4 -> ", string_to_list( row[ 1 ] )

conn2.close()

print "Reading all records at once"

conn3 = sqlite3.connect( "ntg1" )

cur3 = conn3.cursor()

cur3.execute( "select * from idx_str" );

row = cur3.fetchone()

while not row == None:

print "stored ", row[ 0 ], " -> ", row[ 1 ]
print "list ", row[ 0 ], " -> ", string_to_list( row[ 1 ] )
row = cur3.fetchone()

conn3.close()

One thing you haven't considered, what happens if a user has so many 
downloads in his list that the converted list doesn't fit in the declared 
string column width in your database?

-- 
Denis McMahon, denismfmcma...@gmail.com
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Help me with this code PLEASE

2013-11-05 Thread Joel Goldstick
On Tue, Nov 5, 2013 at 1:11 PM, Tobiah  wrote:
> All this problem arises because MySQL's hasn't got a datatype able to store
> an array of elements, a list.
>
> Um, yes it does.  It's called a table.
> --
> https://mail.python.org/mailman/listinfo/python-list

Perhaps we are splitting hairs, but a relational database will let you
stuff any text you want in a text field, but First Normal Form is a
necessary (and not complete) requirement (from wikipedia)

First normal form (1NF) is a property of a relation in a relational
database. A relation is in first normal form if the domain of each
attribute contains only atomic values, and the value of each attribute
contains only a single value from that domain.[1]

If you don't have a single thing in a field, you can't search or sort
it or filter by its value with sql.  Since those are very important
properties of sql, not using 1nf is similar to tossing a whole
warehouse of stuff in a warehouse without any sort of organization for
where different things are to be put, and therefore, where they can be
found.

If you don't have first normal form data, you are misusing a
relational database.  If you don't want to learn about relational
databases and what they can do to help you construct software that
solves interesting problems, then perhaps you are not curious enough
to ever become competent in the field.

That said, sql is a different beast than is python or php or any
procedural (or oops) language.  Just like writing HTML and CSS is
something that people who write computer programs for the web might
do,  its a total shift in thinking from writing python.

There is no rule that a person must be a computer programmer, but if a
person wants to be a competent and successful computer programmer, one
must learn from the evolving understanding of the last 50 years or so.
 Its a craft of understanding how to divide impossibly large problems
into understandable pieces -- using the best tools for each piece.
You may say this is just one person's opinion -- but then again, I'm
not the one screaming in all caps about the same several problems over
and over for the last year or more on this list.  The results show no
more understanding or growth in skills, and an attitude that never
shows a glimmer of interest in learning.




-- 
Joel Goldstick
http://joelgoldstick.com
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Help me with this code PLEASE

2013-11-05 Thread Tobiah

All this problem arises because MySQL's hasn't got a datatype able to store an 
array of elements, a list.

Um, yes it does.  It's called a table.
--
https://mail.python.org/mailman/listinfo/python-list


Re: Help me with this code PLEASE

2013-11-05 Thread Nick the Gr33k

Στις 5/11/2013 8:02 μμ, ο/η Denis McMahon έγραψε:

On Tue, 05 Nov 2013 19:06:25 +0200, Nick the Gr33k wrote:


IAM STRUGGLING WITH IT 2 DAYS NOW AND I CANNOT GET IT TO WORK.


Try starting with something simple. The following is a step by step guide
to working out how you need to do this. Follow all the steps. Do not skip
any steps. Each stage builds on the previous code.

Write python code to create a list of strings and print out the elements
of the list. This just needs to generate a list of n entries of m
characters in length of random character data.

Write python code to concatenate a list of strings into some longer
string using a separator and print out the long string.

Now add some python code to split the long string into a list based on
the separator, and print out the list elements.

Once you have this code working, make a test table with a single string
column and an integer index field in a test database.

Now this is starting to push the boundaries of your abilities, but write
code to create two lists, and store them with indexes 1 and 2
respectively.

Now, and this is really really going to tax you, write some more code
that will first retrieve the string for index 1, convert it back into a
list, and display the list contents, then do the same for list 2, then
retrieve all the data from the db and reproduce each list in turn.

Then you might be ready to try coding what you're trying to code.




--
Denis, i have already provided my code trying to do what i need and i 
need some commendation on how to make it work.


O even better an rdbms than allows complex data such as tuples, lists, 
dicts to be saved into the db as they are so i dont have to cobvet back 
and forth each time.

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


Re: Help me with this code PLEASE

2013-11-05 Thread Denis McMahon
On Tue, 05 Nov 2013 19:06:25 +0200, Nick the Gr33k wrote:

> IAM STRUGGLING WITH IT 2 DAYS NOW AND I CANNOT GET IT TO WORK.

Try starting with something simple. The following is a step by step guide 
to working out how you need to do this. Follow all the steps. Do not skip 
any steps. Each stage builds on the previous code.

Write python code to create a list of strings and print out the elements 
of the list. This just needs to generate a list of n entries of m 
characters in length of random character data.

Write python code to concatenate a list of strings into some longer 
string using a separator and print out the long string.

Now add some python code to split the long string into a list based on 
the separator, and print out the list elements.

Once you have this code working, make a test table with a single string 
column and an integer index field in a test database.

Now this is starting to push the boundaries of your abilities, but write 
code to create two lists, and store them with indexes 1 and 2 
respectively.

Now, and this is really really going to tax you, write some more code 
that will first retrieve the string for index 1, convert it back into a 
list, and display the list contents, then do the same for list 2, then 
retrieve all the data from the db and reproduce each list in turn.

Then you might be ready to try coding what you're trying to code.

-- 
Denis McMahon, denismfmcma...@gmail.com
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Help me with this code PLEASE

2013-11-05 Thread Nick the Gr33k

Στις 5/11/2013 7:41 μμ, ο/η Steven D'Aprano έγραψε:

On Tue, 05 Nov 2013 19:06:25 +0200, Nick the Gr33k wrote:


ALL I WANT IT TO DO IS JUST

1. RETRIEVE 3 COLUMNS THAT CONSIST OF 3 LONG STRINGS 2. CONVERT LONG
STRINGS TO LISTS
3. ADD SOME CURRENT VALUES TO THOSE LISTS 4. CONVERT FROM LISTS TO LONG
STRINGS SO I CAN STORE SUCCESSFULLY LIST PYTHON DATATYPE TO MYSQL SCALAR
STRING.



You cannot do this with Python. You should use a better language more
suited to you. Try this:

http://php.net/manual/en/tutorial.php


I hope that you will be much happier with this, since you are struggling
to get Python to work the way you want it to.





--
Steven, i want switch to php, i uses to start with php, then perl and 
now i ended up with python.


All this problem arises because MySQL's hasn't got a datatype able to 
store an array of elements, a list.


Is there some other database, sql connector i can use except 'pymysql' 
that supports a "collection" record type?


postgresql or somethign similar?
If it does i will switch to that and avoid these tedius convertions from 
long strings => list and backwise.


Somehting that can just take Python's datatypes 'list' or 'tuple' or 
'dict' as tehy are and just store them into the database convertionless.



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


Re: Help me with this code PLEASE

2013-11-05 Thread Antoon Pardon
Op 05-11-13 18:41, Steven D'Aprano schreef:
> On Tue, 05 Nov 2013 19:06:25 +0200, Nick the Gr33k wrote:
> 
>> ALL I WANT IT TO DO IS JUST
>>
>> 1. RETRIEVE 3 COLUMNS THAT CONSIST OF 3 LONG STRINGS 2. CONVERT LONG
>> STRINGS TO LISTS
>> 3. ADD SOME CURRENT VALUES TO THOSE LISTS 4. CONVERT FROM LISTS TO LONG
>> STRINGS SO I CAN STORE SUCCESSFULLY LIST PYTHON DATATYPE TO MYSQL SCALAR
>> STRING.
> 
> 
> You cannot do this with Python. You should use a better language more 
> suited to you. Try this:
> 
> http://php.net/manual/en/tutorial.php
> 
> 
> I hope that you will be much happier with this, since you are struggling 
> to get Python to work the way you want it to.

Steve, what went on in your mind? If Nikos follows you advise he will
probably come here with all kinds of php questions.

-- 
Antoon Pardon

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


Re: Help me with this code PLEASE

2013-11-05 Thread Steven D'Aprano
On Tue, 05 Nov 2013 19:06:25 +0200, Nick the Gr33k wrote:

> ALL I WANT IT TO DO IS JUST
> 
> 1. RETRIEVE 3 COLUMNS THAT CONSIST OF 3 LONG STRINGS 2. CONVERT LONG
> STRINGS TO LISTS
> 3. ADD SOME CURRENT VALUES TO THOSE LISTS 4. CONVERT FROM LISTS TO LONG
> STRINGS SO I CAN STORE SUCCESSFULLY LIST PYTHON DATATYPE TO MYSQL SCALAR
> STRING.


You cannot do this with Python. You should use a better language more 
suited to you. Try this:

http://php.net/manual/en/tutorial.php


I hope that you will be much happier with this, since you are struggling 
to get Python to work the way you want it to.


-- 
Steven
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Help me with this code PLEASE

2013-11-05 Thread mm0fmf

EVERYHTIGN I TRIED FAILED.


Maybe try some of the advice you have been given instead?

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


Re: Help me with this code PLEASE

2013-11-05 Thread Antoon Pardon

> =
> 
> IAM STRUGGLING WITH IT 2 DAYS NOW AND I CANNOT GET IT TO WORK.
> 
> ALL I WANT IT TO DO IS JUST
> 
> 1. RETRIEVE 3 COLUMNS THAT CONSIST OF 3 LONG STRINGS
> 2. CONVERT LONG STRINGS TO LISTS
> 3. ADD SOME CURRENT VALUES TO THOSE LISTS
> 4. CONVERT FROM LISTS TO LONG STRINGS SO I CAN STORE SUCCESSFULLY LIST PYTHON 
> DATATYPE TO MYSQL SCALAR STRING.
> 
> EVERYHTIGN I TRIED FAILED.

Don't start a new thread, where you just ask the same question, you already 
asked before.
The answer will not be different just because you started a new thread.

Did you already read the documentation of fetchone?

-- 
Antoon Pardon


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


Help me with this code PLEASE

2013-11-05 Thread Nick the Gr33k

==
# fetch those columns that act as lists but are stored as 
strings
		cur.execute('''SELECT refs, visits, downloads FROM visitors WHERE 
counterID = %s and host = %s''', (cID, host) )

data = cur.fetchone()

ref = visit = download = []
if cur.rowcount:
# unpack data into variables
(ref, visit, download) = data

# retrieve long strings and convert them into lists 
respectively
ref = ref.split()
visit = visit.split()
download = download.split()
else:
# initiate these values
ref = ref
visit = lastvisit
download = ''

refs = visits = downloads = []
# add current values to each list respectively
refs.append( ref )
visits.append( visit )
downloads.append( download )

# convert lists back to longstrings
refs = ', '.join( refs )
visits = ', '.join( visits )
downloads = ', '.join( downloads )

# save this visit as an entry into database
		cur.execute('''INSERT INTO visitors (counterID, refs, host, city, 
useros, browser, visits, downloads) VALUES (%s, %s, %s, %s, %s, %s, %s, %s)
		ON DUPLICATE KEY UPDATE refs = %s, visits = %s, hits = hits + 1, 
downloads = %s''',
		(cID, refs, host, city, useros, browser, visits, downloads, refs, 
visits, downloads) )

=

IAM STRUGGLING WITH IT 2 DAYS NOW AND I CANNOT GET IT TO WORK.

ALL I WANT IT TO DO IS JUST

1. RETRIEVE 3 COLUMNS THAT CONSIST OF 3 LONG STRINGS
2. CONVERT LONG STRINGS TO LISTS
3. ADD SOME CURRENT VALUES TO THOSE LISTS
4. CONVERT FROM LISTS TO LONG STRINGS SO I CAN STORE SUCCESSFULLY LIST 
PYTHON DATATYPE TO MYSQL SCALAR STRING.


EVERYHTIGN I TRIED FAILED.
--
https://mail.python.org/mailman/listinfo/python-list


Re: pls help me with this prog

2012-10-21 Thread 88888 Dihedral
On Friday, October 19, 2012 4:40:42 PM UTC+8, inshu chauhan wrote:
> in this prog I have written a code to calculate teh centre of a given 3D 
> data..
> 
> 
> 
> but i want to calculate it for every 3 points not the whole data, but
> 
> instead of giving me centre for every 3 data the prog is printing the
> 
> centre 3 times...
> 
> 
> 
> import cv
> 
> from math import floor, sqrt, ceil
> 
> from numpy import array, dot, subtract, add, linalg as lin
> 
> 
> 
> 
> 
> 
> 
> 
> 
> def CalcCentre(data):
> 
> centre = array([0,0,0])
> 
> count = 0
> 
> n = 0
> 
> for p in data[n:n+3]:
> 
> centre = add(centre, array(p[:3]))
> 
> count += 1
> 
> centre = dot(1./count, centre)
> 
> return centre
> 
> n += 1
> 
> def ReadPointCloud(filename):
> 
> f = open(filename)
> 
> result = []
> 
> for l in f:
> 
> sp = l.split()
> 
> t = tuple(map(float, sp[1:4]))
> 
> result.append(t)
> 
> return result
> 
> 
> 
> def main (data):
> 
> 
> 
> 
> 
> j = 0
> 
> for  i in data[:3]:
> 
> while j != 3:
> 
>  centre = CalcCentre(data)
> 
>  j += 1
> 
>  print centre
> 
> 
> 
> 
> 
> if __name__ == '__main__':
> 
> data = ReadPointCloud(r'Z:\data\NEHreflectance_Scanner 1_part.txt')
> 
> 
> 
> main(data)
> 
> 
> 
> 
> 
> 
> 
> 
> 
> PLS HELP 

# assume data is a list of 3 n numbers, n!=0

n3=data.length()
n=n/3
x=sum(data[0:n3:3])/n
y=sum(data[1:n3:3])/n
z=sum(data[2:n3:3])/n #(x,y,z)



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


Re: pls help me with this prog

2012-10-20 Thread Tim Roberts
inshu chauhan  wrote:
>
>but i want to calculate it for every 3 points not the whole data, but
>instead of giving me centre for every 3 data the prog is printing the
>centre 3 times...
>
>def main (data):
>j = 0
>for  i in data[:3]:
>while j != 3:
> centre = CalcCentre(data)
> j += 1
> print centre

This loop doesn't do what you think it does.  Think about the problem you
have to solve.  You have a set of data.  You want to operate on the whole
set, 3 points at a time.  What this loop does is grab the first three
points (only), then calculates the center on the entire list three times.

You want something like:

# As long as there is data in the list:
while data:
# Split the list into two parts: first 3 and the rest.
first3, data = data[3:],data[3:]
# Operate on the first three.
print CalcCenter( first3 )

To make it a little more resilient, you might make sure that len(data) is a
multiple of 3, or at least stop when it is shorter than 3.

To be more clever, you can write a function that returns 3 at a time:

def N_at_a_time( iter, n ):
while len(iter) >= n:
yield iter[:n]
iter = iter[n:]

Now you can do this:

def main(data):
for first3 in N_at_a_time(data, 3):
print CalcCenter(first3)
-- 
Tim Roberts, t...@probo.com
Providenza & Boekelheide, Inc.
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: pls help me with this prog

2012-10-19 Thread rusi
Dont know what your code does/tries to do. Anyway some points:

On Oct 19, 1:40 pm, inshu chauhan  wrote:
> in this prog I have written a code to calculate teh centre of a given 3D 
> data..
>
> but i want to calculate it for every 3 points not the whole data, but
> instead of giving me centre for every 3 data the prog is printing the
> centre 3 times...
>
> import cv
> from math import floor, sqrt, ceil
> from numpy import array, dot, subtract, add, linalg as lin
>
> def CalcCentre(data):
>     centre = array([0,0,0])
>     count = 0
>     n = 0
>     for p in data[n:n+3]:
>         centre = add(centre, array(p[:3]))
>         count += 1
>         centre = dot(1./count, centre)
>         return centre
>     n += 1



The return makes the for iterate only once
And n += 1 will never be reached

>t = tuple(map(float, sp[1:4]))
drops the sp[0] element.
-- 
http://mail.python.org/mailman/listinfo/python-list


pls help me with this prog

2012-10-19 Thread inshu chauhan
in this prog I have written a code to calculate teh centre of a given 3D data..

but i want to calculate it for every 3 points not the whole data, but
instead of giving me centre for every 3 data the prog is printing the
centre 3 times...

import cv
from math import floor, sqrt, ceil
from numpy import array, dot, subtract, add, linalg as lin




def CalcCentre(data):
centre = array([0,0,0])
count = 0
n = 0
for p in data[n:n+3]:
centre = add(centre, array(p[:3]))
count += 1
centre = dot(1./count, centre)
return centre
n += 1
def ReadPointCloud(filename):
f = open(filename)
result = []
for l in f:
sp = l.split()
t = tuple(map(float, sp[1:4]))
result.append(t)
return result

def main (data):


j = 0
for  i in data[:3]:
while j != 3:
 centre = CalcCentre(data)
 j += 1
 print centre


if __name__ == '__main__':
data = ReadPointCloud(r'Z:\data\NEHreflectance_Scanner 1_part.txt')

main(data)




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


Re: PLEASE HELP ME WITH THIS ASSIGNMENT...PLEASE....

2008-09-12 Thread Bruno Desthuilliers

Daniel Fetchinson a écrit :




Thanking you,

Ms. Vaidehi Pawar

How much do you pay?


How much do you ask for

cheers
Ms. Vaidehi


I was just kidding. But if you seriously need people to do this kind
of job it's better to post the amount you are willing to pay otherwise
nobody will take it seriously.


If the OP seriously need people to do his schoolwork for him, then he'd 
better try to learn something else. Cheating is not a solution.

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


Re: PLEASE HELP ME WITH THIS ASSIGNMENT...PLEASE....

2008-09-12 Thread Almar Klein
>
> I find it impossible to take anyone asking others to do their school
> work for them seriously no matter how or what they are willing to pay.
>

Exactly, and writing the subject of the post in all capitals doesn't help
either...

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

Re: PLEASE HELP ME WITH THIS ASSIGNMENT...PLEASE....

2008-09-11 Thread Timothy Grant
On Thu, Sep 11, 2008 at 12:02 PM, Daniel Fetchinson
<[EMAIL PROTECTED]> wrote:
> 
>
>>> Thanking you,
>>>
>>> Ms. Vaidehi Pawar
>>
>> How much do you pay?
>>
>>> How much do you ask for
>>>
>>> cheers
>>> Ms. Vaidehi
>
> I was just kidding. But if you seriously need people to do this kind
> of job it's better to post the amount you are willing to pay otherwise
> nobody will take it seriously.
>
> Cheers,
> Daniel
> --
> Psss, psss, put it down! - http://www.cafepress.com/putitdown
> --
> http://mail.python.org/mailman/listinfo/python-list
>

I find it impossible to take anyone asking others to do their school
work for them seriously no matter how or what they are willing to pay.

-- 
Stand Fast,
tjg. [Timothy Grant]
--
http://mail.python.org/mailman/listinfo/python-list


Re: PLEASE HELP ME WITH THIS ASSIGNMENT...PLEASE....

2008-09-11 Thread Daniel Fetchinson


>> Thanking you,
>>
>> Ms. Vaidehi Pawar
>
> How much do you pay?
>
>> How much do you ask for
>>
>> cheers
>> Ms. Vaidehi

I was just kidding. But if you seriously need people to do this kind
of job it's better to post the amount you are willing to pay otherwise
nobody will take it seriously.

Cheers,
Daniel
-- 
Psss, psss, put it down! - http://www.cafepress.com/putitdown
--
http://mail.python.org/mailman/listinfo/python-list


Re: PLEASE HELP ME WITH THIS ASSIGNMENT...PLEASE....

2008-09-10 Thread Sean DiZazzo
On Sep 10, 3:33 pm, "Daniel Fetchinson" <[EMAIL PROTECTED]>
wrote:
> > I know I'm to late to ask you for helpbut please help me out..I
> > am really new to unix and dont know how to finish this assignment on
> > time.proff. said he will be using MOSS to detect whether I
> > downloaded the code..please help me..
>
> > email me : [EMAIL PROTECTED]
>
> > Assignment 1
> > Processes, Inter-Process Communication, and Concurrency
> > Due September 13th midnight
> > Summary
> > In
> > this assignment, you will create two versions of a simple multi-process
> > game : one without and another with inter-process synchronization. You
> > will also measure the relative performance of the two versions of your
> > multi-process game.
> > Objectives
>
> >     * Learn how to create and terminate processes.
> >     * Learn use of inter-process communication using shared memory,
> > semaphores, signals, etc.
> >     * Learn the basic workings of CPU scheduling code in Linux.
>
> > Part A: Multi-Process Game of Turns
>
> > In
> > this part, you are asked to write a simple program that takes two
> > command-line arguments P and N. The main process creates P other child
> > processes, waits for all of them to complete, and then exits. All the
> > child processes form one logical ring among each other. (In rest of the
> > description, the term "process" refers to a "child process".) For
> > example, if the processes are numbered 1 to P, then
>
> >     * The next neighbor of process 1 is process 2,
> >     * The next neighbor of process i is process i+1 for all i < P , and
> >     * The next neighbor of process P is process 1, which completes a ring
> > among the processes.
>
> > Assume
> > that a shared integer variable, called turn, identifies the number of
> > the processes whose turn it is at any instant. A second process-local
> > variable in each process, called me, identifies the identity of each
> > process (i.e. each process stores its own identity in a per-process
> > local variable me). A third per-process local variable, called next,
> > identifies the next process in the ring.
>
> > The processes
> > sequentially pass the turns among each other in the logical ring. When
> > each process gets its turn, it increments another shared variable
> > called counter. The pseudo-code within each process for passing turns
> > might look something as follows. (Note: Replace turn and counter below
> > with appropriate ways of accessing data within shared memory regions).
>
> >     while(turn != me )
> >         /* do nothing - just busy loop*/ ;
>
> >     /* got my turn - increment counter */
> >     counter = counter + 1;
>
> >     /* give the turn to next process */
> >     turn = next;
>
> > The program terminates when the each process has received N turns.
>
> > In the above description, several programming details have been omitted for
> > you to figure out. This includes
>
> >     * Creating P child processes from main process.
> >     * Constructing the logical ring among child processes.
> >           o Initializing each child process's identity in the me variable.
> >           o Initializing each child process' next neighbor in the next
> > variable.
> >     * Initializing the shared memory region and the shared data values.
> >     * Have the main process wait for child processes to complete N turns
> > before exiting.
>
> > Part B: More Efficient Game of Turns
>
> > You
> > might notice that the program you wrote in Part A is inefficient
> > because each process busy loops till the CPU scheduler kicks it out
> > (after its time-slice is over) and allows another process to execute
> > and make progress. (Does the program in Part A have a race condition?
> > Why or why not?)
>
> > What we ideally want is that each process
> > should be given control of the CPU only when it is that process' turn.
> > Modify the program you wrote in Part A (using appropriate
> > synchronization mechanisms) such that each process gets to run (i.e.,
> > gets control of the CPU) only when it is that process's turn, and at no
> > other time.
>
> > Again, you would need to work out the programming details of how and where
> > to use inter-process synchronization.
> > Part C: Profiling the Game of Turns
>
> > In
> > this part, you are asked to write user-level profiling code in order to
> > measure the performance of the two versions of programs you wrote in
> > Part A and Part B. Use a combination of gettimeofday() system call and
> > inter-process synchronization to measure (1) the average hand-over time
> > between two consecutive processes in the ring and (b) the total
> > execution time to complete N turns. Plot the measured values as graphs
> > when varying number of processes P and number of turns N. Explain the
> > results you obtain.
> > Submission Guidelines
>
> > Thanking you,
>
> > Ms. Vaidehi Pawar
>
> How much do you pay?
>
> Cheers,
> Daniel
> --
> Psss, psss, put it down! -http://www.cafepress.com/putitdown

Maybe.  I

Re: PLEASE HELP ME WITH THIS ASSIGNMENT...PLEASE....

2008-09-10 Thread Daniel Fetchinson
> I know I'm to late to ask you for helpbut please help me out..I
> am really new to unix and dont know how to finish this assignment on
> time.proff. said he will be using MOSS to detect whether I
> downloaded the code..please help me..
>
> email me : [EMAIL PROTECTED]
>
> Assignment 1
> Processes, Inter-Process Communication, and Concurrency
> Due September 13th midnight
> Summary
> In
> this assignment, you will create two versions of a simple multi-process
> game : one without and another with inter-process synchronization. You
> will also measure the relative performance of the two versions of your
> multi-process game.
> Objectives
>
> * Learn how to create and terminate processes.
> * Learn use of inter-process communication using shared memory,
> semaphores, signals, etc.
> * Learn the basic workings of CPU scheduling code in Linux.
>
> Part A: Multi-Process Game of Turns
>
> In
> this part, you are asked to write a simple program that takes two
> command-line arguments P and N. The main process creates P other child
> processes, waits for all of them to complete, and then exits. All the
> child processes form one logical ring among each other. (In rest of the
> description, the term "process" refers to a "child process".) For
> example, if the processes are numbered 1 to P, then
>
> * The next neighbor of process 1 is process 2,
> * The next neighbor of process i is process i+1 for all i < P , and
> * The next neighbor of process P is process 1, which completes a ring
> among the processes.
>
> Assume
> that a shared integer variable, called turn, identifies the number of
> the processes whose turn it is at any instant. A second process-local
> variable in each process, called me, identifies the identity of each
> process (i.e. each process stores its own identity in a per-process
> local variable me). A third per-process local variable, called next,
> identifies the next process in the ring.
>
> The processes
> sequentially pass the turns among each other in the logical ring. When
> each process gets its turn, it increments another shared variable
> called counter. The pseudo-code within each process for passing turns
> might look something as follows. (Note: Replace turn and counter below
> with appropriate ways of accessing data within shared memory regions).
>
> while(turn != me )
> /* do nothing - just busy loop*/ ;
>
> /* got my turn - increment counter */
> counter = counter + 1;
>
> /* give the turn to next process */
> turn = next;
>
> The program terminates when the each process has received N turns.
>
> In the above description, several programming details have been omitted for
> you to figure out. This includes
>
> * Creating P child processes from main process.
> * Constructing the logical ring among child processes.
>   o Initializing each child process's identity in the me variable.
>   o Initializing each child process' next neighbor in the next
> variable.
> * Initializing the shared memory region and the shared data values.
> * Have the main process wait for child processes to complete N turns
> before exiting.
>
> Part B: More Efficient Game of Turns
>
> You
> might notice that the program you wrote in Part A is inefficient
> because each process busy loops till the CPU scheduler kicks it out
> (after its time-slice is over) and allows another process to execute
> and make progress. (Does the program in Part A have a race condition?
> Why or why not?)
>
> What we ideally want is that each process
> should be given control of the CPU only when it is that process' turn.
> Modify the program you wrote in Part A (using appropriate
> synchronization mechanisms) such that each process gets to run (i.e.,
> gets control of the CPU) only when it is that process's turn, and at no
> other time.
>
> Again, you would need to work out the programming details of how and where
> to use inter-process synchronization.
> Part C: Profiling the Game of Turns
>
> In
> this part, you are asked to write user-level profiling code in order to
> measure the performance of the two versions of programs you wrote in
> Part A and Part B. Use a combination of gettimeofday() system call and
> inter-process synchronization to measure (1) the average hand-over time
> between two consecutive processes in the ring and (b) the total
> execution time to complete N turns. Plot the measured values as graphs
> when varying number of processes P and number of turns N. Explain the
> results you obtain.
> Submission Guidelines
>
> Thanking you,
>
> Ms. Vaidehi Pawar

How much do you pay?

Cheers,
Daniel
-- 
Psss, psss, put it down! - http://www.cafepress.com/putitdown
--
http://mail.python.org/mailman/listinfo/python-list


PLEASE HELP ME WITH THIS ASSIGNMENT...PLEASE....

2008-09-10 Thread Vaidehi Pawar
I know I'm to late to ask you for helpbut please help me out..I
am really new to unix and dont know how to finish this assignment on
time.proff. said he will be using MOSS to detect whether I
downloaded the code..please help me..

email me : [EMAIL PROTECTED]

Assignment 1
Processes, Inter-Process Communication, and Concurrency
Due September 13th midnight
Summary
In
this assignment, you will create two versions of a simple multi-process
game : one without and another with inter-process synchronization. You
will also measure the relative performance of the two versions of your
multi-process game.
Objectives

* Learn how to create and terminate processes.
* Learn use of inter-process communication using shared memory, semaphores, 
signals, etc.
* Learn the basic workings of CPU scheduling code in Linux.

Part A: Multi-Process Game of Turns

In
this part, you are asked to write a simple program that takes two
command-line arguments P and N. The main process creates P other child
processes, waits for all of them to complete, and then exits. All the
child processes form one logical ring among each other. (In rest of the
description, the term "process" refers to a "child process".) For
example, if the processes are numbered 1 to P, then

* The next neighbor of process 1 is process 2,
* The next neighbor of process i is process i+1 for all i < P , and
* The next neighbor of process P is process 1, which completes a ring among 
the processes.

Assume
that a shared integer variable, called turn, identifies the number of
the processes whose turn it is at any instant. A second process-local
variable in each process, called me, identifies the identity of each
process (i.e. each process stores its own identity in a per-process
local variable me). A third per-process local variable, called next,
identifies the next process in the ring.

The processes
sequentially pass the turns among each other in the logical ring. When
each process gets its turn, it increments another shared variable
called counter. The pseudo-code within each process for passing turns
might look something as follows. (Note: Replace turn and counter below
with appropriate ways of accessing data within shared memory regions).

while(turn != me ) 
/* do nothing - just busy loop*/ ; 

/* got my turn - increment counter */
counter = counter + 1;

/* give the turn to next process */
turn = next;  

The program terminates when the each process has received N turns.

In the above description, several programming details have been omitted for you 
to figure out. This includes

* Creating P child processes from main process.
* Constructing the logical ring among child processes.
  o Initializing each child process's identity in the me variable.
  o Initializing each child process' next neighbor in the next variable.
* Initializing the shared memory region and the shared data values.
* Have the main process wait for child processes to complete N turns before 
exiting.

Part B: More Efficient Game of Turns

You
might notice that the program you wrote in Part A is inefficient
because each process busy loops till the CPU scheduler kicks it out
(after its time-slice is over) and allows another process to execute
and make progress. (Does the program in Part A have a race condition?
Why or why not?)

What we ideally want is that each process
should be given control of the CPU only when it is that process' turn.
Modify the program you wrote in Part A (using appropriate
synchronization mechanisms) such that each process gets to run (i.e.,
gets control of the CPU) only when it is that process's turn, and at no
other time.

Again, you would need to work out the programming details of how and where to 
use inter-process synchronization.
Part C: Profiling the Game of Turns

In
this part, you are asked to write user-level profiling code in order to
measure the performance of the two versions of programs you wrote in
Part A and Part B. Use a combination of gettimeofday() system call and
inter-process synchronization to measure (1) the average hand-over time
between two consecutive processes in the ring and (b) the total
execution time to complete N turns. Plot the measured values as graphs
when varying number of processes P and number of turns N. Explain the
results you obtain.
Submission Guidelines

Thanking you,

Ms. Vaidehi Pawar




  Check out the all-new face of Yahoo! India. Go to http://in.yahoo.com/--
http://mail.python.org/mailman/listinfo/python-list

Re: Help me with this!!!

2007-02-01 Thread Steven D'Aprano
On Thu, 01 Feb 2007 08:53:55 -0800, Ravi Teja wrote:

>> > > It search a text inside that hex value.
>> > > It works perfecly on a txt file but if I open a binary file (.exe,.bin
>> > > ecc...) with the same value it wont work, why?
>> > > Please help!
>>
>> > Because the pattern isn't in the file, perhaps.
>>
>> This pattern IS in the file (I made it and I double check with an hex
>> editor).
>> It display the file correcltly (print line) but...
> 
> No! Peter is right. Regular expressions match ASCII representation of
> data, not hex. In simple terms, do you see your pattern when you open
> the file in notepad (or other text editor)? You do not use regex to
> search binary files.

I don't see why not.

>>> pattern = "NULL\0"
>>> source = "\0\01\02-more-bytes-here-NULL\0-more-bytes"
>>> m = re.search(pattern, source)
>>> m.group()
'NULL\x00'
>>> m.span()
(20, 25)

Here's the Original Poster's code again:


regex = re.compile(r"(?si)(\x8B\xF0\x85\xF6)(?P.*)
(\xC6\x44\x24)",re.IGNORECASE)
file = open(fileName, "rb")
for line in file:
  if (match):
  print line
file.close()

I suggest that the reason it doesn't work is because he never actually
uses the regex. Presumably the name "match" was assigned somewhere else to
a false value, and so the code simply walks through the file doing nothing.


-- 
Steven D'Aprano 

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


Re: Help me with this!!!

2007-02-01 Thread Ravi Teja
> > > It search a text inside that hex value.
> > > It works perfecly on a txt file but if I open a binary file (.exe,.bin
> > > ecc...) with the same value it wont work, why?
> > > Please help!
>
> > Because the pattern isn't in the file, perhaps.
>
> This pattern IS in the file (I made it and I double check with an hex
> editor).
> It display the file correcltly (print line) but...

No! Peter is right. Regular expressions match ASCII representation of
data, not hex. In simple terms, do you see your pattern when you open
the file in notepad (or other text editor)? You do not use regex to
search binary files.

Ravi Teja.

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


Re: Help me with this!!!

2007-02-01 Thread Paul McGuire
On Feb 1, 4:21 am, "TOXiC" <[EMAIL PROTECTED]> wrote:
> Hi all, I've this code:
>
> regex = re.compile(r"(?si)(\x8B\xF0\x85\xF6)(?P.*)
> (\xC6\x44\x24)",re.IGNORECASE)
> file = open(fileName, "rb")
> for line in file:
>   if (match):
>   print line
> file.close()
>
> It search a text inside that hex value.
> It works perfecly on a txt file but if I open a binary file (.exe,.bin
> ecc...) with the same value it wont work, why?
> Please help!

Where do you assign the value of 'match'?  Perhaps you are missing a
line such as "match=regex.match(line)" or "match=regex.search(line)"?
Perhaps this is why Peter Otten says that this code can't possibly be
the code that works for text files?

What is the significance of "for line in file" when file is opened in
binary mode?  Are EOLs properly interpreted in binary mode?  ('file'
is not the best variable name, by the way, as it masks Python's built-
in file.  How about "file_", or "inputfile", or "fileToBeSearched"?)

-- Paul

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


Re: Help me with this!!!

2007-02-01 Thread TOXiC

Peter Otten ha scritto:

> TOXiC wrote:
>
> > Hi all, I've this code:
>
> No you don't.
>

!

> > regex = re.compile(r"(?si)(\x8B\xF0\x85\xF6)(?P.*)
> > (\xC6\x44\x24)",re.IGNORECASE)
> > file = open(fileName, "rb")
> > for line in file:
> >   if (match):
> >   print line
> > file.close()
> >
> > It search a text inside that hex value.
> > It works perfecly on a txt file but if I open a binary file (.exe,.bin
> > ecc...) with the same value it wont work, why?
> > Please help!
>
> Because the pattern isn't in the file, perhaps.
>

This pattern IS in the file (I made it and I double check with an hex
editor).
It display the file correcltly (print line) but...

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


Re: Help me with this!!!

2007-02-01 Thread Peter Otten
TOXiC wrote:

> Hi all, I've this code:

No you don't. 

> regex = re.compile(r"(?si)(\x8B\xF0\x85\xF6)(?P.*)
> (\xC6\x44\x24)",re.IGNORECASE)
> file = open(fileName, "rb")
> for line in file:
>   if (match):
>   print line
> file.close()
> 
> It search a text inside that hex value.
> It works perfecly on a txt file but if I open a binary file (.exe,.bin
> ecc...) with the same value it wont work, why?
> Please help!

Because the pattern isn't in the file, perhaps. 

Peter

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


Help me with this!!!

2007-02-01 Thread TOXiC
Hi all, I've this code:

regex = re.compile(r"(?si)(\x8B\xF0\x85\xF6)(?P.*)
(\xC6\x44\x24)",re.IGNORECASE)
file = open(fileName, "rb")
for line in file:
  if (match):
  print line
file.close()

It search a text inside that hex value.
It works perfecly on a txt file but if I open a binary file (.exe,.bin
ecc...) with the same value it wont work, why?
Please help!

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