The Shed is going Social! Join us on FaceBook and Twitter and chime in on the conversation.
|
 |
|
Dev Shed Forums
> Programming Languages
> Python Programming
|
Newbie: Overlay data from one thing unto another?
Discuss Newbie: Overlay data from one thing unto another? in the Python Programming forum on Dev Shed. Newbie: Overlay data from one thing unto another? Python Programming forum discussing coding techniques, tips and tricks, and Zope related information. Python was designed from the ground up to be a completely object-oriented programming language.
|
|
 |
|
|
|
|

Dev Shed Forums Sponsor:
|
|
|

January 2nd, 2013, 02:52 PM
|
|
Registered User
|
|
Join Date: Jan 2013
Posts: 2
Time spent in forums: 31 m 23 sec
Reputation Power: 0
|
|
|
Newbie: Overlay data from one thing unto another?
Yup, newbie to both python and the forums. Normally, this would not be a problem with something like C, but I'm kinda confused on how the supposed auto iteration works with python. I'm starting with the calendar module and using 'calendar.monthcalendar(Year, Month)'. Pretty simple as to what this outputs, but I noticed that march of 2013 has an extra week of almost all zeros so its actually (6x7=42). I'd suspect this might happen again in the future, so I want to make sure my output (text or otherwise) is always 6x7 no matter what.
What I want is to overlay the data from 'calendar.monthcalendar(Year, Month)' unto a pre-created variable like so:
Mdata = [0,0,0,0,0,0,0], [0,0,0,0,0,0,0], [0,0,0,0,0,0,0], [0,0,0,0,0,0,0], [0,0,0,0,0,0,0], [0,0,0,0,0,0,0]
..so that when I print/output Mdata, it always has a 6x7 block of data regardless of how many weeks/days are in the month. My intent here is use this data with libreoffice calc to create a mini calender book with many years. I need to get this stuff right before I actually print it. I've some other things in mind I will be using this data for as well.
Currently, I'm seeing errors about tuples or mismatched something or other... The indenting is getting on my nerves as well.
Any suggestions or a good read on WHAT I need to focus on? There's something about python I'm not able to wrap my head around.
Notice I didn't ASK for a direct answer
Any assistance appreciated...
Thanks
|

January 2nd, 2013, 03:26 PM
|
 |
Contributing User
|
|
|
|
If you edit python code with emacs in python mode C-j (control-j) to enter a new line almost always indents correctly. If not, hit the tab key until the line is in the correct position. The indentation need not be so troublesome.
Mdata = [0,0,0,0,0,0,0], [0,0,0,0,0,0,0], [0,0,0,0,0,0,0], [0,0,0,0,0,0,0], [0,0,0,0,0,0,0], [0,0,0,0,0,0,0]
Mdata is a tuple. tuples are immutable.
You can change the entries in each list, these are mutable.
Mdata[2][2] = 'Wednesday, Meet after school'
would be valid.
Mdata[2] = 'anything' # invalid
Here, Mdata is a list:
Code:
>>> Mdata = [[0]*7 for i in range(6)]
>>> Mdata
[[0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0]]
>>> Mdata[1][3] = 'Wed: meet at secret clubhouse'
>>> Mdata[3]=list('Smtwrfs')
>>> import pprint
>>> pprint.pprint(Mdata)
[[0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 'Wed: meet at secret clubhouse', 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0],
['S', 'm', 't', 'w', 'r', 'f', 's'],
[0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0]]
>>>
__________________
[code] Code tags[/code] are essential for python code!
|

January 2nd, 2013, 03:28 PM
|
 |
Contributing User
|
|
|
|
You might have something like this in mind:
Code:
# allows Python27 to use Python3 print() options
from __future__ import print_function
import calendar
days = ['Mon', 'Tue', 'Wed', 'Thur', 'Fri', 'Sat', 'Sun']
Year = 2013
Month = 1
# gives a list of lists (matrix)
# each sublist is a week (unused days are zero)
calendar_list = calendar.monthcalendar(Year, Month)
space = " "
sf = "%3s "
# optional day header
for day in days:
print(sf % day, end="")
for week in calendar_list:
print("")
for day in week:
# unused days are zero, convert to space
if day == 0:
day = space
print(sf % day, end="")
print("")
''' my result >>>
Mon Tue Wed Thur Fri Sat Sun
1 2 3 4 5 6
7 8 9 10 11 12 13
14 15 16 17 18 19 20
21 22 23 24 25 26 27
28 29 30 31
'''
Actually, the tuple of calendar_list might be what you want:
Code:
import calendar
import pprint
Year = 2013
Month = 1 # January=1 etc.
# gives a list of sublists (matrix)
# each sublist is a week (unused days are zero)
calendar_list = calendar.monthcalendar(Year, Month)
pprint.pprint(calendar_list)
''' my result -->
[[0, 1, 2, 3, 4, 5, 6],
[7, 8, 9, 10, 11, 12, 13],
[14, 15, 16, 17, 18, 19, 20],
[21, 22, 23, 24, 25, 26, 27],
[28, 29, 30, 31, 0, 0, 0]]
'''
# the way you show Mdata is a tuple of sublists
# the outer () is optional
pprint.pprint(tuple(calendar_list))
''' my result -->
([0, 1, 2, 3, 4, 5, 6],
[7, 8, 9, 10, 11, 12, 13],
[14, 15, 16, 17, 18, 19, 20],
[21, 22, 23, 24, 25, 26, 27],
[28, 29, 30, 31, 0, 0, 0])
'''
Somewhat simpler if you just want a string :
Code:
import calendar
year = 2013
month = 1 # January=1 etc.
# assigns a given month's calendar to a multiline string
month_str = calendar.month(year, month)
print(month_str)
''' my result >>>
January 2013
Mo Tu We Th Fr Sa Su
1 2 3 4 5 6
7 8 9 10 11 12 13
14 15 16 17 18 19 20
21 22 23 24 25 26 27
28 29 30 31
'''
__________________
Real Programmers always confuse Christmas and Halloween because Oct31 == Dec25
Last edited by Dietrich : January 2nd, 2013 at 04:11 PM.
|

January 6th, 2013, 04:17 PM
|
|
Registered User
|
|
Join Date: Jan 2013
Posts: 2
Time spent in forums: 31 m 23 sec
Reputation Power: 0
|
|
Thanks guys,
I suppose I should have 'actually' read the included help manual before posting  Anyway, I've got what I need now, and after some minor adjustments to libreoffice formulas, I'll be ready to print the calendar book containing the next 6 years. Yep, did indeed double check my results as well. Now to figure out what other kinda crap I can do with python.
I still say there is an initial learning cost to indenting with python. Of course, after seeing numerous "indenting errors" you catch on pretty quick. Haven't looked into Emacs, but Scite seems to do a pretty good job in its own right.
And lastly,
So, I have python on the MS side and can (or will be able to) create some pure console commands to do various jobs. But what if I want to take it with me on a pen-drive? Those other computers aren't likely to have python installed. Any suggestions on the best way to get a pure executable from python? Is 'py2exe' the best route to go on the MS side?
Thanks,
|
Developer Shed Advertisers and Affiliates
| Thread Tools |
Search this Thread |
|
|
|
| Display Modes |
Rate This Thread |
Linear Mode
|
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
|
|