Re: [Tutor] Mobile apps in Python

2015-02-14 Thread wolfrage8...@gmail.com
I definitely recommend Kivy. I have enjoyed great success with it, and
it was very easy to port my code to work on Android.
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] sql-like join on two lists or dictionaries

2015-02-14 Thread Alan Gauld

On 14/02/15 09:55, Peter Otten wrote:


with open(headerfile) as f:
 lookup_header = {
 headerdata[:6]: headerdata.rstrip(\n) for headerdata in f}

Then you can iterate over the lines in linefile, extract the key from that
and look it up in the dict:

with open(linefile) as lines, open(hl.dat, w) as joined:
 for line in lines:
 try:
 header = lookup_header[line[:6]]
 except KeyError:
 header = 
 print(line.rstrip(\n), header, sep=, file=joined)



The try/except could be written more concisely as

header = lookup_header.get(line[:6], )


HTH
--
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] sql-like join on two lists or dictionaries

2015-02-14 Thread Peter Otten
street.swee...@mailworks.org wrote:

 Hello all,
 
 Basically what I have here is header and line data for sales or purchase
 orders, and I'm trying to do a sql-like join to bring them together
 (which ultimately I did because I couldn't figure this out :)).  I've
 managed to get the files into python using string slicing, that's not a
 problem.
 
 headers - h.dat
 
 B134542Bob  ZQ775235
 B875432Joe  ZQ987656
 B567943SteveZQ256222
 
 lines - l.dat
 
 B134542   112342   0012
 B134542   176542   0001
 B875732   765420003
 B567943   654565   0001
 B567943   900011   0001
 
 desired result - hl.dat
 
 B134542   112342   0012BobZQ775235
 B134542   176542   0001BobZQ775235
 B875732   765420003JoeZQ987656
 B567943   654565   0001Steve  ZQ256222
 B567943   900011   0001Steve  ZQ256222
 
 
 
 in python3 on linux:
 
 #!/usr/bin/env python3
 
 import os
 
 basepath=os.path.join(os.path.expanduser('~'),'temp',)
 linefile=os.path.join(basepath,'l.dat')
 headerfile=os.path.join(basepath,'h.dat')
 
 with open(headerfile) as h, open(linefile) as l:
   lines = l.readlines()
   headers = h.readlines()
 
 llist = [[linedata[0:7],
   linedata[14:23],
   linedata[23:27]] for linedata in lines]
 
 hlist = [[headerdata[0:7],
   headerdata[11:19],
   headerdata[19:28]] for headerdata in headers]
 
 ldict = [{linedata[0:7]:
   [linedata[14:23],
linedata[23:27]]} for linedata in lines]
 
 hdict = [{headerdata[0:7]:
   [headerdata[11:19],
headerdata[19:28]]} for headerdata in headers]
 
 # :)
 
 quit()
 
 
 
 Details on the data are that it's a one or many lines to one header
 relationship, at least one of each will exist in each file, and
 performance probably isn't an issue as it will only be a few tens to
 about 100 lines maximum in the lines file.  The match string will be the
 0:7 slice.
 
 You can probably guess my questions: should I be making lists or
 dictionaries out of this data, and then of course, what should I do with
 them to arrive at the combined file?  I saw some examples of joining two
 two-item lists, or dictionaries with a single string as the value, but I
 couldn't seem to adapt them to what I'm doing here. I also ran across
 the dict.extend method, but looking at the result, I didn't think that
 was going to go anywhere, particularly with the one to many
 headers:lines relationship.
 
 After a while I pulled this into a sqlite file in memory and did the
 join.  Using writelines I think I'll be able to get it out to a file,
 but it seems to me that there's probably a way to do this without
 resorting to sql.  Or is there?

You only need one dictionary that maps the first column in headerfile to the 
complete line:

with open(headerfile) as f:
lookup_header = {
headerdata[:6]: headerdata.rstrip(\n) for headerdata in f}

Then you can iterate over the lines in linefile, extract the key from that 
and look it up in the dict:

with open(linefile) as lines, open(hl.dat, w) as joined:
for line in lines:
try:
header = lookup_header[line[:6]]
except KeyError:
header = 
print(line.rstrip(\n), header, sep=, file=joined)

This approach works with linefiles of arbitrary size as you only ever have 
one line of that file in memory. 

As written the output file includes lines with no matching header; replace

header = 

with a

continue

statement to get an inner join.

A general remark: whenever possible you should avoid the readlines() method 
which creates a list of lines and instead iterate over the file directly.

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


Re: [Tutor] trivial simple program..can it be made more concise?

2015-02-14 Thread Dave Angel

On 02/14/2015 04:07 AM, Steven D'Aprano wrote:

On Sat, Feb 14, 2015 at 03:17:28AM +, steve10br...@comcast.net wrote:

Hi all,

I was playing with Python tonight and created a simple program that
outputs numbers counting up then counting down all on the same
terminal line. The code is as follows:

#
a = 32 #number to count up to

for i in range (a):
 print i, '\r',

for i in range ((a-1),0,-1):
 print i, '\r',

#

It works as desired. However, I was trying to figure out a way to make
it more concise but cannot see a way since the 'range' parameters must
be integers (no functions allowed?).



Parameters to range can be anything which evaluates to integers, but
I'm not sure how that will help you. Also, in Python 2 xrange is a
little more efficient than range.

How's this?

a = 32
counts = (xrange(a), xrange(a-1, 0, -1))
for counter in counts:
 for i in counter:
 print i, '\r'


BUT I'm not sure why you are worried about making it more concise when
your code doesn't do what you want, as far as I can tell. You want the
counter to be written on the same line, not 640 thousand lines, but when
I try it, I get each number written to a different line.


That's probably because you've dropped the trailing comma that the OP 
used in the print statements.




Try this instead:

import sys
a = 32
counts = (xrange(a), xrange(a-1, 0, -1))
for counter in counts:
 for i in counter:
 sys.stdout.write(str(i) + '\r')
 sys.stdout.flush()





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


Re: [Tutor] trivial simple program..can it be made more concise?

2015-02-14 Thread Steven D'Aprano
On Sat, Feb 14, 2015 at 06:40:56AM -0500, Dave Angel wrote:
 On 02/14/2015 04:07 AM, Steven D'Aprano wrote:
 On Sat, Feb 14, 2015 at 03:17:28AM +, steve10br...@comcast.net wrote:

[...]
 for i in range (a):
  print i, '\r',
[...]

 BUT I'm not sure why you are worried about making it more concise when
 your code doesn't do what you want, as far as I can tell. You want the
 counter to be written on the same line, not 640 thousand lines, but when
 I try it, I get each number written to a different line.
 
 That's probably because you've dropped the trailing comma that the OP 
 used in the print statements.

So I did :-(

Have I mentioned recently just how awesome Python 3's print is?

for i in range(10):
print(i, end='\r')

Much nicer :-)


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


Re: [Tutor] sql-like join on two lists or dictionaries

2015-02-14 Thread Joel Goldstick
On Sat, Feb 14, 2015 at 8:03 AM, Alan Gauld alan.ga...@btinternet.com
wrote:

 On 14/02/15 09:55, Peter Otten wrote:

  with open(headerfile) as f:
  lookup_header = {
  headerdata[:6]: headerdata.rstrip(\n) for headerdata in f}

 Then you can iterate over the lines in linefile, extract the key from that
 and look it up in the dict:

 with open(linefile) as lines, open(hl.dat, w) as joined:
  for line in lines:
  try:
  header = lookup_header[line[:6]]
  except KeyError:
  header = 
  print(line.rstrip(\n), header, sep=, file=joined)


 The try/except could be written more concisely as

 header = lookup_header.get(line[:6], )


 HTH
 --
 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

 You can dispense with the slicing if you use the str.split() method.  It
 will put each item in a list.

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




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


Re: [Tutor] trivial simple program..can it be made more concise?

2015-02-14 Thread Dave Angel

On 02/14/2015 07:51 AM, Steven D'Aprano wrote:

On Sat, Feb 14, 2015 at 06:40:56AM -0500, Dave Angel wrote:

On 02/14/2015 04:07 AM, Steven D'Aprano wrote:

On Sat, Feb 14, 2015 at 03:17:28AM +, steve10br...@comcast.net wrote:


[...]

for i in range (a):
 print i, '\r',

[...]


BUT I'm not sure why you are worried about making it more concise when
your code doesn't do what you want, as far as I can tell. You want the
counter to be written on the same line, not 640 thousand lines, but when
I try it, I get each number written to a different line.


That's probably because you've dropped the trailing comma that the OP
used in the print statements.


So I did :-(

Have I mentioned recently just how awesome Python 3's print is?

for i in range(10):
 print(i, end='\r')

Much nicer :-)




Agreed.  But don't you need  \r so when you're counting down you don't 
have the trailing crud?  The space was implicit on Python 2, because it 
mistakenly thought the code was printing two elements.



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


[Tutor] Mobile apps in Python

2015-02-14 Thread Unee0x
I am a fairly new student of Python, and so far I've been impressed with the 
speed,ease and comfort of it. In the near future, I would like to build 3d 
graphical mobile games.
Is there any platform out there that would allow me to build 3d mobile apps 
using the Python language exclusively?

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


Re: [Tutor] Mobile apps in Python

2015-02-14 Thread Steven D'Aprano
On Sat, Feb 14, 2015 at 05:12:59PM -0500, Unee0x wrote:

 I am a fairly new student of Python, and so far I've been impressed 
 with the speed,ease and comfort of it. In the near future, I would 
 like to build 3d graphical mobile games. Is there any platform out 
 there that would allow me to build 3d mobile apps using the Python 
 language exclusively?

Oh, if only there were a website where you could type in simple queries 
and have the website search the entire Internet for an answer!

https://duckduckgo.com/html/?q=how+to+search+the+internet
http://www.google.com.au/search?q=how+to+search+the+internet

*wink*

If you search for Python mobile apps, e.g. using this:

https://startpage.com/do/search/?q=Python+mobile+apps

then the top search result that comes up is for Kivy:

http://kivy.org/

which is probably the most popular of the mobile app frameworks for 
Python. Searching for 3D apps:

http://www.bing.com/search?q=python+3d+mobile+apps

also suggests PyMob and QPython.


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


Re: [Tutor] trivial simple program..can it be made more concise?

2015-02-14 Thread Steven D'Aprano
On Sat, Feb 14, 2015 at 03:17:28AM +, steve10br...@comcast.net wrote:
 Hi all, 
 
 I was playing with Python tonight and created a simple program that 
 outputs numbers counting up then counting down all on the same 
 terminal line. The code is as follows:
 
 # 
 a = 32 #number to count up to 
 
 for i in range (a): 
 print i, '\r', 
 
 for i in range ((a-1),0,-1): 
 print i, '\r', 
 
 # 
 
 It works as desired. However, I was trying to figure out a way to make 
 it more concise but cannot see a way since the 'range' parameters must 
 be integers (no functions allowed?).


Parameters to range can be anything which evaluates to integers, but 
I'm not sure how that will help you. Also, in Python 2 xrange is a 
little more efficient than range.

How's this?

a = 32
counts = (xrange(a), xrange(a-1, 0, -1))
for counter in counts:
for i in counter:
print i, '\r'


BUT I'm not sure why you are worried about making it more concise when 
your code doesn't do what you want, as far as I can tell. You want the 
counter to be written on the same line, not 640 thousand lines, but when 
I try it, I get each number written to a different line.

Try this instead:

import sys
a = 32
counts = (xrange(a), xrange(a-1, 0, -1))
for counter in counts:
for i in counter:
sys.stdout.write(str(i) + '\r')
sys.stdout.flush()


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


[Tutor] sql-like join on two lists or dictionaries

2015-02-14 Thread street . sweeper
Hello all,

Basically what I have here is header and line data for sales or purchase
orders, and I'm trying to do a sql-like join to bring them together
(which ultimately I did because I couldn't figure this out :)).  I've
managed to get the files into python using string slicing, that's not a
problem.

headers - h.dat

B134542Bob  ZQ775235
B875432Joe  ZQ987656
B567943SteveZQ256222

lines - l.dat

B134542   112342   0012
B134542   176542   0001
B875732   765420003
B567943   654565   0001
B567943   900011   0001

desired result - hl.dat

B134542   112342   0012BobZQ775235
B134542   176542   0001BobZQ775235
B875732   765420003JoeZQ987656
B567943   654565   0001Steve  ZQ256222
B567943   900011   0001Steve  ZQ256222



in python3 on linux:

#!/usr/bin/env python3

import os

basepath=os.path.join(os.path.expanduser('~'),'temp',)
linefile=os.path.join(basepath,'l.dat')
headerfile=os.path.join(basepath,'h.dat')

with open(headerfile) as h, open(linefile) as l:
  lines = l.readlines()
  headers = h.readlines()

llist = [[linedata[0:7],
  linedata[14:23],
  linedata[23:27]] for linedata in lines]

hlist = [[headerdata[0:7],
  headerdata[11:19],
  headerdata[19:28]] for headerdata in headers]

ldict = [{linedata[0:7]:
  [linedata[14:23],
   linedata[23:27]]} for linedata in lines]

hdict = [{headerdata[0:7]:
  [headerdata[11:19],
   headerdata[19:28]]} for headerdata in headers]

# :)

quit()



Details on the data are that it's a one or many lines to one header
relationship, at least one of each will exist in each file, and
performance probably isn't an issue as it will only be a few tens to
about 100 lines maximum in the lines file.  The match string will be the
0:7 slice.

You can probably guess my questions: should I be making lists or
dictionaries out of this data, and then of course, what should I do with
them to arrive at the combined file?  I saw some examples of joining two
two-item lists, or dictionaries with a single string as the value, but I
couldn't seem to adapt them to what I'm doing here. I also ran across
the dict.extend method, but looking at the result, I didn't think that
was going to go anywhere, particularly with the one to many
headers:lines relationship.

After a while I pulled this into a sqlite file in memory and did the
join.  Using writelines I think I'll be able to get it out to a file,
but it seems to me that there's probably a way to do this without
resorting to sql.  Or is there?

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