GPWiki.org
GPWiki.org
It is currently Mon May 20, 2013 2:22 am

All times are UTC




Post new topic Reply to topic  [ 9 posts ] 
Author Message
PostPosted: Thu Apr 02, 2009 10:52 am 
Game Programming Guru

Joined: Thu Jun 08, 2006 7:48 pm
Posts: 1248
Location: Scotland
basically if given a string "Mon-Thu" i'd like to get a list ["Mon", "Tue", "Wed", "Thu"]. The only days are Mon, Tue, Wed, Thu, and Fri. And the ranges will never wrap around over the weekend.

Is there a neat little pythony way of doing this?

Thanks


Top
 Profile  
 
 Post subject:
PostPosted: Thu Apr 02, 2009 11:13 am 
Corpse Bride
User avatar

Joined: Tue Jul 01, 2008 11:44 pm
Posts: 2216
Location: England
Is this just cutting up a string to create a list?

In basic, one would use mid$(), and sample letter triples from the source string and file them into an array.

Code:
source$="MonTueWedThuFri"
for i=1 to 5
   list(i) = mid$(source$, 3*i-2, 3)
next


I'm sure python has similar string functions

_________________
I ain't pushing no moon buttons.


Top
 Profile  
 
 Post subject:
PostPosted: Thu Apr 02, 2009 1:22 pm 
Corpse Bride
User avatar

Joined: Tue Jul 01, 2008 11:44 pm
Posts: 2216
Location: England
For example, you can use this string function:

Code:
str.split([sep[, maxsplit]])

sep is the delimiter string.
maxsplit is optional, determines the maximum number of string splits.

Example,

Code:
'Mon-Tue-Wed-Thu-Fri'.split('-')


returns a list

Code:
['Mon', 'Tue', 'Wed', 'Thu', 'Fri']

_________________
I ain't pushing no moon buttons.


Top
 Profile  
 
 Post subject:
PostPosted: Thu Apr 02, 2009 6:16 pm 
Bibliotherapist
User avatar

Joined: Wed Dec 21, 2005 6:23 pm
Posts: 6210
Location: Manchester, UK
I think jamie actually wants a day-range, so it would split just the string 'Mon-Thurs' into the list ['Mon', 'Tues', 'Weds', 'Thurs'].

I don't think there is a nice way of doing it in python. I'd suggest writing one and submitting it to a python library site, as it can then be expanded and possibly included into a nice advanced python date library at some point ;)

Something like Chronic for python at some point in the future maybe?
http://www.rubyinside.com/chronic-natur ... y-229.html

_________________
God must love stupid people, he made so many.
theraje: 'God doesn't love stupid people, they're just easier to make'
http://sharedillusions.blogspot.com


Top
 Profile  
 
 Post subject:
PostPosted: Thu Apr 02, 2009 6:30 pm 
Technomaniac
User avatar

Joined: Mon Aug 23, 2004 12:24 am
Posts: 3096
Location: Washington , USA
Well, if you had some constant array "weekDays" that contains ['Sun','Mon', ..., 'Sat'], you could:

1) Split the string into beginDay and endDay.
2) Walk the list until you find beginDay, add to your results.
3) Continue walking/pushing until you reach endDay.

=X

Sounds inefficient. I don't know Python.

You could always make it constant:

Code:
if str == 'Sun-Mon':
    result = ['Sun', 'Mon']
elif str == 'Sun-Tue'
    result = ['Sun', 'Mon', 'Tue']
elif
    ...


:P

_________________
GMan
Black Ninja Games
Imagine ASCII art here!


Top
 Profile  
 
 Post subject:
PostPosted: Thu Apr 02, 2009 6:35 pm 
Corpse Bride
User avatar

Joined: Tue Jul 01, 2008 11:44 pm
Posts: 2216
Location: England
workmad3 wrote:
I think jamie actually wants a day-range, so it would split just the string 'Mon-Thurs' into the list ['Mon', 'Tues', 'Weds', 'Thurs'].

I don't think there is a nice way of doing it in python. I'd suggest writing one and submitting it to a python library site, as it can then be expanded and possibly included into a nice advanced python date library at some point ;)


oh I see... 'mon-thu' is the string, not jamie's shorthand :)

Well it sounds too specific to have it's own function. You could perhaps make a function to crop the full list.

sourcestr ='mon-tue-wed-thu-fri'

1. Use sourcestr.split() to take the source string to a source list
= ['mon', 'tue', 'wed', 'thu', 'fri]

2. then create a subsequence of the source list from elements i to j
sourcestr.split()[i:j]

3. we need to determine i and j using
str.find(sub[, start[, end]])


i = sourcestr.split().find('mon')
j = sourcestr.split().find('thu')

So
I don't know how to define the python function, but would this work?

Code:
function dayrange(start, end)

   sourcestr ='mon-tue-wed-thu-fri'

   dayrange = sourcestr.split()[sourcestr.split().find(start) : sourcestr.split().find(end)]

end function



this is a crazy language :rofl

_________________
I ain't pushing no moon buttons.


Top
 Profile  
 
 Post subject:
PostPosted: Thu Apr 02, 2009 7:46 pm 
Babirusa
User avatar

Joined: Thu Aug 19, 2004 2:55 pm
Posts: 9241
Location: The Netherlands
How about making an associated array and simply loop from key $firstday until $endday key?

_________________
Serious game developer

http://www.persistentrealities.com
http://www.persistentrealities.com/vbfibre
http://www.ambiances.nl


Top
 Profile  
 
 Post subject:
PostPosted: Thu Apr 02, 2009 8:51 pm 
Bibliotherapist
User avatar

Joined: Wed Dec 21, 2005 6:23 pm
Posts: 6210
Location: Manchester, UK
After some thought, this is probably the cleverest method I can think of:
Code:
import re
def weekRange(weekString):
    days = ["Mon", "Tues", "Weds", "Thurs", "Fri"]
    (start, end) = re.split("-", weekString, 1)
    first = days.index(start)
    last = days.index(end)
    return days[first:last]

It's still a bit nasty though... and needs a fair bit of extra error checking and maybe even some expansion to make it more versatile :)

_________________
God must love stupid people, he made so many.
theraje: 'God doesn't love stupid people, they're just easier to make'
http://sharedillusions.blogspot.com


Top
 Profile  
 
PostPosted: Tue Dec 13, 2011 4:32 am 
Novice

Joined: Sat Dec 10, 2011 5:37 am
Posts: 6
Here is how I would do it..

day_list = ['Mon', 'Tues', 'Wed', 'Thurs', 'Fri', 'Sat', 'Sun']

def Days(dayrange):
start = day_list.index(dayrange.partition('-')[0])
end = day_list.index(dayrange.partition('-')[2]) + 1
return day_list[start:end]

This is fairly bare bones, and will give you lots of neat errors if you treat it improperly. Obviously .index() will error if no occurrence is found. This will take your input as string, in the form of 'Day1-Day2' and return you a list of the range. I cannot think of a simpler way to do it. However you could skip the 'start' and 'end' declarations to sum it up in 1 line, remember to add '1' to the 'end' index tho.


Top
 Profile  
 
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 9 posts ] 

All times are UTC


Who is online

Users browsing this forum: No registered users and 1 guest


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
Jump to:  
cron
Powered by phpBB® Forum Software © phpBB Group