Python Programming
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me

The Shed is going Social! Join us on FaceBook and Twitter and chime in on the conversation.

Go Back   Dev Shed ForumsProgramming LanguagesPython Programming

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
  #1  
Old January 2nd, 2013, 02:52 PM
TLurky109 TLurky109 is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jan 2013
Posts: 2 TLurky109 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
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

Reply With Quote
  #2  
Old January 2nd, 2013, 03:26 PM
b49P23TIvg's Avatar
b49P23TIvg b49P23TIvg is offline
Contributing User
Dev Shed Loyal (3000 - 3499 posts)
 
Join Date: Aug 2011
Posts: 3,372 b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level) 
Time spent in forums: 1 Month 2 Weeks 3 Days 11 h 39 m 38 sec
Reputation Power: 383
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!

Reply With Quote
  #3  
Old January 2nd, 2013, 03:28 PM
Dietrich's Avatar
Dietrich Dietrich is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Feb 2005
Posts: 483 Dietrich User rank is Second Lieutenant (5000 - 10000 Reputation Level)Dietrich User rank is Second Lieutenant (5000 - 10000 Reputation Level)Dietrich User rank is Second Lieutenant (5000 - 10000 Reputation Level)Dietrich User rank is Second Lieutenant (5000 - 10000 Reputation Level)Dietrich User rank is Second Lieutenant (5000 - 10000 Reputation Level)Dietrich User rank is Second Lieutenant (5000 - 10000 Reputation Level)Dietrich User rank is Second Lieutenant (5000 - 10000 Reputation Level) 
Time spent in forums: 3 Days 22 h 51 m 26 sec
Reputation Power: 63
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.

Reply With Quote
  #4  
Old January 6th, 2013, 04:17 PM
TLurky109 TLurky109 is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jan 2013
Posts: 2 TLurky109 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
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,

Reply With Quote
Reply

Viewing: Dev Shed ForumsProgramming LanguagesPython Programming > Newbie: Overlay data from one thing unto another?

Developer Shed Advertisers and Affiliates



Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump

Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 


Powered by: vBulletin Version 3.0.5
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.

© 2003-2013 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap