Re: [Tutor] Tutor Digest, Vol 160, Issue 34

2017-06-27 Thread anish singh
> >> anish singh wrote:
> >>
> >>> I need a custom comparator.
> >>>
> >>> dictionary = {a:[b,c], d:[e,f]}
> >>>
> >>> If both 'b' and 'e' belong to the same bin
> >>> then it should be compared based on 'c' and 'f'.
> >>>
> >>> However, I want to also represent the result of the
> >>> sorted operation in a ordered dictionary as order is
> >>> important.
> >>>
> >>> My custom comparator is something like this:
> >>>
> >>>
> >>> ''' x and y is a list of two elements each'''
> >>> def cmpr(x, y):
> >>>r = 3
> >>>if x[0]//r != y[0]//r:
> >>>return x[0]//r < y[0]//r
> >>>return x[1] < y[1]
> >>
> >> This looks like it should be called less() rather than compare() as it
> >> doesn't differentiate between the x < y and x == y case.
> >>
> >>> Please note it is not exactly comparing the first elements
> >>> of the value but checking if they belong to the same bin
> >>> and they do then it checks the second element as as shown
> >>> above.
> >>
> >> The effect should be the same.
> >
> > Well no, take the case of [1,100] and [2,0]
> > Both belong to same bin suppose then it should
> > be sorted based on second index and I would
> > expect [2,0] [1,100] as output.
> > This is not happening currently with the original
> > code I have sent.
>
> I think that is because you do not consider all three cases.
> Let's start with a function cmp() modeled after the Python 2 built-in
>
> def cmp(a, b):
> if a < b:
> return -1
> elif a > b:
> return 1
> return 0
>
> Then your comparator could be fixed (I think) as follows
>
> def compare(x, y):
> def bin(a): return a[0] // 3
>
> result = cmp(bin(x), bin(y))
> if result:
> return result
> return cmp(x[1], y[1])
>
> and that "fixed" version would be equivalent (I think) to
>
> def compare(x, y)
> def key(a): return (a[0] // 3, a[1])
>
> return cmp((key(x), key(y))
>
> That said, even if you use Python 2 you should use sorted() with a key
> function rather than a comparison -- as shown below. Did that work for you?
>

Yes it did. Thanks.

>
> >>> Example:
> >>> {0:[0, 8], 1:[2, 5], 2:[2, 11], 3:[16, 17], 4:[13, 14], 5:[1, 17],
> >>> {6:[17,
> >>> 17] }
> >>> output should be:
> >>> {1:[2, 5], 0:[0, 8], 2:[2, 11], 5:[1, 17], 4:[13, 14], 3:[16, 17],
> >>> {6:[17,
> >>> 17] }
> >>
> > input = {0:[0, 8], 1:[2, 5], 2:[2, 11], 3:[16, 17], 4:[13, 14], 5:[1,
> >> 17], 6:[17,
> >> ... 17] }
> > wanted = {1:[2, 5], 0:[0, 8], 2:[2, 11], 5:[1, 17], 4:[13, 14],
> 3:[16,
> >> 17], 6:[17,
> >> ... 17] }
> > output = {k: v for k, v in sorted(input.items(), key=lambda x: (x[1]
> >> [0]//3, x[1][1]))}
> > assert list(output.items()) == list(wanted.items())
> >>
> >> As written it will work with CPython 3.6. However, for compatibility
> with
> >> other versions of Python I recommend that you replace the plain dicts
> >> above with collections.OrderedDict instances. Quoting
> >>
> >> https://docs.python.org/dev/whatsnew/3.6.html#whatsnew36-pep520
> >>
> >> """
> >> The order-preserving aspect of this new [dict] implementation is
> >> considered an implementation detail and should not be relied upon [...]
> >> """
>
>
>
>
> --
>
> Message: 3
> Date: Mon, 26 Jun 2017 11:22:21 -0600
> From: Mats Wichmann 
> To: tutor@python.org
> Subject: Re: [Tutor] custom comparator with ordered list
> Message-ID: <9ecb3bcb-ceaa-8e2c-d6a2-edf78689a...@wichmann.us>
> Content-Type: text/plain; charset=utf-8
>
> On 06/26/2017 10:38 AM, Anish Kumar wrote:
> >
> >> anish singh wrote:
> >>
> >>> I need a custom comparator.
> >>>
> >>> dictionary = {a:[b,c], d:[e,f]}
> >>>
> >>> If both 'b' and 'e' belong to the same bin
>
> if would help alot if your problem statement included a description of
> what "same bin" is.
>
> > Well no, take the case of [1,100] and [2,0]
> > Both belong to same bin
>
> how would we conclude that these "belong to same bin"?
>
>
>
>
> --
>
> Message: 4
> Date: Mon, 26 Jun 2017 11:18:36 -0600
> From: Mats Wichmann 
> To: tutor@python.org
> Subject: Re: [Tutor] how-to generate specific lines of text from two
> python lists
> Message-ID: <56d6e476-64aa-1a3a-b156-83b06a4f1...@wichmann.us>
> Content-Type: text/plain; charset=utf-8
>
> On 06/25/2017 12:44 PM, Tahir Hafiz wrote:
> > Thanks Alan and Peter,
> >
> > Alan you are right this could be solved via an SQL statement but I was
> > asked to finish the python script.
> > Anyways, this worked and helped to solve the problem in the end:
> >
> > # Create iterator object, dictionary which can be used to iterate
> against.
> > b_iter = iter(new_emails)
> >
> >
> > print "Creating a list of usernames and email addresses from retreived
> > database data:"
> > if __name__ == "__main__":
> > dictionary = dict(zip(usernames, new_emails))
> > my_list = []
> > for username in usernames:
> >
> >   my_list.append({'username':username, 

Re: [Tutor] Fwd: Re: Using files to read data

2017-06-27 Thread Mats Wichmann
On 06/27/2017 05:01 AM, Alan Gauld via Tutor wrote:
> Forwarding to list
> 
> Please always use ReplyAll or ReplyList when responding to list mail.
> 
> 
> 
>  Forwarded Message 
> 
> i apologize.  i guess it didn’t attach correctly.
> my issue is how do i get it out of my file and use it. 
> 
> *the json file, its only about a fifth of it but it should serve its
> purpose*
> [
> 0.9889,
> 0.02,
> "Mon Jun 26 20:37:34 2017"
> ]


A few thoughts here...

suggest storing the time not as a datetime string, but as a time value.
That is, use time.time() to generate it; you can always convert that
value to a string later if needed.

if you're going to write as json, then you should read as json, don't
create your own reader.

in order to get data serialized in a more usable form, you could define
a class, and serialize the class instances, this way you get the
variable name stored along with the data. Since json only understands a
limited number of data types, one cheap approach is to fish out the
dictionary of data from the instance object - json does understand
dicts. I'm using vars() for that.  So here's a cheap example, not
fleshed out to use any of your code:

import time
import json

class Point(object):
def __init__(self, x, y):
self.x = x
self.y = y
self.time = time.time()

# create a few points
A = Point(0.03, 0.9777)
B = Point(0.02, 0.9889)

print(json.dumps(vars(A)))
print(json.dumps(vars(B)))

You'll see you get formatted data that json.loads() ought to be able to
make use of:

{"y": 0.9777, "x": 0.03, "time":
1498577730.524801}
{"y": 0.9889, "x": 0.02, "time":
1498577730.524802}

You don't need a class for that, you can just build the dict yourself,
it was just a cheap way to illustrate.

Simulated read:

 >>> import json
 >>> point = json.loads('{"y": 0.9777, "x":
0.03, "time": 1498577980.382325}')
 >>> print(point['x'])
 0.0
 >>> print(point['y'])
 0.9778
 >>> print(point(['time'])
 1498577980.38
 >>>


I wouldn't open/close the json file you're writing to each time, that
seems a bit wasteful.

And do take on board some of the refactoring suggestions already made.

Dividing the heading by 90 using integer division (//) will give you
four possible values, 0-3, you can then base your definitions/decisions
on that, instead of your "if heading in range(1, 91):"  selections.
range actually generates the values, which you don't really need.

Best of luck!



___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Fwd: Re: Using files to read data

2017-06-27 Thread Peter Otten
Alan Gauld via Tutor wrote:

> Forwarding to list
> 
> Please always use ReplyAll or ReplyList when responding to list mail.
> 
> 
> 
>  Forwarded Message 
> 
> i apologize.  i guess it didn’t attach correctly.
> my issue is how do i get it out of my file and use it.
> 
> *the json file, its only about a fifth of it but it should serve its
> purpose*

> [
> 0.9889,
> 0.02,
> "Mon Jun 26 20:37:34 2017"
> ]
> [
> 0.9777,
> 0.03,
> "Mon Jun 26 20:37:34 2017"
> ]

The problem with this data is that it's not proper json which allows only 
one toplevel data structure, e. g.

[
> [
> 0.9889,
> 0.02,
> "Mon Jun 26 20:37:34 2017"
> ]
,
> [
> 0.9777,
> 0.03,
> "Mon Jun 26 20:37:34 2017"
> ]
]

If you stick to that file format you need to split it into parts that are 
valid json before you can process them with the standard library's json 
module.

If you can rely on your records always consisting of five lines the 
following should work


import json

records = []

def read_five_lines(f):
return "".join(f.readline() for i in range(5))

with open("xyz_save.json") as f:
while True:
chunk = read_five_lines(f)
if not chunk:  # empty string --> reached end of file
break
records.append(json.loads(chunk))

print(records)

Another option would be to change your writing script so that it writes one 
record per line, e. g.

[0.24446, 0.8556, "Tue Jun 27 14:21:44 2017"]
[-0.29333, 1.907, "Tue Jun 27 14:21:44 2017"]

This can be achieved by omitting the indent=0 argument in your json.dump() 
calls. Then your reader can be simplified:

import json

records = []

with open("xyz_save.json") as f:
for line in f:
records.append(json.loads(line))

print(records)



> if heading in range(1, 91):

Did you know that

>>> 1.5 in range(1, 91)
False

? If you want to accept non-integral values you better write the above as

if 0 <= heading < 90:  # swap operators if want to exclude the lower 
   # and include the upper bound
...

or similar.

> north = heading
> east = 90 - heading
> y = (north / 90) * forward_time
> x = (east / 90) * forward_time
> now_time = time.ctime()
> 
> xyz = [x, y, now_time]
> 
> xyz_save = "xyz_save.json"
> 
> 
> with open(xyz_save, "a") as stamp:
> json.dump(xyz, stamp, indent=0)
> stamp.write("\n")
> 
> return xyz
> 
> elif heading in range(91, 181):
> east = heading - 90
> south = 180 - heading
> y = (south / 90) * forward_time
> x = (east / -90) * forward_time
> now_time = time.ctime()
> 
> xyz = [x, y, now_time]
> 
> xyz_save = "xyz_save.json"
> 
> 
> with open(xyz_save, "a") as stamp:
> json.dump(xyz, stamp, indent=0)
> stamp.write("\n")
> 
> return xyz

There's *a* *lot* of repetition here. At the very least you should move the 
parts that are completely identical to the end of the if ... elif ... chain:

if heading in range(1, 91):
north = heading
east = 90 - heading
y = (north / 90) * forward_time
x = (east / 90) * forward_time
elif heading in range(91, 181):
east = heading - 90
south = 180 - heading
y = (south / 90) * forward_time
x = (east / -90) * forward_time
 ...

else: 
# handling the case where no condition matches
print("unhandled heading", heading, file=sys.stderr)
return

now_time = time.ctime()
xyz = [x, y, now_time]
xyz_save = "xyz_save.json"

with open(xyz_save, "a") as stamp:
json.dump(xyz, stamp)
stamp.write("\n")

return xyz

PS:

> north = heading
> east = 90 - heading
> y = (north / 90) * forward_time
> x = (east / 90) * forward_time

I wonder whether there should be a sin() and cos() somewhere...

___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


[Tutor] Fwd: Re: Using files to read data

2017-06-27 Thread Alan Gauld via Tutor
Forwarding to list

Please always use ReplyAll or ReplyList when responding to list mail.



 Forwarded Message 

i apologize.  i guess it didn’t attach correctly.
my issue is how do i get it out of my file and use it. 

*the json file, its only about a fifth of it but it should serve its
purpose*
[
0.9889,
0.02,
"Mon Jun 26 20:37:34 2017"
]
[
0.9777,
0.03,
"Mon Jun 26 20:37:34 2017"
]
[
0.9667,
0.0,
"Mon Jun 26 20:37:34 2017"
]
[
0.9556,
0.06,
"Mon Jun 26 20:37:34 2017"
]
[
0.9444,
0.0,
"Mon Jun 26 20:37:34 2017"
]
[
0.9333,
0.06667,
"Mon Jun 26 20:37:34 2017"
]
[
0.9223,
0.07778,
"Mon Jun 26 20:37:34 2017"
]
[
0.9111,
0.08889,
"Mon Jun 26 20:37:34 2017"
]
[
0.9,
0.1,
"Mon Jun 26 20:37:34 2017"
]
[
0.,
0.,
"Mon Jun 26 20:37:34 2017"
]
[
0.8778,
0.1,
"Mon Jun 26 20:37:34 2017"
]
[
0.8667,
0.1,
"Mon Jun 26 20:37:34 2017"
]
[
0.8555,
0.14443,
"Mon Jun 26 20:37:34 2017"
]
[
0.8444,
0.15556,
"Mon Jun 26 20:37:34 2017”

*the code that creates it*

import json
import time

def xyz(heading, forward_time):
"""get x and y increments of 360 degrees"""

if heading in range(1, 91):
north = heading
east = 90 - heading
y = (north / 90) * forward_time
x = (east / 90) * forward_time
now_time = time.ctime()

xyz = [x, y, now_time]

xyz_save = "xyz_save.json"


with open(xyz_save, "a") as stamp:
json.dump(xyz, stamp, indent=0)
stamp.write("\n")

return xyz

elif heading in range(91, 181):
east = heading - 90
south = 180 - heading
y = (south / 90) * forward_time
x = (east / -90) * forward_time
now_time = time.ctime()

xyz = [x, y, now_time]

xyz_save = "xyz_save.json"


with open(xyz_save, "a") as stamp:
json.dump(xyz, stamp, indent=0)
stamp.write("\n")

return xyz

elif heading in range(181, 271):
south = heading - 180
west = 270 - heading
y = (south / -90) * forward_time
x = (west / -90) * forward_time
now_time = time.ctime()

xyz = [x, y, now_time]

xyz_save = "xyz_save.json"


with open(xyz_save, "a") as stamp:
json.dump(xyz, stamp, indent=0)
stamp.write("\n")

return xyz

elif heading in range(271, 361):
west = heading - 270
north = 360 - heading
y = (north / -90) * forward_time
x = (west / 90) * forward_time
now_time = time.ctime()

xyz = [x, y, now_time]

xyz_save = "xyz_save.json"


with open(xyz_save, "a") as stamp:
json.dump(xyz, stamp, indent=0)
stamp.write("\n")

return xyz
 

*One of multiple loads I’ve got. *
*
*
', '0', ':', '3', '7', ':', '3', '4', ' ', '2', '0', '1', '7', '"',
'\n', ']', '\n', '[', '\n', '0', '.', '0', '2', '2', '2', '2', '2', '2',
'2', '2', '2', '2', '2', '2', '2', '2', '2', '2', '3', ',', '\n', '0',
'.', '9', '7', '7', '7', '7', '7', '7', '7', '7', '7', '7', '7', '7',
'7', '7', '7', ',', '\n', '"', 'M', 'o', 'n', ' ', 'J', 'u', 'n', ' ',
'2', '6', ' ', '2', '0', ':', '3', '7', ':', '3', '4', ' ', '2', '0',
'1', '7', '"', '\n', ']', '\n', '[', '\n', '0', '.', '0', '1', '1', '1',
'1', '1', '1', '1', '1', '1', '1', '1', '1', '1', '1', '1', '1', '2',
',', '\n', '0', '.', '9', '8', '8', '8', '8', '8', '8', '8', '8', '8',
'8', '8', '8', '8', '8', '9', ',', '\n', '"', 'M', 'o', 'n', ' ', 'J',
'u', 'n', ' ', '2', '6', ' ', '2', '0', ':', '3', '7', ':', '3', '4', '
', '2', '0', '1', '7', '"', '\n', ']', '\n', '\n’]
*
*
*
*
*
*
*the closest i have got is this*
*
*
[
0.07778,
0.9223,
"Mon Jun 26 20:37:34 2017"
]
[
0.06667,
0.9333,
"Mon Jun 26 20:37:34 2017"
]
[
0.0,
0.9444,
"Mon Jun 26 20:37:34 2017"
]
[
0.06,
0.9556,
"Mon Jun 26 20:37:34 2017"
]
[
0.0,
0.9667,
"Mon Jun 26 20:37:34 2017"
]
[
0.03,
0.9777,
"Mon Jun 26 20:37:34 2017"
]
[
0.02,
0.9889,
"Mon Jun 26 20:37:34 2017”

*with this code*

filename = "xyz_save.json"
with open(filename) as f_obj:
lines = f_obj.read()

print(lines)
*
*
*
*
*
*
*i guess my ultimate question is how do i take all the x and y values
and use them.*
*i apologize for my ignorance as this is my first post about something
very new to me.*
*i also appreciate your patience*
*
*
*
*
*
*








> On Jun 27, 2017, at 1:56 AM, Alan Gauld 

Re: [Tutor] Using files to read data

2017-06-27 Thread Alan Gauld via Tutor
On 27/06/17 06:32, Micheal Dale Peterson wrote:
> I am trying to write something that stores x and y data
> with a time reference and then use it later ...

Lets stick to one thing at a time.
How far did you get writing the data to a file?
Did the file get created? Can you edit in the
text editor?

The simplest way to do it is probably

x = 42
y = 66
time = "10:40:22.5"

with open("Myfile.txt") as output:
output.write(str(x)+'\n')
output.write(str(y)+'\n')
output.write(time + '\n')


That only outputs one line to the file Myfile.txt,
but you could put the write lines into a loop if
you had a list of data items.

> the closest i have come is using the json save.  

Using json is a reasonable approach for this type of problem.
Can you show us the actual code you used to write the data?
I assume it did actually get saved to a file in this case?

> Trying to read it back does me no good.

That's too vague to be any use. What does "no good" mean?
Could you open the file from Python?
Did you get any data at all?
Show us some code, even if it doesn't work as you expected.

>  i can print each thing individually. 

What do you mean? Do you mean you can read each
item from the file and print it? If so what is
it that you can't do? Can you save it in a variable?
Can you add the variable to a list for future use?

> everyday i try i either get a bunch of added characters 

Again this this too vague to be helpful.
Show us what kind of characters.
Show us the code you are using to read the data and print it.

> or it separates every character. 

Again show us code, and sample data so we can see.
The problem could lie in how you read the file or
in how you display the data. But without code we can't tell.

> Sorry i can’t  post all of the ways i have tried. 

Just post the latest attempts we can start there.

> I am running OS X version 10.12.5 with python 3.6.1.

Thanks for that, it helps.

> my code to get saved list

There's no code...

Did you send it as a binary attachment maybe?
The list server strips off all binary attachments
Cut 'n paste the code from your text editor into
the mail body.

-- 
Alan G
Author of the Learn to Program web site
http://www.alan-g.me.uk/
http://www.amazon.com/author/alan_gauld
Follow my photo-blog on Flickr at:
http://www.flickr.com/photos/alangauldphotos


___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Using files to read data

2017-06-27 Thread Peter Otten
Micheal Dale Peterson wrote:

> my saved lists

Hello Micheal! 

Have a look at



to learn what we see of your mail. 

Unfortunately attachments are stripped off. Please resend as plain text, 
with your code and data inlined in the body of the mail. (Make sure that the 
data is small, just enough that we can get an idea of its structure.)


___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


[Tutor] Using files to read data

2017-06-27 Thread Micheal Dale Peterson
hello I’m new to python. I have been trying to teach myself without asking 
questions because i feel i learn more. But i am stuck on this for a bout a week 
now.  I am trying to write something that stores x and y data with a time 
reference and then use it later to create some type of a graph.  I have tried 
saving to text files. and the closest i have come is using the json save.  this 
is the closest output i can get(posted below).  Trying to read it back does me 
no good. i can print each thing individually. everyday i try i either get a 
bunch of added characters or it separates every character. Sorry i can’t  post 
all of the ways i have tried. I have been at this for a week and have changed 
things a thousand times and made a mess of most my files at this point. haha. 
Any help would be appreciated.  I am running OS X version 10.12.5 with python 
3.6.1. thank you in advance


my saved lists



my code to get saved list



___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor